blob: 5499a568c021dff14a73ab2f385747efbce7332d [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * kmp_taskdeps.cpp
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
5
6//===----------------------------------------------------------------------===//
7//
8// The LLVM Compiler Infrastructure
9//
10// This file is dual licensed under the MIT and the University of Illinois Open
11// Source Licenses. See LICENSE.txt for details.
12//
13//===----------------------------------------------------------------------===//
14
15
16//#define KMP_SUPPORT_GRAPH_OUTPUT 1
17
18#include "kmp.h"
19#include "kmp_io.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000020#include "kmp_wait_release.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000021
22#if OMP_40_ENABLED
23
Jonathan Peyton30419822017-05-12 18:01:32 +000024// TODO: Improve memory allocation? keep a list of pre-allocated structures?
25// allocate in blocks? re-use list finished list entries?
26// TODO: don't use atomic ref counters for stack-allocated nodes.
27// TODO: find an alternate to atomic refs for heap-allocated nodes?
28// TODO: Finish graph output support
29// TODO: kmp_lock_t seems a tad to big (and heavy weight) for this. Check other
30// runtime locks
31// TODO: Any ITT support needed?
Jim Cownie5e8470a2013-09-27 10:38:44 +000032
33#ifdef KMP_SUPPORT_GRAPH_OUTPUT
34static kmp_int32 kmp_node_id_seed = 0;
35#endif
36
Jonathan Peyton30419822017-05-12 18:01:32 +000037static void __kmp_init_node(kmp_depnode_t *node) {
38 node->dn.task = NULL; // set to null initially, it will point to the right
39 // task once dependences have been processed
40 node->dn.successors = NULL;
41 __kmp_init_lock(&node->dn.lock);
42 node->dn.nrefs = 1; // init creates the first reference to the node
Jim Cownie5e8470a2013-09-27 10:38:44 +000043#ifdef KMP_SUPPORT_GRAPH_OUTPUT
Jonathan Peyton30419822017-05-12 18:01:32 +000044 node->dn.id = KMP_TEST_THEN_INC32(&kmp_node_id_seed);
Jim Cownie5e8470a2013-09-27 10:38:44 +000045#endif
46}
47
Jonathan Peyton30419822017-05-12 18:01:32 +000048static inline kmp_depnode_t *__kmp_node_ref(kmp_depnode_t *node) {
Andrey Churbanovc47afcd2017-07-03 11:24:08 +000049 KMP_TEST_THEN_INC32(CCAST(kmp_int32 *, &node->dn.nrefs));
Jonathan Peyton30419822017-05-12 18:01:32 +000050 return node;
Jim Cownie5e8470a2013-09-27 10:38:44 +000051}
52
Jonathan Peyton30419822017-05-12 18:01:32 +000053static inline void __kmp_node_deref(kmp_info_t *thread, kmp_depnode_t *node) {
54 if (!node)
55 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +000056
Andrey Churbanovc47afcd2017-07-03 11:24:08 +000057 kmp_int32 n = KMP_TEST_THEN_DEC32(CCAST(kmp_int32 *, &node->dn.nrefs)) - 1;
Jonathan Peyton30419822017-05-12 18:01:32 +000058 if (n == 0) {
59 KMP_ASSERT(node->dn.nrefs == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +000060#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +000061 __kmp_fast_free(thread, node);
Jim Cownie5e8470a2013-09-27 10:38:44 +000062#else
Jonathan Peyton30419822017-05-12 18:01:32 +000063 __kmp_thread_free(thread, node);
Jim Cownie5e8470a2013-09-27 10:38:44 +000064#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000065 }
Jim Cownie5e8470a2013-09-27 10:38:44 +000066}
67
Jonathan Peyton30419822017-05-12 18:01:32 +000068#define KMP_ACQUIRE_DEPNODE(gtid, n) __kmp_acquire_lock(&(n)->dn.lock, (gtid))
69#define KMP_RELEASE_DEPNODE(gtid, n) __kmp_release_lock(&(n)->dn.lock, (gtid))
Jim Cownie5e8470a2013-09-27 10:38:44 +000070
Jonathan Peyton30419822017-05-12 18:01:32 +000071static void __kmp_depnode_list_free(kmp_info_t *thread, kmp_depnode_list *list);
Jim Cownie5e8470a2013-09-27 10:38:44 +000072
Jonathan Peyton30419822017-05-12 18:01:32 +000073enum { KMP_DEPHASH_OTHER_SIZE = 97, KMP_DEPHASH_MASTER_SIZE = 997 };
Jim Cownie5e8470a2013-09-27 10:38:44 +000074
Jonathan Peyton30419822017-05-12 18:01:32 +000075static inline kmp_int32 __kmp_dephash_hash(kmp_intptr_t addr, size_t hsize) {
76 // TODO alternate to try: set = (((Addr64)(addrUsefulBits * 9.618)) %
77 // m_num_sets );
78 return ((addr >> 6) ^ (addr >> 2)) % hsize;
Jim Cownie5e8470a2013-09-27 10:38:44 +000079}
80
Jonathan Peyton30419822017-05-12 18:01:32 +000081static kmp_dephash_t *__kmp_dephash_create(kmp_info_t *thread,
82 kmp_taskdata_t *current_task) {
83 kmp_dephash_t *h;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000084
Jonathan Peyton30419822017-05-12 18:01:32 +000085 size_t h_size;
Jonathan Peyton7d454512016-01-28 23:10:44 +000086
Jonathan Peyton30419822017-05-12 18:01:32 +000087 if (current_task->td_flags.tasktype == TASK_IMPLICIT)
88 h_size = KMP_DEPHASH_MASTER_SIZE;
89 else
90 h_size = KMP_DEPHASH_OTHER_SIZE;
Jonathan Peyton7d454512016-01-28 23:10:44 +000091
Jonathan Peyton30419822017-05-12 18:01:32 +000092 kmp_int32 size =
93 h_size * sizeof(kmp_dephash_entry_t *) + sizeof(kmp_dephash_t);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000094
Jim Cownie5e8470a2013-09-27 10:38:44 +000095#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +000096 h = (kmp_dephash_t *)__kmp_fast_allocate(thread, size);
Jim Cownie5e8470a2013-09-27 10:38:44 +000097#else
Jonathan Peyton30419822017-05-12 18:01:32 +000098 h = (kmp_dephash_t *)__kmp_thread_malloc(thread, size);
Jim Cownie5e8470a2013-09-27 10:38:44 +000099#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000100 h->size = h_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000101
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000102#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000103 h->nelements = 0;
104 h->nconflicts = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000105#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000106 h->buckets = (kmp_dephash_entry **)(h + 1);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000107
Jonathan Peyton30419822017-05-12 18:01:32 +0000108 for (size_t i = 0; i < h_size; i++)
109 h->buckets[i] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000110
Jonathan Peyton30419822017-05-12 18:01:32 +0000111 return h;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000112}
113
Jonathan Peyton30419822017-05-12 18:01:32 +0000114void __kmp_dephash_free_entries(kmp_info_t *thread, kmp_dephash_t *h) {
115 for (size_t i = 0; i < h->size; i++) {
116 if (h->buckets[i]) {
117 kmp_dephash_entry_t *next;
118 for (kmp_dephash_entry_t *entry = h->buckets[i]; entry; entry = next) {
119 next = entry->next_in_bucket;
120 __kmp_depnode_list_free(thread, entry->last_ins);
121 __kmp_node_deref(thread, entry->last_out);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000122#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000123 __kmp_fast_free(thread, entry);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000124#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000125 __kmp_thread_free(thread, entry);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000126#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000127 }
128 h->buckets[i] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000129 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000130 }
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000131}
132
Jonathan Peyton30419822017-05-12 18:01:32 +0000133void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h) {
134 __kmp_dephash_free_entries(thread, h);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000135#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000136 __kmp_fast_free(thread, h);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000137#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000138 __kmp_thread_free(thread, h);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000139#endif
140}
141
142static kmp_dephash_entry *
Jonathan Peyton30419822017-05-12 18:01:32 +0000143__kmp_dephash_find(kmp_info_t *thread, kmp_dephash_t *h, kmp_intptr_t addr) {
144 kmp_int32 bucket = __kmp_dephash_hash(addr, h->size);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000145
Jonathan Peyton30419822017-05-12 18:01:32 +0000146 kmp_dephash_entry_t *entry;
147 for (entry = h->buckets[bucket]; entry; entry = entry->next_in_bucket)
148 if (entry->addr == addr)
149 break;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000150
Jonathan Peyton30419822017-05-12 18:01:32 +0000151 if (entry == NULL) {
152// create entry. This is only done by one thread so no locking required
Jim Cownie5e8470a2013-09-27 10:38:44 +0000153#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000154 entry = (kmp_dephash_entry_t *)__kmp_fast_allocate(
155 thread, sizeof(kmp_dephash_entry_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000156#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000157 entry = (kmp_dephash_entry_t *)__kmp_thread_malloc(
158 thread, sizeof(kmp_dephash_entry_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000159#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000160 entry->addr = addr;
161 entry->last_out = NULL;
162 entry->last_ins = NULL;
163 entry->next_in_bucket = h->buckets[bucket];
164 h->buckets[bucket] = entry;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000165#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000166 h->nelements++;
167 if (entry->next_in_bucket)
168 h->nconflicts++;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000169#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000170 }
171 return entry;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000172}
173
Jonathan Peyton30419822017-05-12 18:01:32 +0000174static kmp_depnode_list_t *__kmp_add_node(kmp_info_t *thread,
175 kmp_depnode_list_t *list,
176 kmp_depnode_t *node) {
177 kmp_depnode_list_t *new_head;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000178
179#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000180 new_head = (kmp_depnode_list_t *)__kmp_fast_allocate(
181 thread, sizeof(kmp_depnode_list_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000182#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000183 new_head = (kmp_depnode_list_t *)__kmp_thread_malloc(
184 thread, sizeof(kmp_depnode_list_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000185#endif
186
Jonathan Peyton30419822017-05-12 18:01:32 +0000187 new_head->node = __kmp_node_ref(node);
188 new_head->next = list;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000189
Jonathan Peyton30419822017-05-12 18:01:32 +0000190 return new_head;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000191}
192
Jonathan Peyton30419822017-05-12 18:01:32 +0000193static void __kmp_depnode_list_free(kmp_info_t *thread,
194 kmp_depnode_list *list) {
195 kmp_depnode_list *next;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000196
Jonathan Peyton30419822017-05-12 18:01:32 +0000197 for (; list; list = next) {
198 next = list->next;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000199
Jonathan Peyton30419822017-05-12 18:01:32 +0000200 __kmp_node_deref(thread, list->node);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000201#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000202 __kmp_fast_free(thread, list);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000203#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000204 __kmp_thread_free(thread, list);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000205#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000206 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000207}
208
Jonathan Peyton30419822017-05-12 18:01:32 +0000209static inline void __kmp_track_dependence(kmp_depnode_t *source,
210 kmp_depnode_t *sink,
211 kmp_task_t *sink_task) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000212#ifdef KMP_SUPPORT_GRAPH_OUTPUT
Jonathan Peyton30419822017-05-12 18:01:32 +0000213 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
214 // do not use sink->dn.task as that is only filled after the dependencies
215 // are already processed!
216 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000217
Jonathan Peyton30419822017-05-12 18:01:32 +0000218 __kmp_printf("%d(%s) -> %d(%s)\n", source->dn.id,
219 task_source->td_ident->psource, sink->dn.id,
220 task_sink->td_ident->psource);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000221#endif
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000222#if OMPT_SUPPORT && OMPT_TRACE
Jonathan Peyton30419822017-05-12 18:01:32 +0000223 // OMPT tracks dependences between task (a=source, b=sink) in which
224 // task a blocks the execution of b through the ompt_new_dependence_callback
225 if (ompt_enabled &&
226 ompt_callbacks.ompt_callback(ompt_event_task_dependence_pair)) {
227 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
228 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000229
Jonathan Peyton30419822017-05-12 18:01:32 +0000230 ompt_callbacks.ompt_callback(ompt_event_task_dependence_pair)(
231 task_source->ompt_task_info.task_id, task_sink->ompt_task_info.task_id);
232 }
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000233#endif /* OMPT_SUPPORT && OMPT_TRACE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000234}
235
Jonathan Peyton30419822017-05-12 18:01:32 +0000236template <bool filter>
Jim Cownie5e8470a2013-09-27 10:38:44 +0000237static inline kmp_int32
Jonathan Peyton30419822017-05-12 18:01:32 +0000238__kmp_process_deps(kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t *hash,
239 bool dep_barrier, kmp_int32 ndeps,
240 kmp_depend_info_t *dep_list, kmp_task_t *task) {
241 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d processing %d dependencies : "
242 "dep_barrier = %d\n",
243 filter, gtid, ndeps, dep_barrier));
Jonathan Peyton61118492016-05-20 19:03:38 +0000244
Jonathan Peyton30419822017-05-12 18:01:32 +0000245 kmp_info_t *thread = __kmp_threads[gtid];
246 kmp_int32 npredecessors = 0;
247 for (kmp_int32 i = 0; i < ndeps; i++) {
248 const kmp_depend_info_t *dep = &dep_list[i];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000249
Jonathan Peyton30419822017-05-12 18:01:32 +0000250 KMP_DEBUG_ASSERT(dep->flags.in);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000251
Jonathan Peyton30419822017-05-12 18:01:32 +0000252 if (filter && dep->base_addr == 0)
253 continue; // skip filtered entries
Jim Cownie5e8470a2013-09-27 10:38:44 +0000254
Jonathan Peyton30419822017-05-12 18:01:32 +0000255 kmp_dephash_entry_t *info =
256 __kmp_dephash_find(thread, hash, dep->base_addr);
257 kmp_depnode_t *last_out = info->last_out;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000258
Jonathan Peyton30419822017-05-12 18:01:32 +0000259 if (dep->flags.out && info->last_ins) {
260 for (kmp_depnode_list_t *p = info->last_ins; p; p = p->next) {
261 kmp_depnode_t *indep = p->node;
262 if (indep->dn.task) {
263 KMP_ACQUIRE_DEPNODE(gtid, indep);
264 if (indep->dn.task) {
265 __kmp_track_dependence(indep, node, task);
266 indep->dn.successors =
267 __kmp_add_node(thread, indep->dn.successors, node);
268 KA_TRACE(40, ("__kmp_process_deps<%d>: T#%d adding dependence from "
269 "%p to %p\n",
270 filter, gtid, KMP_TASK_TO_TASKDATA(indep->dn.task),
271 KMP_TASK_TO_TASKDATA(task)));
272 npredecessors++;
273 }
274 KMP_RELEASE_DEPNODE(gtid, indep);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000275 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000276 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000277
Jonathan Peyton30419822017-05-12 18:01:32 +0000278 __kmp_depnode_list_free(thread, info->last_ins);
279 info->last_ins = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000280
Jonathan Peyton30419822017-05-12 18:01:32 +0000281 } else if (last_out && last_out->dn.task) {
282 KMP_ACQUIRE_DEPNODE(gtid, last_out);
283 if (last_out->dn.task) {
284 __kmp_track_dependence(last_out, node, task);
285 last_out->dn.successors =
286 __kmp_add_node(thread, last_out->dn.successors, node);
287 KA_TRACE(
288 40,
289 ("__kmp_process_deps<%d>: T#%d adding dependence from %p to %p\n",
290 filter, gtid, KMP_TASK_TO_TASKDATA(last_out->dn.task),
291 KMP_TASK_TO_TASKDATA(task)));
292
293 npredecessors++;
294 }
295 KMP_RELEASE_DEPNODE(gtid, last_out);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000296 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000297
Jonathan Peyton30419822017-05-12 18:01:32 +0000298 if (dep_barrier) {
299 // if this is a sync point in the serial sequence, then the previous
300 // outputs are guaranteed to be completed after
301 // the execution of this task so the previous output nodes can be cleared.
302 __kmp_node_deref(thread, last_out);
303 info->last_out = NULL;
304 } else {
305 if (dep->flags.out) {
306 __kmp_node_deref(thread, last_out);
307 info->last_out = __kmp_node_ref(node);
308 } else
309 info->last_ins = __kmp_add_node(thread, info->last_ins, node);
310 }
311 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000312
Jonathan Peyton30419822017-05-12 18:01:32 +0000313 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d found %d predecessors\n", filter,
314 gtid, npredecessors));
315
316 return npredecessors;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000317}
318
319#define NO_DEP_BARRIER (false)
320#define DEP_BARRIER (true)
321
322// returns true if the task has any outstanding dependence
Jonathan Peyton30419822017-05-12 18:01:32 +0000323static bool __kmp_check_deps(kmp_int32 gtid, kmp_depnode_t *node,
324 kmp_task_t *task, kmp_dephash_t *hash,
325 bool dep_barrier, kmp_int32 ndeps,
326 kmp_depend_info_t *dep_list,
327 kmp_int32 ndeps_noalias,
328 kmp_depend_info_t *noalias_dep_list) {
329 int i;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000330
Jonathan Peytond2eb3c72015-08-26 20:02:21 +0000331#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000332 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
Jonathan Peytond2eb3c72015-08-26 20:02:21 +0000333#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000334 KA_TRACE(20, ("__kmp_check_deps: T#%d checking dependencies for task %p : %d "
335 "possibly aliased dependencies, %d non-aliased depedencies : "
336 "dep_barrier=%d .\n",
337 gtid, taskdata, ndeps, ndeps_noalias, dep_barrier));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000338
Jonathan Peyton30419822017-05-12 18:01:32 +0000339 // Filter deps in dep_list
340 // TODO: Different algorithm for large dep_list ( > 10 ? )
341 for (i = 0; i < ndeps; i++) {
342 if (dep_list[i].base_addr != 0)
343 for (int j = i + 1; j < ndeps; j++)
344 if (dep_list[i].base_addr == dep_list[j].base_addr) {
345 dep_list[i].flags.in |= dep_list[j].flags.in;
346 dep_list[i].flags.out |= dep_list[j].flags.out;
347 dep_list[j].base_addr = 0; // Mark j element as void
348 }
349 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000350
Jonathan Peyton30419822017-05-12 18:01:32 +0000351 // doesn't need to be atomic as no other thread is going to be accessing this
352 // node just yet.
353 // npredecessors is set -1 to ensure that none of the releasing tasks queues
354 // this task before we have finished processing all the dependencies
355 node->dn.npredecessors = -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000356
Jonathan Peyton30419822017-05-12 18:01:32 +0000357 // used to pack all npredecessors additions into a single atomic operation at
358 // the end
359 int npredecessors;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000360
Jonathan Peyton30419822017-05-12 18:01:32 +0000361 npredecessors = __kmp_process_deps<true>(gtid, node, hash, dep_barrier, ndeps,
362 dep_list, task);
363 npredecessors += __kmp_process_deps<false>(
364 gtid, node, hash, dep_barrier, ndeps_noalias, noalias_dep_list, task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000365
Jonathan Peyton30419822017-05-12 18:01:32 +0000366 node->dn.task = task;
367 KMP_MB();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000368
Jonathan Peyton30419822017-05-12 18:01:32 +0000369 // Account for our initial fake value
370 npredecessors++;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000371
Jonathan Peyton30419822017-05-12 18:01:32 +0000372 // Update predecessors and obtain current value to check if there are still
373 // any outstandig dependences (some tasks may have finished while we processed
374 // the dependences)
Andrey Churbanovc47afcd2017-07-03 11:24:08 +0000375 npredecessors =
376 KMP_TEST_THEN_ADD32(CCAST(kmp_int32 *, &node->dn.npredecessors),
377 npredecessors) +
378 npredecessors;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000379
Jonathan Peyton30419822017-05-12 18:01:32 +0000380 KA_TRACE(20, ("__kmp_check_deps: T#%d found %d predecessors for task %p \n",
381 gtid, npredecessors, taskdata));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000382
Jonathan Peyton30419822017-05-12 18:01:32 +0000383 // beyond this point the task could be queued (and executed) by a releasing
384 // task...
385 return npredecessors > 0 ? true : false;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000386}
387
Jonathan Peyton30419822017-05-12 18:01:32 +0000388void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
389 kmp_info_t *thread = __kmp_threads[gtid];
390 kmp_depnode_t *node = task->td_depnode;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000391
Jonathan Peyton30419822017-05-12 18:01:32 +0000392 if (task->td_dephash) {
393 KA_TRACE(
394 40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
395 gtid, task));
396 __kmp_dephash_free(thread, task->td_dephash);
397 task->td_dephash = NULL;
398 }
399
400 if (!node)
401 return;
402
403 KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n",
404 gtid, task));
405
406 KMP_ACQUIRE_DEPNODE(gtid, node);
407 node->dn.task =
408 NULL; // mark this task as finished, so no new dependencies are generated
409 KMP_RELEASE_DEPNODE(gtid, node);
410
411 kmp_depnode_list_t *next;
412 for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
413 kmp_depnode_t *successor = p->node;
414 kmp_int32 npredecessors =
Andrey Churbanovc47afcd2017-07-03 11:24:08 +0000415 KMP_TEST_THEN_DEC32(CCAST(kmp_int32 *, &successor->dn.npredecessors)) -
416 1;
Jonathan Peyton30419822017-05-12 18:01:32 +0000417 // successor task can be NULL for wait_depends or because deps are still
418 // being processed
419 if (npredecessors == 0) {
420 KMP_MB();
421 if (successor->dn.task) {
422 KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled "
423 "for execution.\n",
424 gtid, successor->dn.task, task));
425 __kmp_omp_task(gtid, successor->dn.task, false);
426 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000427 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000428
Jonathan Peyton30419822017-05-12 18:01:32 +0000429 next = p->next;
430 __kmp_node_deref(thread, p->node);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000431#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000432 __kmp_fast_free(thread, p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000433#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000434 __kmp_thread_free(thread, p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000435#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000436 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000437
Jonathan Peyton30419822017-05-12 18:01:32 +0000438 __kmp_node_deref(thread, node);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000439
Jonathan Peyton30419822017-05-12 18:01:32 +0000440 KA_TRACE(
441 20,
442 ("__kmp_release_deps: T#%d all successors of %p notified of completion\n",
443 gtid, task));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000444}
445
446/*!
447@ingroup TASKING
448@param loc_ref location of the original task directive
449@param gtid Global Thread ID of encountering thread
Jonathan Peyton30419822017-05-12 18:01:32 +0000450@param new_task task thunk allocated by __kmp_omp_task_alloc() for the ''new
451task''
Jim Cownie5e8470a2013-09-27 10:38:44 +0000452@param ndeps Number of depend items with possible aliasing
453@param dep_list List of depend items with possible aliasing
454@param ndeps_noalias Number of depend items with no aliasing
455@param noalias_dep_list List of depend items with no aliasing
456
Jonathan Peyton30419822017-05-12 18:01:32 +0000457@return Returns either TASK_CURRENT_NOT_QUEUED if the current task was not
458suspendend and queued, or TASK_CURRENT_QUEUED if it was suspended and queued
Jim Cownie5e8470a2013-09-27 10:38:44 +0000459
460Schedule a non-thread-switchable task with dependences for execution
461*/
Jonathan Peyton30419822017-05-12 18:01:32 +0000462kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 gtid,
463 kmp_task_t *new_task, kmp_int32 ndeps,
464 kmp_depend_info_t *dep_list,
465 kmp_int32 ndeps_noalias,
466 kmp_depend_info_t *noalias_dep_list) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000467
Jonathan Peyton30419822017-05-12 18:01:32 +0000468 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
469 KA_TRACE(10, ("__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n", gtid,
470 loc_ref, new_taskdata));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000471
Jonathan Peyton30419822017-05-12 18:01:32 +0000472 kmp_info_t *thread = __kmp_threads[gtid];
473 kmp_taskdata_t *current_task = thread->th.th_current_task;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000474
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000475#if OMPT_SUPPORT && OMPT_TRACE
Jonathan Peyton30419822017-05-12 18:01:32 +0000476 /* OMPT grab all dependences if requested by the tool */
477 if (ompt_enabled && ndeps + ndeps_noalias > 0 &&
478 ompt_callbacks.ompt_callback(ompt_event_task_dependences)) {
479 kmp_int32 i;
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000480
Jonathan Peyton30419822017-05-12 18:01:32 +0000481 new_taskdata->ompt_task_info.ndeps = ndeps + ndeps_noalias;
482 new_taskdata->ompt_task_info.deps =
483 (ompt_task_dependence_t *)KMP_OMPT_DEPS_ALLOC(
484 thread, (ndeps + ndeps_noalias) * sizeof(ompt_task_dependence_t));
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000485
Jonathan Peyton30419822017-05-12 18:01:32 +0000486 KMP_ASSERT(new_taskdata->ompt_task_info.deps != NULL);
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000487
Jonathan Peyton30419822017-05-12 18:01:32 +0000488 for (i = 0; i < ndeps; i++) {
489 new_taskdata->ompt_task_info.deps[i].variable_addr =
490 (void *)dep_list[i].base_addr;
491 if (dep_list[i].flags.in && dep_list[i].flags.out)
492 new_taskdata->ompt_task_info.deps[i].dependence_flags =
493 ompt_task_dependence_type_inout;
494 else if (dep_list[i].flags.out)
495 new_taskdata->ompt_task_info.deps[i].dependence_flags =
496 ompt_task_dependence_type_out;
497 else if (dep_list[i].flags.in)
498 new_taskdata->ompt_task_info.deps[i].dependence_flags =
499 ompt_task_dependence_type_in;
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000500 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000501 for (i = 0; i < ndeps_noalias; i++) {
502 new_taskdata->ompt_task_info.deps[ndeps + i].variable_addr =
503 (void *)noalias_dep_list[i].base_addr;
504 if (noalias_dep_list[i].flags.in && noalias_dep_list[i].flags.out)
505 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
506 ompt_task_dependence_type_inout;
507 else if (noalias_dep_list[i].flags.out)
508 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
509 ompt_task_dependence_type_out;
510 else if (noalias_dep_list[i].flags.in)
511 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
512 ompt_task_dependence_type_in;
513 }
514 }
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000515#endif /* OMPT_SUPPORT && OMPT_TRACE */
516
Jonathan Peyton30419822017-05-12 18:01:32 +0000517 bool serial = current_task->td_flags.team_serial ||
518 current_task->td_flags.tasking_ser ||
519 current_task->td_flags.final;
Jonathan Peytondf6818b2016-06-14 17:57:47 +0000520#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000521 kmp_task_team_t *task_team = thread->th.th_task_team;
522 serial = serial && !(task_team && task_team->tt.tt_found_proxy_tasks);
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000523#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000524
Jonathan Peyton30419822017-05-12 18:01:32 +0000525 if (!serial && (ndeps > 0 || ndeps_noalias > 0)) {
526 /* if no dependencies have been tracked yet, create the dependence hash */
527 if (current_task->td_dephash == NULL)
528 current_task->td_dephash = __kmp_dephash_create(thread, current_task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000529
530#if USE_FAST_MEMORY
Jonathan Peyton30419822017-05-12 18:01:32 +0000531 kmp_depnode_t *node =
532 (kmp_depnode_t *)__kmp_fast_allocate(thread, sizeof(kmp_depnode_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000533#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000534 kmp_depnode_t *node =
535 (kmp_depnode_t *)__kmp_thread_malloc(thread, sizeof(kmp_depnode_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000536#endif
537
Jonathan Peyton30419822017-05-12 18:01:32 +0000538 __kmp_init_node(node);
539 new_taskdata->td_depnode = node;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000540
Jonathan Peyton30419822017-05-12 18:01:32 +0000541 if (__kmp_check_deps(gtid, node, new_task, current_task->td_dephash,
542 NO_DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
543 noalias_dep_list)) {
544 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had blocking "
545 "dependencies: "
546 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
547 gtid, loc_ref, new_taskdata));
548 return TASK_CURRENT_NOT_QUEUED;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000549 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000550 } else {
551 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d ignored dependencies "
552 "for task (serialized)"
553 "loc=%p task=%p\n",
554 gtid, loc_ref, new_taskdata));
555 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000556
Jonathan Peyton30419822017-05-12 18:01:32 +0000557 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had no blocking "
558 "dependencies : "
559 "loc=%p task=%p, transferring to __kmpc_omp_task\n",
560 gtid, loc_ref, new_taskdata));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000561
Jonathan Peyton30419822017-05-12 18:01:32 +0000562 return __kmpc_omp_task(loc_ref, gtid, new_task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000563}
564
565/*!
566@ingroup TASKING
567@param loc_ref location of the original task directive
568@param gtid Global Thread ID of encountering thread
569@param ndeps Number of depend items with possible aliasing
570@param dep_list List of depend items with possible aliasing
571@param ndeps_noalias Number of depend items with no aliasing
572@param noalias_dep_list List of depend items with no aliasing
573
574Blocks the current task until all specifies dependencies have been fulfilled.
575*/
Jonathan Peyton30419822017-05-12 18:01:32 +0000576void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps,
577 kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
578 kmp_depend_info_t *noalias_dep_list) {
579 KA_TRACE(10, ("__kmpc_omp_wait_deps(enter): T#%d loc=%p\n", gtid, loc_ref));
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000580
Jonathan Peyton30419822017-05-12 18:01:32 +0000581 if (ndeps == 0 && ndeps_noalias == 0) {
582 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no dependencies to "
583 "wait upon : loc=%p\n",
584 gtid, loc_ref));
585 return;
586 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000587
Jonathan Peyton30419822017-05-12 18:01:32 +0000588 kmp_info_t *thread = __kmp_threads[gtid];
589 kmp_taskdata_t *current_task = thread->th.th_current_task;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000590
Jonathan Peyton30419822017-05-12 18:01:32 +0000591 // We can return immediately as:
592 // - dependences are not computed in serial teams (except with proxy tasks)
593 // - if the dephash is not yet created it means we have nothing to wait for
594 bool ignore = current_task->td_flags.team_serial ||
595 current_task->td_flags.tasking_ser ||
596 current_task->td_flags.final;
Jonathan Peytondf6818b2016-06-14 17:57:47 +0000597#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000598 ignore = ignore && thread->th.th_task_team != NULL &&
599 thread->th.th_task_team->tt.tt_found_proxy_tasks == FALSE;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000600#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000601 ignore = ignore || current_task->td_dephash == NULL;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000602
Jonathan Peyton30419822017-05-12 18:01:32 +0000603 if (ignore) {
604 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking "
605 "dependencies : loc=%p\n",
606 gtid, loc_ref));
607 return;
608 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000609
Jonathan Peyton30419822017-05-12 18:01:32 +0000610 kmp_depnode_t node;
611 __kmp_init_node(&node);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000612
Jonathan Peyton30419822017-05-12 18:01:32 +0000613 if (!__kmp_check_deps(gtid, &node, NULL, current_task->td_dephash,
614 DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
615 noalias_dep_list)) {
616 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking "
617 "dependencies : loc=%p\n",
618 gtid, loc_ref));
619 return;
620 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000621
Jonathan Peyton30419822017-05-12 18:01:32 +0000622 int thread_finished = FALSE;
623 kmp_flag_32 flag((volatile kmp_uint32 *)&(node.dn.npredecessors), 0U);
624 while (node.dn.npredecessors > 0) {
625 flag.execute_tasks(thread, gtid, FALSE, &thread_finished,
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000626#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000627 NULL,
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000628#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000629 __kmp_task_stealing_constraint);
630 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000631
Jonathan Peyton30419822017-05-12 18:01:32 +0000632 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d finished waiting : loc=%p\n",
633 gtid, loc_ref));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000634}
635
636#endif /* OMP_40_ENABLED */