blob: 0e4fdec2aff690508dc45e120a71a283d03469e3 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * kmp_taskdeps.cpp
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is dual licensed under the MIT and the University of Illinois Open
10// Source Licenses. See LICENSE.txt for details.
11//
12//===----------------------------------------------------------------------===//
13
Jim Cownie5e8470a2013-09-27 10:38:44 +000014//#define KMP_SUPPORT_GRAPH_OUTPUT 1
15
16#include "kmp.h"
17#include "kmp_io.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000018#include "kmp_wait_release.h"
Joachim Protze82e94a52017-11-01 10:08:30 +000019#if OMPT_SUPPORT
20#include "ompt-specific.h"
21#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000022
23#if OMP_40_ENABLED
24
Jonathan Peyton30419822017-05-12 18:01:32 +000025// TODO: Improve memory allocation? keep a list of pre-allocated structures?
26// allocate in blocks? re-use list finished list entries?
27// TODO: don't use atomic ref counters for stack-allocated nodes.
28// TODO: find an alternate to atomic refs for heap-allocated nodes?
29// TODO: Finish graph output support
30// TODO: kmp_lock_t seems a tad to big (and heavy weight) for this. Check other
31// runtime locks
32// TODO: Any ITT support needed?
Jim Cownie5e8470a2013-09-27 10:38:44 +000033
34#ifdef KMP_SUPPORT_GRAPH_OUTPUT
35static kmp_int32 kmp_node_id_seed = 0;
36#endif
37
Jonathan Peyton30419822017-05-12 18:01:32 +000038static void __kmp_init_node(kmp_depnode_t *node) {
39 node->dn.task = NULL; // set to null initially, it will point to the right
40 // task once dependences have been processed
41 node->dn.successors = NULL;
42 __kmp_init_lock(&node->dn.lock);
43 node->dn.nrefs = 1; // init creates the first reference to the node
Jim Cownie5e8470a2013-09-27 10:38:44 +000044#ifdef KMP_SUPPORT_GRAPH_OUTPUT
Jonathan Peyton30419822017-05-12 18:01:32 +000045 node->dn.id = KMP_TEST_THEN_INC32(&kmp_node_id_seed);
Jim Cownie5e8470a2013-09-27 10:38:44 +000046#endif
47}
48
Jonathan Peyton30419822017-05-12 18:01:32 +000049static inline kmp_depnode_t *__kmp_node_ref(kmp_depnode_t *node) {
Andrey Churbanovc47afcd2017-07-03 11:24:08 +000050 KMP_TEST_THEN_INC32(CCAST(kmp_int32 *, &node->dn.nrefs));
Jonathan Peyton30419822017-05-12 18:01:32 +000051 return node;
Jim Cownie5e8470a2013-09-27 10:38:44 +000052}
53
Jonathan Peyton30419822017-05-12 18:01:32 +000054static inline void __kmp_node_deref(kmp_info_t *thread, kmp_depnode_t *node) {
55 if (!node)
56 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +000057
Andrey Churbanovc47afcd2017-07-03 11:24:08 +000058 kmp_int32 n = KMP_TEST_THEN_DEC32(CCAST(kmp_int32 *, &node->dn.nrefs)) - 1;
Jonathan Peyton30419822017-05-12 18:01:32 +000059 if (n == 0) {
60 KMP_ASSERT(node->dn.nrefs == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +000061#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +000062 __kmp_fast_free(thread, node);
Jim Cownie5e8470a2013-09-27 10:38:44 +000063#else
Jonathan Peyton30419822017-05-12 18:01:32 +000064 __kmp_thread_free(thread, node);
Jim Cownie5e8470a2013-09-27 10:38:44 +000065#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000066 }
Jim Cownie5e8470a2013-09-27 10:38:44 +000067}
68
Jonathan Peyton30419822017-05-12 18:01:32 +000069#define KMP_ACQUIRE_DEPNODE(gtid, n) __kmp_acquire_lock(&(n)->dn.lock, (gtid))
70#define KMP_RELEASE_DEPNODE(gtid, n) __kmp_release_lock(&(n)->dn.lock, (gtid))
Jim Cownie5e8470a2013-09-27 10:38:44 +000071
Jonathan Peyton30419822017-05-12 18:01:32 +000072static void __kmp_depnode_list_free(kmp_info_t *thread, kmp_depnode_list *list);
Jim Cownie5e8470a2013-09-27 10:38:44 +000073
Jonathan Peyton30419822017-05-12 18:01:32 +000074enum { KMP_DEPHASH_OTHER_SIZE = 97, KMP_DEPHASH_MASTER_SIZE = 997 };
Jim Cownie5e8470a2013-09-27 10:38:44 +000075
Jonathan Peyton30419822017-05-12 18:01:32 +000076static inline kmp_int32 __kmp_dephash_hash(kmp_intptr_t addr, size_t hsize) {
77 // TODO alternate to try: set = (((Addr64)(addrUsefulBits * 9.618)) %
78 // m_num_sets );
79 return ((addr >> 6) ^ (addr >> 2)) % hsize;
Jim Cownie5e8470a2013-09-27 10:38:44 +000080}
81
Jonathan Peyton30419822017-05-12 18:01:32 +000082static kmp_dephash_t *__kmp_dephash_create(kmp_info_t *thread,
83 kmp_taskdata_t *current_task) {
84 kmp_dephash_t *h;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000085
Jonathan Peyton30419822017-05-12 18:01:32 +000086 size_t h_size;
Jonathan Peyton7d454512016-01-28 23:10:44 +000087
Jonathan Peyton30419822017-05-12 18:01:32 +000088 if (current_task->td_flags.tasktype == TASK_IMPLICIT)
89 h_size = KMP_DEPHASH_MASTER_SIZE;
90 else
91 h_size = KMP_DEPHASH_OTHER_SIZE;
Jonathan Peyton7d454512016-01-28 23:10:44 +000092
Jonathan Peyton30419822017-05-12 18:01:32 +000093 kmp_int32 size =
94 h_size * sizeof(kmp_dephash_entry_t *) + sizeof(kmp_dephash_t);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000095
Jim Cownie5e8470a2013-09-27 10:38:44 +000096#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +000097 h = (kmp_dephash_t *)__kmp_fast_allocate(thread, size);
Jim Cownie5e8470a2013-09-27 10:38:44 +000098#else
Jonathan Peyton30419822017-05-12 18:01:32 +000099 h = (kmp_dephash_t *)__kmp_thread_malloc(thread, size);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000100#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000101 h->size = h_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000102
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000103#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000104 h->nelements = 0;
105 h->nconflicts = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000106#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000107 h->buckets = (kmp_dephash_entry **)(h + 1);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000108
Jonathan Peyton30419822017-05-12 18:01:32 +0000109 for (size_t i = 0; i < h_size; i++)
110 h->buckets[i] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000111
Jonathan Peyton30419822017-05-12 18:01:32 +0000112 return h;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000113}
114
Jonathan Peyton30419822017-05-12 18:01:32 +0000115void __kmp_dephash_free_entries(kmp_info_t *thread, kmp_dephash_t *h) {
116 for (size_t i = 0; i < h->size; i++) {
117 if (h->buckets[i]) {
118 kmp_dephash_entry_t *next;
119 for (kmp_dephash_entry_t *entry = h->buckets[i]; entry; entry = next) {
120 next = entry->next_in_bucket;
121 __kmp_depnode_list_free(thread, entry->last_ins);
122 __kmp_node_deref(thread, entry->last_out);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000123#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000124 __kmp_fast_free(thread, entry);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000125#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000126 __kmp_thread_free(thread, entry);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000127#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000128 }
129 h->buckets[i] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000130 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000131 }
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000132}
133
Jonathan Peyton30419822017-05-12 18:01:32 +0000134void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h) {
135 __kmp_dephash_free_entries(thread, h);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000136#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000137 __kmp_fast_free(thread, h);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000138#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000139 __kmp_thread_free(thread, h);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000140#endif
141}
142
143static kmp_dephash_entry *
Jonathan Peyton30419822017-05-12 18:01:32 +0000144__kmp_dephash_find(kmp_info_t *thread, kmp_dephash_t *h, kmp_intptr_t addr) {
145 kmp_int32 bucket = __kmp_dephash_hash(addr, h->size);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000146
Jonathan Peyton30419822017-05-12 18:01:32 +0000147 kmp_dephash_entry_t *entry;
148 for (entry = h->buckets[bucket]; entry; entry = entry->next_in_bucket)
149 if (entry->addr == addr)
150 break;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000151
Jonathan Peyton30419822017-05-12 18:01:32 +0000152 if (entry == NULL) {
153// create entry. This is only done by one thread so no locking required
Jim Cownie5e8470a2013-09-27 10:38:44 +0000154#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000155 entry = (kmp_dephash_entry_t *)__kmp_fast_allocate(
156 thread, sizeof(kmp_dephash_entry_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000157#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000158 entry = (kmp_dephash_entry_t *)__kmp_thread_malloc(
159 thread, sizeof(kmp_dephash_entry_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000160#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000161 entry->addr = addr;
162 entry->last_out = NULL;
163 entry->last_ins = NULL;
164 entry->next_in_bucket = h->buckets[bucket];
165 h->buckets[bucket] = entry;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000166#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000167 h->nelements++;
168 if (entry->next_in_bucket)
169 h->nconflicts++;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000170#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000171 }
172 return entry;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000173}
174
Jonathan Peyton30419822017-05-12 18:01:32 +0000175static kmp_depnode_list_t *__kmp_add_node(kmp_info_t *thread,
176 kmp_depnode_list_t *list,
177 kmp_depnode_t *node) {
178 kmp_depnode_list_t *new_head;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000179
180#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000181 new_head = (kmp_depnode_list_t *)__kmp_fast_allocate(
182 thread, sizeof(kmp_depnode_list_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000183#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000184 new_head = (kmp_depnode_list_t *)__kmp_thread_malloc(
185 thread, sizeof(kmp_depnode_list_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000186#endif
187
Jonathan Peyton30419822017-05-12 18:01:32 +0000188 new_head->node = __kmp_node_ref(node);
189 new_head->next = list;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000190
Jonathan Peyton30419822017-05-12 18:01:32 +0000191 return new_head;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000192}
193
Jonathan Peyton30419822017-05-12 18:01:32 +0000194static void __kmp_depnode_list_free(kmp_info_t *thread,
195 kmp_depnode_list *list) {
196 kmp_depnode_list *next;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000197
Jonathan Peyton30419822017-05-12 18:01:32 +0000198 for (; list; list = next) {
199 next = list->next;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000200
Jonathan Peyton30419822017-05-12 18:01:32 +0000201 __kmp_node_deref(thread, list->node);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000202#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000203 __kmp_fast_free(thread, list);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000204#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000205 __kmp_thread_free(thread, list);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000206#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000207 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000208}
209
Jonathan Peyton30419822017-05-12 18:01:32 +0000210static inline void __kmp_track_dependence(kmp_depnode_t *source,
211 kmp_depnode_t *sink,
212 kmp_task_t *sink_task) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000213#ifdef KMP_SUPPORT_GRAPH_OUTPUT
Jonathan Peyton30419822017-05-12 18:01:32 +0000214 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
215 // do not use sink->dn.task as that is only filled after the dependencies
216 // are already processed!
217 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000218
Jonathan Peyton30419822017-05-12 18:01:32 +0000219 __kmp_printf("%d(%s) -> %d(%s)\n", source->dn.id,
220 task_source->td_ident->psource, sink->dn.id,
221 task_sink->td_ident->psource);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000222#endif
Joachim Protze82e94a52017-11-01 10:08:30 +0000223#if OMPT_SUPPORT && OMPT_OPTIONAL
224 /* OMPT tracks dependences between task (a=source, b=sink) in which
225 task a blocks the execution of b through the ompt_new_dependence_callback
226 */
227 if (ompt_enabled.ompt_callback_task_dependence) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000228 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
229 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000230
Joachim Protze82e94a52017-11-01 10:08:30 +0000231 ompt_callbacks.ompt_callback(ompt_callback_task_dependence)(
232 &(task_source->ompt_task_info.task_data),
233 &(task_sink->ompt_task_info.task_data));
Jonathan Peyton30419822017-05-12 18:01:32 +0000234 }
Joachim Protze82e94a52017-11-01 10:08:30 +0000235#endif /* OMPT_SUPPORT && OMPT_OPTIONAL */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000236}
237
Jonathan Peyton30419822017-05-12 18:01:32 +0000238template <bool filter>
Jim Cownie5e8470a2013-09-27 10:38:44 +0000239static inline kmp_int32
Jonathan Peyton30419822017-05-12 18:01:32 +0000240__kmp_process_deps(kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t *hash,
241 bool dep_barrier, kmp_int32 ndeps,
242 kmp_depend_info_t *dep_list, kmp_task_t *task) {
243 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d processing %d dependencies : "
244 "dep_barrier = %d\n",
245 filter, gtid, ndeps, dep_barrier));
Jonathan Peyton61118492016-05-20 19:03:38 +0000246
Jonathan Peyton30419822017-05-12 18:01:32 +0000247 kmp_info_t *thread = __kmp_threads[gtid];
248 kmp_int32 npredecessors = 0;
249 for (kmp_int32 i = 0; i < ndeps; i++) {
250 const kmp_depend_info_t *dep = &dep_list[i];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000251
Jonathan Peyton30419822017-05-12 18:01:32 +0000252 KMP_DEBUG_ASSERT(dep->flags.in);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000253
Jonathan Peyton30419822017-05-12 18:01:32 +0000254 if (filter && dep->base_addr == 0)
255 continue; // skip filtered entries
Jim Cownie5e8470a2013-09-27 10:38:44 +0000256
Jonathan Peyton30419822017-05-12 18:01:32 +0000257 kmp_dephash_entry_t *info =
258 __kmp_dephash_find(thread, hash, dep->base_addr);
259 kmp_depnode_t *last_out = info->last_out;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000260
Jonathan Peyton30419822017-05-12 18:01:32 +0000261 if (dep->flags.out && info->last_ins) {
262 for (kmp_depnode_list_t *p = info->last_ins; p; p = p->next) {
263 kmp_depnode_t *indep = p->node;
264 if (indep->dn.task) {
265 KMP_ACQUIRE_DEPNODE(gtid, indep);
266 if (indep->dn.task) {
267 __kmp_track_dependence(indep, node, task);
268 indep->dn.successors =
269 __kmp_add_node(thread, indep->dn.successors, node);
270 KA_TRACE(40, ("__kmp_process_deps<%d>: T#%d adding dependence from "
271 "%p to %p\n",
272 filter, gtid, KMP_TASK_TO_TASKDATA(indep->dn.task),
273 KMP_TASK_TO_TASKDATA(task)));
274 npredecessors++;
275 }
276 KMP_RELEASE_DEPNODE(gtid, indep);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000277 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000278 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000279
Jonathan Peyton30419822017-05-12 18:01:32 +0000280 __kmp_depnode_list_free(thread, info->last_ins);
281 info->last_ins = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000282
Jonathan Peyton30419822017-05-12 18:01:32 +0000283 } else if (last_out && last_out->dn.task) {
284 KMP_ACQUIRE_DEPNODE(gtid, last_out);
285 if (last_out->dn.task) {
286 __kmp_track_dependence(last_out, node, task);
287 last_out->dn.successors =
288 __kmp_add_node(thread, last_out->dn.successors, node);
289 KA_TRACE(
290 40,
291 ("__kmp_process_deps<%d>: T#%d adding dependence from %p to %p\n",
292 filter, gtid, KMP_TASK_TO_TASKDATA(last_out->dn.task),
293 KMP_TASK_TO_TASKDATA(task)));
294
295 npredecessors++;
296 }
297 KMP_RELEASE_DEPNODE(gtid, last_out);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000298 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000299
Jonathan Peyton30419822017-05-12 18:01:32 +0000300 if (dep_barrier) {
301 // if this is a sync point in the serial sequence, then the previous
302 // outputs are guaranteed to be completed after
303 // the execution of this task so the previous output nodes can be cleared.
304 __kmp_node_deref(thread, last_out);
305 info->last_out = NULL;
306 } else {
307 if (dep->flags.out) {
308 __kmp_node_deref(thread, last_out);
309 info->last_out = __kmp_node_ref(node);
310 } else
311 info->last_ins = __kmp_add_node(thread, info->last_ins, node);
312 }
313 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000314
Jonathan Peyton30419822017-05-12 18:01:32 +0000315 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d found %d predecessors\n", filter,
316 gtid, npredecessors));
317
318 return npredecessors;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000319}
320
321#define NO_DEP_BARRIER (false)
322#define DEP_BARRIER (true)
323
324// returns true if the task has any outstanding dependence
Jonathan Peyton30419822017-05-12 18:01:32 +0000325static bool __kmp_check_deps(kmp_int32 gtid, kmp_depnode_t *node,
326 kmp_task_t *task, kmp_dephash_t *hash,
327 bool dep_barrier, kmp_int32 ndeps,
328 kmp_depend_info_t *dep_list,
329 kmp_int32 ndeps_noalias,
330 kmp_depend_info_t *noalias_dep_list) {
331 int i;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000332
Jonathan Peytond2eb3c72015-08-26 20:02:21 +0000333#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000334 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
Jonathan Peytond2eb3c72015-08-26 20:02:21 +0000335#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000336 KA_TRACE(20, ("__kmp_check_deps: T#%d checking dependencies for task %p : %d "
337 "possibly aliased dependencies, %d non-aliased depedencies : "
338 "dep_barrier=%d .\n",
339 gtid, taskdata, ndeps, ndeps_noalias, dep_barrier));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000340
Jonathan Peyton30419822017-05-12 18:01:32 +0000341 // Filter deps in dep_list
342 // TODO: Different algorithm for large dep_list ( > 10 ? )
343 for (i = 0; i < ndeps; i++) {
344 if (dep_list[i].base_addr != 0)
345 for (int j = i + 1; j < ndeps; j++)
346 if (dep_list[i].base_addr == dep_list[j].base_addr) {
347 dep_list[i].flags.in |= dep_list[j].flags.in;
348 dep_list[i].flags.out |= dep_list[j].flags.out;
349 dep_list[j].base_addr = 0; // Mark j element as void
350 }
351 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000352
Jonathan Peyton30419822017-05-12 18:01:32 +0000353 // doesn't need to be atomic as no other thread is going to be accessing this
354 // node just yet.
355 // npredecessors is set -1 to ensure that none of the releasing tasks queues
356 // this task before we have finished processing all the dependencies
357 node->dn.npredecessors = -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000358
Jonathan Peyton30419822017-05-12 18:01:32 +0000359 // used to pack all npredecessors additions into a single atomic operation at
360 // the end
361 int npredecessors;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000362
Jonathan Peyton30419822017-05-12 18:01:32 +0000363 npredecessors = __kmp_process_deps<true>(gtid, node, hash, dep_barrier, ndeps,
364 dep_list, task);
365 npredecessors += __kmp_process_deps<false>(
366 gtid, node, hash, dep_barrier, ndeps_noalias, noalias_dep_list, task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000367
Jonathan Peyton30419822017-05-12 18:01:32 +0000368 node->dn.task = task;
369 KMP_MB();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000370
Jonathan Peyton30419822017-05-12 18:01:32 +0000371 // Account for our initial fake value
372 npredecessors++;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000373
Jonathan Peyton30419822017-05-12 18:01:32 +0000374 // Update predecessors and obtain current value to check if there are still
375 // any outstandig dependences (some tasks may have finished while we processed
376 // the dependences)
Andrey Churbanovc47afcd2017-07-03 11:24:08 +0000377 npredecessors =
378 KMP_TEST_THEN_ADD32(CCAST(kmp_int32 *, &node->dn.npredecessors),
379 npredecessors) +
380 npredecessors;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000381
Jonathan Peyton30419822017-05-12 18:01:32 +0000382 KA_TRACE(20, ("__kmp_check_deps: T#%d found %d predecessors for task %p \n",
383 gtid, npredecessors, taskdata));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000384
Jonathan Peyton30419822017-05-12 18:01:32 +0000385 // beyond this point the task could be queued (and executed) by a releasing
386 // task...
387 return npredecessors > 0 ? true : false;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000388}
389
Jonathan Peyton30419822017-05-12 18:01:32 +0000390void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
391 kmp_info_t *thread = __kmp_threads[gtid];
392 kmp_depnode_t *node = task->td_depnode;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000393
Jonathan Peyton30419822017-05-12 18:01:32 +0000394 if (task->td_dephash) {
395 KA_TRACE(
396 40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
397 gtid, task));
398 __kmp_dephash_free(thread, task->td_dephash);
399 task->td_dephash = NULL;
400 }
401
402 if (!node)
403 return;
404
405 KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n",
406 gtid, task));
407
408 KMP_ACQUIRE_DEPNODE(gtid, node);
409 node->dn.task =
410 NULL; // mark this task as finished, so no new dependencies are generated
411 KMP_RELEASE_DEPNODE(gtid, node);
412
413 kmp_depnode_list_t *next;
414 for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
415 kmp_depnode_t *successor = p->node;
416 kmp_int32 npredecessors =
Andrey Churbanovc47afcd2017-07-03 11:24:08 +0000417 KMP_TEST_THEN_DEC32(CCAST(kmp_int32 *, &successor->dn.npredecessors)) -
418 1;
Jonathan Peyton30419822017-05-12 18:01:32 +0000419 // successor task can be NULL for wait_depends or because deps are still
420 // being processed
421 if (npredecessors == 0) {
422 KMP_MB();
423 if (successor->dn.task) {
424 KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled "
425 "for execution.\n",
426 gtid, successor->dn.task, task));
427 __kmp_omp_task(gtid, successor->dn.task, false);
428 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000429 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000430
Jonathan Peyton30419822017-05-12 18:01:32 +0000431 next = p->next;
432 __kmp_node_deref(thread, p->node);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000433#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000434 __kmp_fast_free(thread, p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000435#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000436 __kmp_thread_free(thread, p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000437#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000438 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000439
Jonathan Peyton30419822017-05-12 18:01:32 +0000440 __kmp_node_deref(thread, node);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000441
Jonathan Peyton30419822017-05-12 18:01:32 +0000442 KA_TRACE(
443 20,
444 ("__kmp_release_deps: T#%d all successors of %p notified of completion\n",
445 gtid, task));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000446}
447
448/*!
449@ingroup TASKING
450@param loc_ref location of the original task directive
451@param gtid Global Thread ID of encountering thread
Jonathan Peyton30419822017-05-12 18:01:32 +0000452@param new_task task thunk allocated by __kmp_omp_task_alloc() for the ''new
453task''
Jim Cownie5e8470a2013-09-27 10:38:44 +0000454@param ndeps Number of depend items with possible aliasing
455@param dep_list List of depend items with possible aliasing
456@param ndeps_noalias Number of depend items with no aliasing
457@param noalias_dep_list List of depend items with no aliasing
458
Jonathan Peyton30419822017-05-12 18:01:32 +0000459@return Returns either TASK_CURRENT_NOT_QUEUED if the current task was not
460suspendend and queued, or TASK_CURRENT_QUEUED if it was suspended and queued
Jim Cownie5e8470a2013-09-27 10:38:44 +0000461
462Schedule a non-thread-switchable task with dependences for execution
463*/
Jonathan Peyton30419822017-05-12 18:01:32 +0000464kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 gtid,
465 kmp_task_t *new_task, kmp_int32 ndeps,
466 kmp_depend_info_t *dep_list,
467 kmp_int32 ndeps_noalias,
468 kmp_depend_info_t *noalias_dep_list) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000469
Jonathan Peyton30419822017-05-12 18:01:32 +0000470 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
471 KA_TRACE(10, ("__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n", gtid,
472 loc_ref, new_taskdata));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000473
Jonathan Peyton30419822017-05-12 18:01:32 +0000474 kmp_info_t *thread = __kmp_threads[gtid];
475 kmp_taskdata_t *current_task = thread->th.th_current_task;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000476
Joachim Protze82e94a52017-11-01 10:08:30 +0000477#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +0000478 if (ompt_enabled.enabled) {
Joachim Protze265fb582017-12-24 07:30:23 +0000479 OMPT_STORE_RETURN_ADDRESS(gtid);
480 if (!current_task->ompt_task_info.frame.enter_frame)
481 current_task->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
Joachim Protze82e94a52017-11-01 10:08:30 +0000482 if (ompt_enabled.ompt_callback_task_create) {
Joachim Protze82e94a52017-11-01 10:08:30 +0000483 ompt_data_t task_data = ompt_data_none;
484 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
Joachim Protze265fb582017-12-24 07:30:23 +0000485 current_task ? &(current_task->ompt_task_info.task_data) : &task_data,
486 current_task ? &(current_task->ompt_task_info.frame) : NULL,
Joachim Protze82e94a52017-11-01 10:08:30 +0000487 &(new_taskdata->ompt_task_info.task_data),
488 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 1,
489 OMPT_LOAD_RETURN_ADDRESS(gtid));
490 }
491
Joachim Protzec255ca72017-11-05 14:11:10 +0000492 new_taskdata->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(0);
Joachim Protze82e94a52017-11-01 10:08:30 +0000493 }
494
495#if OMPT_OPTIONAL
Jonathan Peyton30419822017-05-12 18:01:32 +0000496 /* OMPT grab all dependences if requested by the tool */
Joachim Protze82e94a52017-11-01 10:08:30 +0000497 if (ndeps + ndeps_noalias > 0 &&
498 ompt_enabled.ompt_callback_task_dependences) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000499 kmp_int32 i;
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000500
Jonathan Peyton30419822017-05-12 18:01:32 +0000501 new_taskdata->ompt_task_info.ndeps = ndeps + ndeps_noalias;
502 new_taskdata->ompt_task_info.deps =
503 (ompt_task_dependence_t *)KMP_OMPT_DEPS_ALLOC(
504 thread, (ndeps + ndeps_noalias) * sizeof(ompt_task_dependence_t));
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000505
Jonathan Peyton30419822017-05-12 18:01:32 +0000506 KMP_ASSERT(new_taskdata->ompt_task_info.deps != NULL);
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000507
Jonathan Peyton30419822017-05-12 18:01:32 +0000508 for (i = 0; i < ndeps; i++) {
509 new_taskdata->ompt_task_info.deps[i].variable_addr =
510 (void *)dep_list[i].base_addr;
511 if (dep_list[i].flags.in && dep_list[i].flags.out)
512 new_taskdata->ompt_task_info.deps[i].dependence_flags =
513 ompt_task_dependence_type_inout;
514 else if (dep_list[i].flags.out)
515 new_taskdata->ompt_task_info.deps[i].dependence_flags =
516 ompt_task_dependence_type_out;
517 else if (dep_list[i].flags.in)
518 new_taskdata->ompt_task_info.deps[i].dependence_flags =
519 ompt_task_dependence_type_in;
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000520 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000521 for (i = 0; i < ndeps_noalias; i++) {
522 new_taskdata->ompt_task_info.deps[ndeps + i].variable_addr =
523 (void *)noalias_dep_list[i].base_addr;
524 if (noalias_dep_list[i].flags.in && noalias_dep_list[i].flags.out)
525 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
526 ompt_task_dependence_type_inout;
527 else if (noalias_dep_list[i].flags.out)
528 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
529 ompt_task_dependence_type_out;
530 else if (noalias_dep_list[i].flags.in)
531 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
532 ompt_task_dependence_type_in;
533 }
Joachim Protze82e94a52017-11-01 10:08:30 +0000534 ompt_callbacks.ompt_callback(ompt_callback_task_dependences)(
535 &(new_taskdata->ompt_task_info.task_data),
536 new_taskdata->ompt_task_info.deps, new_taskdata->ompt_task_info.ndeps);
537 /* We can now free the allocated memory for the dependencies */
538 /* For OMPD we might want to delay the free until task_end */
539 KMP_OMPT_DEPS_FREE(thread, new_taskdata->ompt_task_info.deps);
540 new_taskdata->ompt_task_info.deps = NULL;
541 new_taskdata->ompt_task_info.ndeps = 0;
Jonathan Peyton30419822017-05-12 18:01:32 +0000542 }
Joachim Protze82e94a52017-11-01 10:08:30 +0000543#endif /* OMPT_OPTIONAL */
544#endif /* OMPT_SUPPORT */
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000545
Jonathan Peyton30419822017-05-12 18:01:32 +0000546 bool serial = current_task->td_flags.team_serial ||
547 current_task->td_flags.tasking_ser ||
548 current_task->td_flags.final;
Jonathan Peytondf6818b2016-06-14 17:57:47 +0000549#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000550 kmp_task_team_t *task_team = thread->th.th_task_team;
551 serial = serial && !(task_team && task_team->tt.tt_found_proxy_tasks);
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000552#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000553
Jonathan Peyton30419822017-05-12 18:01:32 +0000554 if (!serial && (ndeps > 0 || ndeps_noalias > 0)) {
555 /* if no dependencies have been tracked yet, create the dependence hash */
556 if (current_task->td_dephash == NULL)
557 current_task->td_dephash = __kmp_dephash_create(thread, current_task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000558
559#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000560 kmp_depnode_t *node =
561 (kmp_depnode_t *)__kmp_fast_allocate(thread, sizeof(kmp_depnode_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000562#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000563 kmp_depnode_t *node =
564 (kmp_depnode_t *)__kmp_thread_malloc(thread, sizeof(kmp_depnode_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000565#endif
566
Jonathan Peyton30419822017-05-12 18:01:32 +0000567 __kmp_init_node(node);
568 new_taskdata->td_depnode = node;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000569
Jonathan Peyton30419822017-05-12 18:01:32 +0000570 if (__kmp_check_deps(gtid, node, new_task, current_task->td_dephash,
571 NO_DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
572 noalias_dep_list)) {
573 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had blocking "
574 "dependencies: "
575 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
576 gtid, loc_ref, new_taskdata));
Joachim Protze265fb582017-12-24 07:30:23 +0000577#if OMPT_SUPPORT
578 if (ompt_enabled.enabled) {
579 current_task->ompt_task_info.frame.enter_frame = NULL;
580 }
581#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000582 return TASK_CURRENT_NOT_QUEUED;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000583 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000584 } else {
585 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d ignored dependencies "
586 "for task (serialized)"
587 "loc=%p task=%p\n",
588 gtid, loc_ref, new_taskdata));
589 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000590
Jonathan Peyton30419822017-05-12 18:01:32 +0000591 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had no blocking "
592 "dependencies : "
593 "loc=%p task=%p, transferring to __kmpc_omp_task\n",
594 gtid, loc_ref, new_taskdata));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000595
Joachim Protze265fb582017-12-24 07:30:23 +0000596 kmp_int32 ret = __kmp_omp_task(gtid, new_task, true);
597#if OMPT_SUPPORT
598 if (ompt_enabled.enabled) {
599 current_task->ompt_task_info.frame.enter_frame = NULL;
600 }
601#endif
602 return ret;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000603}
604
605/*!
606@ingroup TASKING
607@param loc_ref location of the original task directive
608@param gtid Global Thread ID of encountering thread
609@param ndeps Number of depend items with possible aliasing
610@param dep_list List of depend items with possible aliasing
611@param ndeps_noalias Number of depend items with no aliasing
612@param noalias_dep_list List of depend items with no aliasing
613
614Blocks the current task until all specifies dependencies have been fulfilled.
615*/
Jonathan Peyton30419822017-05-12 18:01:32 +0000616void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps,
617 kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
618 kmp_depend_info_t *noalias_dep_list) {
619 KA_TRACE(10, ("__kmpc_omp_wait_deps(enter): T#%d loc=%p\n", gtid, loc_ref));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000620
Jonathan Peyton30419822017-05-12 18:01:32 +0000621 if (ndeps == 0 && ndeps_noalias == 0) {
622 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no dependencies to "
623 "wait upon : loc=%p\n",
624 gtid, loc_ref));
625 return;
626 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000627
Jonathan Peyton30419822017-05-12 18:01:32 +0000628 kmp_info_t *thread = __kmp_threads[gtid];
629 kmp_taskdata_t *current_task = thread->th.th_current_task;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000630
Jonathan Peyton30419822017-05-12 18:01:32 +0000631 // We can return immediately as:
632 // - dependences are not computed in serial teams (except with proxy tasks)
633 // - if the dephash is not yet created it means we have nothing to wait for
634 bool ignore = current_task->td_flags.team_serial ||
635 current_task->td_flags.tasking_ser ||
636 current_task->td_flags.final;
Jonathan Peytondf6818b2016-06-14 17:57:47 +0000637#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000638 ignore = ignore && thread->th.th_task_team != NULL &&
639 thread->th.th_task_team->tt.tt_found_proxy_tasks == FALSE;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000640#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000641 ignore = ignore || current_task->td_dephash == NULL;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000642
Jonathan Peyton30419822017-05-12 18:01:32 +0000643 if (ignore) {
644 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking "
645 "dependencies : loc=%p\n",
646 gtid, loc_ref));
647 return;
648 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000649
Jonathan Peyton30419822017-05-12 18:01:32 +0000650 kmp_depnode_t node;
651 __kmp_init_node(&node);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000652
Jonathan Peyton30419822017-05-12 18:01:32 +0000653 if (!__kmp_check_deps(gtid, &node, NULL, current_task->td_dephash,
654 DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
655 noalias_dep_list)) {
656 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking "
657 "dependencies : loc=%p\n",
658 gtid, loc_ref));
659 return;
660 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000661
Jonathan Peyton30419822017-05-12 18:01:32 +0000662 int thread_finished = FALSE;
663 kmp_flag_32 flag((volatile kmp_uint32 *)&(node.dn.npredecessors), 0U);
664 while (node.dn.npredecessors > 0) {
665 flag.execute_tasks(thread, gtid, FALSE, &thread_finished,
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000666#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000667 NULL,
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000668#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000669 __kmp_task_stealing_constraint);
670 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000671
Jonathan Peyton30419822017-05-12 18:01:32 +0000672 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d finished waiting : loc=%p\n",
673 gtid, loc_ref));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000674}
675
676#endif /* OMP_40_ENABLED */