Skip to content

Commit 38c8a30

Browse files
jtb20Munesanz
andcommitted
[OpenMP] Use ID not index to identify taskgraphs in libomp runtime
In preparation for the following patches, this patch changes the key used to identify taskgraphs from a monotonic index into an ID (stored in a linear table). Co-authored-by: Adrian Munera <[email protected]>
1 parent 736ab96 commit 38c8a30

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

openmp/runtime/src/kmp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2678,7 +2678,7 @@ typedef struct kmp_tdg_info {
26782678
extern int __kmp_tdg_dot;
26792679
extern kmp_int32 __kmp_max_tdgs;
26802680
extern kmp_tdg_info_t **__kmp_global_tdgs;
2681-
extern kmp_int32 __kmp_curr_tdg_idx;
2681+
extern kmp_int32 __kmp_curr_tdg_id;
26822682
extern kmp_int32 __kmp_successors_size;
26832683
extern std::atomic<kmp_int32> __kmp_tdg_task_id;
26842684
extern kmp_int32 __kmp_num_tdg;
@@ -4392,6 +4392,9 @@ KMP_EXPORT kmp_int32 __kmpc_start_record_task(ident_t *loc, kmp_int32 gtid,
43924392
kmp_int32 tdg_id);
43934393
KMP_EXPORT void __kmpc_end_record_task(ident_t *loc, kmp_int32 gtid,
43944394
kmp_int32 input_flags, kmp_int32 tdg_id);
4395+
KMP_EXPORT void __kmpc_taskgraph(ident_t *loc_ref, kmp_int32 gtid,
4396+
kmp_int32 input_flags, kmp_uint32 tdg_id,
4397+
void (*entry)(void *), void *args);
43954398
#endif
43964399
/* Interface to fast scalable reduce methods routines */
43974400

openmp/runtime/src/kmp_global.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ int *__kmp_nesting_nth_level;
556556
int __kmp_tdg_dot = 0;
557557
kmp_int32 __kmp_max_tdgs = 100;
558558
kmp_tdg_info_t **__kmp_global_tdgs = NULL;
559-
kmp_int32 __kmp_curr_tdg_idx =
559+
kmp_int32 __kmp_curr_tdg_id =
560560
0; // Id of the current TDG being recorded or executed
561561
kmp_int32 __kmp_num_tdg = 0;
562562
kmp_int32 __kmp_successors_size = 10; // Initial succesor size list for

openmp/runtime/src/kmp_tasking.cpp

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,11 +1431,11 @@ kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
14311431
}
14321432

