# Brm\_multiple & Parallel computation

**URL:** <https://discourse.mc-stan.org/t/brm-multiple-parallel-computation/28729>\
**Category:** Modeling\
**Tags:** brms\
**Created:** [September 2, 2022, 8:29pm UTC](https://discourse.mc-stan.org/t/brm-multiple-parallel-computation/28729 "2022-09-02T20:29:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yunrui](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yunrui/32/13871_2.png) [@Yunrui](https://discourse.mc-stan.org/u/Yunrui)\
**Post date:** [September 2, 2022, 8:29pm UTC](https://discourse.mc-stan.org/t/brm-multiple-parallel-computation/28729/1 "2022-09-02T20:29:14Z")

</div>

If I would like to set the chain of the brms model as 4, and use brm\_multiple function to run the same multi-level model on 10 imputed datasets, and I need run the brm\_multiple function for other different imputed dataset many times, Can I use the brm\_multiple and parallel at same time? For example, I have 48 cores, how to set the CPU is rational?

---

<div class="post-metadata">

**Author:** ![wpetry](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wpetry/32/4852_2.png) [@wpetry](https://discourse.mc-stan.org/u/wpetry)\
**Post date:** [September 26, 2022, 4:27pm UTC](https://discourse.mc-stan.org/t/brm-multiple-parallel-computation/28729/2 "2022-09-26T16:27:57Z")

</div>

There’s an old [issue on GitHub](https://github.com/paul-buerkner/brms/issues/617#issuecomment-471434337) that might point you in the right direction:

> Both `kfold` and `brm_multiple` have future automatically activated to run **models** in parallel, while `brm` can run **chains** in parallel if `future = TRUE`. That is you have two layers of futures on top of each other for the former two functions if you also activate future in brm. I am not sure how **future** handles this situation exactly, but it is definitely built to deal with it (and it’s surely documended somewhere).

I read this to mean that what you want to do is possible, but probably not without some additional effort from you. I would try setting up a [nested topology with the `future` package](https://github.com/HenrikBengtsson/future#nested-futures-and-evaluation-topologies). Your upper layer could make use of 10 cores (1/imputed dataset) with each lower layer using 4 cores (1/chain). This uses 40 cores, leaving 8 idle. Maybe not ideal, but at least a step in the right direction. I think you may need recreate some of the behavior of `brms::brm_multiple` by calling `brms::brm()` for each imputation, then using `brms::combine_models()` to combine the output appropriately given the nested structure of your output.

---

<div class="post-metadata">

**Author:** ![wpetry](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wpetry/32/4852_2.png) [@wpetry](https://discourse.mc-stan.org/u/wpetry)\
**Post date:** [September 26, 2022, 5:59pm UTC](https://discourse.mc-stan.org/t/brm-multiple-parallel-computation/28729/3 "2022-09-26T17:59:56Z")

</div>

[stantargets](https://docs.ropensci.org/stantargets/index.html) also has parallelization capabilities and could be an alternative approach. I don’t have experience with what you’d need to do, except to say that you could export the Stan code from `brms` using the `make_stancode()` function.
