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 #if OMP_40_ENABLED
24 
25 // TODO: Improve memory allocation? keep a list of pre-allocated structures?
26 // allocate in blocks? re-use list finished list entries?
27 // TODO: don't use atomic ref counters for stack-allocated nodes.
28 // TODO: find an alternate to atomic refs for heap-allocated nodes?
29 // TODO: Finish graph output support
30 // TODO: kmp_lock_t seems a tad to big (and heavy weight) for this. Check other
31 // runtime locks
32 // TODO: Any ITT support needed?
33 
34 #ifdef KMP_SUPPORT_GRAPH_OUTPUT
35 static std::atomic<kmp_int32> kmp_node_id_seed = ATOMIC_VAR_INIT(0);
36 #endif
37 
38 static void __kmp_init_node(kmp_depnode_t *node) {
39   node->dn.successors = NULL;
40   node->dn.task = NULL; // will point to the rigth task
41   // once dependences have been processed
42   for (int i = 0; i < MAX_MTX_DEPS; ++i)
43     node->dn.mtx_locks[i] = NULL;
44   node->dn.mtx_num_locks = 0;
45   __kmp_init_lock(&node->dn.lock);
46   KMP_ATOMIC_ST_RLX(&node->dn.nrefs, 1); // init creates the first reference
47 #ifdef KMP_SUPPORT_GRAPH_OUTPUT
48   node->dn.id = KMP_ATOMIC_INC(&kmp_node_id_seed);
49 #endif
50 }
51 
52 static inline kmp_depnode_t *__kmp_node_ref(kmp_depnode_t *node) {
53   KMP_ATOMIC_INC(&node->dn.nrefs);
54   return node;
55 }
56 
57 enum { KMP_DEPHASH_OTHER_SIZE = 97, KMP_DEPHASH_MASTER_SIZE = 997 };
58 
59 static inline kmp_int32 __kmp_dephash_hash(kmp_intptr_t addr, size_t hsize) {
60   // TODO alternate to try: set = (((Addr64)(addrUsefulBits * 9.618)) %
61   // m_num_sets );
62   return ((addr >> 6) ^ (addr >> 2)) % hsize;
63 }
64 
65 static kmp_dephash_t *__kmp_dephash_create(kmp_info_t *thread,
66                                            kmp_taskdata_t *current_task) {
67   kmp_dephash_t *h;
68 
69   size_t h_size;
70 
71   if (current_task->td_flags.tasktype == TASK_IMPLICIT)
72     h_size = KMP_DEPHASH_MASTER_SIZE;
73   else
74     h_size = KMP_DEPHASH_OTHER_SIZE;
75 
76   kmp_int32 size =
77       h_size * sizeof(kmp_dephash_entry_t *) + sizeof(kmp_dephash_t);
78 
79 #if USE_FAST_MEMORY
80   h = (kmp_dephash_t *)__kmp_fast_allocate(thread, size);
81 #else
82   h = (kmp_dephash_t *)__kmp_thread_malloc(thread, size);
83 #endif
84   h->size = h_size;
85 
86 #ifdef KMP_DEBUG
87   h->nelements = 0;
88   h->nconflicts = 0;
89 #endif
90   h->buckets = (kmp_dephash_entry **)(h + 1);
91 
92   for (size_t i = 0; i < h_size; i++)
93     h->buckets[i] = 0;
94 
95   return h;
96 }
97 
98 #define ENTRY_LAST_INS 0
99 #define ENTRY_LAST_MTXS 1
100 
101 static kmp_dephash_entry *
102 __kmp_dephash_find(kmp_info_t *thread, kmp_dephash_t *h, kmp_intptr_t addr) {
103   kmp_int32 bucket = __kmp_dephash_hash(addr, h->size);
104 
105   kmp_dephash_entry_t *entry;
106   for (entry = h->buckets[bucket]; entry; entry = entry->next_in_bucket)
107     if (entry->addr == addr)
108       break;
109 
110   if (entry == NULL) {
111 // create entry. This is only done by one thread so no locking required
112 #if USE_FAST_MEMORY
113     entry = (kmp_dephash_entry_t *)__kmp_fast_allocate(
114         thread, sizeof(kmp_dephash_entry_t));
115 #else
116     entry = (kmp_dephash_entry_t *)__kmp_thread_malloc(
117         thread, sizeof(kmp_dephash_entry_t));
118 #endif
119     entry->addr = addr;
120     entry->last_out = NULL;
121     entry->last_ins = NULL;
122     entry->last_mtxs = NULL;
123     entry->last_flag = ENTRY_LAST_INS;
124     entry->mtx_lock = NULL;
125     entry->next_in_bucket = h->buckets[bucket];
126     h->buckets[bucket] = entry;
127 #ifdef KMP_DEBUG
128     h->nelements++;
129     if (entry->next_in_bucket)
130       h->nconflicts++;
131 #endif
132   }
133   return entry;
134 }
135 
136 static kmp_depnode_list_t *__kmp_add_node(kmp_info_t *thread,
137                                           kmp_depnode_list_t *list,
138                                           kmp_depnode_t *node) {
139   kmp_depnode_list_t *new_head;
140 
141 #if USE_FAST_MEMORY
142   new_head = (kmp_depnode_list_t *)__kmp_fast_allocate(
143       thread, sizeof(kmp_depnode_list_t));
144 #else
145   new_head = (kmp_depnode_list_t *)__kmp_thread_malloc(
146       thread, sizeof(kmp_depnode_list_t));
147 #endif
148 
149   new_head->node = __kmp_node_ref(node);
150   new_head->next = list;
151 
152   return new_head;
153 }
154 
155 static inline void __kmp_track_dependence(kmp_depnode_t *source,
156                                           kmp_depnode_t *sink,
157                                           kmp_task_t *sink_task) {
158 #ifdef KMP_SUPPORT_GRAPH_OUTPUT
159   kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
160   // do not use sink->dn.task as that is only filled after the dependencies
161   // are already processed!
162   kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
163 
164   __kmp_printf("%d(%s) -> %d(%s)\n", source->dn.id,
165                task_source->td_ident->psource, sink->dn.id,
166                task_sink->td_ident->psource);
167 #endif
168 #if OMPT_SUPPORT && OMPT_OPTIONAL
169   /* OMPT tracks dependences between task (a=source, b=sink) in which
170      task a blocks the execution of b through the ompt_new_dependence_callback
171      */
172   if (ompt_enabled.ompt_callback_task_dependence) {
173     kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
174     kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
175 
176     ompt_callbacks.ompt_callback(ompt_callback_task_dependence)(
177         &(task_source->ompt_task_info.task_data),
178         &(task_sink->ompt_task_info.task_data));
179   }
180 #endif /* OMPT_SUPPORT && OMPT_OPTIONAL */
181 }
182 
183 static inline kmp_int32
184 __kmp_depnode_link_successor(kmp_int32 gtid, kmp_info_t *thread,
185                              kmp_task_t *task, kmp_depnode_t *node,
186                              kmp_depnode_list_t *plist) {
187   if (!plist)
188     return 0;
189   kmp_int32 npredecessors = 0;
190   // link node as successor of list elements
191   for (kmp_depnode_list_t *p = plist; p; p = p->next) {
192     kmp_depnode_t *dep = p->node;
193     if (dep->dn.task) {
194       KMP_ACQUIRE_DEPNODE(gtid, dep);
195       if (dep->dn.task) {
196         __kmp_track_dependence(dep, node, task);
197         dep->dn.successors = __kmp_add_node(thread, dep->dn.successors, node);
198         KA_TRACE(40, ("__kmp_process_deps: T#%d adding dependence from %p to "
199                       "%p\n",
200                       gtid, KMP_TASK_TO_TASKDATA(dep->dn.task),
201                       KMP_TASK_TO_TASKDATA(task)));
202         npredecessors++;
203       }
204       KMP_RELEASE_DEPNODE(gtid, dep);
205     }
206   }
207   return npredecessors;
208 }
209 
210 static inline kmp_int32 __kmp_depnode_link_successor(kmp_int32 gtid,
211                                                      kmp_info_t *thread,
212                                                      kmp_task_t *task,
213                                                      kmp_depnode_t *source,
214                                                      kmp_depnode_t *sink) {
215   if (!sink)
216     return 0;
217   kmp_int32 npredecessors = 0;
218   if (sink->dn.task) {
219     // synchronously add source to sink' list of successors
220     KMP_ACQUIRE_DEPNODE(gtid, sink);
221     if (sink->dn.task) {
222       __kmp_track_dependence(sink, source, task);
223       sink->dn.successors = __kmp_add_node(thread, sink->dn.successors, source);
224       KA_TRACE(40, ("__kmp_process_deps: T#%d adding dependence from %p to "
225                     "%p\n",
226                     gtid, KMP_TASK_TO_TASKDATA(sink->dn.task),
227                     KMP_TASK_TO_TASKDATA(task)));
228       npredecessors++;
229     }
230     KMP_RELEASE_DEPNODE(gtid, sink);
231   }
232   return npredecessors;
233 }
234 
235 template <bool filter>
236 static inline kmp_int32
237 __kmp_process_deps(kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t *hash,
238                    bool dep_barrier, kmp_int32 ndeps,
239                    kmp_depend_info_t *dep_list, kmp_task_t *task) {
240   KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d processing %d dependencies : "
241                 "dep_barrier = %d\n",
242                 filter, gtid, ndeps, dep_barrier));
243 
244   kmp_info_t *thread = __kmp_threads[gtid];
245   kmp_int32 npredecessors = 0;
246   for (kmp_int32 i = 0; i < ndeps; i++) {
247     const kmp_depend_info_t *dep = &dep_list[i];
248 
249     if (filter && dep->base_addr == 0)
250       continue; // skip filtered entries
251 
252     kmp_dephash_entry_t *info =
253         __kmp_dephash_find(thread, hash, dep->base_addr);
254     kmp_depnode_t *last_out = info->last_out;
255     kmp_depnode_list_t *last_ins = info->last_ins;
256     kmp_depnode_list_t *last_mtxs = info->last_mtxs;
257 
258     if (dep->flags.out) { // out --> clean lists of ins and mtxs if any
259       if (last_ins || last_mtxs) {
260         if (info->last_flag == ENTRY_LAST_INS) { // INS were last
261           npredecessors +=
262               __kmp_depnode_link_successor(gtid, thread, task, node, last_ins);
263         } else { // MTXS were last
264           npredecessors +=
265               __kmp_depnode_link_successor(gtid, thread, task, node, last_mtxs);
266         }
267         __kmp_depnode_list_free(thread, last_ins);
268         __kmp_depnode_list_free(thread, last_mtxs);
269         info->last_ins = NULL;
270         info->last_mtxs = NULL;
271       } else {
272         npredecessors +=
273             __kmp_depnode_link_successor(gtid, thread, task, node, last_out);
274       }
275       __kmp_node_deref(thread, last_out);
276       if (dep_barrier) {
277         // if this is a sync point in the serial sequence, then the previous
278         // outputs are guaranteed to be completed after the execution of this
279         // task so the previous output nodes can be cleared.
280         info->last_out = NULL;
281       } else {
282         info->last_out = __kmp_node_ref(node);
283       }
284     } else if (dep->flags.in) {
285       // in --> link node to either last_out or last_mtxs, clean earlier deps
286       if (last_mtxs) {
287         npredecessors +=
288             __kmp_depnode_link_successor(gtid, thread, task, node, last_mtxs);
289         __kmp_node_deref(thread, last_out);
290         info->last_out = NULL;
291         if (info->last_flag == ENTRY_LAST_MTXS && last_ins) { // MTXS were last
292           // clean old INS before creating new list
293           __kmp_depnode_list_free(thread, last_ins);
294           info->last_ins = NULL;
295         }
296       } else {
297         // link node as successor of the last_out if any
298         npredecessors +=
299             __kmp_depnode_link_successor(gtid, thread, task, node, last_out);
300       }
301       info->last_flag = ENTRY_LAST_INS;
302       info->last_ins = __kmp_add_node(thread, info->last_ins, node);
303     } else {
304       KMP_DEBUG_ASSERT(dep->flags.mtx == 1);
305       // mtx --> link node to either last_out or last_ins, clean earlier deps
306       if (last_ins) {
307         npredecessors +=
308             __kmp_depnode_link_successor(gtid, thread, task, node, last_ins);
309         __kmp_node_deref(thread, last_out);
310         info->last_out = NULL;
311         if (info->last_flag == ENTRY_LAST_INS && last_mtxs) { // INS were last
312           // clean old MTXS before creating new list
313           __kmp_depnode_list_free(thread, last_mtxs);
314           info->last_mtxs = NULL;
315         }
316       } else {
317         // link node as successor of the last_out if any
318         npredecessors +=
319             __kmp_depnode_link_successor(gtid, thread, task, node, last_out);
320       }
321       info->last_flag = ENTRY_LAST_MTXS;
322       info->last_mtxs = __kmp_add_node(thread, info->last_mtxs, node);
323       if (info->mtx_lock == NULL) {
324         info->mtx_lock = (kmp_lock_t *)__kmp_allocate(sizeof(kmp_lock_t));
325         __kmp_init_lock(info->mtx_lock);
326       }
327       KMP_DEBUG_ASSERT(node->dn.mtx_num_locks < MAX_MTX_DEPS);
328       kmp_int32 m;
329       // Save lock in node's array
330       for (m = 0; m < MAX_MTX_DEPS; ++m) {
331         // sort pointers in decreasing order to avoid potential livelock
332         if (node->dn.mtx_locks[m] < info->mtx_lock) {
333           KMP_DEBUG_ASSERT(node->dn.mtx_locks[node->dn.mtx_num_locks] == NULL);
334           for (int n = node->dn.mtx_num_locks; n > m; --n) {
335             // shift right all lesser non-NULL pointers
336             KMP_DEBUG_ASSERT(node->dn.mtx_locks[n - 1] != NULL);
337             node->dn.mtx_locks[n] = node->dn.mtx_locks[n - 1];
338           }
339           node->dn.mtx_locks[m] = info->mtx_lock;
340           break;
341         }
342       }
343       KMP_DEBUG_ASSERT(m < MAX_MTX_DEPS); // must break from loop
344       node->dn.mtx_num_locks++;
345     }
346   }
347   KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d found %d predecessors\n", filter,
348                 gtid, npredecessors));
349   return npredecessors;
350 }
351 
352 #define NO_DEP_BARRIER (false)
353 #define DEP_BARRIER (true)
354 
355 // returns true if the task has any outstanding dependence
356 static bool __kmp_check_deps(kmp_int32 gtid, kmp_depnode_t *node,
357                              kmp_task_t *task, kmp_dephash_t *hash,
358                              bool dep_barrier, kmp_int32 ndeps,
359                              kmp_depend_info_t *dep_list,
360                              kmp_int32 ndeps_noalias,
361                              kmp_depend_info_t *noalias_dep_list) {
362   int i, n_mtxs = 0;
363 #if KMP_DEBUG
364   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
365 #endif
366   KA_TRACE(20, ("__kmp_check_deps: T#%d checking dependencies for task %p : %d "
367                 "possibly aliased dependencies, %d non-aliased depedencies : "
368                 "dep_barrier=%d .\n",
369                 gtid, taskdata, ndeps, ndeps_noalias, dep_barrier));
370 
371   // Filter deps in dep_list
372   // TODO: Different algorithm for large dep_list ( > 10 ? )
373   for (i = 0; i < ndeps; i++) {
374     if (dep_list[i].base_addr != 0) {
375       for (int j = i + 1; j < ndeps; j++) {
376         if (dep_list[i].base_addr == dep_list[j].base_addr) {
377           dep_list[i].flags.in |= dep_list[j].flags.in;
378           dep_list[i].flags.out |=
379               (dep_list[j].flags.out ||
380                (dep_list[i].flags.in && dep_list[j].flags.mtx) ||
381                (dep_list[i].flags.mtx && dep_list[j].flags.in));
382           dep_list[i].flags.mtx =
383               dep_list[i].flags.mtx | dep_list[j].flags.mtx &&
384               !dep_list[i].flags.out;
385           dep_list[j].base_addr = 0; // Mark j element as void
386         }
387       }
388       if (dep_list[i].flags.mtx) {
389         // limit number of mtx deps to MAX_MTX_DEPS per node
390         if (n_mtxs < MAX_MTX_DEPS && task != NULL) {
391           ++n_mtxs;
392         } else {
393           dep_list[i].flags.in = 1; // downgrade mutexinoutset to inout
394           dep_list[i].flags.out = 1;
395           dep_list[i].flags.mtx = 0;
396         }
397       }
398     }
399   }
400 
401   // doesn't need to be atomic as no other thread is going to be accessing this
402   // node just yet.
403   // npredecessors is set -1 to ensure that none of the releasing tasks queues
404   // this task before we have finished processing all the dependencies
405   node->dn.npredecessors = -1;
406 
407   // used to pack all npredecessors additions into a single atomic operation at
408   // the end
409   int npredecessors;
410 
411   npredecessors = __kmp_process_deps<true>(gtid, node, hash, dep_barrier, ndeps,
412                                            dep_list, task);
413   npredecessors += __kmp_process_deps<false>(
414       gtid, node, hash, dep_barrier, ndeps_noalias, noalias_dep_list, task);
415 
416   node->dn.task = task;
417   KMP_MB();
418 
419   // Account for our initial fake value
420   npredecessors++;
421 
422   // Update predecessors and obtain current value to check if there are still
423   // any outstandig dependences (some tasks may have finished while we processed
424   // the dependences)
425   npredecessors =
426       node->dn.npredecessors.fetch_add(npredecessors) + npredecessors;
427 
428   KA_TRACE(20, ("__kmp_check_deps: T#%d found %d predecessors for task %p \n",
429                 gtid, npredecessors, taskdata));
430 
431   // beyond this point the task could be queued (and executed) by a releasing
432   // task...
433   return npredecessors > 0 ? true : false;
434 }
435 
436 /*!
437 @ingroup TASKING
438 @param loc_ref location of the original task directive
439 @param gtid Global Thread ID of encountering thread
440 @param new_task task thunk allocated by __kmp_omp_task_alloc() for the ''new
441 task''
442 @param ndeps Number of depend items with possible aliasing
443 @param dep_list List of depend items with possible aliasing
444 @param ndeps_noalias Number of depend items with no aliasing
445 @param noalias_dep_list List of depend items with no aliasing
446 
447 @return Returns either TASK_CURRENT_NOT_QUEUED if the current task was not
448 suspendend and queued, or TASK_CURRENT_QUEUED if it was suspended and queued
449 
450 Schedule a non-thread-switchable task with dependences for execution
451 */
452 kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 gtid,
453                                     kmp_task_t *new_task, kmp_int32 ndeps,
454                                     kmp_depend_info_t *dep_list,
455                                     kmp_int32 ndeps_noalias,
456                                     kmp_depend_info_t *noalias_dep_list) {
457 
458   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
459   KA_TRACE(10, ("__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n", gtid,
460                 loc_ref, new_taskdata));
461 
462   kmp_info_t *thread = __kmp_threads[gtid];
463   kmp_taskdata_t *current_task = thread->th.th_current_task;
464 
465 #if OMPT_SUPPORT
466   if (ompt_enabled.enabled) {
467     OMPT_STORE_RETURN_ADDRESS(gtid);
468     if (!current_task->ompt_task_info.frame.enter_frame.ptr)
469       current_task->ompt_task_info.frame.enter_frame.ptr =
470           OMPT_GET_FRAME_ADDRESS(0);
471     if (ompt_enabled.ompt_callback_task_create) {
472       ompt_data_t task_data = ompt_data_none;
473       ompt_callbacks.ompt_callback(ompt_callback_task_create)(
474           current_task ? &(current_task->ompt_task_info.task_data) : &task_data,
475           current_task ? &(current_task->ompt_task_info.frame) : NULL,
476           &(new_taskdata->ompt_task_info.task_data),
477           ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 1,
478           OMPT_LOAD_RETURN_ADDRESS(gtid));
479     }
480 
481     new_taskdata->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
482   }
483 
484 #if OMPT_OPTIONAL
485   /* OMPT grab all dependences if requested by the tool */
486   if (ndeps + ndeps_noalias > 0 &&
487       ompt_enabled.ompt_callback_dependences) {
488     kmp_int32 i;
489 
490     new_taskdata->ompt_task_info.ndeps = ndeps + ndeps_noalias;
491     new_taskdata->ompt_task_info.deps =
492         (ompt_dependence_t *)KMP_OMPT_DEPS_ALLOC(
493             thread, (ndeps + ndeps_noalias) * sizeof(ompt_dependence_t));
494 
495     KMP_ASSERT(new_taskdata->ompt_task_info.deps != NULL);
496 
497     for (i = 0; i < ndeps; i++) {
498       new_taskdata->ompt_task_info.deps[i].variable.ptr =
499           (void *)dep_list[i].base_addr;
500       if (dep_list[i].flags.in && dep_list[i].flags.out)
501         new_taskdata->ompt_task_info.deps[i].dependence_type =
502             ompt_dependence_type_inout;
503       else if (dep_list[i].flags.out)
504         new_taskdata->ompt_task_info.deps[i].dependence_type =
505             ompt_dependence_type_out;
506       else if (dep_list[i].flags.in)
507         new_taskdata->ompt_task_info.deps[i].dependence_type =
508             ompt_dependence_type_in;
509     }
510     for (i = 0; i < ndeps_noalias; i++) {
511       new_taskdata->ompt_task_info.deps[ndeps + i].variable.ptr =
512           (void *)noalias_dep_list[i].base_addr;
513       if (noalias_dep_list[i].flags.in && noalias_dep_list[i].flags.out)
514         new_taskdata->ompt_task_info.deps[ndeps + i].dependence_type =
515             ompt_dependence_type_inout;
516       else if (noalias_dep_list[i].flags.out)
517         new_taskdata->ompt_task_info.deps[ndeps + i].dependence_type =
518             ompt_dependence_type_out;
519       else if (noalias_dep_list[i].flags.in)
520         new_taskdata->ompt_task_info.deps[ndeps + i].dependence_type =
521             ompt_dependence_type_in;
522     }
523     ompt_callbacks.ompt_callback(ompt_callback_dependences)(
524         &(new_taskdata->ompt_task_info.task_data),
525         new_taskdata->ompt_task_info.deps, new_taskdata->ompt_task_info.ndeps);
526     /* We can now free the allocated memory for the dependencies */
527     /* For OMPD we might want to delay the free until task_end */
528     KMP_OMPT_DEPS_FREE(thread, new_taskdata->ompt_task_info.deps);
529     new_taskdata->ompt_task_info.deps = NULL;
530     new_taskdata->ompt_task_info.ndeps = 0;
531   }
532 #endif /* OMPT_OPTIONAL */
533 #endif /* OMPT_SUPPORT */
534 
535   bool serial = current_task->td_flags.team_serial ||
536                 current_task->td_flags.tasking_ser ||
537                 current_task->td_flags.final;
538 #if OMP_45_ENABLED
539   kmp_task_team_t *task_team = thread->th.th_task_team;
540   serial = serial && !(task_team && task_team->tt.tt_found_proxy_tasks);
541 #endif
542 
543   if (!serial && (ndeps > 0 || ndeps_noalias > 0)) {
544     /* if no dependencies have been tracked yet, create the dependence hash */
545     if (current_task->td_dephash == NULL)
546       current_task->td_dephash = __kmp_dephash_create(thread, current_task);
547 
548 #if USE_FAST_MEMORY
549     kmp_depnode_t *node =
550         (kmp_depnode_t *)__kmp_fast_allocate(thread, sizeof(kmp_depnode_t));
551 #else
552     kmp_depnode_t *node =
553         (kmp_depnode_t *)__kmp_thread_malloc(thread, sizeof(kmp_depnode_t));
554 #endif
555 
556     __kmp_init_node(node);
557     new_taskdata->td_depnode = node;
558 
559     if (__kmp_check_deps(gtid, node, new_task, current_task->td_dephash,
560                          NO_DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
561                          noalias_dep_list)) {
562       KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had blocking "
563                     "dependencies: "
564                     "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
565                     gtid, loc_ref, new_taskdata));
566 #if OMPT_SUPPORT
567       if (ompt_enabled.enabled) {
568         current_task->ompt_task_info.frame.enter_frame = ompt_data_none;
569       }
570 #endif
571       return TASK_CURRENT_NOT_QUEUED;
572     }
573   } else {
574     KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d ignored dependencies "
575                   "for task (serialized)"
576                   "loc=%p task=%p\n",
577                   gtid, loc_ref, new_taskdata));
578   }
579 
580   KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had no blocking "
581                 "dependencies : "
582                 "loc=%p task=%p, transferring to __kmp_omp_task\n",
583                 gtid, loc_ref, new_taskdata));
584 
585   kmp_int32 ret = __kmp_omp_task(gtid, new_task, true);
586 #if OMPT_SUPPORT
587   if (ompt_enabled.enabled) {
588     current_task->ompt_task_info.frame.enter_frame = ompt_data_none;
589   }
590 #endif
591   return ret;
592 }
593 
594 /*!
595 @ingroup TASKING
596 @param loc_ref location of the original task directive
597 @param gtid Global Thread ID of encountering thread
598 @param ndeps Number of depend items with possible aliasing
599 @param dep_list List of depend items with possible aliasing
600 @param ndeps_noalias Number of depend items with no aliasing
601 @param noalias_dep_list List of depend items with no aliasing
602 
603 Blocks the current task until all specifies dependencies have been fulfilled.
604 */
605 void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps,
606                           kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
607                           kmp_depend_info_t *noalias_dep_list) {
608   KA_TRACE(10, ("__kmpc_omp_wait_deps(enter): T#%d loc=%p\n", gtid, loc_ref));
609 
610   if (ndeps == 0 && ndeps_noalias == 0) {
611     KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no dependencies to "
612                   "wait upon : loc=%p\n",
613                   gtid, loc_ref));
614     return;
615   }
616 
617   kmp_info_t *thread = __kmp_threads[gtid];
618   kmp_taskdata_t *current_task = thread->th.th_current_task;
619 
620   // We can return immediately as:
621   // - dependences are not computed in serial teams (except with proxy tasks)
622   // - if the dephash is not yet created it means we have nothing to wait for
623   bool ignore = current_task->td_flags.team_serial ||
624                 current_task->td_flags.tasking_ser ||
625                 current_task->td_flags.final;
626 #if OMP_45_ENABLED
627   ignore = ignore && thread->th.th_task_team != NULL &&
628            thread->th.th_task_team->tt.tt_found_proxy_tasks == FALSE;
629 #endif
630   ignore = ignore || current_task->td_dephash == NULL;
631 
632   if (ignore) {
633     KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking "
634                   "dependencies : loc=%p\n",
635                   gtid, loc_ref));
636     return;
637   }
638 
639   kmp_depnode_t node = {0};
640   __kmp_init_node(&node);
641 
642   if (!__kmp_check_deps(gtid, &node, NULL, current_task->td_dephash,
643                         DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
644                         noalias_dep_list)) {
645     KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking "
646                   "dependencies : loc=%p\n",
647                   gtid, loc_ref));
648     return;
649   }
650 
651   int thread_finished = FALSE;
652   kmp_flag_32 flag((std::atomic<kmp_uint32> *)&node.dn.npredecessors, 0U);
653   while (node.dn.npredecessors > 0) {
654     flag.execute_tasks(thread, gtid, FALSE,
655                        &thread_finished USE_ITT_BUILD_ARG(NULL),
656                        __kmp_task_stealing_constraint);
657   }
658 
659   KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d finished waiting : loc=%p\n",
660                 gtid, loc_ref));
661 }
662 
663 #endif /* OMP_40_ENABLED */
664