# Run multiple stan models in parallel

**URL:** https://discourse.mc-stan.org/t/run-multiple-stan-models-in-parallel/15885
**Category:** Developers
**Tags:** rstan
**Created:** [June 12, 2020, 3:55pm UTC](https://discourse.mc-stan.org/t/run-multiple-stan-models-in-parallel/15885 "2020-06-12T15:55:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kbus007](https://avatars.discourse-cdn.com/v4/letter/k/b9e5f3/32.png) [@Kbus007](https://discourse.mc-stan.org/u/Kbus007)
#### Post date: [June 12, 2020, 3:55pm UTC](https://discourse.mc-stan.org/t/run-multiple-stan-models-in-parallel/15885/1 "2020-06-12T15:55:59Z")

</div>

I am “lucky” to have access to a remote Rstudio server with 80 cores and I would like to use this server to run 5 stan models in parallel (each model runs 4 chains, so I would use 20 cores). I have tried the following:

```R
library(parallel)
tasks <- list(
  fit1<-function() sampling(model1,standata,cores=4),
  fit2<-function() sampling(model2,standata,cores=4),
  fit3<-function() sampling(model3,standata,cores=4),
  fit4<-function() sampling(model4,standata,cores=4),
  fit5<-function() sampling(model5,standata,cores=4)
)

out <- mclapply(
  tasks,
  function(f) f(),
  mc.cores = 20
)

```

When I call `mclapply`, model1 is properly sampled but I get some errors for the other models:

```R
Warning message:
In mclapply(tasks, function(f) f(), mc.cores = 20) :
  scheduled cores 3, 2, 4, 5 encountered errors in user code, all values of the jobs will be affected
Error in socketConnection(master, port = port, blocking = TRUE, open = "a+b", : 
  impossible to open the connection
Calls: <Anonymous> ... tryCatchOne -> doTryCatch -> recvData -> makeSOCKmaster
De plus : There were 17 warnings (use warnings() to see them)
Stopped execution
Error in socketConnection(master, port = port, blocking = TRUE, open = "a+b", : 
  impossible to open the connection
Calls: <Anonymous> ... tryCatchOne -> doTryCatch -> recvData -> makeSOCKmaster
De plus : There were 17 warnings (use warnings() to see them)
Stopped execution
Error in socketConnection(master, port = port, blocking = TRUE, open = "a+b", : 
  impossible to open the connection
Calls: <Anonymous> ... tryCatchOne -> doTryCatch -> recvData -> makeSOCKmaster
De plus : There were 17 warnings (use warnings() to see them)
Stopped execution
Error in socketConnection(master, port = port, blocking = TRUE, open = "a+b", : 
 impossible to open the connection
Calls: <Anonymous> ... tryCatchOne -> doTryCatch -> recvData -> makeSOCKmaster
De plus : There were 17 warnings (use warnings() to see them)
Stopped execution

```

Does anybody know how I can fix this?

Thank you in advance!

---

<div class="post-metadata">

### Author: ![andre.pfeuffer](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@andre.pfeuffer](https://discourse.mc-stan.org/u/andre.pfeuffer)
#### Post date: [June 12, 2020, 6:45pm UTC](https://discourse.mc-stan.org/t/run-multiple-stan-models-in-parallel/15885/2 "2020-06-12T18:45:23Z")

</div>

Why don’t you run 5 R sessions independently?

---

<div class="post-metadata">

### Author: ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)
#### Post date: [June 12, 2020, 6:55pm UTC](https://discourse.mc-stan.org/t/run-multiple-stan-models-in-parallel/15885/3 "2020-06-12T18:55:44Z")

</div>

So far as I know, this isn’t possible with rstan. Let me take a look at what it would take to permit it with [ezStan](https://github.com/mike-lawrence/ezStan); I think it should be just a matter of giving each `stan_temp` folder a unique name…

Oh, though Andre’s suggestion of simply running separate R sessions should for sure work if you can do that.

Edit: I spoke prematurely and this is possible in Rstan with the tweak suggested by Charles below.

---

<div class="post-metadata">

### Author: ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)
#### Post date: [June 12, 2020, 7:07pm UTC](https://discourse.mc-stan.org/t/run-multiple-stan-models-in-parallel/15885/4 "2020-06-12T19:07:35Z")

</div>

you need to initialise the stanfit objects on each worker, via a call something like this:

```
sf<-rstan::sampling(model,iter=0,chains=0,init=0,data=data,check_data=FALSE,
        control=list(max_treedepth=0),save_warmup=FALSE,test_grad=FALSE)
```

---

<div class="post-metadata">

### Author: ![Kbus007](https://avatars.discourse-cdn.com/v4/letter/k/b9e5f3/32.png) [@Kbus007](https://discourse.mc-stan.org/u/Kbus007)
#### Post date: [June 13, 2020, 6:05pm UTC](https://discourse.mc-stan.org/t/run-multiple-stan-models-in-parallel/15885/5 "2020-06-13T18:05:32Z")

</div>

Thank you very much all for your help and suggestions.

@andre.pfeuffer, I cannot (unfortunately) open several R sessions on the remote server.

@mike-lawrence and @Charles_Driver, I tried to initialize the stanfit objects before sampling but it did not help (it might be that I did not call the sampling the right way…)

Finally, I tried to use `parLapply` instead of `mclapply` and it did work!  
For readers who might be interested to run in parallel stan models on the same dataset, here is the code:

```R
samplingfunction<-function(x){
  if (x==1) res<-rstan::sampling(model1,data=standata,cores=4)
  else if (x==2) res<-rstan::sampling(model2,data=standata,cores=4)
  else if (x==3) res<-rstan::sampling(model3,data=standata,cores=4)
  else if (x==4) res<-rstan::sampling(model4,data=standata,cores=4)
  else if (x==5) res<-rstan::sampling(model5,data=standata,cores=4)
}

cl <- makeCluster(20)
clusterExport(cl,c('model1','model2','model3','model4','model5','standata'))
out <- parLapply(cl, c(1:5),samplingfunction)

```
