How to define STAN_THREADS?

I’m running sampling with pure C++ and I’m having issues when trying to run sampling in parallel. I read another post there it says you need to define STAN_THREADS. I’m not sure how or where to do that. Everything works fine with one chain. When I use two chains, it gives various errors (i.e. malloc: *** error for object 0x7f8a04f00738: pointer being freed was not allocated). Here’s what I’m trying to do (some code omitted for clarity).

std::thread *threadArray = new std::thread[chains];
for (int i = 0; i < chains; i++) {
        threadArray[i] = std::thread([i]() {
             ...
             linear_model *model = new linear_model(varContext, 123456789, &amp;pstream);
             ...
             int returnCode = stan::services::sample::hmc_nuts_diag_e_adapt<linear_model>(
                                *model, context, 123456789, i, 2,
                                500, 500, 1,
                                false, 100,
                                1, 0, 10,
                                0.8, 0.05, 0.75, 10,
                                75, 50, 25,
                                interrupt, logger, init,
                                *parameter, diagnostic);
        ...
        });

}

for (int i = 0; i < chains; ++i) {
    threadArray[i].join();
}

You need to add -DSTAN_THREADS to the compiler flags… or use a define for the same effect in your code.

Thanks wds15. Looks like that was it. I had some other issue.

Hey @stanuser! How exactly did you add -DSTAN_THREADS to the compiler flags? I’ve tried multiple times and it just doesn’t work for me…

It’ll depend on what interface you’re using… are you on CmdStan?

I’m using the Stan Math Library in C++ via the #include <stan/math.hpp> include and I need the auto differentiation to be thread-safe.

How do you add it in cmdstan?

I found it:

make STAN_THREADS=TRUE  model_name 
./model_name sample num_threads=4  
1 Like