简体   繁体   中英

How to nest omp for in omp single using OpenMP

I have a for loop, which I don't want to parallelise, which calls a function which I want to parallelise (which has a for loop in it that I want to parallelise). I want to put the parallel region outside of the whole lot, so that my threads only get created once (to reduce the overhead of thread creation).

However, at the moment I have a omp single covering the for loop, which calls the function and an omp for inside the function to deal with the internal for loop. It hands, and according to OMP single hangs inside for this is because doing that is illegal!

If I can't do it that way, how can I approach it? I want to make sure that only one thread runs the outer for loop and calls the function, but that inside the function I can get full parallelism.

Is this possible? Any ideas?

Most implementations only create the threads once - either when the program is started or when the first parallel region is encountered. Once created they are generally not destroyed, but put into a free thread pool (handled by the OpenMP implementation) when the end of parallel region is encountered. This means that you should be able to put the parallel region within the loop and not have the thread creation overhead each time the parallel region is encountered. There will be some small overhead each time the parallel region is encountered, but much smaller than when the threads are created.

What about : - putting your inner loop in a #pragma omp parallel - setting the number of active threads to one before your outer loop - set it back to N before calling your other function - put a #pragma omp for inside the function

?

Parallel sections are transient over function boundaries in OMP and setting the number of actives threads should not be too detrimental. Needs ot be tested/benchmarked though.

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