blob: 6fbf0b0df79208e495ff44001d7dc7938ce9d593 [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
24//TODO: Improve memory allocation? keep a list of pre-allocated structures? allocate in blocks? re-use list finished list entries?
25//TODO: don't use atomic ref counters for stack-allocated nodes.
26//TODO: find an alternate to atomic refs for heap-allocated nodes?
27//TODO: Finish graph output support
28//TODO: kmp_lock_t seems a tad to big (and heavy weight) for this. Check other runtime locks
29//TODO: Any ITT support needed?
30
31#ifdef KMP_SUPPORT_GRAPH_OUTPUT
32static kmp_int32 kmp_node_id_seed = 0;
33#endif
34
35static void
36__kmp_init_node ( kmp_depnode_t *node )
37{
38 node->dn.task = NULL; // set to null initially, it will point to the right task once dependences have been processed
39 node->dn.successors = NULL;
40 __kmp_init_lock(&node->dn.lock);
41 node->dn.nrefs = 1; // init creates the first reference to the node
42#ifdef KMP_SUPPORT_GRAPH_OUTPUT
43 node->dn.id = KMP_TEST_THEN_INC32(&kmp_node_id_seed);
44#endif
45}
46
47static inline kmp_depnode_t *
48__kmp_node_ref ( kmp_depnode_t *node )
49{
50 KMP_TEST_THEN_INC32(&node->dn.nrefs);
51 return node;
52}
53
54static inline void
55__kmp_node_deref ( kmp_info_t *thread, kmp_depnode_t *node )
56{
57 if (!node) return;
58
59 kmp_int32 n = KMP_TEST_THEN_DEC32(&node->dn.nrefs) - 1;
60 if ( n == 0 ) {
61 KMP_ASSERT(node->dn.nrefs == 0);
62#if USE_FAST_MEMORY
63 __kmp_fast_free(thread,node);
64#else
65 __kmp_thread_free(thread,node);
66#endif
67 }
68}
69
70#define KMP_ACQUIRE_DEPNODE(gtid,n) __kmp_acquire_lock(&(n)->dn.lock,(gtid))
71#define KMP_RELEASE_DEPNODE(gtid,n) __kmp_release_lock(&(n)->dn.lock,(gtid))
72
73static void
74__kmp_depnode_list_free ( kmp_info_t *thread, kmp_depnode_list *list );
75
Jonathan Peyton7d454512016-01-28 23:10:44 +000076enum {
77 KMP_DEPHASH_OTHER_SIZE = 97,
78 KMP_DEPHASH_MASTER_SIZE = 997
79};
Jim Cownie5e8470a2013-09-27 10:38:44 +000080
81static inline kmp_int32
Jonathan Peyton7d454512016-01-28 23:10:44 +000082__kmp_dephash_hash ( kmp_intptr_t addr, size_t hsize )
Jim Cownie5e8470a2013-09-27 10:38:44 +000083{
84 //TODO alternate to try: set = (((Addr64)(addrUsefulBits * 9.618)) % m_num_sets );
Jonathan Peyton7d454512016-01-28 23:10:44 +000085 return ((addr >> 6) ^ (addr >> 2)) % hsize;
Jim Cownie5e8470a2013-09-27 10:38:44 +000086}
87
88static kmp_dephash_t *
Jonathan Peyton7d454512016-01-28 23:10:44 +000089__kmp_dephash_create ( kmp_info_t *thread, kmp_taskdata_t *current_task )
Jim Cownie5e8470a2013-09-27 10:38:44 +000090{
91 kmp_dephash_t *h;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000092
Jonathan Peyton7d454512016-01-28 23:10:44 +000093 size_t h_size;
94
95 if ( current_task->td_flags.tasktype == TASK_IMPLICIT )
96 h_size = KMP_DEPHASH_MASTER_SIZE;
97 else
98 h_size = KMP_DEPHASH_OTHER_SIZE;
99
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000100 kmp_int32 size =
101 h_size * sizeof(kmp_dephash_entry_t *) + sizeof(kmp_dephash_t);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000102
Jim Cownie5e8470a2013-09-27 10:38:44 +0000103#if USE_FAST_MEMORY
104 h = (kmp_dephash_t *) __kmp_fast_allocate( thread, size );
105#else
106 h = (kmp_dephash_t *) __kmp_thread_malloc( thread, size );
107#endif
Jonathan Peyton7d454512016-01-28 23:10:44 +0000108 h->size = h_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000109
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000110#ifdef KMP_DEBUG
Jim Cownie5e8470a2013-09-27 10:38:44 +0000111 h->nelements = 0;
Jonathan Peyton7d454512016-01-28 23:10:44 +0000112 h->nconflicts = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000113#endif
114 h->buckets = (kmp_dephash_entry **)(h+1);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000115
Jonathan Peyton7d454512016-01-28 23:10:44 +0000116 for ( size_t i = 0; i < h_size; i++ )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000117 h->buckets[i] = 0;
118
119 return h;
120}
121
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000122void
123__kmp_dephash_free_entries(kmp_info_t *thread, kmp_dephash_t *h)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000124{
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000125 for (size_t i = 0; i < h->size; i++) {
126 if (h->buckets[i]) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000127 kmp_dephash_entry_t *next;
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000128 for (kmp_dephash_entry_t *entry = h->buckets[i]; entry; entry = next) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000129 next = entry->next_in_bucket;
130 __kmp_depnode_list_free(thread,entry->last_ins);
131 __kmp_node_deref(thread,entry->last_out);
132#if USE_FAST_MEMORY
133 __kmp_fast_free(thread,entry);
134#else
135 __kmp_thread_free(thread,entry);
136#endif
137 }
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000138 h->buckets[i] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000139 }
140 }
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +0000141}
142
143void
144__kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h)
145{
146 __kmp_dephash_free_entries(thread, h);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000147#if USE_FAST_MEMORY
148 __kmp_fast_free(thread,h);
149#else
150 __kmp_thread_free(thread,h);
151#endif
152}
153
154static kmp_dephash_entry *
155__kmp_dephash_find ( kmp_info_t *thread, kmp_dephash_t *h, kmp_intptr_t addr )
156{
Jonathan Peyton7d454512016-01-28 23:10:44 +0000157 kmp_int32 bucket = __kmp_dephash_hash(addr,h->size);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000158
Jim Cownie5e8470a2013-09-27 10:38:44 +0000159 kmp_dephash_entry_t *entry;
160 for ( entry = h->buckets[bucket]; entry; entry = entry->next_in_bucket )
161 if ( entry->addr == addr ) break;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000162
Jim Cownie5e8470a2013-09-27 10:38:44 +0000163 if ( entry == NULL ) {
164 // create entry. This is only done by one thread so no locking required
165#if USE_FAST_MEMORY
166 entry = (kmp_dephash_entry_t *) __kmp_fast_allocate( thread, sizeof(kmp_dephash_entry_t) );
167#else
168 entry = (kmp_dephash_entry_t *) __kmp_thread_malloc( thread, sizeof(kmp_dephash_entry_t) );
169#endif
170 entry->addr = addr;
171 entry->last_out = NULL;
172 entry->last_ins = NULL;
173 entry->next_in_bucket = h->buckets[bucket];
174 h->buckets[bucket] = entry;
175#ifdef KMP_DEBUG
176 h->nelements++;
177 if ( entry->next_in_bucket ) h->nconflicts++;
178#endif
179 }
180 return entry;
181}
182
183static kmp_depnode_list_t *
184__kmp_add_node ( kmp_info_t *thread, kmp_depnode_list_t *list, kmp_depnode_t *node )
185{
186 kmp_depnode_list_t *new_head;
187
188#if USE_FAST_MEMORY
189 new_head = (kmp_depnode_list_t *) __kmp_fast_allocate(thread,sizeof(kmp_depnode_list_t));
190#else
191 new_head = (kmp_depnode_list_t *) __kmp_thread_malloc(thread,sizeof(kmp_depnode_list_t));
192#endif
193
194 new_head->node = __kmp_node_ref(node);
195 new_head->next = list;
196
197 return new_head;
198}
199
200static void
201__kmp_depnode_list_free ( kmp_info_t *thread, kmp_depnode_list *list )
202{
203 kmp_depnode_list *next;
204
205 for ( ; list ; list = next ) {
206 next = list->next;
207
208 __kmp_node_deref(thread,list->node);
209#if USE_FAST_MEMORY
210 __kmp_fast_free(thread,list);
211#else
212 __kmp_thread_free(thread,list);
213#endif
214 }
215}
216
217static inline void
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000218__kmp_track_dependence ( kmp_depnode_t *source, kmp_depnode_t *sink,
219 kmp_task_t *sink_task )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000220{
221#ifdef KMP_SUPPORT_GRAPH_OUTPUT
222 kmp_taskdata_t * task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
Jonas Hahnfeld20236612016-08-04 11:03:47 +0000223 // do not use sink->dn.task as that is only filled after the dependencies
224 // are already processed!
225 kmp_taskdata_t * task_sink = KMP_TASK_TO_TASKDATA(sink_task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000226
227 __kmp_printf("%d(%s) -> %d(%s)\n", source->dn.id, task_source->td_ident->psource, sink->dn.id, task_sink->td_ident->psource);
228#endif
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000229#if OMPT_SUPPORT && OMPT_TRACE
230 /* OMPT tracks dependences between task (a=source, b=sink) in which
231 task a blocks the execution of b through the ompt_new_dependence_callback */
232 if (ompt_enabled &&
233 ompt_callbacks.ompt_callback(ompt_event_task_dependence_pair))
234 {
235 kmp_taskdata_t * task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
236 kmp_taskdata_t * task_sink = KMP_TASK_TO_TASKDATA(sink_task);
237
238 ompt_callbacks.ompt_callback(ompt_event_task_dependence_pair)(
239 task_source->ompt_task_info.task_id,
240 task_sink->ompt_task_info.task_id);
241 }
242#endif /* OMPT_SUPPORT && OMPT_TRACE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000243}
244
245template< bool filter >
246static inline kmp_int32
247__kmp_process_deps ( kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t *hash,
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000248 bool dep_barrier,kmp_int32 ndeps, kmp_depend_info_t *dep_list,
249 kmp_task_t *task )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000250{
Jonathan Peytona88e8352016-11-28 20:10:32 +0000251 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d processing %d dependencies : dep_barrier = %d\n", filter, gtid, ndeps, dep_barrier ) );
Jonathan Peyton61118492016-05-20 19:03:38 +0000252
Jim Cownie5e8470a2013-09-27 10:38:44 +0000253 kmp_info_t *thread = __kmp_threads[ gtid ];
254 kmp_int32 npredecessors=0;
255 for ( kmp_int32 i = 0; i < ndeps ; i++ ) {
256 const kmp_depend_info_t * dep = &dep_list[i];
257
258 KMP_DEBUG_ASSERT(dep->flags.in);
259
260 if ( filter && dep->base_addr == 0 ) continue; // skip filtered entries
261
262 kmp_dephash_entry_t *info = __kmp_dephash_find(thread,hash,dep->base_addr);
263 kmp_depnode_t *last_out = info->last_out;
264
265 if ( dep->flags.out && info->last_ins ) {
266 for ( kmp_depnode_list_t * p = info->last_ins; p; p = p->next ) {
267 kmp_depnode_t * indep = p->node;
268 if ( indep->dn.task ) {
269 KMP_ACQUIRE_DEPNODE(gtid,indep);
270 if ( indep->dn.task ) {
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000271 __kmp_track_dependence(indep,node,task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000272 indep->dn.successors = __kmp_add_node(thread, indep->dn.successors, node);
Jonathan Peyton55c447f2015-11-16 22:53:38 +0000273 KA_TRACE(40,("__kmp_process_deps<%d>: T#%d adding dependence from %p to %p\n",
Jonas Hahnfeld20236612016-08-04 11:03:47 +0000274 filter,gtid, KMP_TASK_TO_TASKDATA(indep->dn.task), KMP_TASK_TO_TASKDATA(task)));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000275 npredecessors++;
276 }
277 KMP_RELEASE_DEPNODE(gtid,indep);
278 }
279 }
280
281 __kmp_depnode_list_free(thread,info->last_ins);
282 info->last_ins = NULL;
283
284 } else if ( last_out && last_out->dn.task ) {
285 KMP_ACQUIRE_DEPNODE(gtid,last_out);
286 if ( last_out->dn.task ) {
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000287 __kmp_track_dependence(last_out,node,task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000288 last_out->dn.successors = __kmp_add_node(thread, last_out->dn.successors, node);
Jonathan Peyton61118492016-05-20 19:03:38 +0000289 KA_TRACE(40,("__kmp_process_deps<%d>: T#%d adding dependence from %p to %p\n",
Jonas Hahnfeld20236612016-08-04 11:03:47 +0000290 filter,gtid, KMP_TASK_TO_TASKDATA(last_out->dn.task), KMP_TASK_TO_TASKDATA(task)));
Jonathan Peyton61118492016-05-20 19:03:38 +0000291
Jim Cownie5e8470a2013-09-27 10:38:44 +0000292 npredecessors++;
293 }
294 KMP_RELEASE_DEPNODE(gtid,last_out);
295 }
296
297 if ( dep_barrier ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000298 // if this is a sync point in the serial sequence, then the previous outputs are guaranteed to be completed after
Jim Cownie5e8470a2013-09-27 10:38:44 +0000299 // the execution of this task so the previous output nodes can be cleared.
300 __kmp_node_deref(thread,last_out);
301 info->last_out = NULL;
302 } else {
303 if ( dep->flags.out ) {
304 __kmp_node_deref(thread,last_out);
305 info->last_out = __kmp_node_ref(node);
306 } else
307 info->last_ins = __kmp_add_node(thread, info->last_ins, node);
308 }
309
310 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000311
312 KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d found %d predecessors\n", filter, gtid, npredecessors ) );
313
Jim Cownie5e8470a2013-09-27 10:38:44 +0000314 return npredecessors;
315}
316
317#define NO_DEP_BARRIER (false)
318#define DEP_BARRIER (true)
319
320// returns true if the task has any outstanding dependence
321static bool
322__kmp_check_deps ( kmp_int32 gtid, kmp_depnode_t *node, kmp_task_t *task, kmp_dephash_t *hash, bool dep_barrier,
323 kmp_int32 ndeps, kmp_depend_info_t *dep_list,
324 kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list )
325{
326 int i;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000327
Jonathan Peytond2eb3c72015-08-26 20:02:21 +0000328#if KMP_DEBUG
329 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
330#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000331 KA_TRACE(20, ("__kmp_check_deps: T#%d checking dependencies for task %p : %d possibly aliased dependencies, %d non-aliased depedencies : dep_barrier=%d .\n", gtid, taskdata, ndeps, ndeps_noalias, dep_barrier ) );
332
Jim Cownie5e8470a2013-09-27 10:38:44 +0000333 // Filter deps in dep_list
334 // TODO: Different algorithm for large dep_list ( > 10 ? )
335 for ( i = 0; i < ndeps; i ++ ) {
336 if ( dep_list[i].base_addr != 0 )
337 for ( int j = i+1; j < ndeps; j++ )
338 if ( dep_list[i].base_addr == dep_list[j].base_addr ) {
339 dep_list[i].flags.in |= dep_list[j].flags.in;
340 dep_list[i].flags.out |= dep_list[j].flags.out;
341 dep_list[j].base_addr = 0; // Mark j element as void
342 }
343 }
344
345 // doesn't need to be atomic as no other thread is going to be accessing this node just yet
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000346 // npredecessors is set -1 to ensure that none of the releasing tasks queues this task before we have finished processing all the dependencies
347 node->dn.npredecessors = -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000348
349 // used to pack all npredecessors additions into a single atomic operation at the end
350 int npredecessors;
351
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000352 npredecessors = __kmp_process_deps<true>(gtid, node, hash, dep_barrier,
353 ndeps, dep_list, task);
354 npredecessors += __kmp_process_deps<false>(gtid, node, hash, dep_barrier,
355 ndeps_noalias, noalias_dep_list, task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000356
Jim Cownie5e8470a2013-09-27 10:38:44 +0000357 node->dn.task = task;
358 KMP_MB();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000359
360 // Account for our initial fake value
361 npredecessors++;
362
363 // Update predecessors and obtain current value to check if there are still any outstandig dependences (some tasks may have finished while we processed the dependences)
364 npredecessors = KMP_TEST_THEN_ADD32(&node->dn.npredecessors, npredecessors) + npredecessors;
365
366 KA_TRACE(20, ("__kmp_check_deps: T#%d found %d predecessors for task %p \n", gtid, npredecessors, taskdata ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000367
368 // beyond this point the task could be queued (and executed) by a releasing task...
369 return npredecessors > 0 ? true : false;
370}
371
372void
373__kmp_release_deps ( kmp_int32 gtid, kmp_taskdata_t *task )
374{
375 kmp_info_t *thread = __kmp_threads[ gtid ];
376 kmp_depnode_t *node = task->td_depnode;
377
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000378 if ( task->td_dephash ) {
Jonathan Peyton3c4050d2016-10-26 21:46:43 +0000379 KA_TRACE(40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n", gtid, task ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000380 __kmp_dephash_free(thread,task->td_dephash);
Jonathan Peyton96fe1aa2016-11-21 16:24:59 +0000381 task->td_dephash = NULL;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000382 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000383
384 if ( !node ) return;
385
Jonathan Peyton3c4050d2016-10-26 21:46:43 +0000386 KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n", gtid, task ) );
Jonathan Peyton61118492016-05-20 19:03:38 +0000387
Jim Cownie5e8470a2013-09-27 10:38:44 +0000388 KMP_ACQUIRE_DEPNODE(gtid,node);
389 node->dn.task = NULL; // mark this task as finished, so no new dependencies are generated
390 KMP_RELEASE_DEPNODE(gtid,node);
391
392 kmp_depnode_list_t *next;
393 for ( kmp_depnode_list_t *p = node->dn.successors; p; p = next ) {
394 kmp_depnode_t *successor = p->node;
395 kmp_int32 npredecessors = KMP_TEST_THEN_DEC32(&successor->dn.npredecessors) - 1;
396
397 // successor task can be NULL for wait_depends or because deps are still being processed
398 if ( npredecessors == 0 ) {
399 KMP_MB();
Jonathan Peyton61118492016-05-20 19:03:38 +0000400 if ( successor->dn.task ) {
Jonathan Peyton3c4050d2016-10-26 21:46:43 +0000401 KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled for execution.\n", gtid, successor->dn.task, task ) );
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000402 __kmp_omp_task(gtid,successor->dn.task,false);
403 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000404 }
405
406 next = p->next;
407 __kmp_node_deref(thread,p->node);
408#if USE_FAST_MEMORY
409 __kmp_fast_free(thread,p);
410#else
411 __kmp_thread_free(thread,p);
412#endif
413 }
414
415 __kmp_node_deref(thread,node);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000416
Jonathan Peyton3c4050d2016-10-26 21:46:43 +0000417 KA_TRACE(20, ("__kmp_release_deps: T#%d all successors of %p notified of completion\n", gtid, task ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000418}
419
420/*!
421@ingroup TASKING
422@param loc_ref location of the original task directive
423@param gtid Global Thread ID of encountering thread
424@param new_task task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
425@param ndeps Number of depend items with possible aliasing
426@param dep_list List of depend items with possible aliasing
427@param ndeps_noalias Number of depend items with no aliasing
428@param noalias_dep_list List of depend items with no aliasing
429
430@return Returns either TASK_CURRENT_NOT_QUEUED if the current task was not suspendend and queued, or TASK_CURRENT_QUEUED if it was suspended and queued
431
432Schedule a non-thread-switchable task with dependences for execution
433*/
434kmp_int32
435__kmpc_omp_task_with_deps( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * new_task,
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000436 kmp_int32 ndeps, kmp_depend_info_t *dep_list,
437 kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000438{
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000439
440 kmp_taskdata_t * new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
441 KA_TRACE(10, ("__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n",
442 gtid, loc_ref, new_taskdata ) );
443
Jim Cownie5e8470a2013-09-27 10:38:44 +0000444 kmp_info_t *thread = __kmp_threads[ gtid ];
445 kmp_taskdata_t * current_task = thread->th.th_current_task;
446
Jonas Hahnfeld39b68622016-01-28 10:39:52 +0000447#if OMPT_SUPPORT && OMPT_TRACE
448 /* OMPT grab all dependences if requested by the tool */
449 if (ompt_enabled && ndeps+ndeps_noalias > 0 &&
450 ompt_callbacks.ompt_callback(ompt_event_task_dependences))
451 {
452 kmp_int32 i;
453
454 new_taskdata->ompt_task_info.ndeps = ndeps+ndeps_noalias;
455 new_taskdata->ompt_task_info.deps = (ompt_task_dependence_t *)
456 KMP_OMPT_DEPS_ALLOC(thread,
457 (ndeps+ndeps_noalias)*sizeof(ompt_task_dependence_t));
458
459 KMP_ASSERT(new_taskdata->ompt_task_info.deps != NULL);
460
461 for (i = 0; i < ndeps; i++)
462 {
463 new_taskdata->ompt_task_info.deps[i].variable_addr =
464 (void*) dep_list[i].base_addr;
465 if (dep_list[i].flags.in && dep_list[i].flags.out)
466 new_taskdata->ompt_task_info.deps[i].dependence_flags =
467 ompt_task_dependence_type_inout;
468 else if (dep_list[i].flags.out)
469 new_taskdata->ompt_task_info.deps[i].dependence_flags =
470 ompt_task_dependence_type_out;
471 else if (dep_list[i].flags.in)
472 new_taskdata->ompt_task_info.deps[i].dependence_flags =
473 ompt_task_dependence_type_in;
474 }
475 for (i = 0; i < ndeps_noalias; i++)
476 {
477 new_taskdata->ompt_task_info.deps[ndeps+i].variable_addr =
478 (void*) noalias_dep_list[i].base_addr;
479 if (noalias_dep_list[i].flags.in && noalias_dep_list[i].flags.out)
480 new_taskdata->ompt_task_info.deps[ndeps+i].dependence_flags =
481 ompt_task_dependence_type_inout;
482 else if (noalias_dep_list[i].flags.out)
483 new_taskdata->ompt_task_info.deps[ndeps+i].dependence_flags =
484 ompt_task_dependence_type_out;
485 else if (noalias_dep_list[i].flags.in)
486 new_taskdata->ompt_task_info.deps[ndeps+i].dependence_flags =
487 ompt_task_dependence_type_in;
488 }
489 }
490#endif /* OMPT_SUPPORT && OMPT_TRACE */
491
Jim Cownie5e8470a2013-09-27 10:38:44 +0000492 bool serial = current_task->td_flags.team_serial || current_task->td_flags.tasking_ser || current_task->td_flags.final;
Jonathan Peytondf6818b2016-06-14 17:57:47 +0000493#if OMP_45_ENABLED
Jonas Hahnfeldbedc3712016-08-08 10:08:14 +0000494 kmp_task_team_t * task_team = thread->th.th_task_team;
495 serial = serial && !(task_team && task_team->tt.tt_found_proxy_tasks);
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000496#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000497
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000498 if ( !serial && ( ndeps > 0 || ndeps_noalias > 0 )) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000499 /* if no dependencies have been tracked yet, create the dependence hash */
500 if ( current_task->td_dephash == NULL )
Jonathan Peyton7d454512016-01-28 23:10:44 +0000501 current_task->td_dephash = __kmp_dephash_create(thread, current_task);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000502
503#if USE_FAST_MEMORY
504 kmp_depnode_t *node = (kmp_depnode_t *) __kmp_fast_allocate(thread,sizeof(kmp_depnode_t));
505#else
506 kmp_depnode_t *node = (kmp_depnode_t *) __kmp_thread_malloc(thread,sizeof(kmp_depnode_t));
507#endif
508
509 __kmp_init_node(node);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000510 new_taskdata->td_depnode = node;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000511
512 if ( __kmp_check_deps( gtid, node, new_task, current_task->td_dephash, NO_DEP_BARRIER,
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000513 ndeps, dep_list, ndeps_noalias,noalias_dep_list ) ) {
514 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had blocking dependencies: "
515 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n", gtid, loc_ref,
516 new_taskdata ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000517 return TASK_CURRENT_NOT_QUEUED;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000518 }
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000519 } else {
Jonas Hahnfeldbedc3712016-08-08 10:08:14 +0000520 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d ignored dependencies for task (serialized)"
521 "loc=%p task=%p\n", gtid, loc_ref, new_taskdata ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000522 }
523
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000524 KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had no blocking dependencies : "
525 "loc=%p task=%p, transferring to __kmpc_omp_task\n", gtid, loc_ref,
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000526 new_taskdata ) );
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000527
Jim Cownie5e8470a2013-09-27 10:38:44 +0000528 return __kmpc_omp_task(loc_ref,gtid,new_task);
529}
530
531/*!
532@ingroup TASKING
533@param loc_ref location of the original task directive
534@param gtid Global Thread ID of encountering thread
535@param ndeps Number of depend items with possible aliasing
536@param dep_list List of depend items with possible aliasing
537@param ndeps_noalias Number of depend items with no aliasing
538@param noalias_dep_list List of depend items with no aliasing
539
540Blocks the current task until all specifies dependencies have been fulfilled.
541*/
542void
543__kmpc_omp_wait_deps ( ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
544 kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list )
545{
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000546 KA_TRACE(10, ("__kmpc_omp_wait_deps(enter): T#%d loc=%p\n", gtid, loc_ref) );
547
548 if ( ndeps == 0 && ndeps_noalias == 0 ) {
549 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no dependencies to wait upon : loc=%p\n", gtid, loc_ref) );
550 return;
551 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000552
553 kmp_info_t *thread = __kmp_threads[ gtid ];
554 kmp_taskdata_t * current_task = thread->th.th_current_task;
555
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000556 // We can return immediately as:
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000557 // - dependences are not computed in serial teams (except if we have proxy tasks)
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000558 // - if the dephash is not yet created it means we have nothing to wait for
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000559 bool ignore = current_task->td_flags.team_serial || current_task->td_flags.tasking_ser || current_task->td_flags.final;
Jonathan Peytondf6818b2016-06-14 17:57:47 +0000560#if OMP_45_ENABLED
Jonathan Peytonc505ab62016-06-16 20:18:31 +0000561 ignore = ignore && thread->th.th_task_team != NULL && thread->th.th_task_team->tt.tt_found_proxy_tasks == FALSE;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000562#endif
563 ignore = ignore || current_task->td_dephash == NULL;
564
565 if ( ignore ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000566 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking dependencies : loc=%p\n", gtid, loc_ref) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000567 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000568 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000569
570 kmp_depnode_t node;
571 __kmp_init_node(&node);
572
573 if (!__kmp_check_deps( gtid, &node, NULL, current_task->td_dephash, DEP_BARRIER,
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000574 ndeps, dep_list, ndeps_noalias, noalias_dep_list )) {
575 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking dependencies : loc=%p\n", gtid, loc_ref) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000576 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000577 }
578
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000579 int thread_finished = FALSE;
580 kmp_flag_32 flag((volatile kmp_uint32 *)&(node.dn.npredecessors), 0U);
581 while ( node.dn.npredecessors > 0 ) {
582 flag.execute_tasks(thread, gtid, FALSE, &thread_finished,
583#if USE_ITT_BUILD
584 NULL,
585#endif
586 __kmp_task_stealing_constraint );
587 }
588
589 KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d finished waiting : loc=%p\n", gtid, loc_ref) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000590}
591
592#endif /* OMP_40_ENABLED */
593