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