LLVM OpenMP
kmp_taskdeps.h
Go to the documentation of this file.
1/*
2 * kmp_taskdeps.h
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#ifndef KMP_TASKDEPS_H
14#define KMP_TASKDEPS_H
15
16#include "kmp.h"
17
18#define KMP_ACQUIRE_DEPNODE(gtid, n) __kmp_acquire_lock(&(n)->dn.lock, (gtid))
19#define KMP_RELEASE_DEPNODE(gtid, n) __kmp_release_lock(&(n)->dn.lock, (gtid))
20
21static inline void __kmp_node_deref(kmp_info_t *thread, kmp_depnode_t *node) {
22 if (!node)
23 return;
24
25 kmp_int32 n = KMP_ATOMIC_SUB(&node->dn.nrefs, 2) - 2;
26 KMP_DEBUG_ASSERT(n >= 0);
27 if ((n & ~1) == 0) {
28#if USE_ITT_BUILD && USE_ITT_NOTIFY
29 __itt_sync_destroy(node);
30#endif
31 // These two assertions are somewhat redundant. The first is intended to
32 // detect if we are trying to free a depnode on the stack.
33 KMP_DEBUG_ASSERT((node->dn.nrefs & 1) == 0);
34 KMP_ASSERT(node->dn.nrefs == 0);
35#if USE_FAST_MEMORY
36 __kmp_fast_free(thread, node);
37#else
38 __kmp_thread_free(thread, node);
39#endif
40 }
41}
42
43static inline void __kmp_depnode_list_free(kmp_info_t *thread,
44 kmp_depnode_list *list) {
45 kmp_depnode_list *next;
46
47 for (; list; list = next) {
48 next = list->next;
49
50 __kmp_node_deref(thread, list->node);
51#if USE_FAST_MEMORY
52 __kmp_fast_free(thread, list);
53#else
54 __kmp_thread_free(thread, list);
55#endif
56 }
57}
58
59static inline void __kmp_dephash_free_entries(kmp_info_t *thread,
61 for (size_t i = 0; i < h->size; i++) {
62 if (h->buckets[i]) {
64 for (kmp_dephash_entry_t *entry = h->buckets[i]; entry; entry = next) {
65 next = entry->next_in_bucket;
66 __kmp_depnode_list_free(thread, entry->last_set);
67 __kmp_depnode_list_free(thread, entry->prev_set);
68 __kmp_node_deref(thread, entry->last_out);
69 if (entry->mtx_lock) {
70 __kmp_destroy_lock(entry->mtx_lock);
71 __kmp_free(entry->mtx_lock);
72 }
73#if USE_FAST_MEMORY
74 __kmp_fast_free(thread, entry);
75#else
76 __kmp_thread_free(thread, entry);
77#endif
78 }
79 h->buckets[i] = 0;
80 }
81 }
82 __kmp_node_deref(thread, h->last_all);
83 h->last_all = NULL;
84}
85
86static inline void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h) {
88#if USE_FAST_MEMORY
89 __kmp_fast_free(thread, h);
90#else
91 __kmp_thread_free(thread, h);
92#endif
93}
94
95extern void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start);
96
97static inline void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
98 kmp_info_t *thread = __kmp_threads[gtid];
99 kmp_depnode_t *node = task->td_depnode;
100
101 // Check mutexinoutset dependencies, release locks
102 if (UNLIKELY(node && (node->dn.mtx_num_locks < 0))) {
103 // negative num_locks means all locks were acquired
104 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
105 for (int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
106 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
107 __kmp_release_lock(node->dn.mtx_locks[i], gtid);
108 }
109 }
110
111 if (task->td_dephash) {
112 KA_TRACE(
113 40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
114 gtid, task));
115 __kmp_dephash_free(thread, task->td_dephash);
116 task->td_dephash = NULL;
117 }
118
119 if (!node)
120 return;
121
122 KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n",
123 gtid, task));
124
125 // Order task body memory accesses before dn.task = NULL is observed
126 // by lock-free checks in __kmp_depnode_link_successor().
127 KMP_MB();
128 KMP_ACQUIRE_DEPNODE(gtid, node);
129 node->dn.task =
130 NULL; // mark this task as finished, so no new dependencies are generated
131 KMP_RELEASE_DEPNODE(gtid, node);
132
133 kmp_depnode_list_t *next;
134 kmp_taskdata_t *next_taskdata;
135 for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
136 kmp_depnode_t *successor = p->node;
137#if USE_ITT_BUILD && USE_ITT_NOTIFY
138 __itt_sync_releasing(successor);
139#endif
140 kmp_int32 npredecessors = KMP_ATOMIC_DEC(&successor->dn.npredecessors) - 1;
141
142 // successor task can be NULL for wait_depends or because deps are still
143 // being processed
144 if (npredecessors == 0) {
145#if USE_ITT_BUILD && USE_ITT_NOTIFY
146 __itt_sync_acquired(successor);
147#endif
148 KMP_MB();
149 if (successor->dn.task) {
150 KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled "
151 "for execution.\n",
152 gtid, successor->dn.task, task));
153 // If a regular task depending on a hidden helper task, when the
154 // hidden helper task is done, the regular task should be executed by
155 // its encountering team.
156 if (KMP_HIDDEN_HELPER_THREAD(gtid)) {
157 // Hidden helper thread can only execute hidden helper tasks
158 KMP_ASSERT(task->td_flags.hidden_helper);
159 next_taskdata = KMP_TASK_TO_TASKDATA(successor->dn.task);
160 // If the dependent task is a regular task, we need to push to its
161 // encountering thread's queue; otherwise, it can be pushed to its own
162 // queue.
163 if (!next_taskdata->td_flags.hidden_helper) {
164 kmp_int32 encountering_gtid =
165 next_taskdata->td_alloc_thread->th.th_info.ds.ds_gtid;
166 kmp_int32 encountering_tid = __kmp_tid_from_gtid(encountering_gtid);
167 __kmpc_give_task(successor->dn.task, encountering_tid);
168 } else {
169 __kmp_omp_task(gtid, successor->dn.task, false);
170 }
171 } else {
172 __kmp_omp_task(gtid, successor->dn.task, false);
173 }
174 }
175 }
176
177 next = p->next;
178 __kmp_node_deref(thread, p->node);
179#if USE_FAST_MEMORY
180 __kmp_fast_free(thread, p);
181#else
182 __kmp_thread_free(thread, p);
183#endif
184 }
185
186 __kmp_node_deref(thread, node);
187
188 KA_TRACE(
189 20,
190 ("__kmp_release_deps: T#%d all successors of %p notified of completion\n",
191 gtid, task));
192}
193
194#endif // KMP_TASKDEPS_H
struct task * ptask
struct kmp_task kmp_task_t
void const char const char int ITT_FORMAT __itt_group_sync p
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function h
#define __kmp_free(ptr)
Definition kmp.h:3690
struct kmp_dephash kmp_dephash_t
union kmp_depnode kmp_depnode_t
Definition kmp.h:2512
static int __kmp_tid_from_gtid(int gtid)
Definition kmp.h:3553
#define KMP_TASK_TO_TASKDATA(task)
Definition kmp.h:2457
struct kmp_depnode_list kmp_depnode_list_t
Definition kmp.h:2513
kmp_info_t ** __kmp_threads
#define KMP_HIDDEN_HELPER_THREAD(gtid)
Definition kmp.h:4551
#define UNLIKELY(x)
Definition kmp.h:140
struct kmp_dephash_entry kmp_dephash_entry_t
Definition kmp.h:2514
kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task, bool serialize_immediate)
struct kmp_taskdata kmp_taskdata_t
Definition kmp.h:242
union KMP_ALIGN_CACHE kmp_info kmp_info_t
#define __kmp_thread_free(th, ptr)
Definition kmp.h:3716
#define KA_TRACE(d, x)
Definition kmp_debug.h:157
#define KMP_ASSERT(cond)
Definition kmp_debug.h:59
#define KMP_DEBUG_ASSERT(cond)
Definition kmp_debug.h:61
static void __kmp_destroy_lock(kmp_lock_t *lck)
Definition kmp_lock.h:575
static void __kmp_release_lock(kmp_lock_t *lck, kmp_int32 gtid)
Definition kmp_lock.h:567
#define KMP_ATOMIC_SUB(p, v)
Definition kmp_os.h:1270
#define KMP_MB()
Definition kmp_os.h:1070
#define KMP_ATOMIC_DEC(p)
Definition kmp_os.h:1274
#define i
Definition kmp_stub.cpp:88
static void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h)
#define KMP_ACQUIRE_DEPNODE(gtid, n)
static void __kmp_depnode_list_free(kmp_info_t *thread, kmp_depnode_list *list)
void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start)
static void __kmp_node_deref(kmp_info_t *thread, kmp_depnode_t *node)
#define KMP_RELEASE_DEPNODE(gtid, n)
static void __kmp_dephash_free_entries(kmp_info_t *thread, kmp_dephash_t *h)
static void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task)
int32_t kmp_int32
std::atomic< kmp_int32 > npredecessors
Definition kmp.h:2569
std::atomic< kmp_int32 > nrefs
Definition kmp.h:2570
kmp_lock_t * mtx_locks[MAX_MTX_DEPS]
Definition kmp.h:2563
kmp_task_t * task
Definition kmp.h:2562
kmp_int32 mtx_num_locks
Definition kmp.h:2564
kmp_depnode_list_t * successors
Definition kmp.h:2561
kmp_dephash_entry_t * next_in_bucket
Definition kmp.h:2586
kmp_depnode_list_t * next
Definition kmp.h:2554
kmp_info_p * td_alloc_thread
Definition kmp.h:2703
kmp_tasking_flags_t td_flags
Definition kmp.h:2701
unsigned hidden_helper
Definition kmp.h:2690
kmp_base_depnode_t dn
Definition kmp.h:2576
kmp_base_info_t th
Definition kmp.h:3022