user9414424
user9414424

Reputation: 509

How can I write codes that reuse thread on C++ openmp?

I have a function f that I can use parallel processing. For this purpose, I used openmp. However, this function is called many times, and it seems that thread creation is done every call.

How can we reuse the thread?

void f(X &src, Y &dest) {
   ... // do processing based on "src"
   #pragma omp parallel for
   for (...) { 

   }
   ...// put output into "dest" 
}

int main() {
    ...
    for(...) { // It is impossible to make this loop processing parallel one.
       f(...);
    }
    ...
    return 0;
}

Upvotes: 1

Views: 681

Answers (2)

Zulan
Zulan

Reputation: 22670

This is just in addition to Anton's correct answer. If you are really concerned about the issue, for most programs you can easily move the parallel region on the outside and keep serial work serial like follows:

void f(X &src, Y &dest) {
   // You can also do simple computations
   // without side effects outside of the single section
   #pragma omp single
   {
   ... // do processing based on "src"
   }
   #pragma omp for // note parallel is missing
   for (...) { 

   }
   #pragma omp critical
   ...// each thread puts its own part of the output into "dest" 
}

int main() {
    ...
    // make sure to declare loop variable locally or explicitly private
    #pragma omp parallel
    for(type variable;...;...) {
       f(...);
    }
    ...
    return 0;
}

Use this only if you have measured evidence that you are suffering from the overhead of reopening parallel regions. You may have to juggle with shared variables, or manually inline f, because all variables declared within f will be private - so how it looks in detail depends on your specific application.

Upvotes: 3

Anton
Anton

Reputation: 6557

OpenMP implements thread pool internally, it tries to reuse threads unless you change some of its settings in between or use different application threads to call parallel regions while others are still active.

One can verify that the threads are indeed the same by using thread locals. I'd recommend you to verify your claim about recreating the threads. OpenMP runtime does lots of smart optimizations beyond obvious thread pool idea, you just need to know how to tune and control it properly.

While it is unlikely that threads are recreated, it is easy to see how threads can go to sleep by the time when you call parallel region again and it takes noticeable amount of time to wake them up. You can prevent threads from going to sleep by using OMP_WAIT_POLICY=active and/or implementation-specific environment variables like KMP_BLOCKTIME=infinite (for Intel/LLVM run-times).

Upvotes: 6

Related Questions