Trying to parallelise an optimization routine using rstan, splitting the data up over separate workers. It works well, but I can’t get it to work as part of a function – any pointers? The following code breaks when it is wrapped in the test function, but works fine if the function definition part is removed.
edit: @bgoodri hope you don’t mind being tagged…
test <- function(){
st='
data{
real myval;
}
model{
target+=myval;
}'
sm <- stan_model(model_code=st)
library(parallel)
cl=makeCluster(2,type='PSOCK')
standat=list(myval=1.2)
smf<-c() #placeholder so we can assign to it from a function
clusterExport(cl,c('smf','standat','sm'))
clusterApply(cl,1:2,function(x) {
library(rstan)
standat$myval <<- x #CHANGE STANDATA FOR EACH PROCESS
smf <<- sampling(object=sm,data=standat,iter=0,chains=0,init=0,check_data=FALSE, #regenerate stanfit object
control=list(max_treedepth=0),save_warmup=FALSE,test_grad=FALSE)
}
)
parLapply(cl,1:2,function(x) log_prob(smf,upars=1.2[c()],adjust_transform=TRUE, gradient=TRUE)) #get log prob for each process
}
test()