Failed to create the shared library when compile NIMBLE

51 views
Skip to first unread message

hữu thuần phạm

unread,
Jun 3, 2025, 11:57:28 AM6/3/25
to nimble-users
Hi everyone, I am new to nimble and when I try to run MCMC with my likelihood I have problem with compile. This is my code: 
#define custom likelihood: 
dkl<-nimbleFunction(
  run = function(x = double(0),
               delta = double(2),
               X = double(2),
               Y = double(2),
               log = integer(0, default = 0)) {

  n_x <- dim(X)[1]
  n_y <- dim(Y)[1]
  p <- dim(X)[2]

  sum_x <- 0
  for (i in 1:n_x) {
    row_X <- matrix(X[i, 1:p], nrow = 1)
    plus<-row_X%*%delta
    sum_x<-sum_x+inprod(plus,row_X)
  }

  sum_y <- 0
  for (i in 1:n_y) {
    row_Y <- matrix(Y[i, 1:p], nrow = 1)
    plus<-row_Y%*%delta
    sum_y<-sum_y+inprod(plus,row_Y)
  }

  loglik <- -sum_x / n_x + log(sum_y / n_y)

  returnType(double(0))
  if (log) return(-loglik) else return(exp(-loglik))
}
)
#build model:
model_code<-nimbleCode({
  psi~dunif(0,1)
  #Prior
  for (i in 1:numvar){
    for (j in 1:numvar){
      z[i,j]~dbern(psi)
      delta[i,j]~dnorm(0,sd=10)
      zdelta[i,j]<-z[i,j]*delta[i,j]
    }
  }
 target ~ dkl(delta = zdelta[1:numvar, 1:numvar], X[1:n_x,1:numvar] ,Y[1:n_y,1:numvar])
})
#constant and init
constants<-list(numvar=dim(X)[2],n_x=dim(X)[1],n_y=dim(Y)[1])
Inits <- list(psi = 0.5,
     delta =matrix( rnorm(constants$numvar*constants$numvar),constants$numvar,constants$numvar),
   z = matrix(0,constants$numvar,constants$numvar))
data<-list(X=X,Y=Y)
#registration distribution
registerDistributions(list(
  dkl=list(
    BUGSdist='dkl(delta,X,Y)',
    types=c('value=double(0)','delta=double(2)',
            'X=double(2)','Y=double(2)')
  )
))
#compile model
model<-nimbleModel(code=model_code,constants=constants,inits=Inits,data=data)
conf<-configureMCMC(model)
conf$addMonitors('z')
mcmcRJ<-buildMCMC(conf)
cmodel<-compileNimble(model)
CMCMRJ<-compileNimble(mcmcRJ,project=model)
samples<-runMCMC(CMCMRJ,niter=10000,nburnin=1000)
*******************
and in step cmodel<-compileNimble(model) I received this message: Failed to create the shared library when compile NIMBLE. 
Can anyone know how to fix this problem?

Perry de Valpine

unread,
Jun 3, 2025, 12:20:10 PM6/3/25
to hữu thuần phạm, nimble-users
Hi Pham,
Thanks for the question. I couldn't run your code without having X and Y, so this response is only from reading it.
It looks like you are using inprod(plus,row_X) with both arguments being matrices, but inprod wants vectors as inputs. I am actually not sure what you intend here. If you want the sum of element-wise products of matrices, you could use sum(plus * row_X) (but I am not sure in this case if they have the same dimensions, so I am not sure what you want). A way to get a vector from a matrix is by indexing, e.g. X[i, 1:p] will be a vector. Typing is strict so even if plus is 1-by-n, you would need plus[1, 1:n] or simply plus[1, ] to get a vector. A good way to debug is to run uncompiled and use debug() or put browser() calls in your function (i.e. your nimbleFunction). Unfortunately it can be hard to find problems with matrix vs vector vs scalar in this way because R is forgiving (dynamically typed) but C++ via nimble is not. But nevertheless you can inspect variables within a function call if you have any doubts about their sizes and shapes. If you encounter further errors at the compileNimble step, you can either follow the suggestion about how to print out the C++ compiler messages, or you can first do nimbleOptions(showCompilerOutput=TRUE). The output contains many harmless warnings but there should be any error messages as well. Sometimes one can make a good guess about the source of the problem from the (often mangled) C++ variable names around the error, or you can send it in a new post here. Alternatively you can provide a fully reproducible example, for example using set.seed and creating X and Y, or providing them by file. HTH!
Perry


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/9a5961ba-75c9-4688-b29f-81ce7ed9c91dn%40googlegroups.com.

hữu thuần phạm

unread,
Jun 3, 2025, 12:29:24 PM6/3/25
to nimble-users
Hi Perry,
Thank you for your response, 
X and Y are the 50x9 matrix data I generated from normal distribution. 
plus and row_X are really vector. After update nimble I can run the compile but I have a new problem in samples:
running chain 1... Error in mcmc$run(niter, nburnin = nburnin, thin = thinToUseVec[1], thin2 = thinToUseVec[2], : user-defined distribution dkl provided without random generation function.
I am sending you the file I written. I would be grateful if you could look into it.
Sorry if this is any inconvenience to you.
Thuan
Vào lúc 23:20:10 UTC+7 ngày Thứ Ba, 3 tháng 6, 2025, Perry de Valpine đã viết:
Differential Graph.Rmd
x.RDS
y.RDS

Perry de Valpine

unread,
Jun 3, 2025, 1:05:18 PM6/3/25
to hữu thuần phạm, nimble-users
Ok, here's what I see.
The inprod() calls are fine. I wasn't successfully tracking what the arguments would be from reading the code only. It does handle matrices with one dimension having size 1.
The error you were seeing arose because you had not initialized target. If there is no initial value for a node, then nimble's MCMC system will want to generate one by drawing from the "r" random number function. But that would mean you would need to provide "rkl" so it could do so. To avoid this, provide an init for target, e.g:
Inits <- list(psi = 0.5,
     delta =matrix( rnorm(constants$numvar*constants$numvar),constants$numvar,constants$numvar),
   z = matrix(0,constants$numvar,constants$numvar),
   target = 1) # added by perry

In addition, a user-defined distribution should be used as a stochastic node, i.e. declared with "~" instead of "<-". So please try this:
 target ~ dkl(delta = zdelta[1:numvar, 1:numvar], X[1:n_x,1:numvar] ,Y[1:n_y,1:numvar]) # perry changed from <- to ~
Then when dkl is called, the first argument ("x") will be target. I notice that x is not used within dkl, which is ok but unusual. It means that your "target" node is just a placeholder in order to get the calculations you are doing with delta, X and Y included as a log probability for the model.

Finally, your initial values for X, Y and delta result in the log probability from dkl being Inf. I took a look at this by:

model$calculate('psi') # good
model$calculate('z') # good
model$calculate('delta') #good
model$calculate('target') # returns Inf
debug(dkl)
model$calculate('target') # stepped through, and this returns Inf because sum_y is 0
undebug(dkl)

(Also, is that the negative log probability or the log probability? just checking)

HTH
Perry

hữu thuần phạm

unread,
Jun 3, 2025, 2:19:04 PM6/3/25
to nimble-users
Thank you very much for your support.
I have some mistake.
Now everything is fine.
Thuan

Vào lúc 00:05:18 UTC+7 ngày Thứ Tư, 4 tháng 6, 2025, Perry de Valpine đã viết:
Reply all
Reply to author
Forward
0 new messages