# Multithreading with map\_rect takes more time

**URL:** <https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019>\
**Category:** Modeling\
**Created:** [July 27, 2020, 10:33am UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019 "2020-07-27T10:33:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nerpa](https://avatars.discourse-cdn.com/v4/letter/n/85e7bf/32.png) [@nerpa](https://discourse.mc-stan.org/u/nerpa)\
**Post date:** [July 27, 2020, 10:33am UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/1 "2020-07-27T10:33:44Z")

</div>

Hello all. I implemented multithreading with `map_rect` using this example ([https://pystan.readthedocs.io/en/latest/threading\_support.html](https://pystan.readthedocs.io/en/latest/threading_support.html)), but when I fit the same model with the same data with and without `map_rect`, it takes me **10 times longer** to finish sampling with map\_rect. Any ideas why this could be?  
I use pystan 2.18 on mac.

Thank you!

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [July 27, 2020, 4:49pm UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/2 "2020-07-27T16:49:30Z")

</div>

PyStan 2.18 is pretty old and there’s a better version of map\_rect called “reduce\_sum” that was added to Stan 2.23 - [https://mc-stan.org/users/documentation/case-studies/reduce\_sum\_tutorial.html](https://mc-stan.org/users/documentation/case-studies/reduce_sum_tutorial.html)

the CmdStan/Stan 2.24 release is due out today and CmdStanPy’s `install_cmdstan()` function will download and install the latest CmdStan release for you. It does require Python 3. hope this helps - [https://cmdstanpy.readthedocs.io/en/latest/index.html](https://cmdstanpy.readthedocs.io/en/latest/index.html)

---

<div class="post-metadata">

**Author:** ![nerpa](https://avatars.discourse-cdn.com/v4/letter/n/85e7bf/32.png) [@nerpa](https://discourse.mc-stan.org/u/nerpa)\
**Post date:** [July 27, 2020, 5:01pm UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/3 "2020-07-27T17:01:49Z")

</div>

Thanks! Is pystan2.23 release any close? And is it a known issue that `map_rect` does not speed up sampling?  
Update - I use pystan 2.19.1.1 - is it still considered old?

---

<div class="post-metadata">

**Author:** ![nerpa](https://avatars.discourse-cdn.com/v4/letter/n/85e7bf/32.png) [@nerpa](https://discourse.mc-stan.org/u/nerpa)\
**Post date:** [July 27, 2020, 5:07pm UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/4 "2020-07-27T17:07:19Z")

</div>

Oh I just saw that CmdStanPy is a python module! That’s so cool. But I cannot see that it’s available for downloading…

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [July 27, 2020, 5:22pm UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/5 "2020-07-27T17:22:00Z")

</div>

it’s on PyPi - [https://pypi.org/project/cmdstanpy/](https://pypi.org/project/cmdstanpy/)

---

<div class="post-metadata">

**Author:** ![nerpa](https://avatars.discourse-cdn.com/v4/letter/n/85e7bf/32.png) [@nerpa](https://discourse.mc-stan.org/u/nerpa)\
**Post date:** [July 27, 2020, 5:27pm UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/6 "2020-07-27T17:27:14Z")

</div>

Sweet! Thanks so much – downloading rn :)

---

<div class="post-metadata">

**Author:** ![nerpa](https://avatars.discourse-cdn.com/v4/letter/n/85e7bf/32.png) [@nerpa](https://discourse.mc-stan.org/u/nerpa)\
**Post date:** [July 27, 2020, 7:54pm UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/7 "2020-07-27T19:54:29Z")

</div>

What would be the cmdstanpy equivalent to `cpp_options = list(stan_threads = TRUE)` in R to compile a model that uses `partial_sum`? I tried different things, all failed…

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [July 27, 2020, 10:28pm UTC](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/8 "2020-07-27T22:28:30Z")

</div>

When instantiating a CmdStanModel object, you can specify argument `cpp_options` as a `dict` with entries `{ 'STAN_THREADS' : True }`, e,g:

```
logistic_model = CmdStanModel(stan_file=logistic_stan, cpp_options={ 'STAN_THREADS' : True})

```

sorry - the documentation is quite incomplete here - working on it. your feedback welcome!
