audrey-b / sims

An R package to simulate data from JAGS or R Code
https://poissonconsulting.github.io/sims/
Other
1 stars 2 forks source link

investigate file handling when in parallel #25

Closed joethorley closed 4 years ago

joethorley commented 4 years ago

library(nlist)
library(future)
plan(multisession)
setwd("C:/Users/audre/Downloads")
dir.create("simstest1")
setwd("C:/Users/audre/Downloads/simstest1")
set.seed(10L)
params <- nlist(sigma = 2)
constants <- nlist(mu = 0)
code <- "for(i in 1:10){
         a[i] ~ dnorm(mu, 1/sigma^2)}"
sims::sims_simulate(code,
                   parameters = params,
                   constants = constants,
                   nsims = 50,
                   save=TRUE,
                   exists = NA,
                   ask=FALSE)
list.files(all.files = TRUE, recursive=TRUE)
setwd("C:/Users/audre/Downloads")
dir.create("simstest2")
setwd("C:/Users/audre/Downloads/simstest2")
set.seed(10L)
params <- nlist(sigma = 2)
constants <- nlist(mu = 0)
code <- "for(i in 1:10){
         a[i] ~ dnorm(mu, 1/sigma^2)}"
sims::sims_simulate(code,
                   parameters = params,
                   constants = constants,
                   nsims = 50,
                   save=TRUE,
                   exists = NA,
                   ask=FALSE)
list.files(all.files = TRUE, recursive=TRUE)```
joethorley commented 4 years ago

From @audrey-b

just wanted to mention that I've encountered problems when running sims_simulate in parallel with plan(multisession). I get "TRUE" which confirms that the code ran without problem but then the files are not always saved in the folder.

It seems to work the first time I run the code, but then if I change my working directory it won't save the results... strange

For some reason, the files get saved in my simanalyse directory instead of my working directory

Maybe the workers don't understand that "." is the working directory when in parallel... (edited)

The working directory wouldn't get passed as argument to the workers... (?)

audrey-b commented 4 years ago

Maybe the following would solve the problem:

normalized_path = normalizePath(path) future_map(path = normalized_path, ...)

This way the worker would use the correct path instead of interpreting "."

joethorley commented 4 years ago

The problem appears to be in furrr (https://github.com/DavisVaughan/furrr/issues/90).

The solution is to specify the folder of interest using the path argument not by using setwd() which is generally considered bad practice http://plantarum.ca/code/setwd/