CmdStanPy and multithreading

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), 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!

answered in earlier thread -

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…

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?

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.

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.

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.

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

1 Like

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

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