I am trying to parallelise the heat_plate
algorithm but I am stuck at this bit of code inside my while:
while(1)
{
.....
.....
#pragma omp parallel shared(diff, u, w) private(i, j, my_diff)
{
my_diff = 0.0;
#pragma omp for
for (i = 1; i < M - 1; i++)
{
for (j = 1; j < N - 1; j++)
{
if ( my_diff < fabs (w[i][j] - u[i][j]))
{
my_diff = fabs (w[i][j] - u[i][j]);
}
}
}
#pragma omp critical
{
if (diff < my_diff)
{
diff = my_diff;
}
}
}
....
....
}
Not only I can't get it to work in parallel it actually takes longer to finish
Edit:
The Program runs in parallel .
Thank you in advance for your help.
In OpenMP this data dependency:
for (i = 1; i < M - 1; i++)
for (j = 1; j < N - 1; j++)
if ( my_diff < fabs (w[i][j] - u[i][j]))
my_diff = fabs (w[i][j] - u[i][j]);
is typically solved using OpenMP reduction feature, which in your case will avoid the critical
region (after the parallel for) and, consequently, improve the overall performance of the parallelization. So if you apply that feature your code would look like the following:
#pragma omp parallel shared(u, w) private(i, j)
{
#pragma omp for reduction(max:diff)
for (i = 1; i < M - 1; i++)
for (j = 1; j < N - 1; j++)
if ( diff < fabs (w[i][j] - u[i][j]))
diff = fabs (w[i][j] - u[i][j]);
}
In turn you can merge both pragmas into one:
#pragma omp parallel for reduction(max:diff) shared(u, w) private(i, j)
for (i = 1; i < M - 1; i++)
for (j = 1; j < N - 1; j++)
if ( diff < fabs (w[i][j] - u[i][j]))
diff = fabs (w[i][j] - u[i][j]);