简体   繁体   中英

OpenMP: having a complete 'for' loop into each thread

I have this code:

#pragma omp parallel
{
  #pragma omp single
  {
    for (int i=0; i<given_number; ++i) myBuffer_1[i] = myObject_1->myFunction();
  }

  #pragma omp single
  {
    for (int i=0; i<given_number; ++i) myBuffer_2[i] = myObject_2->myFunction();
  }
}

// and so on... up to 5 or 6 of myObject_x

// Then I sum up the buffers and do something with them
float result;
for (int i=0; i<given_number; ++i)
  result = myBuffer_1[i] + myBuffer_2[i];

// do something with result

If I run this code, I get what I expect but the CPU usage looks quite high. Instead, if I run it normally without OpenMP I get the same results but the CPU usage is much lower, despite running in a single thread.

I don't want to specify a number of threads, I wish the program pick the max number of threads according to the CPU capabilities, but I want that each for loop runs entirely in its own thread. How can I do that?

Also, my expectation is that the for loop for myBuffer_1 runs a thread, the other for loop runs another thread, and the rest runs in the 'master' thread. Is this correct?

  1. #pragma omp single has an implicit barrier at the end, you need to use #pragma omp single nowait if you want the two single block run concurrently.

  2. However, for your requirement, using section might be a better idea

     #pragma omp parallel { #pragma omp sections { #pragma omp section { for (int i=0; i<given_number; ++i) myBuffer_1[i] = myObject_1->myFunction(); } #pragma omp section { for (int i=0; i<given_number; ++i) myBuffer_2[i] = myObject_2->myFunction(); } } } 

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