xref: /freebsd/contrib/llvm-project/openmp/runtime/src/kmp_taskdeps.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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;
266e75b2fbSDimitry 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   }
76*349cc55cSDimitry Andric   __kmp_node_deref(thread, h->last_all);
77*349cc55cSDimitry Andric   h->last_all = NULL;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric static inline void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h) {
810b57cec5SDimitry Andric   __kmp_dephash_free_entries(thread, h);
820b57cec5SDimitry Andric #if USE_FAST_MEMORY
830b57cec5SDimitry Andric   __kmp_fast_free(thread, h);
840b57cec5SDimitry Andric #else
850b57cec5SDimitry Andric   __kmp_thread_free(thread, h);
860b57cec5SDimitry Andric #endif
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
89fe6060f1SDimitry Andric extern void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start);
90fe6060f1SDimitry Andric 
910b57cec5SDimitry Andric static inline void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
920b57cec5SDimitry Andric   kmp_info_t *thread = __kmp_threads[gtid];
930b57cec5SDimitry Andric   kmp_depnode_t *node = task->td_depnode;
940b57cec5SDimitry Andric 
95e8d8bef9SDimitry Andric   // Check mutexinoutset dependencies, release locks
96e8d8bef9SDimitry Andric   if (UNLIKELY(node && (node->dn.mtx_num_locks < 0))) {
97e8d8bef9SDimitry Andric     // negative num_locks means all locks were acquired
98e8d8bef9SDimitry Andric     node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
99e8d8bef9SDimitry Andric     for (int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
100e8d8bef9SDimitry Andric       KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
101e8d8bef9SDimitry Andric       __kmp_release_lock(node->dn.mtx_locks[i], gtid);
102e8d8bef9SDimitry Andric     }
103e8d8bef9SDimitry Andric   }
104e8d8bef9SDimitry Andric 
1050b57cec5SDimitry Andric   if (task->td_dephash) {
1060b57cec5SDimitry Andric     KA_TRACE(
1070b57cec5SDimitry Andric         40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
1080b57cec5SDimitry Andric              gtid, task));
1090b57cec5SDimitry Andric     __kmp_dephash_free(thread, task->td_dephash);
1100b57cec5SDimitry Andric     task->td_dephash = NULL;
1110b57cec5SDimitry Andric   }
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   if (!node)
1140b57cec5SDimitry Andric     return;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n",
1170b57cec5SDimitry Andric                 gtid, task));
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   KMP_ACQUIRE_DEPNODE(gtid, node);
1200b57cec5SDimitry Andric   node->dn.task =
1210b57cec5SDimitry Andric       NULL; // mark this task as finished, so no new dependencies are generated
1220b57cec5SDimitry Andric   KMP_RELEASE_DEPNODE(gtid, node);
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   kmp_depnode_list_t *next;
125e8d8bef9SDimitry Andric   kmp_taskdata_t *next_taskdata;
1260b57cec5SDimitry Andric   for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
1270b57cec5SDimitry Andric     kmp_depnode_t *successor = p->node;
1280b57cec5SDimitry Andric     kmp_int32 npredecessors = KMP_ATOMIC_DEC(&successor->dn.npredecessors) - 1;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric     // successor task can be NULL for wait_depends or because deps are still
1310b57cec5SDimitry Andric     // being processed
1320b57cec5SDimitry Andric     if (npredecessors == 0) {
1330b57cec5SDimitry Andric       KMP_MB();
1340b57cec5SDimitry Andric       if (successor->dn.task) {
1350b57cec5SDimitry Andric         KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled "
1360b57cec5SDimitry Andric                       "for execution.\n",
1370b57cec5SDimitry Andric                       gtid, successor->dn.task, task));
138e8d8bef9SDimitry Andric         // If a regular task depending on a hidden helper task, when the
139e8d8bef9SDimitry Andric         // hidden helper task is done, the regular task should be executed by
140e8d8bef9SDimitry Andric         // its encountering team.
141e8d8bef9SDimitry Andric         if (KMP_HIDDEN_HELPER_THREAD(gtid)) {
142e8d8bef9SDimitry Andric           // Hidden helper thread can only execute hidden helper tasks
143e8d8bef9SDimitry Andric           KMP_ASSERT(task->td_flags.hidden_helper);
144e8d8bef9SDimitry Andric           next_taskdata = KMP_TASK_TO_TASKDATA(successor->dn.task);
145e8d8bef9SDimitry Andric           // If the dependent task is a regular task, we need to push to its
146e8d8bef9SDimitry Andric           // encountering thread's queue; otherwise, it can be pushed to its own
147e8d8bef9SDimitry Andric           // queue.
148e8d8bef9SDimitry Andric           if (!next_taskdata->td_flags.hidden_helper) {
149*349cc55cSDimitry Andric             kmp_int32 encountering_gtid =
150*349cc55cSDimitry Andric                 next_taskdata->td_alloc_thread->th.th_info.ds.ds_gtid;
151*349cc55cSDimitry Andric             kmp_int32 encountering_tid = __kmp_tid_from_gtid(encountering_gtid);
152*349cc55cSDimitry Andric             __kmpc_give_task(successor->dn.task, encountering_tid);
153e8d8bef9SDimitry Andric           } else {
1540b57cec5SDimitry Andric             __kmp_omp_task(gtid, successor->dn.task, false);
1550b57cec5SDimitry Andric           }
156e8d8bef9SDimitry Andric         } else {
157e8d8bef9SDimitry Andric           __kmp_omp_task(gtid, successor->dn.task, false);
158e8d8bef9SDimitry Andric         }
159e8d8bef9SDimitry Andric       }
1600b57cec5SDimitry Andric     }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric     next = p->next;
1630b57cec5SDimitry Andric     __kmp_node_deref(thread, p->node);
1640b57cec5SDimitry Andric #if USE_FAST_MEMORY
1650b57cec5SDimitry Andric     __kmp_fast_free(thread, p);
1660b57cec5SDimitry Andric #else
1670b57cec5SDimitry Andric     __kmp_thread_free(thread, p);
1680b57cec5SDimitry Andric #endif
1690b57cec5SDimitry Andric   }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   __kmp_node_deref(thread, node);
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   KA_TRACE(
1740b57cec5SDimitry Andric       20,
1750b57cec5SDimitry Andric       ("__kmp_release_deps: T#%d all successors of %p notified of completion\n",
1760b57cec5SDimitry Andric        gtid, task));
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric #endif // KMP_TASKDEPS_H
180