kmp_taskdeps.h revision 353358
11638Srgrimes/*
21638Srgrimes * kmp_taskdeps.h
31638Srgrimes */
41638Srgrimes
51638Srgrimes
61638Srgrimes//===----------------------------------------------------------------------===//
71638Srgrimes//
81638Srgrimes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
91638Srgrimes// See https://llvm.org/LICENSE.txt for license information.
101638Srgrimes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
111638Srgrimes//
121638Srgrimes//===----------------------------------------------------------------------===//
131638Srgrimes
141638Srgrimes
151638Srgrimes#ifndef KMP_TASKDEPS_H
161638Srgrimes#define KMP_TASKDEPS_H
171638Srgrimes
181638Srgrimes#include "kmp.h"
191638Srgrimes
201638Srgrimes#define KMP_ACQUIRE_DEPNODE(gtid, n) __kmp_acquire_lock(&(n)->dn.lock, (gtid))
211638Srgrimes#define KMP_RELEASE_DEPNODE(gtid, n) __kmp_release_lock(&(n)->dn.lock, (gtid))
221638Srgrimes
231638Srgrimesstatic inline void __kmp_node_deref(kmp_info_t *thread, kmp_depnode_t *node) {
241638Srgrimes  if (!node)
251638Srgrimes    return;
261638Srgrimes
271638Srgrimes  kmp_int32 n = KMP_ATOMIC_DEC(&node->dn.nrefs) - 1;
281638Srgrimes  if (n == 0) {
291638Srgrimes    KMP_ASSERT(node->dn.nrefs == 0);
301638Srgrimes#if USE_FAST_MEMORY
311638Srgrimes    __kmp_fast_free(thread, node);
321638Srgrimes#else
3350476Speter    __kmp_thread_free(thread, node);
341638Srgrimes#endif
35288193Semaste  }
361638Srgrimes}
371638Srgrimes
381638Srgrimesstatic inline void __kmp_depnode_list_free(kmp_info_t *thread,
391638Srgrimes                                           kmp_depnode_list *list) {
40107788Sru  kmp_depnode_list *next;
411638Srgrimes
42107788Sru  for (; list; list = next) {
43130582Sru    next = list->next;
4471895Sru
45107788Sru    __kmp_node_deref(thread, list->node);
4671895Sru#if USE_FAST_MEMORY
471638Srgrimes    __kmp_fast_free(thread, list);
4871895Sru#else
4954338Sdcs    __kmp_thread_free(thread, list);
5054338Sdcs#endif
51130582Sru  }
5271895Sru}
5354338Sdcs
5454338Sdcsstatic inline void __kmp_dephash_free_entries(kmp_info_t *thread,
55273660Sian                                              kmp_dephash_t *h) {
56273660Sian  for (size_t i = 0; i < h->size; i++) {
57273660Sian    if (h->buckets[i]) {
58273660Sian      kmp_dephash_entry_t *next;
59273660Sian      for (kmp_dephash_entry_t *entry = h->buckets[i]; entry; entry = next) {
60273660Sian        next = entry->next_in_bucket;
61273660Sian        __kmp_depnode_list_free(thread, entry->last_ins);
62273660Sian        __kmp_depnode_list_free(thread, entry->last_mtxs);
6371895Sru        __kmp_node_deref(thread, entry->last_out);
6465526Sobrien        if (entry->mtx_lock) {
65273660Sian          __kmp_destroy_lock(entry->mtx_lock);
6671895Sru          __kmp_free(entry->mtx_lock);
6793959Smurray        }
6879727Sschweikh#if USE_FAST_MEMORY
6979727Sschweikh        __kmp_fast_free(thread, entry);
70273660Sian#else
71273660Sian        __kmp_thread_free(thread, entry);
72273660Sian#endif
73273660Sian      }
7454338Sdcs      h->buckets[i] = 0;
7578824Snik    }
7678824Snik  }
7778824Snik}
7878824Snik
7978824Snikstatic inline void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h) {
8078824Snik  __kmp_dephash_free_entries(thread, h);
8178824Snik#if USE_FAST_MEMORY
8279797Snik  __kmp_fast_free(thread, h);
8371895Sru#else
84138977Sjkoshy  __kmp_thread_free(thread, h);
85138977Sjkoshy#endif
861638Srgrimes}
87138977Sjkoshy
8879727Sschweikhstatic inline void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task) {
891638Srgrimes  kmp_info_t *thread = __kmp_threads[gtid];
901638Srgrimes  kmp_depnode_t *node = task->td_depnode;
911638Srgrimes
92138977Sjkoshy  if (task->td_dephash) {
93138977Sjkoshy    KA_TRACE(
941638Srgrimes        40, ("__kmp_release_deps: T#%d freeing dependencies hash of task %p.\n",
9571895Sru             gtid, task));
9679727Sschweikh    __kmp_dephash_free(thread, task->td_dephash);
971638Srgrimes    task->td_dephash = NULL;
98157693Sbrueffer  }
9971895Sru
10047597Snik  if (!node)
10147597Snik    return;
10247597Snik
103157693Sbrueffer  KA_TRACE(20, ("__kmp_release_deps: T#%d notifying successors of task %p.\n",
104157693Sbrueffer                gtid, task));
10571895Sru
10623524Swosch  KMP_ACQUIRE_DEPNODE(gtid, node);
10723524Swosch  node->dn.task =
10823524Swosch      NULL; // mark this task as finished, so no new dependencies are generated
10971895Sru  KMP_RELEASE_DEPNODE(gtid, node);
11078824Snik
11179727Sschweikh  kmp_depnode_list_t *next;
1121638Srgrimes  for (kmp_depnode_list_t *p = node->dn.successors; p; p = next) {
1131638Srgrimes    kmp_depnode_t *successor = p->node;
11415135Smpp    kmp_int32 npredecessors = KMP_ATOMIC_DEC(&successor->dn.npredecessors) - 1;
11593959Smurray
11693959Smurray    // successor task can be NULL for wait_depends or because deps are still
117162286Sjoel    // being processed
11871895Sru    if (npredecessors == 0) {
11947597Snik      KMP_MB();
12047597Snik      if (successor->dn.task) {
12147597Snik        KA_TRACE(20, ("__kmp_release_deps: T#%d successor %p of %p scheduled "
12247597Snik                      "for execution.\n",
123157693Sbrueffer                      gtid, successor->dn.task, task));
124157693Sbrueffer        __kmp_omp_task(gtid, successor->dn.task, false);
125157693Sbrueffer      }
126157693Sbrueffer    }
127157693Sbrueffer
128157693Sbrueffer    next = p->next;
129157693Sbrueffer    __kmp_node_deref(thread, p->node);
130157693Sbrueffer#if USE_FAST_MEMORY
13171895Sru    __kmp_fast_free(thread, p);
13223524Swosch#else
13379727Sschweikh    __kmp_thread_free(thread, p);
13423524Swosch#endif
135157693Sbrueffer  }
136157693Sbrueffer
137157693Sbrueffer  __kmp_node_deref(thread, node);
138157693Sbrueffer
13971895Sru  KA_TRACE(
14056900Skris      20,
1411638Srgrimes      ("__kmp_release_deps: T#%d all successors of %p notified of completion\n",
142122859Sgordon       gtid, task));
143122859Sgordon}
144122859Sgordon
145122859Sgordon#endif // KMP_TASKDEPS_H
146122859Sgordon