# Cmdstanpy: multithreading issues (threads\_per\_chain)

**URL:** <https://discourse.mc-stan.org/t/cmdstanpy-multithreading-issues-threads-per-chain/33572>\
**Category:** CmdStan\
**Tags:** cmdstanpy\
**Created:** [December 13, 2023, 1:30pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-multithreading-issues-threads-per-chain/33572 "2023-12-13T13:30:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![annelies](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@annelies](https://discourse.mc-stan.org/u/annelies)\
**Post date:** [December 13, 2023, 1:30pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-multithreading-issues-threads-per-chain/33572/1 "2023-12-13T13:30:39Z")

</div>

Hi all,

I’ve been trying to run a stan model using cmdstanpy (it’s my first time using cmdstanpy) with 4 chains of 16 cores, I want my chains to be executed in parallel (`cpp_options={'STAN_THREADS': 'TRUE'}` is specified and this works). Ideally, I want each chain to use 4 cores, but when I specify `threads_per_chain=4' in sample() and run my code, it only uses 4 cores (checked in activity monitor (Mac) and with htop (Linux)) when I would want it to use 16 cores. I have tried specifying `os.environ[“STAN\_NUM\_THREADS”] = str(16)` but this does not seem to have any effect.

currently my code looks like this

```no-highlight
nc=4
threads=4

model = CmdStanModel(stan_file=stan_code, cpp_options={'STAN_THREADS': 'TRUE', force_compile=True)

fit = model.sample(data=d, threads_per_chain=threads, chains=nc, iter_warmup=nw, 
                   iter_sampling=ni, seed=305, thin=thin,)

```

- Operating System: macOS venture 13.5.1 and Linux 4.18.0-477.27.1.el8\_8.x86\_64 x86\_64
- CmdStan Version: cmdstanpy= 1.2.0, cmdstan=2.31.0

Help would 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:** [December 13, 2023, 2:27pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-multithreading-issues-threads-per-chain/33572/2 "2023-12-13T14:27:47Z")

</div>

`threads_per_chain` - cf [API Reference — CmdStanPy 1.2.0 documentation](https://mc-stan.org/cmdstanpy/api.html#cmdstanpy.CmdStanModel.sample)

> The number of threads to use in parallelized sections within an MCMC chain (e.g., when using the Stan functions`reduce_sum()` or `map_rect()` ).

are either `map_rect` or `reduce_sum` being used?

---

<div class="post-metadata">

**Author:** ![annelies](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@annelies](https://discourse.mc-stan.org/u/annelies)\
**Post date:** [December 13, 2023, 3:02pm UTC](https://discourse.mc-stan.org/t/cmdstanpy-multithreading-issues-threads-per-chain/33572/3 "2023-12-13T15:02:02Z")

</div>

No, I did not added map\_rect() to my stan code and it seems to work now :)
