Datiswaken
Datiswaken

Reputation: 11

openMP parallelize for-loop slower compared to serial

I´m having problems to parallelize a for-loop. I have already read a lot of threads in this forum, but none of them helped. The code is quite simple, so I don´t really see where I should change something.

    #pragma omp parallel for
        for (unsigned int i = 0; i < num_bodies; ++i){
          Planet* planet = Planet::planets[i];
          planet->updateVelo(planet->getAccel(), dt);
          planet->updatePos(planet->getVelo(), dt);
          planet->resetAccel();
        }

"num_bodies" is 200 at the beginning and decreases to 133 over a couple of iterations, so no really big numers. I am updating the objects in the vector Planet::planets by updating the member variables. "dt" is a value that is never changed. Without openMP, the whole loop needs around 0.00002 seconds whereas it takes between 0.001 and 0.01 seconds with openMP activated. I have tried several ways do get the parallelized version faster, but nothing helped.

Thank you!

Upvotes: 1

Views: 122

Answers (1)

Marco
Marco

Reputation: 2020

200 is a very small number, it is likely that the extra time needed to set up the multi threading framework is longer that the time you save by parallelizing the job, try to increase the number of loops and also make sure you have used the proper compiler switch(es) es. $gcc -fopenmp

Upvotes: 3

Related Questions