[英]Parallel sections code with nested loops in openmp
I made this parallel code to share the iterations like first and last, fisrst+1 and last-1,... But I don't know how to improve the code in every one of the two parallel sections because I have an inner loop in the sections and I can't think of any way to simplify it, thanks.我制作了这个并行代码来共享迭代,例如 first 和 last、first+1 和 last-1,... 但是我不知道如何改进两个并行部分中的每个部分的代码,因为我有一个内部循环在这些部分中,我想不出任何方法来简化它,谢谢。
This isn't about which values are stored in x or y, I use this sections design because the requisite is execute the iterations from 0 to N like: 0 N, 1 N-1, 2 N-2 but I would like to know if I can optimize the inner loops maintaining this model这与将哪些值存储在 x 或 y 中无关,我使用此部分设计,因为必要条件是执行从 0 到 N 的迭代,例如:0 N、1 N-1、2 N-2 但我想知道如果我可以优化维护这个 model 的内部循环
int x = 0, y = 0,k,i,j,h;
#pragma omp parallel private(i, h) reduction(+:x, y)
{
#pragma omp sections
{
#pragma omp section
{
for (i=0; i<N/2; i++)
{
C[i] = 0;
for (j=0; j<N; j++)
{
C[i] += MAT[i][j] * B[j];
}
x += C[i];
}
}
#pragma omp section
{
for (h=N-1; h>=N/2; h--)
{
C[h] = 0;
for (k=0; k<N; k++)
{
C[h] += MAT[h][k] * B[k];
}
y += C[h];
}
}
}
}
x = x + y;
The section construct is to distribute different tasks to different threads and each section block marks a different task so you will not be able to do that iterations in the order you want I answered you here:部分构造是将不同的任务分配给不同的线程,每个部分块标记一个不同的任务,因此您将无法按照您想要的顺序进行迭代我在这里回答您:
Distribution of loop iterations between threads with a specific order 具有特定顺序的线程之间的循环迭代分布
But I want to clarify that the requirement to use sections is that each block must be independent of the other blocks.但我想澄清一下,使用节的要求是每个块必须独立于其他块。
Using sections seems like the wrong approach.使用部分似乎是错误的方法。 A
pragma omp for
seems more appropriate.一个
pragma omp for
似乎更合适。 Also note that you forgot to declare j
private.另请注意,您忘记将
j
声明为私有。
int x = 0, y = 0,k,i,j;
#pragma omp parallel private(i,j) reduction(+:x, y)
{
# pragma omp for nowait
for(i=0; i<N/2; i++) {
// local variable to make the life easier on the compiler
int ci = 0;
for(j=0; j<N; j++)
ci += MAT[i][j] * B[j];
x += ci;
C[i] = ci;
}
# pragma omp for nowait
for(i=N/2; i < N; i++) {
int ci = 0;
for(j=0; j<N; j++)
ci += MAT[i][j] * B[j];
y += ci;
C[i] = ci;
}
}
x = x + y;
Also, I'm not sure but if you just want x
as your final output, you can simplify the code even further:另外,我不确定,但如果您只想将
x
作为您的最终 output,您可以进一步简化代码:
int x=0, i, j;
#pragma omp parallel for reduction(+:x) private(i,j)
for(i=0; i < N; ++i)
for(j=0; j < N; ++j)
x += MAT[i][j] * B[j];
A section gets only one thread, so you can't make the loops parallel.一个部分只有一个线程,所以你不能使循环并行。 How about
怎么样
N
at the top level,N
做一个并行循环,x,y
?x,y
? Although @Homer512 's solution looks correct to me too.尽管@Homer512 的解决方案对我来说也是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.