20 #include "kmp_wait_release.h" 33 #ifdef KMP_SUPPORT_GRAPH_OUTPUT 34 static kmp_int32 kmp_node_id_seed = 0;
37 static void __kmp_init_node(kmp_depnode_t *node) {
40 node->dn.successors = NULL;
41 __kmp_init_lock(&node->dn.lock);
43 #ifdef KMP_SUPPORT_GRAPH_OUTPUT 44 node->dn.id = KMP_TEST_THEN_INC32(&kmp_node_id_seed);
48 static inline kmp_depnode_t *__kmp_node_ref(kmp_depnode_t *node) {
49 KMP_TEST_THEN_INC32(CCAST(kmp_int32 *, &node->dn.nrefs));
53 static inline void __kmp_node_deref(kmp_info_t *thread, kmp_depnode_t *node) {
57 kmp_int32 n = KMP_TEST_THEN_DEC32(CCAST(kmp_int32 *, &node->dn.nrefs)) - 1;
59 KMP_ASSERT(node->dn.nrefs == 0);
61 __kmp_fast_free(thread, node);
63 __kmp_thread_free(thread, node);
68 #define KMP_ACQUIRE_DEPNODE(gtid, n) __kmp_acquire_lock(&(n)->dn.lock, (gtid)) 69 #define KMP_RELEASE_DEPNODE(gtid, n) __kmp_release_lock(&(n)->dn.lock, (gtid)) 71 static void __kmp_depnode_list_free(kmp_info_t *thread, kmp_depnode_list *list);
73 enum { KMP_DEPHASH_OTHER_SIZE = 97, KMP_DEPHASH_MASTER_SIZE = 997 };
75 static inline kmp_int32 __kmp_dephash_hash(kmp_intptr_t addr,
size_t hsize) {
78 return ((addr >> 6) ^ (addr >> 2)) % hsize;
81 static kmp_dephash_t *__kmp_dephash_create(kmp_info_t *thread,
82 kmp_taskdata_t *current_task) {
87 if (current_task->td_flags.tasktype == TASK_IMPLICIT)
88 h_size = KMP_DEPHASH_MASTER_SIZE;
90 h_size = KMP_DEPHASH_OTHER_SIZE;
93 h_size *
sizeof(kmp_dephash_entry_t *) +
sizeof(kmp_dephash_t);
96 h = (kmp_dephash_t *)__kmp_fast_allocate(thread, size);
98 h = (kmp_dephash_t *)__kmp_thread_malloc(thread, size);
106 h->buckets = (kmp_dephash_entry **)(h + 1);
108 for (
size_t i = 0; i < h_size; i++)
114 void __kmp_dephash_free_entries(kmp_info_t *thread, kmp_dephash_t *h) {
115 for (
size_t i = 0; i < h->size; i++) {
117 kmp_dephash_entry_t *next;
118 for (kmp_dephash_entry_t *entry = h->buckets[i]; entry; entry = next) {
119 next = entry->next_in_bucket;
120 __kmp_depnode_list_free(thread, entry->last_ins);
121 __kmp_node_deref(thread, entry->last_out);
123 __kmp_fast_free(thread, entry);
125 __kmp_thread_free(thread, entry);
133 void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h) {
134 __kmp_dephash_free_entries(thread, h);
136 __kmp_fast_free(thread, h);
138 __kmp_thread_free(thread, h);
142 static kmp_dephash_entry *
143 __kmp_dephash_find(kmp_info_t *thread, kmp_dephash_t *h, kmp_intptr_t addr) {
144 kmp_int32 bucket = __kmp_dephash_hash(addr, h->size);
146 kmp_dephash_entry_t *entry;
147 for (entry = h->buckets[bucket]; entry; entry = entry->next_in_bucket)
148 if (entry->addr == addr)
154 entry = (kmp_dephash_entry_t *)__kmp_fast_allocate(
155 thread,
sizeof(kmp_dephash_entry_t));
157 entry = (kmp_dephash_entry_t *)__kmp_thread_malloc(
158 thread,
sizeof(kmp_dephash_entry_t));
161 entry->last_out = NULL;
162 entry->last_ins = NULL;
163 entry->next_in_bucket = h->buckets[bucket];
164 h->buckets[bucket] = entry;
167 if (entry->next_in_bucket)
174 static kmp_depnode_list_t *__kmp_add_node(kmp_info_t *thread,
175 kmp_depnode_list_t *list,
176 kmp_depnode_t *node) {
177 kmp_depnode_list_t *new_head;
180 new_head = (kmp_depnode_list_t *)__kmp_fast_allocate(
181 thread,
sizeof(kmp_depnode_list_t));
183 new_head = (kmp_depnode_list_t *)__kmp_thread_malloc(
184 thread,
sizeof(kmp_depnode_list_t));
187 new_head->node = __kmp_node_ref(node);
188 new_head->next = list;
193 static void __kmp_depnode_list_free(kmp_info_t *thread,
194 kmp_depnode_list *list) {
195 kmp_depnode_list *next;
197 for (; list; list = next) {
200 __kmp_node_deref(thread, list->node);
202 __kmp_fast_free(thread, list);
204 __kmp_thread_free(thread, list);
209 static inline void __kmp_track_dependence(kmp_depnode_t *source,
211 kmp_task_t *sink_task) {
212 #ifdef KMP_SUPPORT_GRAPH_OUTPUT 213 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
216 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
218 __kmp_printf(
"%d(%s) -> %d(%s)\n", source->dn.id,
219 task_source->td_ident->psource, sink->dn.id,
220 task_sink->td_ident->psource);
222 #if OMPT_SUPPORT && OMPT_TRACE 226 ompt_callbacks.ompt_callback(ompt_event_task_dependence_pair)) {
227 kmp_taskdata_t *task_source = KMP_TASK_TO_TASKDATA(source->dn.task);
228 kmp_taskdata_t *task_sink = KMP_TASK_TO_TASKDATA(sink_task);
230 ompt_callbacks.ompt_callback(ompt_event_task_dependence_pair)(
231 task_source->ompt_task_info.task_id, task_sink->ompt_task_info.task_id);
236 template <
bool filter>
237 static inline kmp_int32
238 __kmp_process_deps(kmp_int32 gtid, kmp_depnode_t *node, kmp_dephash_t *hash,
239 bool dep_barrier, kmp_int32 ndeps,
240 kmp_depend_info_t *dep_list, kmp_task_t *task) {
241 KA_TRACE(30, (
"__kmp_process_deps<%d>: T#%d processing %d dependencies : " 242 "dep_barrier = %d\n",
243 filter, gtid, ndeps, dep_barrier));
245 kmp_info_t *thread = __kmp_threads[gtid];
246 kmp_int32 npredecessors = 0;
247 for (kmp_int32 i = 0; i < ndeps; i++) {
248 const kmp_depend_info_t *dep = &dep_list[i];
250 KMP_DEBUG_ASSERT(dep->flags.in);
252 if (filter && dep->base_addr == 0)
255 kmp_dephash_entry_t *info =
256 __kmp_dephash_find(thread, hash, dep->base_addr);
257 kmp_depnode_t *last_out = info->last_out;
259 if (dep->flags.out && info->last_ins) {
260 for (kmp_depnode_list_t *p = info->last_ins; p; p = p->next) {
261 kmp_depnode_t *indep = p->node;
262 if (indep->dn.task) {
263 KMP_ACQUIRE_DEPNODE(gtid, indep);
264 if (indep->dn.task) {
265 __kmp_track_dependence(indep, node, task);
266 indep->dn.successors =
267 __kmp_add_node(thread, indep->dn.successors, node);
268 KA_TRACE(40, (
"__kmp_process_deps<%d>: T#%d adding dependence from " 270 filter, gtid, KMP_TASK_TO_TASKDATA(indep->dn.task),
271 KMP_TASK_TO_TASKDATA(task)));
274 KMP_RELEASE_DEPNODE(gtid, indep);
278 __kmp_depnode_list_free(thread, info->last_ins);
279 info->last_ins = NULL;
281 }
else if (last_out && last_out->dn.task) {
282 KMP_ACQUIRE_DEPNODE(gtid, last_out);
283 if (last_out->dn.task) {
284 __kmp_track_dependence(last_out, node, task);
285 last_out->dn.successors =
286 __kmp_add_node(thread, last_out->dn.successors, node);
289 (
"__kmp_process_deps<%d>: T#%d adding dependence from %p to %p\n",
290 filter, gtid, KMP_TASK_TO_TASKDATA(last_out->dn.task),
291 KMP_TASK_TO_TASKDATA(task)));
295 KMP_RELEASE_DEPNODE(gtid, last_out);
302 __kmp_node_deref(thread, last_out);
303 info->last_out = NULL;
305 if (dep->flags.out) {
306 __kmp_node_deref(thread, last_out);
307 info->last_out = __kmp_node_ref(node);
309 info->last_ins = __kmp_add_node(thread, info->last_ins, node);
313 KA_TRACE(30, (
"__kmp_process_deps<%d>: T#%d found %d predecessors\n", filter,
314 gtid, npredecessors));
316 return npredecessors;
319 #define NO_DEP_BARRIER (false) 320 #define DEP_BARRIER (true) 323 static bool __kmp_check_deps(kmp_int32 gtid, kmp_depnode_t *node,
324 kmp_task_t *task, kmp_dephash_t *hash,
325 bool dep_barrier, kmp_int32 ndeps,
326 kmp_depend_info_t *dep_list,
327 kmp_int32 ndeps_noalias,
328 kmp_depend_info_t *noalias_dep_list) {
332 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
334 KA_TRACE(20, (
"__kmp_check_deps: T#%d checking dependencies for task %p : %d " 335 "possibly aliased dependencies, %d non-aliased depedencies : " 336 "dep_barrier=%d .\n",
337 gtid, taskdata, ndeps, ndeps_noalias, dep_barrier));
341 for (i = 0; i < ndeps; i++) {
342 if (dep_list[i].base_addr != 0)
343 for (
int j = i + 1; j < ndeps; j++)
344 if (dep_list[i].base_addr == dep_list[j].base_addr) {
345 dep_list[i].flags.in |= dep_list[j].flags.in;
346 dep_list[i].flags.out |= dep_list[j].flags.out;
347 dep_list[j].base_addr = 0;
355 node->dn.npredecessors = -1;
361 npredecessors = __kmp_process_deps<true>(gtid, node, hash, dep_barrier, ndeps,
363 npredecessors += __kmp_process_deps<false>(
364 gtid, node, hash, dep_barrier, ndeps_noalias, noalias_dep_list, task);
366 node->dn.task = task;
376 KMP_TEST_THEN_ADD32(CCAST(kmp_int32 *, &node->dn.npredecessors),
380 KA_TRACE(20, (
"__kmp_check_deps: T#%d found %d predecessors for task %p \n",
381 gtid, npredecessors, taskdata));
385 return npredecessors > 0 ? true :
false;
388 void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
389 kmp_info_t *thread = __kmp_threads[gtid];
390 kmp_depnode_t *node = task->td_depnode;
392 if (task->td_dephash) {
394 40, (
"__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
396 __kmp_dephash_free(thread, task->td_dephash);
397 task->td_dephash = NULL;
403 KA_TRACE(20, (
"__kmp_release_deps: T#%d notifying successors of task %p.\n",
406 KMP_ACQUIRE_DEPNODE(gtid, node);
409 KMP_RELEASE_DEPNODE(gtid, node);
411 kmp_depnode_list_t *next;
412 for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
413 kmp_depnode_t *successor = p->node;
414 kmp_int32 npredecessors =
415 KMP_TEST_THEN_DEC32(CCAST(kmp_int32 *, &successor->dn.npredecessors)) -
419 if (npredecessors == 0) {
421 if (successor->dn.task) {
422 KA_TRACE(20, (
"__kmp_release_deps: T#%d successor %p of %p scheduled " 424 gtid, successor->dn.task, task));
425 __kmp_omp_task(gtid, successor->dn.task,
false);
430 __kmp_node_deref(thread, p->node);
432 __kmp_fast_free(thread, p);
434 __kmp_thread_free(thread, p);
438 __kmp_node_deref(thread, node);
442 (
"__kmp_release_deps: T#%d all successors of %p notified of completion\n",
463 kmp_task_t *new_task, kmp_int32 ndeps,
464 kmp_depend_info_t *dep_list,
465 kmp_int32 ndeps_noalias,
466 kmp_depend_info_t *noalias_dep_list) {
468 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
469 KA_TRACE(10, (
"__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n", gtid,
470 loc_ref, new_taskdata));
472 kmp_info_t *thread = __kmp_threads[gtid];
473 kmp_taskdata_t *current_task = thread->th.th_current_task;
475 #if OMPT_SUPPORT && OMPT_TRACE 477 if (ompt_enabled && ndeps + ndeps_noalias > 0 &&
478 ompt_callbacks.ompt_callback(ompt_event_task_dependences)) {
481 new_taskdata->ompt_task_info.ndeps = ndeps + ndeps_noalias;
482 new_taskdata->ompt_task_info.deps =
483 (ompt_task_dependence_t *)KMP_OMPT_DEPS_ALLOC(
484 thread, (ndeps + ndeps_noalias) *
sizeof(ompt_task_dependence_t));
486 KMP_ASSERT(new_taskdata->ompt_task_info.deps != NULL);
488 for (i = 0; i < ndeps; i++) {
489 new_taskdata->ompt_task_info.deps[i].variable_addr =
490 (
void *)dep_list[i].base_addr;
491 if (dep_list[i].flags.in && dep_list[i].flags.out)
492 new_taskdata->ompt_task_info.deps[i].dependence_flags =
493 ompt_task_dependence_type_inout;
494 else if (dep_list[i].flags.out)
495 new_taskdata->ompt_task_info.deps[i].dependence_flags =
496 ompt_task_dependence_type_out;
497 else if (dep_list[i].flags.in)
498 new_taskdata->ompt_task_info.deps[i].dependence_flags =
499 ompt_task_dependence_type_in;
501 for (i = 0; i < ndeps_noalias; i++) {
502 new_taskdata->ompt_task_info.deps[ndeps + i].variable_addr =
503 (
void *)noalias_dep_list[i].base_addr;
504 if (noalias_dep_list[i].flags.in && noalias_dep_list[i].flags.out)
505 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
506 ompt_task_dependence_type_inout;
507 else if (noalias_dep_list[i].flags.out)
508 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
509 ompt_task_dependence_type_out;
510 else if (noalias_dep_list[i].flags.in)
511 new_taskdata->ompt_task_info.deps[ndeps + i].dependence_flags =
512 ompt_task_dependence_type_in;
517 bool serial = current_task->td_flags.team_serial ||
518 current_task->td_flags.tasking_ser ||
519 current_task->td_flags.final;
521 kmp_task_team_t *task_team = thread->th.th_task_team;
522 serial = serial && !(task_team && task_team->tt.tt_found_proxy_tasks);
525 if (!serial && (ndeps > 0 || ndeps_noalias > 0)) {
527 if (current_task->td_dephash == NULL)
528 current_task->td_dephash = __kmp_dephash_create(thread, current_task);
531 kmp_depnode_t *node =
532 (kmp_depnode_t *)__kmp_fast_allocate(thread,
sizeof(kmp_depnode_t));
534 kmp_depnode_t *node =
535 (kmp_depnode_t *)__kmp_thread_malloc(thread,
sizeof(kmp_depnode_t));
538 __kmp_init_node(node);
539 new_taskdata->td_depnode = node;
541 if (__kmp_check_deps(gtid, node, new_task, current_task->td_dephash,
542 NO_DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
544 KA_TRACE(10, (
"__kmpc_omp_task_with_deps(exit): T#%d task had blocking " 546 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
547 gtid, loc_ref, new_taskdata));
548 return TASK_CURRENT_NOT_QUEUED;
551 KA_TRACE(10, (
"__kmpc_omp_task_with_deps(exit): T#%d ignored dependencies " 552 "for task (serialized)" 554 gtid, loc_ref, new_taskdata));
557 KA_TRACE(10, (
"__kmpc_omp_task_with_deps(exit): T#%d task had no blocking " 559 "loc=%p task=%p, transferring to __kmpc_omp_task\n",
560 gtid, loc_ref, new_taskdata));
562 return __kmpc_omp_task(loc_ref, gtid, new_task);
577 kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
578 kmp_depend_info_t *noalias_dep_list) {
579 KA_TRACE(10, (
"__kmpc_omp_wait_deps(enter): T#%d loc=%p\n", gtid, loc_ref));
581 if (ndeps == 0 && ndeps_noalias == 0) {
582 KA_TRACE(10, (
"__kmpc_omp_wait_deps(exit): T#%d has no dependencies to " 583 "wait upon : loc=%p\n",
588 kmp_info_t *thread = __kmp_threads[gtid];
589 kmp_taskdata_t *current_task = thread->th.th_current_task;
594 bool ignore = current_task->td_flags.team_serial ||
595 current_task->td_flags.tasking_ser ||
596 current_task->td_flags.final;
598 ignore = ignore && thread->th.th_task_team != NULL &&
599 thread->th.th_task_team->tt.tt_found_proxy_tasks == FALSE;
601 ignore = ignore || current_task->td_dephash == NULL;
604 KA_TRACE(10, (
"__kmpc_omp_wait_deps(exit): T#%d has no blocking " 605 "dependencies : loc=%p\n",
611 __kmp_init_node(&node);
613 if (!__kmp_check_deps(gtid, &node, NULL, current_task->td_dephash,
614 DEP_BARRIER, ndeps, dep_list, ndeps_noalias,
616 KA_TRACE(10, (
"__kmpc_omp_wait_deps(exit): T#%d has no blocking " 617 "dependencies : loc=%p\n",
622 int thread_finished = FALSE;
623 kmp_flag_32 flag((
volatile kmp_uint32 *)&(node.dn.npredecessors), 0U);
624 while (node.dn.npredecessors > 0) {
625 flag.execute_tasks(thread, gtid, FALSE, &thread_finished,
629 __kmp_task_stealing_constraint);
632 KA_TRACE(10, (
"__kmpc_omp_wait_deps(exit): T#%d finished waiting : loc=%p\n",
void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list)
kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list)