简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM