Error: Failed to create the shared library" compiler error

2,476 views
Skip to first unread message

alireza beheshty

unread,
Apr 9, 2021, 6:01:46 AM4/9/21
to nimble-users
HI
I've written a nimbleFunction but when I try to compile the model code, I get a "Failed to create the shared library" error. Here's the code:

R version 4.0.3 & 4.0.4

---------------------------------------------
library(spdep)

data("baltimore")

str(baltimore)

N <- S <- 211

## pairwise matrix of distance

 distMat <- matrix(nrow = nrow(baltimore), ncol = nrow(baltimore))

for (i in 1:nrow(distMat)) {
  for (j in i:nrow(distMat)) {
    distMat[i, j] <- distMat[j, i] <-
      sqrt(sum((baltimore[i, c("X", "Y")] - baltimore[j, c("X", "Y")]))^2)
      }
       }
## normalization
  distMat <- distMat / max(distMat) * 10


library(nimble)
SLMMCode <- nimbleCode({
  for (i in 1:S) {
    y[i] ~ dnorm(mu_y[i], tau = tau_y)
    mu_y[i] <- b[i, 1] * x1[i] + b[i, 2] * x2[i] +
      b[i, 3] * x3[i] + b[i, 4] * x4[i] + b[i, 5] * x5[i] +
      b[i, 6] * x6[i] + b[i, 7] * x7[i] + b[i, 8] * x8[i] + 
      b[i, 9] * x9[i] + b[i, 10]* x10[i] + b[i,11] * x11[i] +
      b[i,12] * x12[i]+ b[i,13] * x13[i] + W[i]
    
    b[i, 1:13] <- bm[latent[i], 1:13]
    
    latent[i] ~ dcat(zlatent[1:M])
  }
  
  
  for (j in 1:S) {
    for (k in 1:S) {
      H[j, k] <- exp(-Dist[j, k]/phi)
    }
  }
  
  W[1:S] ~ dmnorm(mu_w[1:S], prec = prec_W[1:S, 1:S])
  prec_W[1:S, 1:S] <- tau_w * inverse(H[1:S, 1:S])
  
  phi ~ dunif(0, D)
  tau_w ~ dgamma(1, 1)
  
  mu_w[1:S] <- rep(0, S)
  
  for (k in 1:M) {
    bm[k, 1:13] ~ dmnorm(mu_bm[1:13], cov = var_bm[1:13, 1:13])
  }
 
 var_bm[1:13, 1:13] <- 1/tau_bm * diag(rep(1, 13))

  tau_bm ~ dgamma(1, 1)
  
  for (j in 1:13) {
    mu_bm[j] ~ dnorm(0, 1)
  }
  
  zlatent[1:M] <- stick_breaking(vlatent[1:(M - 1)])
  
  for (j in 1:(M - 1)) {
    vlatent[j] ~ dbeta(1, alpha)
  }
  
  alpha ~ dgamma(1, 1)
  tau_y ~ dgamma(1, 1)

})

 SLMMdata <- list(y = baltimore$PRICE, x1 = baltimore$NROOM,
                  x2 = baltimore$DWELL,x3 = baltimore$NBATH,
                  x4 = baltimore$PATIO,x5 = baltimore$FIREPL,
                  x6 =baltimore$AC,    x7 = baltimore$BMENT,
                  x8 =baltimore$NSTOR, x9 = baltimore$GAR,
                  x10 =baltimore$AGE,  x11=baltimore$CITCOU,
                  x12 =baltimore$LOTSZ,x13=baltimore$SQFT,
                  Dist = distMat)
  SLMMConsts <- list(S = 211, M = 50, D = 100)
  SLMMInits <- list(tau_y = 1,
                   latent = rep(1, SLMMConsts$S),
                   alpha = 2,
                   tau_bm = 1,
                   mu_bm = rnorm(13),
                   phi = 1,
                   tau_w = 1,
                   vlatent = rbeta(SLMMConsts$M - 1, 1, 1))
 
  mcmc.out <- nimbleMCMC(code = SLMMCode, data = SLMMdata,
                        constants = SLMMConsts,
                        inits = SLMMInits,
                        monitors = c("bm","b","phi", "tau_w",
                                     "alpha", "latent", "tau_y"),
                        niter = 50000,
                        thin = 10, nchains = 1, setSeed = TRUE)


defining model...
building model...
setting data and initial values...
running calculate on model (any error reports that follow may simply reflect missing values in model variables) ... Error in solve.default(x) : 
  Lapack routine dgesv: system is exactly singular: U[49,49] = 0

checking model sizes and dimensions... This model is not fully initialized. This is not an error. To see which variables are not initialized, use model$initializeInfo(). For more information on model initialization, see help(modelInitialization).
checking model calculations...
Error in solve.default(x) : 
  Lapack routine dgesv: system is exactly singular: U[49,49] = 0
Error in solve.default(x) : 
  Lapack routine dgesv: system is exactly singular: U[49,49] = 0
Note: cannot calculate logProb for node  prec_W[1:211, 1:211] .
Error in chol.default(model$prec_W[1:211, 1:211]) : 
  the leading minor of order 1 is not positive definite
Note: cannot calculate logProb for node  lifted_chol_oPprec_W_oB1to211_comma_1to211_cB_cP[1:211, 1:211] .
NAs were detected in model variables: prec_W, bm, logProb_bm, lifted_chol_oPprec_W_oB1to211_comma_1to211_cB_cP, W, logProb_W, b, mu_y, logProb_y.
model building finished.
compiling... this may take a minute. Use 'showCompilerOutput = TRUE' to see C++ compilation details.
Error: Failed to create the shared library. Run 'printErrors()' to see the compilation errors.

THANKS

Perry de Valpine

unread,
Apr 9, 2021, 6:18:07 PM4/9/21
to alireza beheshty, nimble-users
Hi Alireza,

Thanks for the message.  Your code works for me!

Have you updated R and/or nimble and/or your C++ compiler tools recently?  Sometimes one needs to take an extra step after updating compiler tools.  In OS X, sometimes one needs to launch Xcode outside of R to give it permission.  Also one needs command line tools ("xcode-select --install").  On Windows, there is a relatively recent extra procedure for setting up the PATH environment variable: https://cran.r-project.org/bin/windows/Rtools/.

You can check if your compiler setup is working by a simple test like this:
foo <- nimbleFunction( run = function(x = double(1)) {return(sum(x)); returnType(double())})
cfoo <- compileNimble(foo)
cfoo(1:10)

If it fails, please follow the instructions by entering printErrors() to see what happened, and send the result if you're still stuck.  An alternative is to use "showCompilerOutput = TRUE" in compileNimble, but know that that can generate lots of harmless warnings.

If the compiler works, please try the component steps of nimbleMCMC like this:

SLMMmodel <- nimbleModel(SLMMCode, data = SLMMdata,
                        constants = SLMMConsts,
                        inits = SLMMInits)

cSLMMmodel <- compileNimble(SLMMmodel)
MCMC <- buildMCMC(cSLMMmodel)
cMCMC <- compileNimble(MCMC, project = SLMMmodel)
samples <- runMCMC(cMCMC, niter = 1000)

I hope something works out.

-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 on the web visit https://groups.google.com/d/msgid/nimble-users/40277b46-e85a-4d3b-bbbc-22d077b483a5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages