-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
kmp_tasking.c
3098 lines (2609 loc) · 131 KB
/
kmp_tasking.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* kmp_tasking.c -- OpenMP 3.0 tasking support.
*/
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//
#include "kmp.h"
#include "kmp_i18n.h"
#include "kmp_itt.h"
#include "kmp_wait_release.h"
#include "kmp_stats.h"
#if OMPT_SUPPORT
#include "ompt-specific.h"
#endif
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* forward declaration */
static void __kmp_enable_tasking( kmp_task_team_t *task_team, kmp_info_t *this_thr );
static void __kmp_alloc_task_deque( kmp_info_t *thread, kmp_thread_data_t *thread_data );
static int __kmp_realloc_task_threads_data( kmp_info_t *thread, kmp_task_team_t *task_team );
#ifdef OMP_45_ENABLED
static void __kmp_bottom_half_finish_proxy( kmp_int32 gtid, kmp_task_t * ptask );
#endif
#ifdef BUILD_TIED_TASK_STACK
//---------------------------------------------------------------------------
// __kmp_trace_task_stack: print the tied tasks from the task stack in order
// from top do bottom
//
// gtid: global thread identifier for thread containing stack
// thread_data: thread data for task team thread containing stack
// threshold: value above which the trace statement triggers
// location: string identifying call site of this function (for trace)
static void
__kmp_trace_task_stack( kmp_int32 gtid, kmp_thread_data_t *thread_data, int threshold, char *location )
{
kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks;
kmp_taskdata_t **stack_top = task_stack -> ts_top;
kmp_int32 entries = task_stack -> ts_entries;
kmp_taskdata_t *tied_task;
KA_TRACE(threshold, ("__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
"first_block = %p, stack_top = %p \n",
location, gtid, entries, task_stack->ts_first_block, stack_top ) );
KMP_DEBUG_ASSERT( stack_top != NULL );
KMP_DEBUG_ASSERT( entries > 0 );
while ( entries != 0 )
{
KMP_DEBUG_ASSERT( stack_top != & task_stack->ts_first_block.sb_block[0] );
// fix up ts_top if we need to pop from previous block
if ( entries & TASK_STACK_INDEX_MASK == 0 )
{
kmp_stack_block_t *stack_block = (kmp_stack_block_t *) (stack_top) ;
stack_block = stack_block -> sb_prev;
stack_top = & stack_block -> sb_block[TASK_STACK_BLOCK_SIZE];
}
// finish bookkeeping
stack_top--;
entries--;
tied_task = * stack_top;
KMP_DEBUG_ASSERT( tied_task != NULL );
KMP_DEBUG_ASSERT( tied_task -> td_flags.tasktype == TASK_TIED );
KA_TRACE(threshold, ("__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
"stack_top=%p, tied_task=%p\n",
location, gtid, entries, stack_top, tied_task ) );
}
KMP_DEBUG_ASSERT( stack_top == & task_stack->ts_first_block.sb_block[0] );
KA_TRACE(threshold, ("__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
location, gtid ) );
}
//---------------------------------------------------------------------------
// __kmp_init_task_stack: initialize the task stack for the first time
// after a thread_data structure is created.
// It should not be necessary to do this again (assuming the stack works).
//
// gtid: global thread identifier of calling thread
// thread_data: thread data for task team thread containing stack
static void
__kmp_init_task_stack( kmp_int32 gtid, kmp_thread_data_t *thread_data )
{
kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks;
kmp_stack_block_t *first_block;
// set up the first block of the stack
first_block = & task_stack -> ts_first_block;
task_stack -> ts_top = (kmp_taskdata_t **) first_block;
memset( (void *) first_block, '\0', TASK_STACK_BLOCK_SIZE * sizeof(kmp_taskdata_t *));
// initialize the stack to be empty
task_stack -> ts_entries = TASK_STACK_EMPTY;
first_block -> sb_next = NULL;
first_block -> sb_prev = NULL;
}
//---------------------------------------------------------------------------
// __kmp_free_task_stack: free the task stack when thread_data is destroyed.
//
// gtid: global thread identifier for calling thread
// thread_data: thread info for thread containing stack
static void
__kmp_free_task_stack( kmp_int32 gtid, kmp_thread_data_t *thread_data )
{
kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks;
kmp_stack_block_t *stack_block = & task_stack -> ts_first_block;
KMP_DEBUG_ASSERT( task_stack -> ts_entries == TASK_STACK_EMPTY );
// free from the second block of the stack
while ( stack_block != NULL ) {
kmp_stack_block_t *next_block = (stack_block) ? stack_block -> sb_next : NULL;
stack_block -> sb_next = NULL;
stack_block -> sb_prev = NULL;
if (stack_block != & task_stack -> ts_first_block) {
__kmp_thread_free( thread, stack_block ); // free the block, if not the first
}
stack_block = next_block;
}
// initialize the stack to be empty
task_stack -> ts_entries = 0;
task_stack -> ts_top = NULL;
}
//---------------------------------------------------------------------------
// __kmp_push_task_stack: Push the tied task onto the task stack.
// Grow the stack if necessary by allocating another block.
//
// gtid: global thread identifier for calling thread
// thread: thread info for thread containing stack
// tied_task: the task to push on the stack
static void
__kmp_push_task_stack( kmp_int32 gtid, kmp_info_t *thread, kmp_taskdata_t * tied_task )
{
// GEH - need to consider what to do if tt_threads_data not allocated yet
kmp_thread_data_t *thread_data = & thread -> th.th_task_team ->
tt.tt_threads_data[ __kmp_tid_from_gtid( gtid ) ];
kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks ;
if ( tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser ) {
return; // Don't push anything on stack if team or team tasks are serialized
}
KMP_DEBUG_ASSERT( tied_task -> td_flags.tasktype == TASK_TIED );
KMP_DEBUG_ASSERT( task_stack -> ts_top != NULL );
KA_TRACE(20, ("__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
gtid, thread, tied_task ) );
// Store entry
* (task_stack -> ts_top) = tied_task;
// Do bookkeeping for next push
task_stack -> ts_top++;
task_stack -> ts_entries++;
if ( task_stack -> ts_entries & TASK_STACK_INDEX_MASK == 0 )
{
// Find beginning of this task block
kmp_stack_block_t *stack_block =
(kmp_stack_block_t *) (task_stack -> ts_top - TASK_STACK_BLOCK_SIZE);
// Check if we already have a block
if ( stack_block -> sb_next != NULL )
{ // reset ts_top to beginning of next block
task_stack -> ts_top = & stack_block -> sb_next -> sb_block[0];
}
else
{ // Alloc new block and link it up
kmp_stack_block_t *new_block = (kmp_stack_block_t *)
__kmp_thread_calloc(thread, sizeof(kmp_stack_block_t));
task_stack -> ts_top = & new_block -> sb_block[0];
stack_block -> sb_next = new_block;
new_block -> sb_prev = stack_block;
new_block -> sb_next = NULL;
KA_TRACE(30, ("__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
gtid, tied_task, new_block ) );
}
}
KA_TRACE(20, ("__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid, tied_task ) );
}
//---------------------------------------------------------------------------
// __kmp_pop_task_stack: Pop the tied task from the task stack. Don't return
// the task, just check to make sure it matches the ending task passed in.
//
// gtid: global thread identifier for the calling thread
// thread: thread info structure containing stack
// tied_task: the task popped off the stack
// ending_task: the task that is ending (should match popped task)
static void
__kmp_pop_task_stack( kmp_int32 gtid, kmp_info_t *thread, kmp_taskdata_t *ending_task )
{
// GEH - need to consider what to do if tt_threads_data not allocated yet
kmp_thread_data_t *thread_data = & thread -> th.th_task_team -> tt_threads_data[ __kmp_tid_from_gtid( gtid ) ];
kmp_task_stack_t *task_stack = & thread_data->td.td_susp_tied_tasks ;
kmp_taskdata_t *tied_task;
if ( ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser ) {
return; // Don't pop anything from stack if team or team tasks are serialized
}
KMP_DEBUG_ASSERT( task_stack -> ts_top != NULL );
KMP_DEBUG_ASSERT( task_stack -> ts_entries > 0 );
KA_TRACE(20, ("__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid, thread ) );
// fix up ts_top if we need to pop from previous block
if ( task_stack -> ts_entries & TASK_STACK_INDEX_MASK == 0 )
{
kmp_stack_block_t *stack_block =
(kmp_stack_block_t *) (task_stack -> ts_top) ;
stack_block = stack_block -> sb_prev;
task_stack -> ts_top = & stack_block -> sb_block[TASK_STACK_BLOCK_SIZE];
}
// finish bookkeeping
task_stack -> ts_top--;
task_stack -> ts_entries--;
tied_task = * (task_stack -> ts_top );
KMP_DEBUG_ASSERT( tied_task != NULL );
KMP_DEBUG_ASSERT( tied_task -> td_flags.tasktype == TASK_TIED );
KMP_DEBUG_ASSERT( tied_task == ending_task ); // If we built the stack correctly
KA_TRACE(20, ("__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid, tied_task ) );
return;
}
#endif /* BUILD_TIED_TASK_STACK */
//---------------------------------------------------
// __kmp_push_task: Add a task to the thread's deque
static kmp_int32
__kmp_push_task(kmp_int32 gtid, kmp_task_t * task )
{
kmp_info_t * thread = __kmp_threads[ gtid ];
kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
kmp_task_team_t * task_team = thread->th.th_task_team;
kmp_int32 tid = __kmp_tid_from_gtid( gtid );
kmp_thread_data_t * thread_data;
KA_TRACE(20, ("__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata ) );
if ( taskdata->td_flags.tiedness == TASK_UNTIED ) {
// untied task needs to increment counter so that the task structure is not freed prematurely
kmp_int32 counter = 1 + KMP_TEST_THEN_INC32(&taskdata->td_untied_count);
KA_TRACE(20, ( "__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
gtid, counter, taskdata ) );
}
// The first check avoids building task_team thread data if serialized
if ( taskdata->td_flags.task_serial ) {
KA_TRACE(20, ( "__kmp_push_task: T#%d team serialized; returning TASK_NOT_PUSHED for task %p\n",
gtid, taskdata ) );
return TASK_NOT_PUSHED;
}
// Now that serialized tasks have returned, we can assume that we are not in immediate exec mode
KMP_DEBUG_ASSERT( __kmp_tasking_mode != tskm_immediate_exec );
if ( ! KMP_TASKING_ENABLED(task_team) ) {
__kmp_enable_tasking( task_team, thread );
}
KMP_DEBUG_ASSERT( TCR_4(task_team -> tt.tt_found_tasks) == TRUE );
KMP_DEBUG_ASSERT( TCR_PTR(task_team -> tt.tt_threads_data) != NULL );
// Find tasking deque specific to encountering thread
thread_data = & task_team -> tt.tt_threads_data[ tid ];
// No lock needed since only owner can allocate
if (thread_data -> td.td_deque == NULL ) {
__kmp_alloc_task_deque( thread, thread_data );
}
// Check if deque is full
if ( TCR_4(thread_data -> td.td_deque_ntasks) >= TASK_DEQUE_SIZE(thread_data->td) )
{
KA_TRACE(20, ( "__kmp_push_task: T#%d deque is full; returning TASK_NOT_PUSHED for task %p\n",
gtid, taskdata ) );
return TASK_NOT_PUSHED;
}
// Lock the deque for the task push operation
__kmp_acquire_bootstrap_lock( & thread_data -> td.td_deque_lock );
#if OMP_45_ENABLED
// Need to recheck as we can get a proxy task from a thread outside of OpenMP
if ( TCR_4(thread_data -> td.td_deque_ntasks) >= TASK_DEQUE_SIZE(thread_data->td) )
{
__kmp_release_bootstrap_lock( & thread_data -> td.td_deque_lock );
KA_TRACE(20, ( "__kmp_push_task: T#%d deque is full on 2nd check; returning TASK_NOT_PUSHED for task %p\n",
gtid, taskdata ) );
return TASK_NOT_PUSHED;
}
#else
// Must have room since no thread can add tasks but calling thread
KMP_DEBUG_ASSERT( TCR_4(thread_data -> td.td_deque_ntasks) < TASK_DEQUE_SIZE(thread_data->td) );
#endif
thread_data -> td.td_deque[ thread_data -> td.td_deque_tail ] = taskdata; // Push taskdata
// Wrap index.
thread_data -> td.td_deque_tail = ( thread_data -> td.td_deque_tail + 1 ) & TASK_DEQUE_MASK(thread_data->td);
TCW_4(thread_data -> td.td_deque_ntasks, TCR_4(thread_data -> td.td_deque_ntasks) + 1); // Adjust task count
__kmp_release_bootstrap_lock( & thread_data -> td.td_deque_lock );
KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
"task=%p ntasks=%d head=%u tail=%u\n",
gtid, taskdata, thread_data->td.td_deque_ntasks,
thread_data->td.td_deque_tail, thread_data->td.td_deque_head) );
return TASK_SUCCESSFULLY_PUSHED;
}
//-----------------------------------------------------------------------------------------
// __kmp_pop_current_task_from_thread: set up current task from called thread when team ends
// this_thr: thread structure to set current_task in.
void
__kmp_pop_current_task_from_thread( kmp_info_t *this_thr )
{
KF_TRACE( 10, ("__kmp_pop_current_task_from_thread(enter): T#%d this_thread=%p, curtask=%p, "
"curtask_parent=%p\n",
0, this_thr, this_thr -> th.th_current_task,
this_thr -> th.th_current_task -> td_parent ) );
this_thr -> th.th_current_task = this_thr -> th.th_current_task -> td_parent;
KF_TRACE( 10, ("__kmp_pop_current_task_from_thread(exit): T#%d this_thread=%p, curtask=%p, "
"curtask_parent=%p\n",
0, this_thr, this_thr -> th.th_current_task,
this_thr -> th.th_current_task -> td_parent ) );
}
//---------------------------------------------------------------------------------------
// __kmp_push_current_task_to_thread: set up current task in called thread for a new team
// this_thr: thread structure to set up
// team: team for implicit task data
// tid: thread within team to set up
void
__kmp_push_current_task_to_thread( kmp_info_t *this_thr, kmp_team_t *team, int tid )
{
// current task of the thread is a parent of the new just created implicit tasks of new team
KF_TRACE( 10, ( "__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p curtask=%p "
"parent_task=%p\n",
tid, this_thr, this_thr->th.th_current_task,
team->t.t_implicit_task_taskdata[tid].td_parent ) );
KMP_DEBUG_ASSERT (this_thr != NULL);
if( tid == 0 ) {
if( this_thr->th.th_current_task != & team -> t.t_implicit_task_taskdata[ 0 ] ) {
team -> t.t_implicit_task_taskdata[ 0 ].td_parent = this_thr->th.th_current_task;
this_thr->th.th_current_task = & team -> t.t_implicit_task_taskdata[ 0 ];
}
} else {
team -> t.t_implicit_task_taskdata[ tid ].td_parent = team -> t.t_implicit_task_taskdata[ 0 ].td_parent;
this_thr->th.th_current_task = & team -> t.t_implicit_task_taskdata[ tid ];
}
KF_TRACE( 10, ( "__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p curtask=%p "
"parent_task=%p\n",
tid, this_thr, this_thr->th.th_current_task,
team->t.t_implicit_task_taskdata[tid].td_parent ) );
}
//----------------------------------------------------------------------
// __kmp_task_start: bookkeeping for a task starting execution
// GTID: global thread id of calling thread
// task: task starting execution
// current_task: task suspending
static void
__kmp_task_start( kmp_int32 gtid, kmp_task_t * task, kmp_taskdata_t * current_task )
{
kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
kmp_info_t * thread = __kmp_threads[ gtid ];
KA_TRACE(10, ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
gtid, taskdata, current_task) );
KMP_DEBUG_ASSERT( taskdata -> td_flags.tasktype == TASK_EXPLICIT );
// mark currently executing task as suspended
// TODO: GEH - make sure root team implicit task is initialized properly.
// KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 );
current_task -> td_flags.executing = 0;
// Add task to stack if tied
#ifdef BUILD_TIED_TASK_STACK
if ( taskdata -> td_flags.tiedness == TASK_TIED )
{
__kmp_push_task_stack( gtid, thread, taskdata );
}
#endif /* BUILD_TIED_TASK_STACK */
// mark starting task as executing and as current task
thread -> th.th_current_task = taskdata;
KMP_DEBUG_ASSERT( taskdata->td_flags.started == 0 || taskdata->td_flags.tiedness == TASK_UNTIED );
KMP_DEBUG_ASSERT( taskdata->td_flags.executing == 0 || taskdata->td_flags.tiedness == TASK_UNTIED );
taskdata -> td_flags.started = 1;
taskdata -> td_flags.executing = 1;
KMP_DEBUG_ASSERT( taskdata -> td_flags.complete == 0 );
KMP_DEBUG_ASSERT( taskdata -> td_flags.freed == 0 );
// GEH TODO: shouldn't we pass some sort of location identifier here?
// APT: yes, we will pass location here.
// need to store current thread state (in a thread or taskdata structure)
// before setting work_state, otherwise wrong state is set after end of task
KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n",
gtid, taskdata ) );
#if OMPT_SUPPORT
if (ompt_enabled &&
ompt_callbacks.ompt_callback(ompt_event_task_begin)) {
kmp_taskdata_t *parent = taskdata->td_parent;
ompt_callbacks.ompt_callback(ompt_event_task_begin)(
parent ? parent->ompt_task_info.task_id : ompt_task_id_none,
parent ? &(parent->ompt_task_info.frame) : NULL,
taskdata->ompt_task_info.task_id,
taskdata->ompt_task_info.function);
}
#endif
#if OMP_40_ENABLED && OMPT_SUPPORT && OMPT_TRACE
/* OMPT emit all dependences if requested by the tool */
if (ompt_enabled && taskdata->ompt_task_info.ndeps > 0 &&
ompt_callbacks.ompt_callback(ompt_event_task_dependences))
{
ompt_callbacks.ompt_callback(ompt_event_task_dependences)(
taskdata->ompt_task_info.task_id,
taskdata->ompt_task_info.deps,
taskdata->ompt_task_info.ndeps
);
/* We can now free the allocated memory for the dependencies */
KMP_OMPT_DEPS_FREE (thread, taskdata->ompt_task_info.deps);
taskdata->ompt_task_info.deps = NULL;
taskdata->ompt_task_info.ndeps = 0;
}
#endif /* OMP_40_ENABLED && OMPT_SUPPORT && OMPT_TRACE */
return;
}
//----------------------------------------------------------------------
// __kmpc_omp_task_begin_if0: report that a given serialized task has started execution
// loc_ref: source location information; points to beginning of task block.
// gtid: global thread number.
// task: task thunk for the started task.
void
__kmpc_omp_task_begin_if0( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * task )
{
kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
kmp_taskdata_t * current_task = __kmp_threads[ gtid ] -> th.th_current_task;
KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p current_task=%p\n",
gtid, loc_ref, taskdata, current_task ) );
if ( taskdata->td_flags.tiedness == TASK_UNTIED ) {
// untied task needs to increment counter so that the task structure is not freed prematurely
kmp_int32 counter = 1 + KMP_TEST_THEN_INC32(&taskdata->td_untied_count);
KA_TRACE(20, ( "__kmpc_omp_task_begin_if0: T#%d untied_count (%d) incremented for task %p\n",
gtid, counter, taskdata ) );
}
taskdata -> td_flags.task_serial = 1; // Execute this task immediately, not deferred.
__kmp_task_start( gtid, task, current_task );
KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n",
gtid, loc_ref, taskdata ) );
return;
}
#ifdef TASK_UNUSED
//----------------------------------------------------------------------
// __kmpc_omp_task_begin: report that a given task has started execution
// NEVER GENERATED BY COMPILER, DEPRECATED!!!
void
__kmpc_omp_task_begin( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * task )
{
kmp_taskdata_t * current_task = __kmp_threads[ gtid ] -> th.th_current_task;
KA_TRACE(10, ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task ) );
__kmp_task_start( gtid, task, current_task );
KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n",
gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
return;
}
#endif // TASK_UNUSED
//-------------------------------------------------------------------------------------
// __kmp_free_task: free the current task space and the space for shareds
// gtid: Global thread ID of calling thread
// taskdata: task to free
// thread: thread data structure of caller
static void
__kmp_free_task( kmp_int32 gtid, kmp_taskdata_t * taskdata, kmp_info_t * thread )
{
KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n",
gtid, taskdata) );
// Check to make sure all flags and counters have the correct values
KMP_DEBUG_ASSERT( taskdata->td_flags.tasktype == TASK_EXPLICIT );
KMP_DEBUG_ASSERT( taskdata->td_flags.executing == 0 );
KMP_DEBUG_ASSERT( taskdata->td_flags.complete == 1 );
KMP_DEBUG_ASSERT( taskdata->td_flags.freed == 0 );
KMP_DEBUG_ASSERT( TCR_4(taskdata->td_allocated_child_tasks) == 0 || taskdata->td_flags.task_serial == 1);
KMP_DEBUG_ASSERT( TCR_4(taskdata->td_incomplete_child_tasks) == 0 );
taskdata->td_flags.freed = 1;
// deallocate the taskdata and shared variable blocks associated with this task
#if USE_FAST_MEMORY
__kmp_fast_free( thread, taskdata );
#else /* ! USE_FAST_MEMORY */
__kmp_thread_free( thread, taskdata );
#endif
KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n",
gtid, taskdata) );
}
//-------------------------------------------------------------------------------------
// __kmp_free_task_and_ancestors: free the current task and ancestors without children
//
// gtid: Global thread ID of calling thread
// taskdata: task to free
// thread: thread data structure of caller
static void
__kmp_free_task_and_ancestors( kmp_int32 gtid, kmp_taskdata_t * taskdata, kmp_info_t * thread )
{
// Proxy tasks must always be allowed to free their parents
// because they can be run in background even in serial mode.
kmp_int32 task_serial = taskdata->td_flags.task_serial && !taskdata->td_flags.proxy;
KMP_DEBUG_ASSERT( taskdata -> td_flags.tasktype == TASK_EXPLICIT );
kmp_int32 children = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata -> td_allocated_child_tasks) ) - 1;
KMP_DEBUG_ASSERT( children >= 0 );
// Now, go up the ancestor tree to see if any ancestors can now be freed.
while ( children == 0 )
{
kmp_taskdata_t * parent_taskdata = taskdata -> td_parent;
KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
"and freeing itself\n", gtid, taskdata) );
// --- Deallocate my ancestor task ---
__kmp_free_task( gtid, taskdata, thread );
taskdata = parent_taskdata;
// Stop checking ancestors at implicit task
// instead of walking up ancestor tree to avoid premature deallocation of ancestors.
if ( task_serial || taskdata -> td_flags.tasktype == TASK_IMPLICIT )
return;
// Predecrement simulated by "- 1" calculation
children = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata -> td_allocated_child_tasks) ) - 1;
KMP_DEBUG_ASSERT( children >= 0 );
}
KA_TRACE(20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
"not freeing it yet\n", gtid, taskdata, children) );
}
//---------------------------------------------------------------------
// __kmp_task_finish: bookkeeping to do when a task finishes execution
// gtid: global thread ID for calling thread
// task: task to be finished
// resumed_task: task to be resumed. (may be NULL if task is serialized)
static void
__kmp_task_finish( kmp_int32 gtid, kmp_task_t *task, kmp_taskdata_t *resumed_task )
{
kmp_taskdata_t * taskdata = KMP_TASK_TO_TASKDATA(task);
kmp_info_t * thread = __kmp_threads[ gtid ];
kmp_int32 children = 0;
#if OMPT_SUPPORT
if (ompt_enabled &&
ompt_callbacks.ompt_callback(ompt_event_task_end)) {
kmp_taskdata_t *parent = taskdata->td_parent;
ompt_callbacks.ompt_callback(ompt_event_task_end)(
taskdata->ompt_task_info.task_id);
}
#endif
KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming task %p\n",
gtid, taskdata, resumed_task) );
KMP_DEBUG_ASSERT( taskdata -> td_flags.tasktype == TASK_EXPLICIT );
// Pop task from stack if tied
#ifdef BUILD_TIED_TASK_STACK
if ( taskdata -> td_flags.tiedness == TASK_TIED )
{
__kmp_pop_task_stack( gtid, thread, taskdata );
}
#endif /* BUILD_TIED_TASK_STACK */
if ( taskdata->td_flags.tiedness == TASK_UNTIED ) {
// untied task needs to check the counter so that the task structure is not freed prematurely
kmp_int32 counter = KMP_TEST_THEN_DEC32(&taskdata->td_untied_count) - 1;
KA_TRACE(20, ( "__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
gtid, counter, taskdata ) );
if ( counter > 0 ) {
// untied task is not done, to be continued possibly by other thread, do not free it now
if (resumed_task == NULL) {
KMP_DEBUG_ASSERT( taskdata->td_flags.task_serial );
resumed_task = taskdata->td_parent; // In a serialized task, the resumed task is the parent
}
thread->th.th_current_task = resumed_task; // restore current_task
resumed_task->td_flags.executing = 1; // resume previous task
KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, resuming task %p\n",
gtid, taskdata, resumed_task) );
return;
}
}
KMP_DEBUG_ASSERT( taskdata -> td_flags.complete == 0 );
taskdata -> td_flags.complete = 1; // mark the task as completed
KMP_DEBUG_ASSERT( taskdata -> td_flags.started == 1 );
KMP_DEBUG_ASSERT( taskdata -> td_flags.freed == 0 );
// Only need to keep track of count if team parallel and tasking not serialized
if ( !( taskdata -> td_flags.team_serial || taskdata -> td_flags.tasking_ser ) ) {
// Predecrement simulated by "- 1" calculation
children = KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata -> td_parent -> td_incomplete_child_tasks) ) - 1;
KMP_DEBUG_ASSERT( children >= 0 );
#if OMP_40_ENABLED
if ( taskdata->td_taskgroup )
KMP_TEST_THEN_DEC32( (kmp_int32 *)(& taskdata->td_taskgroup->count) );
__kmp_release_deps(gtid,taskdata);
#endif
}
// td_flags.executing must be marked as 0 after __kmp_release_deps has been called
// Othertwise, if a task is executed immediately from the release_deps code
// the flag will be reset to 1 again by this same function
KMP_DEBUG_ASSERT( taskdata -> td_flags.executing == 1 );
taskdata -> td_flags.executing = 0; // suspend the finishing task
KA_TRACE(20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
gtid, taskdata, children) );
#if OMP_40_ENABLED
/* If the tasks' destructor thunk flag has been set, we need to invoke the
destructor thunk that has been generated by the compiler.
The code is placed here, since at this point other tasks might have been released
hence overlapping the destructor invokations with some other work in the
released tasks. The OpenMP spec is not specific on when the destructors are
invoked, so we should be free to choose.
*/
if (taskdata->td_flags.destructors_thunk) {
kmp_routine_entry_t destr_thunk = task->data1.destructors;
KMP_ASSERT(destr_thunk);
destr_thunk(gtid, task);
}
#endif // OMP_40_ENABLED
// bookkeeping for resuming task:
// GEH - note tasking_ser => task_serial
KMP_DEBUG_ASSERT( (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
taskdata->td_flags.task_serial);
if ( taskdata->td_flags.task_serial )
{
if (resumed_task == NULL) {
resumed_task = taskdata->td_parent; // In a serialized task, the resumed task is the parent
}
else {
// verify resumed task passed in points to parent
KMP_DEBUG_ASSERT( resumed_task == taskdata->td_parent );
}
}
else {
KMP_DEBUG_ASSERT( resumed_task != NULL ); // verify that resumed task is passed as arguemnt
}
// Free this task and then ancestor tasks if they have no children.
// Restore th_current_task first as suggested by John:
// johnmc: if an asynchronous inquiry peers into the runtime system
// it doesn't see the freed task as the current task.
thread->th.th_current_task = resumed_task;
__kmp_free_task_and_ancestors(gtid, taskdata, thread);
// TODO: GEH - make sure root team implicit task is initialized properly.
// KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 );
resumed_task->td_flags.executing = 1; // resume previous task
KA_TRACE(10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
gtid, taskdata, resumed_task) );
return;
}
//---------------------------------------------------------------------
// __kmpc_omp_task_complete_if0: report that a task has completed execution
// loc_ref: source location information; points to end of task block.
// gtid: global thread number.
// task: task thunk for the completed task.
void
__kmpc_omp_task_complete_if0( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task )
{
KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
__kmp_task_finish( gtid, task, NULL ); // this routine will provide task to resume
KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
return;
}
#ifdef TASK_UNUSED
//---------------------------------------------------------------------
// __kmpc_omp_task_complete: report that a task has completed execution
// NEVER GENERATED BY COMPILER, DEPRECATED!!!
void
__kmpc_omp_task_complete( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task )
{
KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n",
gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
__kmp_task_finish( gtid, task, NULL ); // Not sure how to find task to resume
KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n",
gtid, loc_ref, KMP_TASK_TO_TASKDATA(task) ) );
return;
}
#endif // TASK_UNUSED
#if OMPT_SUPPORT
//----------------------------------------------------------------------------------------------------
// __kmp_task_init_ompt:
// Initialize OMPT fields maintained by a task. This will only be called after
// ompt_tool, so we already know whether ompt is enabled or not.
static inline void
__kmp_task_init_ompt( kmp_taskdata_t * task, int tid, void * function )
{
if (ompt_enabled) {
task->ompt_task_info.task_id = __ompt_task_id_new(tid);
task->ompt_task_info.function = function;
task->ompt_task_info.frame.exit_runtime_frame = NULL;
task->ompt_task_info.frame.reenter_runtime_frame = NULL;
#if OMP_40_ENABLED
task->ompt_task_info.ndeps = 0;
task->ompt_task_info.deps = NULL;
#endif /* OMP_40_ENABLED */
}
}
#endif
//----------------------------------------------------------------------------------------------------
// __kmp_init_implicit_task: Initialize the appropriate fields in the implicit task for a given thread
//
// loc_ref: reference to source location of parallel region
// this_thr: thread data structure corresponding to implicit task
// team: team for this_thr
// tid: thread id of given thread within team
// set_curr_task: TRUE if need to push current task to thread
// NOTE: Routine does not set up the implicit task ICVS. This is assumed to have already been done elsewhere.
// TODO: Get better loc_ref. Value passed in may be NULL
void
__kmp_init_implicit_task( ident_t *loc_ref, kmp_info_t *this_thr, kmp_team_t *team, int tid, int set_curr_task )
{
kmp_taskdata_t * task = & team->t.t_implicit_task_taskdata[ tid ];
KF_TRACE(10, ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
tid, team, task, set_curr_task ? "TRUE" : "FALSE" ) );
task->td_task_id = KMP_GEN_TASK_ID();
task->td_team = team;
// task->td_parent = NULL; // fix for CQ230101 (broken parent task info in debugger)
task->td_ident = loc_ref;
task->td_taskwait_ident = NULL;
task->td_taskwait_counter = 0;
task->td_taskwait_thread = 0;
task->td_flags.tiedness = TASK_TIED;
task->td_flags.tasktype = TASK_IMPLICIT;
#if OMP_45_ENABLED
task->td_flags.proxy = TASK_FULL;
#endif
// All implicit tasks are executed immediately, not deferred
task->td_flags.task_serial = 1;
task->td_flags.tasking_ser = ( __kmp_tasking_mode == tskm_immediate_exec );
task->td_flags.team_serial = ( team->t.t_serialized ) ? 1 : 0;
task->td_flags.started = 1;
task->td_flags.executing = 1;
task->td_flags.complete = 0;
task->td_flags.freed = 0;
#if OMP_40_ENABLED
task->td_dephash = NULL;
task->td_depnode = NULL;
#endif
if (set_curr_task) { // only do this initialization the first time a thread is created
task->td_incomplete_child_tasks = 0;
task->td_allocated_child_tasks = 0; // Not used because do not need to deallocate implicit task
#if OMP_40_ENABLED
task->td_taskgroup = NULL; // An implicit task does not have taskgroup
#endif
__kmp_push_current_task_to_thread( this_thr, team, tid );
} else {
KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
}
#if OMPT_SUPPORT
__kmp_task_init_ompt(task, tid, NULL);
#endif
KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n",
tid, team, task ) );
}
// Round up a size to a power of two specified by val
// Used to insert padding between structures co-allocated using a single malloc() call
static size_t
__kmp_round_up_to_val( size_t size, size_t val ) {
if ( size & ( val - 1 ) ) {
size &= ~ ( val - 1 );
if ( size <= KMP_SIZE_T_MAX - val ) {
size += val; // Round up if there is no overflow.
}; // if
}; // if
return size;
} // __kmp_round_up_to_va
//---------------------------------------------------------------------------------
// __kmp_task_alloc: Allocate the taskdata and task data structures for a task
//
// loc_ref: source location information
// gtid: global thread number.
// flags: include tiedness & task type (explicit vs. implicit) of the ''new'' task encountered.
// Converted from kmp_int32 to kmp_tasking_flags_t in routine.
// sizeof_kmp_task_t: Size in bytes of kmp_task_t data structure including private vars accessed in task.
// sizeof_shareds: Size in bytes of array of pointers to shared vars accessed in task.
// task_entry: Pointer to task code entry point generated by compiler.
// returns: a pointer to the allocated kmp_task_t structure (task).
kmp_task_t *
__kmp_task_alloc( ident_t *loc_ref, kmp_int32 gtid, kmp_tasking_flags_t *flags,
size_t sizeof_kmp_task_t, size_t sizeof_shareds,
kmp_routine_entry_t task_entry )
{
kmp_task_t *task;
kmp_taskdata_t *taskdata;
kmp_info_t *thread = __kmp_threads[ gtid ];
kmp_team_t *team = thread->th.th_team;
kmp_taskdata_t *parent_task = thread->th.th_current_task;
size_t shareds_offset;
KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
"sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
sizeof_shareds, task_entry) );
if ( parent_task->td_flags.final ) {
if (flags->merged_if0) {
}
flags->final = 1;
}
#if OMP_45_ENABLED
if ( flags->proxy == TASK_PROXY ) {
flags->tiedness = TASK_UNTIED;
flags->merged_if0 = 1;
/* are we running in a sequential parallel or tskm_immediate_exec... we need tasking support enabled */
if ( (thread->th.th_task_team) == NULL ) {
/* This should only happen if the team is serialized
setup a task team and propagate it to the thread
*/
KMP_DEBUG_ASSERT(team->t.t_serialized);
KA_TRACE(30,("T#%d creating task team in __kmp_task_alloc for proxy task\n", gtid));
__kmp_task_team_setup(thread,team,1); // 1 indicates setup the current team regardless of nthreads
thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
}
kmp_task_team_t * task_team = thread->th.th_task_team;
/* tasking must be enabled now as the task might not be pushed */
if ( !KMP_TASKING_ENABLED( task_team ) ) {
KA_TRACE(30,("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
__kmp_enable_tasking( task_team, thread );
kmp_int32 tid = thread->th.th_info.ds.ds_tid;
kmp_thread_data_t * thread_data = & task_team -> tt.tt_threads_data[ tid ];
// No lock needed since only owner can allocate
if (thread_data -> td.td_deque == NULL ) {
__kmp_alloc_task_deque( thread, thread_data );
}
}
if ( task_team->tt.tt_found_proxy_tasks == FALSE )
TCW_4(task_team -> tt.tt_found_proxy_tasks, TRUE);
}
#endif
// Calculate shared structure offset including padding after kmp_task_t struct
// to align pointers in shared struct
shareds_offset = sizeof( kmp_taskdata_t ) + sizeof_kmp_task_t;
shareds_offset = __kmp_round_up_to_val( shareds_offset, sizeof( void * ));
// Allocate a kmp_taskdata_t block and a kmp_task_t block.
KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n",
gtid, shareds_offset) );
KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n",
gtid, sizeof_shareds) );
// Avoid double allocation here by combining shareds with taskdata
#if USE_FAST_MEMORY
taskdata = (kmp_taskdata_t *) __kmp_fast_allocate( thread, shareds_offset + sizeof_shareds );
#else /* ! USE_FAST_MEMORY */
taskdata = (kmp_taskdata_t *) __kmp_thread_malloc( thread, shareds_offset + sizeof_shareds );
#endif /* USE_FAST_MEMORY */
task = KMP_TASKDATA_TO_TASK(taskdata);
// Make sure task & taskdata are aligned appropriately
#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)taskdata) & (sizeof(double)-1) ) == 0 );
KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)task) & (sizeof(double)-1) ) == 0 );
#else
KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)taskdata) & (sizeof(_Quad)-1) ) == 0 );
KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)task) & (sizeof(_Quad)-1) ) == 0 );
#endif
if (sizeof_shareds > 0) {
// Avoid double allocation here by combining shareds with taskdata
task->shareds = & ((char *) taskdata)[ shareds_offset ];
// Make sure shareds struct is aligned to pointer size
KMP_DEBUG_ASSERT( ( ((kmp_uintptr_t)task->shareds) & (sizeof(void *)-1) ) == 0 );
} else {
task->shareds = NULL;
}
task->routine = task_entry;
task->part_id = 0; // AC: Always start with 0 part id
taskdata->td_task_id = KMP_GEN_TASK_ID();
taskdata->td_team = team;
taskdata->td_alloc_thread = thread;
taskdata->td_parent = parent_task;