# CmdStanPy and multithreading

**URL:** <https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038>\
**Category:** Modeling\
**Created:** [July 27, 2020, 8:57pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038 "2020-07-27T20:57:59Z")\
**Posts on this page:** 11\
**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, 8:58pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/1 "2020-07-27T20:58:00Z")

</div>

I am trying to compile a CmdStanPy model that uses multithreading with `reduce_sum`, but I have no idea what arguments to put into the compiling options and into the python code itself. All tutorials and forum answers on this topic say “do it the same way you do it with CmdStan”, which is not very helpful. In the minimal example ([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 implementation is in R, so I guess I am looking for the CmdStanPy equivalent for `cpp_options = list(stan_threads = TRUE)`. Any help will be appreciated!

---

<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:35pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/2 "2020-07-27T22:35:02Z")

</div>

answered in earlier thread -

> [@Multithreading with map\_rect takes more time](https://discourse.mc-stan.org/t/multithreading-with-map-rect-takes-more-time/17019/8):
>
> When instantiating a CmdStanModel object, you can specify argument `cpp_options` as a `dict` with entries `{ 'STAN_THREADS' : True }` , e,g:
> 
> ```no-highlight
> logistic_model = CmdStanModel(stan_file=logistic_stan, cpp_options={ 'STAN_TH
> 
> ```

---

<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 28, 2020, 6:49am UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/3 "2020-07-28T06:49:57Z")

</div>

Thank you! Something still doesn’t work. I defined the following:

`os.environ['STAN_NUM_THREADS'] = "2"`

`sm = CmdStanModel(stan_file="model.stan", cpp_options={'STAN_THREADS': True})`

`fit = sm.sample(data=data_stan, iter_sampling=300, chains=1, iter_warmup=100, thin=5, seed=101, output_dir="./test/", show_progress=True)`

But I still see only one thread running…

---

<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 28, 2020, 1:49pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/4 "2020-07-28T13:49:00Z")

</div>

not sure what’s going on. could you try to run this model directly via the command line?  
here’s what to do:

the complete command used to run the chain is stored in the `fit` variable (a CmdStanMCMC object) as  
`fit.runset.cmds[0]` - so `' '.join(fit.runset.cmds[0])` shows you how to call this in CmdStan directly.  
you might need to create a local json data file if your model requires input data. you can skip sending the output to the tmp dir - etc.

how many threads to you see running when running this via the shell?

---

<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 28, 2020, 3:23pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/5 "2020-07-28T15:23:40Z")

</div>

First of all – thank you! Second, I am not sure I understand how to grab the `fit` variable after running the model via the shell.  
I compiled my model in pyton using `sm = CmdStanModel(stan_file="model.stan", cpp_options={'STAN_THREADS': True})`  
and then run it via the shell with `./model sample data file=data.json`. All I get is an `output.csv` file.

---

<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 28, 2020, 3:28pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/6 "2020-07-28T15:28:12Z")

</div>

OK, I thought you were doing the sampling via CmdStanPy.  
how are you checking how many threads are running? what OS?  
at this point, asking someone like @bbbales2 to help diagnose this.

---

<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 28, 2020, 3:32pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/7 "2020-07-28T15:32:28Z")

</div>

Sorry for the confusion! I did it via pycharm using CmdStanPy at first, but tried to run it from the shell like you suggested. I am using macOS 10.12.6. To see how many threads are running I am looking at the Activity Monitor.

---

<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 28, 2020, 3:32pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/8 "2020-07-28T15:32:43Z")

</div>

many thanks for reporting this here - it’s extremely useful as this information needs to go into the CmdStan manual - this came up as part of an issue reported here: [https://github.com/stan-dev/docs/issues/234#issuecomment-663611709](https://github.com/stan-dev/docs/issues/234#issuecomment-663611709)

---

<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 28, 2020, 3:36pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/9 "2020-07-28T15:36:09Z")

</div>

> [@nerpa](#):
>
> To see how many threads are running I am looking at the Activity Monitor.

dunno what’s going on here - will try to reproduce on my Macs - got laptops here running High Sierra (10.13.6) and Catalina.

---

<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 28, 2020, 3:36pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/10 "2020-07-28T15:36:57Z")

</div>

ok, so now I understood what you meant in your first reply. So my shell command looks like this:  
`model id=1 random seed=101 data file=data.json output file=model-202007281818-1.csv method=sample num_samples=300 num_warmup=100 thin=5 algorithm=hmc adapt engaged=1`

When I run it via the shell I still saw one thread

---

<div class="post-metadata">

**Author:** ![tbrown122387](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tbrown122387/32/6301_2.png) [@tbrown122387](https://discourse.mc-stan.org/u/tbrown122387)\
**Post date:** [June 27, 2024, 1:22am UTC](https://discourse.mc-stan.org/t/cmdstanpy-and-multithreading/17038/11 "2024-06-27T01:22:36Z")

</div>

Same thing happened to me. I fixed it by adding two things. I fixed this in Python. Didn’t have to open up a terminal.

1. I added `force_compile=True` It’s easy to not notice that it doesn’t always recompile if there’s already an executable there.

So something like this:

`model = CmdStanModel(stan_file=model_code, cpp_options={ 'STAN_THREADS' : True }, force_compile=True)`

1. 

You need to specify `threads_per_chain=` in `.sample()` Something like this maybe:  
`fit = model.sample(data = more_columns_data, threads_per_chain = 20)`

Ends up looking like this on my system monitor:

 ![Screenshot from 2024-06-26 21-23-07](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/1/1/11ba264fa0ef8b758895fb721871bfb67c4f13c7.png)
