R encounters fatal error when using run()

10 views
Skip to first unread message

heather...@gmail.com

unread,
Apr 29, 2023, 2:16:36 PM4/29/23
to nimble-users
Hi all,

I'm running a fairly complex model that takes up a lot of memory, and I've been trying to write functions that will do some of the calculations and lighten the memory usage. 

I'm running the model by creating the model, changing the mcmc samplers, compiling the model and then using run(). Everything goes fine until I get to run, at which point R crashes. As you can imagine, this makes finding the problem a little difficult. My suspicion that either my custom sampler or one of my Nimble functions is written incorrectly, but I'm struggling to pinpoint the exact issue. 

I've attached my nimble code and two scripts that hold the functions and a simulation data set for reproducibility. Here's the run code part, which fails on the last line: 

prepbirds <- nimbleModel(code = nimIBM, constants = nc,
                             data = nd, inits = ni, calculate = T, check = T)
    prepbirds$phi.p[,1] <- 1
    prepbirds$initializeInfo()
    prepbirds$calculate()
    mcmcbirds <- configureMCMC(prepbirds, monitors = pars, print = T, control = list(adaptInterval = adaptInterval, scale=.1))
    mcmcbirds$removeSampler(paste0("s[",1:M,", 1:2, 1]"))
 
    for(i in 1:M){
      mcmcbirds$addSampler(target = paste0("s[",i,", 1:2, 1]"),
                           type = 'RW_block', silent=TRUE,
                           control = list(adaptInterval = adaptInterval, propCov=diag(2), scale=SamplerScale, adaptScaleOnly=TRUE))
      for(tt in 2:occ){
        mcmcbirds$addSampler(target = paste0("s[",i,", 1:2, ", tt, "]"),
                             type = 'myRWtrajectoryPotentialSampler',
                             control = list(ind=i, scale=SamplerScale, npix=npix, pxw = pxw,xmin = xmin, ymin = ymin, xmax = xmax, ymax = ymax, gcoords= gcoords, pixMat = pixels))
      }
    }
                               
    birdsMCMC <- buildMCMC(mcmcbirds)
    Cmodel <- compileNimble(prepbirds) #takes a long time (sometimes)
    Compbirds <- compileNimble(birdsMCMC, project = prepbirds)
    Compbirds$run(niter = 2, nburnin = 0, thin = 1, reset = F)

Any tips or thoughts are appreciated! 


IBM_nimble.R
Sim_IBM_Surv1.R
Langevin_Mvmt.R

Perry de Valpine

unread,
May 23, 2023, 12:28:56 PM5/23/23
to heather...@gmail.com, nimble-users
Dear Heather,
I'm very sorry we have not replied on this earlier. I agree it is difficult to track down the problem. Are you still stuck or have you resolved it? In looking at how your code works, my first guess would be that an element of pix[i, t] is getting an invalid value. The reason I'm guessing that is because pix is determined by a user-defined function, getPixLoc, with an input, s[i,1:2,1] that follows a user-defined distribution, dPointProcess or dRWTrajectoryPotential. An invalid value would access an invalid memory location, and such errors can cause intermittent crashing behavior (not necessarily the moment the memory violation occurs), which sounds possibly like what you're seeing. One idea would be to put a manual check in dPointProcess and dRWTrajectoryPotential to see if they are ever encountering (or not rejecting) invalid values. Sometimes even sensible math/logic in determining index values can cause glitches in corner cases or for numerical reasons. This is somewhat of a generic guess so apologies if it's entirely off target.
A related possibility could occur in your custom sampler. I haven't followed all the logic there, but if there is any possible way s.cand (or related values) can be proposed with invalid values, then after you put them in the model and do model$calculate, I think they could lead to accessing values outside the range of valid indices.
I hope that helps. Our development team time is spread thin right now but do let us know if you're still stuck and want further help on this.  Apologies again that your query fell between the cracks.
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/da4b048f-d6cf-481d-8c45-c6ff11ddde9dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages