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

好久不见. 提交于 2019-12-11 03:18:35

问题


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:


  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();  
            }
        }
    
    }
    


来源:https://stackoverflow.com/questions/27877420/openmp-having-a-complete-for-loop-into-each-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!