OpenMP:在每个线程中有一个完整的“for”循环 [英] OpenMP: having a complete 'for' loop into each thread
问题描述
我有这个代码:
#pragma omp parallel
{
#pragma omp single
{
for(int i = 0; i }
#pragma omp single
{
for(int i = 0; i }
}
//等等...最多5或6个myObject_x
//然后我总结缓冲区和做一些与他们
float result;
for(int i = 0; i result = myBuffer_1 [i] + myBuffer_2 [i];
//与结果做某事
如果我运行此代码,得到我所期望的但CPU使用率看起来相当高。相反,如果我正常运行没有OpenMP我得到相同的结果,但CPU使用率是低得多,尽管在单个线程中运行。
我不想指定一个线程数,我希望程序根据CPU能力选择最大线程数,但我想让每个 for
循环运行完全在自己的线程。我该怎么办?
此外,我的期望是,对于myBuffer_1,为
循环运行一个线程,另一个 for
循环运行另一个线程,其余的运行在'master'线程中。这是正确的吗?
-
#pragma omp single
在结束时有一个隐式屏障,你需要使用#pragma omp single nowait
如果你想要两个single
一个更好的主意
#pragma omp parallel
{
#pragma omp sections
{
#pragma omp section
{
for(int i = 0; i }
#pragma omp section
{
for(int i = 0; i }
}
}
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?
#pragma omp single
has an implicit barrier at the end, you need to use#pragma omp single nowait
if you want the twosingle
block run concurrently.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(); } } }
这篇关于OpenMP:在每个线程中有一个完整的“for”循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!