headmyshoulder / odeint-v2

odeint - solving ordinary differential equations in c++ v2
http://headmyshoulder.github.com/odeint-v2/
Other
341 stars 101 forks source link

Problem with embarassingly simple OpenMP parallelization #222

Open kartikkumar opened 6 years ago

kartikkumar commented 6 years ago

Hi,

I'm encountering a strange problem that is likely unrelated to odeint but I figured I should ask here since I'm totally stumped at the moment.

I've got a simulator that's simulating the dynamics of circumplanetary dust particles. I'm basically setting up a simple Monte Carlo simulation, executing thousands of dust particle simulations that are all independent, and only uniquely different because of a different initial state.

The for-loop that runs the Monte Carlo simulation seems to work fine when I execute it on a single thread, and call one of the odeint integrate functions inside the body of the for-loop using any of one of the steppers available.

As soon as I parallelize the for-loop over multiple threads, a really peculiar thing happens: the whole Monte Carlo simulation suddenly takes MUCH longer to execute. For instance, when I'm running 10 dust particle simulations, each for 1000 years, using the Bulirsch-Stoer dense output stepper, it takes 15 mins on 1 thread and 1hr 15 mins on 2 threads!

It makes absolutely no sense and I've been going through the code line-by-line to understand what's causing this. I'm totally stumped and now I'm starting to wonder whether for some reason multiple calls to the integrate function is somehow causing some weird problem behind the scenes.

My simulator codebase can be found here: https://github.com/kartikkumar/dustsim`

The problem relates to this line in my bulk particle simulator: https://github.com/kartikkumar/dustsim/blob/master/src/bulkParticleSimulator.cpp#L342`

Any help would be greatly appreciated!