简体   繁体   中英

c++ OpenMP multiple threads in each loop

I have a function myFunc() which utilizes OMP loops internally, but does not consume all the cores I have available. At a higher level, I call this function several times within a loop. Is there a simple way to use OMP on this outer loop, but not restrict each loop pass to a single thread?

eg:

// I have 24 cores
// Would like to allow each for loop pass to have 12 cores
for (int outIter=0; outIter<2; outIter++) {
    x[outIter] = myFunc(...)  // This function has OMP inside
}

You should first find out, why myFunc cannot exhaust all 24 cores. Often you are limited by memory bandwidth. Having said that, the above loop is obviously not parallel. You would have to try something like this:

#pragma omp parallel num_threads(2)
{
   myFunc(...);
}

where myFunc's input would be a function of omp_get_thread_num() . But again, do not be to hopeful of being able to achieve more parallelism. If your problem is limited by memory bandwidth there is little hope for speedup. What happens roughly in you myFunc ?

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