Skip to content

Commit fb81ce0

Browse files
authored
proper skip omp parallel loops (#7376)
1 parent 3ad93f7 commit fb81ce0

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/froll.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void frollmeanExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill
245245
}
246246
bool truehasnf = hasnf>0; // flag to re-run with NA support if NAs detected
247247
if (!truehasnf || !narm) {
248-
#pragma omp parallel for num_threads(getDTthreads(nx, true))
248+
#pragma omp parallel for num_threads(getDTthreads(nx, true)) shared(truehasnf)
249249
for (uint64_t i=k-1; i<nx; i++) { // loop on every observation with complete window, partial already filled in single threaded section
250250
if (narm && truehasnf) {
251251
continue; // if NAs detected no point to continue
@@ -265,6 +265,7 @@ void frollmeanExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill
265265
if (!narm) {
266266
ans->dbl_v[i] = (double) w; // NAs should be propagated
267267
}
268+
#pragma omp atomic write
268269
truehasnf = true; // NAs detected for this window, set flag so rest of windows will not be re-run
269270
} else {
270271
ans->dbl_v[i] = (double) w; // Inf and -Inf
@@ -432,7 +433,7 @@ void frollsumExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill,
432433
}
433434
bool truehasnf = hasnf>0;
434435
if (!truehasnf || !narm) {
435-
#pragma omp parallel for num_threads(getDTthreads(nx, true))
436+
#pragma omp parallel for num_threads(getDTthreads(nx, true)) shared(truehasnf)
436437
for (uint64_t i=k-1; i<nx; i++) {
437438
if (narm && truehasnf) {
438439
continue;
@@ -447,6 +448,7 @@ void frollsumExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill,
447448
if (!narm) {
448449
ans->dbl_v[i] = (double) w;
449450
}
451+
#pragma omp atomic write
450452
truehasnf = true;
451453
} else {
452454
ans->dbl_v[i] = (double) w;
@@ -1079,7 +1081,7 @@ void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill
10791081
}
10801082
bool truehasnf = hasnf>0;
10811083
if (!truehasnf || !narm) {
1082-
#pragma omp parallel for num_threads(getDTthreads(nx, true))
1084+
#pragma omp parallel for num_threads(getDTthreads(nx, true)) shared(truehasnf)
10831085
for (uint64_t i=k-1; i<nx; i++) {
10841086
if (narm && truehasnf) {
10851087
continue;
@@ -1094,6 +1096,7 @@ void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill
10941096
if (!narm) {
10951097
ans->dbl_v[i] = (double) w;
10961098
}
1099+
#pragma omp atomic write
10971100
truehasnf = true;
10981101
} else {
10991102
ans->dbl_v[i] = (double) w;

src/frolladaptive.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ void frolladaptivemeanExact(const double *x, uint64_t nx, ans_t *ans, const int
216216
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivemeanExact", (uint64_t)nx, hasnf, (int) narm);
217217
bool truehasnf = hasnf>0; // flag to re-run if NAs detected
218218
if (!truehasnf || !narm) { // narm=FALSE handled here as NAs properly propagated in exact algo
219-
#pragma omp parallel for num_threads(getDTthreads(nx, true))
219+
#pragma omp parallel for num_threads(getDTthreads(nx, true)) shared(truehasnf)
220220
for (uint64_t i=0; i<nx; i++) { // loop on every observation to produce final answer
221221
if (narm && truehasnf) {
222222
continue; // if NAs detected no point to continue
@@ -239,6 +239,7 @@ void frolladaptivemeanExact(const double *x, uint64_t nx, ans_t *ans, const int
239239
if (!narm) {
240240
ans->dbl_v[i] = (double) w;
241241
}
242+
#pragma omp atomic write
242243
truehasnf = true; // NAs detected for this window, set flag so rest of windows will not be re-run
243244
} else {
244245
ans->dbl_v[i] = (double) w; // Inf and -Inf
@@ -436,7 +437,7 @@ void frolladaptivesumExact(const double *x, uint64_t nx, ans_t *ans, const int *
436437
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivesumExact", (uint64_t)nx, hasnf, (int) narm);
437438
bool truehasnf = hasnf>0;
438439
if (!truehasnf || !narm) {
439-
#pragma omp parallel for num_threads(getDTthreads(nx, true))
440+
#pragma omp parallel for num_threads(getDTthreads(nx, true)) shared(truehasnf)
440441
for (uint64_t i=0; i<nx; i++) {
441442
if (narm && truehasnf) {
442443
continue;
@@ -454,6 +455,7 @@ void frolladaptivesumExact(const double *x, uint64_t nx, ans_t *ans, const int *
454455
if (!narm) {
455456
ans->dbl_v[i] = (double) w;
456457
}
458+
#pragma omp atomic write
457459
truehasnf = true; // NAs detected for this window, set flag so rest of windows will not be re-run
458460
} else {
459461
ans->dbl_v[i] = (double) w; // Inf and -Inf
@@ -671,7 +673,7 @@ void frolladaptiveprodExact(const double *x, uint64_t nx, ans_t *ans, const int
671673
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptiveprodExact", (uint64_t)nx, hasnf, (int) narm);
672674
bool truehasnf = hasnf>0;
673675
if (!truehasnf || !narm) {
674-
#pragma omp parallel for num_threads(getDTthreads(nx, true))
676+
#pragma omp parallel for num_threads(getDTthreads(nx, true)) shared(truehasnf)
675677
for (uint64_t i=0; i<nx; i++) {
676678
if (narm && truehasnf) {
677679
continue;
@@ -689,6 +691,7 @@ void frolladaptiveprodExact(const double *x, uint64_t nx, ans_t *ans, const int
689691
if (!narm) {
690692
ans->dbl_v[i] = (double) w;
691693
}
694+
#pragma omp atomic write
692695
truehasnf = true; // NAs detected for this window, set flag so rest of windows will not be re-run
693696
} else {
694697
ans->dbl_v[i] = (double) w; // Inf and -Inf

0 commit comments

Comments
 (0)