Skip to content

Commit 2c7db5f

Browse files
committed
[OpenMP] Fix reallocation of task successors in taskgraph mode
Ensure proper resizing and copying of the successors array when its capacity is exceeded. The previous implementation allocated a new array but did not copy the existing elements. This led to loss of successor data and incorrect task dependency tracking.
1 parent 106473c commit 2c7db5f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

openmp/runtime/src/kmp_taskdeps.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,12 @@ static inline void __kmp_track_dependence(kmp_int32 gtid, kmp_depnode_t *source,
243243
}
244244
if (!exists) {
245245
if (source_info->nsuccessors >= source_info->successors_size) {
246+
kmp_uint old_size = source_info->successors_size;
246247
source_info->successors_size = 2 * source_info->successors_size;
247248
kmp_int32 *old_succ_ids = source_info->successors;
248249
kmp_int32 *new_succ_ids = (kmp_int32 *)__kmp_allocate(
249250
source_info->successors_size * sizeof(kmp_int32));
251+
KMP_MEMCPY(new_succ_ids, old_succ_ids, old_size * sizeof(kmp_int32));
250252
source_info->successors = new_succ_ids;
251253
__kmp_free(old_succ_ids);
252254
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// REQUIRES: ompx_taskgraph
2+
// RUN: %libomp-cxx-compile-and-run
3+
#include <omp.h>
4+
#include <cassert>
5+
#include <vector>
6+
7+
#define TASKS_SIZE 12
8+
9+
typedef struct ident {
10+
void *dummy;
11+
} ident_t;
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
int __kmpc_global_thread_num(ident_t *);
16+
int __kmpc_start_record_task(ident_t *, int, int, int);
17+
void __kmpc_end_record_task(ident_t *, int, int, int);
18+
}
19+
#endif
20+
21+
void init(int &A, int val) { A = val; }
22+
23+
void update(int &A, int &B, int val) { A = B + val; }
24+
25+
void test(int nb, std::vector<std::vector<int>> &Ah) {
26+
#pragma omp parallel
27+
#pragma omp single
28+
{
29+
int gtid = __kmpc_global_thread_num(nullptr);
30+
int res = __kmpc_start_record_task(nullptr, gtid, 0, 0);
31+
if (res) {
32+
for (int k = 0; k < nb; ++k) {
33+
#pragma omp task depend(inout : Ah[k][0])
34+
init(Ah[k][0], k);
35+
36+
for (int i = 1; i < nb; ++i) {
37+
#pragma omp task depend(in : Ah[k][0]) depend(out : Ah[k][i])
38+
update(Ah[k][i], Ah[k][0], 1);
39+
}
40+
}
41+
}
42+
__kmpc_end_record_task(nullptr, gtid, 0, 0);
43+
}
44+
}
45+
46+
int main() {
47+
std::vector<std::vector<int>> matrix(TASKS_SIZE,
48+
std::vector<int>(TASKS_SIZE, 0));
49+
50+
test(TASKS_SIZE, matrix);
51+
test(TASKS_SIZE, matrix);
52+
53+
for (int k = 0; k < TASKS_SIZE; ++k) {
54+
assert(matrix[k][0] == k);
55+
for (int i = 1; i < TASKS_SIZE; ++i) {
56+
assert(matrix[k][i] == k + 1);
57+
}
58+
}
59+
return 0;
60+
}

0 commit comments

Comments
 (0)