# Within-Chain parallelization & Seed

**URL:** <https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072>\
**Category:** Modeling\
**Tags:** rstan\
**Created:** [October 26, 2024, 2:54pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072 "2024-10-26T14:54:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sheeta27](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sheeta27/32/19189_2.png) [@Sheeta27](https://discourse.mc-stan.org/u/Sheeta27)\
**Post date:** [October 26, 2024, 2:54pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/1 "2024-10-26T14:54:51Z")

</div>

Hi everyone,

I’m using the within chain parallelization (with the function reduce-sum and partial-sum), and I was wondering if we could specify a seed to obtain the same results from a run to another run of the same model, please ?

Indeed, I’m able to specify a seed in the r part and the stan part of my model, leading to the same results from a run to another run of the same model when I don’t use the within chain parallelization.

However, when I use the within chain parallelization with a seed in the r part and the stan part, I don’t obtain the same results from a run to another run of the same exact model (with the same exact data also). So I was wondering If there is a way to specify a seed for stan to perform the same within chain parallelization each time?

Thank you !

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [October 26, 2024, 5:18pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/2 "2024-10-26T17:18:27Z")

</div>

Using threading makes things not easily reproducible in an exact way. You may get exact reproducibility if you use the static version of reduce\_sum. This version chunks the large sum always in the same packets…and hence the choice of the grainsize is more important to choose well for good performance.

---

<div class="post-metadata">

**Author:** ![Sheeta27](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sheeta27/32/19189_2.png) [@Sheeta27](https://discourse.mc-stan.org/u/Sheeta27)\
**Post date:** [October 26, 2024, 7:41pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/3 "2024-10-26T19:41:07Z")

</div>

Thank you for the reply!

How could I tell stan that I want to use the static version of reduce sum?

For now, I was using a grainsize of 1, which seemed to be the one with the best performance.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [October 26, 2024, 9:22pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/4 "2024-10-26T21:22:00Z")

</div>

Please have a look at the Stan functions manual.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [October 28, 2024, 8:08pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/5 "2024-10-28T20:08:34Z")

</div>

> [@wds15](#):
>
> the static version of reduce\_sum.

I’m guessing that @wds15 means the version without threading. To do that, you need to change the compiler flags to not use threading I believe. But I don’t know how to do that. @WardBrian or @stevebronder should know.

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [October 28, 2024, 8:15pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/6 "2024-10-28T20:15:07Z")

</div>

I believe he meant [the `reduce_sum_static` function](https://mc-stan.org/docs/functions-reference/higher-order_functions.html#functions-reduce)

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [October 28, 2024, 10:17pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/7 "2024-10-28T22:17:47Z")

</div>

Yes, that’s what I meant.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [October 28, 2024, 10:31pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-seed/37072/8 "2024-10-28T22:31:22Z")

</div>

Thanks—I didn’t even know there was such a function. Sorry for confusing matters!
