Compare commits

...

1 Commits

Author SHA1 Message Date
indiano
983ce4ff50 ft:2D-omp outer loop parallelization 2025-05-07 10:26:01 +02:00

View File

@ -45,6 +45,7 @@ int get_dim_start(int dim_rank, int dim_comm_size, int dim_size)
int get_x_choord(const int proc_num, const int const* dims) { return proc_num / dims[1]; } int get_x_choord(const int proc_num, const int const* dims) { return proc_num / dims[1]; }
int get_y_choord(const int proc_num, const int const* dims) { return proc_num % dims[1]; } int get_y_choord(const int proc_num, const int const* dims) { return proc_num % dims[1]; }
void initSolver(Solver* solver, Parameter* params, int problem) void initSolver(Solver* solver, Parameter* params, int problem)
{ {
solver->imax = params->imax; solver->imax = params->imax;
@ -139,25 +140,23 @@ void solveRB(Solver* solver)
} }
double res = 0.0; double res = 0.0;
#pragma omp parallel shared(res)
{
const int tid = omp_get_thread_num();
const int li_start = get_dim_start(get_x_choord(tid, dim), dim[0], imax);
const int lj_start = get_dim_start(get_y_choord(tid, dim), dim[1], jmax);
for (int it = 0; it < itermax; ++it) { const int limax = li_start + distribute_dim(get_x_choord(tid, dim), dim[0], imax);
const int ljmax = lj_start + distribute_dim(get_y_choord(tid, dim), dim[1], jmax);
res = 0.0; for (int it = 0; it < itermax; ++it) {
#pragma omp parallel reduction(+ : res) #pragma omp single
{ {
const int tid = omp_get_thread_num(); res = 0;
}
const int li_start = get_dim_start(get_x_choord(tid, dim), dim[0], imax); double local_res = 0;
const int lj_start = get_dim_start(get_y_choord(tid, dim), dim[1], jmax); int jsw = ((li_start) % 2 == 0) == ((lj_start) % 2 == 0) ? 1 : 2;
const int limax = li_start +
distribute_dim(get_x_choord(tid, dim), dim[0], imax);
const int ljmax = lj_start +
distribute_dim(get_y_choord(tid, dim), dim[1], jmax);
int jsw = ((li_start) % 2 == 0) == ((lj_start) % 2 == 0) ? 1 : 2;
for (int pass = 0; pass < 2; ++pass) { for (int pass = 0; pass < 2; ++pass) {
int isw = jsw; int isw = jsw;
@ -170,13 +169,17 @@ void solveRB(Solver* solver)
idy2); idy2);
P(i, j) -= factor * r; P(i, j) -= factor * r;
res += r * r; /* reduction variable */ local_res += r * r; /* reduction variable */
} }
isw = 3 - isw; isw = 3 - isw;
} }
#pragma omp barrier #pragma omp barrier
jsw = 3 - jsw; jsw = 3 - jsw;
} }
#pragma omp critical
{
res += local_res;
}
if (lj_start == 0) if (lj_start == 0)
for (int i = li_start + 1; i < limax + 1; i++) for (int i = li_start + 1; i < limax + 1; i++)
P(i, 0) = P(i, 1); P(i, 0) = P(i, 1);
@ -189,21 +192,20 @@ void solveRB(Solver* solver)
if (limax == imax) if (limax == imax)
for (int j = lj_start + 1; j < ljmax + 1; j++) for (int j = lj_start + 1; j < ljmax + 1; j++)
P(limax + 1, j) = P(limax, j); P(limax + 1, j) = P(limax, j);
#pragma omp single
} {
res /= (double)(imax * jmax);
res /= (double)(imax * jmax);
#ifdef DEBUG #ifdef DEBUG
printf("%d Residuum: %e\n", it, res); printf("%d Residuum: %e\n", it, res);
#endif #endif
}
if (res < epssq) { #pragma omp barrier
printf("Solver took %d iterations to reach %e\n", it + 1, sqrt(res)); if (res < epssq) {
return; break;
}
#pragma omp barrier
} }
} }
printf("Solver reached itermax (%d) with residual %e\n", itermax, sqrt(res)); printf("Solver reached itermax (%d) with residual %e\n", itermax, sqrt(res));
} }