xref: /freebsd/contrib/llvm-project/openmp/runtime/src/kmp_taskdeps.h (revision 6e75b2fbf9a03e6876e0a3c089e0b3ad71876125)
10b57cec5SDimitry Andric /*
20b57cec5SDimitry Andric  * kmp_taskdeps.h
30b57cec5SDimitry Andric  */
40b57cec5SDimitry Andric 
50b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
80b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
90b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef KMP_TASKDEPS_H
140b57cec5SDimitry Andric #define KMP_TASKDEPS_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "kmp.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #define KMP_ACQUIRE_DEPNODE(gtid, n) __kmp_acquire_lock(&(n)->dn.lock, (gtid))
190b57cec5SDimitry Andric #define KMP_RELEASE_DEPNODE(gtid, n) __kmp_release_lock(&(n)->dn.lock, (gtid))
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric static inline void __kmp_node_deref(kmp_info_t *thread, kmp_depnode_t *node) {
220b57cec5SDimitry Andric   if (!node)
230b57cec5SDimitry Andric     return;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric   kmp_int32 n = KMP_ATOMIC_DEC(&node->dn.nrefs) - 1;
26*6e75b2fbSDimitry Andric   KMP_DEBUG_ASSERT(n >= 0);
270b57cec5SDimitry Andric   if (n == 0) {
280b57cec5SDimitry Andric     KMP_ASSERT(node->dn.nrefs == 0);
290b57cec5SDimitry Andric #if USE_FAST_MEMORY
300b57cec5SDimitry Andric     __kmp_fast_free(thread, node);
310b57cec5SDimitry Andric #else
320b57cec5SDimitry Andric     __kmp_thread_free(thread, node);
330b57cec5SDimitry Andric #endif
340b57cec5SDimitry Andric   }
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric static inline void __kmp_depnode_list_free(kmp_info_t *thread,
380b57cec5SDimitry Andric                                            kmp_depnode_list *list) {
390b57cec5SDimitry Andric   kmp_depnode_list *next;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   for (; list; list = next) {
420b57cec5SDimitry Andric     next = list->next;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric     __kmp_node_deref(thread, list->node);
450b57cec5SDimitry Andric #if USE_FAST_MEMORY
460b57cec5SDimitry Andric     __kmp_fast_free(thread, list);
470b57cec5SDimitry Andric #else
480b57cec5SDimitry Andric     __kmp_thread_free(thread, list);
490b57cec5SDimitry Andric #endif
500b57cec5SDimitry Andric   }
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric static inline void __kmp_dephash_free_entries(kmp_info_t *thread,
540b57cec5SDimitry Andric                                               kmp_dephash_t *h) {
550b57cec5SDimitry Andric   for (size_t i = 0; i < h->size; i++) {
560b57cec5SDimitry Andric     if (h->buckets[i]) {
570b57cec5SDimitry Andric       kmp_dephash_entry_t *next;
580b57cec5SDimitry Andric       for (kmp_dephash_entry_t *entry = h->buckets[i]; entry; entry = next) {
590b57cec5SDimitry Andric         next = entry->next_in_bucket;
60fe6060f1SDimitry Andric         __kmp_depnode_list_free(thread, entry->last_set);
61fe6060f1SDimitry Andric         __kmp_depnode_list_free(thread, entry->prev_set);
620b57cec5SDimitry Andric         __kmp_node_deref(thread, entry->last_out);
630b57cec5SDimitry Andric         if (entry->mtx_lock) {
640b57cec5SDimitry Andric           __kmp_destroy_lock(entry->mtx_lock);
650b57cec5SDimitry Andric           __kmp_free(entry->mtx_lock);
660b57cec5SDimitry Andric         }
670b57cec5SDimitry Andric #if USE_FAST_MEMORY
680b57cec5SDimitry Andric         __kmp_fast_free(thread, entry);
690b57cec5SDimitry Andric #else
700b57cec5SDimitry Andric         __kmp_thread_free(thread, entry);
710b57cec5SDimitry Andric #endif
720b57cec5SDimitry Andric       }
730b57cec5SDimitry Andric       h->buckets[i] = 0;
740b57cec5SDimitry Andric     }
750b57cec5SDimitry Andric   }
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric static inline void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h) {
790b57cec5SDimitry Andric   __kmp_dephash_free_entries(thread, h);
800b57cec5SDimitry Andric #if USE_FAST_MEMORY
810b57cec5SDimitry Andric   __kmp_fast_free(thread, h);
820b57cec5SDimitry Andric #else
830b57cec5SDimitry Andric   __kmp_thread_free(thread, h);
840b57cec5SDimitry Andric #endif
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
87fe6060f1SDimitry Andric extern void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start);
88fe6060f1SDimitry Andric 
890b57cec5SDimitry Andric static inline void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
900b57cec5SDimitry Andric   kmp_info_t *thread = __kmp_threads[gtid];
910b57cec5SDimitry Andric   kmp_depnode_t *node = task->td_depnode;
920b57cec5SDimitry Andric 
93e8d8bef9SDimitry Andric   // Check mutexinoutset dependencies, release locks
94e8d8bef9SDimitry Andric   if (UNLIKELY(node && (node->dn.mtx_num_locks < 0))) {
95e8d8bef9SDimitry Andric     // negative num_locks means all locks were acquired
96e8d8bef9SDimitry Andric     node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
97e8d8bef9SDimitry Andric     for (int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
98e8d8bef9SDimitry Andric       KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
99e8d8bef9SDimitry Andric       __kmp_release_lock(node->dn.mtx_locks[i], gtid);
100e8d8bef9SDimitry Andric     }
101e8d8bef9SDimitry Andric   }
102e8d8bef9SDimitry Andric 
1030b57cec5SDimitry Andric   if (task->td_dephash) {
1040b57cec5SDimitry Andric     KA_TRACE(
1050b57cec5SDimitry Andric         40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
1060b57cec5SDimitry Andric              gtid, task));
1070b57cec5SDimitry Andric     __kmp_dephash_free(thread, task->td_dephash);
1080b57cec5SDimitry Andric     task->td_dephash = NULL;
1090b57cec5SDimitry Andric   }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   if (!node)
1120b57cec5SDimitry Andric     return;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n",
1150b57cec5SDimitry Andric                 gtid, task));
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   KMP_ACQUIRE_DEPNODE(gtid, node);
1180b57cec5SDimitry Andric   node->dn.task =
1190b57cec5SDimitry Andric       NULL; // mark this task as finished, so no new dependencies are generated
1200b57cec5SDimitry Andric   KMP_RELEASE_DEPNODE(gtid, node);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   kmp_depnode_list_t *next;
123e8d8bef9SDimitry Andric   kmp_taskdata_t *next_taskdata;
1240b57cec5SDimitry Andric   for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
1250b57cec5SDimitry Andric     kmp_depnode_t *successor = p->node;
1260b57cec5SDimitry Andric     kmp_int32 npredecessors = KMP_ATOMIC_DEC(&successor->dn.npredecessors) - 1;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric     // successor task can be NULL for wait_depends or because deps are still
1290b57cec5SDimitry Andric     // being processed
1300b57cec5SDimitry Andric     if (npredecessors == 0) {
1310b57cec5SDimitry Andric       KMP_MB();
1320b57cec5SDimitry Andric       if (successor->dn.task) {
1330b57cec5SDimitry Andric         KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled "
1340b57cec5SDimitry Andric                       "for execution.\n",
1350b57cec5SDimitry Andric                       gtid, successor->dn.task, task));
136e8d8bef9SDimitry Andric         // If a regular task depending on a hidden helper task, when the
137e8d8bef9SDimitry Andric         // hidden helper task is done, the regular task should be executed by
138e8d8bef9SDimitry Andric         // its encountering team.
139e8d8bef9SDimitry Andric         if (KMP_HIDDEN_HELPER_THREAD(gtid)) {
140e8d8bef9SDimitry Andric           // Hidden helper thread can only execute hidden helper tasks
141e8d8bef9SDimitry Andric           KMP_ASSERT(task->td_flags.hidden_helper);
142e8d8bef9SDimitry Andric           next_taskdata = KMP_TASK_TO_TASKDATA(successor->dn.task);
143e8d8bef9SDimitry Andric           // If the dependent task is a regular task, we need to push to its
144e8d8bef9SDimitry Andric           // encountering thread's queue; otherwise, it can be pushed to its own
145e8d8bef9SDimitry Andric           // queue.
146e8d8bef9SDimitry Andric           if (!next_taskdata->td_flags.hidden_helper) {
147fe6060f1SDimitry Andric             __kmpc_give_task(
148fe6060f1SDimitry Andric                 successor->dn.task,
149fe6060f1SDimitry Andric                 __kmp_tid_from_gtid(next_taskdata->encountering_gtid));
150e8d8bef9SDimitry Andric           } else {
1510b57cec5SDimitry Andric             __kmp_omp_task(gtid, successor->dn.task, false);
1520b57cec5SDimitry Andric           }
153e8d8bef9SDimitry Andric         } else {
154e8d8bef9SDimitry Andric           __kmp_omp_task(gtid, successor->dn.task, false);
155e8d8bef9SDimitry Andric         }
156e8d8bef9SDimitry Andric       }
1570b57cec5SDimitry Andric     }
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric     next = p->next;
1600b57cec5SDimitry Andric     __kmp_node_deref(thread, p->node);
1610b57cec5SDimitry Andric #if USE_FAST_MEMORY
1620b57cec5SDimitry Andric     __kmp_fast_free(thread, p);
1630b57cec5SDimitry Andric #else
1640b57cec5SDimitry Andric     __kmp_thread_free(thread, p);
1650b57cec5SDimitry Andric #endif
1660b57cec5SDimitry Andric   }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   __kmp_node_deref(thread, node);
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   KA_TRACE(
1710b57cec5SDimitry Andric       20,
1720b57cec5SDimitry Andric       ("__kmp_release_deps: T#%d all successors of %p notified of completion\n",
1730b57cec5SDimitry Andric        gtid, task));
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric #endif // KMP_TASKDEPS_H
177