donaldRwilliams / chkptstanr

Checkpoint Stan R
https://donaldrwilliams.github.io/chkptstanr/
Other
9 stars 5 forks source link

model_threads.exe not found on ubuntu / mac #3

Closed julianquandt closed 2 years ago

julianquandt commented 2 years ago

Hi,

First of all, this is great work! Thank you so much, I have been looking forward for something like this for a long time.

I have, however, encountered a problem:

I tried running the function on unbuntu and encountered the problem that I cannot continue sampling after stopping when I run the code from the vignette and stopping after e.g. 4 checkpoints:

library(chkptstanr)
library(brms)

path <- create_folder(folder_name = "chkpt_folder_m2")

fit_m2 <- chkpt_brms(
  bf(formula = count ~ zAge + zBase,
     family = poisson()),
  data = epilepsy,
  path  = path,
  iter_warmup = 1000,
  iter_sampling = 1000,
  iter_per_chkpt = 250,
)
#Initial Warmup (Typical Set)
#Chkpt: 1 / 8; Iteration: 250 / 2000 (warmup)
#Chkpt: 2 / 8; Iteration: 500 / 2000 (warmup)
#Chkpt: 3 / 8; Iteration: 750 / 2000 (warmup)
#Chkpt: 4 / 8; Iteration: 1000 / 2000 (warmup)

I get the following error on trying to restart:

Error in cmdstanr::cmdstan_model(stan_file = stan_code_path, cpp_options = list(stan_threads = TRUE)) : 
  object 'stan_code_path' not found

I did some digging and it seems that while isFALSE(check_for_model("model.stan", path)) does find the model file and returns FALSE thereby skips to recompile the model as it should, isFALSE(check_for_model("model_threads.exe", path)) returns TRUE and tries to compile a model without having the stan_code_path available.

The problem seems to be that on ubuntu the threads executable is saved as model_threads instead of model_threads.exe. Renaming the file does also not solve the problem as the reference path in comp$exe_file in comp.rds is still referencing the model_threads file without the .exe .

In debug mode, renaming the file to model_threads.exe before the execution of

  if (isFALSE(check_for_model("model_threads.exe", path))) {
    stan_m3 <- cmdstanr::cmdstan_model(stan_file = stan_code_path, 
      cpp_options = list(stan_threads = TRUE))
    saveRDS(stan_m3, file = paste0(path, "/stan_model/comp.rds"))
    saveRDS(args, paste0(path, "/stan_model/args.rds"))
  }

when starting to resample, and renaming it back to model_threads afterwards solved the problem for me. Maybe checking the operating system and setting the file name in the block above accordingly could solve this?

I changed the code above to the following. It worked for me on linux and should also work on mac:

  model_threads_name <- ifelse(.Platform$OS.type == "unix", "model_threads", "model_threads.exe")
  if (isFALSE(check_for_model(eval(model_threads_name), path))) {
    stan_m3 <- cmdstanr::cmdstan_model(stan_file = stan_code_path,
      cpp_options = list(stan_threads = TRUE))
    saveRDS(stan_m3, file = paste0(path, "/stan_model/comp.rds"))
    saveRDS(args, paste0(path, "/stan_model/args.rds"))
  } 

I hope this helps. Again - Thanks for the great work!

Cheers, Julian

donaldRwilliams commented 2 years ago

Thanks for this !

I will add that and then check that it works on windows !!

donaldRwilliams commented 2 years ago

@julianquandt I just added that to package. new version was just submitted to CRAN !!

Thank you !