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, &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();
}