LLVM OpenMP* Runtime Library
Loading...
Searching...
No Matches
kmp_taskdeps.cpp
1/*
2 * kmp_taskdeps.cpp
3 */
4
5//===----------------------------------------------------------------------===//
6//
7// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10//
11//===----------------------------------------------------------------------===//
12
13//#define KMP_SUPPORT_GRAPH_OUTPUT 1
14
15#include "kmp.h"
16#include "kmp_io.h"
17#include "kmp_wait_release.h"
18#include "kmp_taskdeps.h"
19#if OMPT_SUPPORT
20#include "ompt-specific.h"
21#endif
22
23// TODO: Improve memory allocation? keep a list of pre-allocated structures?
24// allocate in blocks? re-use list finished list entries?
25// TODO: don't use atomic ref counters for stack-allocated nodes.
26// TODO: find an alternate to atomic refs for heap-allocated nodes?
27// TODO: Finish graph output support
28// TODO: kmp_lock_t seems a tad to big (and heavy weight) for this. Check other
29// runtime locks
30// TODO: Any ITT support needed?
31
32#ifdef KMP_SUPPORT_GRAPH_OUTPUT
33static std::atomic<kmp_int32> kmp_node_id_seed = 0;
34#endif
35
36static void __kmp_init_node(kmp_depnode_t *node, bool on_stack) {
37 node->dn.successors = NULL;
38 node->dn.task = NULL; // will point to the right task
39 // once dependences have been processed
40 for (int i = 0; i < MAX_MTX_DEPS; ++i)
41 node->dn.mtx_locks[i] = NULL;
42 node->dn.mtx_num_locks = 0;
43 __kmp_init_lock(&node->dn.lock);
44 // Init creates the first reference. Bit 0 indicates that this node
45 // resides on the stack. The refcount is incremented and decremented in
46 // steps of two, maintaining use of even numbers for heap nodes and odd
47 // numbers for stack nodes.
48 KMP_ATOMIC_ST_RLX(&node->dn.nrefs, on_stack ? 3 : 2);
49#ifdef KMP_SUPPORT_GRAPH_OUTPUT
50 node->dn.id = KMP_ATOMIC_INC(&kmp_node_id_seed);
51#endif
52#if USE_ITT_BUILD && USE_ITT_NOTIFY
53 __itt_sync_create(node, "OMP task dep node", NULL, 0);
54#endif
55}
56
57static inline kmp_depnode_t *__kmp_node_ref(kmp_depnode_t *node) {
58 KMP_ATOMIC_ADD(&node->dn.nrefs, 2);
59 return node;
60}
61
62enum { KMP_DEPHASH_OTHER_SIZE = 97, KMP_DEPHASH_MASTER_SIZE = 997 };
63
64size_t sizes[] = {997, 2003, 4001, 8191, 16001, 32003, 64007, 131071, 270029};
65const size_t MAX_GEN = 8;
66
67static inline size_t __kmp_dephash_hash(kmp_intptr_t addr, size_t hsize) {
68 // TODO alternate to try: set = (((Addr64)(addrUsefulBits * 9.618)) %
69 // m_num_sets );
70 return ((addr >> 6) ^ (addr >> 2)) % hsize;
71}
72
73static kmp_dephash_t *__kmp_dephash_extend(kmp_info_t *thread,
74 kmp_dephash_t *current_dephash) {
75 kmp_dephash_t *h;
76
77 size_t gen = current_dephash->generation + 1;
78 if (gen >= MAX_GEN)
79 return current_dephash;
80 size_t new_size = sizes[gen];
81
82 size_t size_to_allocate =
83 new_size * sizeof(kmp_dephash_entry_t *) + sizeof(kmp_dephash_t);
84
85#if USE_FAST_MEMORY
86 h = (kmp_dephash_t *)__kmp_fast_allocate(thread, size_to_allocate);
87#else
88 h = (kmp_dephash_t *)__kmp_thread_malloc(thread, size_to_allocate);
89#endif
90
91 h->size = new_size;
92 h->nelements = current_dephash->nelements;
93 h->buckets = (kmp_dephash_entry **)(h + 1);
94 h->generation = gen;
95 h->nconflicts = 0;
96 h->last_all = current_dephash->last_all;
97
98 // make sure buckets are properly initialized
99 for (size_t i = 0; i < new_size; i++) {
100 h->buckets[i] = NULL;
101 }
102
103 // insert existing elements in the new table
104 for (size_t i = 0; i < current_dephash->size; i++) {
105 kmp_dephash_entry_t *next, *entry;
106 for (entry = current_dephash->buckets[i]; entry; entry = next) {
107 next = entry->next_in_bucket;
108 // Compute the new hash using the new size, and insert the entry in
109 // the new bucket.
110 size_t new_bucket = __kmp_dephash_hash(entry->addr, h->size);
111 entry->next_in_bucket = h->buckets[new_bucket];
112 if (entry->next_in_bucket) {
113 h->nconflicts++;
114 }
115 h->buckets[new_bucket] = entry;
116 }
117 }
118
119 // Free old hash table
120#if USE_FAST_MEMORY
121 __kmp_fast_free(thread, current_dephash);
122#else
123 __kmp_thread_free(thread, current_dephash);
124#endif
125
126 return h;
127}
128
129static kmp_dephash_t *__kmp_dephash_create(kmp_info_t *thread,
130 kmp_taskdata_t *current_task) {
131 kmp_dephash_t *h;
132
133 size_t h_size;
134
135 if (current_task->td_flags.tasktype == TASK_IMPLICIT)
136 h_size = KMP_DEPHASH_MASTER_SIZE;
137 else
138 h_size = KMP_DEPHASH_OTHER_SIZE;
139
140 size_t size = h_size * sizeof(kmp_dephash_entry_t *) + sizeof(kmp_dephash_t);
141
142#if USE_FAST_MEMORY
143 h = (kmp_dephash_t *)__kmp_fast_allocate(thread, size);
144#else
145 h = (kmp_dephash_t *)__kmp_thread_malloc(thread, size);
146#endif
147 h->size = h_size;
148
149 h->generation = 0;
150 h->nelements = 0;
151 h->nconflicts = 0;
152 h->buckets = (kmp_dephash_entry **)(h + 1);
153 h->last_all = NULL;
154
155 for (size_t i = 0; i < h_size; i++)
156 h->buckets[i] = 0;
157
158 return h;
159}
160
161static kmp_dephash_entry *__kmp_dephash_find(kmp_info_t *thread,
162 kmp_dephash_t **hash,
163 kmp_intptr_t addr) {
164 kmp_dephash_t *h = *hash;
165 if (h->nelements != 0 && h->nconflicts / h->size >= 1) {
166 *hash = __kmp_dephash_extend(thread, h);
167 h = *hash;
168 }
169 size_t bucket = __kmp_dephash_hash(addr, h->size);
170
171 kmp_dephash_entry_t *entry;
172 for (entry = h->buckets[bucket]; entry; entry = entry->next_in_bucket)
173 if (entry->addr == addr)
174 break;
175
176 if (entry == NULL) {
177// create entry. This is only done by one thread so no locking required
178#if USE_FAST_MEMORY
179 entry = (kmp_dephash_entry_t *)__kmp_fast_allocate(
180 thread, sizeof(kmp_dephash_entry_t));
181#else
182 entry = (kmp_dephash_entry_t *)__kmp_thread_malloc(
183 thread, sizeof(kmp_dephash_entry_t));
184#endif
185 entry->addr = addr;
186 if (!h->last_all) // no predecessor task with omp_all_memory dependence
187 entry->last_out = NULL;
188 else // else link the omp_all_memory depnode to the new entry
189 entry->last_out = __kmp_node_ref(h->last_all);
190 entry->last_set = NULL;
191 entry->prev_set = NULL;
192 entry->last_flag = 0;
193 entry->mtx_lock = NULL;
194 entry->next_in_bucket = h->buckets[bucket];
195 h->buckets[bucket] = entry;
196 h->nelements++;
197 if (entry->next_in_bucket)
198 h->nconflicts++;
199 }
200 return entry;
201}
202
203static kmp_depnode_list_t *__kmp_add_node(kmp_info_t *thread,
204 kmp_depnode_list_t *list,
205 kmp_depnode_t *node) {
206 kmp_depnode_list_t *new_head;
207
208#if USE_FAST_MEMORY
209 new_head = (kmp_depnode_list_t *)__kmp_fast_allocate(
210 thread, sizeof(kmp_depnode_list_t));
211#else
212 new_head = (kmp_depnode_list_t *)__kmp_thread_malloc(
213 thread, sizeof(kmp_depnode_list_t));
214#endif
215
216 new_head->node = __kmp_node_ref(node);
217 new_head->next = list;
218
219 return new_head;
220}
221
222static inline void __kmp_track_dependence(kmp_int32 gtid, kmp_depnode_t *source,
223 kmp_depnode_t *sink,
224 kmp_task_t *sink_task) {
225#if OMPX_TASKGRAPH
226 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
227 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
228 if (source->dn.task && sink_task) {
229 // Not supporting dependency between two tasks that one is within the TDG
230 // and the other is not
231 KMP_ASSERT(task_source->is_taskgraph == task_sink->is_taskgraph);
232 }
233 if (task_sink->is_taskgraph &&
234 __kmp_tdg_is_recording(task_sink->tdg->tdg_status)) {
235 kmp_node_info_t *source_info =
236 &task_sink->tdg->record_map[task_source->td_task_id];
237 bool exists = false;
238 for (int i = 0; i < source_info->nsuccessors; i++) {
239 if (source_info->successors[i] == task_sink->td_task_id) {
240 exists = true;
241 break;
242 }
243 }
244 if (!exists) {
245 if (source_info->nsuccessors >= source_info->successors_size) {
246 source_info->successors_size = 2 * source_info->successors_size;
247 kmp_int32 *old_succ_ids = source_info->successors;
248 kmp_int32 *new_succ_ids = (kmp_int32 *)__kmp_allocate(
249 source_info->successors_size * sizeof(kmp_int32));
250 source_info->successors = new_succ_ids;
251 __kmp_free(old_succ_ids);
252 }
253
254 source_info->successors[source_info->nsuccessors] = task_sink->td_task_id;
255 source_info->nsuccessors++;
256
257 kmp_node_info_t *sink_info =
258 &(task_sink->tdg->record_map[task_sink->td_task_id]);
259 sink_info->npredecessors++;
260 }
261 }
262#endif
263#ifdef KMP_SUPPORT_GRAPH_OUTPUT
264 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
265 // do not use sink->dn.task as that is only filled after the dependences
266 // are already processed!
267 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
268
269 __kmp_printf("%d(%s) -> %d(%s)\n", source->dn.id,
270 task_source->td_ident->psource, sink->dn.id,
271 task_sink->td_ident->psource);
272#endif
273#if OMPT_SUPPORT && OMPT_OPTIONAL
274 /* OMPT tracks dependences between task (a=source, b=sink) in which
275 task a blocks the execution of b through the ompt_new_dependence_callback
276 */
277 if (ompt_enabled.ompt_callback_task_dependence) {
278 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
279 ompt_data_t *sink_data;
280 if (sink_task)
281 sink_data = &(KMP_TASK_TO_TASKDATA(sink_task)->ompt_task_info.task_data);
282 else
283 sink_data = &__kmp_threads[gtid]->th.ompt_thread_info.task_data;
284
285 ompt_callbacks.ompt_callback(ompt_callback_task_dependence)(
286 &(task_source->ompt_task_info.task_data), sink_data);
287 }
288#endif /* OMPT_SUPPORT && OMPT_OPTIONAL */
289}
290
291kmp_base_depnode_t *__kmpc_task_get_depnode(kmp_task_t *task) {
292 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
293 return td->td_depnode ? &(td->td_depnode->dn) : NULL;
294}
295
296kmp_depnode_list_t *__kmpc_task_get_successors(kmp_task_t *task) {
297 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
298 return td->td_depnode->dn.successors;
299}
300
301static inline kmp_int32
302__kmp_depnode_link_successor(kmp_int32 gtid, kmp_info_t *thread,
303 kmp_task_t *task, kmp_depnode_t *node,
304 kmp_depnode_list_t *plist) {
305 if (!plist)
306 return 0;
307 kmp_int32 npredecessors = 0;
308 // link node as successor of list elements
309 for (kmp_depnode_list_t *p = plist; p; p = p->next) {
310 kmp_depnode_t *dep = p->node;
311#if OMPX_TASKGRAPH
312 kmp_tdg_status tdg_status = KMP_TDG_NONE;
313 if (task) {
314 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
315 if (td->is_taskgraph)
316 tdg_status = KMP_TASK_TO_TASKDATA(task)->tdg->tdg_status;
317 if (__kmp_tdg_is_recording(tdg_status))
318 __kmp_track_dependence(gtid, dep, node, task);
319 }
320#endif
321 if (dep->dn.task) {
322 KMP_ACQUIRE_DEPNODE(gtid, dep);
323 if (dep->dn.task) {
324 if (!dep->dn.successors || dep->dn.successors->node != node) {
325#if OMPX_TASKGRAPH
326 if (!(__kmp_tdg_is_recording(tdg_status)) && task)
327#endif
328 __kmp_track_dependence(gtid, dep, node, task);
329 dep->dn.successors = __kmp_add_node(thread, dep->dn.successors, node);
330 KA_TRACE(40, ("__kmp_process_deps: T#%d adding dependence from %p to "
331 "%p\n",
332 gtid, KMP_TASK_TO_TASKDATA(dep->dn.task),
333 KMP_TASK_TO_TASKDATA(task)));
334 npredecessors++;
335 }
336 }
337 KMP_RELEASE_DEPNODE(gtid, dep);
338 }
339 }
340 return npredecessors;
341}
342
343// Add the edge 'sink' -> 'source' in the task dependency graph
344static inline kmp_int32 __kmp_depnode_link_successor(kmp_int32 gtid,
345 kmp_info_t *thread,
346 kmp_task_t *task,
347 kmp_depnode_t *source,
348 kmp_depnode_t *sink) {
349 if (!sink)
350 return 0;
351 kmp_int32 npredecessors = 0;
352#if OMPX_TASKGRAPH
353 kmp_tdg_status tdg_status = KMP_TDG_NONE;
354 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
355 if (task) {
356 if (td->is_taskgraph)
357 tdg_status = KMP_TASK_TO_TASKDATA(task)->tdg->tdg_status;
358 if (__kmp_tdg_is_recording(tdg_status) && sink->dn.task)
359 __kmp_track_dependence(gtid, sink, source, task);
360 }
361#endif
362 if (sink->dn.task) {
363 // synchronously add source to sink' list of successors
364 KMP_ACQUIRE_DEPNODE(gtid, sink);
365 if (sink->dn.task) {
366 if (!sink->dn.successors || sink->dn.successors->node != source) {
367#if OMPX_TASKGRAPH
368 if (!(__kmp_tdg_is_recording(tdg_status)) && task)
369#endif
370 __kmp_track_dependence(gtid, sink, source, task);
371 sink->dn.successors = __kmp_add_node(thread, sink->dn.successors, source);
372 KA_TRACE(40, ("__kmp_process_deps: T#%d adding dependence from %p to "
373 "%p\n",
374 gtid, KMP_TASK_TO_TASKDATA(sink->dn.task),
375 KMP_TASK_TO_TASKDATA(task)));
376#if OMPX_TASKGRAPH
377 if (__kmp_tdg_is_recording(tdg_status)) {
378 kmp_taskdata_t *tdd = KMP_TASK_TO_TASKDATA(sink->dn.task);
379 if (tdd->is_taskgraph) {
380 if (tdd->td_flags.onced)
381 // decrement npredecessors if sink->dn.task belongs to a taskgraph
382 // and
383 // 1) the task is reset to its initial state (by kmp_free_task) or
384 // 2) the task is complete but not yet reset
385 npredecessors--;
386 }
387 }
388#endif
389 npredecessors++;
390 }
391 }
392 KMP_RELEASE_DEPNODE(gtid, sink);
393 }
394 return npredecessors;
395}
396
397static inline kmp_int32
398__kmp_process_dep_all(kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t *h,
399 bool dep_barrier, kmp_task_t *task) {
400 KA_TRACE(30, ("__kmp_process_dep_all: T#%d processing dep_all, "
401 "dep_barrier = %d\n",
402 gtid, dep_barrier));
403 kmp_info_t *thread = __kmp_threads[gtid];
404 kmp_int32 npredecessors = 0;
405
406 // process previous omp_all_memory node if any
407 npredecessors +=
408 __kmp_depnode_link_successor(gtid, thread, task, node, h->last_all);
409 __kmp_node_deref(thread, h->last_all);
410 if (!dep_barrier) {
411 h->last_all = __kmp_node_ref(node);
412 } else {
413 // if this is a sync point in the serial sequence, then the previous
414 // outputs are guaranteed to be completed after the execution of this
415 // task so the previous output nodes can be cleared.
416 h->last_all = NULL;
417 }
418
419 // process all regular dependences
420 for (size_t i = 0; i < h->size; i++) {
421 kmp_dephash_entry_t *info = h->buckets[i];
422 if (!info) // skip empty slots in dephash
423 continue;
424 for (; info; info = info->next_in_bucket) {
425 // for each entry the omp_all_memory works as OUT dependence
426 kmp_depnode_t *last_out = info->last_out;
427 kmp_depnode_list_t *last_set = info->last_set;
428 kmp_depnode_list_t *prev_set = info->prev_set;
429 if (last_set) {
430 npredecessors +=
431 __kmp_depnode_link_successor(gtid, thread, task, node, last_set);
432 __kmp_depnode_list_free(thread, last_set);
433 __kmp_depnode_list_free(thread, prev_set);
434 info->last_set = NULL;
435 info->prev_set = NULL;
436 info->last_flag = 0; // no sets in this dephash entry
437 } else {
438 npredecessors +=
439 __kmp_depnode_link_successor(gtid, thread, task, node, last_out);
440 }
441 __kmp_node_deref(thread, last_out);
442 if (!dep_barrier) {
443 info->last_out = __kmp_node_ref(node);
444 } else {
445 info->last_out = NULL;
446 }
447 }
448 }
449 KA_TRACE(30, ("__kmp_process_dep_all: T#%d found %d predecessors\n", gtid,
450 npredecessors));
451 return npredecessors;
452}
453
454template <bool filter>
455static inline kmp_int32
456__kmp_process_deps(kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t **hash,
457 bool dep_barrier, kmp_int32 ndeps,
458 kmp_depend_info_t *dep_list, kmp_task_t *task) {
459 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d processing %d dependences : "
460 "dep_barrier = %d\n",
461 filter, gtid, ndeps, dep_barrier));
462
463 kmp_info_t *thread = __kmp_threads[gtid];
464 kmp_int32 npredecessors = 0;
465 for (kmp_int32 i = 0; i < ndeps; i++) {
466 const kmp_depend_info_t *dep = &dep_list[i];
467
468 if (filter && dep->base_addr == 0)
469 continue; // skip filtered entries
470
471 kmp_dephash_entry_t *info =
472 __kmp_dephash_find(thread, hash, dep->base_addr);
473 kmp_depnode_t *last_out = info->last_out;
474 kmp_depnode_list_t *last_set = info->last_set;
475 kmp_depnode_list_t *prev_set = info->prev_set;
476
477 if (dep->flags.out) { // out or inout --> clean lists if any
478 if (last_set) {
479 npredecessors +=
480 __kmp_depnode_link_successor(gtid, thread, task, node, last_set);
481 __kmp_depnode_list_free(thread, last_set);
482 __kmp_depnode_list_free(thread, prev_set);
483 info->last_set = NULL;
484 info->prev_set = NULL;
485 info->last_flag = 0; // no sets in this dephash entry
486 } else {
487 npredecessors +=
488 __kmp_depnode_link_successor(gtid, thread, task, node, last_out);
489 }
490 __kmp_node_deref(thread, last_out);
491 if (!dep_barrier) {
492 info->last_out = __kmp_node_ref(node);
493 } else {
494 // if this is a sync point in the serial sequence, then the previous
495 // outputs are guaranteed to be completed after the execution of this
496 // task so the previous output nodes can be cleared.
497 info->last_out = NULL;
498 }
499 } else { // either IN or MTX or SET
500 if (info->last_flag == 0 || info->last_flag == dep->flag) {
501 // last_set either didn't exist or of same dep kind
502 // link node as successor of the last_out if any
503 npredecessors +=
504 __kmp_depnode_link_successor(gtid, thread, task, node, last_out);
505 // link node as successor of all nodes in the prev_set if any
506 npredecessors +=
507 __kmp_depnode_link_successor(gtid, thread, task, node, prev_set);
508 if (dep_barrier) {
509 // clean last_out and prev_set if any; don't touch last_set
510 __kmp_node_deref(thread, last_out);
511 info->last_out = NULL;
512 __kmp_depnode_list_free(thread, prev_set);
513 info->prev_set = NULL;
514 }
515 } else { // last_set is of different dep kind, make it prev_set
516 // link node as successor of all nodes in the last_set
517 npredecessors +=
518 __kmp_depnode_link_successor(gtid, thread, task, node, last_set);
519 // clean last_out if any
520 __kmp_node_deref(thread, last_out);
521 info->last_out = NULL;
522 // clean prev_set if any
523 __kmp_depnode_list_free(thread, prev_set);
524 if (!dep_barrier) {
525 // move last_set to prev_set, new last_set will be allocated
526 info->prev_set = last_set;
527 } else {
528 info->prev_set = NULL;
529 info->last_flag = 0;
530 }
531 info->last_set = NULL;
532 }
533 // for dep_barrier last_flag value should remain:
534 // 0 if last_set is empty, unchanged otherwise
535 if (!dep_barrier) {
536 info->last_flag = dep->flag; // store dep kind of the last_set
537 info->last_set = __kmp_add_node(thread, info->last_set, node);
538 }
539 // check if we are processing MTX dependency
540 if (dep->flag == KMP_DEP_MTX) {
541 if (info->mtx_lock == NULL) {
542 info->mtx_lock = (kmp_lock_t *)__kmp_allocate(sizeof(kmp_lock_t));
543 __kmp_init_lock(info->mtx_lock);
544 }
545 KMP_DEBUG_ASSERT(node->dn.mtx_num_locks < MAX_MTX_DEPS);
546 kmp_int32 m;
547 // Save lock in node's array
548 for (m = 0; m < MAX_MTX_DEPS; ++m) {
549 // sort pointers in decreasing order to avoid potential livelock
550 if (node->dn.mtx_locks[m] < info->mtx_lock) {
551 KMP_DEBUG_ASSERT(!node->dn.mtx_locks[node->dn.mtx_num_locks]);
552 for (int n = node->dn.mtx_num_locks; n > m; --n) {
553 // shift right all lesser non-NULL pointers
554 KMP_DEBUG_ASSERT(node->dn.mtx_locks[n - 1] != NULL);
555 node->dn.mtx_locks[n] = node->dn.mtx_locks[n - 1];
556 }
557 node->dn.mtx_locks[m] = info->mtx_lock;
558 break;
559 }
560 }
561 KMP_DEBUG_ASSERT(m < MAX_MTX_DEPS); // must break from loop
562 node->dn.mtx_num_locks++;
563 }
564 }
565 }
566 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d found %d predecessors\n", filter,
567 gtid, npredecessors));
568 return npredecessors;
569}
570
571#define NO_DEP_BARRIER (false)
572#define DEP_BARRIER (true)
573
574// returns true if the task has any outstanding dependence
575static bool __kmp_check_deps(kmp_int32 gtid, kmp_depnode_t *node,
576 kmp_task_t *task, kmp_dephash_t **hash,
577 bool dep_barrier, kmp_int32 ndeps,
578 kmp_depend_info_t *dep_list,
579 kmp_int32 ndeps_noalias,
580 kmp_depend_info_t *noalias_dep_list) {
581 int i, n_mtxs = 0, dep_all = 0;
582#if KMP_DEBUG
583 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
584#endif
585 KA_TRACE(20, ("__kmp_check_deps: T#%d checking dependences for task %p : %d "
586 "possibly aliased dependences, %d non-aliased dependences : "
587 "dep_barrier=%d .\n",
588 gtid, taskdata, ndeps, ndeps_noalias, dep_barrier));
589
590 // Filter deps in dep_list
591 // TODO: Different algorithm for large dep_list ( > 10 ? )
592 for (i = 0; i < ndeps; i++) {
593 if (dep_list[i].base_addr != 0 &&
594 dep_list[i].base_addr != (kmp_intptr_t)KMP_SIZE_T_MAX) {
595 KMP_DEBUG_ASSERT(
596 dep_list[i].flag == KMP_DEP_IN || dep_list[i].flag == KMP_DEP_OUT ||
597 dep_list[i].flag == KMP_DEP_INOUT ||
598 dep_list[i].flag == KMP_DEP_MTX || dep_list[i].flag == KMP_DEP_SET);
599 for (int j = i + 1; j < ndeps; j++) {
600 if (dep_list[i].base_addr == dep_list[j].base_addr) {
601 if (dep_list[i].flag != dep_list[j].flag) {
602 // two different dependences on same address work identical to OUT
603 dep_list[i].flag = KMP_DEP_OUT;
604 }
605 dep_list[j].base_addr = 0; // Mark j element as void
606 }
607 }
608 if (dep_list[i].flag == KMP_DEP_MTX) {
609 // limit number of mtx deps to MAX_MTX_DEPS per node
610 if (n_mtxs < MAX_MTX_DEPS && task != NULL) {
611 ++n_mtxs;
612 } else {
613 dep_list[i].flag = KMP_DEP_OUT; // downgrade mutexinoutset to inout
614 }
615 }
616 } else if (dep_list[i].flag == KMP_DEP_ALL ||
617 dep_list[i].base_addr == (kmp_intptr_t)KMP_SIZE_T_MAX) {
618 // omp_all_memory dependence can be marked by compiler by either
619 // (addr=0 && flag=0x80) (flag KMP_DEP_ALL), or (addr=-1).
620 // omp_all_memory overrides all other dependences if any
621 dep_all = 1;
622 break;
623 }
624 }
625
626 // doesn't need to be atomic as no other thread is going to be accessing this
627 // node just yet.
628 // npredecessors is set -1 to ensure that none of the releasing tasks queues
629 // this task before we have finished processing all the dependences
630 node->dn.npredecessors = -1;
631
632 // used to pack all npredecessors additions into a single atomic operation at
633 // the end
634 int npredecessors;
635
636 if (!dep_all) { // regular dependences
637 npredecessors = __kmp_process_deps<true>(gtid, node, hash, dep_barrier,
638 ndeps, dep_list, task);
639 npredecessors += __kmp_process_deps<false>(
640 gtid, node, hash, dep_barrier, ndeps_noalias, noalias_dep_list, task);
641 } else { // omp_all_memory dependence
642 npredecessors = __kmp_process_dep_all(gtid, node, *hash, dep_barrier, task);
643 }
644
645 node->dn.task = task;
646 KMP_MB();
647
648 // Account for our initial fake value
649 npredecessors++;
650
651 // Update predecessors and obtain current value to check if there are still
652 // any outstanding dependences (some tasks may have finished while we
653 // processed the dependences)
654 npredecessors =
655 node->dn.npredecessors.fetch_add(npredecessors) + npredecessors;
656
657 KA_TRACE(20, ("__kmp_check_deps: T#%d found %d predecessors for task %p \n",
658 gtid, npredecessors, taskdata));
659
660 // beyond this point the task could be queued (and executed) by a releasing
661 // task...
662 return npredecessors > 0 ? true : false;
663}
664
681kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 gtid,
682 kmp_task_t *new_task, kmp_int32 ndeps,
683 kmp_depend_info_t *dep_list,
684 kmp_int32 ndeps_noalias,
685 kmp_depend_info_t *noalias_dep_list) {
686
687 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
688 KA_TRACE(10, ("__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n", gtid,
689 loc_ref, new_taskdata));
690 __kmp_assert_valid_gtid(gtid);
691 kmp_info_t *thread = __kmp_threads[gtid];
692 kmp_taskdata_t *current_task = thread->th.th_current_task;
693
694#if OMPX_TASKGRAPH
695 // record TDG with deps
696 if (new_taskdata->is_taskgraph &&
697 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
698 kmp_tdg_info_t *tdg = new_taskdata->tdg;
699 // extend record_map if needed
700 if (new_taskdata->td_task_id >= tdg->map_size) {
701 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
702 if (new_taskdata->td_task_id >= tdg->map_size) {
703 kmp_uint old_size = tdg->map_size;
704 kmp_uint new_size = old_size * 2;
705 kmp_node_info_t *old_record = tdg->record_map;
706 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
707 new_size * sizeof(kmp_node_info_t));
708 KMP_MEMCPY(new_record, tdg->record_map,
709 old_size * sizeof(kmp_node_info_t));
710 tdg->record_map = new_record;
711
712 __kmp_free(old_record);
713
714 for (kmp_int i = old_size; i < new_size; i++) {
715 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
716 __kmp_successors_size * sizeof(kmp_int32));
717 new_record[i].task = nullptr;
718 new_record[i].successors = successorsList;
719 new_record[i].nsuccessors = 0;
720 new_record[i].npredecessors = 0;
721 new_record[i].successors_size = __kmp_successors_size;
722 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
723 }
724 // update the size at the end, so that we avoid other
725 // threads use old_record while map_size is already updated
726 tdg->map_size = new_size;
727 }
728 __kmp_release_bootstrap_lock(&tdg->graph_lock);
729 }
730 tdg->record_map[new_taskdata->td_task_id].task = new_task;
731 tdg->record_map[new_taskdata->td_task_id].parent_task =
732 new_taskdata->td_parent;
733 KMP_ATOMIC_INC(&tdg->num_tasks);
734 }
735#endif
736#if OMPT_SUPPORT
737 if (ompt_enabled.enabled) {
738 if (!current_task->ompt_task_info.frame.enter_frame.ptr)
739 current_task->ompt_task_info.frame.enter_frame.ptr =
740 OMPT_GET_FRAME_ADDRESS(0);
741 if (ompt_enabled.ompt_callback_task_create) {
742 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
743 &(current_task->ompt_task_info.task_data),
744 &(current_task->ompt_task_info.frame),
745 &(new_taskdata->ompt_task_info.task_data),
746 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 1,
747 OMPT_LOAD_OR_GET_RETURN_ADDRESS(gtid));
748 }
749
750 new_taskdata->ompt_task_info.frame.enter_frame.ptr =
751 OMPT_GET_FRAME_ADDRESS(0);
752 }
753
754#if OMPT_OPTIONAL
755 /* OMPT grab all dependences if requested by the tool */
756 if (ndeps + ndeps_noalias > 0 && ompt_enabled.ompt_callback_dependences) {
757 kmp_int32 i;
758
759 int ompt_ndeps = ndeps + ndeps_noalias;
760 ompt_dependence_t *ompt_deps = (ompt_dependence_t *)KMP_OMPT_DEPS_ALLOC(
761 thread, (ndeps + ndeps_noalias) * sizeof(ompt_dependence_t));
762
763 KMP_ASSERT(ompt_deps != NULL);
764
765 for (i = 0; i < ndeps; i++) {
766 ompt_deps[i].variable.ptr = (void *)dep_list[i].base_addr;
767 if (dep_list[i].base_addr == KMP_SIZE_T_MAX)
768 ompt_deps[i].dependence_type = ompt_dependence_type_out_all_memory;
769 else if (dep_list[i].flags.in && dep_list[i].flags.out)
770 ompt_deps[i].dependence_type = ompt_dependence_type_inout;
771 else if (dep_list[i].flags.out)
772 ompt_deps[i].dependence_type = ompt_dependence_type_out;
773 else if (dep_list[i].flags.in)
774 ompt_deps[i].dependence_type = ompt_dependence_type_in;
775 else if (dep_list[i].flags.mtx)
776 ompt_deps[i].dependence_type = ompt_dependence_type_mutexinoutset;
777 else if (dep_list[i].flags.set)
778 ompt_deps[i].dependence_type = ompt_dependence_type_inoutset;
779 else if (dep_list[i].flags.all)
780 ompt_deps[i].dependence_type = ompt_dependence_type_out_all_memory;
781 }
782 for (i = 0; i < ndeps_noalias; i++) {
783 ompt_deps[ndeps + i].variable.ptr = (void *)noalias_dep_list[i].base_addr;
784 if (noalias_dep_list[i].base_addr == KMP_SIZE_T_MAX)
785 ompt_deps[ndeps + i].dependence_type =
786 ompt_dependence_type_out_all_memory;
787 else if (noalias_dep_list[i].flags.in && noalias_dep_list[i].flags.out)
788 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_inout;
789 else if (noalias_dep_list[i].flags.out)
790 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_out;
791 else if (noalias_dep_list[i].flags.in)
792 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_in;
793 else if (noalias_dep_list[i].flags.mtx)
794 ompt_deps[ndeps + i].dependence_type =
795 ompt_dependence_type_mutexinoutset;
796 else if (noalias_dep_list[i].flags.set)
797 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_inoutset;
798 else if (noalias_dep_list[i].flags.all)
799 ompt_deps[ndeps + i].dependence_type =
800 ompt_dependence_type_out_all_memory;
801 }
802 ompt_callbacks.ompt_callback(ompt_callback_dependences)(
803 &(new_taskdata->ompt_task_info.task_data), ompt_deps, ompt_ndeps);
804 /* We can now free the allocated memory for the dependences */
805 /* For OMPD we might want to delay the free until end of this function */
806 KMP_OMPT_DEPS_FREE(thread, ompt_deps);
807 }
808#endif /* OMPT_OPTIONAL */
809#endif /* OMPT_SUPPORT */
810
811 bool serial = current_task->td_flags.team_serial ||
812 current_task->td_flags.tasking_ser ||
813 current_task->td_flags.final;
814 kmp_task_team_t *task_team = thread->th.th_task_team;
815 serial = serial &&
816 !(task_team && (task_team->tt.tt_found_proxy_tasks ||
817 task_team->tt.tt_hidden_helper_task_encountered));
818
819 if (!serial && (ndeps > 0 || ndeps_noalias > 0)) {
820 /* if no dependences have been tracked yet, create the dependence hash */
821 if (current_task->td_dephash == NULL)
822 current_task->td_dephash = __kmp_dephash_create(thread, current_task);
823
824#if USE_FAST_MEMORY
825 kmp_depnode_t *node =
826 (kmp_depnode_t *)__kmp_fast_allocate(thread, sizeof(kmp_depnode_t));
827#else
828 kmp_depnode_t *node =
829 (kmp_depnode_t *)__kmp_thread_malloc(thread, sizeof(kmp_depnode_t));
830#endif
831
832 __kmp_init_node(node, /*on_stack=*/false);
833 new_taskdata->td_depnode = node;
834
835 if (__kmp_check_deps(gtid, node, new_task, &current_task->td_dephash,
836 NO_DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
837 noalias_dep_list)) {
838 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had blocking "
839 "dependences: "
840 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
841 gtid, loc_ref, new_taskdata));
842#if OMPT_SUPPORT
843 if (ompt_enabled.enabled) {
844 current_task->ompt_task_info.frame.enter_frame = ompt_data_none;
845 }
846#endif
847 return TASK_CURRENT_NOT_QUEUED;
848 }
849 } else {
850 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d ignored dependences "
851 "for task (serialized) loc=%p task=%p\n",
852 gtid, loc_ref, new_taskdata));
853 }
854
855 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had no blocking "
856 "dependences : "
857 "loc=%p task=%p, transferring to __kmp_omp_task\n",
858 gtid, loc_ref, new_taskdata));
859
860 kmp_int32 ret = __kmp_omp_task(gtid, new_task, true);
861#if OMPT_SUPPORT
862 if (ompt_enabled.enabled) {
863 current_task->ompt_task_info.frame.enter_frame = ompt_data_none;
864 }
865#endif
866 return ret;
867}
868
869#if OMPT_SUPPORT
870void __ompt_taskwait_dep_finish(kmp_taskdata_t *current_task,
871 ompt_data_t *taskwait_task_data) {
872 if (ompt_enabled.ompt_callback_task_schedule) {
873 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
874 taskwait_task_data, ompt_taskwait_complete, NULL);
875 }
876 current_task->ompt_task_info.frame.enter_frame.ptr = NULL;
877 *taskwait_task_data = ompt_data_none;
878}
879#endif /* OMPT_SUPPORT */
880
892void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps,
893 kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
894 kmp_depend_info_t *noalias_dep_list) {
895 __kmpc_omp_taskwait_deps_51(loc_ref, gtid, ndeps, dep_list, ndeps_noalias,
896 noalias_dep_list, false);
897}
898
899/* __kmpc_omp_taskwait_deps_51 : Function for OpenMP 5.1 nowait clause.
900 Placeholder for taskwait with nowait clause.
901 Earlier code of __kmpc_omp_wait_deps() is now
902 in this function.
903*/
904void __kmpc_omp_taskwait_deps_51(ident_t *loc_ref, kmp_int32 gtid,
905 kmp_int32 ndeps, kmp_depend_info_t *dep_list,
906 kmp_int32 ndeps_noalias,
907 kmp_depend_info_t *noalias_dep_list,
908 kmp_int32 has_no_wait) {
909 KA_TRACE(10, ("__kmpc_omp_taskwait_deps(enter): T#%d loc=%p nowait#%d\n",
910 gtid, loc_ref, has_no_wait));
911 if (ndeps == 0 && ndeps_noalias == 0) {
912 KA_TRACE(10, ("__kmpc_omp_taskwait_deps(exit): T#%d has no dependences to "
913 "wait upon : loc=%p\n",
914 gtid, loc_ref));
915 return;
916 }
917 __kmp_assert_valid_gtid(gtid);
918 kmp_info_t *thread = __kmp_threads[gtid];
919 kmp_taskdata_t *current_task = thread->th.th_current_task;
920
921#if OMPT_SUPPORT
922 // this function represents a taskwait construct with depend clause
923 // We signal 4 events:
924 // - creation of the taskwait task
925 // - dependences of the taskwait task
926 // - schedule and finish of the taskwait task
927 ompt_data_t *taskwait_task_data = &thread->th.ompt_thread_info.task_data;
928 KMP_ASSERT(taskwait_task_data->ptr == NULL);
929 if (ompt_enabled.enabled) {
930 if (!current_task->ompt_task_info.frame.enter_frame.ptr)
931 current_task->ompt_task_info.frame.enter_frame.ptr =
932 OMPT_GET_FRAME_ADDRESS(0);
933 if (ompt_enabled.ompt_callback_task_create) {
934 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
935 &(current_task->ompt_task_info.task_data),
936 &(current_task->ompt_task_info.frame), taskwait_task_data,
937 ompt_task_taskwait | ompt_task_undeferred | ompt_task_mergeable, 1,
938 OMPT_LOAD_OR_GET_RETURN_ADDRESS(gtid));
939 }
940 }
941
942#if OMPT_OPTIONAL
943 /* OMPT grab all dependences if requested by the tool */
944 if (ndeps + ndeps_noalias > 0 && ompt_enabled.ompt_callback_dependences) {
945 kmp_int32 i;
946
947 int ompt_ndeps = ndeps + ndeps_noalias;
948 ompt_dependence_t *ompt_deps = (ompt_dependence_t *)KMP_OMPT_DEPS_ALLOC(
949 thread, (ndeps + ndeps_noalias) * sizeof(ompt_dependence_t));
950
951 KMP_ASSERT(ompt_deps != NULL);
952
953 for (i = 0; i < ndeps; i++) {
954 ompt_deps[i].variable.ptr = (void *)dep_list[i].base_addr;
955 if (dep_list[i].flags.in && dep_list[i].flags.out)
956 ompt_deps[i].dependence_type = ompt_dependence_type_inout;
957 else if (dep_list[i].flags.out)
958 ompt_deps[i].dependence_type = ompt_dependence_type_out;
959 else if (dep_list[i].flags.in)
960 ompt_deps[i].dependence_type = ompt_dependence_type_in;
961 else if (dep_list[i].flags.mtx)
962 ompt_deps[ndeps + i].dependence_type =
963 ompt_dependence_type_mutexinoutset;
964 else if (dep_list[i].flags.set)
965 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_inoutset;
966 }
967 for (i = 0; i < ndeps_noalias; i++) {
968 ompt_deps[ndeps + i].variable.ptr = (void *)noalias_dep_list[i].base_addr;
969 if (noalias_dep_list[i].flags.in && noalias_dep_list[i].flags.out)
970 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_inout;
971 else if (noalias_dep_list[i].flags.out)
972 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_out;
973 else if (noalias_dep_list[i].flags.in)
974 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_in;
975 else if (noalias_dep_list[i].flags.mtx)
976 ompt_deps[ndeps + i].dependence_type =
977 ompt_dependence_type_mutexinoutset;
978 else if (noalias_dep_list[i].flags.set)
979 ompt_deps[ndeps + i].dependence_type = ompt_dependence_type_inoutset;
980 }
981 ompt_callbacks.ompt_callback(ompt_callback_dependences)(
982 taskwait_task_data, ompt_deps, ompt_ndeps);
983 /* We can now free the allocated memory for the dependences */
984 /* For OMPD we might want to delay the free until end of this function */
985 KMP_OMPT_DEPS_FREE(thread, ompt_deps);
986 ompt_deps = NULL;
987 }
988#endif /* OMPT_OPTIONAL */
989#endif /* OMPT_SUPPORT */
990
991 // We can return immediately as:
992 // - dependences are not computed in serial teams (except with proxy tasks)
993 // - if the dephash is not yet created it means we have nothing to wait for
994 bool ignore = current_task->td_flags.team_serial ||
995 current_task->td_flags.tasking_ser ||
996 current_task->td_flags.final;
997 ignore =
998 ignore && thread->th.th_task_team != NULL &&
999 thread->th.th_task_team->tt.tt_found_proxy_tasks == FALSE &&
1000 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered == FALSE;
1001 ignore = ignore || current_task->td_dephash == NULL;
1002
1003 if (ignore) {
1004 KA_TRACE(10, ("__kmpc_omp_taskwait_deps(exit): T#%d has no blocking "
1005 "dependences : loc=%p\n",
1006 gtid, loc_ref));
1007#if OMPT_SUPPORT
1008 __ompt_taskwait_dep_finish(current_task, taskwait_task_data);
1009#endif /* OMPT_SUPPORT */
1010 return;
1011 }
1012
1013 kmp_depnode_t node = {0};
1014 __kmp_init_node(&node, /*on_stack=*/true);
1015
1016 if (!__kmp_check_deps(gtid, &node, NULL, &current_task->td_dephash,
1017 DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
1018 noalias_dep_list)) {
1019 KA_TRACE(10, ("__kmpc_omp_taskwait_deps(exit): T#%d has no blocking "
1020 "dependences : loc=%p\n",
1021 gtid, loc_ref));
1022#if OMPT_SUPPORT
1023 __ompt_taskwait_dep_finish(current_task, taskwait_task_data);
1024#endif /* OMPT_SUPPORT */
1025
1026 // There may still be references to this node here, due to task stealing.
1027 // Wait for them to be released.
1028 kmp_int32 nrefs;
1029 while ((nrefs = node.dn.nrefs) > 3) {
1030 KMP_DEBUG_ASSERT((nrefs & 1) == 1);
1031 KMP_YIELD(TRUE);
1032 }
1033 KMP_DEBUG_ASSERT(nrefs == 3);
1034
1035 return;
1036 }
1037
1038 int thread_finished = FALSE;
1039 kmp_flag_32<false, false> flag(
1040 (std::atomic<kmp_uint32> *)&node.dn.npredecessors, 0U);
1041 while (node.dn.npredecessors > 0) {
1042 flag.execute_tasks(thread, gtid, FALSE,
1043 &thread_finished USE_ITT_BUILD_ARG(NULL),
1044 __kmp_task_stealing_constraint);
1045 }
1046
1047 // Wait until the last __kmp_release_deps is finished before we free the
1048 // current stack frame holding the "node" variable; once its nrefs count
1049 // reaches 3 (meaning 1, since bit zero of the refcount indicates a stack
1050 // rather than a heap address), we're sure nobody else can try to reference
1051 // it again.
1052 kmp_int32 nrefs;
1053 while ((nrefs = node.dn.nrefs) > 3) {
1054 KMP_DEBUG_ASSERT((nrefs & 1) == 1);
1055 KMP_YIELD(TRUE);
1056 }
1057 KMP_DEBUG_ASSERT(nrefs == 3);
1058
1059#if OMPT_SUPPORT
1060 __ompt_taskwait_dep_finish(current_task, taskwait_task_data);
1061#endif /* OMPT_SUPPORT */
1062 KA_TRACE(10, ("__kmpc_omp_taskwait_deps(exit): T#%d finished waiting : loc=%p\
1063 \n",
1064 gtid, loc_ref));
1065}
kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list)
void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list)
Definition kmp.h:247