Skip to content

Commit bd2fb41

Browse files
[openmp] Fixed nonmonotonic schedule when #threads > #chunks in a loop.
Differential Revision: https://reviews.llvm.org/D70713
1 parent 755dfaa commit bd2fb41

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

openmp/runtime/src/kmp_dispatch.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,15 @@ void __kmp_dispatch_init_algorithm(ident_t *loc, int gtid,
379379
}
380380
break;
381381
} else {
382-
KD_TRACE(100, ("__kmp_dispatch_init_algorithm: T#%d falling-through to "
383-
"kmp_sch_static_balanced\n",
384-
gtid));
385-
schedule = kmp_sch_static_balanced;
386-
/* too few iterations: fall-through to kmp_sch_static_balanced */
382+
/* too few chunks: switching to kmp_sch_dynamic_chunked */
383+
schedule = kmp_sch_dynamic_chunked;
384+
KD_TRACE(100, ("__kmp_dispatch_init_algorithm: T#%d switching to "
385+
"kmp_sch_dynamic_chunked\n",
386+
gtid));
387+
if (pr->u.p.parm1 <= 0)
388+
pr->u.p.parm1 = KMP_DEFAULT_CHUNK;
389+
break;
387390
} // if
388-
/* FALL-THROUGH to static balanced */
389-
KMP_FALLTHROUGH();
390391
} // case
391392
#endif
392393
case kmp_sch_static_balanced: {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %libomp-compile
2+
// RUN: env OMP_SCHEDULE=nonmonotonic:dynamic,10 %libomp-run
3+
4+
// The test checks iterations distribution for OMP 5.0 nonmonotonic OMP_SCHEDULE
5+
// case #threads > #chunks (fallback to monotonic dynamic)
6+
7+
#include <stdio.h>
8+
#include <omp.h>
9+
10+
#define ITERS 100
11+
#define CHUNK 10
12+
int err = 0;
13+
14+
int main(int argc, char **argv) {
15+
int i, ch, it[ITERS];
16+
omp_set_num_threads(16); // #threads is bigger than #chunks
17+
#pragma omp parallel for schedule(runtime)
18+
for (i = 0; i < ITERS; ++i) {
19+
it[i] = omp_get_thread_num();
20+
}
21+
// check that each chunk executed by single thread
22+
for (ch = 0; ch < ITERS/CHUNK; ++ch) {
23+
int iter = ch * CHUNK;
24+
int nt = it[iter]; // thread number
25+
for (i = 1; i < CHUNK; ++i) {
26+
#if _DEBUG
27+
printf("iter %d: (%d %d)\n", iter + i, nt, it[iter + i]);
28+
#endif
29+
if (nt != it[iter + i]) {
30+
err++;
31+
}
32+
}
33+
}
34+
if (err > 0) {
35+
printf("Failed, err = %d\n", err);
36+
return 1;
37+
}
38+
printf("Passed\n");
39+
return 0;
40+
}

0 commit comments

Comments
 (0)