roualdes / bridgestan

BridgeStan provides efficient in-memory access through Python, Julia, and R to the methods of a Stan model.
https://roualdes.github.io/bridgestan
BSD 3-Clause "New" or "Revised" License
87 stars 12 forks source link

Control over the internal threading of a model #194

Open WardBrian opened 7 months ago

WardBrian commented 7 months ago

If a model is compiled with STAN_THREADS and uses a function such as map_rect or reduce_sum, this will lead to the model spawning multiple threads during each call to the log density.

Currently, the model is allowed to spawn up to the hardware number of threads, which means if it is done in a loop which is also parallel on the "outside" you get oversubscription.

The Stan Math library lets you set the maximum number of threads by using init_threadpool_tbb. We could just expose this, but it has some issues, most notably that calling it multiple times has no effect. Similar to the print callback, this will also globally apply to all instances from the same shared library.