Skip to content

Commit 2e57e63

Browse files
authored
[OpenMP][libomp] Fix tasking debug assert (#95823)
The debug assert is meant to check that the index is a valid which means the runtime needs to check against the size of the array instead of the number of threads. A free()-ed thread put back in the thread pool may index into anywhere inside the task team's available array from 0 to tt_max_threads potentially. Fixes: #94260
1 parent 97839a8 commit 2e57e63

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

openmp/runtime/src/kmp_tasking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3245,7 +3245,7 @@ static kmp_task_t *__kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid,
32453245
threads_data = task_team->tt.tt_threads_data;
32463246
KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
32473247
KMP_DEBUG_ASSERT(victim_tid >= 0);
3248-
KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_nproc);
3248+
KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_max_threads);
32493249

32503250
victim_td = &threads_data[victim_tid];
32513251
victim_thr = victim_td->td.td_thr;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// RUN: %libomp-cxx-compile-and-run
2+
3+
#include <assert.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <omp.h>
7+
8+
// The number of times to run each test
9+
#define NTIMES 2
10+
11+
// Every thread creates a single "increment" task
12+
void test_tasks() {
13+
for (int i = 0; i < 100; ++i)
14+
#pragma omp task
15+
{
16+
int tid = omp_get_thread_num();
17+
}
18+
}
19+
20+
// Testing single level of parallelism with increment tasks
21+
void test_base(int nthreads) {
22+
#ifdef VERBOSE
23+
#pragma omp master
24+
printf(" test_base(%d)\n", nthreads);
25+
#endif
26+
#pragma omp parallel num_threads(nthreads)
27+
{ test_tasks(); }
28+
}
29+
30+
// Testing nested parallel with increment tasks
31+
// first = nthreads of outer parallel
32+
// second = nthreads of nested parallel
33+
void test_nest(int first, int second) {
34+
#ifdef VERBOSE
35+
#pragma omp master
36+
printf(" test_nest(%d, %d)\n", first, second);
37+
#endif
38+
#pragma omp parallel num_threads(first)
39+
{
40+
for (int i = 0; i < 100; ++i)
41+
#pragma omp task
42+
{
43+
int tid = omp_get_thread_num();
44+
}
45+
test_base(second);
46+
}
47+
}
48+
49+
template <typename... Args>
50+
void run_ntimes(int n, void (*func)(Args...), Args... args) {
51+
for (int i = 0; i < n; ++i) {
52+
func(args...);
53+
}
54+
}
55+
56+
int main() {
57+
omp_set_max_active_levels(5);
58+
59+
for (int i = 0; i < 100; ++i) {
60+
run_ntimes(NTIMES, test_nest, 4, 3);
61+
run_ntimes(NTIMES, test_nest, 2, 1);
62+
}
63+
64+
printf("PASS\n");
65+
return EXIT_SUCCESS;
66+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %libomp-compile-and-run
2+
3+
#include <stdio.h>
4+
#include <omp.h>
5+
6+
int test_omp_parallel_num_threads() {
7+
int num_failed;
8+
int threads;
9+
int nthreads;
10+
int max_threads = 0;
11+
12+
num_failed = 0;
13+
#pragma omp task
14+
{}
15+
16+
/* first we check how many threads are available */
17+
#pragma omp parallel
18+
{
19+
#pragma omp task
20+
{}
21+
#pragma omp master
22+
max_threads = omp_get_num_threads();
23+
}
24+
25+
/* we increase the number of threads from one to maximum:*/
26+
for (threads = 1; threads <= max_threads; threads++) {
27+
nthreads = 0;
28+
#pragma omp parallel reduction(+ : num_failed) num_threads(threads)
29+
{
30+
#pragma omp task
31+
{}
32+
num_failed = num_failed + !(threads == omp_get_num_threads());
33+
#pragma omp atomic
34+
nthreads += 1;
35+
}
36+
num_failed = num_failed + !(nthreads == threads);
37+
}
38+
return (!num_failed);
39+
}
40+
41+
int main() {
42+
int i;
43+
int num_failed = 0;
44+
45+
for (i = 0; i < 100; i++) {
46+
if (!test_omp_parallel_num_threads()) {
47+
num_failed++;
48+
}
49+
}
50+
return num_failed;
51+
}

0 commit comments

Comments
 (0)