Skip to content

Commit b13090d

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 b13090d

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// REQUIRES: ompx_taskgraph
2+
// RUN: %libomp-cxx-compile-and-run
3+
#include <omp.h>
4+
#include <cassert>
5+
#include <vector>
6+
7+
constexpr const int TASKS_SIZE = 12;
8+
9+
typedef struct ident ident_t;
10+
11+
extern "C" {
12+
int __kmpc_global_thread_num(ident_t *);
13+
int __kmpc_start_record_task(ident_t *, int, int, int);
14+
void __kmpc_end_record_task(ident_t *, int, int, int);
15+
}
16+
17+
void init(int &A, int val) { A = val; }
18+
19+
void update(int &A, int &B, int val) { A = B + val; }
20+
21+
void test(int nb, std::vector<std::vector<int>> &Ah) {
22+
#pragma omp parallel
23+
#pragma omp single
24+
{
25+
int gtid = __kmpc_global_thread_num(nullptr);
26+
int res = __kmpc_start_record_task(nullptr, gtid, 0, 0);
27+
if (res) {
28+
for (int k = 0; k < nb; ++k) {
29+
#pragma omp task depend(inout : Ah[k][0])
30+
init(Ah[k][0], k);
31+
32+
for (int i = 1; i < nb; ++i) {
33+
#pragma omp task depend(in : Ah[k][0]) depend(out : Ah[k][i])
34+
update(Ah[k][i], Ah[k][0], 1);
35+
}
36+
}
37+
}
38+
__kmpc_end_record_task(nullptr, gtid, 0, 0);
39+
}
40+
}
41+
42+
int main() {
43+
std::vector<std::vector<int>> matrix(TASKS_SIZE,
44+
std::vector<int>(TASKS_SIZE, 0));
45+
46+
test(TASKS_SIZE, matrix);
47+
test(TASKS_SIZE, matrix);
48+
49+
for (int k = 0; k < TASKS_SIZE; ++k) {
50+
assert(matrix[k][0] == k);
51+
for (int i = 1; i < TASKS_SIZE; ++i) {
52+
assert(matrix[k][i] == k + 1);
53+
}
54+
}
55+
return 0;
56+
}

0 commit comments

Comments
 (0)