xref: /freebsd/contrib/llvm-project/openmp/runtime/src/kmp_taskdeps.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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*fe6060f1SDimitry Andric   // TODO: temporarily disable assertion until the bug with dependences is fixed
27*fe6060f1SDimitry Andric   //  KMP_DEBUG_ASSERT(n >= 0);
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;
61*fe6060f1SDimitry Andric         __kmp_depnode_list_free(thread, entry->last_set);
62*fe6060f1SDimitry Andric         __kmp_depnode_list_free(thread, entry->prev_set);
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 
88*fe6060f1SDimitry Andric extern void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start);
89*fe6060f1SDimitry Andric 
900b57cec5SDimitry Andric static inline void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
910b57cec5SDimitry Andric   kmp_info_t *thread = __kmp_threads[gtid];
920b57cec5SDimitry Andric   kmp_depnode_t *node = task->td_depnode;
930b57cec5SDimitry Andric 
94e8d8bef9SDimitry Andric   // Check mutexinoutset dependencies, release locks
95e8d8bef9SDimitry Andric   if (UNLIKELY(node && (node->dn.mtx_num_locks < 0))) {
96e8d8bef9SDimitry Andric     // negative num_locks means all locks were acquired
97e8d8bef9SDimitry Andric     node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
98e8d8bef9SDimitry Andric     for (int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
99e8d8bef9SDimitry Andric       KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
100e8d8bef9SDimitry Andric       __kmp_release_lock(node->dn.mtx_locks[i], gtid);
101e8d8bef9SDimitry Andric     }
102e8d8bef9SDimitry Andric   }
103e8d8bef9SDimitry Andric 
1040b57cec5SDimitry Andric   if (task->td_dephash) {
1050b57cec5SDimitry Andric     KA_TRACE(
1060b57cec5SDimitry Andric         40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
1070b57cec5SDimitry Andric              gtid, task));
1080b57cec5SDimitry Andric     __kmp_dephash_free(thread, task->td_dephash);
1090b57cec5SDimitry Andric     task->td_dephash = NULL;
1100b57cec5SDimitry Andric   }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   if (!node)
1130b57cec5SDimitry Andric     return;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n",
1160b57cec5SDimitry Andric                 gtid, task));
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   KMP_ACQUIRE_DEPNODE(gtid, node);
1190b57cec5SDimitry Andric   node->dn.task =
1200b57cec5SDimitry Andric       NULL; // mark this task as finished, so no new dependencies are generated
1210b57cec5SDimitry Andric   KMP_RELEASE_DEPNODE(gtid, node);
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   kmp_depnode_list_t *next;
124e8d8bef9SDimitry Andric   kmp_taskdata_t *next_taskdata;
1250b57cec5SDimitry Andric   for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
1260b57cec5SDimitry Andric     kmp_depnode_t *successor = p->node;
1270b57cec5SDimitry Andric     kmp_int32 npredecessors = KMP_ATOMIC_DEC(&successor->dn.npredecessors) - 1;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric     // successor task can be NULL for wait_depends or because deps are still
1300b57cec5SDimitry Andric     // being processed
1310b57cec5SDimitry Andric     if (npredecessors == 0) {
1320b57cec5SDimitry Andric       KMP_MB();
1330b57cec5SDimitry Andric       if (successor->dn.task) {
1340b57cec5SDimitry Andric         KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled "
1350b57cec5SDimitry Andric                       "for execution.\n",
1360b57cec5SDimitry Andric                       gtid, successor->dn.task, task));
137e8d8bef9SDimitry Andric         // If a regular task depending on a hidden helper task, when the
138e8d8bef9SDimitry Andric         // hidden helper task is done, the regular task should be executed by
139e8d8bef9SDimitry Andric         // its encountering team.
140e8d8bef9SDimitry Andric         if (KMP_HIDDEN_HELPER_THREAD(gtid)) {
141e8d8bef9SDimitry Andric           // Hidden helper thread can only execute hidden helper tasks
142e8d8bef9SDimitry Andric           KMP_ASSERT(task->td_flags.hidden_helper);
143e8d8bef9SDimitry Andric           next_taskdata = KMP_TASK_TO_TASKDATA(successor->dn.task);
144e8d8bef9SDimitry Andric           // If the dependent task is a regular task, we need to push to its
145e8d8bef9SDimitry Andric           // encountering thread's queue; otherwise, it can be pushed to its own
146e8d8bef9SDimitry Andric           // queue.
147e8d8bef9SDimitry Andric           if (!next_taskdata->td_flags.hidden_helper) {
148*fe6060f1SDimitry Andric             __kmpc_give_task(
149*fe6060f1SDimitry Andric                 successor->dn.task,
150*fe6060f1SDimitry Andric                 __kmp_tid_from_gtid(next_taskdata->encountering_gtid));
151e8d8bef9SDimitry Andric           } else {
1520b57cec5SDimitry Andric             __kmp_omp_task(gtid, successor->dn.task, false);
1530b57cec5SDimitry Andric           }
154e8d8bef9SDimitry Andric         } else {
155e8d8bef9SDimitry Andric           __kmp_omp_task(gtid, successor->dn.task, false);
156e8d8bef9SDimitry Andric         }
157e8d8bef9SDimitry Andric       }
1580b57cec5SDimitry Andric     }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric     next = p->next;
1610b57cec5SDimitry Andric     __kmp_node_deref(thread, p->node);
1620b57cec5SDimitry Andric #if USE_FAST_MEMORY
1630b57cec5SDimitry Andric     __kmp_fast_free(thread, p);
1640b57cec5SDimitry Andric #else
1650b57cec5SDimitry Andric     __kmp_thread_free(thread, p);
1660b57cec5SDimitry Andric #endif
1670b57cec5SDimitry Andric   }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   __kmp_node_deref(thread, node);
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   KA_TRACE(
1720b57cec5SDimitry Andric       20,
1730b57cec5SDimitry Andric       ("__kmp_release_deps: T#%d all successors of %p notified of completion\n",
1740b57cec5SDimitry Andric        gtid, task));
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric #endif // KMP_TASKDEPS_H
178