14331433
#if OMPX_TASKGRAPH
1434-
kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1434+
kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_id);
14351435
if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
14361436
(task_entry != (kmp_routine_entry_t)__kmp_taskloop_task)) {
14371437
taskdata->is_taskgraph = 1;
1438-
taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1438+
taskdata->tdg = tdg;
14391439
taskdata->td_task_id = KMP_GEN_TASK_ID();
14401440
taskdata->td_tdg_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
14411441
}
@@ -2365,9 +2365,9 @@ without help of the runtime library.
23652365
*/
23662366
void *__kmpc_task_reduction_init(int gtid, int num, void *data) {
23672367
#if OMPX_TASKGRAPH
2368-
kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2368+
kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_id);
23692369
if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2370-
kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2370+
kmp_tdg_info_t *this_tdg = __kmp_find_tdg(__kmp_curr_tdg_id);
23712371
this_tdg->rec_taskred_data =
23722372
__kmp_allocate(sizeof(kmp_task_red_input_t) * num);
23732373
this_tdg->rec_num_taskred = num;
@@ -2392,14 +2392,11 @@ has two parameters, pointer to object to be initialized and pointer to omp_orig
23922392
*/
23932393
void *__kmpc_taskred_init(int gtid, int num, void *data) {
23942394
#if OMPX_TASKGRAPH
2395-
kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2395+
kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_id);
23962396
if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2397-
kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2398-
this_tdg->rec_taskred_data =
2399-
__kmp_allocate(sizeof(kmp_task_red_input_t) * num);
2400-
this_tdg->rec_num_taskred = num;
2401-
KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2402-
sizeof(kmp_task_red_input_t) * num);
2397+
tdg->rec_taskred_data = __kmp_allocate(sizeof(kmp_task_red_input_t) * num);
2398+
tdg->rec_num_taskred = num;
2399+
KMP_MEMCPY(tdg->rec_taskred_data, data, sizeof(kmp_task_red_input_t) * num);
24032400
}
24042401
#endif
24052402
return __kmp_task_reduction_init(gtid, num, (kmp_taskred_input_t *)data);
@@ -2451,7 +2448,7 @@ void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
24512448
#if OMPX_TASKGRAPH
24522449
if ((thread->th.th_current_task->is_taskgraph) &&
24532450
(!__kmp_tdg_is_recording(
2454-
__kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2451+
__kmp_find_tdg(__kmp_curr_tdg_id)->tdg_status))) {
24552452
tg = thread->th.th_current_task->td_taskgroup;
24562453
KMP_ASSERT(tg != NULL);
24572454
KMP_ASSERT(tg->reduce_data != NULL);
@@ -5232,6 +5229,24 @@ bool __kmpc_omp_has_task_team(kmp_int32 gtid) {
52325229
}
52335230

52345231
#if OMPX_TASKGRAPH
5232+
// __kmpc_taskgraph: record or replay taskgraph
5233+
// loc_ref: Location of TDG, not used yet
5234+
// gtid: Global Thread ID of the encountering thread
5235+
// input_flags: Flags associated with the TDG
5236+
// tdg_id: ID of the TDG to record, for now, incremental integer
5237+
// entry: Pointer to the entry function
5238+
// args: Pointer to the function arguments
5239+
void __kmpc_taskgraph(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 input_flags,
5240+
kmp_uint32 tdg_id, void (*entry)(void *), void *args) {
5241+
kmp_int32 res = __kmpc_start_record_task(loc_ref, gtid, input_flags, tdg_id);
5242+
// When res = 1, we either start recording or only execute tasks
5243+
// without recording. Need to execute entry function in both cases.
5244+
if (res)
5245+
entry(args);
5246+
5247+
__kmpc_end_record_task(loc_ref, gtid, input_flags, tdg_id);
5248+
}
5249+
52355250
// __kmp_find_tdg: identify a TDG through its ID
52365251
// tdg_id: ID of the TDG
52375252
// returns: If a TDG corresponding to this ID is found and not
@@ -5245,9 +5260,14 @@ static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
52455260
__kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
52465261
sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
52475262

5248-
if ((__kmp_global_tdgs[tdg_id]) &&
5249-
(__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5250-
res = __kmp_global_tdgs[tdg_id];
5263+
for (kmp_int32 i = 0; i < __kmp_num_tdg; ++i) {
5264+
if ((__kmp_global_tdgs[i]) && (__kmp_global_tdgs[i]->tdg_id == tdg_id) &&
5265+
(__kmp_global_tdgs[i]->tdg_status != KMP_TDG_NONE)) {
5266+
res = __kmp_global_tdgs[i];
5267+
__kmp_curr_tdg_id = tdg_id;
5268+
break;
5269+
}
5270+
}
52515271
return res;
52525272
}
52535273

@@ -5256,7 +5276,8 @@ static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
52565276
// gtid: Global Thread ID
52575277
void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg, kmp_int32 gtid) {
52585278
kmp_int32 tdg_id = tdg->tdg_id;
5259-
KA_TRACE(10, ("__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5279+
KA_TRACE(10, ("__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n",
5280+
__kmp_get_gtid(), tdg_id));
52605281

52615282
char file_name[20];
52625283
sprintf(file_name, "tdg_%d.dot", tdg_id);
@@ -5282,7 +5303,8 @@ void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg, kmp_int32 gtid) {
52825303
}
52835304
}
52845305
fprintf(tdg_file, "}");
5285-
KA_TRACE(10, ("__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5306+
KA_TRACE(10, ("__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n",
5307+
__kmp_get_gtid(), tdg_id));
52865308
}
52875309

52885310
// __kmp_exec_tdg: launch the execution of a previous
@@ -5347,7 +5369,7 @@ static inline void __kmp_start_record(kmp_int32 gtid,
53475369
kmp_int32 tdg_id) {
53485370
kmp_tdg_info_t *tdg =
53495371
(kmp_tdg_info_t *)__kmp_allocate(sizeof(kmp_tdg_info_t));
5350-
__kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5372+
__kmp_global_tdgs[__kmp_num_tdg - 1] = tdg;
53515373
// Initializing the TDG structure
53525374
tdg->tdg_id = tdg_id;
53535375
tdg->map_size = INIT_MAPSIZE;
@@ -5372,7 +5394,7 @@ static inline void __kmp_start_record(kmp_int32 gtid,
53725394
KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
53735395
}
53745396

5375-
__kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5397+
tdg->record_map = this_record_map;
53765398
}
53775399

53785400
// __kmpc_start_record_task: Wrapper around __kmp_start_record to mark
@@ -5406,10 +5428,14 @@ kmp_int32 __kmpc_start_record_task(ident_t *loc_ref, kmp_int32 gtid,
54065428
__kmp_exec_tdg(gtid, tdg);
54075429
res = 0;
54085430
} else {
5409-
__kmp_curr_tdg_idx = tdg_id;
5410-
KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5411-
__kmp_start_record(gtid, flags, tdg_id);
5412-
__kmp_num_tdg++;
5431+
if (__kmp_num_tdg < __kmp_max_tdgs) {
5432+
__kmp_curr_tdg_id = tdg_id;
5433+
__kmp_num_tdg++;
5434+
KMP_DEBUG_ASSERT(__kmp_num_tdg <= __kmp_max_tdgs);
5435+
__kmp_start_record(gtid, flags, tdg_id);
5436+
}
5437+
// if no TDG found, need to execute the task
5438+
// even not recording
54135439
res = 1;
54145440
}
54155441
KA_TRACE(10, ("__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",

0 commit comments

Comments
 (0)