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.
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;
CodePudding user response:
Using sections seems like the wrong approach. A pragma omp for
seems more appropriate. Also note that you forgot to declare j
private.
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:
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];
CodePudding user response:
A section gets only one thread, so you can't make the loops parallel. How about
- Make a parallel loop to
N
at the top level, - then inside each iteration use a conditional to decide whether to accumulate into
x,y
?
Although @Homer512 's solution looks correct to me too.