blob: 79ba3f3e72acead2b1187050cba23e0c1d1918ed [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * kmp_taskq.c -- TASKQ support for OpenMP.
3 * $Revision: 42582 $
4 * $Date: 2013-08-09 06:30:22 -0500 (Fri, 09 Aug 2013) $
5 */
6
7
8//===----------------------------------------------------------------------===//
9//
10// The LLVM Compiler Infrastructure
11//
12// This file is dual licensed under the MIT and the University of Illinois Open
13// Source Licenses. See LICENSE.txt for details.
14//
15//===----------------------------------------------------------------------===//
16
17
18#include "kmp.h"
19#include "kmp_i18n.h"
20#include "kmp_io.h"
21#include "kmp_error.h"
22
23#define MAX_MESSAGE 512
24
25/* ------------------------------------------------------------------------ */
26/* ------------------------------------------------------------------------ */
27
28/*
29 * Taskq routines and global variables
30 */
31
32#define KMP_DEBUG_REF_CTS(x) KF_TRACE(1, x);
33
34#define THREAD_ALLOC_FOR_TASKQ
35
36static void
37__kmp_static_delay( int arg )
38{
39/* Work around weird code-gen bug that causes assert to trip */
40#if KMP_ARCH_X86_64 && KMP_OS_LINUX
41 KMP_ASSERT( arg != 0 );
42#else
43 KMP_ASSERT( arg >= 0 );
44#endif
45}
46
47static void
48__kmp_static_yield( int arg )
49{
50 __kmp_yield( arg );
51}
52
53static int
54in_parallel_context( kmp_team_t *team )
55{
56 return ! team -> t.t_serialized;
57}
58
59static void
60__kmp_taskq_eo( int *gtid_ref, int *cid_ref, ident_t *loc_ref )
61{
62 int gtid = *gtid_ref;
63 int tid = __kmp_tid_from_gtid( gtid );
64 kmp_uint32 spins;
65 kmp_uint32 my_token;
66 kmpc_task_queue_t *taskq;
67 kmp_taskq_t *tq = & __kmp_threads[gtid] -> th.th_team -> t.t_taskq;
68
69 if ( __kmp_env_consistency_check )
70 __kmp_push_sync( gtid, ct_ordered_in_taskq, loc_ref, NULL );
71
72 if ( ! __kmp_threads[ gtid ]-> th.th_team -> t.t_serialized ) {
73 KMP_MB(); /* Flush all pending memory write invalidates. */
74
75 /* GEH - need check here under stats to make sure */
76 /* inside task (curr_thunk[*tid_ref] != NULL) */
77
78 my_token =tq->tq_curr_thunk[ tid ]-> th_tasknum;
79
80 taskq = tq->tq_curr_thunk[ tid ]-> th.th_shareds -> sv_queue;
81
82 KMP_WAIT_YIELD(&taskq->tq_tasknum_serving, my_token, KMP_EQ, NULL);
83 KMP_MB();
84 }
85}
86
87static void
88__kmp_taskq_xo( int *gtid_ref, int *cid_ref, ident_t *loc_ref )
89{
90 int gtid = *gtid_ref;
91 int tid = __kmp_tid_from_gtid( gtid );
92 kmp_uint32 my_token;
93 kmp_taskq_t *tq = & __kmp_threads[gtid] -> th.th_team -> t.t_taskq;
94
95 if ( __kmp_env_consistency_check )
96 __kmp_pop_sync( gtid, ct_ordered_in_taskq, loc_ref );
97
98 if ( ! __kmp_threads[ gtid ]-> th.th_team -> t.t_serialized ) {
99 KMP_MB(); /* Flush all pending memory write invalidates. */
100
101 /* GEH - need check here under stats to make sure */
102 /* inside task (curr_thunk[tid] != NULL) */
103
104 my_token = tq->tq_curr_thunk[ tid ]->th_tasknum;
105
106 KMP_MB(); /* Flush all pending memory write invalidates. */
107
108 tq->tq_curr_thunk[ tid ]-> th.th_shareds -> sv_queue -> tq_tasknum_serving = my_token + 1;
109
110 KMP_MB(); /* Flush all pending memory write invalidates. */
111 }
112}
113
114static void
115__kmp_taskq_check_ordered( kmp_int32 gtid, kmpc_thunk_t *thunk )
116{
117 kmp_uint32 spins;
118 kmp_uint32 my_token;
119 kmpc_task_queue_t *taskq;
120
121 /* assume we are always called from an active parallel context */
122
123 KMP_MB(); /* Flush all pending memory write invalidates. */
124
125 my_token = thunk -> th_tasknum;
126
127 taskq = thunk -> th.th_shareds -> sv_queue;
128
129 if(taskq->tq_tasknum_serving <= my_token) {
130 KMP_WAIT_YIELD(&taskq->tq_tasknum_serving, my_token, KMP_GE, NULL);
131 KMP_MB();
132 taskq->tq_tasknum_serving = my_token +1;
133 KMP_MB();
134 }
135}
136
137static void
138__kmp_dump_TQF(kmp_int32 flags)
139{
140 if (flags & TQF_IS_ORDERED)
141 __kmp_printf("ORDERED ");
142 if (flags & TQF_IS_LASTPRIVATE)
143 __kmp_printf("LAST_PRIV ");
144 if (flags & TQF_IS_NOWAIT)
145 __kmp_printf("NOWAIT ");
146 if (flags & TQF_HEURISTICS)
147 __kmp_printf("HEURIST ");
148 if (flags & TQF_INTERFACE_RESERVED1)
149 __kmp_printf("RESERV1 ");
150 if (flags & TQF_INTERFACE_RESERVED2)
151 __kmp_printf("RESERV2 ");
152 if (flags & TQF_INTERFACE_RESERVED3)
153 __kmp_printf("RESERV3 ");
154 if (flags & TQF_INTERFACE_RESERVED4)
155 __kmp_printf("RESERV4 ");
156 if (flags & TQF_IS_LAST_TASK)
157 __kmp_printf("LAST_TASK ");
158 if (flags & TQF_TASKQ_TASK)
159 __kmp_printf("TASKQ_TASK ");
160 if (flags & TQF_RELEASE_WORKERS)
161 __kmp_printf("RELEASE ");
162 if (flags & TQF_ALL_TASKS_QUEUED)
163 __kmp_printf("ALL_QUEUED ");
164 if (flags & TQF_PARALLEL_CONTEXT)
165 __kmp_printf("PARALLEL ");
166 if (flags & TQF_DEALLOCATED)
167 __kmp_printf("DEALLOC ");
168 if (!(flags & (TQF_INTERNAL_FLAGS|TQF_INTERFACE_FLAGS)))
169 __kmp_printf("(NONE)");
170}
171
172static void
173__kmp_dump_thunk( kmp_taskq_t *tq, kmpc_thunk_t *thunk, kmp_int32 global_tid )
174{
175 int i;
176 int nproc = __kmp_threads[global_tid] -> th.th_team -> t.t_nproc;
177
178 __kmp_printf("\tThunk at %p on (%d): ", thunk, global_tid);
179
180 if (thunk != NULL) {
181 for (i = 0; i < nproc; i++) {
182 if( tq->tq_curr_thunk[i] == thunk ) {
183 __kmp_printf("[%i] ", i);
184 }
185 }
186 __kmp_printf("th_shareds=%p, ", thunk->th.th_shareds);
187 __kmp_printf("th_task=%p, ", thunk->th_task);
188 __kmp_printf("th_encl_thunk=%p, ", thunk->th_encl_thunk);
189 __kmp_printf("th_status=%d, ", thunk->th_status);
190 __kmp_printf("th_tasknum=%u, ", thunk->th_tasknum);
191 __kmp_printf("th_flags="); __kmp_dump_TQF(thunk->th_flags);
192 }
193
194 __kmp_printf("\n");
195}
196
197static void
198__kmp_dump_thunk_stack(kmpc_thunk_t *thunk, kmp_int32 thread_num)
199{
200 kmpc_thunk_t *th;
201
202 __kmp_printf(" Thunk stack for T#%d: ", thread_num);
203
204 for (th = thunk; th != NULL; th = th->th_encl_thunk )
205 __kmp_printf("%p ", th);
206
207 __kmp_printf("\n");
208}
209
210static void
211__kmp_dump_task_queue( kmp_taskq_t *tq, kmpc_task_queue_t *queue, kmp_int32 global_tid )
212{
213 int qs, count, i;
214 kmpc_thunk_t *thunk;
215 kmpc_task_queue_t *taskq;
216
217 __kmp_printf("Task Queue at %p on (%d):\n", queue, global_tid);
218
219 if (queue != NULL) {
220 int in_parallel = queue->tq_flags & TQF_PARALLEL_CONTEXT;
221
222 if ( __kmp_env_consistency_check ) {
223 __kmp_printf(" tq_loc : ");
224 }
225 if (in_parallel) {
226
227 //if (queue->tq.tq_parent != 0)
228 //__kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
229
230 //__kmp_acquire_lock(& queue->tq_link_lck, global_tid);
231
232 KMP_MB(); /* make sure data structures are in consistent state before querying them */
233 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
234
235 __kmp_printf(" tq_parent : %p\n", queue->tq.tq_parent);
236 __kmp_printf(" tq_first_child : %p\n", queue->tq_first_child);
237 __kmp_printf(" tq_next_child : %p\n", queue->tq_next_child);
238 __kmp_printf(" tq_prev_child : %p\n", queue->tq_prev_child);
239 __kmp_printf(" tq_ref_count : %d\n", queue->tq_ref_count);
240
241 //__kmp_release_lock(& queue->tq_link_lck, global_tid);
242
243 //if (queue->tq.tq_parent != 0)
244 //__kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
245
246 //__kmp_acquire_lock(& queue->tq_free_thunks_lck, global_tid);
247 //__kmp_acquire_lock(& queue->tq_queue_lck, global_tid);
248
249 KMP_MB(); /* make sure data structures are in consistent state before querying them */
250 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
251 }
252
253 __kmp_printf(" tq_shareds : ");
254 for (i=0; i<((queue == tq->tq_root) ? queue->tq_nproc : 1); i++)
255 __kmp_printf("%p ", queue->tq_shareds[i].ai_data);
256 __kmp_printf("\n");
257
258 if (in_parallel) {
259 __kmp_printf(" tq_tasknum_queuing : %u\n", queue->tq_tasknum_queuing);
260 __kmp_printf(" tq_tasknum_serving : %u\n", queue->tq_tasknum_serving);
261 }
262
263 __kmp_printf(" tq_queue : %p\n", queue->tq_queue);
264 __kmp_printf(" tq_thunk_space : %p\n", queue->tq_thunk_space);
265 __kmp_printf(" tq_taskq_slot : %p\n", queue->tq_taskq_slot);
266
267 __kmp_printf(" tq_free_thunks : ");
268 for (thunk = queue->tq_free_thunks; thunk != NULL; thunk = thunk->th.th_next_free )
269 __kmp_printf("%p ", thunk);
270 __kmp_printf("\n");
271
272 __kmp_printf(" tq_nslots : %d\n", queue->tq_nslots);
273 __kmp_printf(" tq_head : %d\n", queue->tq_head);
274 __kmp_printf(" tq_tail : %d\n", queue->tq_tail);
275 __kmp_printf(" tq_nfull : %d\n", queue->tq_nfull);
276 __kmp_printf(" tq_hiwat : %d\n", queue->tq_hiwat);
277 __kmp_printf(" tq_flags : "); __kmp_dump_TQF(queue->tq_flags);
278 __kmp_printf("\n");
279
280 if (in_parallel) {
281 __kmp_printf(" tq_th_thunks : ");
282 for (i = 0; i < queue->tq_nproc; i++) {
283 __kmp_printf("%d ", queue->tq_th_thunks[i].ai_data);
284 }
285 __kmp_printf("\n");
286 }
287
288 __kmp_printf("\n");
289 __kmp_printf(" Queue slots:\n");
290
291
292 qs = queue->tq_tail;
293 for ( count = 0; count < queue->tq_nfull; ++count ) {
294 __kmp_printf("(%d)", qs);
295 __kmp_dump_thunk( tq, queue->tq_queue[qs].qs_thunk, global_tid );
296 qs = (qs+1) % queue->tq_nslots;
297 }
298
299 __kmp_printf("\n");
300
301 if (in_parallel) {
302 if (queue->tq_taskq_slot != NULL) {
303 __kmp_printf(" TaskQ slot:\n");
304 __kmp_dump_thunk( tq, (kmpc_thunk_t *) queue->tq_taskq_slot, global_tid );
305 __kmp_printf("\n");
306 }
307 //__kmp_release_lock(& queue->tq_queue_lck, global_tid);
308 //__kmp_release_lock(& queue->tq_free_thunks_lck, global_tid);
309 }
310 }
311
312 __kmp_printf(" Taskq freelist: ");
313
314 //__kmp_acquire_lock( & tq->tq_freelist_lck, global_tid );
315
316 KMP_MB(); /* make sure data structures are in consistent state before querying them */
317 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
318
319 for( taskq = tq->tq_freelist; taskq != NULL; taskq = taskq->tq.tq_next_free )
320 __kmp_printf("%p ", taskq);
321
322 //__kmp_release_lock( & tq->tq_freelist_lck, global_tid );
323
324 __kmp_printf("\n\n");
325}
326
327static void
328__kmp_aux_dump_task_queue_tree( kmp_taskq_t *tq, kmpc_task_queue_t *curr_queue, kmp_int32 level, kmp_int32 global_tid )
329{
330 int i, count, qs;
331 int nproc = __kmp_threads[global_tid] -> th.th_team -> t.t_nproc;
332 kmpc_task_queue_t *queue = curr_queue;
333
334 if (curr_queue == NULL)
335 return;
336
337 __kmp_printf(" ");
338
339 for (i=0; i<level; i++)
340 __kmp_printf(" ");
341
342 __kmp_printf("%p", curr_queue);
343
344 for (i = 0; i < nproc; i++) {
345 if( tq->tq_curr_thunk[i] && tq->tq_curr_thunk[i]->th.th_shareds->sv_queue == curr_queue ) {
346 __kmp_printf(" [%i]", i);
347 }
348 }
349
350 __kmp_printf(":");
351
352 //__kmp_acquire_lock(& curr_queue->tq_queue_lck, global_tid);
353
354 KMP_MB(); /* make sure data structures are in consistent state before querying them */
355 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
356
357 qs = curr_queue->tq_tail;
358
359 for ( count = 0; count < curr_queue->tq_nfull; ++count ) {
360 __kmp_printf("%p ", curr_queue->tq_queue[qs].qs_thunk);
361 qs = (qs+1) % curr_queue->tq_nslots;
362 }
363
364 //__kmp_release_lock(& curr_queue->tq_queue_lck, global_tid);
365
366 __kmp_printf("\n");
367
368 if (curr_queue->tq_first_child) {
369 //__kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
370
371 KMP_MB(); /* make sure data structures are in consistent state before querying them */
372 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
373
374 if (curr_queue->tq_first_child) {
375 for(queue = (kmpc_task_queue_t *)curr_queue->tq_first_child;
376 queue != NULL;
377 queue = queue->tq_next_child) {
378 __kmp_aux_dump_task_queue_tree( tq, queue, level+1, global_tid );
379 }
380 }
381
382 //__kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
383 }
384}
385
386static void
387__kmp_dump_task_queue_tree( kmp_taskq_t *tq, kmpc_task_queue_t *tqroot, kmp_int32 global_tid)
388{
389 __kmp_printf("TaskQ Tree at root %p on (%d):\n", tqroot, global_tid);
390
391 __kmp_aux_dump_task_queue_tree( tq, tqroot, 0, global_tid );
392
393 __kmp_printf("\n");
394}
395
396/* --------------------------------------------------------------------------- */
397
398/*
399 New taskq storage routines that try to minimize overhead of mallocs but
400 still provide cache line alignment.
401*/
402
403
404static void *
405__kmp_taskq_allocate(size_t size, kmp_int32 global_tid)
406{
407 void *addr, *orig_addr;
408 size_t bytes;
409
410 KB_TRACE( 5, ("__kmp_taskq_allocate: called size=%d, gtid=%d\n", (int) size, global_tid ) );
411
412 bytes = sizeof(void *) + CACHE_LINE + size;
413
414#ifdef THREAD_ALLOC_FOR_TASKQ
415 orig_addr = (void *) __kmp_thread_malloc( __kmp_thread_from_gtid(global_tid), bytes );
416#else
417 KE_TRACE( 10, ("%%%%%% MALLOC( %d )\n", bytes ) );
418 orig_addr = (void *) KMP_INTERNAL_MALLOC( bytes );
419#endif /* THREAD_ALLOC_FOR_TASKQ */
420
421 if (orig_addr == 0)
422 KMP_FATAL( OutOfHeapMemory );
423
424 addr = orig_addr;
425
426 if (((kmp_uintptr_t) addr & ( CACHE_LINE - 1 )) != 0) {
427 KB_TRACE( 50, ("__kmp_taskq_allocate: adjust for cache alignment\n" ) );
428 addr = (void *) (((kmp_uintptr_t) addr + CACHE_LINE) & ~( CACHE_LINE - 1 ));
429 }
430
431 (* (void **) addr) = orig_addr;
432
433 KB_TRACE( 10, ("__kmp_taskq_allocate: allocate: %p, use: %p - %p, size: %d, gtid: %d\n",
434 orig_addr, ((void **) addr) + 1, ((char *)(((void **) addr) + 1)) + size-1,
435 (int) size, global_tid ));
436
437 return ( ((void **) addr) + 1 );
438}
439
440static void
441__kmpc_taskq_free(void *p, kmp_int32 global_tid)
442{
443 KB_TRACE( 5, ("__kmpc_taskq_free: called addr=%p, gtid=%d\n", p, global_tid ) );
444
445 KB_TRACE(10, ("__kmpc_taskq_free: freeing: %p, gtid: %d\n", (*( ((void **) p)-1)), global_tid ));
446
447#ifdef THREAD_ALLOC_FOR_TASKQ
448 __kmp_thread_free( __kmp_thread_from_gtid(global_tid), *( ((void **) p)-1) );
449#else
450 KMP_INTERNAL_FREE( *( ((void **) p)-1) );
451#endif /* THREAD_ALLOC_FOR_TASKQ */
452}
453
454/* --------------------------------------------------------------------------- */
455
456/*
457 * Keep freed kmpc_task_queue_t on an internal freelist and recycle since
458 * they're of constant size.
459 */
460
461static kmpc_task_queue_t *
462__kmp_alloc_taskq ( kmp_taskq_t *tq, int in_parallel, kmp_int32 nslots, kmp_int32 nthunks,
463 kmp_int32 nshareds, kmp_int32 nproc, size_t sizeof_thunk,
464 size_t sizeof_shareds, kmpc_thunk_t **new_taskq_thunk, kmp_int32 global_tid )
465{
466 kmp_int32 i;
467 size_t bytes;
468 kmpc_task_queue_t *new_queue;
469 kmpc_aligned_shared_vars_t *shared_var_array;
470 char *shared_var_storage;
471 char *pt; /* for doing byte-adjusted address computations */
472
473 __kmp_acquire_lock( & tq->tq_freelist_lck, global_tid );
474
475 KMP_MB(); /* make sure data structures are in consistent state before querying them */
476 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
477
478 if( tq->tq_freelist ) {
479 new_queue = tq -> tq_freelist;
480 tq -> tq_freelist = tq -> tq_freelist -> tq.tq_next_free;
481
482 KMP_DEBUG_ASSERT(new_queue->tq_flags & TQF_DEALLOCATED);
483
484 new_queue->tq_flags = 0;
485
486 __kmp_release_lock( & tq->tq_freelist_lck, global_tid );
487 }
488 else {
489 __kmp_release_lock( & tq->tq_freelist_lck, global_tid );
490
491 new_queue = (kmpc_task_queue_t *) __kmp_taskq_allocate (sizeof (kmpc_task_queue_t), global_tid);
492 new_queue->tq_flags = 0;
493 }
494
495 /* space in the task queue for queue slots (allocate as one big chunk */
496 /* of storage including new_taskq_task space) */
497
498 sizeof_thunk += (CACHE_LINE - (sizeof_thunk % CACHE_LINE)); /* pad to cache line size */
499 pt = (char *) __kmp_taskq_allocate (nthunks * sizeof_thunk, global_tid);
500 new_queue->tq_thunk_space = (kmpc_thunk_t *)pt;
501 *new_taskq_thunk = (kmpc_thunk_t *)(pt + (nthunks - 1) * sizeof_thunk);
502
503 /* chain the allocated thunks into a freelist for this queue */
504
505 new_queue->tq_free_thunks = (kmpc_thunk_t *)pt;
506
507 for (i = 0; i < (nthunks - 2); i++) {
508 ((kmpc_thunk_t *)(pt+i*sizeof_thunk))->th.th_next_free = (kmpc_thunk_t *)(pt + (i+1)*sizeof_thunk);
509#ifdef KMP_DEBUG
510 ((kmpc_thunk_t *)(pt+i*sizeof_thunk))->th_flags = TQF_DEALLOCATED;
511#endif
512 }
513
514 ((kmpc_thunk_t *)(pt+(nthunks-2)*sizeof_thunk))->th.th_next_free = NULL;
515#ifdef KMP_DEBUG
516 ((kmpc_thunk_t *)(pt+(nthunks-2)*sizeof_thunk))->th_flags = TQF_DEALLOCATED;
517#endif
518
519 /* initialize the locks */
520
521 if (in_parallel) {
522 __kmp_init_lock( & new_queue->tq_link_lck );
523 __kmp_init_lock( & new_queue->tq_free_thunks_lck );
524 __kmp_init_lock( & new_queue->tq_queue_lck );
525 }
526
527 /* now allocate the slots */
528
529 bytes = nslots * sizeof (kmpc_aligned_queue_slot_t);
530 new_queue->tq_queue = (kmpc_aligned_queue_slot_t *) __kmp_taskq_allocate( bytes, global_tid );
531
532 /* space for array of pointers to shared variable structures */
533 sizeof_shareds += sizeof(kmpc_task_queue_t *);
534 sizeof_shareds += (CACHE_LINE - (sizeof_shareds % CACHE_LINE)); /* pad to cache line size */
535
536 bytes = nshareds * sizeof (kmpc_aligned_shared_vars_t);
537 shared_var_array = (kmpc_aligned_shared_vars_t *) __kmp_taskq_allocate ( bytes, global_tid);
538
539 bytes = nshareds * sizeof_shareds;
540 shared_var_storage = (char *) __kmp_taskq_allocate ( bytes, global_tid);
541
542 for (i=0; i<nshareds; i++) {
543 shared_var_array[i].ai_data = (kmpc_shared_vars_t *) (shared_var_storage + i*sizeof_shareds);
544 shared_var_array[i].ai_data->sv_queue = new_queue;
545 }
546 new_queue->tq_shareds = shared_var_array;
547
548
549 /* array for number of outstanding thunks per thread */
550
551 if (in_parallel) {
552 bytes = nproc * sizeof(kmpc_aligned_int32_t);
553 new_queue->tq_th_thunks = (kmpc_aligned_int32_t *) __kmp_taskq_allocate ( bytes, global_tid);
554 new_queue->tq_nproc = nproc;
555
556 for (i=0; i<nproc; i++)
557 new_queue->tq_th_thunks[i].ai_data = 0;
558 }
559
560 return new_queue;
561}
562
563static void
564__kmp_free_taskq (kmp_taskq_t *tq, kmpc_task_queue_t *p, int in_parallel, kmp_int32 global_tid)
565{
566 __kmpc_taskq_free(p->tq_thunk_space, global_tid);
567 __kmpc_taskq_free(p->tq_queue, global_tid);
568
569 /* free shared var structure storage */
570 __kmpc_taskq_free((void *) p->tq_shareds[0].ai_data, global_tid);
571
572 /* free array of pointers to shared vars storage */
573 __kmpc_taskq_free(p->tq_shareds, global_tid);
574
575#ifdef KMP_DEBUG
576 p->tq_first_child = NULL;
577 p->tq_next_child = NULL;
578 p->tq_prev_child = NULL;
579 p->tq_ref_count = -10;
580 p->tq_shareds = NULL;
581 p->tq_tasknum_queuing = 0;
582 p->tq_tasknum_serving = 0;
583 p->tq_queue = NULL;
584 p->tq_thunk_space = NULL;
585 p->tq_taskq_slot = NULL;
586 p->tq_free_thunks = NULL;
587 p->tq_nslots = 0;
588 p->tq_head = 0;
589 p->tq_tail = 0;
590 p->tq_nfull = 0;
591 p->tq_hiwat = 0;
592
593 if (in_parallel) {
594 int i;
595
596 for (i=0; i<p->tq_nproc; i++)
597 p->tq_th_thunks[i].ai_data = 0;
598 }
599 if ( __kmp_env_consistency_check )
600 p->tq_loc = NULL;
601 KMP_DEBUG_ASSERT( p->tq_flags & TQF_DEALLOCATED );
602 p->tq_flags = TQF_DEALLOCATED;
603#endif /* KMP_DEBUG */
604
605 if (in_parallel) {
606 __kmpc_taskq_free(p->tq_th_thunks, global_tid);
607 __kmp_destroy_lock(& p->tq_link_lck);
608 __kmp_destroy_lock(& p->tq_queue_lck);
609 __kmp_destroy_lock(& p->tq_free_thunks_lck);
610 }
611#ifdef KMP_DEBUG
612 p->tq_th_thunks = NULL;
613#endif /* KMP_DEBUG */
614
615 KMP_MB(); /* make sure data structures are in consistent state before querying them */
616 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
617
618 __kmp_acquire_lock( & tq->tq_freelist_lck, global_tid );
619 p->tq.tq_next_free = tq->tq_freelist;
620
621 tq->tq_freelist = p;
622 __kmp_release_lock( & tq->tq_freelist_lck, global_tid );
623}
624
625/*
626 * Once a group of thunks has been allocated for use in a particular queue,
627 * these are managed via a per-queue freelist.
628 * We force a check that there's always a thunk free if we need one.
629 */
630
631static kmpc_thunk_t *
632__kmp_alloc_thunk (kmpc_task_queue_t *queue, int in_parallel, kmp_int32 global_tid)
633{
634 kmpc_thunk_t *fl;
635
636 if (in_parallel) {
637 __kmp_acquire_lock(& queue->tq_free_thunks_lck, global_tid);
638
639 KMP_MB(); /* make sure data structures are in consistent state before querying them */
640 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
641 }
642
643 fl = queue->tq_free_thunks;
644
645 KMP_DEBUG_ASSERT (fl != NULL);
646
647 queue->tq_free_thunks = fl->th.th_next_free;
648 fl->th_flags = 0;
649
650 if (in_parallel)
651 __kmp_release_lock(& queue->tq_free_thunks_lck, global_tid);
652
653 return fl;
654}
655
656static void
657__kmp_free_thunk (kmpc_task_queue_t *queue, kmpc_thunk_t *p, int in_parallel, kmp_int32 global_tid)
658{
659#ifdef KMP_DEBUG
660 p->th_task = 0;
661 p->th_encl_thunk = 0;
662 p->th_status = 0;
663 p->th_tasknum = 0;
664 /* Also could zero pointers to private vars */
665#endif
666
667 if (in_parallel) {
668 __kmp_acquire_lock(& queue->tq_free_thunks_lck, global_tid);
669
670 KMP_MB(); /* make sure data structures are in consistent state before querying them */
671 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
672 }
673
674 p->th.th_next_free = queue->tq_free_thunks;
675 queue->tq_free_thunks = p;
676
677#ifdef KMP_DEBUG
678 p->th_flags = TQF_DEALLOCATED;
679#endif
680
681 if (in_parallel)
682 __kmp_release_lock(& queue->tq_free_thunks_lck, global_tid);
683}
684
685/* --------------------------------------------------------------------------- */
686
687/* returns nonzero if the queue just became full after the enqueue */
688
689static kmp_int32
690__kmp_enqueue_task ( kmp_taskq_t *tq, kmp_int32 global_tid, kmpc_task_queue_t *queue, kmpc_thunk_t *thunk, int in_parallel )
691{
692 kmp_int32 ret;
693
694 /* dkp: can we get around the lock in the TQF_RELEASE_WORKERS case (only the master is executing then) */
695 if (in_parallel) {
696 __kmp_acquire_lock(& queue->tq_queue_lck, global_tid);
697
698 KMP_MB(); /* make sure data structures are in consistent state before querying them */
699 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
700 }
701
702 KMP_DEBUG_ASSERT (queue->tq_nfull < queue->tq_nslots); /* check queue not full */
703
704 queue->tq_queue[(queue->tq_head)++].qs_thunk = thunk;
705
706 if (queue->tq_head >= queue->tq_nslots)
707 queue->tq_head = 0;
708
709 (queue->tq_nfull)++;
710
711 KMP_MB(); /* to assure that nfull is seen to increase before TQF_ALL_TASKS_QUEUED is set */
712
713 ret = (in_parallel) ? (queue->tq_nfull == queue->tq_nslots) : FALSE;
714
715 if (in_parallel) {
716 /* don't need to wait until workers are released before unlocking */
717 __kmp_release_lock(& queue->tq_queue_lck, global_tid);
718
719 if( tq->tq_global_flags & TQF_RELEASE_WORKERS ) {
720 /* If just creating the root queue, the worker threads are waiting at */
721 /* a join barrier until now, when there's something in the queue for */
722 /* them to do; release them now to do work. */
723 /* This should only be done when this is the first task enqueued, */
724 /* so reset the flag here also. */
725
726 tq->tq_global_flags &= ~TQF_RELEASE_WORKERS; /* no lock needed, workers are still in spin mode */
727
728 KMP_MB(); /* avoid releasing barrier twice if taskq_task switches threads */
729
730 __kmpc_end_barrier_master( NULL, global_tid);
731 }
732 }
733
734 return ret;
735}
736
737static kmpc_thunk_t *
738__kmp_dequeue_task (kmp_int32 global_tid, kmpc_task_queue_t *queue, int in_parallel)
739{
740 kmpc_thunk_t *pt;
741 int tid = __kmp_tid_from_gtid( global_tid );
742
743 KMP_DEBUG_ASSERT (queue->tq_nfull > 0); /* check queue not empty */
744
745 if (queue->tq.tq_parent != NULL && in_parallel) {
746 int ct;
747 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
748 ct = ++(queue->tq_ref_count);
749 __kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
750 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p inc %d\n",
751 __LINE__, global_tid, queue, ct));
752 }
753
754 pt = queue->tq_queue[(queue->tq_tail)++].qs_thunk;
755
756 if (queue->tq_tail >= queue->tq_nslots)
757 queue->tq_tail = 0;
758
759 if (in_parallel) {
760 queue->tq_th_thunks[tid].ai_data++;
761
762 KMP_MB(); /* necessary so ai_data increment is propagated to other threads immediately (digital) */
763
764 KF_TRACE(200, ("__kmp_dequeue_task: T#%d(:%d) now has %d outstanding thunks from queue %p\n",
765 global_tid, tid, queue->tq_th_thunks[tid].ai_data, queue));
766 }
767
768 (queue->tq_nfull)--;
769
770#ifdef KMP_DEBUG
771 KMP_MB();
772
773 /* necessary so (queue->tq_nfull > 0) above succeeds after tq_nfull is decremented */
774
775 KMP_DEBUG_ASSERT(queue->tq_nfull >= 0);
776
777 if (in_parallel) {
778 KMP_DEBUG_ASSERT(queue->tq_th_thunks[tid].ai_data <= __KMP_TASKQ_THUNKS_PER_TH);
779 }
780#endif
781
782 return pt;
783}
784
785/*
786 * Find the next (non-null) task to dequeue and return it.
787 * This is never called unless in_parallel=TRUE
788 *
789 * Here are the rules for deciding which queue to take the task from:
790 * 1. Walk up the task queue tree from the current queue's parent and look
791 * on the way up (for loop, below).
792 * 2. Do a depth-first search back down the tree from the root and
793 * look (find_task_in_descandent_queue()).
794 *
795 * Here are the rules for deciding which task to take from a queue
796 * (__kmp_find_task_in_queue ()):
797 * 1. Never take the last task from a queue if TQF_IS_LASTPRIVATE; this task
798 * must be staged to make sure we execute the last one with
799 * TQF_IS_LAST_TASK at the end of task queue execution.
800 * 2. If the queue length is below some high water mark and the taskq task
801 * is enqueued, prefer running the taskq task.
802 * 3. Otherwise, take a (normal) task from the queue.
803 *
804 * If we do all this and return pt == NULL at the bottom of this routine,
805 * this means there are no more tasks to execute (except possibly for
806 * TQF_IS_LASTPRIVATE).
807 */
808
809static kmpc_thunk_t *
810__kmp_find_task_in_queue (kmp_int32 global_tid, kmpc_task_queue_t *queue)
811{
812 kmpc_thunk_t *pt = NULL;
813 int tid = __kmp_tid_from_gtid( global_tid );
814
815 /* To prevent deadlock from tq_queue_lck if queue already deallocated */
816 if ( !(queue->tq_flags & TQF_DEALLOCATED) ) {
817
818 __kmp_acquire_lock(& queue->tq_queue_lck, global_tid);
819
820 /* Check again to avoid race in __kmpc_end_taskq() */
821 if ( !(queue->tq_flags & TQF_DEALLOCATED) ) {
822
823 KMP_MB(); /* make sure data structures are in consistent state before querying them */
824 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
825
826 if ((queue->tq_taskq_slot != NULL) && (queue->tq_nfull <= queue->tq_hiwat)) {
827 /* if there's enough room in the queue and the dispatcher */
828 /* (taskq task) is available, schedule more tasks */
829 pt = (kmpc_thunk_t *) queue->tq_taskq_slot;
830 queue->tq_taskq_slot = NULL;
831 }
832 else if (queue->tq_nfull == 0 ||
833 queue->tq_th_thunks[tid].ai_data >= __KMP_TASKQ_THUNKS_PER_TH) {
834 /* do nothing if no thunks available or this thread can't */
835 /* run any because it already is executing too many */
836
837 pt = NULL;
838 }
839 else if (queue->tq_nfull > 1) {
840 /* always safe to schedule a task even if TQF_IS_LASTPRIVATE */
841
842 pt = __kmp_dequeue_task (global_tid, queue, TRUE);
843 }
844 else if (!(queue->tq_flags & TQF_IS_LASTPRIVATE)) {
845 /* one thing in queue, always safe to schedule if !TQF_IS_LASTPRIVATE */
846
847 pt = __kmp_dequeue_task (global_tid, queue, TRUE);
848 }
849 else if (queue->tq_flags & TQF_IS_LAST_TASK) {
850 /* TQF_IS_LASTPRIVATE, one thing in queue, kmpc_end_taskq_task() */
851 /* has been run so this is last task, run with TQF_IS_LAST_TASK so */
852 /* instrumentation does copy-out. */
853
854 pt = __kmp_dequeue_task (global_tid, queue, TRUE);
855 pt->th_flags |= TQF_IS_LAST_TASK; /* don't need test_then_or since already locked */
856 }
857 }
858
859 /* GEH - What happens here if is lastprivate, but not last task? */
860 __kmp_release_lock(& queue->tq_queue_lck, global_tid);
861 }
862
863 return pt;
864}
865
866/*
867 * Walk a tree of queues starting at queue's first child
868 * and return a non-NULL thunk if one can be scheduled.
869 * Must only be called when in_parallel=TRUE
870 */
871
872static kmpc_thunk_t *
873__kmp_find_task_in_descendant_queue (kmp_int32 global_tid, kmpc_task_queue_t *curr_queue)
874{
875 kmpc_thunk_t *pt = NULL;
876 kmpc_task_queue_t *queue = curr_queue;
877
878 if (curr_queue->tq_first_child != NULL) {
879 __kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
880
881 KMP_MB(); /* make sure data structures are in consistent state before querying them */
882 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
883
884 queue = (kmpc_task_queue_t *) curr_queue->tq_first_child;
885 if (queue == NULL) {
886 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
887 return NULL;
888 }
889
890 while (queue != NULL) {
891 int ct;
892 kmpc_task_queue_t *next;
893
894 ct= ++(queue->tq_ref_count);
895 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
896 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p inc %d\n",
897 __LINE__, global_tid, queue, ct));
898
899 pt = __kmp_find_task_in_queue (global_tid, queue);
900
901 if (pt != NULL) {
902 int ct;
903
904 __kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
905
906 KMP_MB(); /* make sure data structures are in consistent state before querying them */
907 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
908
909 ct = --(queue->tq_ref_count);
910 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n",
911 __LINE__, global_tid, queue, ct));
912 KMP_DEBUG_ASSERT( queue->tq_ref_count >= 0 );
913
914 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
915
916 return pt;
917 }
918
919 /* although reference count stays active during descendant walk, shouldn't matter */
920 /* since if children still exist, reference counts aren't being monitored anyway */
921
922 pt = __kmp_find_task_in_descendant_queue (global_tid, queue);
923
924 if (pt != NULL) {
925 int ct;
926
927 __kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
928
929 KMP_MB(); /* make sure data structures are in consistent state before querying them */
930 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
931
932 ct = --(queue->tq_ref_count);
933 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n",
934 __LINE__, global_tid, queue, ct));
935 KMP_DEBUG_ASSERT( ct >= 0 );
936
937 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
938
939 return pt;
940 }
941
942 __kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
943
944 KMP_MB(); /* make sure data structures are in consistent state before querying them */
945 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
946
947 next = queue->tq_next_child;
948
949 ct = --(queue->tq_ref_count);
950 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n",
951 __LINE__, global_tid, queue, ct));
952 KMP_DEBUG_ASSERT( ct >= 0 );
953
954 queue = next;
955 }
956
957 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
958 }
959
960 return pt;
961}
962
963/*
964 * Walk up the taskq tree looking for a task to execute.
965 * If we get to the root, search the tree for a descendent queue task.
966 * Must only be called when in_parallel=TRUE
967 */
968
969static kmpc_thunk_t *
970__kmp_find_task_in_ancestor_queue (kmp_taskq_t *tq, kmp_int32 global_tid, kmpc_task_queue_t *curr_queue)
971{
972 kmpc_task_queue_t *queue;
973 kmpc_thunk_t *pt;
974
975 pt = NULL;
976
977 if (curr_queue->tq.tq_parent != NULL) {
978 queue = curr_queue->tq.tq_parent;
979
980 while (queue != NULL) {
981 if (queue->tq.tq_parent != NULL) {
982 int ct;
983 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
984
985 KMP_MB(); /* make sure data structures are in consistent state before querying them */
986 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
987
988 ct = ++(queue->tq_ref_count);
989 __kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
990 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p inc %d\n",
991 __LINE__, global_tid, queue, ct));
992 }
993
994 pt = __kmp_find_task_in_queue (global_tid, queue);
995 if (pt != NULL) {
996 if (queue->tq.tq_parent != NULL) {
997 int ct;
998 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
999
1000 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1001 /* Seems to work without this call for digital/alpha, needed for IBM/RS6000 */
1002
1003 ct = --(queue->tq_ref_count);
1004 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n",
1005 __LINE__, global_tid, queue, ct));
1006 KMP_DEBUG_ASSERT( ct >= 0 );
1007
1008 __kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1009 }
1010
1011 return pt;
1012 }
1013
1014 if (queue->tq.tq_parent != NULL) {
1015 int ct;
1016 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1017
1018 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1019 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
1020
1021 ct = --(queue->tq_ref_count);
1022 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n",
1023 __LINE__, global_tid, queue, ct));
1024 KMP_DEBUG_ASSERT( ct >= 0 );
1025 }
1026 queue = queue->tq.tq_parent;
1027
1028 if (queue != NULL)
1029 __kmp_release_lock(& queue->tq_link_lck, global_tid);
1030 }
1031
1032 }
1033
1034 pt = __kmp_find_task_in_descendant_queue( global_tid, tq->tq_root );
1035
1036 return pt;
1037}
1038
1039static int
1040__kmp_taskq_tasks_finished (kmpc_task_queue_t *queue)
1041{
1042 int i;
1043
1044 /* KMP_MB(); *//* is this really necessary? */
1045
1046 for (i=0; i<queue->tq_nproc; i++) {
1047 if (queue->tq_th_thunks[i].ai_data != 0)
1048 return FALSE;
1049 }
1050
1051 return TRUE;
1052}
1053
1054static int
1055__kmp_taskq_has_any_children (kmpc_task_queue_t *queue)
1056{
1057 return (queue->tq_first_child != NULL);
1058}
1059
1060static void
1061__kmp_remove_queue_from_tree( kmp_taskq_t *tq, kmp_int32 global_tid, kmpc_task_queue_t *queue, int in_parallel )
1062{
1063#ifdef KMP_DEBUG
1064 kmp_int32 i;
1065 kmpc_thunk_t *thunk;
1066#endif
1067
1068 KF_TRACE(50, ("Before Deletion of TaskQ at %p on (%d):\n", queue, global_tid));
1069 KF_DUMP(50, __kmp_dump_task_queue( tq, queue, global_tid ));
1070
1071 /* sub-queue in a recursion, not the root task queue */
1072 KMP_DEBUG_ASSERT (queue->tq.tq_parent != NULL);
1073
1074 if (in_parallel) {
1075 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1076
1077 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1078 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
1079 }
1080
1081 KMP_DEBUG_ASSERT (queue->tq_first_child == NULL);
1082
1083 /* unlink queue from its siblings if any at this level */
1084 if (queue->tq_prev_child != NULL)
1085 queue->tq_prev_child->tq_next_child = queue->tq_next_child;
1086 if (queue->tq_next_child != NULL)
1087 queue->tq_next_child->tq_prev_child = queue->tq_prev_child;
1088 if (queue->tq.tq_parent->tq_first_child == queue)
1089 queue->tq.tq_parent->tq_first_child = queue->tq_next_child;
1090
1091 queue->tq_prev_child = NULL;
1092 queue->tq_next_child = NULL;
1093
1094 if (in_parallel) {
1095 kmp_uint32 spins;
1096
1097 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p waiting for ref_count of %d to reach 1\n",
1098 __LINE__, global_tid, queue, queue->tq_ref_count));
1099
1100 /* wait until all other threads have stopped accessing this queue */
1101 while (queue->tq_ref_count > 1) {
1102 __kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1103
1104 KMP_WAIT_YIELD((volatile kmp_uint32*)&queue->tq_ref_count, 1, KMP_LE, NULL);
1105
1106 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1107
1108 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1109 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
1110 }
1111
1112 __kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1113 }
1114
1115 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p freeing queue\n",
1116 __LINE__, global_tid, queue));
1117
1118#ifdef KMP_DEBUG
1119 KMP_DEBUG_ASSERT(queue->tq_flags & TQF_ALL_TASKS_QUEUED);
1120 KMP_DEBUG_ASSERT(queue->tq_nfull == 0);
1121
1122 for (i=0; i<queue->tq_nproc; i++) {
1123 KMP_DEBUG_ASSERT(queue->tq_th_thunks[i].ai_data == 0);
1124 }
1125
1126 i = 0;
1127 for (thunk=queue->tq_free_thunks; thunk != NULL; thunk=thunk->th.th_next_free)
1128 ++i;
1129
1130 KMP_ASSERT (i == queue->tq_nslots + (queue->tq_nproc * __KMP_TASKQ_THUNKS_PER_TH));
1131#endif
1132
1133 /* release storage for queue entry */
1134 __kmp_free_taskq ( tq, queue, TRUE, global_tid );
1135
1136 KF_TRACE(50, ("After Deletion of TaskQ at %p on (%d):\n", queue, global_tid));
1137 KF_DUMP(50, __kmp_dump_task_queue_tree( tq, tq->tq_root, global_tid ));
1138}
1139
1140/*
1141 * Starting from indicated queue, proceed downward through tree and
1142 * remove all taskqs which are finished, but only go down to taskqs
1143 * which have the "nowait" clause present. Assume this is only called
1144 * when in_parallel=TRUE.
1145 */
1146
1147static void
1148__kmp_find_and_remove_finished_child_taskq( kmp_taskq_t *tq, kmp_int32 global_tid, kmpc_task_queue_t *curr_queue )
1149{
1150 kmpc_task_queue_t *queue = curr_queue;
1151
1152 if (curr_queue->tq_first_child != NULL) {
1153 __kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
1154
1155 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1156 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
1157
1158 queue = (kmpc_task_queue_t *) curr_queue->tq_first_child;
1159 if (queue != NULL) {
1160 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
1161 return;
1162 }
1163
1164 while (queue != NULL) {
1165 kmpc_task_queue_t *next;
1166 int ct = ++(queue->tq_ref_count);
1167 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p inc %d\n",
1168 __LINE__, global_tid, queue, ct));
1169
1170
1171 /* although reference count stays active during descendant walk, */
1172 /* shouldn't matter since if children still exist, reference */
1173 /* counts aren't being monitored anyway */
1174
1175 if (queue->tq_flags & TQF_IS_NOWAIT) {
1176 __kmp_find_and_remove_finished_child_taskq ( tq, global_tid, queue );
1177
1178 if ((queue->tq_flags & TQF_ALL_TASKS_QUEUED) && (queue->tq_nfull == 0) &&
1179 __kmp_taskq_tasks_finished(queue) && ! __kmp_taskq_has_any_children(queue)) {
1180
1181 /*
1182 Only remove this if we have not already marked it for deallocation.
1183 This should prevent multiple threads from trying to free this.
1184 */
1185
1186 if ( __kmp_test_lock(& queue->tq_queue_lck, global_tid) ) {
1187 if ( !(queue->tq_flags & TQF_DEALLOCATED) ) {
1188 queue->tq_flags |= TQF_DEALLOCATED;
1189 __kmp_release_lock(& queue->tq_queue_lck, global_tid);
1190
1191 __kmp_remove_queue_from_tree( tq, global_tid, queue, TRUE );
1192
1193 /* Can't do any more here since can't be sure where sibling queue is so just exit this level */
1194 return;
1195 }
1196 else {
1197 __kmp_release_lock(& queue->tq_queue_lck, global_tid);
1198 }
1199 }
1200 /* otherwise, just fall through and decrement reference count */
1201 }
1202 }
1203
1204 __kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
1205
1206 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1207 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
1208
1209 next = queue->tq_next_child;
1210
1211 ct = --(queue->tq_ref_count);
1212 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n",
1213 __LINE__, global_tid, queue, ct));
1214 KMP_DEBUG_ASSERT( ct >= 0 );
1215
1216 queue = next;
1217 }
1218
1219 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
1220 }
1221}
1222
1223/*
1224 * Starting from indicated queue, proceed downward through tree and
1225 * remove all taskq's assuming all are finished and
1226 * assuming NO other threads are executing at this point.
1227 */
1228
1229static void
1230__kmp_remove_all_child_taskq( kmp_taskq_t *tq, kmp_int32 global_tid, kmpc_task_queue_t *queue )
1231{
1232 kmpc_task_queue_t *next_child;
1233
1234 queue = (kmpc_task_queue_t *) queue->tq_first_child;
1235
1236 while (queue != NULL) {
1237 __kmp_remove_all_child_taskq ( tq, global_tid, queue );
1238
1239 next_child = queue->tq_next_child;
1240 queue->tq_flags |= TQF_DEALLOCATED;
1241 __kmp_remove_queue_from_tree ( tq, global_tid, queue, FALSE );
1242 queue = next_child;
1243 }
1244}
1245
1246static void
1247__kmp_execute_task_from_queue( kmp_taskq_t *tq, ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *thunk, int in_parallel )
1248{
1249 kmpc_task_queue_t *queue = thunk->th.th_shareds->sv_queue;
1250 kmp_int32 tid = __kmp_tid_from_gtid( global_tid );
1251
1252 KF_TRACE(100, ("After dequeueing this Task on (%d):\n", global_tid));
1253 KF_DUMP(100, __kmp_dump_thunk( tq, thunk, global_tid ));
1254 KF_TRACE(100, ("Task Queue: %p looks like this (%d):\n", queue, global_tid));
1255 KF_DUMP(100, __kmp_dump_task_queue( tq, queue, global_tid ));
1256
1257 /*
1258 * For the taskq task, the curr_thunk pushes and pop pairs are set up as follows:
1259 *
1260 * happens exactly once:
1261 * 1) __kmpc_taskq : push (if returning thunk only)
1262 * 4) __kmpc_end_taskq_task : pop
1263 *
1264 * optionally happens *each* time taskq task is dequeued/enqueued:
1265 * 2) __kmpc_taskq_task : pop
1266 * 3) __kmp_execute_task_from_queue : push
1267 *
1268 * execution ordering: 1,(2,3)*,4
1269 */
1270
1271 if (!(thunk->th_flags & TQF_TASKQ_TASK)) {
1272 kmp_int32 index = (queue == tq->tq_root) ? tid : 0;
1273 thunk->th.th_shareds = (kmpc_shared_vars_t *) queue->tq_shareds[index].ai_data;
1274
1275 if ( __kmp_env_consistency_check ) {
1276 __kmp_push_workshare( global_tid,
1277 (queue->tq_flags & TQF_IS_ORDERED) ? ct_task_ordered : ct_task,
1278 queue->tq_loc );
1279 }
1280 }
1281 else {
1282 if ( __kmp_env_consistency_check )
1283 __kmp_push_workshare( global_tid, ct_taskq, queue->tq_loc );
1284 }
1285
1286 if (in_parallel) {
1287 thunk->th_encl_thunk = tq->tq_curr_thunk[tid];
1288 tq->tq_curr_thunk[tid] = thunk;
1289
1290 KF_DUMP( 200, __kmp_dump_thunk_stack( tq->tq_curr_thunk[tid], global_tid ));
1291 }
1292
1293 KF_TRACE( 50, ("Begin Executing Thunk %p from queue %p on (%d)\n", thunk, queue, global_tid));
1294 thunk->th_task (global_tid, thunk);
1295 KF_TRACE( 50, ("End Executing Thunk %p from queue %p on (%d)\n", thunk, queue, global_tid));
1296
1297 if (!(thunk->th_flags & TQF_TASKQ_TASK)) {
1298 if ( __kmp_env_consistency_check )
1299 __kmp_pop_workshare( global_tid, (queue->tq_flags & TQF_IS_ORDERED) ? ct_task_ordered : ct_task,
1300 queue->tq_loc );
1301
1302 if (in_parallel) {
1303 tq->tq_curr_thunk[tid] = thunk->th_encl_thunk;
1304 thunk->th_encl_thunk = NULL;
1305 KF_DUMP( 200, __kmp_dump_thunk_stack( tq->tq_curr_thunk[tid], global_tid ));
1306 }
1307
1308 if ((thunk->th_flags & TQF_IS_ORDERED) && in_parallel) {
1309 __kmp_taskq_check_ordered(global_tid, thunk);
1310 }
1311
1312 __kmp_free_thunk (queue, thunk, in_parallel, global_tid);
1313
1314 KF_TRACE(100, ("T#%d After freeing thunk: %p, TaskQ looks like this:\n", global_tid, thunk));
1315 KF_DUMP(100, __kmp_dump_task_queue( tq, queue, global_tid ));
1316
1317 if (in_parallel) {
1318 KMP_MB(); /* needed so thunk put on free list before outstanding thunk count is decremented */
1319
1320 KMP_DEBUG_ASSERT(queue->tq_th_thunks[tid].ai_data >= 1);
1321
1322 KF_TRACE( 200, ("__kmp_execute_task_from_queue: T#%d has %d thunks in queue %p\n",
1323 global_tid, queue->tq_th_thunks[tid].ai_data-1, queue));
1324
1325 queue->tq_th_thunks[tid].ai_data--;
1326
1327 /* KMP_MB(); */ /* is MB really necessary ? */
1328 }
1329
1330 if (queue->tq.tq_parent != NULL && in_parallel) {
1331 int ct;
1332 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1333 ct = --(queue->tq_ref_count);
1334 __kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1335 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n",
1336 __LINE__, global_tid, queue, ct));
1337 KMP_DEBUG_ASSERT( ct >= 0 );
1338 }
1339 }
1340}
1341
1342/* --------------------------------------------------------------------------- */
1343
1344/* starts a taskq; creates and returns a thunk for the taskq_task */
1345/* also, returns pointer to shared vars for this thread in "shareds" arg */
1346
1347kmpc_thunk_t *
1348__kmpc_taskq( ident_t *loc, kmp_int32 global_tid, kmpc_task_t taskq_task,
1349 size_t sizeof_thunk, size_t sizeof_shareds,
1350 kmp_int32 flags, kmpc_shared_vars_t **shareds )
1351{
1352 int in_parallel;
1353 kmp_int32 nslots, nthunks, nshareds, nproc;
1354 kmpc_task_queue_t *new_queue, *curr_queue;
1355 kmpc_thunk_t *new_taskq_thunk;
1356 kmp_info_t *th;
1357 kmp_team_t *team;
1358 kmp_taskq_t *tq;
1359 kmp_int32 tid;
1360
1361 KE_TRACE( 10, ("__kmpc_taskq called (%d)\n", global_tid));
1362
1363 th = __kmp_threads[ global_tid ];
1364 team = th -> th.th_team;
1365 tq = & team -> t.t_taskq;
1366 nproc = team -> t.t_nproc;
1367 tid = __kmp_tid_from_gtid( global_tid );
1368
1369 /* find out whether this is a parallel taskq or serialized one. */
1370 in_parallel = in_parallel_context( team );
1371
1372 if( ! tq->tq_root ) {
1373 if (in_parallel) {
1374 /* Vector ORDERED SECTION to taskq version */
1375 th->th.th_dispatch->th_deo_fcn = __kmp_taskq_eo;
1376
1377 /* Vector ORDERED SECTION to taskq version */
1378 th->th.th_dispatch->th_dxo_fcn = __kmp_taskq_xo;
1379 }
1380
1381 if (in_parallel) {
1382 /* This shouldn't be a barrier region boundary, it will confuse the user. */
1383 /* Need the boundary to be at the end taskq instead. */
1384 if ( __kmp_barrier( bs_plain_barrier, global_tid, TRUE, 0, NULL, NULL )) {
1385 /* Creating the active root queue, and we are not the master thread. */
1386 /* The master thread below created the queue and tasks have been */
1387 /* enqueued, and the master thread released this barrier. This */
1388 /* worker thread can now proceed and execute tasks. See also the */
1389 /* TQF_RELEASE_WORKERS which is used to handle this case. */
1390
1391 *shareds = (kmpc_shared_vars_t *) tq->tq_root->tq_shareds[tid].ai_data;
1392
1393 KE_TRACE( 10, ("__kmpc_taskq return (%d)\n", global_tid));
1394
1395 return NULL;
1396 }
1397 }
1398
1399 /* master thread only executes this code */
1400
1401 if( tq->tq_curr_thunk_capacity < nproc ) {
1402 int i;
1403
1404 if(tq->tq_curr_thunk)
1405 __kmp_free(tq->tq_curr_thunk);
1406 else {
1407 /* only need to do this once at outer level, i.e. when tq_curr_thunk is still NULL */
1408 __kmp_init_lock( & tq->tq_freelist_lck );
1409 }
1410
1411 tq->tq_curr_thunk = (kmpc_thunk_t **) __kmp_allocate( nproc * sizeof(kmpc_thunk_t *) );
1412 tq -> tq_curr_thunk_capacity = nproc;
1413 }
1414
1415 if (in_parallel)
1416 tq->tq_global_flags = TQF_RELEASE_WORKERS;
1417 }
1418
1419 /* dkp: in future, if flags & TQF_HEURISTICS, will choose nslots based */
1420 /* on some heuristics (e.g., depth of queue nesting?). */
1421
1422 nslots = (in_parallel) ? (2 * nproc) : 1;
1423
1424 /* There must be nproc * __KMP_TASKQ_THUNKS_PER_TH extra slots for pending */
1425 /* jobs being executed by other threads, and one extra for taskq slot */
1426
1427 nthunks = (in_parallel) ? (nslots + (nproc * __KMP_TASKQ_THUNKS_PER_TH) + 1) : nslots + 2;
1428
1429 /* Only the root taskq gets a per-thread array of shareds. */
1430 /* The rest of the taskq's only get one copy of the shared vars. */
1431
1432 nshareds = ( !tq->tq_root && in_parallel) ? nproc : 1;
1433
1434 /* create overall queue data structure and its components that require allocation */
1435
1436 new_queue = __kmp_alloc_taskq ( tq, in_parallel, nslots, nthunks, nshareds, nproc,
1437 sizeof_thunk, sizeof_shareds, &new_taskq_thunk, global_tid );
1438
1439 /* rest of new_queue initializations */
1440
1441 new_queue->tq_flags = flags & TQF_INTERFACE_FLAGS;
1442
1443 if (in_parallel) {
1444 new_queue->tq_tasknum_queuing = 0;
1445 new_queue->tq_tasknum_serving = 0;
1446 new_queue->tq_flags |= TQF_PARALLEL_CONTEXT;
1447 }
1448
1449 new_queue->tq_taskq_slot = NULL;
1450 new_queue->tq_nslots = nslots;
1451 new_queue->tq_hiwat = HIGH_WATER_MARK (nslots);
1452 new_queue->tq_nfull = 0;
1453 new_queue->tq_head = 0;
1454 new_queue->tq_tail = 0;
1455 new_queue->tq_loc = loc;
1456
1457 if ((new_queue->tq_flags & TQF_IS_ORDERED) && in_parallel) {
1458 /* prepare to serve the first-queued task's ORDERED directive */
1459 new_queue->tq_tasknum_serving = 1;
1460
1461 /* Vector ORDERED SECTION to taskq version */
1462 th->th.th_dispatch->th_deo_fcn = __kmp_taskq_eo;
1463
1464 /* Vector ORDERED SECTION to taskq version */
1465 th->th.th_dispatch->th_dxo_fcn = __kmp_taskq_xo;
1466 }
1467
1468 /* create a new thunk for the taskq_task in the new_queue */
1469 *shareds = (kmpc_shared_vars_t *) new_queue->tq_shareds[0].ai_data;
1470
1471 new_taskq_thunk->th.th_shareds = *shareds;
1472 new_taskq_thunk->th_task = taskq_task;
1473 new_taskq_thunk->th_flags = new_queue->tq_flags | TQF_TASKQ_TASK;
1474 new_taskq_thunk->th_status = 0;
1475
1476 KMP_DEBUG_ASSERT (new_taskq_thunk->th_flags & TQF_TASKQ_TASK);
1477
1478 /* KMP_MB(); */ /* make sure these inits complete before threads start using this queue (necessary?) */
1479
1480 /* insert the new task queue into the tree, but only after all fields initialized */
1481
1482 if (in_parallel) {
1483 if( ! tq->tq_root ) {
1484 new_queue->tq.tq_parent = NULL;
1485 new_queue->tq_first_child = NULL;
1486 new_queue->tq_next_child = NULL;
1487 new_queue->tq_prev_child = NULL;
1488 new_queue->tq_ref_count = 1;
1489 tq->tq_root = new_queue;
1490 }
1491 else {
1492 curr_queue = tq->tq_curr_thunk[tid]->th.th_shareds->sv_queue;
1493 new_queue->tq.tq_parent = curr_queue;
1494 new_queue->tq_first_child = NULL;
1495 new_queue->tq_prev_child = NULL;
1496 new_queue->tq_ref_count = 1; /* for this the thread that built the queue */
1497
1498 KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p alloc %d\n",
1499 __LINE__, global_tid, new_queue, new_queue->tq_ref_count));
1500
1501 __kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);
1502
1503 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1504 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
1505
1506 new_queue->tq_next_child = (struct kmpc_task_queue_t *) curr_queue->tq_first_child;
1507
1508 if (curr_queue->tq_first_child != NULL)
1509 curr_queue->tq_first_child->tq_prev_child = new_queue;
1510
1511 curr_queue->tq_first_child = new_queue;
1512
1513 __kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
1514 }
1515
1516 /* set up thunk stack only after code that determines curr_queue above */
1517 new_taskq_thunk->th_encl_thunk = tq->tq_curr_thunk[tid];
1518 tq->tq_curr_thunk[tid] = new_taskq_thunk;
1519
1520 KF_DUMP( 200, __kmp_dump_thunk_stack( tq->tq_curr_thunk[tid], global_tid ));
1521 }
1522 else {
1523 new_taskq_thunk->th_encl_thunk = 0;
1524 new_queue->tq.tq_parent = NULL;
1525 new_queue->tq_first_child = NULL;
1526 new_queue->tq_next_child = NULL;
1527 new_queue->tq_prev_child = NULL;
1528 new_queue->tq_ref_count = 1;
1529 }
1530
1531#ifdef KMP_DEBUG
1532 KF_TRACE(150, ("Creating TaskQ Task on (%d):\n", global_tid));
1533 KF_DUMP(150, __kmp_dump_thunk( tq, new_taskq_thunk, global_tid ));
1534
1535 if (in_parallel) {
1536 KF_TRACE(25, ("After TaskQ at %p Creation on (%d):\n", new_queue, global_tid));
1537 } else {
1538 KF_TRACE(25, ("After Serial TaskQ at %p Creation on (%d):\n", new_queue, global_tid));
1539 }
1540
1541 KF_DUMP(25, __kmp_dump_task_queue( tq, new_queue, global_tid ));
1542
1543 if (in_parallel) {
1544 KF_DUMP(50, __kmp_dump_task_queue_tree( tq, tq->tq_root, global_tid ));
1545 }
1546#endif /* KMP_DEBUG */
1547
1548 if ( __kmp_env_consistency_check )
1549 __kmp_push_workshare( global_tid, ct_taskq, new_queue->tq_loc );
1550
1551 KE_TRACE( 10, ("__kmpc_taskq return (%d)\n", global_tid));
1552
1553 return new_taskq_thunk;
1554}
1555
1556
1557/* ends a taskq; last thread out destroys the queue */
1558
1559void
1560__kmpc_end_taskq(ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *taskq_thunk)
1561{
1562#ifdef KMP_DEBUG
1563 kmp_int32 i;
1564#endif
1565 kmp_taskq_t *tq;
1566 int in_parallel;
1567 kmp_info_t *th;
1568 kmp_int32 is_outermost;
1569 kmpc_task_queue_t *queue;
1570 kmpc_thunk_t *thunk;
1571 int nproc;
1572
1573 KE_TRACE( 10, ("__kmpc_end_taskq called (%d)\n", global_tid));
1574
1575 tq = & __kmp_threads[global_tid] -> th.th_team -> t.t_taskq;
1576 nproc = __kmp_threads[global_tid] -> th.th_team -> t.t_nproc;
1577
1578 /* For the outermost taskq only, all but one thread will have taskq_thunk == NULL */
1579 queue = (taskq_thunk == NULL) ? tq->tq_root : taskq_thunk->th.th_shareds->sv_queue;
1580
1581 KE_TRACE( 50, ("__kmpc_end_taskq queue=%p (%d) \n", queue, global_tid));
1582 is_outermost = (queue == tq->tq_root);
1583 in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);
1584
1585 if (in_parallel) {
1586 kmp_uint32 spins;
1587
1588 /* this is just a safeguard to release the waiting threads if */
1589 /* the outermost taskq never queues a task */
1590
1591 if (is_outermost && (KMP_MASTER_GTID( global_tid ))) {
1592 if( tq->tq_global_flags & TQF_RELEASE_WORKERS ) {
1593 /* no lock needed, workers are still in spin mode */
1594 tq->tq_global_flags &= ~TQF_RELEASE_WORKERS;
1595
1596 __kmp_end_split_barrier( bs_plain_barrier, global_tid );
1597 }
1598 }
1599
1600 /* keep dequeueing work until all tasks are queued and dequeued */
1601
1602 do {
1603 /* wait until something is available to dequeue */
1604 KMP_INIT_YIELD(spins);
1605
1606 while ( (queue->tq_nfull == 0)
1607 && (queue->tq_taskq_slot == NULL)
1608 && (! __kmp_taskq_has_any_children(queue) )
1609 && (! (queue->tq_flags & TQF_ALL_TASKS_QUEUED) )
1610 ) {
1611 __kmp_static_delay( 1 );
1612 KMP_YIELD_WHEN( TRUE, spins );
1613 }
1614
1615 /* check to see if we can execute tasks in the queue */
1616 while ( ( (queue->tq_nfull != 0) || (queue->tq_taskq_slot != NULL) )
1617 && (thunk = __kmp_find_task_in_queue(global_tid, queue)) != NULL
1618 ) {
1619 KF_TRACE(50, ("Found thunk: %p in primary queue %p (%d)\n", thunk, queue, global_tid));
1620 __kmp_execute_task_from_queue( tq, loc, global_tid, thunk, in_parallel );
1621 }
1622
1623 /* see if work found can be found in a descendant queue */
1624 if ( (__kmp_taskq_has_any_children(queue))
1625 && (thunk = __kmp_find_task_in_descendant_queue(global_tid, queue)) != NULL
1626 ) {
1627
1628 KF_TRACE(50, ("Stole thunk: %p in descendant queue: %p while waiting in queue: %p (%d)\n",
1629 thunk, thunk->th.th_shareds->sv_queue, queue, global_tid ));
1630
1631 __kmp_execute_task_from_queue( tq, loc, global_tid, thunk, in_parallel );
1632 }
1633
1634 } while ( (! (queue->tq_flags & TQF_ALL_TASKS_QUEUED))
1635 || (queue->tq_nfull != 0)
1636 );
1637
1638 KF_TRACE(50, ("All tasks queued and dequeued in queue: %p (%d)\n", queue, global_tid));
1639
1640 /* wait while all tasks are not finished and more work found
1641 in descendant queues */
1642
1643 while ( (!__kmp_taskq_tasks_finished(queue))
1644 && (thunk = __kmp_find_task_in_descendant_queue(global_tid, queue)) != NULL
1645 ) {
1646
1647 KF_TRACE(50, ("Stole thunk: %p in descendant queue: %p while waiting in queue: %p (%d)\n",
1648 thunk, thunk->th.th_shareds->sv_queue, queue, global_tid));
1649
1650 __kmp_execute_task_from_queue( tq, loc, global_tid, thunk, in_parallel );
1651 }
1652
1653 KF_TRACE(50, ("No work found in descendent queues or all work finished in queue: %p (%d)\n", queue, global_tid));
1654
1655 if (!is_outermost) {
1656 /* need to return if NOWAIT present and not outermost taskq */
1657
1658 if (queue->tq_flags & TQF_IS_NOWAIT) {
1659 __kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1660 queue->tq_ref_count--;
1661 KMP_DEBUG_ASSERT( queue->tq_ref_count >= 0 );
1662 __kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);
1663
1664 KE_TRACE( 10, ("__kmpc_end_taskq return for nowait case (%d)\n", global_tid));
1665
1666 return;
1667 }
1668
1669 __kmp_find_and_remove_finished_child_taskq( tq, global_tid, queue );
1670
1671 /* WAIT until all tasks are finished and no child queues exist before proceeding */
1672 KMP_INIT_YIELD(spins);
1673
1674 while (!__kmp_taskq_tasks_finished(queue) || __kmp_taskq_has_any_children(queue)) {
1675 thunk = __kmp_find_task_in_ancestor_queue( tq, global_tid, queue );
1676
1677 if (thunk != NULL) {
1678 KF_TRACE(50, ("Stole thunk: %p in ancestor queue: %p while waiting in queue: %p (%d)\n",
1679 thunk, thunk->th.th_shareds->sv_queue, queue, global_tid));
1680 __kmp_execute_task_from_queue( tq, loc, global_tid, thunk, in_parallel );
1681 }
1682
1683 KMP_YIELD_WHEN( thunk == NULL, spins );
1684
1685 __kmp_find_and_remove_finished_child_taskq( tq, global_tid, queue );
1686 }
1687
1688 __kmp_acquire_lock(& queue->tq_queue_lck, global_tid);
1689 if ( !(queue->tq_flags & TQF_DEALLOCATED) ) {
1690 queue->tq_flags |= TQF_DEALLOCATED;
1691 }
1692 __kmp_release_lock(& queue->tq_queue_lck, global_tid);
1693
1694 /* only the allocating thread can deallocate the queue */
1695 if (taskq_thunk != NULL) {
1696 __kmp_remove_queue_from_tree( tq, global_tid, queue, TRUE );
1697 }
1698
1699 KE_TRACE( 10, ("__kmpc_end_taskq return for non_outermost queue, wait case (%d)\n", global_tid));
1700
1701 return;
1702 }
1703
1704 /* Outermost Queue: steal work from descendants until all tasks are finished */
1705
1706 KMP_INIT_YIELD(spins);
1707
1708 while (!__kmp_taskq_tasks_finished(queue)) {
1709 thunk = __kmp_find_task_in_descendant_queue(global_tid, queue);
1710
1711 if (thunk != NULL) {
1712 KF_TRACE(50, ("Stole thunk: %p in descendant queue: %p while waiting in queue: %p (%d)\n",
1713 thunk, thunk->th.th_shareds->sv_queue, queue, global_tid));
1714
1715 __kmp_execute_task_from_queue( tq, loc, global_tid, thunk, in_parallel );
1716 }
1717
1718 KMP_YIELD_WHEN( thunk == NULL, spins );
1719 }
1720
1721 /* Need this barrier to prevent destruction of queue before threads have all executed above code */
1722 /* This may need to be done earlier when NOWAIT is implemented for the outermost level */
1723
1724 if ( !__kmp_barrier( bs_plain_barrier, global_tid, TRUE, 0, NULL, NULL )) {
1725 /* the queue->tq_flags & TQF_IS_NOWAIT case is not yet handled here; */
1726 /* for right now, everybody waits, and the master thread destroys the */
1727 /* remaining queues. */
1728
1729 __kmp_remove_all_child_taskq( tq, global_tid, queue );
1730
1731 /* Now destroy the root queue */
1732 KF_TRACE(100, ("T#%d Before Deletion of top-level TaskQ at %p:\n", global_tid, queue ));
1733 KF_DUMP(100, __kmp_dump_task_queue( tq, queue, global_tid ));
1734
1735#ifdef KMP_DEBUG
1736 /* the root queue entry */
1737 KMP_DEBUG_ASSERT ((queue->tq.tq_parent == NULL) && (queue->tq_next_child == NULL));
1738
1739 /* children must all be gone by now because of barrier above */
1740 KMP_DEBUG_ASSERT (queue->tq_first_child == NULL);
1741
1742 for (i=0; i<nproc; i++) {
1743 KMP_DEBUG_ASSERT(queue->tq_th_thunks[i].ai_data == 0);
1744 }
1745
1746 for (i=0, thunk=queue->tq_free_thunks; thunk != NULL; i++, thunk=thunk->th.th_next_free);
1747
1748 KMP_DEBUG_ASSERT (i == queue->tq_nslots + (nproc * __KMP_TASKQ_THUNKS_PER_TH));
1749
1750 for (i = 0; i < nproc; i++) {
1751 KMP_DEBUG_ASSERT( ! tq->tq_curr_thunk[i] );
1752 }
1753#endif
1754 /* unlink the root queue entry */
1755 tq -> tq_root = NULL;
1756
1757 /* release storage for root queue entry */
1758 KF_TRACE(50, ("After Deletion of top-level TaskQ at %p on (%d):\n", queue, global_tid));
1759
1760 queue->tq_flags |= TQF_DEALLOCATED;
1761 __kmp_free_taskq ( tq, queue, in_parallel, global_tid );
1762
1763 KF_DUMP(50, __kmp_dump_task_queue_tree( tq, tq->tq_root, global_tid ));
1764
1765 /* release the workers now that the data structures are up to date */
1766 __kmp_end_split_barrier( bs_plain_barrier, global_tid );
1767 }
1768
1769 th = __kmp_threads[ global_tid ];
1770
1771 /* Reset ORDERED SECTION to parallel version */
1772 th->th.th_dispatch->th_deo_fcn = 0;
1773
1774 /* Reset ORDERED SECTION to parallel version */
1775 th->th.th_dispatch->th_dxo_fcn = 0;
1776 }
1777 else {
1778 /* in serial execution context, dequeue the last task */
1779 /* and execute it, if there were any tasks encountered */
1780
1781 if (queue->tq_nfull > 0) {
1782 KMP_DEBUG_ASSERT(queue->tq_nfull == 1);
1783
1784 thunk = __kmp_dequeue_task(global_tid, queue, in_parallel);
1785
1786 if (queue->tq_flags & TQF_IS_LAST_TASK) {
1787 /* TQF_IS_LASTPRIVATE, one thing in queue, __kmpc_end_taskq_task() */
1788 /* has been run so this is last task, run with TQF_IS_LAST_TASK so */
1789 /* instrumentation does copy-out. */
1790
1791 /* no need for test_then_or call since already locked */
1792 thunk->th_flags |= TQF_IS_LAST_TASK;
1793 }
1794
1795 KF_TRACE(50, ("T#%d found thunk: %p in serial queue: %p\n", global_tid, thunk, queue));
1796
1797 __kmp_execute_task_from_queue( tq, loc, global_tid, thunk, in_parallel );
1798 }
1799
1800 /* destroy the unattached serial queue now that there is no more work to do */
1801 KF_TRACE(100, ("Before Deletion of Serialized TaskQ at %p on (%d):\n", queue, global_tid));
1802 KF_DUMP(100, __kmp_dump_task_queue( tq, queue, global_tid ));
1803
1804#ifdef KMP_DEBUG
1805 i = 0;
1806 for (thunk=queue->tq_free_thunks; thunk != NULL; thunk=thunk->th.th_next_free)
1807 ++i;
1808 KMP_DEBUG_ASSERT (i == queue->tq_nslots + 1);
1809#endif
1810 /* release storage for unattached serial queue */
1811 KF_TRACE(50, ("Serialized TaskQ at %p deleted on (%d).\n", queue, global_tid));
1812
1813 queue->tq_flags |= TQF_DEALLOCATED;
1814 __kmp_free_taskq ( tq, queue, in_parallel, global_tid );
1815 }
1816
1817 KE_TRACE( 10, ("__kmpc_end_taskq return (%d)\n", global_tid));
1818}
1819
1820/* Enqueues a task for thunk previously created by __kmpc_task_buffer. */
1821/* Returns nonzero if just filled up queue */
1822
1823kmp_int32
1824__kmpc_task(ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *thunk)
1825{
1826 kmp_int32 ret;
1827 kmpc_task_queue_t *queue;
1828 int in_parallel;
1829 kmp_taskq_t *tq;
1830
1831 KE_TRACE( 10, ("__kmpc_task called (%d)\n", global_tid));
1832
1833 KMP_DEBUG_ASSERT (!(thunk->th_flags & TQF_TASKQ_TASK)); /* thunk->th_task is a regular task */
1834
1835 tq = &__kmp_threads[global_tid] -> th.th_team -> t.t_taskq;
1836 queue = thunk->th.th_shareds->sv_queue;
1837 in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);
1838
1839 if (in_parallel && (thunk->th_flags & TQF_IS_ORDERED))
1840 thunk->th_tasknum = ++queue->tq_tasknum_queuing;
1841
1842 /* For serial execution dequeue the preceding task and execute it, if one exists */
1843 /* This cannot be the last task. That one is handled in __kmpc_end_taskq */
1844
1845 if (!in_parallel && queue->tq_nfull > 0) {
1846 kmpc_thunk_t *prev_thunk;
1847
1848 KMP_DEBUG_ASSERT(queue->tq_nfull == 1);
1849
1850 prev_thunk = __kmp_dequeue_task(global_tid, queue, in_parallel);
1851
1852 KF_TRACE(50, ("T#%d found thunk: %p in serial queue: %p\n", global_tid, prev_thunk, queue));
1853
1854 __kmp_execute_task_from_queue( tq, loc, global_tid, prev_thunk, in_parallel );
1855 }
1856
1857 /* The instrumentation sequence is: __kmpc_task_buffer(), initialize private */
1858 /* variables, __kmpc_task(). The __kmpc_task_buffer routine checks that the */
1859 /* task queue is not full and allocates a thunk (which is then passed to */
1860 /* __kmpc_task()). So, the enqueue below should never fail due to a full queue. */
1861
1862 KF_TRACE(100, ("After enqueueing this Task on (%d):\n", global_tid));
1863 KF_DUMP(100, __kmp_dump_thunk( tq, thunk, global_tid ));
1864
1865 ret = __kmp_enqueue_task ( tq, global_tid, queue, thunk, in_parallel );
1866
1867 KF_TRACE(100, ("Task Queue looks like this on (%d):\n", global_tid));
1868 KF_DUMP(100, __kmp_dump_task_queue( tq, queue, global_tid ));
1869
1870 KE_TRACE( 10, ("__kmpc_task return (%d)\n", global_tid));
1871
1872 return ret;
1873}
1874
1875/* enqueues a taskq_task for thunk previously created by __kmpc_taskq */
1876/* this should never be called unless in a parallel context */
1877
1878void
1879__kmpc_taskq_task(ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *thunk, kmp_int32 status)
1880{
1881 kmpc_task_queue_t *queue;
1882 kmp_taskq_t *tq = &__kmp_threads[global_tid] -> th.th_team -> t.t_taskq;
1883 int tid = __kmp_tid_from_gtid( global_tid );
1884
1885 KE_TRACE( 10, ("__kmpc_taskq_task called (%d)\n", global_tid));
1886 KF_TRACE(100, ("TaskQ Task argument thunk on (%d):\n", global_tid));
1887 KF_DUMP(100, __kmp_dump_thunk( tq, thunk, global_tid ));
1888
1889 queue = thunk->th.th_shareds->sv_queue;
1890
1891 if ( __kmp_env_consistency_check )
1892 __kmp_pop_workshare( global_tid, ct_taskq, loc );
1893
1894 /* thunk->th_task is the taskq_task */
1895 KMP_DEBUG_ASSERT (thunk->th_flags & TQF_TASKQ_TASK);
1896
1897 /* not supposed to call __kmpc_taskq_task if it's already enqueued */
1898 KMP_DEBUG_ASSERT (queue->tq_taskq_slot == NULL);
1899
1900 /* dequeue taskq thunk from curr_thunk stack */
1901 tq->tq_curr_thunk[tid] = thunk->th_encl_thunk;
1902 thunk->th_encl_thunk = NULL;
1903
1904 KF_DUMP( 200, __kmp_dump_thunk_stack( tq->tq_curr_thunk[tid], global_tid ));
1905
1906 thunk->th_status = status;
1907
1908 KMP_MB(); /* flush thunk->th_status before taskq_task enqueued to avoid race condition */
1909
1910 /* enqueue taskq_task in thunk into special slot in queue */
1911 /* GEH - probably don't need to lock taskq slot since only one */
1912 /* thread enqueues & already a lock set at dequeue point */
1913
1914 queue->tq_taskq_slot = thunk;
1915
1916 KE_TRACE( 10, ("__kmpc_taskq_task return (%d)\n", global_tid));
1917}
1918
1919/* ends a taskq_task; done generating tasks */
1920
1921void
1922__kmpc_end_taskq_task(ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *thunk)
1923{
1924 kmp_taskq_t *tq;
1925 kmpc_task_queue_t *queue;
1926 int in_parallel;
1927 int tid;
1928
1929 KE_TRACE( 10, ("__kmpc_end_taskq_task called (%d)\n", global_tid));
1930
1931 tq = &__kmp_threads[global_tid] -> th.th_team -> t.t_taskq;
1932 queue = thunk->th.th_shareds->sv_queue;
1933 in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);
1934 tid = __kmp_tid_from_gtid( global_tid );
1935
1936 if ( __kmp_env_consistency_check )
1937 __kmp_pop_workshare( global_tid, ct_taskq, loc );
1938
1939 if (in_parallel) {
1940#if KMP_ARCH_X86 || \
1941 KMP_ARCH_X86_64
1942
1943 KMP_TEST_THEN_OR32( &queue->tq_flags, (kmp_int32) TQF_ALL_TASKS_QUEUED );
1944#else
1945 {
1946 __kmp_acquire_lock(& queue->tq_queue_lck, global_tid);
1947
1948 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1949 /* Seems to work fine without this call for digital/alpha, needed for IBM/RS6000 */
1950
1951 queue->tq_flags |= TQF_ALL_TASKS_QUEUED;
1952
1953 __kmp_release_lock(& queue->tq_queue_lck, global_tid);
1954 }
1955#endif
1956 }
1957
1958 if (thunk->th_flags & TQF_IS_LASTPRIVATE) {
1959 /* Normally, __kmp_find_task_in_queue() refuses to schedule the last task in the */
1960 /* queue if TQF_IS_LASTPRIVATE so we can positively identify that last task */
1961 /* and run it with its TQF_IS_LAST_TASK bit turned on in th_flags. When */
1962 /* __kmpc_end_taskq_task() is called we are done generating all the tasks, so */
1963 /* we know the last one in the queue is the lastprivate task. Mark the queue */
1964 /* as having gotten to this state via tq_flags & TQF_IS_LAST_TASK; when that */
1965 /* task actually executes mark it via th_flags & TQF_IS_LAST_TASK (this th_flags */
1966 /* bit signals the instrumented code to do copy-outs after execution). */
1967
1968 if (! in_parallel) {
1969 /* No synchronization needed for serial context */
1970 queue->tq_flags |= TQF_IS_LAST_TASK;
1971 }
1972 else {
1973#if KMP_ARCH_X86 || \
1974 KMP_ARCH_X86_64
1975
1976 KMP_TEST_THEN_OR32( &queue->tq_flags, (kmp_int32) TQF_IS_LAST_TASK );
1977#else
1978 {
1979 __kmp_acquire_lock(& queue->tq_queue_lck, global_tid);
1980
1981 KMP_MB(); /* make sure data structures are in consistent state before querying them */
1982 /* Seems to work without this call for digital/alpha, needed for IBM/RS6000 */
1983
1984 queue->tq_flags |= TQF_IS_LAST_TASK;
1985
1986 __kmp_release_lock(& queue->tq_queue_lck, global_tid);
1987 }
1988#endif
1989 /* to prevent race condition where last task is dequeued but */
1990 /* flag isn't visible yet (not sure about this) */
1991 KMP_MB();
1992 }
1993 }
1994
1995 /* dequeue taskq thunk from curr_thunk stack */
1996 if (in_parallel) {
1997 tq->tq_curr_thunk[tid] = thunk->th_encl_thunk;
1998 thunk->th_encl_thunk = NULL;
1999
2000 KF_DUMP( 200, __kmp_dump_thunk_stack( tq->tq_curr_thunk[tid], global_tid ));
2001 }
2002
2003 KE_TRACE( 10, ("__kmpc_end_taskq_task return (%d)\n", global_tid));
2004}
2005
2006/* returns thunk for a regular task based on taskq_thunk */
2007/* (__kmpc_taskq_task does the analogous thing for a TQF_TASKQ_TASK) */
2008
2009kmpc_thunk_t *
2010__kmpc_task_buffer(ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *taskq_thunk, kmpc_task_t task)
2011{
2012 kmp_taskq_t *tq;
2013 kmpc_task_queue_t *queue;
2014 kmpc_thunk_t *new_thunk;
2015 int in_parallel;
2016
2017 KE_TRACE( 10, ("__kmpc_task_buffer called (%d)\n", global_tid));
2018
2019 KMP_DEBUG_ASSERT (taskq_thunk->th_flags & TQF_TASKQ_TASK); /* taskq_thunk->th_task is the taskq_task */
2020
2021 tq = &__kmp_threads[global_tid] -> th.th_team -> t.t_taskq;
2022 queue = taskq_thunk->th.th_shareds->sv_queue;
2023 in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);
2024
2025 /* The instrumentation sequence is: __kmpc_task_buffer(), initialize private */
2026 /* variables, __kmpc_task(). The __kmpc_task_buffer routine checks that the */
2027 /* task queue is not full and allocates a thunk (which is then passed to */
2028 /* __kmpc_task()). So, we can pre-allocate a thunk here assuming it will be */
2029 /* the next to be enqueued in __kmpc_task(). */
2030
2031 new_thunk = __kmp_alloc_thunk (queue, in_parallel, global_tid);
2032 new_thunk->th.th_shareds = (kmpc_shared_vars_t *) queue->tq_shareds[0].ai_data;
2033 new_thunk->th_encl_thunk = NULL;
2034 new_thunk->th_task = task;
2035
2036 /* GEH - shouldn't need to lock the read of tq_flags here */
2037 new_thunk->th_flags = queue->tq_flags & TQF_INTERFACE_FLAGS;
2038
2039 new_thunk->th_status = 0;
2040
2041 KMP_DEBUG_ASSERT (!(new_thunk->th_flags & TQF_TASKQ_TASK));
2042
2043 KF_TRACE(100, ("Creating Regular Task on (%d):\n", global_tid));
2044 KF_DUMP(100, __kmp_dump_thunk( tq, new_thunk, global_tid ));
2045
2046 KE_TRACE( 10, ("__kmpc_task_buffer return (%d)\n", global_tid));
2047
2048 return new_thunk;
2049}
2050
2051/* --------------------------------------------------------------------------- */