blob: 028ffcbd5b38588435740538a14af350063d13ff [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * kmp_tasking.c -- OpenMP 3.0 tasking support.
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#include "kmp.h"
17#include "kmp_i18n.h"
18#include "kmp_itt.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000019#include "kmp_wait_release.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000020
Andrey Churbanove5f44922015-04-29 16:22:07 +000021#if OMPT_SUPPORT
22#include "ompt-specific.h"
23#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000024
Jim Cownie5e8470a2013-09-27 10:38:44 +000025
26/* ------------------------------------------------------------------------ */
27/* ------------------------------------------------------------------------ */
28
29
30/* forward declaration */
31static void __kmp_enable_tasking( kmp_task_team_t *task_team, kmp_info_t *this_thr );
32static void __kmp_alloc_task_deque( kmp_info_t *thread, kmp_thread_data_t *thread_data );
33static int __kmp_realloc_task_threads_data( kmp_info_t *thread, kmp_task_team_t *task_team );
34
Andrey Churbanov535b6fa2015-05-07 17:41:51 +000035#ifdef OMP_41_ENABLED
36static void __kmp_bottom_half_finish_proxy( kmp_int32 gtid, kmp_task_t * ptask );
37#endif
38
Jim Cownie4cc4bb42014-10-07 16:25:50 +000039static inline void __kmp_null_resume_wrapper(int gtid, volatile void *flag) {
40 switch (((kmp_flag_64 *)flag)->get_type()) {
41 case flag32: __kmp_resume_32(gtid, NULL); break;
42 case flag64: __kmp_resume_64(gtid, NULL); break;
43 case flag_oncore: __kmp_resume_oncore(gtid, NULL); break;
44 }
Jim Cownie5e8470a2013-09-27 10:38:44 +000045}
46
47#ifdef BUILD_TIED_TASK_STACK
48
49//---------------------------------------------------------------------------
50// __kmp_trace_task_stack: print the tied tasks from the task stack in order
51// from top do bottom
52//
53// gtid: global thread identifier for thread containing stack
54// thread_data: thread data for task team thread containing stack
55// threshold: value above which the trace statement triggers
56// location: string identifying call site of this function (for trace)
57
58static void
59__kmp_trace_task_stack( kmp_int32 gtid, kmp_thread_data_t *thread_data, int threshold, char *location )
60{
61 kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks;
62 kmp_taskdata_t **stack_top = task_stack -> ts_top;
63 kmp_int32 entries = task_stack -> ts_entries;
64 kmp_taskdata_t *tied_task;
65
66 KA_TRACE(threshold, ("__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
67 "first_block = %p, stack_top = %p \n",
68 location, gtid, entries, task_stack->ts_first_block, stack_top ) );
69
70 KMP_DEBUG_ASSERT( stack_top != NULL );
71 KMP_DEBUG_ASSERT( entries > 0 );
72
73 while ( entries != 0 )
74 {
75 KMP_DEBUG_ASSERT( stack_top != & task_stack->ts_first_block.sb_block[0] );
76 // fix up ts_top if we need to pop from previous block
77 if ( entries & TASK_STACK_INDEX_MASK == 0 )
78 {
79 kmp_stack_block_t *stack_block = (kmp_stack_block_t *) (stack_top) ;
80
81 stack_block = stack_block -> sb_prev;
82 stack_top = & stack_block -> sb_block[TASK_STACK_BLOCK_SIZE];
83 }
84
85 // finish bookkeeping
86 stack_top--;
87 entries--;
88
89 tied_task = * stack_top;
90
91 KMP_DEBUG_ASSERT( tied_task != NULL );
92 KMP_DEBUG_ASSERT( tied_task -> td_flags.tasktype == TASK_TIED );
93
94 KA_TRACE(threshold, ("__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
95 "stack_top=%p, tied_task=%p\n",
96 location, gtid, entries, stack_top, tied_task ) );
97 }
98 KMP_DEBUG_ASSERT( stack_top == & task_stack->ts_first_block.sb_block[0] );
99
100 KA_TRACE(threshold, ("__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
101 location, gtid ) );
102}
103
104//---------------------------------------------------------------------------
105// __kmp_init_task_stack: initialize the task stack for the first time
106// after a thread_data structure is created.
107// It should not be necessary to do this again (assuming the stack works).
108//
109// gtid: global thread identifier of calling thread
110// thread_data: thread data for task team thread containing stack
111
112static void
113__kmp_init_task_stack( kmp_int32 gtid, kmp_thread_data_t *thread_data )
114{
115 kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks;
116 kmp_stack_block_t *first_block;
117
118 // set up the first block of the stack
119 first_block = & task_stack -> ts_first_block;
120 task_stack -> ts_top = (kmp_taskdata_t **) first_block;
121 memset( (void *) first_block, '\0', TASK_STACK_BLOCK_SIZE * sizeof(kmp_taskdata_t *));
122
123 // initialize the stack to be empty
124 task_stack -> ts_entries = TASK_STACK_EMPTY;
125 first_block -> sb_next = NULL;
126 first_block -> sb_prev = NULL;
127}
128
129
130//---------------------------------------------------------------------------
131// __kmp_free_task_stack: free the task stack when thread_data is destroyed.
132//
133// gtid: global thread identifier for calling thread
134// thread_data: thread info for thread containing stack
135
136static void
137__kmp_free_task_stack( kmp_int32 gtid, kmp_thread_data_t *thread_data )
138{
139 kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks;
140 kmp_stack_block_t *stack_block = & task_stack -> ts_first_block;
141
142 KMP_DEBUG_ASSERT( task_stack -> ts_entries == TASK_STACK_EMPTY );
143 // free from the second block of the stack
144 while ( stack_block != NULL ) {
145 kmp_stack_block_t *next_block = (stack_block) ? stack_block -> sb_next : NULL;
146
147 stack_block -> sb_next = NULL;
148 stack_block -> sb_prev = NULL;
149 if (stack_block != & task_stack -> ts_first_block) {
150 __kmp_thread_free( thread, stack_block ); // free the block, if not the first
151 }
152 stack_block = next_block;
153 }
154 // initialize the stack to be empty
155 task_stack -> ts_entries = 0;
156 task_stack -> ts_top = NULL;
157}
158
159
160//---------------------------------------------------------------------------
161// __kmp_push_task_stack: Push the tied task onto the task stack.
162// Grow the stack if necessary by allocating another block.
163//
164// gtid: global thread identifier for calling thread
165// thread: thread info for thread containing stack
166// tied_task: the task to push on the stack
167
168static void
169__kmp_push_task_stack( kmp_int32 gtid, kmp_info_t *thread, kmp_taskdata_t * tied_task )
170{
171 // GEH - need to consider what to do if tt_threads_data not allocated yet
172 kmp_thread_data_t *thread_data = & thread -> th.th_task_team ->
173 tt.tt_threads_data[ __kmp_tid_from_gtid( gtid ) ];
174 kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks ;
175
176 if ( tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser ) {
177 return; // Don't push anything on stack if team or team tasks are serialized
178 }
179
180 KMP_DEBUG_ASSERT( tied_task -> td_flags.tasktype == TASK_TIED );
181 KMP_DEBUG_ASSERT( task_stack -> ts_top != NULL );
182
183 KA_TRACE(20, ("__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
184 gtid, thread, tied_task ) );
185 // Store entry
186 * (task_stack -> ts_top) = tied_task;
187
188 // Do bookkeeping for next push
189 task_stack -> ts_top++;
190 task_stack -> ts_entries++;
191
192 if ( task_stack -> ts_entries & TASK_STACK_INDEX_MASK == 0 )
193 {
194 // Find beginning of this task block
195 kmp_stack_block_t *stack_block =
196 (kmp_stack_block_t *) (task_stack -> ts_top - TASK_STACK_BLOCK_SIZE);
197
198 // Check if we already have a block
199 if ( stack_block -> sb_next != NULL )
200 { // reset ts_top to beginning of next block
201 task_stack -> ts_top = & stack_block -> sb_next -> sb_block[0];
202 }
203 else
204 { // Alloc new block and link it up
205 kmp_stack_block_t *new_block = (kmp_stack_block_t *)
206 __kmp_thread_calloc(thread, sizeof(kmp_stack_block_t));
207
208 task_stack -> ts_top = & new_block -> sb_block[0];
209 stack_block -> sb_next = new_block;
210 new_block -> sb_prev = stack_block;
211 new_block -> sb_next = NULL;
212
213 KA_TRACE(30, ("__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
214 gtid, tied_task, new_block ) );
215 }
216 }
217 KA_TRACE(20, ("__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid, tied_task ) );
218}
219
220//---------------------------------------------------------------------------
221// __kmp_pop_task_stack: Pop the tied task from the task stack. Don't return
222// the task, just check to make sure it matches the ending task passed in.
223//
224// gtid: global thread identifier for the calling thread
225// thread: thread info structure containing stack
226// tied_task: the task popped off the stack
227// ending_task: the task that is ending (should match popped task)
228
229static void
230__kmp_pop_task_stack( kmp_int32 gtid, kmp_info_t *thread, kmp_taskdata_t *ending_task )
231{
232 // GEH - need to consider what to do if tt_threads_data not allocated yet
233 kmp_thread_data_t *thread_data = & thread -> th.th_task_team -> tt_threads_data[ __kmp_tid_from_gtid( gtid ) ];
234 kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks ;
235 kmp_taskdata_t *tied_task;
236
237 if ( ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser ) {
238 return; // Don't pop anything from stack if team or team tasks are serialized
239 }
240
241 KMP_DEBUG_ASSERT( task_stack -> ts_top != NULL );
242 KMP_DEBUG_ASSERT( task_stack -> ts_entries > 0 );
243
244 KA_TRACE(20, ("__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid, thread ) );
245
246 // fix up ts_top if we need to pop from previous block
247 if ( task_stack -> ts_entries & TASK_STACK_INDEX_MASK == 0 )
248 {
249 kmp_stack_block_t *stack_block =
250 (kmp_stack_block_t *) (task_stack -> ts_top) ;
251
252 stack_block = stack_block -> sb_prev;
253 task_stack -> ts_top = & stack_block -> sb_block[TASK_STACK_BLOCK_SIZE];
254 }
255
256 // finish bookkeeping
257 task_stack -> ts_top--;
258 task_stack -> ts_entries--;
259
260 tied_task = * (task_stack -> ts_top );
261
262 KMP_DEBUG_ASSERT( tied_task != NULL );
263 KMP_DEBUG_ASSERT( tied_task -> td_flags.tasktype == TASK_TIED );
264 KMP_DEBUG_ASSERT( tied_task == ending_task ); // If we built the stack correctly
265
266 KA_TRACE(20, ("__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid, tied_task ) );
267 return;
268}
269#endif /* BUILD_TIED_TASK_STACK */
270
271//---------------------------------------------------
272// __kmp_push_task: Add a task to the thread's deque
273
274static kmp_int32
275__kmp_push_task(kmp_int32 gtid, kmp_task_t * task )
276{
277 kmp_info_t * thread = __kmp_threads[ gtid ];
278 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
279 kmp_task_team_t * task_team = thread->th.th_task_team;
280 kmp_int32 tid = __kmp_tid_from_gtid( gtid );
281 kmp_thread_data_t * thread_data;
282
283 KA_TRACE(20, ("__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata ) );
284
285 // The first check avoids building task_team thread data if serialized
286 if ( taskdata->td_flags.task_serial ) {
287 KA_TRACE(20, ( "__kmp_push_task: T#%d team serialized; returning TASK_NOT_PUSHED for task %p\n",
288 gtid, taskdata ) );
289 return TASK_NOT_PUSHED;
290 }
291
292 // Now that serialized tasks have returned, we can assume that we are not in immediate exec mode
293 KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
Andrey Churbanov6d224db2015-02-10 18:37:43 +0000294 if ( ! KMP_TASKING_ENABLED(task_team) ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000295 __kmp_enable_tasking( task_team, thread );
296 }
297 KMP_DEBUG_ASSERT( TCR_4(task_team -> tt.tt_found_tasks) == TRUE );
298 KMP_DEBUG_ASSERT( TCR_PTR(task_team -> tt.tt_threads_data) != NULL );
299
300 // Find tasking deque specific to encountering thread
301 thread_data = & task_team -> tt.tt_threads_data[ tid ];
302
303 // No lock needed since only owner can allocate
304 if (thread_data -> td.td_deque == NULL ) {
305 __kmp_alloc_task_deque( thread, thread_data );
306 }
307
308 // Check if deque is full
309 if ( TCR_4(thread_data -> td.td_deque_ntasks) >= TASK_DEQUE_SIZE )
310 {
311 KA_TRACE(20, ( "__kmp_push_task: T#%d deque is full; returning TASK_NOT_PUSHED for task %p\n",
312 gtid, taskdata ) );
313 return TASK_NOT_PUSHED;
314 }
315
316 // Lock the deque for the task push operation
317 __kmp_acquire_bootstrap_lock( & thread_data -> td.td_deque_lock );
318
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000319#if OMP_41_ENABLED
320 // Need to recheck as we can get a proxy task from a thread outside of OpenMP
321 if ( TCR_4(thread_data -> td.td_deque_ntasks) >= TASK_DEQUE_SIZE )
322 {
323 __kmp_release_bootstrap_lock( & thread_data -> td.td_deque_lock );
324 KA_TRACE(20, ( "__kmp_push_task: T#%d deque is full on 2nd check; returning TASK_NOT_PUSHED for task %p\n",
325 gtid, taskdata ) );
326 return TASK_NOT_PUSHED;
327 }
328#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000329 // Must have room since no thread can add tasks but calling thread
330 KMP_DEBUG_ASSERT( TCR_4(thread_data -> td.td_deque_ntasks) < TASK_DEQUE_SIZE );
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000331#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000332
333 thread_data -> td.td_deque[ thread_data -> td.td_deque_tail ] = taskdata; // Push taskdata
334 // Wrap index.
335 thread_data -> td.td_deque_tail = ( thread_data -> td.td_deque_tail + 1 ) & TASK_DEQUE_MASK;
336 TCW_4(thread_data -> td.td_deque_ntasks, TCR_4(thread_data -> td.td_deque_ntasks) + 1); // Adjust task count
337
338 __kmp_release_bootstrap_lock( & thread_data -> td.td_deque_lock );
339
340 KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
341 "task=%p ntasks=%d head=%u tail=%u\n",
342 gtid, taskdata, thread_data->td.td_deque_ntasks,
343 thread_data->td.td_deque_tail, thread_data->td.td_deque_head) );
344
345 return TASK_SUCCESSFULLY_PUSHED;
346}
347
348
349//-----------------------------------------------------------------------------------------
350// __kmp_pop_current_task_from_thread: set up current task from called thread when team ends
351// this_thr: thread structure to set current_task in.
352
353void
354__kmp_pop_current_task_from_thread( kmp_info_t *this_thr )
355{
356 KF_TRACE( 10, ("__kmp_pop_current_task_from_thread(enter): T#%d this_thread=%p, curtask=%p, "
357 "curtask_parent=%p\n",
358 0, this_thr, this_thr -> th.th_current_task,
359 this_thr -> th.th_current_task -> td_parent ) );
360
361 this_thr -> th.th_current_task = this_thr -> th.th_current_task -> td_parent;
362
363 KF_TRACE( 10, ("__kmp_pop_current_task_from_thread(exit): T#%d this_thread=%p, curtask=%p, "
364 "curtask_parent=%p\n",
365 0, this_thr, this_thr -> th.th_current_task,
366 this_thr -> th.th_current_task -> td_parent ) );
367}
368
369
370//---------------------------------------------------------------------------------------
371// __kmp_push_current_task_to_thread: set up current task in called thread for a new team
372// this_thr: thread structure to set up
373// team: team for implicit task data
374// tid: thread within team to set up
375
376void
377__kmp_push_current_task_to_thread( kmp_info_t *this_thr, kmp_team_t *team, int tid )
378{
379 // current task of the thread is a parent of the new just created implicit tasks of new team
380 KF_TRACE( 10, ( "__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p curtask=%p "
381 "parent_task=%p\n",
382 tid, this_thr, this_thr->th.th_current_task,
383 team->t.t_implicit_task_taskdata[tid].td_parent ) );
384
385 KMP_DEBUG_ASSERT (this_thr != NULL);
386
387 if( tid == 0 ) {
388 if( this_thr->th.th_current_task != & team -> t.t_implicit_task_taskdata[ 0 ] ) {
389 team -> t.t_implicit_task_taskdata[ 0 ].td_parent = this_thr->th.th_current_task;
390 this_thr->th.th_current_task = & team -> t.t_implicit_task_taskdata[ 0 ];
391 }
392 } else {
393 team -> t.t_implicit_task_taskdata[ tid ].td_parent = team -> t.t_implicit_task_taskdata[ 0 ].td_parent;
394 this_thr->th.th_current_task = & team -> t.t_implicit_task_taskdata[ tid ];
395 }
396
397 KF_TRACE( 10, ( "__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p curtask=%p "
398 "parent_task=%p\n",
399 tid, this_thr, this_thr->th.th_current_task,
400 team->t.t_implicit_task_taskdata[tid].td_parent ) );
401}
402
403
404//----------------------------------------------------------------------
405// __kmp_task_start: bookkeeping for a task starting execution
406// GTID: global thread id of calling thread
407// task: task starting execution
408// current_task: task suspending
409
410static void
411__kmp_task_start( kmp_int32 gtid, kmp_task_t * task, kmp_taskdata_t * current_task )
412{
413 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
414 kmp_info_t * thread = __kmp_threads[ gtid ];
415
416 KA_TRACE(10, ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
417 gtid, taskdata, current_task) );
418
419 KMP_DEBUG_ASSERT( taskdata -> td_flags.tasktype == TASK_EXPLICIT );
420
421 // mark currently executing task as suspended
422 // TODO: GEH - make sure root team implicit task is initialized properly.
423 // KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 );
424 current_task -> td_flags.executing = 0;
425
426 // Add task to stack if tied
427#ifdef BUILD_TIED_TASK_STACK
428 if ( taskdata -> td_flags.tiedness == TASK_TIED )
429 {
430 __kmp_push_task_stack( gtid, thread, taskdata );
431 }
432#endif /* BUILD_TIED_TASK_STACK */
433
434 // mark starting task as executing and as current task
435 thread -> th.th_current_task = taskdata;
436
437 KMP_DEBUG_ASSERT( taskdata -> td_flags.started == 0 );
438 KMP_DEBUG_ASSERT( taskdata -> td_flags.executing == 0 );
439 taskdata -> td_flags.started = 1;
440 taskdata -> td_flags.executing = 1;
441 KMP_DEBUG_ASSERT( taskdata -> td_flags.complete == 0 );
442 KMP_DEBUG_ASSERT( taskdata -> td_flags.freed == 0 );
443
444 // GEH TODO: shouldn't we pass some sort of location identifier here?
445 // APT: yes, we will pass location here.
446 // need to store current thread state (in a thread or taskdata structure)
447 // before setting work_state, otherwise wrong state is set after end of task
448
449 KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n",
450 gtid, taskdata ) );
451
Andrey Churbanovd7d088f2015-04-29 16:42:24 +0000452#if OMPT_SUPPORT
453 if ((ompt_status == ompt_status_track_callback) &&
454 ompt_callbacks.ompt_callback(ompt_event_task_begin)) {
455 kmp_taskdata_t *parent = taskdata->td_parent;
456 ompt_callbacks.ompt_callback(ompt_event_task_begin)(
457 parent ? parent->ompt_task_info.task_id : ompt_task_id_none,
458 parent ? &(parent->ompt_task_info.frame) : NULL,
459 taskdata->ompt_task_info.task_id,
460 taskdata->ompt_task_info.function);
461 }
462#endif
463
Jim Cownie5e8470a2013-09-27 10:38:44 +0000464 return;
465}
466
467
468//----------------------------------------------------------------------
469// __kmpc_omp_task_begin_if0: report that a given serialized task has started execution
470// loc_ref: source location information; points to beginning of task block.
471// gtid: global thread number.
472// task: task thunk for the started task.
473
474void
475__kmpc_omp_task_begin_if0( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * task )
476{
477 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
478 kmp_taskdata_t * current_task = __kmp_threads[ gtid ] -> th.th_current_task;
479
480 KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p current_task=%p\n",
481 gtid, loc_ref, taskdata, current_task ) );
482
483 taskdata -> td_flags.task_serial = 1; // Execute this task immediately, not deferred.
484 __kmp_task_start( gtid, task, current_task );
485
486 KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n",
487 gtid, loc_ref, taskdata ) );
488
489 return;
490}
491
492#ifdef TASK_UNUSED
493//----------------------------------------------------------------------
494// __kmpc_omp_task_begin: report that a given task has started execution
495// NEVER GENERATED BY COMPILER, DEPRECATED!!!
496
497void
498__kmpc_omp_task_begin( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * task )
499{
500 kmp_taskdata_t * current_task = __kmp_threads[ gtid ] -> th.th_current_task;
501
502 KA_TRACE(10, ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
503 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task ) );
504
505 __kmp_task_start( gtid, task, current_task );
506
507 KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n",
508 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
509
510 return;
511}
512#endif // TASK_UNUSED
513
514
515//-------------------------------------------------------------------------------------
516// __kmp_free_task: free the current task space and the space for shareds
517// gtid: Global thread ID of calling thread
518// taskdata: task to free
519// thread: thread data structure of caller
520
521static void
522__kmp_free_task( kmp_int32 gtid, kmp_taskdata_t * taskdata, kmp_info_t * thread )
523{
524 KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n",
525 gtid, taskdata) );
526
527 // Check to make sure all flags and counters have the correct values
528 KMP_DEBUG_ASSERT( taskdata->td_flags.tasktype == TASK_EXPLICIT );
529 KMP_DEBUG_ASSERT( taskdata->td_flags.executing == 0 );
530 KMP_DEBUG_ASSERT( taskdata->td_flags.complete == 1 );
531 KMP_DEBUG_ASSERT( taskdata->td_flags.freed == 0 );
532 KMP_DEBUG_ASSERT( TCR_4(taskdata->td_allocated_child_tasks) == 0 || taskdata->td_flags.task_serial == 1);
533 KMP_DEBUG_ASSERT( TCR_4(taskdata->td_incomplete_child_tasks) == 0 );
534
535 taskdata->td_flags.freed = 1;
536 // deallocate the taskdata and shared variable blocks associated with this task
537 #if USE_FAST_MEMORY
538 __kmp_fast_free( thread, taskdata );
539 #else /* ! USE_FAST_MEMORY */
540 __kmp_thread_free( thread, taskdata );
541 #endif
542
543 KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n",
544 gtid, taskdata) );
545}
546
547//-------------------------------------------------------------------------------------
548// __kmp_free_task_and_ancestors: free the current task and ancestors without children
549//
550// gtid: Global thread ID of calling thread
551// taskdata: task to free
552// thread: thread data structure of caller
553
554static void
555__kmp_free_task_and_ancestors( kmp_int32 gtid, kmp_taskdata_t * taskdata, kmp_info_t * thread )
556{
557 kmp_int32 children = 0;
558 kmp_int32 team_or_tasking_serialized = taskdata -> td_flags.team_serial || taskdata -> td_flags.tasking_ser;
559
560 KMP_DEBUG_ASSERT( taskdata -> td_flags.tasktype == TASK_EXPLICIT );
561
562 if ( !team_or_tasking_serialized ) {
563 children = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata -> td_allocated_child_tasks) ) - 1;
564 KMP_DEBUG_ASSERT( children >= 0 );
565 }
566
567 // Now, go up the ancestor tree to see if any ancestors can now be freed.
568 while ( children == 0 )
569 {
570 kmp_taskdata_t * parent_taskdata = taskdata -> td_parent;
571
572 KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
573 "and freeing itself\n", gtid, taskdata) );
574
575 // --- Deallocate my ancestor task ---
576 __kmp_free_task( gtid, taskdata, thread );
577
578 taskdata = parent_taskdata;
579
580 // Stop checking ancestors at implicit task or if tasking serialized
581 // instead of walking up ancestor tree to avoid premature deallocation of ancestors.
582 if ( team_or_tasking_serialized || taskdata -> td_flags.tasktype == TASK_IMPLICIT )
583 return;
584
585 if ( !team_or_tasking_serialized ) {
586 // Predecrement simulated by "- 1" calculation
587 children = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata -> td_allocated_child_tasks) ) - 1;
588 KMP_DEBUG_ASSERT( children >= 0 );
589 }
590 }
591
592 KA_TRACE(20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
593 "not freeing it yet\n", gtid, taskdata, children) );
594}
595
596//---------------------------------------------------------------------
597// __kmp_task_finish: bookkeeping to do when a task finishes execution
598// gtid: global thread ID for calling thread
599// task: task to be finished
600// resumed_task: task to be resumed. (may be NULL if task is serialized)
601
602static void
603__kmp_task_finish( kmp_int32 gtid, kmp_task_t *task, kmp_taskdata_t *resumed_task )
604{
605 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
606 kmp_info_t * thread = __kmp_threads[ gtid ];
607 kmp_int32 children = 0;
608
Andrey Churbanovd7d088f2015-04-29 16:42:24 +0000609#if OMPT_SUPPORT
610 if ((ompt_status == ompt_status_track_callback) &&
611 ompt_callbacks.ompt_callback(ompt_event_task_end)) {
612 kmp_taskdata_t *parent = taskdata->td_parent;
613 ompt_callbacks.ompt_callback(ompt_event_task_end)(
614 taskdata->ompt_task_info.task_id);
615 }
616#endif
617
Jim Cownie5e8470a2013-09-27 10:38:44 +0000618 KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming task %p\n",
619 gtid, taskdata, resumed_task) );
620
621 KMP_DEBUG_ASSERT( taskdata -> td_flags.tasktype == TASK_EXPLICIT );
622
623 // Pop task from stack if tied
624#ifdef BUILD_TIED_TASK_STACK
625 if ( taskdata -> td_flags.tiedness == TASK_TIED )
626 {
627 __kmp_pop_task_stack( gtid, thread, taskdata );
628 }
629#endif /* BUILD_TIED_TASK_STACK */
630
Jim Cownie5e8470a2013-09-27 10:38:44 +0000631 KMP_DEBUG_ASSERT( taskdata -> td_flags.complete == 0 );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000632 taskdata -> td_flags.complete = 1; // mark the task as completed
633 KMP_DEBUG_ASSERT( taskdata -> td_flags.started == 1 );
634 KMP_DEBUG_ASSERT( taskdata -> td_flags.freed == 0 );
635
636 // Only need to keep track of count if team parallel and tasking not serialized
637 if ( !( taskdata -> td_flags.team_serial || taskdata -> td_flags.tasking_ser ) ) {
638 // Predecrement simulated by "- 1" calculation
639 children = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata -> td_parent -> td_incomplete_child_tasks) ) - 1;
640 KMP_DEBUG_ASSERT( children >= 0 );
641#if OMP_40_ENABLED
642 if ( taskdata->td_taskgroup )
643 KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata->td_taskgroup->count) );
Jim Cownie181b4bb2013-12-23 17:28:57 +0000644 __kmp_release_deps(gtid,taskdata);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000645#endif
646 }
647
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000648 // td_flags.executing must be marked as 0 after __kmp_release_deps has been called
649 // Othertwise, if a task is executed immediately from the release_deps code
650 // the flag will be reset to 1 again by this same function
651 KMP_DEBUG_ASSERT( taskdata -> td_flags.executing == 1 );
652 taskdata -> td_flags.executing = 0; // suspend the finishing task
653
Jim Cownie5e8470a2013-09-27 10:38:44 +0000654 KA_TRACE(20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
655 gtid, taskdata, children) );
656
Jim Cownie181b4bb2013-12-23 17:28:57 +0000657#if OMP_40_ENABLED
658 /* If the tasks' destructor thunk flag has been set, we need to invoke the
659 destructor thunk that has been generated by the compiler.
660 The code is placed here, since at this point other tasks might have been released
661 hence overlapping the destructor invokations with some other work in the
662 released tasks. The OpenMP spec is not specific on when the destructors are
663 invoked, so we should be free to choose.
664 */
665 if (taskdata->td_flags.destructors_thunk) {
666 kmp_routine_entry_t destr_thunk = task->destructors;
667 KMP_ASSERT(destr_thunk);
668 destr_thunk(gtid, task);
669 }
670#endif // OMP_40_ENABLED
671
Jim Cownie5e8470a2013-09-27 10:38:44 +0000672 // bookkeeping for resuming task:
673 // GEH - note tasking_ser => task_serial
674 KMP_DEBUG_ASSERT( (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
675 taskdata->td_flags.task_serial);
676 if ( taskdata->td_flags.task_serial )
677 {
678 if (resumed_task == NULL) {
679 resumed_task = taskdata->td_parent; // In a serialized task, the resumed task is the parent
680 }
681 else {
682 // verify resumed task passed in points to parent
683 KMP_DEBUG_ASSERT( resumed_task == taskdata->td_parent );
684 }
685 }
686 else {
687 KMP_DEBUG_ASSERT( resumed_task != NULL ); // verify that resumed task is passed as arguemnt
688 }
689
690 // Free this task and then ancestor tasks if they have no children.
691 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
692
Andrey Churbanovd7d088f2015-04-29 16:42:24 +0000693 // FIXME johnmc: I this statement should be before the last one so if an
694 // asynchronous inquiry peers into the runtime system it doesn't see the freed
695 // task as the current task
Jim Cownie5e8470a2013-09-27 10:38:44 +0000696 __kmp_threads[ gtid ] -> th.th_current_task = resumed_task; // restore current_task
697
698 // TODO: GEH - make sure root team implicit task is initialized properly.
699 // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 );
700 resumed_task->td_flags.executing = 1; // resume previous task
701
702 KA_TRACE(10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
703 gtid, taskdata, resumed_task) );
704
705 return;
706}
707
708//---------------------------------------------------------------------
709// __kmpc_omp_task_complete_if0: report that a task has completed execution
710// loc_ref: source location information; points to end of task block.
711// gtid: global thread number.
712// task: task thunk for the completed task.
713
714void
715__kmpc_omp_task_complete_if0( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task )
716{
717 KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
718 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
719
720 __kmp_task_finish( gtid, task, NULL ); // this routine will provide task to resume
721
722 KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
723 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
724
725 return;
726}
727
728#ifdef TASK_UNUSED
729//---------------------------------------------------------------------
730// __kmpc_omp_task_complete: report that a task has completed execution
731// NEVER GENERATED BY COMPILER, DEPRECATED!!!
732
733void
734__kmpc_omp_task_complete( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task )
735{
736 KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n",
737 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
738
739 __kmp_task_finish( gtid, task, NULL ); // Not sure how to find task to resume
740
741 KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n",
742 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
743 return;
744}
745#endif // TASK_UNUSED
746
747
Andrey Churbanove5f44922015-04-29 16:22:07 +0000748#if OMPT_SUPPORT
749//----------------------------------------------------------------------------------------------------
750// __kmp_task_init_ompt:
751// Initialize OMPT fields maintained by a task. Since the serial task is initialized before
752// ompt_initialize is called, at the point the serial task is initialized we don't know whether
753// OMPT will be used or not when the serial task is initialized. This function provides the support
754// needed to initialize OMPT for the serial task after the fact.
755
756void
757__kmp_task_init_ompt( kmp_taskdata_t * task, int tid )
758{
759 task->ompt_task_info.task_id = __ompt_task_id_new(tid);
760 task->ompt_task_info.function = NULL;
761 task->ompt_task_info.frame = (ompt_frame_t) {
762 .exit_runtime_frame = NULL,
763 .reenter_runtime_frame = NULL
764 };
765}
766#endif
767
768
Jim Cownie5e8470a2013-09-27 10:38:44 +0000769//----------------------------------------------------------------------------------------------------
770// __kmp_init_implicit_task: Initialize the appropriate fields in the implicit task for a given thread
771//
772// loc_ref: reference to source location of parallel region
773// this_thr: thread data structure corresponding to implicit task
774// team: team for this_thr
775// tid: thread id of given thread within team
776// set_curr_task: TRUE if need to push current task to thread
777// NOTE: Routine does not set up the implicit task ICVS. This is assumed to have already been done elsewhere.
778// TODO: Get better loc_ref. Value passed in may be NULL
779
780void
781__kmp_init_implicit_task( ident_t *loc_ref, kmp_info_t *this_thr, kmp_team_t *team, int tid, int set_curr_task )
782{
783 kmp_taskdata_t * task = & team->t.t_implicit_task_taskdata[ tid ];
784
785 KF_TRACE(10, ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
786 tid, team, task, set_curr_task ? "TRUE" : "FALSE" ) );
787
788 task->td_task_id = KMP_GEN_TASK_ID();
789 task->td_team = team;
790// task->td_parent = NULL; // fix for CQ230101 (broken parent task info in debugger)
791 task->td_ident = loc_ref;
792 task->td_taskwait_ident = NULL;
793 task->td_taskwait_counter = 0;
794 task->td_taskwait_thread = 0;
795
796 task->td_flags.tiedness = TASK_TIED;
797 task->td_flags.tasktype = TASK_IMPLICIT;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000798#if OMP_41_ENABLED
799 task->td_flags.proxy = TASK_FULL;
800#endif
801
Jim Cownie5e8470a2013-09-27 10:38:44 +0000802 // All implicit tasks are executed immediately, not deferred
803 task->td_flags.task_serial = 1;
804 task->td_flags.tasking_ser = ( __kmp_tasking_mode == tskm_immediate_exec );
805 task->td_flags.team_serial = ( team->t.t_serialized ) ? 1 : 0;
806
807 task->td_flags.started = 1;
808 task->td_flags.executing = 1;
809 task->td_flags.complete = 0;
810 task->td_flags.freed = 0;
811
Jim Cownie181b4bb2013-12-23 17:28:57 +0000812#if OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000813 task->td_dephash = NULL;
814 task->td_depnode = NULL;
Jim Cownie181b4bb2013-12-23 17:28:57 +0000815#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000816
817 if (set_curr_task) { // only do this initialization the first time a thread is created
818 task->td_incomplete_child_tasks = 0;
819 task->td_allocated_child_tasks = 0; // Not used because do not need to deallocate implicit task
820#if OMP_40_ENABLED
821 task->td_taskgroup = NULL; // An implicit task does not have taskgroup
822#endif
823 __kmp_push_current_task_to_thread( this_thr, team, tid );
824 } else {
825 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
826 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
827 }
828
Andrey Churbanovd7d088f2015-04-29 16:42:24 +0000829#if OMPT_SUPPORT
830 __kmp_task_init_ompt(task, tid);
831#endif
832
Jim Cownie5e8470a2013-09-27 10:38:44 +0000833 KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n",
834 tid, team, task ) );
835}
836
837// Round up a size to a power of two specified by val
838// Used to insert padding between structures co-allocated using a single malloc() call
839static size_t
840__kmp_round_up_to_val( size_t size, size_t val ) {
841 if ( size & ( val - 1 ) ) {
842 size &= ~ ( val - 1 );
843 if ( size <= KMP_SIZE_T_MAX - val ) {
844 size += val; // Round up if there is no overflow.
845 }; // if
846 }; // if
847 return size;
848} // __kmp_round_up_to_va
849
850
851//---------------------------------------------------------------------------------
852// __kmp_task_alloc: Allocate the taskdata and task data structures for a task
853//
854// loc_ref: source location information
855// gtid: global thread number.
856// flags: include tiedness & task type (explicit vs. implicit) of the ''new'' task encountered.
857// Converted from kmp_int32 to kmp_tasking_flags_t in routine.
858// sizeof_kmp_task_t: Size in bytes of kmp_task_t data structure including private vars accessed in task.
859// sizeof_shareds: Size in bytes of array of pointers to shared vars accessed in task.
860// task_entry: Pointer to task code entry point generated by compiler.
861// returns: a pointer to the allocated kmp_task_t structure (task).
862
863kmp_task_t *
864__kmp_task_alloc( ident_t *loc_ref, kmp_int32 gtid, kmp_tasking_flags_t *flags,
865 size_t sizeof_kmp_task_t, size_t sizeof_shareds,
866 kmp_routine_entry_t task_entry )
867{
868 kmp_task_t *task;
869 kmp_taskdata_t *taskdata;
870 kmp_info_t *thread = __kmp_threads[ gtid ];
871 kmp_team_t *team = thread->th.th_team;
872 kmp_taskdata_t *parent_task = thread->th.th_current_task;
873 size_t shareds_offset;
874
875 KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
876 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
877 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
878 sizeof_shareds, task_entry) );
879
880 if ( parent_task->td_flags.final ) {
881 if (flags->merged_if0) {
882 }
883 flags->final = 1;
884 }
885
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000886#if OMP_41_ENABLED
887 if ( flags->proxy == TASK_PROXY ) {
888 flags->tiedness = TASK_UNTIED;
889 flags->merged_if0 = 1;
890
891 /* are we running in a sequential parallel or tskm_immediate_exec... we need tasking support enabled */
892 if ( (thread->th.th_task_team) == NULL ) {
893 /* This should only happen if the team is serialized
894 setup a task team and propagate it to the thread
895 */
896 KMP_DEBUG_ASSERT(team->t.t_serialized);
897 KA_TRACE(30,("T#%d creating task team in __kmp_task_alloc for proxy task\n", gtid));
898 __kmp_task_team_setup(thread,team,0,1); // 0,1 indicates only setup the current team regardless of nthreads
899 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
900 }
901 kmp_task_team_t * task_team = thread->th.th_task_team;
902
903 /* tasking must be enabled now as the task might not be pushed */
904 if ( !KMP_TASKING_ENABLED( task_team ) ) {
905 KA_TRACE(30,("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
906 __kmp_enable_tasking( task_team, thread );
907 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
908 kmp_thread_data_t * thread_data = & task_team -> tt.tt_threads_data[ tid ];
909 // No lock needed since only owner can allocate
910 if (thread_data -> td.td_deque == NULL ) {
911 __kmp_alloc_task_deque( thread, thread_data );
912 }
913 }
914
915 if ( task_team->tt.tt_found_proxy_tasks == FALSE )
916 TCW_4(task_team -> tt.tt_found_proxy_tasks, TRUE);
917 }
918#endif
919
Jim Cownie5e8470a2013-09-27 10:38:44 +0000920 // Calculate shared structure offset including padding after kmp_task_t struct
921 // to align pointers in shared struct
922 shareds_offset = sizeof( kmp_taskdata_t ) + sizeof_kmp_task_t;
923 shareds_offset = __kmp_round_up_to_val( shareds_offset, sizeof( void * ));
924
925 // Allocate a kmp_taskdata_t block and a kmp_task_t block.
926 KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n",
927 gtid, shareds_offset) );
928 KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n",
929 gtid, sizeof_shareds) );
930
931 // Avoid double allocation here by combining shareds with taskdata
932 #if USE_FAST_MEMORY
933 taskdata = (kmp_taskdata_t *) __kmp_fast_allocate( thread, shareds_offset + sizeof_shareds );
934 #else /* ! USE_FAST_MEMORY */
935 taskdata = (kmp_taskdata_t *) __kmp_thread_malloc( thread, shareds_offset + sizeof_shareds );
936 #endif /* USE_FAST_MEMORY */
937
938 task = KMP_TASKDATA_TO_TASK(taskdata);
939
940 // Make sure task & taskdata are aligned appropriately
Andrey Churbanovd1c55042015-01-19 18:29:35 +0000941#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000942 KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)taskdata) & (sizeof(double)-1) ) == 0 );
943 KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)task) & (sizeof(double)-1) ) == 0 );
944#else
945 KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)taskdata) & (sizeof(_Quad)-1) ) == 0 );
946 KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)task) & (sizeof(_Quad)-1) ) == 0 );
947#endif
948 if (sizeof_shareds > 0) {
949 // Avoid double allocation here by combining shareds with taskdata
950 task->shareds = & ((char *) taskdata)[ shareds_offset ];
951 // Make sure shareds struct is aligned to pointer size
952 KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)task->shareds) & (sizeof(void *)-1) ) == 0 );
953 } else {
954 task->shareds = NULL;
955 }
956 task->routine = task_entry;
957 task->part_id = 0; // AC: Always start with 0 part id
958
959 taskdata->td_task_id = KMP_GEN_TASK_ID();
960 taskdata->td_team = team;
Jim Cownie181b4bb2013-12-23 17:28:57 +0000961 taskdata->td_alloc_thread = thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000962 taskdata->td_parent = parent_task;
963 taskdata->td_level = parent_task->td_level + 1; // increment nesting level
964 taskdata->td_ident = loc_ref;
965 taskdata->td_taskwait_ident = NULL;
966 taskdata->td_taskwait_counter = 0;
967 taskdata->td_taskwait_thread = 0;
968 KMP_DEBUG_ASSERT( taskdata->td_parent != NULL );
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000969#if OMP_41_ENABLED
970 // avoid copying icvs for proxy tasks
971 if ( flags->proxy == TASK_FULL )
972#endif
973 copy_icvs( &taskdata->td_icvs, &taskdata->td_parent->td_icvs );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000974
975 taskdata->td_flags.tiedness = flags->tiedness;
976 taskdata->td_flags.final = flags->final;
977 taskdata->td_flags.merged_if0 = flags->merged_if0;
Jim Cownie181b4bb2013-12-23 17:28:57 +0000978#if OMP_40_ENABLED
979 taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
980#endif // OMP_40_ENABLED
Andrey Churbanov535b6fa2015-05-07 17:41:51 +0000981#if OMP_41_ENABLED
982 taskdata->td_flags.proxy = flags->proxy;
983#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000984 taskdata->td_flags.tasktype = TASK_EXPLICIT;
985
986 // GEH - TODO: fix this to copy parent task's value of tasking_ser flag
987 taskdata->td_flags.tasking_ser = ( __kmp_tasking_mode == tskm_immediate_exec );
988
989 // GEH - TODO: fix this to copy parent task's value of team_serial flag
990 taskdata->td_flags.team_serial = ( team->t.t_serialized ) ? 1 : 0;
991
992 // GEH - Note we serialize the task if the team is serialized to make sure implicit parallel region
993 // tasks are not left until program termination to execute. Also, it helps locality to execute
994 // immediately.
Jonathan Peyton7881aa12015-05-21 21:16:38 +0000995 taskdata->td_flags.task_serial = ( parent_task->td_flags.final
Jim Cownie5e8470a2013-09-27 10:38:44 +0000996 || taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser );
997
998 taskdata->td_flags.started = 0;
999 taskdata->td_flags.executing = 0;
1000 taskdata->td_flags.complete = 0;
1001 taskdata->td_flags.freed = 0;
1002
1003 taskdata->td_flags.native = flags->native;
1004
1005 taskdata->td_incomplete_child_tasks = 0;
1006 taskdata->td_allocated_child_tasks = 1; // start at one because counts current task and children
1007#if OMP_40_ENABLED
1008 taskdata->td_taskgroup = parent_task->td_taskgroup; // task inherits the taskgroup from the parent task
1009 taskdata->td_dephash = NULL;
1010 taskdata->td_depnode = NULL;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001011#endif
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001012
1013 // Only need to keep track of child task counts if team parallel and tasking not serialized or if it is a proxy task
1014#if OMP_41_ENABLED
1015 if ( flags->proxy == TASK_PROXY || !( taskdata -> td_flags.team_serial || taskdata -> td_flags.tasking_ser ) )
1016#else
1017 if ( !( taskdata -> td_flags.team_serial || taskdata -> td_flags.tasking_ser ) )
1018#endif
1019 {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001020 KMP_TEST_THEN_INC32( (kmp_int32 *)(& parent_task->td_incomplete_child_tasks) );
1021#if OMP_40_ENABLED
1022 if ( parent_task->td_taskgroup )
1023 KMP_TEST_THEN_INC32( (kmp_int32 *)(& parent_task->td_taskgroup->count) );
1024#endif
1025 // Only need to keep track of allocated child tasks for explicit tasks since implicit not deallocated
1026 if ( taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT ) {
1027 KMP_TEST_THEN_INC32( (kmp_int32 *)(& taskdata->td_parent->td_allocated_child_tasks) );
1028 }
1029 }
1030
1031 KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1032 gtid, taskdata, taskdata->td_parent) );
1033
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001034#if OMPT_SUPPORT
1035 if (ompt_status & ompt_status_track) {
1036 taskdata->ompt_task_info.task_id = __ompt_task_id_new(gtid);
1037 taskdata->ompt_task_info.function = (void*) task_entry;
1038 taskdata->ompt_task_info.frame = (ompt_frame_t)
1039 { .exit_runtime_frame = NULL, .reenter_runtime_frame = NULL };
1040 }
1041#endif
1042
Jim Cownie5e8470a2013-09-27 10:38:44 +00001043 return task;
1044}
1045
1046
1047kmp_task_t *
1048__kmpc_omp_task_alloc( ident_t *loc_ref, kmp_int32 gtid, kmp_int32 flags,
1049 size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1050 kmp_routine_entry_t task_entry )
1051{
1052 kmp_task_t *retval;
1053 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *) & flags;
1054
1055 input_flags->native = FALSE;
1056 // __kmp_task_alloc() sets up all other runtime flags
1057
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001058 KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) "
Jim Cownie5e8470a2013-09-27 10:38:44 +00001059 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1060 gtid, loc_ref, input_flags->tiedness ? "tied " : "untied",
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001061#if OMP_41_ENABLED
1062 input_flags->proxy ? "proxy" : "",
1063#else
1064 "",
1065#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001066 sizeof_kmp_task_t, sizeof_shareds, task_entry) );
1067
1068 retval = __kmp_task_alloc( loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1069 sizeof_shareds, task_entry );
1070
1071 KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval) );
1072
1073 return retval;
1074}
1075
1076//-----------------------------------------------------------
1077// __kmp_invoke_task: invoke the specified task
1078//
1079// gtid: global thread ID of caller
1080// task: the task to invoke
1081// current_task: the task to resume after task invokation
1082
1083static void
1084__kmp_invoke_task( kmp_int32 gtid, kmp_task_t *task, kmp_taskdata_t * current_task )
1085{
1086 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
Jim Cownie181b4bb2013-12-23 17:28:57 +00001087#if OMP_40_ENABLED
1088 int discard = 0 /* false */;
1089#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001090 KA_TRACE(30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1091 gtid, taskdata, current_task) );
1092
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001093#if OMP_41_ENABLED
1094 if ( taskdata->td_flags.proxy == TASK_PROXY &&
1095 taskdata->td_flags.complete == 1)
1096 {
1097 // This is a proxy task that was already completed but it needs to run
1098 // its bottom-half finish
1099 KA_TRACE(30, ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1100 gtid, taskdata) );
1101
1102 __kmp_bottom_half_finish_proxy(gtid,task);
1103
1104 KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for proxy task %p, resuming task %p\n", gtid, taskdata, current_task) );
1105
1106 return;
1107 }
1108#endif
1109
1110#if OMP_41_ENABLED
1111 // Proxy tasks are not handled by the runtime
1112 if ( taskdata->td_flags.proxy != TASK_PROXY )
1113#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001114 __kmp_task_start( gtid, task, current_task );
1115
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001116#if OMPT_SUPPORT
1117 ompt_thread_info_t oldInfo;
1118 kmp_info_t * thread;
1119 if (ompt_status & ompt_status_track) {
1120 // Store the threads states and restore them after the task
1121 thread = __kmp_threads[ gtid ];
1122 oldInfo = thread->th.ompt_thread_info;
1123 thread->th.ompt_thread_info.wait_id = 0;
1124 thread->th.ompt_thread_info.state = ompt_state_work_parallel;
1125 taskdata->ompt_task_info.frame.exit_runtime_frame = __builtin_frame_address(0);
1126 }
1127#endif
1128
Jim Cownie181b4bb2013-12-23 17:28:57 +00001129#if OMP_40_ENABLED
1130 // TODO: cancel tasks if the parallel region has also been cancelled
1131 // TODO: check if this sequence can be hoisted above __kmp_task_start
1132 // if cancellation has been enabled for this run ...
1133 if (__kmp_omp_cancellation) {
1134 kmp_info_t *this_thr = __kmp_threads [ gtid ];
1135 kmp_team_t * this_team = this_thr->th.th_team;
1136 kmp_taskgroup_t * taskgroup = taskdata->td_taskgroup;
1137 if ((taskgroup && taskgroup->cancel_request) || (this_team->t.t_cancel_request == cancel_parallel)) {
1138 // this task belongs to a task group and we need to cancel it
1139 discard = 1 /* true */;
1140 }
1141 }
1142
Jim Cownie5e8470a2013-09-27 10:38:44 +00001143 //
1144 // Invoke the task routine and pass in relevant data.
1145 // Thunks generated by gcc take a different argument list.
1146 //
Jim Cownie181b4bb2013-12-23 17:28:57 +00001147 if (!discard) {
1148#endif // OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +00001149#ifdef KMP_GOMP_COMPAT
Jim Cownie181b4bb2013-12-23 17:28:57 +00001150 if (taskdata->td_flags.native) {
1151 ((void (*)(void *))(*(task->routine)))(task->shareds);
1152 }
1153 else
Jim Cownie5e8470a2013-09-27 10:38:44 +00001154#endif /* KMP_GOMP_COMPAT */
Jim Cownie181b4bb2013-12-23 17:28:57 +00001155 {
1156 (*(task->routine))(gtid, task);
1157 }
1158#if OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +00001159 }
Jim Cownie181b4bb2013-12-23 17:28:57 +00001160#endif // OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +00001161
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001162
1163#if OMPT_SUPPORT
1164 if (ompt_status & ompt_status_track) {
1165 thread->th.ompt_thread_info = oldInfo;
1166 taskdata->ompt_task_info.frame.exit_runtime_frame = 0;
1167 }
1168#endif
1169
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001170#if OMP_41_ENABLED
1171 // Proxy tasks are not handled by the runtime
1172 if ( taskdata->td_flags.proxy != TASK_PROXY )
1173#endif
1174 __kmp_task_finish( gtid, task, current_task );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001175
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001176 KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001177 gtid, taskdata, current_task) );
1178 return;
1179}
1180
1181//-----------------------------------------------------------------------
1182// __kmpc_omp_task_parts: Schedule a thread-switchable task for execution
1183//
1184// loc_ref: location of original task pragma (ignored)
1185// gtid: Global Thread ID of encountering thread
1186// new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
1187// Returns:
1188// TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to be resumed later.
1189// TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be resumed later.
1190
1191kmp_int32
1192__kmpc_omp_task_parts( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * new_task)
1193{
1194 kmp_taskdata_t * new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1195
1196 KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n",
1197 gtid, loc_ref, new_taskdata ) );
1198
1199 /* Should we execute the new task or queue it? For now, let's just always try to
1200 queue it. If the queue fills up, then we'll execute it. */
1201
1202 if ( __kmp_push_task( gtid, new_task ) == TASK_NOT_PUSHED ) // if cannot defer
1203 { // Execute this task immediately
1204 kmp_taskdata_t * current_task = __kmp_threads[ gtid ] -> th.th_current_task;
1205 new_taskdata->td_flags.task_serial = 1;
1206 __kmp_invoke_task( gtid, new_task, current_task );
1207 }
1208
1209 KA_TRACE(10, ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1210 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n", gtid, loc_ref,
1211 new_taskdata ) );
1212
1213 return TASK_CURRENT_NOT_QUEUED;
1214}
1215
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001216//---------------------------------------------------------------------
1217// __kmp_omp_task: Schedule a non-thread-switchable task for execution
1218// gtid: Global Thread ID of encountering thread
1219// new_task: non-thread-switchable task thunk allocated by __kmp_omp_task_alloc()
1220// serialize_immediate: if TRUE then if the task is executed immediately its execution will be serialized
1221// returns:
1222//
1223// TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to be resumed later.
1224// TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be resumed later.
1225kmp_int32
1226__kmp_omp_task( kmp_int32 gtid, kmp_task_t * new_task, bool serialize_immediate )
1227{
1228 kmp_taskdata_t * new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1229
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001230#if OMPT_SUPPORT
1231 if (ompt_status & ompt_status_track) {
1232 new_taskdata->ompt_task_info.frame.reenter_runtime_frame =
1233 __builtin_frame_address(0);
1234 }
1235#endif
1236
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001237 /* Should we execute the new task or queue it? For now, let's just always try to
1238 queue it. If the queue fills up, then we'll execute it. */
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001239#if OMP_41_ENABLED
1240 if ( new_taskdata->td_flags.proxy == TASK_PROXY || __kmp_push_task( gtid, new_task ) == TASK_NOT_PUSHED ) // if cannot defer
1241#else
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001242 if ( __kmp_push_task( gtid, new_task ) == TASK_NOT_PUSHED ) // if cannot defer
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001243#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001244 { // Execute this task immediately
1245 kmp_taskdata_t * current_task = __kmp_threads[ gtid ] -> th.th_current_task;
1246 if ( serialize_immediate )
1247 new_taskdata -> td_flags.task_serial = 1;
1248 __kmp_invoke_task( gtid, new_task, current_task );
1249 }
1250
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001251#if OMPT_SUPPORT
1252 if (ompt_status & ompt_status_track) {
1253 new_taskdata->ompt_task_info.frame.reenter_runtime_frame = 0;
1254 }
1255#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001256
1257 return TASK_CURRENT_NOT_QUEUED;
1258}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001259
1260//---------------------------------------------------------------------
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001261// __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a non-thread-switchable task from
1262// the parent thread only!
Jim Cownie5e8470a2013-09-27 10:38:44 +00001263// loc_ref: location of original task pragma (ignored)
1264// gtid: Global Thread ID of encountering thread
1265// new_task: non-thread-switchable task thunk allocated by __kmp_omp_task_alloc()
1266// returns:
1267//
1268// TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to be resumed later.
1269// TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be resumed later.
1270
1271kmp_int32
1272__kmpc_omp_task( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * new_task)
1273{
1274 kmp_taskdata_t * new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001275 kmp_int32 res;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001276
1277 KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n",
1278 gtid, loc_ref, new_taskdata ) );
1279
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001280 res = __kmp_omp_task(gtid,new_task,true);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001281
1282 KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1283 gtid, loc_ref, new_taskdata ) );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001284 return res;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001285}
1286
Jim Cownie5e8470a2013-09-27 10:38:44 +00001287//-------------------------------------------------------------------------------------
1288// __kmpc_omp_taskwait: Wait until all tasks generated by the current task are complete
1289
1290kmp_int32
1291__kmpc_omp_taskwait( ident_t *loc_ref, kmp_int32 gtid )
1292{
1293 kmp_taskdata_t * taskdata;
1294 kmp_info_t * thread;
1295 int thread_finished = FALSE;
1296
1297 KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n",
1298 gtid, loc_ref) );
1299
1300 if ( __kmp_tasking_mode != tskm_immediate_exec ) {
1301 // GEH TODO: shouldn't we have some sort of OMPRAP API calls here to mark begin wait?
1302
1303 thread = __kmp_threads[ gtid ];
1304 taskdata = thread -> th.th_current_task;
1305#if USE_ITT_BUILD
1306 // Note: These values are used by ITT events as well.
1307#endif /* USE_ITT_BUILD */
1308 taskdata->td_taskwait_counter += 1;
1309 taskdata->td_taskwait_ident = loc_ref;
1310 taskdata->td_taskwait_thread = gtid + 1;
1311
1312#if USE_ITT_BUILD
1313 void * itt_sync_obj = __kmp_itt_taskwait_object( gtid );
1314 if ( itt_sync_obj != NULL )
1315 __kmp_itt_taskwait_starting( gtid, itt_sync_obj );
1316#endif /* USE_ITT_BUILD */
1317
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001318#if OMP_41_ENABLED
1319 if ( ! taskdata->td_flags.team_serial || (thread->th.th_task_team != NULL && thread->th.th_task_team->tt.tt_found_proxy_tasks) )
1320#else
1321 if ( ! taskdata->td_flags.team_serial )
1322#endif
1323 {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001324 // GEH: if team serialized, avoid reading the volatile variable below.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001325 kmp_flag_32 flag(&(taskdata->td_incomplete_child_tasks), 0U);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001326 while ( TCR_4(taskdata -> td_incomplete_child_tasks) != 0 ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001327 flag.execute_tasks(thread, gtid, FALSE, &thread_finished
1328 USE_ITT_BUILD_ARG(itt_sync_obj), __kmp_task_stealing_constraint );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001329 }
1330 }
1331#if USE_ITT_BUILD
1332 if ( itt_sync_obj != NULL )
1333 __kmp_itt_taskwait_finished( gtid, itt_sync_obj );
1334#endif /* USE_ITT_BUILD */
1335
1336 // GEH TODO: shouldn't we have some sort of OMPRAP API calls here to mark end of wait?
1337 taskdata->td_taskwait_thread = - taskdata->td_taskwait_thread;
1338 }
1339
1340 KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
1341 "returning TASK_CURRENT_NOT_QUEUED\n", gtid, taskdata) );
1342
1343 return TASK_CURRENT_NOT_QUEUED;
1344}
1345
1346
1347//-------------------------------------------------
1348// __kmpc_omp_taskyield: switch to a different task
1349
1350kmp_int32
1351__kmpc_omp_taskyield( ident_t *loc_ref, kmp_int32 gtid, int end_part )
1352{
1353 kmp_taskdata_t * taskdata;
1354 kmp_info_t * thread;
1355 int thread_finished = FALSE;
1356
1357 KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
1358 gtid, loc_ref, end_part) );
1359
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001360 if ( __kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001361 // GEH TODO: shouldn't we have some sort of OMPRAP API calls here to mark begin wait?
1362
1363 thread = __kmp_threads[ gtid ];
1364 taskdata = thread -> th.th_current_task;
1365 // Should we model this as a task wait or not?
1366#if USE_ITT_BUILD
1367 // Note: These values are used by ITT events as well.
1368#endif /* USE_ITT_BUILD */
1369 taskdata->td_taskwait_counter += 1;
1370 taskdata->td_taskwait_ident = loc_ref;
1371 taskdata->td_taskwait_thread = gtid + 1;
1372
1373#if USE_ITT_BUILD
1374 void * itt_sync_obj = __kmp_itt_taskwait_object( gtid );
1375 if ( itt_sync_obj != NULL )
1376 __kmp_itt_taskwait_starting( gtid, itt_sync_obj );
1377#endif /* USE_ITT_BUILD */
1378 if ( ! taskdata->td_flags.team_serial ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001379 kmp_task_team_t * task_team = thread->th.th_task_team;
1380 if (task_team != NULL) {
Andrey Churbanov6d224db2015-02-10 18:37:43 +00001381 if (KMP_TASKING_ENABLED(task_team)) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001382 __kmp_execute_tasks_32( thread, gtid, NULL, FALSE, &thread_finished
1383 USE_ITT_BUILD_ARG(itt_sync_obj), __kmp_task_stealing_constraint );
1384 }
1385 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001386 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001387#if USE_ITT_BUILD
1388 if ( itt_sync_obj != NULL )
1389 __kmp_itt_taskwait_finished( gtid, itt_sync_obj );
1390#endif /* USE_ITT_BUILD */
1391
1392 // GEH TODO: shouldn't we have some sort of OMPRAP API calls here to mark end of wait?
1393 taskdata->td_taskwait_thread = - taskdata->td_taskwait_thread;
1394 }
1395
1396 KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
1397 "returning TASK_CURRENT_NOT_QUEUED\n", gtid, taskdata) );
1398
1399 return TASK_CURRENT_NOT_QUEUED;
1400}
1401
1402
1403#if OMP_40_ENABLED
1404//-------------------------------------------------------------------------------------
1405// __kmpc_taskgroup: Start a new taskgroup
1406
1407void
Jim Cownie181b4bb2013-12-23 17:28:57 +00001408__kmpc_taskgroup( ident_t* loc, int gtid )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001409{
1410 kmp_info_t * thread = __kmp_threads[ gtid ];
1411 kmp_taskdata_t * taskdata = thread->th.th_current_task;
1412 kmp_taskgroup_t * tg_new =
1413 (kmp_taskgroup_t *)__kmp_thread_malloc( thread, sizeof( kmp_taskgroup_t ) );
1414 KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new) );
1415 tg_new->count = 0;
Jim Cownie181b4bb2013-12-23 17:28:57 +00001416 tg_new->cancel_request = cancel_noreq;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001417 tg_new->parent = taskdata->td_taskgroup;
1418 taskdata->td_taskgroup = tg_new;
1419}
1420
1421
1422//-------------------------------------------------------------------------------------
1423// __kmpc_end_taskgroup: Wait until all tasks generated by the current task
1424// and its descendants are complete
1425
1426void
Jim Cownie181b4bb2013-12-23 17:28:57 +00001427__kmpc_end_taskgroup( ident_t* loc, int gtid )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001428{
1429 kmp_info_t * thread = __kmp_threads[ gtid ];
1430 kmp_taskdata_t * taskdata = thread->th.th_current_task;
1431 kmp_taskgroup_t * taskgroup = taskdata->td_taskgroup;
1432 int thread_finished = FALSE;
1433
1434 KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc) );
1435 KMP_DEBUG_ASSERT( taskgroup != NULL );
1436
1437 if ( __kmp_tasking_mode != tskm_immediate_exec ) {
1438#if USE_ITT_BUILD
1439 // For ITT the taskgroup wait is similar to taskwait until we need to distinguish them
1440 void * itt_sync_obj = __kmp_itt_taskwait_object( gtid );
1441 if ( itt_sync_obj != NULL )
1442 __kmp_itt_taskwait_starting( gtid, itt_sync_obj );
1443#endif /* USE_ITT_BUILD */
1444
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001445#if OMP_41_ENABLED
1446 if ( ! taskdata->td_flags.team_serial || (thread->th.th_task_team != NULL && thread->th.th_task_team->tt.tt_found_proxy_tasks) )
1447#else
1448 if ( ! taskdata->td_flags.team_serial )
1449#endif
1450 {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001451 kmp_flag_32 flag(&(taskgroup->count), 0U);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001452 while ( TCR_4(taskgroup->count) != 0 ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001453 flag.execute_tasks(thread, gtid, FALSE, &thread_finished
1454 USE_ITT_BUILD_ARG(itt_sync_obj), __kmp_task_stealing_constraint );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001455 }
1456 }
1457
1458#if USE_ITT_BUILD
1459 if ( itt_sync_obj != NULL )
1460 __kmp_itt_taskwait_finished( gtid, itt_sync_obj );
1461#endif /* USE_ITT_BUILD */
1462 }
1463 KMP_DEBUG_ASSERT( taskgroup->count == 0 );
1464
1465 // Restore parent taskgroup for the current task
1466 taskdata->td_taskgroup = taskgroup->parent;
1467 __kmp_thread_free( thread, taskgroup );
1468
1469 KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n", gtid, taskdata) );
1470}
1471#endif
1472
1473
1474//------------------------------------------------------
1475// __kmp_remove_my_task: remove a task from my own deque
1476
1477static kmp_task_t *
1478__kmp_remove_my_task( kmp_info_t * thread, kmp_int32 gtid, kmp_task_team_t *task_team,
1479 kmp_int32 is_constrained )
1480{
1481 kmp_task_t * task;
1482 kmp_taskdata_t * taskdata;
1483 kmp_thread_data_t *thread_data;
1484 kmp_uint32 tail;
1485
1486 KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
1487 KMP_DEBUG_ASSERT( task_team -> tt.tt_threads_data != NULL ); // Caller should check this condition
1488
1489 thread_data = & task_team -> tt.tt_threads_data[ __kmp_tid_from_gtid( gtid ) ];
1490
1491 KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
1492 gtid, thread_data->td.td_deque_ntasks, thread_data->td.td_deque_head,
1493 thread_data->td.td_deque_tail) );
1494
1495 if (TCR_4(thread_data -> td.td_deque_ntasks) == 0) {
1496 KA_TRACE(10, ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: ntasks=%d head=%u tail=%u\n",
1497 gtid, thread_data->td.td_deque_ntasks, thread_data->td.td_deque_head,
1498 thread_data->td.td_deque_tail) );
1499 return NULL;
1500 }
1501
1502 __kmp_acquire_bootstrap_lock( & thread_data -> td.td_deque_lock );
1503
1504 if (TCR_4(thread_data -> td.td_deque_ntasks) == 0) {
1505 __kmp_release_bootstrap_lock( & thread_data -> td.td_deque_lock );
1506 KA_TRACE(10, ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: ntasks=%d head=%u tail=%u\n",
1507 gtid, thread_data->td.td_deque_ntasks, thread_data->td.td_deque_head,
1508 thread_data->td.td_deque_tail) );
1509 return NULL;
1510 }
1511
1512 tail = ( thread_data -> td.td_deque_tail - 1 ) & TASK_DEQUE_MASK; // Wrap index.
1513 taskdata = thread_data -> td.td_deque[ tail ];
1514
1515 if (is_constrained) {
1516 // we need to check if the candidate obeys task scheduling constraint:
1517 // only child of current task can be scheduled
1518 kmp_taskdata_t * current = thread->th.th_current_task;
1519 kmp_int32 level = current->td_level;
1520 kmp_taskdata_t * parent = taskdata->td_parent;
1521 while ( parent != current && parent->td_level > level ) {
1522 parent = parent->td_parent; // check generation up to the level of the current task
1523 KMP_DEBUG_ASSERT(parent != NULL);
1524 }
1525 if ( parent != current ) {
1526 // If the tail task is not a child, then no other childs can appear in the deque.
1527 __kmp_release_bootstrap_lock( & thread_data -> td.td_deque_lock );
1528 KA_TRACE(10, ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: ntasks=%d head=%u tail=%u\n",
1529 gtid, thread_data->td.td_deque_ntasks, thread_data->td.td_deque_head,
1530 thread_data->td.td_deque_tail) );
1531 return NULL;
1532 }
1533 }
1534
1535 thread_data -> td.td_deque_tail = tail;
1536 TCW_4(thread_data -> td.td_deque_ntasks, thread_data -> td.td_deque_ntasks - 1);
1537
1538 __kmp_release_bootstrap_lock( & thread_data->td.td_deque_lock );
1539
1540 KA_TRACE(10, ("__kmp_remove_my_task(exit #2): T#%d task %p removed: ntasks=%d head=%u tail=%u\n",
1541 gtid, taskdata, thread_data->td.td_deque_ntasks, thread_data->td.td_deque_head,
1542 thread_data->td.td_deque_tail) );
1543
1544 task = KMP_TASKDATA_TO_TASK( taskdata );
1545 return task;
1546}
1547
1548
1549//-----------------------------------------------------------
1550// __kmp_steal_task: remove a task from another thread's deque
1551// Assume that calling thread has already checked existence of
1552// task_team thread_data before calling this routine.
1553
1554static kmp_task_t *
1555__kmp_steal_task( kmp_info_t *victim, kmp_int32 gtid, kmp_task_team_t *task_team,
1556 volatile kmp_uint32 *unfinished_threads, int *thread_finished,
1557 kmp_int32 is_constrained )
1558{
1559 kmp_task_t * task;
1560 kmp_taskdata_t * taskdata;
1561 kmp_thread_data_t *victim_td, *threads_data;
1562 kmp_int32 victim_tid, thread_tid;
1563
1564 KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
1565
1566 threads_data = task_team -> tt.tt_threads_data;
1567 KMP_DEBUG_ASSERT( threads_data != NULL ); // Caller should check this condition
1568
1569 victim_tid = victim->th.th_info.ds.ds_tid;
1570 victim_td = & threads_data[ victim_tid ];
1571
1572 KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: task_team=%p ntasks=%d "
1573 "head=%u tail=%u\n",
1574 gtid, __kmp_gtid_from_thread( victim ), task_team, victim_td->td.td_deque_ntasks,
1575 victim_td->td.td_deque_head, victim_td->td.td_deque_tail) );
1576
1577 if ( (TCR_4(victim_td -> td.td_deque_ntasks) == 0) || // Caller should not check this condition
1578 (TCR_PTR(victim->th.th_task_team) != task_team)) // GEH: why would this happen?
1579 {
1580 KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: task_team=%p "
1581 "ntasks=%d head=%u tail=%u\n",
1582 gtid, __kmp_gtid_from_thread( victim ), task_team, victim_td->td.td_deque_ntasks,
1583 victim_td->td.td_deque_head, victim_td->td.td_deque_tail) );
1584 return NULL;
1585 }
1586
1587 __kmp_acquire_bootstrap_lock( & victim_td -> td.td_deque_lock );
1588
1589 // Check again after we acquire the lock
1590 if ( (TCR_4(victim_td -> td.td_deque_ntasks) == 0) ||
1591 (TCR_PTR(victim->th.th_task_team) != task_team)) // GEH: why would this happen?
1592 {
1593 __kmp_release_bootstrap_lock( & victim_td -> td.td_deque_lock );
1594 KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: task_team=%p "
1595 "ntasks=%d head=%u tail=%u\n",
1596 gtid, __kmp_gtid_from_thread( victim ), task_team, victim_td->td.td_deque_ntasks,
1597 victim_td->td.td_deque_head, victim_td->td.td_deque_tail) );
1598 return NULL;
1599 }
1600
1601 KMP_DEBUG_ASSERT( victim_td -> td.td_deque != NULL );
1602
1603 if ( !is_constrained ) {
1604 taskdata = victim_td -> td.td_deque[ victim_td -> td.td_deque_head ];
1605 // Bump head pointer and Wrap.
1606 victim_td -> td.td_deque_head = ( victim_td -> td.td_deque_head + 1 ) & TASK_DEQUE_MASK;
1607 } else {
1608 // While we have postponed tasks let's steal from tail of the deque (smaller tasks)
1609 kmp_int32 tail = ( victim_td -> td.td_deque_tail - 1 ) & TASK_DEQUE_MASK; // Wrap index.
1610 taskdata = victim_td -> td.td_deque[ tail ];
1611 // we need to check if the candidate obeys task scheduling constraint:
1612 // only child of current task can be scheduled
1613 kmp_taskdata_t * current = __kmp_threads[ gtid ]->th.th_current_task;
1614 kmp_int32 level = current->td_level;
1615 kmp_taskdata_t * parent = taskdata->td_parent;
1616 while ( parent != current && parent->td_level > level ) {
1617 parent = parent->td_parent; // check generation up to the level of the current task
1618 KMP_DEBUG_ASSERT(parent != NULL);
1619 }
1620 if ( parent != current ) {
1621 // If the tail task is not a child, then no other childs can appear in the deque (?).
1622 __kmp_release_bootstrap_lock( & victim_td -> td.td_deque_lock );
1623 KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: task_team=%p "
1624 "ntasks=%d head=%u tail=%u\n",
1625 gtid, __kmp_gtid_from_thread( threads_data[victim_tid].td.td_thr ),
1626 task_team, victim_td->td.td_deque_ntasks,
1627 victim_td->td.td_deque_head, victim_td->td.td_deque_tail) );
1628 return NULL;
1629 }
1630 victim_td -> td.td_deque_tail = tail;
1631 }
1632 if (*thread_finished) {
1633 // We need to un-mark this victim as a finished victim. This must be done before
1634 // releasing the lock, or else other threads (starting with the master victim)
1635 // might be prematurely released from the barrier!!!
1636 kmp_uint32 count = KMP_TEST_THEN_INC32( (kmp_int32 *)unfinished_threads );
1637
1638 KA_TRACE(20, ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
1639 gtid, count + 1, task_team) );
1640
1641 *thread_finished = FALSE;
1642 }
1643 TCW_4(victim_td -> td.td_deque_ntasks, TCR_4(victim_td -> td.td_deque_ntasks) - 1);
1644
1645 __kmp_release_bootstrap_lock( & victim_td -> td.td_deque_lock );
1646
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001647 KA_TRACE(10, ("__kmp_steal_task(exit #3): T#%d stole task %p from T#%d: task_team=%p "
Jim Cownie5e8470a2013-09-27 10:38:44 +00001648 "ntasks=%d head=%u tail=%u\n",
1649 gtid, taskdata, __kmp_gtid_from_thread( victim ), task_team,
1650 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
1651 victim_td->td.td_deque_tail) );
1652
1653 task = KMP_TASKDATA_TO_TASK( taskdata );
1654 return task;
1655}
1656
1657
1658//-----------------------------------------------------------------------------
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001659// __kmp_execute_tasks_template: Choose and execute tasks until either the condition
Jim Cownie5e8470a2013-09-27 10:38:44 +00001660// is statisfied (return true) or there are none left (return false).
1661// final_spin is TRUE if this is the spin at the release barrier.
1662// thread_finished indicates whether the thread is finished executing all
1663// the tasks it has on its deque, and is at the release barrier.
1664// spinner is the location on which to spin.
1665// spinner == NULL means only execute a single task and return.
1666// checker is the value to check to terminate the spin.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001667template <class C>
1668static inline int __kmp_execute_tasks_template(kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin,
1669 int *thread_finished
1670 USE_ITT_BUILD_ARG(void * itt_sync_obj), kmp_int32 is_constrained)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001671{
1672 kmp_task_team_t * task_team;
1673 kmp_team_t * team;
1674 kmp_thread_data_t * threads_data;
1675 kmp_task_t * task;
1676 kmp_taskdata_t * current_task = thread -> th.th_current_task;
1677 volatile kmp_uint32 * unfinished_threads;
1678 kmp_int32 nthreads, last_stolen, k, tid;
1679
1680 KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
1681 KMP_DEBUG_ASSERT( thread == __kmp_threads[ gtid ] );
1682
1683 task_team = thread -> th.th_task_team;
1684 KMP_DEBUG_ASSERT( task_team != NULL );
1685
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001686 KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d *thread_finished=%d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001687 gtid, final_spin, *thread_finished) );
1688
1689 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team -> tt.tt_threads_data);
1690 KMP_DEBUG_ASSERT( threads_data != NULL );
1691
1692 nthreads = task_team -> tt.tt_nproc;
1693 unfinished_threads = &(task_team -> tt.tt_unfinished_threads);
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001694#if OMP_41_ENABLED
1695 KMP_DEBUG_ASSERT( nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
1696#else
Jim Cownie5e8470a2013-09-27 10:38:44 +00001697 KMP_DEBUG_ASSERT( nthreads > 1 );
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001698#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001699 KMP_DEBUG_ASSERT( TCR_4((int)*unfinished_threads) >= 0 );
1700
1701 // Choose tasks from our own work queue.
1702 start:
1703 while (( task = __kmp_remove_my_task( thread, gtid, task_team, is_constrained )) != NULL ) {
1704#if USE_ITT_BUILD && USE_ITT_NOTIFY
1705 if ( __itt_sync_create_ptr || KMP_ITT_DEBUG ) {
1706 if ( itt_sync_obj == NULL ) {
1707 // we are at fork barrier where we could not get the object reliably
1708 itt_sync_obj = __kmp_itt_barrier_object( gtid, bs_forkjoin_barrier );
1709 }
1710 __kmp_itt_task_starting( itt_sync_obj );
1711 }
1712#endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
1713 __kmp_invoke_task( gtid, task, current_task );
1714#if USE_ITT_BUILD
1715 if ( itt_sync_obj != NULL )
1716 __kmp_itt_task_finished( itt_sync_obj );
1717#endif /* USE_ITT_BUILD */
1718
1719 // If this thread is only partway through the barrier and the condition
1720 // is met, then return now, so that the barrier gather/release pattern can proceed.
1721 // If this thread is in the last spin loop in the barrier, waiting to be
1722 // released, we know that the termination condition will not be satisified,
1723 // so don't waste any cycles checking it.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001724 if (flag == NULL || (!final_spin && flag->done_check())) {
1725 KA_TRACE(15, ("__kmp_execute_tasks_template(exit #1): T#%d spin condition satisfied\n", gtid) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001726 return TRUE;
1727 }
1728 KMP_YIELD( __kmp_library == library_throughput ); // Yield before executing next task
1729 }
1730
1731 // This thread's work queue is empty. If we are in the final spin loop
1732 // of the barrier, check and see if the termination condition is satisfied.
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001733#if OMP_41_ENABLED
1734 // The work queue may be empty but there might be proxy tasks still executing
1735 if (final_spin && TCR_4(current_task -> td_incomplete_child_tasks) == 0)
1736#else
1737 if (final_spin)
1738#endif
1739 {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001740 // First, decrement the #unfinished threads, if that has not already
1741 // been done. This decrement might be to the spin location, and
1742 // result in the termination condition being satisfied.
1743 if (! *thread_finished) {
1744 kmp_uint32 count = KMP_TEST_THEN_DEC32( (kmp_int32 *)unfinished_threads ) - 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001745 KA_TRACE(20, ("__kmp_execute_tasks_template(dec #1): T#%d dec unfinished_threads to %d task_team=%p\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001746 gtid, count, task_team) );
1747 *thread_finished = TRUE;
1748 }
1749
1750 // It is now unsafe to reference thread->th.th_team !!!
1751 // Decrementing task_team->tt.tt_unfinished_threads can allow the master
1752 // thread to pass through the barrier, where it might reset each thread's
1753 // th.th_team field for the next parallel region.
1754 // If we can steal more work, we know that this has not happened yet.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001755 if (flag != NULL && flag->done_check()) {
1756 KA_TRACE(15, ("__kmp_execute_tasks_template(exit #2): T#%d spin condition satisfied\n", gtid) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001757 return TRUE;
1758 }
1759 }
1760
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001761#if OMP_41_ENABLED
1762 // check if there are other threads to steal from, otherwise go back
1763 if ( nthreads == 1 )
1764 goto start;
1765#endif
1766
Jim Cownie5e8470a2013-09-27 10:38:44 +00001767 // Try to steal from the last place I stole from successfully.
1768 tid = thread -> th.th_info.ds.ds_tid;//__kmp_tid_from_gtid( gtid );
1769 last_stolen = threads_data[ tid ].td.td_deque_last_stolen;
1770
1771 if (last_stolen != -1) {
1772 kmp_info_t *other_thread = threads_data[last_stolen].td.td_thr;
1773
1774 while ((task = __kmp_steal_task( other_thread, gtid, task_team, unfinished_threads,
1775 thread_finished, is_constrained )) != NULL)
1776 {
1777#if USE_ITT_BUILD && USE_ITT_NOTIFY
1778 if ( __itt_sync_create_ptr || KMP_ITT_DEBUG ) {
1779 if ( itt_sync_obj == NULL ) {
1780 // we are at fork barrier where we could not get the object reliably
1781 itt_sync_obj = __kmp_itt_barrier_object( gtid, bs_forkjoin_barrier );
1782 }
1783 __kmp_itt_task_starting( itt_sync_obj );
1784 }
1785#endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
1786 __kmp_invoke_task( gtid, task, current_task );
1787#if USE_ITT_BUILD
1788 if ( itt_sync_obj != NULL )
1789 __kmp_itt_task_finished( itt_sync_obj );
1790#endif /* USE_ITT_BUILD */
1791
1792 // Check to see if this thread can proceed.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001793 if (flag == NULL || (!final_spin && flag->done_check())) {
1794 KA_TRACE(15, ("__kmp_execute_tasks_template(exit #3): T#%d spin condition satisfied\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001795 gtid) );
1796 return TRUE;
1797 }
1798
1799 KMP_YIELD( __kmp_library == library_throughput ); // Yield before executing next task
1800 // If the execution of the stolen task resulted in more tasks being
1801 // placed on our run queue, then restart the whole process.
1802 if (TCR_4(threads_data[ tid ].td.td_deque_ntasks) != 0) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001803 KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned other tasks, restart\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001804 gtid) );
1805 goto start;
1806 }
1807 }
1808
1809 // Don't give priority to stealing from this thread anymore.
1810 threads_data[ tid ].td.td_deque_last_stolen = -1;
1811
1812 // The victims's work queue is empty. If we are in the final spin loop
1813 // of the barrier, check and see if the termination condition is satisfied.
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001814#if OMP_41_ENABLED
1815 // The work queue may be empty but there might be proxy tasks still executing
1816 if (final_spin && TCR_4(current_task -> td_incomplete_child_tasks) == 0)
1817#else
1818 if (final_spin)
1819#endif
1820 {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001821 // First, decrement the #unfinished threads, if that has not already
1822 // been done. This decrement might be to the spin location, and
1823 // result in the termination condition being satisfied.
1824 if (! *thread_finished) {
1825 kmp_uint32 count = KMP_TEST_THEN_DEC32( (kmp_int32 *)unfinished_threads ) - 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001826 KA_TRACE(20, ("__kmp_execute_tasks_template(dec #2): T#%d dec unfinished_threads to %d "
Jim Cownie5e8470a2013-09-27 10:38:44 +00001827 "task_team=%p\n", gtid, count, task_team) );
1828 *thread_finished = TRUE;
1829 }
1830
1831 // If __kmp_tasking_mode != tskm_immediate_exec
1832 // then it is now unsafe to reference thread->th.th_team !!!
1833 // Decrementing task_team->tt.tt_unfinished_threads can allow the master
1834 // thread to pass through the barrier, where it might reset each thread's
1835 // th.th_team field for the next parallel region.
1836 // If we can steal more work, we know that this has not happened yet.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001837 if (flag != NULL && flag->done_check()) {
1838 KA_TRACE(15, ("__kmp_execute_tasks_template(exit #4): T#%d spin condition satisfied\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001839 gtid) );
1840 return TRUE;
1841 }
1842 }
1843 }
1844
1845 // Find a different thread to steal work from. Pick a random thread.
1846 // My initial plan was to cycle through all the threads, and only return
1847 // if we tried to steal from every thread, and failed. Arch says that's
1848 // not such a great idea.
1849 // GEH - need yield code in this loop for throughput library mode?
1850 new_victim:
1851 k = __kmp_get_random( thread ) % (nthreads - 1);
1852 if ( k >= thread -> th.th_info.ds.ds_tid ) {
1853 ++k; // Adjusts random distribution to exclude self
1854 }
1855 {
1856 kmp_info_t *other_thread = threads_data[k].td.td_thr;
1857 int first;
1858
1859 // There is a slight chance that __kmp_enable_tasking() did not wake up
1860 // all threads waiting at the barrier. If this thread is sleeping, then
1861 // then wake it up. Since we weree going to pay the cache miss penalty
1862 // for referenceing another thread's kmp_info_t struct anyway, the check
1863 // shouldn't cost too much performance at this point.
1864 // In extra barrier mode, tasks do not sleep at the separate tasking
1865 // barrier, so this isn't a problem.
1866 if ( ( __kmp_tasking_mode == tskm_task_teams ) &&
1867 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
1868 (TCR_PTR(other_thread->th.th_sleep_loc) != NULL))
1869 {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001870 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread), other_thread->th.th_sleep_loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001871 // A sleeping thread should not have any tasks on it's queue.
Alp Toker8f2d3f02014-02-24 10:40:15 +00001872 // There is a slight possibility that it resumes, steals a task from
Jim Cownie5e8470a2013-09-27 10:38:44 +00001873 // another thread, which spawns more tasks, all in the that it takes
1874 // this thread to check => don't write an assertion that the victim's
1875 // queue is empty. Try stealing from a different thread.
1876 goto new_victim;
1877 }
1878
1879 // Now try to steal work from the selected thread
1880 first = TRUE;
1881 while ((task = __kmp_steal_task( other_thread, gtid, task_team, unfinished_threads,
1882 thread_finished, is_constrained )) != NULL)
1883 {
1884#if USE_ITT_BUILD && USE_ITT_NOTIFY
1885 if ( __itt_sync_create_ptr || KMP_ITT_DEBUG ) {
1886 if ( itt_sync_obj == NULL ) {
1887 // we are at fork barrier where we could not get the object reliably
1888 itt_sync_obj = __kmp_itt_barrier_object( gtid, bs_forkjoin_barrier );
1889 }
1890 __kmp_itt_task_starting( itt_sync_obj );
1891 }
1892#endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
1893 __kmp_invoke_task( gtid, task, current_task );
1894#if USE_ITT_BUILD
1895 if ( itt_sync_obj != NULL )
1896 __kmp_itt_task_finished( itt_sync_obj );
1897#endif /* USE_ITT_BUILD */
1898
1899 // Try stealing from this victim again, in the future.
1900 if (first) {
1901 threads_data[ tid ].td.td_deque_last_stolen = k;
1902 first = FALSE;
1903 }
1904
1905 // Check to see if this thread can proceed.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001906 if (flag == NULL || (!final_spin && flag->done_check())) {
1907 KA_TRACE(15, ("__kmp_execute_tasks_template(exit #5): T#%d spin condition satisfied\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001908 gtid) );
1909 return TRUE;
1910 }
1911 KMP_YIELD( __kmp_library == library_throughput ); // Yield before executing next task
1912
1913 // If the execution of the stolen task resulted in more tasks being
1914 // placed on our run queue, then restart the whole process.
1915 if (TCR_4(threads_data[ tid ].td.td_deque_ntasks) != 0) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001916 KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned other tasks, restart\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001917 gtid) );
1918 goto start;
1919 }
1920 }
1921
1922 // The victims's work queue is empty. If we are in the final spin loop
1923 // of the barrier, check and see if the termination condition is satisfied.
1924 // Going on and finding a new victim to steal from is expensive, as it
1925 // involves a lot of cache misses, so we definitely want to re-check the
1926 // termination condition before doing that.
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00001927#if OMP_41_ENABLED
1928 // The work queue may be empty but there might be proxy tasks still executing
1929 if (final_spin && TCR_4(current_task -> td_incomplete_child_tasks) == 0)
1930#else
1931 if (final_spin)
1932#endif
1933 {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001934 // First, decrement the #unfinished threads, if that has not already
1935 // been done. This decrement might be to the spin location, and
1936 // result in the termination condition being satisfied.
1937 if (! *thread_finished) {
1938 kmp_uint32 count = KMP_TEST_THEN_DEC32( (kmp_int32 *)unfinished_threads ) - 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001939 KA_TRACE(20, ("__kmp_execute_tasks_template(dec #3): T#%d dec unfinished_threads to %d; "
Jim Cownie5e8470a2013-09-27 10:38:44 +00001940 "task_team=%p\n",
1941 gtid, count, task_team) );
1942 *thread_finished = TRUE;
1943 }
1944
1945 // If __kmp_tasking_mode != tskm_immediate_exec,
1946 // then it is now unsafe to reference thread->th.th_team !!!
1947 // Decrementing task_team->tt.tt_unfinished_threads can allow the master
1948 // thread to pass through the barrier, where it might reset each thread's
1949 // th.th_team field for the next parallel region.
1950 // If we can steal more work, we know that this has not happened yet.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001951 if (flag != NULL && flag->done_check()) {
1952 KA_TRACE(15, ("__kmp_execute_tasks_template(exit #6): T#%d spin condition satisfied\n", gtid) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001953 return TRUE;
1954 }
1955 }
1956 }
1957
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001958 KA_TRACE(15, ("__kmp_execute_tasks_template(exit #7): T#%d can't find work\n", gtid) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001959 return FALSE;
1960}
1961
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001962int __kmp_execute_tasks_32(kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag, int final_spin,
1963 int *thread_finished
1964 USE_ITT_BUILD_ARG(void * itt_sync_obj), kmp_int32 is_constrained)
1965{
1966 return __kmp_execute_tasks_template(thread, gtid, flag, final_spin, thread_finished
1967 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
1968}
1969
1970int __kmp_execute_tasks_64(kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag, int final_spin,
1971 int *thread_finished
1972 USE_ITT_BUILD_ARG(void * itt_sync_obj), kmp_int32 is_constrained)
1973{
1974 return __kmp_execute_tasks_template(thread, gtid, flag, final_spin, thread_finished
1975 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
1976}
1977
1978int __kmp_execute_tasks_oncore(kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin,
1979 int *thread_finished
1980 USE_ITT_BUILD_ARG(void * itt_sync_obj), kmp_int32 is_constrained)
1981{
1982 return __kmp_execute_tasks_template(thread, gtid, flag, final_spin, thread_finished
1983 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
1984}
1985
1986
Jim Cownie5e8470a2013-09-27 10:38:44 +00001987
1988//-----------------------------------------------------------------------------
1989// __kmp_enable_tasking: Allocate task team and resume threads sleeping at the
1990// next barrier so they can assist in executing enqueued tasks.
1991// First thread in allocates the task team atomically.
1992
1993static void
1994__kmp_enable_tasking( kmp_task_team_t *task_team, kmp_info_t *this_thr )
1995{
1996 kmp_team_t *team = this_thr->th.th_team;
1997 kmp_thread_data_t *threads_data;
1998 int nthreads, i, is_init_thread;
1999
2000 KA_TRACE( 10, ( "__kmp_enable_tasking(enter): T#%d\n",
2001 __kmp_gtid_from_thread( this_thr ) ) );
2002
2003 KMP_DEBUG_ASSERT(task_team != NULL);
2004 KMP_DEBUG_ASSERT(team != NULL);
2005
2006 nthreads = task_team->tt.tt_nproc;
2007 KMP_DEBUG_ASSERT(nthreads > 0);
2008 KMP_DEBUG_ASSERT(nthreads == team->t.t_nproc);
2009
2010 // Allocate or increase the size of threads_data if necessary
2011 is_init_thread = __kmp_realloc_task_threads_data( this_thr, task_team );
2012
2013 if (!is_init_thread) {
2014 // Some other thread already set up the array.
2015 KA_TRACE( 20, ( "__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
2016 __kmp_gtid_from_thread( this_thr ) ) );
2017 return;
2018 }
2019 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team -> tt.tt_threads_data);
2020 KMP_DEBUG_ASSERT( threads_data != NULL );
2021
2022 if ( ( __kmp_tasking_mode == tskm_task_teams ) &&
2023 ( __kmp_dflt_blocktime != KMP_MAX_BLOCKTIME ) )
2024 {
2025 // Release any threads sleeping at the barrier, so that they can steal
2026 // tasks and execute them. In extra barrier mode, tasks do not sleep
2027 // at the separate tasking barrier, so this isn't a problem.
2028 for (i = 0; i < nthreads; i++) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002029 volatile void *sleep_loc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002030 kmp_info_t *thread = threads_data[i].td.td_thr;
2031
2032 if (i == this_thr->th.th_info.ds.ds_tid) {
2033 continue;
2034 }
2035 // Since we haven't locked the thread's suspend mutex lock at this
2036 // point, there is a small window where a thread might be putting
2037 // itself to sleep, but hasn't set the th_sleep_loc field yet.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002038 // To work around this, __kmp_execute_tasks_template() periodically checks
Jim Cownie5e8470a2013-09-27 10:38:44 +00002039 // see if other threads are sleeping (using the same random
2040 // mechanism that is used for task stealing) and awakens them if
2041 // they are.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002042 if ( ( sleep_loc = TCR_PTR( thread -> th.th_sleep_loc) ) != NULL )
Jim Cownie5e8470a2013-09-27 10:38:44 +00002043 {
2044 KF_TRACE( 50, ( "__kmp_enable_tasking: T#%d waking up thread T#%d\n",
2045 __kmp_gtid_from_thread( this_thr ),
2046 __kmp_gtid_from_thread( thread ) ) );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002047 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002048 }
2049 else {
2050 KF_TRACE( 50, ( "__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
2051 __kmp_gtid_from_thread( this_thr ),
2052 __kmp_gtid_from_thread( thread ) ) );
2053 }
2054 }
2055 }
2056
2057 KA_TRACE( 10, ( "__kmp_enable_tasking(exit): T#%d\n",
2058 __kmp_gtid_from_thread( this_thr ) ) );
2059}
2060
2061
2062/* ------------------------------------------------------------------------ */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002063/* // TODO: Check the comment consistency
Jim Cownie5e8470a2013-09-27 10:38:44 +00002064 * Utility routines for "task teams". A task team (kmp_task_t) is kind of
2065 * like a shadow of the kmp_team_t data struct, with a different lifetime.
2066 * After a child * thread checks into a barrier and calls __kmp_release() from
2067 * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no
2068 * longer assume that the kmp_team_t structure is intact (at any moment, the
2069 * master thread may exit the barrier code and free the team data structure,
2070 * and return the threads to the thread pool).
2071 *
2072 * This does not work with the the tasking code, as the thread is still
2073 * expected to participate in the execution of any tasks that may have been
2074 * spawned my a member of the team, and the thread still needs access to all
2075 * to each thread in the team, so that it can steal work from it.
2076 *
2077 * Enter the existence of the kmp_task_team_t struct. It employs a reference
2078 * counting mechanims, and is allocated by the master thread before calling
2079 * __kmp_<barrier_kind>_release, and then is release by the last thread to
2080 * exit __kmp_<barrier_kind>_release at the next barrier. I.e. the lifetimes
2081 * of the kmp_task_team_t structs for consecutive barriers can overlap
2082 * (and will, unless the master thread is the last thread to exit the barrier
2083 * release phase, which is not typical).
2084 *
2085 * The existence of such a struct is useful outside the context of tasking,
2086 * but for now, I'm trying to keep it specific to the OMP_30_ENABLED macro,
2087 * so that any performance differences show up when comparing the 2.5 vs. 3.0
2088 * libraries.
2089 *
2090 * We currently use the existence of the threads array as an indicator that
2091 * tasks were spawned since the last barrier. If the structure is to be
2092 * useful outside the context of tasking, then this will have to change, but
2093 * not settting the field minimizes the performance impact of tasking on
2094 * barriers, when no explicit tasks were spawned (pushed, actually).
2095 */
2096
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002097
Jim Cownie5e8470a2013-09-27 10:38:44 +00002098static kmp_task_team_t *__kmp_free_task_teams = NULL; // Free list for task_team data structures
2099// Lock for task team data structures
2100static kmp_bootstrap_lock_t __kmp_task_team_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER( __kmp_task_team_lock );
2101
2102
2103//------------------------------------------------------------------------------
2104// __kmp_alloc_task_deque:
2105// Allocates a task deque for a particular thread, and initialize the necessary
2106// data structures relating to the deque. This only happens once per thread
2107// per task team since task teams are recycled.
2108// No lock is needed during allocation since each thread allocates its own
2109// deque.
2110
2111static void
2112__kmp_alloc_task_deque( kmp_info_t *thread, kmp_thread_data_t *thread_data )
2113{
2114 __kmp_init_bootstrap_lock( & thread_data -> td.td_deque_lock );
2115 KMP_DEBUG_ASSERT( thread_data -> td.td_deque == NULL );
2116
2117 // Initialize last stolen task field to "none"
2118 thread_data -> td.td_deque_last_stolen = -1;
2119
2120 KMP_DEBUG_ASSERT( TCR_4(thread_data -> td.td_deque_ntasks) == 0 );
2121 KMP_DEBUG_ASSERT( thread_data -> td.td_deque_head == 0 );
2122 KMP_DEBUG_ASSERT( thread_data -> td.td_deque_tail == 0 );
2123
2124 KE_TRACE( 10, ( "__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
2125 __kmp_gtid_from_thread( thread ), TASK_DEQUE_SIZE, thread_data ) );
2126 // Allocate space for task deque, and zero the deque
2127 // Cannot use __kmp_thread_calloc() because threads not around for
2128 // kmp_reap_task_team( ).
2129 thread_data -> td.td_deque = (kmp_taskdata_t **)
2130 __kmp_allocate( TASK_DEQUE_SIZE * sizeof(kmp_taskdata_t *));
2131}
2132
2133
2134//------------------------------------------------------------------------------
2135// __kmp_free_task_deque:
2136// Deallocates a task deque for a particular thread.
2137// Happens at library deallocation so don't need to reset all thread data fields.
2138
2139static void
2140__kmp_free_task_deque( kmp_thread_data_t *thread_data )
2141{
2142 __kmp_acquire_bootstrap_lock( & thread_data -> td.td_deque_lock );
2143
2144 if ( thread_data -> td.td_deque != NULL ) {
2145 TCW_4(thread_data -> td.td_deque_ntasks, 0);
2146 __kmp_free( thread_data -> td.td_deque );
2147 thread_data -> td.td_deque = NULL;
2148 }
2149 __kmp_release_bootstrap_lock( & thread_data -> td.td_deque_lock );
2150
2151#ifdef BUILD_TIED_TASK_STACK
2152 // GEH: Figure out what to do here for td_susp_tied_tasks
2153 if ( thread_data -> td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY ) {
2154 __kmp_free_task_stack( __kmp_thread_from_gtid( gtid ), thread_data );
2155 }
2156#endif // BUILD_TIED_TASK_STACK
2157}
2158
2159
2160//------------------------------------------------------------------------------
2161// __kmp_realloc_task_threads_data:
2162// Allocates a threads_data array for a task team, either by allocating an initial
2163// array or enlarging an existing array. Only the first thread to get the lock
2164// allocs or enlarges the array and re-initializes the array eleemnts.
2165// That thread returns "TRUE", the rest return "FALSE".
2166// Assumes that the new array size is given by task_team -> tt.tt_nproc.
2167// The current size is given by task_team -> tt.tt_max_threads.
2168
2169static int
2170__kmp_realloc_task_threads_data( kmp_info_t *thread, kmp_task_team_t *task_team )
2171{
2172 kmp_thread_data_t ** threads_data_p;
2173 kmp_int32 nthreads, maxthreads;
2174 int is_init_thread = FALSE;
2175
2176 if ( TCR_4(task_team -> tt.tt_found_tasks) ) {
2177 // Already reallocated and initialized.
2178 return FALSE;
2179 }
2180
2181 threads_data_p = & task_team -> tt.tt_threads_data;
2182 nthreads = task_team -> tt.tt_nproc;
2183 maxthreads = task_team -> tt.tt_max_threads;
2184
2185 // All threads must lock when they encounter the first task of the implicit task
2186 // region to make sure threads_data fields are (re)initialized before used.
2187 __kmp_acquire_bootstrap_lock( & task_team -> tt.tt_threads_lock );
2188
2189 if ( ! TCR_4(task_team -> tt.tt_found_tasks) ) {
2190 // first thread to enable tasking
2191 kmp_team_t *team = thread -> th.th_team;
2192 int i;
2193
2194 is_init_thread = TRUE;
2195 if ( maxthreads < nthreads ) {
2196
2197 if ( *threads_data_p != NULL ) {
2198 kmp_thread_data_t *old_data = *threads_data_p;
2199 kmp_thread_data_t *new_data = NULL;
2200
2201 KE_TRACE( 10, ( "__kmp_realloc_task_threads_data: T#%d reallocating "
2202 "threads data for task_team %p, new_size = %d, old_size = %d\n",
2203 __kmp_gtid_from_thread( thread ), task_team,
2204 nthreads, maxthreads ) );
2205 // Reallocate threads_data to have more elements than current array
2206 // Cannot use __kmp_thread_realloc() because threads not around for
2207 // kmp_reap_task_team( ). Note all new array entries are initialized
2208 // to zero by __kmp_allocate().
2209 new_data = (kmp_thread_data_t *)
2210 __kmp_allocate( nthreads * sizeof(kmp_thread_data_t) );
2211 // copy old data to new data
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002212 KMP_MEMCPY_S( (void *) new_data, nthreads * sizeof(kmp_thread_data_t),
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002213 (void *) old_data,
2214 maxthreads * sizeof(kmp_taskdata_t *) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002215
2216#ifdef BUILD_TIED_TASK_STACK
2217 // GEH: Figure out if this is the right thing to do
2218 for (i = maxthreads; i < nthreads; i++) {
2219 kmp_thread_data_t *thread_data = & (*threads_data_p)[i];
2220 __kmp_init_task_stack( __kmp_gtid_from_thread( thread ), thread_data );
2221 }
2222#endif // BUILD_TIED_TASK_STACK
2223 // Install the new data and free the old data
2224 (*threads_data_p) = new_data;
2225 __kmp_free( old_data );
2226 }
2227 else {
2228 KE_TRACE( 10, ( "__kmp_realloc_task_threads_data: T#%d allocating "
2229 "threads data for task_team %p, size = %d\n",
2230 __kmp_gtid_from_thread( thread ), task_team, nthreads ) );
2231 // Make the initial allocate for threads_data array, and zero entries
2232 // Cannot use __kmp_thread_calloc() because threads not around for
2233 // kmp_reap_task_team( ).
2234 *threads_data_p = (kmp_thread_data_t *)
2235 __kmp_allocate( nthreads * sizeof(kmp_thread_data_t) );
2236#ifdef BUILD_TIED_TASK_STACK
2237 // GEH: Figure out if this is the right thing to do
2238 for (i = 0; i < nthreads; i++) {
2239 kmp_thread_data_t *thread_data = & (*threads_data_p)[i];
2240 __kmp_init_task_stack( __kmp_gtid_from_thread( thread ), thread_data );
2241 }
2242#endif // BUILD_TIED_TASK_STACK
2243 }
2244 task_team -> tt.tt_max_threads = nthreads;
2245 }
2246 else {
2247 // If array has (more than) enough elements, go ahead and use it
2248 KMP_DEBUG_ASSERT( *threads_data_p != NULL );
2249 }
2250
2251 // initialize threads_data pointers back to thread_info structures
2252 for (i = 0; i < nthreads; i++) {
2253 kmp_thread_data_t *thread_data = & (*threads_data_p)[i];
2254 thread_data -> td.td_thr = team -> t.t_threads[i];
2255
2256 if ( thread_data -> td.td_deque_last_stolen >= nthreads) {
2257 // The last stolen field survives across teams / barrier, and the number
2258 // of threads may have changed. It's possible (likely?) that a new
2259 // parallel region will exhibit the same behavior as the previous region.
2260 thread_data -> td.td_deque_last_stolen = -1;
2261 }
2262 }
2263
2264 KMP_MB();
2265 TCW_SYNC_4(task_team -> tt.tt_found_tasks, TRUE);
2266 }
2267
2268 __kmp_release_bootstrap_lock( & task_team -> tt.tt_threads_lock );
2269 return is_init_thread;
2270}
2271
2272
2273//------------------------------------------------------------------------------
2274// __kmp_free_task_threads_data:
2275// Deallocates a threads_data array for a task team, including any attached
2276// tasking deques. Only occurs at library shutdown.
2277
2278static void
2279__kmp_free_task_threads_data( kmp_task_team_t *task_team )
2280{
2281 __kmp_acquire_bootstrap_lock( & task_team -> tt.tt_threads_lock );
2282 if ( task_team -> tt.tt_threads_data != NULL ) {
2283 int i;
2284 for (i = 0; i < task_team->tt.tt_max_threads; i++ ) {
2285 __kmp_free_task_deque( & task_team -> tt.tt_threads_data[i] );
2286 }
2287 __kmp_free( task_team -> tt.tt_threads_data );
2288 task_team -> tt.tt_threads_data = NULL;
2289 }
2290 __kmp_release_bootstrap_lock( & task_team -> tt.tt_threads_lock );
2291}
2292
2293
2294//------------------------------------------------------------------------------
2295// __kmp_allocate_task_team:
2296// Allocates a task team associated with a specific team, taking it from
2297// the global task team free list if possible. Also initializes data structures.
2298
2299static kmp_task_team_t *
2300__kmp_allocate_task_team( kmp_info_t *thread, kmp_team_t *team )
2301{
2302 kmp_task_team_t *task_team = NULL;
2303 int nthreads;
2304
2305 KA_TRACE( 20, ( "__kmp_allocate_task_team: T#%d entering; team = %p\n",
2306 (thread ? __kmp_gtid_from_thread( thread ) : -1), team ) );
2307
2308 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
2309 // Take a task team from the task team pool
2310 __kmp_acquire_bootstrap_lock( &__kmp_task_team_lock );
2311 if (__kmp_free_task_teams != NULL) {
2312 task_team = __kmp_free_task_teams;
2313 TCW_PTR(__kmp_free_task_teams, task_team -> tt.tt_next);
2314 task_team -> tt.tt_next = NULL;
2315 }
2316 __kmp_release_bootstrap_lock( &__kmp_task_team_lock );
2317 }
2318
2319 if (task_team == NULL) {
2320 KE_TRACE( 10, ( "__kmp_allocate_task_team: T#%d allocating "
2321 "task team for team %p\n",
2322 __kmp_gtid_from_thread( thread ), team ) );
2323 // Allocate a new task team if one is not available.
2324 // Cannot use __kmp_thread_malloc() because threads not around for
2325 // kmp_reap_task_team( ).
2326 task_team = (kmp_task_team_t *) __kmp_allocate( sizeof(kmp_task_team_t) );
2327 __kmp_init_bootstrap_lock( & task_team -> tt.tt_threads_lock );
2328 //task_team -> tt.tt_threads_data = NULL; // AC: __kmp_allocate zeroes returned memory
2329 //task_team -> tt.tt_max_threads = 0;
2330 //task_team -> tt.tt_next = NULL;
2331 }
2332
2333 TCW_4(task_team -> tt.tt_found_tasks, FALSE);
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002334#if OMP_41_ENABLED
2335 TCW_4(task_team -> tt.tt_found_proxy_tasks, FALSE);
2336#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002337 task_team -> tt.tt_nproc = nthreads = team->t.t_nproc;
2338
Jim Cownie5e8470a2013-09-27 10:38:44 +00002339 TCW_4( task_team -> tt.tt_unfinished_threads, nthreads );
2340 TCW_4( task_team -> tt.tt_active, TRUE );
2341 TCW_4( task_team -> tt.tt_ref_ct, nthreads - 1);
2342
2343 KA_TRACE( 20, ( "__kmp_allocate_task_team: T#%d exiting; task_team = %p\n",
2344 (thread ? __kmp_gtid_from_thread( thread ) : -1), task_team ) );
2345 return task_team;
2346}
2347
2348
2349//------------------------------------------------------------------------------
2350// __kmp_free_task_team:
2351// Frees the task team associated with a specific thread, and adds it
2352// to the global task team free list.
2353//
2354
2355static void
2356__kmp_free_task_team( kmp_info_t *thread, kmp_task_team_t *task_team )
2357{
2358 KA_TRACE( 20, ( "__kmp_free_task_team: T#%d task_team = %p\n",
2359 thread ? __kmp_gtid_from_thread( thread ) : -1, task_team ) );
2360
2361 KMP_DEBUG_ASSERT( TCR_4(task_team -> tt.tt_ref_ct) == 0 );
2362
2363 // Put task team back on free list
2364 __kmp_acquire_bootstrap_lock( & __kmp_task_team_lock );
2365
2366 KMP_DEBUG_ASSERT( task_team -> tt.tt_next == NULL );
2367 task_team -> tt.tt_next = __kmp_free_task_teams;
2368 TCW_4(task_team -> tt.tt_found_tasks, FALSE);
2369 TCW_PTR(__kmp_free_task_teams, task_team);
2370
2371 __kmp_release_bootstrap_lock( & __kmp_task_team_lock );
2372}
2373
2374
2375//------------------------------------------------------------------------------
2376// __kmp_reap_task_teams:
2377// Free all the task teams on the task team free list.
2378// Should only be done during library shutdown.
2379// Cannot do anything that needs a thread structure or gtid since they are already gone.
2380
2381void
2382__kmp_reap_task_teams( void )
2383{
2384 kmp_task_team_t *task_team;
2385
2386 if ( TCR_PTR(__kmp_free_task_teams) != NULL ) {
2387 // Free all task_teams on the free list
2388 __kmp_acquire_bootstrap_lock( &__kmp_task_team_lock );
2389 while ( ( task_team = __kmp_free_task_teams ) != NULL ) {
2390 __kmp_free_task_teams = task_team -> tt.tt_next;
2391 task_team -> tt.tt_next = NULL;
2392
2393 // Free threads_data if necessary
2394 if ( task_team -> tt.tt_threads_data != NULL ) {
2395 __kmp_free_task_threads_data( task_team );
2396 }
2397 __kmp_free( task_team );
2398 }
2399 __kmp_release_bootstrap_lock( &__kmp_task_team_lock );
2400 }
2401}
2402
2403
2404//------------------------------------------------------------------------------
2405// __kmp_unref_task_teams:
2406// Remove one thread from referencing the task team structure by
2407// decreasing the reference count and deallocate task team if no more
2408// references to it.
2409//
2410void
2411__kmp_unref_task_team( kmp_task_team_t *task_team, kmp_info_t *thread )
2412{
2413 kmp_uint ref_ct;
2414
2415 ref_ct = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& task_team->tt.tt_ref_ct) ) - 1;
2416
2417 KA_TRACE( 20, ( "__kmp_unref_task_team: T#%d task_team = %p ref_ct = %d\n",
2418 __kmp_gtid_from_thread( thread ), task_team, ref_ct ) );
2419
2420
2421 if ( ref_ct == 0 ) {
2422 __kmp_free_task_team( thread, task_team );
2423 }
2424
2425 TCW_PTR( *((volatile kmp_task_team_t **)(&thread->th.th_task_team)), NULL );
2426}
2427
2428
2429//------------------------------------------------------------------------------
2430// __kmp_wait_to_unref_task_teams:
2431// Some threads could still be in the fork barrier release code, possibly
2432// trying to steal tasks. Wait for each thread to unreference its task team.
2433//
2434void
2435__kmp_wait_to_unref_task_teams(void)
2436{
2437 kmp_info_t *thread;
2438 kmp_uint32 spins;
2439 int done;
2440
2441 KMP_INIT_YIELD( spins );
2442
2443
2444 for (;;) {
2445 done = TRUE;
2446
2447 // TODO: GEH - this may be is wrong because some sync would be necessary
2448 // in case threads are added to the pool during the traversal.
2449 // Need to verify that lock for thread pool is held when calling
2450 // this routine.
2451 for (thread = (kmp_info_t *)__kmp_thread_pool;
2452 thread != NULL;
2453 thread = thread->th.th_next_pool)
2454 {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002455#if KMP_OS_WINDOWS
2456 DWORD exit_val;
2457#endif
2458 if ( TCR_PTR(thread->th.th_task_team) == NULL ) {
2459 KA_TRACE( 10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
2460 __kmp_gtid_from_thread( thread ) ) );
2461 continue;
2462 }
2463#if KMP_OS_WINDOWS
2464 // TODO: GEH - add this check for Linux* OS / OS X* as well?
2465 if (!__kmp_is_thread_alive(thread, &exit_val)) {
2466 if (TCR_PTR(thread->th.th_task_team) != NULL) {
2467 __kmp_unref_task_team( thread->th.th_task_team, thread );
2468 }
2469 continue;
2470 }
2471#endif
2472
2473 done = FALSE; // Because th_task_team pointer is not NULL for this thread
2474
2475 KA_TRACE( 10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to unreference task_team\n",
2476 __kmp_gtid_from_thread( thread ) ) );
2477
2478 if ( __kmp_dflt_blocktime != KMP_MAX_BLOCKTIME ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002479 volatile void *sleep_loc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002480 // If the thread is sleeping, awaken it.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002481 if ( ( sleep_loc = TCR_PTR( thread->th.th_sleep_loc) ) != NULL ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002482 KA_TRACE( 10, ( "__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
2483 __kmp_gtid_from_thread( thread ), __kmp_gtid_from_thread( thread ) ) );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002484 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002485 }
2486 }
2487 }
2488 if (done) {
2489 break;
2490 }
2491
2492 // If we are oversubscribed,
2493 // or have waited a bit (and library mode is throughput), yield.
2494 // Pause is in the following code.
2495 KMP_YIELD( TCR_4(__kmp_nth) > __kmp_avail_proc );
2496 KMP_YIELD_SPIN( spins ); // Yields only if KMP_LIBRARY=throughput
2497 }
2498
2499
2500}
2501
2502
2503//------------------------------------------------------------------------------
2504// __kmp_task_team_setup: Create a task_team for the current team, but use
2505// an already created, unused one if it already exists.
2506// This may be called by any thread, but only for teams with # threads >1.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002507void
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002508__kmp_task_team_setup( kmp_info_t *this_thr, kmp_team_t *team, int both, int always )
Jim Cownie5e8470a2013-09-27 10:38:44 +00002509{
2510 KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
2511
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002512 if ( ( team->t.t_task_team[this_thr->th.th_task_state] == NULL ) && ( always || team->t.t_nproc > 1 ) ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002513 // Allocate a new task team, which will be propagated to
2514 // all of the worker threads after the barrier. As they
2515 // spin in the barrier release phase, then will continue
2516 // to use the previous task team struct, until they receive
2517 // the signal to stop checking for tasks (they can't safely
2518 // reference the kmp_team_t struct, which could be reallocated
2519 // by the master thread).
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002520 team->t.t_task_team[this_thr->th.th_task_state] = __kmp_allocate_task_team( this_thr, team );
2521 KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created new task_team %p for team %d\n",
2522 __kmp_gtid_from_thread(this_thr), team->t.t_task_team[this_thr->th.th_task_state],
Jim Cownie5e8470a2013-09-27 10:38:44 +00002523 ((team != NULL) ? team->t.t_id : -1)) );
2524 }
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002525 //else
Jim Cownie5e8470a2013-09-27 10:38:44 +00002526 // All threads have reported in, and no tasks were spawned
2527 // for this release->gather region. Leave the old task
2528 // team struct in place for the upcoming region. No task
2529 // teams are formed for serialized teams.
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002530 if (both) {
2531 int other_team = 1 - this_thr->th.th_task_state;
2532 if ( ( team->t.t_task_team[other_team] == NULL ) && ( team->t.t_nproc > 1 ) ) { // setup other team as well
2533 team->t.t_task_team[other_team] = __kmp_allocate_task_team( this_thr, team );
2534 KA_TRACE( 20, ( "__kmp_task_team_setup: Master T#%d created new task_team %p for team %d\n",
2535 __kmp_gtid_from_thread( this_thr ), team->t.t_task_team[other_team],
2536 ((team != NULL) ? team->t.t_id : -1)) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002537 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002538 }
2539}
2540
2541
2542//------------------------------------------------------------------------------
2543// __kmp_task_team_sync: Propagation of task team data from team to threads
2544// which happens just after the release phase of a team barrier. This may be
2545// called by any thread, but only for teams with # threads > 1.
2546
2547void
2548__kmp_task_team_sync( kmp_info_t *this_thr, kmp_team_t *team )
2549{
2550 KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
2551
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002552 // In case this thread never saw that the task team was no longer active, unref/deallocate it now.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002553 if ( this_thr->th.th_task_team != NULL ) {
2554 if ( ! TCR_SYNC_4( this_thr->th.th_task_team->tt.tt_active ) ) {
2555 KMP_DEBUG_ASSERT( ! KMP_MASTER_TID( __kmp_tid_from_gtid( __kmp_gtid_from_thread( this_thr ) ) ) );
2556 __kmp_unref_task_team( this_thr->th.th_task_team, this_thr );
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002557 } else { // We are re-using a task team that was never enabled.
2558 KMP_DEBUG_ASSERT(this_thr->th.th_task_team == team->t.t_task_team[this_thr->th.th_task_state]);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002559 }
2560 }
2561
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002562 // Toggle the th_task_state field, to switch which task_team this thread refers to
Jim Cownie5e8470a2013-09-27 10:38:44 +00002563 this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002564 // It is now safe to propagate the task team pointer from the team struct to the current thread.
2565 TCW_PTR(this_thr->th.th_task_team, team->t.t_task_team[this_thr->th.th_task_state]);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002566 KA_TRACE( 20, ( "__kmp_task_team_sync: Thread T#%d task team assigned pointer (%p) from Team #%d task team\n",
2567 __kmp_gtid_from_thread( this_thr ), &this_thr->th.th_task_team,
2568 this_thr->th.th_task_team, ((team != NULL) ? (team->t.t_id) : -1) ) );
2569}
2570
2571
2572//------------------------------------------------------------------------------
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002573// __kmp_task_team_wait: Master thread waits for outstanding tasks after the
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002574// barrier gather phase. Only called by master thread if #threads in team > 1 or if proxy tasks were created
Jim Cownie5e8470a2013-09-27 10:38:44 +00002575void
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002576__kmp_task_team_wait( kmp_info_t *this_thr, kmp_team_t *team
Jim Cownie181b4bb2013-12-23 17:28:57 +00002577 USE_ITT_BUILD_ARG(void * itt_sync_obj)
Jim Cownie5e8470a2013-09-27 10:38:44 +00002578 )
2579{
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002580 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002581
2582 KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
2583 KMP_DEBUG_ASSERT( task_team == this_thr->th.th_task_team );
2584
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002585 if ( ( task_team != NULL ) && KMP_TASKING_ENABLED(task_team) ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002586 KA_TRACE( 20, ( "__kmp_task_team_wait: Master T#%d waiting for all tasks: task_team = %p\n",
2587 __kmp_gtid_from_thread( this_thr ), task_team ) );
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002588 // All worker threads might have dropped through to the release phase, but could still
2589 // be executing tasks. Wait here for all tasks to complete. To avoid memory contention,
2590 // only the master thread checks for the termination condition.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002591 kmp_flag_32 flag(&task_team->tt.tt_unfinished_threads, 0U);
2592 flag.wait(this_thr, TRUE
2593 USE_ITT_BUILD_ARG(itt_sync_obj));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002594
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002595 // Kill the old task team, so that the worker threads will stop referencing it while spinning.
2596 // They will deallocate it when the reference count reaches zero.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002597 // The master thread is not included in the ref count.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002598 KA_TRACE( 20, ( "__kmp_task_team_wait: Master T#%d deactivating task_team %p\n",
2599 __kmp_gtid_from_thread( this_thr ), task_team ) );
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002600#if OMP_41_ENABLED
2601 KMP_DEBUG_ASSERT( task_team->tt.tt_nproc > 1 || task_team->tt.tt_found_proxy_tasks == TRUE );
2602 TCW_SYNC_4( task_team->tt.tt_found_proxy_tasks, FALSE );
2603#else
Jim Cownie5e8470a2013-09-27 10:38:44 +00002604 KMP_DEBUG_ASSERT( task_team->tt.tt_nproc > 1 );
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002605#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002606 TCW_SYNC_4( task_team->tt.tt_active, FALSE );
2607 KMP_MB();
2608
2609 TCW_PTR(this_thr->th.th_task_team, NULL);
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002610 team->t.t_task_team[this_thr->th.th_task_state] = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002611 }
2612}
2613
2614
2615//------------------------------------------------------------------------------
2616// __kmp_tasking_barrier:
2617// Internal function to execute all tasks prior to a regular barrier or a
2618// join barrier. It is a full barrier itself, which unfortunately turns
2619// regular barriers into double barriers and join barriers into 1 1/2
2620// barriers.
2621// This routine may only called when __kmp_tasking_mode == tskm_extra_barrier.
2622
2623void
2624__kmp_tasking_barrier( kmp_team_t *team, kmp_info_t *thread, int gtid )
2625{
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002626 volatile kmp_uint32 *spin = &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002627 int flag = FALSE;
2628 KMP_DEBUG_ASSERT( __kmp_tasking_mode == tskm_extra_barrier );
2629
2630#if USE_ITT_BUILD
2631 KMP_FSYNC_SPIN_INIT( spin, (kmp_uint32*) NULL );
2632#endif /* USE_ITT_BUILD */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002633 kmp_flag_32 spin_flag(spin, 0U);
2634 while (! spin_flag.execute_tasks(thread, gtid, TRUE, &flag
2635 USE_ITT_BUILD_ARG(NULL), 0 ) ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002636#if USE_ITT_BUILD
2637 // TODO: What about itt_sync_obj??
2638 KMP_FSYNC_SPIN_PREPARE( spin );
2639#endif /* USE_ITT_BUILD */
2640
2641 if( TCR_4(__kmp_global.g.g_done) ) {
2642 if( __kmp_global.g.g_abort )
2643 __kmp_abort_thread( );
2644 break;
2645 }
2646 KMP_YIELD( TRUE ); // GH: We always yield here
2647 }
2648#if USE_ITT_BUILD
2649 KMP_FSYNC_SPIN_ACQUIRED( (void*) spin );
2650#endif /* USE_ITT_BUILD */
2651}
2652
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002653
2654#if OMP_41_ENABLED
2655
2656/* __kmp_give_task puts a task into a given thread queue if:
2657 - the queue for that thread it was created
2658 - there's space in that queue
2659
2660 Because of this, __kmp_push_task needs to check if there's space after getting the lock
2661 */
2662static bool __kmp_give_task ( kmp_info_t *thread, kmp_int32 tid, kmp_task_t * task )
2663{
2664 kmp_task_team_t * task_team = thread->th.th_task_team;
2665 kmp_thread_data_t * thread_data = & task_team -> tt.tt_threads_data[ tid ];
2666 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
2667 bool result = false;
2668
2669 KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n", taskdata, tid ) );
2670
2671 // assert tasking is enabled? what if not?
2672 KMP_DEBUG_ASSERT( task_team != NULL );
2673
2674 if (thread_data -> td.td_deque == NULL ) {
2675 // There's no queue in this thread, go find another one
2676 // We're guaranteed that at least one thread has a queue
2677 KA_TRACE(30, ("__kmp_give_task: thread %d has no queue while giving task %p.\n", tid, taskdata ) );
2678 return result;
2679 }
2680
2681 if ( TCR_4(thread_data -> td.td_deque_ntasks) >= TASK_DEQUE_SIZE )
2682 {
2683 KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to thread %d.\n", taskdata, tid ) );
2684 return result;
2685 }
2686
2687 __kmp_acquire_bootstrap_lock( & thread_data-> td.td_deque_lock );
2688
2689 if ( TCR_4(thread_data -> td.td_deque_ntasks) >= TASK_DEQUE_SIZE )
2690 {
2691 KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to thread %d.\n", taskdata, tid ) );
2692 goto release_and_exit;
2693 }
2694
2695 thread_data -> td.td_deque[ thread_data -> td.td_deque_tail ] = taskdata;
2696 // Wrap index.
2697 thread_data -> td.td_deque_tail = ( thread_data -> td.td_deque_tail + 1 ) & TASK_DEQUE_MASK;
2698 TCW_4(thread_data -> td.td_deque_ntasks, TCR_4(thread_data -> td.td_deque_ntasks) + 1);
2699
2700 result = true;
Jonathan Peyton1406f012015-05-22 22:35:51 +00002701 KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n", taskdata, tid ) );
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002702
2703release_and_exit:
2704 __kmp_release_bootstrap_lock( & thread_data-> td.td_deque_lock );
2705
2706 return result;
2707}
2708
2709
2710/* The finish of the a proxy tasks is divided in two pieces:
2711 - the top half is the one that can be done from a thread outside the team
2712 - the bottom half must be run from a them within the team
2713
2714 In order to run the bottom half the task gets queued back into one of the threads of the team.
2715 Once the td_incomplete_child_task counter of the parent is decremented the threads can leave the barriers.
2716 So, the bottom half needs to be queued before the counter is decremented. The top half is therefore divided in two parts:
2717 - things that can be run before queuing the bottom half
2718 - things that must be run after queuing the bottom half
2719
2720 This creates a second race as the bottom half can free the task before the second top half is executed. To avoid this
2721 we use the td_incomplete_child_task of the proxy task to synchronize the top and bottom half.
2722*/
2723
2724static void __kmp_first_top_half_finish_proxy( kmp_taskdata_t * taskdata )
2725{
2726 KMP_DEBUG_ASSERT( taskdata -> td_flags.tasktype == TASK_EXPLICIT );
2727 KMP_DEBUG_ASSERT( taskdata -> td_flags.proxy == TASK_PROXY );
2728 KMP_DEBUG_ASSERT( taskdata -> td_flags.complete == 0 );
2729 KMP_DEBUG_ASSERT( taskdata -> td_flags.freed == 0 );
2730
2731 taskdata -> td_flags.complete = 1; // mark the task as completed
2732
2733 if ( taskdata->td_taskgroup )
2734 KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata->td_taskgroup->count) );
2735
2736 // Create an imaginary children for this task so the bottom half cannot release the task before we have completed the second top half
2737 TCR_4(taskdata->td_incomplete_child_tasks++);
2738}
2739
2740static void __kmp_second_top_half_finish_proxy( kmp_taskdata_t * taskdata )
2741{
2742 kmp_int32 children = 0;
2743
2744 // Predecrement simulated by "- 1" calculation
2745 children = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata -> td_parent -> td_incomplete_child_tasks) ) - 1;
2746 KMP_DEBUG_ASSERT( children >= 0 );
2747
2748 // Remove the imaginary children
2749 TCR_4(taskdata->td_incomplete_child_tasks--);
2750}
2751
2752static void __kmp_bottom_half_finish_proxy( kmp_int32 gtid, kmp_task_t * ptask )
2753{
2754 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(ptask);
2755 kmp_info_t * thread = __kmp_threads[ gtid ];
2756
2757 KMP_DEBUG_ASSERT( taskdata -> td_flags.proxy == TASK_PROXY );
2758 KMP_DEBUG_ASSERT( taskdata -> td_flags.complete == 1 ); // top half must run before bottom half
2759
2760 // We need to wait to make sure the top half is finished
2761 // Spinning here should be ok as this should happen quickly
2762 while ( TCR_4(taskdata->td_incomplete_child_tasks) > 0 ) ;
2763
2764 __kmp_release_deps(gtid,taskdata);
2765 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
2766}
2767
2768/*!
2769@ingroup TASKING
2770@param gtid Global Thread ID of encountering thread
2771@param ptask Task which execution is completed
2772
2773Execute the completation of a proxy task from a thread of that is part of the team. Run first and bottom halves directly.
2774*/
2775void __kmpc_proxy_task_completed( kmp_int32 gtid, kmp_task_t *ptask )
2776{
2777 KMP_DEBUG_ASSERT( ptask != NULL );
2778 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(ptask);
2779 KA_TRACE(10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n", gtid, taskdata ) );
2780
2781 KMP_DEBUG_ASSERT( taskdata->td_flags.proxy == TASK_PROXY );
2782
2783 __kmp_first_top_half_finish_proxy(taskdata);
2784 __kmp_second_top_half_finish_proxy(taskdata);
2785 __kmp_bottom_half_finish_proxy(gtid,ptask);
2786
2787 KA_TRACE(10, ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n", gtid, taskdata ) );
2788}
2789
2790/*!
2791@ingroup TASKING
2792@param ptask Task which execution is completed
2793
2794Execute the completation of a proxy task from a thread that could not belong to the team.
2795*/
2796void __kmpc_proxy_task_completed_ooo ( kmp_task_t *ptask )
2797{
2798 KMP_DEBUG_ASSERT( ptask != NULL );
2799 kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(ptask);
2800
2801 KA_TRACE(10, ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n", taskdata ) );
2802
2803 KMP_DEBUG_ASSERT( taskdata->td_flags.proxy == TASK_PROXY );
2804
2805 __kmp_first_top_half_finish_proxy(taskdata);
2806
2807 // Enqueue task to complete bottom half completation from a thread within the corresponding team
2808 kmp_team_t * team = taskdata->td_team;
2809 kmp_int32 nthreads = team->t.t_nproc;
2810 kmp_info_t *thread;
2811 kmp_int32 k = 0;
2812
2813 do {
Jonathan Peyton1406f012015-05-22 22:35:51 +00002814 //This should be similar to k = __kmp_get_random( thread ) % nthreads but we cannot use __kmp_get_random here
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00002815 //For now we're just linearly trying to find a thread
2816 k = (k+1) % nthreads;
2817 thread = team->t.t_threads[k];
2818 } while ( !__kmp_give_task( thread, k, ptask ) );
2819
2820 __kmp_second_top_half_finish_proxy(taskdata);
2821
2822 KA_TRACE(10, ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n", taskdata ) );
2823}
2824
2825#endif