1 /*
2  * kmp_taskdeps.cpp
3  * $Revision: 42539 $
4  * $Date: 2013-07-17 11:20:01 -0500 (Wed, 17 Jul 2013) $
5  */
6 
7 
8 //===----------------------------------------------------------------------===//
9 //
10 //                     The LLVM Compiler Infrastructure
11 //
12 // This file is dual licensed under the MIT and the University of Illinois Open
13 // Source Licenses. See LICENSE.txt for details.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 
18 //#define KMP_SUPPORT_GRAPH_OUTPUT 1
19 
20 #include "kmp.h"
21 #include "kmp_io.h"
22 #include "kmp_wait_release.h"
23 
24 #if OMP_40_ENABLED
25 
26 //TODO: Improve memory allocation? keep a list of pre-allocated structures? 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 runtime locks
31 //TODO: Any ITT support needed?
32 
33 #ifdef KMP_SUPPORT_GRAPH_OUTPUT
34 static kmp_int32 kmp_node_id_seed = 0;
35 #endif
36 
37 static void
38 __kmp_init_node ( kmp_depnode_t *node )
39 {
40     node->dn.task = NULL; // set to null initially, it will point to the right 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
44 #ifdef KMP_SUPPORT_GRAPH_OUTPUT
45     node->dn.id = KMP_TEST_THEN_INC32(&kmp_node_id_seed);
46 #endif
47 }
48 
49 static inline kmp_depnode_t *
50 __kmp_node_ref ( kmp_depnode_t *node )
51 {
52     KMP_TEST_THEN_INC32(&node->dn.nrefs);
53     return node;
54 }
55 
56 static inline void
57 __kmp_node_deref ( kmp_info_t *thread, kmp_depnode_t *node )
58 {
59     if (!node) return;
60 
61     kmp_int32 n = KMP_TEST_THEN_DEC32(&node->dn.nrefs) - 1;
62     if ( n == 0 ) {
63         KMP_ASSERT(node->dn.nrefs == 0);
64 #if USE_FAST_MEMORY
65         __kmp_fast_free(thread,node);
66 #else
67         __kmp_thread_free(thread,node);
68 #endif
69     }
70 }
71 
72 #define KMP_ACQUIRE_DEPNODE(gtid,n) __kmp_acquire_lock(&(n)->dn.lock,(gtid))
73 #define KMP_RELEASE_DEPNODE(gtid,n) __kmp_release_lock(&(n)->dn.lock,(gtid))
74 
75 static void
76 __kmp_depnode_list_free ( kmp_info_t *thread, kmp_depnode_list *list );
77 
78 static const kmp_int32 kmp_dephash_log2 = 6;
79 static const kmp_int32 kmp_dephash_size = (1 << kmp_dephash_log2);
80 
81 static inline kmp_int32
82 __kmp_dephash_hash ( kmp_intptr_t addr )
83 {
84     //TODO alternate to try: set = (((Addr64)(addrUsefulBits * 9.618)) % m_num_sets );
85     return ((addr >> kmp_dephash_log2) ^ addr) % kmp_dephash_size;
86 }
87 
88 static kmp_dephash_t *
89 __kmp_dephash_create ( kmp_info_t *thread )
90 {
91     kmp_dephash_t *h;
92 
93     kmp_int32 size = kmp_dephash_size * sizeof(kmp_dephash_entry_t) + sizeof(kmp_dephash_t);
94 
95 #if USE_FAST_MEMORY
96     h = (kmp_dephash_t *) __kmp_fast_allocate( thread, size );
97 #else
98     h = (kmp_dephash_t *) __kmp_thread_malloc( thread, size );
99 #endif
100 
101 #ifdef KMP_DEBUG
102     h->nelements = 0;
103 #endif
104     h->buckets = (kmp_dephash_entry **)(h+1);
105 
106     for ( kmp_int32 i = 0; i < kmp_dephash_size; i++ )
107         h->buckets[i] = 0;
108 
109     return h;
110 }
111 
112 static void
113 __kmp_dephash_free ( kmp_info_t *thread, kmp_dephash_t *h )
114 {
115     for ( kmp_int32 i=0; i < kmp_dephash_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);
122 #if USE_FAST_MEMORY
123                 __kmp_fast_free(thread,entry);
124 #else
125                 __kmp_thread_free(thread,entry);
126 #endif
127             }
128         }
129     }
130 #if USE_FAST_MEMORY
131     __kmp_fast_free(thread,h);
132 #else
133     __kmp_thread_free(thread,h);
134 #endif
135 }
136 
137 static kmp_dephash_entry *
138 __kmp_dephash_find ( kmp_info_t *thread, kmp_dephash_t *h, kmp_intptr_t addr )
139 {
140     kmp_int32 bucket = __kmp_dephash_hash(addr);
141 
142     kmp_dephash_entry_t *entry;
143     for ( entry = h->buckets[bucket]; entry; entry = entry->next_in_bucket )
144         if ( entry->addr == addr ) break;
145 
146     if ( entry == NULL ) {
147         // create entry. This is only done by one thread so no locking required
148 #if USE_FAST_MEMORY
149         entry = (kmp_dephash_entry_t *) __kmp_fast_allocate( thread, sizeof(kmp_dephash_entry_t) );
150 #else
151         entry = (kmp_dephash_entry_t *) __kmp_thread_malloc( thread, sizeof(kmp_dephash_entry_t) );
152 #endif
153         entry->addr = addr;
154         entry->last_out = NULL;
155         entry->last_ins = NULL;
156         entry->next_in_bucket = h->buckets[bucket];
157         h->buckets[bucket] = entry;
158 #ifdef KMP_DEBUG
159         h->nelements++;
160         if ( entry->next_in_bucket ) h->nconflicts++;
161 #endif
162     }
163     return entry;
164 }
165 
166 static kmp_depnode_list_t *
167 __kmp_add_node ( kmp_info_t *thread, kmp_depnode_list_t *list, kmp_depnode_t *node )
168 {
169     kmp_depnode_list_t *new_head;
170 
171 #if USE_FAST_MEMORY
172     new_head = (kmp_depnode_list_t *) __kmp_fast_allocate(thread,sizeof(kmp_depnode_list_t));
173 #else
174     new_head = (kmp_depnode_list_t *) __kmp_thread_malloc(thread,sizeof(kmp_depnode_list_t));
175 #endif
176 
177     new_head->node = __kmp_node_ref(node);
178     new_head->next = list;
179 
180     return new_head;
181 }
182 
183 static void
184 __kmp_depnode_list_free ( kmp_info_t *thread, kmp_depnode_list *list )
185 {
186     kmp_depnode_list *next;
187 
188     for ( ; list ; list = next ) {
189         next = list->next;
190 
191         __kmp_node_deref(thread,list->node);
192 #if USE_FAST_MEMORY
193         __kmp_fast_free(thread,list);
194 #else
195         __kmp_thread_free(thread,list);
196 #endif
197     }
198 }
199 
200 static inline void
201 __kmp_track_dependence ( kmp_depnode_t *source, kmp_depnode_t *sink )
202 {
203 #ifdef KMP_SUPPORT_GRAPH_OUTPUT
204     kmp_taskdata_t * task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
205     kmp_taskdata_t * task_sink = KMP_TASK_TO_TASKDATA(sink->dn.task);    // this can be NULL when if(0) ...
206 
207     __kmp_printf("%d(%s) -> %d(%s)\n", source->dn.id, task_source->td_ident->psource, sink->dn.id, task_sink->td_ident->psource);
208 #endif
209 }
210 
211 template< bool filter >
212 static inline kmp_int32
213 __kmp_process_deps ( kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t *hash,
214                      bool dep_barrier,kmp_int32 ndeps, kmp_depend_info_t *dep_list)
215 {
216     KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d processing %d depencies : dep_barrier = %d\n", filter, gtid, ndeps, dep_barrier ) );
217 
218     kmp_info_t *thread = __kmp_threads[ gtid ];
219     kmp_int32 npredecessors=0;
220     for ( kmp_int32 i = 0; i < ndeps ; i++ ) {
221         const kmp_depend_info_t * dep = &dep_list[i];
222 
223         KMP_DEBUG_ASSERT(dep->flags.in);
224 
225         if ( filter && dep->base_addr == 0 ) continue; // skip filtered entries
226 
227         kmp_dephash_entry_t *info = __kmp_dephash_find(thread,hash,dep->base_addr);
228         kmp_depnode_t *last_out = info->last_out;
229 
230         if ( dep->flags.out && info->last_ins ) {
231             for ( kmp_depnode_list_t * p = info->last_ins; p; p = p->next ) {
232                 kmp_depnode_t * indep = p->node;
233                 if ( indep->dn.task ) {
234                     KMP_ACQUIRE_DEPNODE(gtid,indep);
235                     if ( indep->dn.task ) {
236                         __kmp_track_dependence(indep,node);
237                         indep->dn.successors = __kmp_add_node(thread, indep->dn.successors, node);
238                         KA_TRACE(40,("__kmp_process_deps<%d>: T#%d adding dependence from %p to %p",
239                                  filter,gtid, KMP_TASK_TO_TASKDATA(indep->dn.task), KMP_TASK_TO_TASKDATA(node->dn.task)));
240                         npredecessors++;
241                     }
242                     KMP_RELEASE_DEPNODE(gtid,indep);
243                 }
244             }
245 
246             __kmp_depnode_list_free(thread,info->last_ins);
247             info->last_ins = NULL;
248 
249         } else if ( last_out && last_out->dn.task ) {
250             KMP_ACQUIRE_DEPNODE(gtid,last_out);
251             if ( last_out->dn.task ) {
252                 __kmp_track_dependence(last_out,node);
253                 last_out->dn.successors = __kmp_add_node(thread, last_out->dn.successors, node);
254                 KA_TRACE(40,("__kmp_process_deps<%d>: T#%d adding dependence from %p to %p",
255                              filter,gtid, KMP_TASK_TO_TASKDATA(last_out->dn.task), KMP_TASK_TO_TASKDATA(node->dn.task)));
256 
257                 npredecessors++;
258             }
259             KMP_RELEASE_DEPNODE(gtid,last_out);
260         }
261 
262         if ( dep_barrier ) {
263             // if this is a sync point in the serial sequence, then the previous outputs are guaranteed to be completed after
264             // the execution of this task so the previous output nodes can be cleared.
265             __kmp_node_deref(thread,last_out);
266             info->last_out = NULL;
267         } else {
268             if ( dep->flags.out ) {
269                 __kmp_node_deref(thread,last_out);
270                 info->last_out = __kmp_node_ref(node);
271             } else
272                 info->last_ins = __kmp_add_node(thread, info->last_ins, node);
273         }
274 
275     }
276 
277     KA_TRACE(30, ("__kmp_process_deps<%d>: T#%d found %d predecessors\n", filter, gtid, npredecessors ) );
278 
279     return npredecessors;
280 }
281 
282 #define NO_DEP_BARRIER (false)
283 #define DEP_BARRIER (true)
284 
285 // returns true if the task has any outstanding dependence
286 static bool
287 __kmp_check_deps ( kmp_int32 gtid, kmp_depnode_t *node, kmp_task_t *task, kmp_dephash_t *hash, bool dep_barrier,
288                    kmp_int32 ndeps, kmp_depend_info_t *dep_list,
289                    kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list )
290 {
291     int i;
292 
293     kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
294     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 ) );
295 
296     // Filter deps in dep_list
297     // TODO: Different algorithm for large dep_list ( > 10 ? )
298     for ( i = 0; i < ndeps; i ++ ) {
299         if ( dep_list[i].base_addr != 0 )
300             for ( int j = i+1; j < ndeps; j++ )
301                 if ( dep_list[i].base_addr == dep_list[j].base_addr ) {
302                     dep_list[i].flags.in |= dep_list[j].flags.in;
303                     dep_list[i].flags.out |= dep_list[j].flags.out;
304                     dep_list[j].base_addr = 0; // Mark j element as void
305                 }
306     }
307 
308     // doesn't need to be atomic as no other thread is going to be accessing this node just yet
309     // npredecessors is set -1 to ensure that none of the releasing tasks queues this task before we have finished processing all the dependencies
310     node->dn.npredecessors = -1;
311 
312     // used to pack all npredecessors additions into a single atomic operation at the end
313     int npredecessors;
314 
315     npredecessors = __kmp_process_deps<true>(gtid, node, hash, dep_barrier, ndeps, dep_list);
316     npredecessors += __kmp_process_deps<false>(gtid, node, hash, dep_barrier, ndeps_noalias, noalias_dep_list);
317 
318     node->dn.task = task;
319     KMP_MB();
320 
321     // Account for our initial fake value
322     npredecessors++;
323 
324     // 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)
325     npredecessors = KMP_TEST_THEN_ADD32(&node->dn.npredecessors, npredecessors) + npredecessors;
326 
327     KA_TRACE(20, ("__kmp_check_deps: T#%d found %d predecessors for task %p \n", gtid, npredecessors, taskdata ) );
328 
329     // beyond this point the task could be queued (and executed) by a releasing task...
330     return npredecessors > 0 ? true : false;
331 }
332 
333 void
334 __kmp_release_deps ( kmp_int32 gtid, kmp_taskdata_t *task )
335 {
336     kmp_info_t *thread = __kmp_threads[ gtid ];
337     kmp_depnode_t *node = task->td_depnode;
338 
339     if ( task->td_dephash ) {
340         KA_TRACE(40, ("__kmp_realease_deps: T#%d freeing dependencies hash of task %p.\n", gtid, task ) );
341         __kmp_dephash_free(thread,task->td_dephash);
342     }
343 
344     if ( !node ) return;
345 
346     KA_TRACE(20, ("__kmp_realease_deps: T#%d notifying succesors of task %p.\n", gtid, task ) );
347 
348     KMP_ACQUIRE_DEPNODE(gtid,node);
349     node->dn.task = NULL; // mark this task as finished, so no new dependencies are generated
350     KMP_RELEASE_DEPNODE(gtid,node);
351 
352     kmp_depnode_list_t *next;
353     for ( kmp_depnode_list_t *p = node->dn.successors; p; p = next ) {
354         kmp_depnode_t *successor = p->node;
355         kmp_int32 npredecessors = KMP_TEST_THEN_DEC32(&successor->dn.npredecessors) - 1;
356 
357         // successor task can be NULL for wait_depends or because deps are still being processed
358         if ( npredecessors == 0 ) {
359             KMP_MB();
360             if ( successor->dn.task ) {
361                 KA_TRACE(20, ("__kmp_realease_deps: T#%d successor %p of %p scheduled for execution.\n", gtid, successor->dn.task, task ) );
362                 __kmp_omp_task(gtid,successor->dn.task,false);
363             }
364         }
365 
366         next = p->next;
367         __kmp_node_deref(thread,p->node);
368 #if USE_FAST_MEMORY
369         __kmp_fast_free(thread,p);
370 #else
371         __kmp_thread_free(thread,p);
372 #endif
373     }
374 
375     __kmp_node_deref(thread,node);
376 
377     KA_TRACE(20, ("__kmp_realease_deps: T#%d all successors of %p notified of completation\n", gtid, task ) );
378 }
379 
380 /*!
381 @ingroup TASKING
382 @param loc_ref location of the original task directive
383 @param gtid Global Thread ID of encountering thread
384 @param new_task task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
385 @param ndeps Number of depend items with possible aliasing
386 @param dep_list List of depend items with possible aliasing
387 @param ndeps_noalias Number of depend items with no aliasing
388 @param noalias_dep_list List of depend items with no aliasing
389 
390 @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
391 
392 Schedule a non-thread-switchable task with dependences for execution
393 */
394 kmp_int32
395 __kmpc_omp_task_with_deps( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * new_task,
396                             kmp_int32 ndeps, kmp_depend_info_t *dep_list,
397                             kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list )
398 {
399 
400     kmp_taskdata_t * new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
401     KA_TRACE(10, ("__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n",
402                   gtid, loc_ref, new_taskdata ) );
403 
404     kmp_info_t *thread = __kmp_threads[ gtid ];
405     kmp_taskdata_t * current_task = thread->th.th_current_task;
406 
407     bool serial = current_task->td_flags.team_serial || current_task->td_flags.tasking_ser || current_task->td_flags.final;
408 
409     if ( !serial && ( ndeps > 0 || ndeps_noalias > 0 )) {
410         /* if no dependencies have been tracked yet, create the dependence hash */
411         if ( current_task->td_dephash == NULL )
412             current_task->td_dephash = __kmp_dephash_create(thread);
413 
414 #if USE_FAST_MEMORY
415         kmp_depnode_t *node = (kmp_depnode_t *) __kmp_fast_allocate(thread,sizeof(kmp_depnode_t));
416 #else
417         kmp_depnode_t *node = (kmp_depnode_t *) __kmp_thread_malloc(thread,sizeof(kmp_depnode_t));
418 #endif
419 
420         __kmp_init_node(node);
421         new_taskdata->td_depnode = node;
422 
423         if ( __kmp_check_deps( gtid, node, new_task, current_task->td_dephash, NO_DEP_BARRIER,
424                                ndeps, dep_list, ndeps_noalias,noalias_dep_list ) ) {
425             KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had blocking dependencies: "
426                   "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n", gtid, loc_ref,
427                   new_taskdata ) );
428             return TASK_CURRENT_NOT_QUEUED;
429         }
430     }
431 
432     KA_TRACE(10, ("__kmpc_omp_task_with_deps(exit): T#%d task had no blocking dependencies : "
433                   "loc=%p task=%p, transferring to __kmpc_omp_task\n", gtid, loc_ref,
434                   new_taskdata ) );
435 
436     return __kmpc_omp_task(loc_ref,gtid,new_task);
437 }
438 
439 /*!
440 @ingroup TASKING
441 @param loc_ref location of the original task directive
442 @param gtid Global Thread ID of encountering thread
443 @param ndeps Number of depend items with possible aliasing
444 @param dep_list List of depend items with possible aliasing
445 @param ndeps_noalias Number of depend items with no aliasing
446 @param noalias_dep_list List of depend items with no aliasing
447 
448 Blocks the current task until all specifies dependencies have been fulfilled.
449 */
450 void
451 __kmpc_omp_wait_deps ( ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
452                        kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list )
453 {
454     KA_TRACE(10, ("__kmpc_omp_wait_deps(enter): T#%d loc=%p\n", gtid, loc_ref) );
455 
456     if ( ndeps == 0 && ndeps_noalias == 0 ) {
457         KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no dependencies to wait upon : loc=%p\n", gtid, loc_ref) );
458         return;
459     }
460 
461     kmp_info_t *thread = __kmp_threads[ gtid ];
462     kmp_taskdata_t * current_task = thread->th.th_current_task;
463 
464     // We can return immediately as:
465     //   - dependences are not computed in serial teams
466     //   - if the dephash is not yet created it means we have nothing to wait for
467     if ( current_task->td_flags.team_serial || current_task->td_flags.tasking_ser || current_task->td_flags.final || current_task->td_dephash == NULL ) {
468         KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking dependencies : loc=%p\n", gtid, loc_ref) );
469         return;
470     }
471 
472     kmp_depnode_t node;
473     __kmp_init_node(&node);
474 
475     if (!__kmp_check_deps( gtid, &node, NULL, current_task->td_dephash, DEP_BARRIER,
476                            ndeps, dep_list, ndeps_noalias, noalias_dep_list )) {
477         KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d has no blocking dependencies : loc=%p\n", gtid, loc_ref) );
478         return;
479     }
480 
481     int thread_finished = FALSE;
482     kmp_flag_32 flag((volatile kmp_uint32 *)&(node.dn.npredecessors), 0U);
483     while ( node.dn.npredecessors > 0 ) {
484         flag.execute_tasks(thread, gtid, FALSE, &thread_finished,
485 #if USE_ITT_BUILD
486                            NULL,
487 #endif
488                            __kmp_task_stealing_constraint );
489     }
490 
491     KA_TRACE(10, ("__kmpc_omp_wait_deps(exit): T#%d finished waiting : loc=%p\n", gtid, loc_ref) );
492 }
493 
494 #endif /* OMP_40_ENABLED */
495 
496