annotate libgomp/task.c @ 134:71d4882a9ac3

add cbc-example
author kono
date Thu, 08 Nov 2018 06:52:59 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1 /* Copyright (C) 2007-2018 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 Contributed by Richard Henderson <rth@redhat.com>.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
111
kono
parents: 0
diff changeset
4 This file is part of the GNU Offloading and Multi Processing Library
kono
parents: 0
diff changeset
5 (libgomp).
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 Libgomp is free software; you can redistribute it and/or modify it
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 any later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 /* This file handles the maintainence of tasks in response to task
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 creation and termination. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 #include "libgomp.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include <stdlib.h>
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 #include <string.h>
111
kono
parents: 0
diff changeset
32 #include "gomp-constants.h"
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
111
kono
parents: 0
diff changeset
34 typedef struct gomp_task_depend_entry *hash_entry_type;
kono
parents: 0
diff changeset
35
kono
parents: 0
diff changeset
36 static inline void *
kono
parents: 0
diff changeset
37 htab_alloc (size_t size)
kono
parents: 0
diff changeset
38 {
kono
parents: 0
diff changeset
39 return gomp_malloc (size);
kono
parents: 0
diff changeset
40 }
kono
parents: 0
diff changeset
41
kono
parents: 0
diff changeset
42 static inline void
kono
parents: 0
diff changeset
43 htab_free (void *ptr)
kono
parents: 0
diff changeset
44 {
kono
parents: 0
diff changeset
45 free (ptr);
kono
parents: 0
diff changeset
46 }
kono
parents: 0
diff changeset
47
kono
parents: 0
diff changeset
48 #include "hashtab.h"
kono
parents: 0
diff changeset
49
kono
parents: 0
diff changeset
50 static inline hashval_t
kono
parents: 0
diff changeset
51 htab_hash (hash_entry_type element)
kono
parents: 0
diff changeset
52 {
kono
parents: 0
diff changeset
53 return hash_pointer (element->addr);
kono
parents: 0
diff changeset
54 }
kono
parents: 0
diff changeset
55
kono
parents: 0
diff changeset
56 static inline bool
kono
parents: 0
diff changeset
57 htab_eq (hash_entry_type x, hash_entry_type y)
kono
parents: 0
diff changeset
58 {
kono
parents: 0
diff changeset
59 return x->addr == y->addr;
kono
parents: 0
diff changeset
60 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 /* Create a new task data structure. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 gomp_init_task (struct gomp_task *task, struct gomp_task *parent_task,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 struct gomp_task_icv *prev_icv)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 {
111
kono
parents: 0
diff changeset
68 /* It would seem that using memset here would be a win, but it turns
kono
parents: 0
diff changeset
69 out that partially filling gomp_task allows us to keep the
kono
parents: 0
diff changeset
70 overhead of task creation low. In the nqueens-1.c test, for a
kono
parents: 0
diff changeset
71 sufficiently large N, we drop the overhead from 5-6% to 1%.
kono
parents: 0
diff changeset
72
kono
parents: 0
diff changeset
73 Note, the nqueens-1.c test in serial mode is a good test to
kono
parents: 0
diff changeset
74 benchmark the overhead of creating tasks as there are millions of
kono
parents: 0
diff changeset
75 tiny tasks created that all run undeferred. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 task->parent = parent_task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 task->icv = *prev_icv;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 task->kind = GOMP_TASK_IMPLICIT;
111
kono
parents: 0
diff changeset
79 task->taskwait = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 task->in_tied_task = false;
111
kono
parents: 0
diff changeset
81 task->final_task = false;
kono
parents: 0
diff changeset
82 task->copy_ctors_done = false;
kono
parents: 0
diff changeset
83 task->parent_depends_on = false;
kono
parents: 0
diff changeset
84 priority_queue_init (&task->children_queue);
kono
parents: 0
diff changeset
85 task->taskgroup = NULL;
kono
parents: 0
diff changeset
86 task->dependers = NULL;
kono
parents: 0
diff changeset
87 task->depend_hash = NULL;
kono
parents: 0
diff changeset
88 task->depend_count = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 /* Clean up a task, after completing it. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 gomp_end_task (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 struct gomp_thread *thr = gomp_thread ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 struct gomp_task *task = thr->task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 gomp_finish_task (task);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 thr->task = task->parent;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102
111
kono
parents: 0
diff changeset
103 /* Clear the parent field of every task in LIST. */
kono
parents: 0
diff changeset
104
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 static inline void
111
kono
parents: 0
diff changeset
106 gomp_clear_parent_in_list (struct priority_list *list)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 {
111
kono
parents: 0
diff changeset
108 struct priority_node *p = list->tasks;
kono
parents: 0
diff changeset
109 if (p)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 do
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 {
111
kono
parents: 0
diff changeset
112 priority_node_to_task (PQ_CHILDREN, p)->parent = NULL;
kono
parents: 0
diff changeset
113 p = p->next;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 }
111
kono
parents: 0
diff changeset
115 while (p != list->tasks);
kono
parents: 0
diff changeset
116 }
kono
parents: 0
diff changeset
117
kono
parents: 0
diff changeset
118 /* Splay tree version of gomp_clear_parent_in_list.
kono
parents: 0
diff changeset
119
kono
parents: 0
diff changeset
120 Clear the parent field of every task in NODE within SP, and free
kono
parents: 0
diff changeset
121 the node when done. */
kono
parents: 0
diff changeset
122
kono
parents: 0
diff changeset
123 static void
kono
parents: 0
diff changeset
124 gomp_clear_parent_in_tree (prio_splay_tree sp, prio_splay_tree_node node)
kono
parents: 0
diff changeset
125 {
kono
parents: 0
diff changeset
126 if (!node)
kono
parents: 0
diff changeset
127 return;
kono
parents: 0
diff changeset
128 prio_splay_tree_node left = node->left, right = node->right;
kono
parents: 0
diff changeset
129 gomp_clear_parent_in_list (&node->key.l);
kono
parents: 0
diff changeset
130 #if _LIBGOMP_CHECKING_
kono
parents: 0
diff changeset
131 memset (node, 0xaf, sizeof (*node));
kono
parents: 0
diff changeset
132 #endif
kono
parents: 0
diff changeset
133 /* No need to remove the node from the tree. We're nuking
kono
parents: 0
diff changeset
134 everything, so just free the nodes and our caller can clear the
kono
parents: 0
diff changeset
135 entire splay tree. */
kono
parents: 0
diff changeset
136 free (node);
kono
parents: 0
diff changeset
137 gomp_clear_parent_in_tree (sp, left);
kono
parents: 0
diff changeset
138 gomp_clear_parent_in_tree (sp, right);
kono
parents: 0
diff changeset
139 }
kono
parents: 0
diff changeset
140
kono
parents: 0
diff changeset
141 /* Clear the parent field of every task in Q and remove every task
kono
parents: 0
diff changeset
142 from Q. */
kono
parents: 0
diff changeset
143
kono
parents: 0
diff changeset
144 static inline void
kono
parents: 0
diff changeset
145 gomp_clear_parent (struct priority_queue *q)
kono
parents: 0
diff changeset
146 {
kono
parents: 0
diff changeset
147 if (priority_queue_multi_p (q))
kono
parents: 0
diff changeset
148 {
kono
parents: 0
diff changeset
149 gomp_clear_parent_in_tree (&q->t, q->t.root);
kono
parents: 0
diff changeset
150 /* All the nodes have been cleared in gomp_clear_parent_in_tree.
kono
parents: 0
diff changeset
151 No need to remove anything. We can just nuke everything. */
kono
parents: 0
diff changeset
152 q->t.root = NULL;
kono
parents: 0
diff changeset
153 }
kono
parents: 0
diff changeset
154 else
kono
parents: 0
diff changeset
155 gomp_clear_parent_in_list (&q->l);
kono
parents: 0
diff changeset
156 }
kono
parents: 0
diff changeset
157
kono
parents: 0
diff changeset
158 /* Helper function for GOMP_task and gomp_create_target_task.
kono
parents: 0
diff changeset
159
kono
parents: 0
diff changeset
160 For a TASK with in/out dependencies, fill in the various dependency
kono
parents: 0
diff changeset
161 queues. PARENT is the parent of said task. DEPEND is as in
kono
parents: 0
diff changeset
162 GOMP_task. */
kono
parents: 0
diff changeset
163
kono
parents: 0
diff changeset
164 static void
kono
parents: 0
diff changeset
165 gomp_task_handle_depend (struct gomp_task *task, struct gomp_task *parent,
kono
parents: 0
diff changeset
166 void **depend)
kono
parents: 0
diff changeset
167 {
kono
parents: 0
diff changeset
168 size_t ndepend = (uintptr_t) depend[0];
kono
parents: 0
diff changeset
169 size_t nout = (uintptr_t) depend[1];
kono
parents: 0
diff changeset
170 size_t i;
kono
parents: 0
diff changeset
171 hash_entry_type ent;
kono
parents: 0
diff changeset
172
kono
parents: 0
diff changeset
173 task->depend_count = ndepend;
kono
parents: 0
diff changeset
174 task->num_dependees = 0;
kono
parents: 0
diff changeset
175 if (parent->depend_hash == NULL)
kono
parents: 0
diff changeset
176 parent->depend_hash = htab_create (2 * ndepend > 12 ? 2 * ndepend : 12);
kono
parents: 0
diff changeset
177 for (i = 0; i < ndepend; i++)
kono
parents: 0
diff changeset
178 {
kono
parents: 0
diff changeset
179 task->depend[i].addr = depend[2 + i];
kono
parents: 0
diff changeset
180 task->depend[i].next = NULL;
kono
parents: 0
diff changeset
181 task->depend[i].prev = NULL;
kono
parents: 0
diff changeset
182 task->depend[i].task = task;
kono
parents: 0
diff changeset
183 task->depend[i].is_in = i >= nout;
kono
parents: 0
diff changeset
184 task->depend[i].redundant = false;
kono
parents: 0
diff changeset
185 task->depend[i].redundant_out = false;
kono
parents: 0
diff changeset
186
kono
parents: 0
diff changeset
187 hash_entry_type *slot = htab_find_slot (&parent->depend_hash,
kono
parents: 0
diff changeset
188 &task->depend[i], INSERT);
kono
parents: 0
diff changeset
189 hash_entry_type out = NULL, last = NULL;
kono
parents: 0
diff changeset
190 if (*slot)
kono
parents: 0
diff changeset
191 {
kono
parents: 0
diff changeset
192 /* If multiple depends on the same task are the same, all but the
kono
parents: 0
diff changeset
193 first one are redundant. As inout/out come first, if any of them
kono
parents: 0
diff changeset
194 is inout/out, it will win, which is the right semantics. */
kono
parents: 0
diff changeset
195 if ((*slot)->task == task)
kono
parents: 0
diff changeset
196 {
kono
parents: 0
diff changeset
197 task->depend[i].redundant = true;
kono
parents: 0
diff changeset
198 continue;
kono
parents: 0
diff changeset
199 }
kono
parents: 0
diff changeset
200 for (ent = *slot; ent; ent = ent->next)
kono
parents: 0
diff changeset
201 {
kono
parents: 0
diff changeset
202 if (ent->redundant_out)
kono
parents: 0
diff changeset
203 break;
kono
parents: 0
diff changeset
204
kono
parents: 0
diff changeset
205 last = ent;
kono
parents: 0
diff changeset
206
kono
parents: 0
diff changeset
207 /* depend(in:...) doesn't depend on earlier depend(in:...). */
kono
parents: 0
diff changeset
208 if (i >= nout && ent->is_in)
kono
parents: 0
diff changeset
209 continue;
kono
parents: 0
diff changeset
210
kono
parents: 0
diff changeset
211 if (!ent->is_in)
kono
parents: 0
diff changeset
212 out = ent;
kono
parents: 0
diff changeset
213
kono
parents: 0
diff changeset
214 struct gomp_task *tsk = ent->task;
kono
parents: 0
diff changeset
215 if (tsk->dependers == NULL)
kono
parents: 0
diff changeset
216 {
kono
parents: 0
diff changeset
217 tsk->dependers
kono
parents: 0
diff changeset
218 = gomp_malloc (sizeof (struct gomp_dependers_vec)
kono
parents: 0
diff changeset
219 + 6 * sizeof (struct gomp_task *));
kono
parents: 0
diff changeset
220 tsk->dependers->n_elem = 1;
kono
parents: 0
diff changeset
221 tsk->dependers->allocated = 6;
kono
parents: 0
diff changeset
222 tsk->dependers->elem[0] = task;
kono
parents: 0
diff changeset
223 task->num_dependees++;
kono
parents: 0
diff changeset
224 continue;
kono
parents: 0
diff changeset
225 }
kono
parents: 0
diff changeset
226 /* We already have some other dependency on tsk from earlier
kono
parents: 0
diff changeset
227 depend clause. */
kono
parents: 0
diff changeset
228 else if (tsk->dependers->n_elem
kono
parents: 0
diff changeset
229 && (tsk->dependers->elem[tsk->dependers->n_elem - 1]
kono
parents: 0
diff changeset
230 == task))
kono
parents: 0
diff changeset
231 continue;
kono
parents: 0
diff changeset
232 else if (tsk->dependers->n_elem == tsk->dependers->allocated)
kono
parents: 0
diff changeset
233 {
kono
parents: 0
diff changeset
234 tsk->dependers->allocated
kono
parents: 0
diff changeset
235 = tsk->dependers->allocated * 2 + 2;
kono
parents: 0
diff changeset
236 tsk->dependers
kono
parents: 0
diff changeset
237 = gomp_realloc (tsk->dependers,
kono
parents: 0
diff changeset
238 sizeof (struct gomp_dependers_vec)
kono
parents: 0
diff changeset
239 + (tsk->dependers->allocated
kono
parents: 0
diff changeset
240 * sizeof (struct gomp_task *)));
kono
parents: 0
diff changeset
241 }
kono
parents: 0
diff changeset
242 tsk->dependers->elem[tsk->dependers->n_elem++] = task;
kono
parents: 0
diff changeset
243 task->num_dependees++;
kono
parents: 0
diff changeset
244 }
kono
parents: 0
diff changeset
245 task->depend[i].next = *slot;
kono
parents: 0
diff changeset
246 (*slot)->prev = &task->depend[i];
kono
parents: 0
diff changeset
247 }
kono
parents: 0
diff changeset
248 *slot = &task->depend[i];
kono
parents: 0
diff changeset
249
kono
parents: 0
diff changeset
250 /* There is no need to store more than one depend({,in}out:) task per
kono
parents: 0
diff changeset
251 address in the hash table chain for the purpose of creation of
kono
parents: 0
diff changeset
252 deferred tasks, because each out depends on all earlier outs, thus it
kono
parents: 0
diff changeset
253 is enough to record just the last depend({,in}out:). For depend(in:),
kono
parents: 0
diff changeset
254 we need to keep all of the previous ones not terminated yet, because
kono
parents: 0
diff changeset
255 a later depend({,in}out:) might need to depend on all of them. So, if
kono
parents: 0
diff changeset
256 the new task's clause is depend({,in}out:), we know there is at most
kono
parents: 0
diff changeset
257 one other depend({,in}out:) clause in the list (out). For
kono
parents: 0
diff changeset
258 non-deferred tasks we want to see all outs, so they are moved to the
kono
parents: 0
diff changeset
259 end of the chain, after first redundant_out entry all following
kono
parents: 0
diff changeset
260 entries should be redundant_out. */
kono
parents: 0
diff changeset
261 if (!task->depend[i].is_in && out)
kono
parents: 0
diff changeset
262 {
kono
parents: 0
diff changeset
263 if (out != last)
kono
parents: 0
diff changeset
264 {
kono
parents: 0
diff changeset
265 out->next->prev = out->prev;
kono
parents: 0
diff changeset
266 out->prev->next = out->next;
kono
parents: 0
diff changeset
267 out->next = last->next;
kono
parents: 0
diff changeset
268 out->prev = last;
kono
parents: 0
diff changeset
269 last->next = out;
kono
parents: 0
diff changeset
270 if (out->next)
kono
parents: 0
diff changeset
271 out->next->prev = out;
kono
parents: 0
diff changeset
272 }
kono
parents: 0
diff changeset
273 out->redundant_out = true;
kono
parents: 0
diff changeset
274 }
kono
parents: 0
diff changeset
275 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 /* Called when encountering an explicit task directive. If IF_CLAUSE is
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 false, then we must not delay in executing the task. If UNTIED is true,
111
kono
parents: 0
diff changeset
280 then the task may be executed by any member of the team.
kono
parents: 0
diff changeset
281
kono
parents: 0
diff changeset
282 DEPEND is an array containing:
kono
parents: 0
diff changeset
283 depend[0]: number of depend elements.
kono
parents: 0
diff changeset
284 depend[1]: number of depend elements of type "out".
kono
parents: 0
diff changeset
285 depend[2..N+1]: address of [1..N]th depend element. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 GOMP_task (void (*fn) (void *), void *data, void (*cpyfn) (void *, void *),
111
kono
parents: 0
diff changeset
289 long arg_size, long arg_align, bool if_clause, unsigned flags,
kono
parents: 0
diff changeset
290 void **depend, int priority)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 struct gomp_thread *thr = gomp_thread ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 struct gomp_team *team = thr->ts.team;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 /* If pthread_mutex_* is used for omp_*lock*, then each task must be
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 tied to one thread all the time. This means UNTIED tasks must be
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 tied and if CPYFN is non-NULL IF(0) must be forced, as CPYFN
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 might be running on different thread than FN. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 if (cpyfn)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 if_clause = false;
111
kono
parents: 0
diff changeset
302 flags &= ~GOMP_TASK_FLAG_UNTIED;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 #endif
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
304
111
kono
parents: 0
diff changeset
305 /* If parallel or taskgroup has been cancelled, don't start new tasks. */
kono
parents: 0
diff changeset
306 if (team
kono
parents: 0
diff changeset
307 && (gomp_team_barrier_cancelled (&team->barrier)
kono
parents: 0
diff changeset
308 || (thr->task->taskgroup && thr->task->taskgroup->cancelled)))
kono
parents: 0
diff changeset
309 return;
kono
parents: 0
diff changeset
310
kono
parents: 0
diff changeset
311 if ((flags & GOMP_TASK_FLAG_PRIORITY) == 0)
kono
parents: 0
diff changeset
312 priority = 0;
kono
parents: 0
diff changeset
313 else if (priority > gomp_max_task_priority_var)
kono
parents: 0
diff changeset
314 priority = gomp_max_task_priority_var;
kono
parents: 0
diff changeset
315
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 if (!if_clause || team == NULL
111
kono
parents: 0
diff changeset
317 || (thr->task && thr->task->final_task)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 || team->task_count > 64 * team->nthreads)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 struct gomp_task task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
321
111
kono
parents: 0
diff changeset
322 /* If there are depend clauses and earlier deferred sibling tasks
kono
parents: 0
diff changeset
323 with depend clauses, check if there isn't a dependency. If there
kono
parents: 0
diff changeset
324 is, we need to wait for them. There is no need to handle
kono
parents: 0
diff changeset
325 depend clauses for non-deferred tasks other than this, because
kono
parents: 0
diff changeset
326 the parent task is suspended until the child task finishes and thus
kono
parents: 0
diff changeset
327 it can't start further child tasks. */
kono
parents: 0
diff changeset
328 if ((flags & GOMP_TASK_FLAG_DEPEND)
kono
parents: 0
diff changeset
329 && thr->task && thr->task->depend_hash)
kono
parents: 0
diff changeset
330 gomp_task_maybe_wait_for_dependencies (depend);
kono
parents: 0
diff changeset
331
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 gomp_init_task (&task, thr->task, gomp_icv (false));
111
kono
parents: 0
diff changeset
333 task.kind = GOMP_TASK_UNDEFERRED;
kono
parents: 0
diff changeset
334 task.final_task = (thr->task && thr->task->final_task)
kono
parents: 0
diff changeset
335 || (flags & GOMP_TASK_FLAG_FINAL);
kono
parents: 0
diff changeset
336 task.priority = priority;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 if (thr->task)
111
kono
parents: 0
diff changeset
338 {
kono
parents: 0
diff changeset
339 task.in_tied_task = thr->task->in_tied_task;
kono
parents: 0
diff changeset
340 task.taskgroup = thr->task->taskgroup;
kono
parents: 0
diff changeset
341 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 thr->task = &task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 if (__builtin_expect (cpyfn != NULL, 0))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 char buf[arg_size + arg_align - 1];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 char *arg = (char *) (((uintptr_t) buf + arg_align - 1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 & ~(uintptr_t) (arg_align - 1));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 cpyfn (arg, data);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 fn (arg);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 fn (data);
111
kono
parents: 0
diff changeset
353 /* Access to "children" is normally done inside a task_lock
kono
parents: 0
diff changeset
354 mutex region, but the only way this particular task.children
kono
parents: 0
diff changeset
355 can be set is if this thread's task work function (fn)
kono
parents: 0
diff changeset
356 creates children. So since the setter is *this* thread, we
kono
parents: 0
diff changeset
357 need no barriers here when testing for non-NULL. We can have
kono
parents: 0
diff changeset
358 task.children set by the current thread then changed by a
kono
parents: 0
diff changeset
359 child thread, but seeing a stale non-NULL value is not a
kono
parents: 0
diff changeset
360 problem. Once past the task_lock acquisition, this thread
kono
parents: 0
diff changeset
361 will see the real value of task.children. */
kono
parents: 0
diff changeset
362 if (!priority_queue_empty_p (&task.children_queue, MEMMODEL_RELAXED))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 gomp_mutex_lock (&team->task_lock);
111
kono
parents: 0
diff changeset
365 gomp_clear_parent (&task.children_queue);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 gomp_mutex_unlock (&team->task_lock);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 gomp_end_task ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 struct gomp_task *task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 struct gomp_task *parent = thr->task;
111
kono
parents: 0
diff changeset
374 struct gomp_taskgroup *taskgroup = parent->taskgroup;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 char *arg;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 bool do_wake;
111
kono
parents: 0
diff changeset
377 size_t depend_size = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
378
111
kono
parents: 0
diff changeset
379 if (flags & GOMP_TASK_FLAG_DEPEND)
kono
parents: 0
diff changeset
380 depend_size = ((uintptr_t) depend[0]
kono
parents: 0
diff changeset
381 * sizeof (struct gomp_task_depend_entry));
kono
parents: 0
diff changeset
382 task = gomp_malloc (sizeof (*task) + depend_size
kono
parents: 0
diff changeset
383 + arg_size + arg_align - 1);
kono
parents: 0
diff changeset
384 arg = (char *) (((uintptr_t) (task + 1) + depend_size + arg_align - 1)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 & ~(uintptr_t) (arg_align - 1));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
386 gomp_init_task (task, parent, gomp_icv (false));
111
kono
parents: 0
diff changeset
387 task->priority = priority;
kono
parents: 0
diff changeset
388 task->kind = GOMP_TASK_UNDEFERRED;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 task->in_tied_task = parent->in_tied_task;
111
kono
parents: 0
diff changeset
390 task->taskgroup = taskgroup;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 thr->task = task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 if (cpyfn)
111
kono
parents: 0
diff changeset
393 {
kono
parents: 0
diff changeset
394 cpyfn (arg, data);
kono
parents: 0
diff changeset
395 task->copy_ctors_done = true;
kono
parents: 0
diff changeset
396 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 memcpy (arg, data, arg_size);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 thr->task = parent;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 task->kind = GOMP_TASK_WAITING;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 task->fn = fn;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 task->fn_data = arg;
111
kono
parents: 0
diff changeset
403 task->final_task = (flags & GOMP_TASK_FLAG_FINAL) >> 1;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
404 gomp_mutex_lock (&team->task_lock);
111
kono
parents: 0
diff changeset
405 /* If parallel or taskgroup has been cancelled, don't start new
kono
parents: 0
diff changeset
406 tasks. */
kono
parents: 0
diff changeset
407 if (__builtin_expect ((gomp_team_barrier_cancelled (&team->barrier)
kono
parents: 0
diff changeset
408 || (taskgroup && taskgroup->cancelled))
kono
parents: 0
diff changeset
409 && !task->copy_ctors_done, 0))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 {
111
kono
parents: 0
diff changeset
411 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
412 gomp_finish_task (task);
kono
parents: 0
diff changeset
413 free (task);
kono
parents: 0
diff changeset
414 return;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
415 }
111
kono
parents: 0
diff changeset
416 if (taskgroup)
kono
parents: 0
diff changeset
417 taskgroup->num_children++;
kono
parents: 0
diff changeset
418 if (depend_size)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 {
111
kono
parents: 0
diff changeset
420 gomp_task_handle_depend (task, parent, depend);
kono
parents: 0
diff changeset
421 if (task->num_dependees)
kono
parents: 0
diff changeset
422 {
kono
parents: 0
diff changeset
423 /* Tasks that depend on other tasks are not put into the
kono
parents: 0
diff changeset
424 various waiting queues, so we are done for now. Said
kono
parents: 0
diff changeset
425 tasks are instead put into the queues via
kono
parents: 0
diff changeset
426 gomp_task_run_post_handle_dependers() after their
kono
parents: 0
diff changeset
427 dependencies have been satisfied. After which, they
kono
parents: 0
diff changeset
428 can be picked up by the various scheduling
kono
parents: 0
diff changeset
429 points. */
kono
parents: 0
diff changeset
430 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
431 return;
kono
parents: 0
diff changeset
432 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 }
111
kono
parents: 0
diff changeset
434
kono
parents: 0
diff changeset
435 priority_queue_insert (PQ_CHILDREN, &parent->children_queue,
kono
parents: 0
diff changeset
436 task, priority,
kono
parents: 0
diff changeset
437 PRIORITY_INSERT_BEGIN,
kono
parents: 0
diff changeset
438 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
439 task->parent_depends_on);
kono
parents: 0
diff changeset
440 if (taskgroup)
kono
parents: 0
diff changeset
441 priority_queue_insert (PQ_TASKGROUP, &taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
442 task, priority,
kono
parents: 0
diff changeset
443 PRIORITY_INSERT_BEGIN,
kono
parents: 0
diff changeset
444 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
445 task->parent_depends_on);
kono
parents: 0
diff changeset
446
kono
parents: 0
diff changeset
447 priority_queue_insert (PQ_TEAM, &team->task_queue,
kono
parents: 0
diff changeset
448 task, priority,
kono
parents: 0
diff changeset
449 PRIORITY_INSERT_END,
kono
parents: 0
diff changeset
450 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
451 task->parent_depends_on);
kono
parents: 0
diff changeset
452
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 ++team->task_count;
111
kono
parents: 0
diff changeset
454 ++team->task_queued_count;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
455 gomp_team_barrier_set_task_pending (&team->barrier);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 do_wake = team->task_running_count + !parent->in_tied_task
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 < team->nthreads;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 gomp_mutex_unlock (&team->task_lock);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 if (do_wake)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 gomp_team_barrier_wake (&team->barrier, 1);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
463
111
kono
parents: 0
diff changeset
464 ialias (GOMP_taskgroup_start)
kono
parents: 0
diff changeset
465 ialias (GOMP_taskgroup_end)
kono
parents: 0
diff changeset
466
kono
parents: 0
diff changeset
467 #define TYPE long
kono
parents: 0
diff changeset
468 #define UTYPE unsigned long
kono
parents: 0
diff changeset
469 #define TYPE_is_long 1
kono
parents: 0
diff changeset
470 #include "taskloop.c"
kono
parents: 0
diff changeset
471 #undef TYPE
kono
parents: 0
diff changeset
472 #undef UTYPE
kono
parents: 0
diff changeset
473 #undef TYPE_is_long
kono
parents: 0
diff changeset
474
kono
parents: 0
diff changeset
475 #define TYPE unsigned long long
kono
parents: 0
diff changeset
476 #define UTYPE TYPE
kono
parents: 0
diff changeset
477 #define GOMP_taskloop GOMP_taskloop_ull
kono
parents: 0
diff changeset
478 #include "taskloop.c"
kono
parents: 0
diff changeset
479 #undef TYPE
kono
parents: 0
diff changeset
480 #undef UTYPE
kono
parents: 0
diff changeset
481 #undef GOMP_taskloop
kono
parents: 0
diff changeset
482
kono
parents: 0
diff changeset
483 static void inline
kono
parents: 0
diff changeset
484 priority_queue_move_task_first (enum priority_queue_type type,
kono
parents: 0
diff changeset
485 struct priority_queue *head,
kono
parents: 0
diff changeset
486 struct gomp_task *task)
kono
parents: 0
diff changeset
487 {
kono
parents: 0
diff changeset
488 #if _LIBGOMP_CHECKING_
kono
parents: 0
diff changeset
489 if (!priority_queue_task_in_queue_p (type, head, task))
kono
parents: 0
diff changeset
490 gomp_fatal ("Attempt to move first missing task %p", task);
kono
parents: 0
diff changeset
491 #endif
kono
parents: 0
diff changeset
492 struct priority_list *list;
kono
parents: 0
diff changeset
493 if (priority_queue_multi_p (head))
kono
parents: 0
diff changeset
494 {
kono
parents: 0
diff changeset
495 list = priority_queue_lookup_priority (head, task->priority);
kono
parents: 0
diff changeset
496 #if _LIBGOMP_CHECKING_
kono
parents: 0
diff changeset
497 if (!list)
kono
parents: 0
diff changeset
498 gomp_fatal ("Unable to find priority %d", task->priority);
kono
parents: 0
diff changeset
499 #endif
kono
parents: 0
diff changeset
500 }
kono
parents: 0
diff changeset
501 else
kono
parents: 0
diff changeset
502 list = &head->l;
kono
parents: 0
diff changeset
503 priority_list_remove (list, task_to_priority_node (type, task), 0);
kono
parents: 0
diff changeset
504 priority_list_insert (type, list, task, task->priority,
kono
parents: 0
diff changeset
505 PRIORITY_INSERT_BEGIN, type == PQ_CHILDREN,
kono
parents: 0
diff changeset
506 task->parent_depends_on);
kono
parents: 0
diff changeset
507 }
kono
parents: 0
diff changeset
508
kono
parents: 0
diff changeset
509 /* Actual body of GOMP_PLUGIN_target_task_completion that is executed
kono
parents: 0
diff changeset
510 with team->task_lock held, or is executed in the thread that called
kono
parents: 0
diff changeset
511 gomp_target_task_fn if GOMP_PLUGIN_target_task_completion has been
kono
parents: 0
diff changeset
512 run before it acquires team->task_lock. */
kono
parents: 0
diff changeset
513
kono
parents: 0
diff changeset
514 static void
kono
parents: 0
diff changeset
515 gomp_target_task_completion (struct gomp_team *team, struct gomp_task *task)
kono
parents: 0
diff changeset
516 {
kono
parents: 0
diff changeset
517 struct gomp_task *parent = task->parent;
kono
parents: 0
diff changeset
518 if (parent)
kono
parents: 0
diff changeset
519 priority_queue_move_task_first (PQ_CHILDREN, &parent->children_queue,
kono
parents: 0
diff changeset
520 task);
kono
parents: 0
diff changeset
521
kono
parents: 0
diff changeset
522 struct gomp_taskgroup *taskgroup = task->taskgroup;
kono
parents: 0
diff changeset
523 if (taskgroup)
kono
parents: 0
diff changeset
524 priority_queue_move_task_first (PQ_TASKGROUP, &taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
525 task);
kono
parents: 0
diff changeset
526
kono
parents: 0
diff changeset
527 priority_queue_insert (PQ_TEAM, &team->task_queue, task, task->priority,
kono
parents: 0
diff changeset
528 PRIORITY_INSERT_BEGIN, false,
kono
parents: 0
diff changeset
529 task->parent_depends_on);
kono
parents: 0
diff changeset
530 task->kind = GOMP_TASK_WAITING;
kono
parents: 0
diff changeset
531 if (parent && parent->taskwait)
kono
parents: 0
diff changeset
532 {
kono
parents: 0
diff changeset
533 if (parent->taskwait->in_taskwait)
kono
parents: 0
diff changeset
534 {
kono
parents: 0
diff changeset
535 /* One more task has had its dependencies met.
kono
parents: 0
diff changeset
536 Inform any waiters. */
kono
parents: 0
diff changeset
537 parent->taskwait->in_taskwait = false;
kono
parents: 0
diff changeset
538 gomp_sem_post (&parent->taskwait->taskwait_sem);
kono
parents: 0
diff changeset
539 }
kono
parents: 0
diff changeset
540 else if (parent->taskwait->in_depend_wait)
kono
parents: 0
diff changeset
541 {
kono
parents: 0
diff changeset
542 /* One more task has had its dependencies met.
kono
parents: 0
diff changeset
543 Inform any waiters. */
kono
parents: 0
diff changeset
544 parent->taskwait->in_depend_wait = false;
kono
parents: 0
diff changeset
545 gomp_sem_post (&parent->taskwait->taskwait_sem);
kono
parents: 0
diff changeset
546 }
kono
parents: 0
diff changeset
547 }
kono
parents: 0
diff changeset
548 if (taskgroup && taskgroup->in_taskgroup_wait)
kono
parents: 0
diff changeset
549 {
kono
parents: 0
diff changeset
550 /* One more task has had its dependencies met.
kono
parents: 0
diff changeset
551 Inform any waiters. */
kono
parents: 0
diff changeset
552 taskgroup->in_taskgroup_wait = false;
kono
parents: 0
diff changeset
553 gomp_sem_post (&taskgroup->taskgroup_sem);
kono
parents: 0
diff changeset
554 }
kono
parents: 0
diff changeset
555
kono
parents: 0
diff changeset
556 ++team->task_queued_count;
kono
parents: 0
diff changeset
557 gomp_team_barrier_set_task_pending (&team->barrier);
kono
parents: 0
diff changeset
558 /* I'm afraid this can't be done after releasing team->task_lock,
kono
parents: 0
diff changeset
559 as gomp_target_task_completion is run from unrelated thread and
kono
parents: 0
diff changeset
560 therefore in between gomp_mutex_unlock and gomp_team_barrier_wake
kono
parents: 0
diff changeset
561 the team could be gone already. */
kono
parents: 0
diff changeset
562 if (team->nthreads > team->task_running_count)
kono
parents: 0
diff changeset
563 gomp_team_barrier_wake (&team->barrier, 1);
kono
parents: 0
diff changeset
564 }
kono
parents: 0
diff changeset
565
kono
parents: 0
diff changeset
566 /* Signal that a target task TTASK has completed the asynchronously
kono
parents: 0
diff changeset
567 running phase and should be requeued as a task to handle the
kono
parents: 0
diff changeset
568 variable unmapping. */
kono
parents: 0
diff changeset
569
kono
parents: 0
diff changeset
570 void
kono
parents: 0
diff changeset
571 GOMP_PLUGIN_target_task_completion (void *data)
kono
parents: 0
diff changeset
572 {
kono
parents: 0
diff changeset
573 struct gomp_target_task *ttask = (struct gomp_target_task *) data;
kono
parents: 0
diff changeset
574 struct gomp_task *task = ttask->task;
kono
parents: 0
diff changeset
575 struct gomp_team *team = ttask->team;
kono
parents: 0
diff changeset
576
kono
parents: 0
diff changeset
577 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
578 if (ttask->state == GOMP_TARGET_TASK_READY_TO_RUN)
kono
parents: 0
diff changeset
579 {
kono
parents: 0
diff changeset
580 ttask->state = GOMP_TARGET_TASK_FINISHED;
kono
parents: 0
diff changeset
581 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
582 return;
kono
parents: 0
diff changeset
583 }
kono
parents: 0
diff changeset
584 ttask->state = GOMP_TARGET_TASK_FINISHED;
kono
parents: 0
diff changeset
585 gomp_target_task_completion (team, task);
kono
parents: 0
diff changeset
586 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
587 }
kono
parents: 0
diff changeset
588
kono
parents: 0
diff changeset
589 static void gomp_task_run_post_handle_depend_hash (struct gomp_task *);
kono
parents: 0
diff changeset
590
kono
parents: 0
diff changeset
591 /* Called for nowait target tasks. */
kono
parents: 0
diff changeset
592
kono
parents: 0
diff changeset
593 bool
kono
parents: 0
diff changeset
594 gomp_create_target_task (struct gomp_device_descr *devicep,
kono
parents: 0
diff changeset
595 void (*fn) (void *), size_t mapnum, void **hostaddrs,
kono
parents: 0
diff changeset
596 size_t *sizes, unsigned short *kinds,
kono
parents: 0
diff changeset
597 unsigned int flags, void **depend, void **args,
kono
parents: 0
diff changeset
598 enum gomp_target_task_state state)
kono
parents: 0
diff changeset
599 {
kono
parents: 0
diff changeset
600 struct gomp_thread *thr = gomp_thread ();
kono
parents: 0
diff changeset
601 struct gomp_team *team = thr->ts.team;
kono
parents: 0
diff changeset
602
kono
parents: 0
diff changeset
603 /* If parallel or taskgroup has been cancelled, don't start new tasks. */
kono
parents: 0
diff changeset
604 if (team
kono
parents: 0
diff changeset
605 && (gomp_team_barrier_cancelled (&team->barrier)
kono
parents: 0
diff changeset
606 || (thr->task->taskgroup && thr->task->taskgroup->cancelled)))
kono
parents: 0
diff changeset
607 return true;
kono
parents: 0
diff changeset
608
kono
parents: 0
diff changeset
609 struct gomp_target_task *ttask;
kono
parents: 0
diff changeset
610 struct gomp_task *task;
kono
parents: 0
diff changeset
611 struct gomp_task *parent = thr->task;
kono
parents: 0
diff changeset
612 struct gomp_taskgroup *taskgroup = parent->taskgroup;
kono
parents: 0
diff changeset
613 bool do_wake;
kono
parents: 0
diff changeset
614 size_t depend_size = 0;
kono
parents: 0
diff changeset
615 uintptr_t depend_cnt = 0;
kono
parents: 0
diff changeset
616 size_t tgt_align = 0, tgt_size = 0;
kono
parents: 0
diff changeset
617
kono
parents: 0
diff changeset
618 if (depend != NULL)
kono
parents: 0
diff changeset
619 {
kono
parents: 0
diff changeset
620 depend_cnt = (uintptr_t) depend[0];
kono
parents: 0
diff changeset
621 depend_size = depend_cnt * sizeof (struct gomp_task_depend_entry);
kono
parents: 0
diff changeset
622 }
kono
parents: 0
diff changeset
623 if (fn)
kono
parents: 0
diff changeset
624 {
kono
parents: 0
diff changeset
625 /* GOMP_MAP_FIRSTPRIVATE need to be copied first, as they are
kono
parents: 0
diff changeset
626 firstprivate on the target task. */
kono
parents: 0
diff changeset
627 size_t i;
kono
parents: 0
diff changeset
628 for (i = 0; i < mapnum; i++)
kono
parents: 0
diff changeset
629 if ((kinds[i] & 0xff) == GOMP_MAP_FIRSTPRIVATE)
kono
parents: 0
diff changeset
630 {
kono
parents: 0
diff changeset
631 size_t align = (size_t) 1 << (kinds[i] >> 8);
kono
parents: 0
diff changeset
632 if (tgt_align < align)
kono
parents: 0
diff changeset
633 tgt_align = align;
kono
parents: 0
diff changeset
634 tgt_size = (tgt_size + align - 1) & ~(align - 1);
kono
parents: 0
diff changeset
635 tgt_size += sizes[i];
kono
parents: 0
diff changeset
636 }
kono
parents: 0
diff changeset
637 if (tgt_align)
kono
parents: 0
diff changeset
638 tgt_size += tgt_align - 1;
kono
parents: 0
diff changeset
639 else
kono
parents: 0
diff changeset
640 tgt_size = 0;
kono
parents: 0
diff changeset
641 }
kono
parents: 0
diff changeset
642
kono
parents: 0
diff changeset
643 task = gomp_malloc (sizeof (*task) + depend_size
kono
parents: 0
diff changeset
644 + sizeof (*ttask)
kono
parents: 0
diff changeset
645 + mapnum * (sizeof (void *) + sizeof (size_t)
kono
parents: 0
diff changeset
646 + sizeof (unsigned short))
kono
parents: 0
diff changeset
647 + tgt_size);
kono
parents: 0
diff changeset
648 gomp_init_task (task, parent, gomp_icv (false));
kono
parents: 0
diff changeset
649 task->priority = 0;
kono
parents: 0
diff changeset
650 task->kind = GOMP_TASK_WAITING;
kono
parents: 0
diff changeset
651 task->in_tied_task = parent->in_tied_task;
kono
parents: 0
diff changeset
652 task->taskgroup = taskgroup;
kono
parents: 0
diff changeset
653 ttask = (struct gomp_target_task *) &task->depend[depend_cnt];
kono
parents: 0
diff changeset
654 ttask->devicep = devicep;
kono
parents: 0
diff changeset
655 ttask->fn = fn;
kono
parents: 0
diff changeset
656 ttask->mapnum = mapnum;
kono
parents: 0
diff changeset
657 ttask->args = args;
kono
parents: 0
diff changeset
658 memcpy (ttask->hostaddrs, hostaddrs, mapnum * sizeof (void *));
kono
parents: 0
diff changeset
659 ttask->sizes = (size_t *) &ttask->hostaddrs[mapnum];
kono
parents: 0
diff changeset
660 memcpy (ttask->sizes, sizes, mapnum * sizeof (size_t));
kono
parents: 0
diff changeset
661 ttask->kinds = (unsigned short *) &ttask->sizes[mapnum];
kono
parents: 0
diff changeset
662 memcpy (ttask->kinds, kinds, mapnum * sizeof (unsigned short));
kono
parents: 0
diff changeset
663 if (tgt_align)
kono
parents: 0
diff changeset
664 {
kono
parents: 0
diff changeset
665 char *tgt = (char *) &ttask->kinds[mapnum];
kono
parents: 0
diff changeset
666 size_t i;
kono
parents: 0
diff changeset
667 uintptr_t al = (uintptr_t) tgt & (tgt_align - 1);
kono
parents: 0
diff changeset
668 if (al)
kono
parents: 0
diff changeset
669 tgt += tgt_align - al;
kono
parents: 0
diff changeset
670 tgt_size = 0;
kono
parents: 0
diff changeset
671 for (i = 0; i < mapnum; i++)
kono
parents: 0
diff changeset
672 if ((kinds[i] & 0xff) == GOMP_MAP_FIRSTPRIVATE)
kono
parents: 0
diff changeset
673 {
kono
parents: 0
diff changeset
674 size_t align = (size_t) 1 << (kinds[i] >> 8);
kono
parents: 0
diff changeset
675 tgt_size = (tgt_size + align - 1) & ~(align - 1);
kono
parents: 0
diff changeset
676 memcpy (tgt + tgt_size, hostaddrs[i], sizes[i]);
kono
parents: 0
diff changeset
677 ttask->hostaddrs[i] = tgt + tgt_size;
kono
parents: 0
diff changeset
678 tgt_size = tgt_size + sizes[i];
kono
parents: 0
diff changeset
679 }
kono
parents: 0
diff changeset
680 }
kono
parents: 0
diff changeset
681 ttask->flags = flags;
kono
parents: 0
diff changeset
682 ttask->state = state;
kono
parents: 0
diff changeset
683 ttask->task = task;
kono
parents: 0
diff changeset
684 ttask->team = team;
kono
parents: 0
diff changeset
685 task->fn = NULL;
kono
parents: 0
diff changeset
686 task->fn_data = ttask;
kono
parents: 0
diff changeset
687 task->final_task = 0;
kono
parents: 0
diff changeset
688 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
689 /* If parallel or taskgroup has been cancelled, don't start new tasks. */
kono
parents: 0
diff changeset
690 if (__builtin_expect (gomp_team_barrier_cancelled (&team->barrier)
kono
parents: 0
diff changeset
691 || (taskgroup && taskgroup->cancelled), 0))
kono
parents: 0
diff changeset
692 {
kono
parents: 0
diff changeset
693 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
694 gomp_finish_task (task);
kono
parents: 0
diff changeset
695 free (task);
kono
parents: 0
diff changeset
696 return true;
kono
parents: 0
diff changeset
697 }
kono
parents: 0
diff changeset
698 if (depend_size)
kono
parents: 0
diff changeset
699 {
kono
parents: 0
diff changeset
700 gomp_task_handle_depend (task, parent, depend);
kono
parents: 0
diff changeset
701 if (task->num_dependees)
kono
parents: 0
diff changeset
702 {
kono
parents: 0
diff changeset
703 if (taskgroup)
kono
parents: 0
diff changeset
704 taskgroup->num_children++;
kono
parents: 0
diff changeset
705 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
706 return true;
kono
parents: 0
diff changeset
707 }
kono
parents: 0
diff changeset
708 }
kono
parents: 0
diff changeset
709 if (state == GOMP_TARGET_TASK_DATA)
kono
parents: 0
diff changeset
710 {
kono
parents: 0
diff changeset
711 gomp_task_run_post_handle_depend_hash (task);
kono
parents: 0
diff changeset
712 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
713 gomp_finish_task (task);
kono
parents: 0
diff changeset
714 free (task);
kono
parents: 0
diff changeset
715 return false;
kono
parents: 0
diff changeset
716 }
kono
parents: 0
diff changeset
717 if (taskgroup)
kono
parents: 0
diff changeset
718 taskgroup->num_children++;
kono
parents: 0
diff changeset
719 /* For async offloading, if we don't need to wait for dependencies,
kono
parents: 0
diff changeset
720 run the gomp_target_task_fn right away, essentially schedule the
kono
parents: 0
diff changeset
721 mapping part of the task in the current thread. */
kono
parents: 0
diff changeset
722 if (devicep != NULL
kono
parents: 0
diff changeset
723 && (devicep->capabilities & GOMP_OFFLOAD_CAP_OPENMP_400))
kono
parents: 0
diff changeset
724 {
kono
parents: 0
diff changeset
725 priority_queue_insert (PQ_CHILDREN, &parent->children_queue, task, 0,
kono
parents: 0
diff changeset
726 PRIORITY_INSERT_END,
kono
parents: 0
diff changeset
727 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
728 task->parent_depends_on);
kono
parents: 0
diff changeset
729 if (taskgroup)
kono
parents: 0
diff changeset
730 priority_queue_insert (PQ_TASKGROUP, &taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
731 task, 0, PRIORITY_INSERT_END,
kono
parents: 0
diff changeset
732 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
733 task->parent_depends_on);
kono
parents: 0
diff changeset
734 task->pnode[PQ_TEAM].next = NULL;
kono
parents: 0
diff changeset
735 task->pnode[PQ_TEAM].prev = NULL;
kono
parents: 0
diff changeset
736 task->kind = GOMP_TASK_TIED;
kono
parents: 0
diff changeset
737 ++team->task_count;
kono
parents: 0
diff changeset
738 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
739
kono
parents: 0
diff changeset
740 thr->task = task;
kono
parents: 0
diff changeset
741 gomp_target_task_fn (task->fn_data);
kono
parents: 0
diff changeset
742 thr->task = parent;
kono
parents: 0
diff changeset
743
kono
parents: 0
diff changeset
744 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
745 task->kind = GOMP_TASK_ASYNC_RUNNING;
kono
parents: 0
diff changeset
746 /* If GOMP_PLUGIN_target_task_completion has run already
kono
parents: 0
diff changeset
747 in between gomp_target_task_fn and the mutex lock,
kono
parents: 0
diff changeset
748 perform the requeuing here. */
kono
parents: 0
diff changeset
749 if (ttask->state == GOMP_TARGET_TASK_FINISHED)
kono
parents: 0
diff changeset
750 gomp_target_task_completion (team, task);
kono
parents: 0
diff changeset
751 else
kono
parents: 0
diff changeset
752 ttask->state = GOMP_TARGET_TASK_RUNNING;
kono
parents: 0
diff changeset
753 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
754 return true;
kono
parents: 0
diff changeset
755 }
kono
parents: 0
diff changeset
756 priority_queue_insert (PQ_CHILDREN, &parent->children_queue, task, 0,
kono
parents: 0
diff changeset
757 PRIORITY_INSERT_BEGIN,
kono
parents: 0
diff changeset
758 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
759 task->parent_depends_on);
kono
parents: 0
diff changeset
760 if (taskgroup)
kono
parents: 0
diff changeset
761 priority_queue_insert (PQ_TASKGROUP, &taskgroup->taskgroup_queue, task, 0,
kono
parents: 0
diff changeset
762 PRIORITY_INSERT_BEGIN,
kono
parents: 0
diff changeset
763 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
764 task->parent_depends_on);
kono
parents: 0
diff changeset
765 priority_queue_insert (PQ_TEAM, &team->task_queue, task, 0,
kono
parents: 0
diff changeset
766 PRIORITY_INSERT_END,
kono
parents: 0
diff changeset
767 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
768 task->parent_depends_on);
kono
parents: 0
diff changeset
769 ++team->task_count;
kono
parents: 0
diff changeset
770 ++team->task_queued_count;
kono
parents: 0
diff changeset
771 gomp_team_barrier_set_task_pending (&team->barrier);
kono
parents: 0
diff changeset
772 do_wake = team->task_running_count + !parent->in_tied_task
kono
parents: 0
diff changeset
773 < team->nthreads;
kono
parents: 0
diff changeset
774 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
775 if (do_wake)
kono
parents: 0
diff changeset
776 gomp_team_barrier_wake (&team->barrier, 1);
kono
parents: 0
diff changeset
777 return true;
kono
parents: 0
diff changeset
778 }
kono
parents: 0
diff changeset
779
kono
parents: 0
diff changeset
780 /* Given a parent_depends_on task in LIST, move it to the front of its
kono
parents: 0
diff changeset
781 priority so it is run as soon as possible.
kono
parents: 0
diff changeset
782
kono
parents: 0
diff changeset
783 Care is taken to update the list's LAST_PARENT_DEPENDS_ON field.
kono
parents: 0
diff changeset
784
kono
parents: 0
diff changeset
785 We rearrange the queue such that all parent_depends_on tasks are
kono
parents: 0
diff changeset
786 first, and last_parent_depends_on points to the last such task we
kono
parents: 0
diff changeset
787 rearranged. For example, given the following tasks in a queue
kono
parents: 0
diff changeset
788 where PD[123] are the parent_depends_on tasks:
kono
parents: 0
diff changeset
789
kono
parents: 0
diff changeset
790 task->children
kono
parents: 0
diff changeset
791 |
kono
parents: 0
diff changeset
792 V
kono
parents: 0
diff changeset
793 C1 -> C2 -> C3 -> PD1 -> PD2 -> PD3 -> C4
kono
parents: 0
diff changeset
794
kono
parents: 0
diff changeset
795 We rearrange such that:
kono
parents: 0
diff changeset
796
kono
parents: 0
diff changeset
797 task->children
kono
parents: 0
diff changeset
798 | +--- last_parent_depends_on
kono
parents: 0
diff changeset
799 | |
kono
parents: 0
diff changeset
800 V V
kono
parents: 0
diff changeset
801 PD1 -> PD2 -> PD3 -> C1 -> C2 -> C3 -> C4. */
kono
parents: 0
diff changeset
802
kono
parents: 0
diff changeset
803 static void inline
kono
parents: 0
diff changeset
804 priority_list_upgrade_task (struct priority_list *list,
kono
parents: 0
diff changeset
805 struct priority_node *node)
kono
parents: 0
diff changeset
806 {
kono
parents: 0
diff changeset
807 struct priority_node *last_parent_depends_on
kono
parents: 0
diff changeset
808 = list->last_parent_depends_on;
kono
parents: 0
diff changeset
809 if (last_parent_depends_on)
kono
parents: 0
diff changeset
810 {
kono
parents: 0
diff changeset
811 node->prev->next = node->next;
kono
parents: 0
diff changeset
812 node->next->prev = node->prev;
kono
parents: 0
diff changeset
813 node->prev = last_parent_depends_on;
kono
parents: 0
diff changeset
814 node->next = last_parent_depends_on->next;
kono
parents: 0
diff changeset
815 node->prev->next = node;
kono
parents: 0
diff changeset
816 node->next->prev = node;
kono
parents: 0
diff changeset
817 }
kono
parents: 0
diff changeset
818 else if (node != list->tasks)
kono
parents: 0
diff changeset
819 {
kono
parents: 0
diff changeset
820 node->prev->next = node->next;
kono
parents: 0
diff changeset
821 node->next->prev = node->prev;
kono
parents: 0
diff changeset
822 node->prev = list->tasks->prev;
kono
parents: 0
diff changeset
823 node->next = list->tasks;
kono
parents: 0
diff changeset
824 list->tasks = node;
kono
parents: 0
diff changeset
825 node->prev->next = node;
kono
parents: 0
diff changeset
826 node->next->prev = node;
kono
parents: 0
diff changeset
827 }
kono
parents: 0
diff changeset
828 list->last_parent_depends_on = node;
kono
parents: 0
diff changeset
829 }
kono
parents: 0
diff changeset
830
kono
parents: 0
diff changeset
831 /* Given a parent_depends_on TASK in its parent's children_queue, move
kono
parents: 0
diff changeset
832 it to the front of its priority so it is run as soon as possible.
kono
parents: 0
diff changeset
833
kono
parents: 0
diff changeset
834 PARENT is passed as an optimization.
kono
parents: 0
diff changeset
835
kono
parents: 0
diff changeset
836 (This function could be defined in priority_queue.c, but we want it
kono
parents: 0
diff changeset
837 inlined, and putting it in priority_queue.h is not an option, given
kono
parents: 0
diff changeset
838 that gomp_task has not been properly defined at that point). */
kono
parents: 0
diff changeset
839
kono
parents: 0
diff changeset
840 static void inline
kono
parents: 0
diff changeset
841 priority_queue_upgrade_task (struct gomp_task *task,
kono
parents: 0
diff changeset
842 struct gomp_task *parent)
kono
parents: 0
diff changeset
843 {
kono
parents: 0
diff changeset
844 struct priority_queue *head = &parent->children_queue;
kono
parents: 0
diff changeset
845 struct priority_node *node = &task->pnode[PQ_CHILDREN];
kono
parents: 0
diff changeset
846 #if _LIBGOMP_CHECKING_
kono
parents: 0
diff changeset
847 if (!task->parent_depends_on)
kono
parents: 0
diff changeset
848 gomp_fatal ("priority_queue_upgrade_task: task must be a "
kono
parents: 0
diff changeset
849 "parent_depends_on task");
kono
parents: 0
diff changeset
850 if (!priority_queue_task_in_queue_p (PQ_CHILDREN, head, task))
kono
parents: 0
diff changeset
851 gomp_fatal ("priority_queue_upgrade_task: cannot find task=%p", task);
kono
parents: 0
diff changeset
852 #endif
kono
parents: 0
diff changeset
853 if (priority_queue_multi_p (head))
kono
parents: 0
diff changeset
854 {
kono
parents: 0
diff changeset
855 struct priority_list *list
kono
parents: 0
diff changeset
856 = priority_queue_lookup_priority (head, task->priority);
kono
parents: 0
diff changeset
857 priority_list_upgrade_task (list, node);
kono
parents: 0
diff changeset
858 }
kono
parents: 0
diff changeset
859 else
kono
parents: 0
diff changeset
860 priority_list_upgrade_task (&head->l, node);
kono
parents: 0
diff changeset
861 }
kono
parents: 0
diff changeset
862
kono
parents: 0
diff changeset
863 /* Given a CHILD_TASK in LIST that is about to be executed, move it out of
kono
parents: 0
diff changeset
864 the way in LIST so that other tasks can be considered for
kono
parents: 0
diff changeset
865 execution. LIST contains tasks of type TYPE.
kono
parents: 0
diff changeset
866
kono
parents: 0
diff changeset
867 Care is taken to update the queue's LAST_PARENT_DEPENDS_ON field
kono
parents: 0
diff changeset
868 if applicable. */
kono
parents: 0
diff changeset
869
kono
parents: 0
diff changeset
870 static void inline
kono
parents: 0
diff changeset
871 priority_list_downgrade_task (enum priority_queue_type type,
kono
parents: 0
diff changeset
872 struct priority_list *list,
kono
parents: 0
diff changeset
873 struct gomp_task *child_task)
kono
parents: 0
diff changeset
874 {
kono
parents: 0
diff changeset
875 struct priority_node *node = task_to_priority_node (type, child_task);
kono
parents: 0
diff changeset
876 if (list->tasks == node)
kono
parents: 0
diff changeset
877 list->tasks = node->next;
kono
parents: 0
diff changeset
878 else if (node->next != list->tasks)
kono
parents: 0
diff changeset
879 {
kono
parents: 0
diff changeset
880 /* The task in NODE is about to become TIED and TIED tasks
kono
parents: 0
diff changeset
881 cannot come before WAITING tasks. If we're about to
kono
parents: 0
diff changeset
882 leave the queue in such an indeterminate state, rewire
kono
parents: 0
diff changeset
883 things appropriately. However, a TIED task at the end is
kono
parents: 0
diff changeset
884 perfectly fine. */
kono
parents: 0
diff changeset
885 struct gomp_task *next_task = priority_node_to_task (type, node->next);
kono
parents: 0
diff changeset
886 if (next_task->kind == GOMP_TASK_WAITING)
kono
parents: 0
diff changeset
887 {
kono
parents: 0
diff changeset
888 /* Remove from list. */
kono
parents: 0
diff changeset
889 node->prev->next = node->next;
kono
parents: 0
diff changeset
890 node->next->prev = node->prev;
kono
parents: 0
diff changeset
891 /* Rewire at the end. */
kono
parents: 0
diff changeset
892 node->next = list->tasks;
kono
parents: 0
diff changeset
893 node->prev = list->tasks->prev;
kono
parents: 0
diff changeset
894 list->tasks->prev->next = node;
kono
parents: 0
diff changeset
895 list->tasks->prev = node;
kono
parents: 0
diff changeset
896 }
kono
parents: 0
diff changeset
897 }
kono
parents: 0
diff changeset
898
kono
parents: 0
diff changeset
899 /* If the current task is the last_parent_depends_on for its
kono
parents: 0
diff changeset
900 priority, adjust last_parent_depends_on appropriately. */
kono
parents: 0
diff changeset
901 if (__builtin_expect (child_task->parent_depends_on, 0)
kono
parents: 0
diff changeset
902 && list->last_parent_depends_on == node)
kono
parents: 0
diff changeset
903 {
kono
parents: 0
diff changeset
904 struct gomp_task *prev_child = priority_node_to_task (type, node->prev);
kono
parents: 0
diff changeset
905 if (node->prev != node
kono
parents: 0
diff changeset
906 && prev_child->kind == GOMP_TASK_WAITING
kono
parents: 0
diff changeset
907 && prev_child->parent_depends_on)
kono
parents: 0
diff changeset
908 list->last_parent_depends_on = node->prev;
kono
parents: 0
diff changeset
909 else
kono
parents: 0
diff changeset
910 {
kono
parents: 0
diff changeset
911 /* There are no more parent_depends_on entries waiting
kono
parents: 0
diff changeset
912 to run, clear the list. */
kono
parents: 0
diff changeset
913 list->last_parent_depends_on = NULL;
kono
parents: 0
diff changeset
914 }
kono
parents: 0
diff changeset
915 }
kono
parents: 0
diff changeset
916 }
kono
parents: 0
diff changeset
917
kono
parents: 0
diff changeset
918 /* Given a TASK in HEAD that is about to be executed, move it out of
kono
parents: 0
diff changeset
919 the way so that other tasks can be considered for execution. HEAD
kono
parents: 0
diff changeset
920 contains tasks of type TYPE.
kono
parents: 0
diff changeset
921
kono
parents: 0
diff changeset
922 Care is taken to update the queue's LAST_PARENT_DEPENDS_ON field
kono
parents: 0
diff changeset
923 if applicable.
kono
parents: 0
diff changeset
924
kono
parents: 0
diff changeset
925 (This function could be defined in priority_queue.c, but we want it
kono
parents: 0
diff changeset
926 inlined, and putting it in priority_queue.h is not an option, given
kono
parents: 0
diff changeset
927 that gomp_task has not been properly defined at that point). */
kono
parents: 0
diff changeset
928
kono
parents: 0
diff changeset
929 static void inline
kono
parents: 0
diff changeset
930 priority_queue_downgrade_task (enum priority_queue_type type,
kono
parents: 0
diff changeset
931 struct priority_queue *head,
kono
parents: 0
diff changeset
932 struct gomp_task *task)
kono
parents: 0
diff changeset
933 {
kono
parents: 0
diff changeset
934 #if _LIBGOMP_CHECKING_
kono
parents: 0
diff changeset
935 if (!priority_queue_task_in_queue_p (type, head, task))
kono
parents: 0
diff changeset
936 gomp_fatal ("Attempt to downgrade missing task %p", task);
kono
parents: 0
diff changeset
937 #endif
kono
parents: 0
diff changeset
938 if (priority_queue_multi_p (head))
kono
parents: 0
diff changeset
939 {
kono
parents: 0
diff changeset
940 struct priority_list *list
kono
parents: 0
diff changeset
941 = priority_queue_lookup_priority (head, task->priority);
kono
parents: 0
diff changeset
942 priority_list_downgrade_task (type, list, task);
kono
parents: 0
diff changeset
943 }
kono
parents: 0
diff changeset
944 else
kono
parents: 0
diff changeset
945 priority_list_downgrade_task (type, &head->l, task);
kono
parents: 0
diff changeset
946 }
kono
parents: 0
diff changeset
947
kono
parents: 0
diff changeset
948 /* Setup CHILD_TASK to execute. This is done by setting the task to
kono
parents: 0
diff changeset
949 TIED, and updating all relevant queues so that CHILD_TASK is no
kono
parents: 0
diff changeset
950 longer chosen for scheduling. Also, remove CHILD_TASK from the
kono
parents: 0
diff changeset
951 overall team task queue entirely.
kono
parents: 0
diff changeset
952
kono
parents: 0
diff changeset
953 Return TRUE if task or its containing taskgroup has been
kono
parents: 0
diff changeset
954 cancelled. */
kono
parents: 0
diff changeset
955
kono
parents: 0
diff changeset
956 static inline bool
kono
parents: 0
diff changeset
957 gomp_task_run_pre (struct gomp_task *child_task, struct gomp_task *parent,
kono
parents: 0
diff changeset
958 struct gomp_team *team)
kono
parents: 0
diff changeset
959 {
kono
parents: 0
diff changeset
960 #if _LIBGOMP_CHECKING_
kono
parents: 0
diff changeset
961 if (child_task->parent)
kono
parents: 0
diff changeset
962 priority_queue_verify (PQ_CHILDREN,
kono
parents: 0
diff changeset
963 &child_task->parent->children_queue, true);
kono
parents: 0
diff changeset
964 if (child_task->taskgroup)
kono
parents: 0
diff changeset
965 priority_queue_verify (PQ_TASKGROUP,
kono
parents: 0
diff changeset
966 &child_task->taskgroup->taskgroup_queue, false);
kono
parents: 0
diff changeset
967 priority_queue_verify (PQ_TEAM, &team->task_queue, false);
kono
parents: 0
diff changeset
968 #endif
kono
parents: 0
diff changeset
969
kono
parents: 0
diff changeset
970 /* Task is about to go tied, move it out of the way. */
kono
parents: 0
diff changeset
971 if (parent)
kono
parents: 0
diff changeset
972 priority_queue_downgrade_task (PQ_CHILDREN, &parent->children_queue,
kono
parents: 0
diff changeset
973 child_task);
kono
parents: 0
diff changeset
974
kono
parents: 0
diff changeset
975 /* Task is about to go tied, move it out of the way. */
kono
parents: 0
diff changeset
976 struct gomp_taskgroup *taskgroup = child_task->taskgroup;
kono
parents: 0
diff changeset
977 if (taskgroup)
kono
parents: 0
diff changeset
978 priority_queue_downgrade_task (PQ_TASKGROUP, &taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
979 child_task);
kono
parents: 0
diff changeset
980
kono
parents: 0
diff changeset
981 priority_queue_remove (PQ_TEAM, &team->task_queue, child_task,
kono
parents: 0
diff changeset
982 MEMMODEL_RELAXED);
kono
parents: 0
diff changeset
983 child_task->pnode[PQ_TEAM].next = NULL;
kono
parents: 0
diff changeset
984 child_task->pnode[PQ_TEAM].prev = NULL;
kono
parents: 0
diff changeset
985 child_task->kind = GOMP_TASK_TIED;
kono
parents: 0
diff changeset
986
kono
parents: 0
diff changeset
987 if (--team->task_queued_count == 0)
kono
parents: 0
diff changeset
988 gomp_team_barrier_clear_task_pending (&team->barrier);
kono
parents: 0
diff changeset
989 if ((gomp_team_barrier_cancelled (&team->barrier)
kono
parents: 0
diff changeset
990 || (taskgroup && taskgroup->cancelled))
kono
parents: 0
diff changeset
991 && !child_task->copy_ctors_done)
kono
parents: 0
diff changeset
992 return true;
kono
parents: 0
diff changeset
993 return false;
kono
parents: 0
diff changeset
994 }
kono
parents: 0
diff changeset
995
kono
parents: 0
diff changeset
996 static void
kono
parents: 0
diff changeset
997 gomp_task_run_post_handle_depend_hash (struct gomp_task *child_task)
kono
parents: 0
diff changeset
998 {
kono
parents: 0
diff changeset
999 struct gomp_task *parent = child_task->parent;
kono
parents: 0
diff changeset
1000 size_t i;
kono
parents: 0
diff changeset
1001
kono
parents: 0
diff changeset
1002 for (i = 0; i < child_task->depend_count; i++)
kono
parents: 0
diff changeset
1003 if (!child_task->depend[i].redundant)
kono
parents: 0
diff changeset
1004 {
kono
parents: 0
diff changeset
1005 if (child_task->depend[i].next)
kono
parents: 0
diff changeset
1006 child_task->depend[i].next->prev = child_task->depend[i].prev;
kono
parents: 0
diff changeset
1007 if (child_task->depend[i].prev)
kono
parents: 0
diff changeset
1008 child_task->depend[i].prev->next = child_task->depend[i].next;
kono
parents: 0
diff changeset
1009 else
kono
parents: 0
diff changeset
1010 {
kono
parents: 0
diff changeset
1011 hash_entry_type *slot
kono
parents: 0
diff changeset
1012 = htab_find_slot (&parent->depend_hash, &child_task->depend[i],
kono
parents: 0
diff changeset
1013 NO_INSERT);
kono
parents: 0
diff changeset
1014 if (*slot != &child_task->depend[i])
kono
parents: 0
diff changeset
1015 abort ();
kono
parents: 0
diff changeset
1016 if (child_task->depend[i].next)
kono
parents: 0
diff changeset
1017 *slot = child_task->depend[i].next;
kono
parents: 0
diff changeset
1018 else
kono
parents: 0
diff changeset
1019 htab_clear_slot (parent->depend_hash, slot);
kono
parents: 0
diff changeset
1020 }
kono
parents: 0
diff changeset
1021 }
kono
parents: 0
diff changeset
1022 }
kono
parents: 0
diff changeset
1023
kono
parents: 0
diff changeset
1024 /* After a CHILD_TASK has been run, adjust the dependency queue for
kono
parents: 0
diff changeset
1025 each task that depends on CHILD_TASK, to record the fact that there
kono
parents: 0
diff changeset
1026 is one less dependency to worry about. If a task that depended on
kono
parents: 0
diff changeset
1027 CHILD_TASK now has no dependencies, place it in the various queues
kono
parents: 0
diff changeset
1028 so it gets scheduled to run.
kono
parents: 0
diff changeset
1029
kono
parents: 0
diff changeset
1030 TEAM is the team to which CHILD_TASK belongs to. */
kono
parents: 0
diff changeset
1031
kono
parents: 0
diff changeset
1032 static size_t
kono
parents: 0
diff changeset
1033 gomp_task_run_post_handle_dependers (struct gomp_task *child_task,
kono
parents: 0
diff changeset
1034 struct gomp_team *team)
kono
parents: 0
diff changeset
1035 {
kono
parents: 0
diff changeset
1036 struct gomp_task *parent = child_task->parent;
kono
parents: 0
diff changeset
1037 size_t i, count = child_task->dependers->n_elem, ret = 0;
kono
parents: 0
diff changeset
1038 for (i = 0; i < count; i++)
kono
parents: 0
diff changeset
1039 {
kono
parents: 0
diff changeset
1040 struct gomp_task *task = child_task->dependers->elem[i];
kono
parents: 0
diff changeset
1041
kono
parents: 0
diff changeset
1042 /* CHILD_TASK satisfies a dependency for TASK. Keep track of
kono
parents: 0
diff changeset
1043 TASK's remaining dependencies. Once TASK has no other
kono
parents: 0
diff changeset
1044 depenencies, put it into the various queues so it will get
kono
parents: 0
diff changeset
1045 scheduled for execution. */
kono
parents: 0
diff changeset
1046 if (--task->num_dependees != 0)
kono
parents: 0
diff changeset
1047 continue;
kono
parents: 0
diff changeset
1048
kono
parents: 0
diff changeset
1049 struct gomp_taskgroup *taskgroup = task->taskgroup;
kono
parents: 0
diff changeset
1050 if (parent)
kono
parents: 0
diff changeset
1051 {
kono
parents: 0
diff changeset
1052 priority_queue_insert (PQ_CHILDREN, &parent->children_queue,
kono
parents: 0
diff changeset
1053 task, task->priority,
kono
parents: 0
diff changeset
1054 PRIORITY_INSERT_BEGIN,
kono
parents: 0
diff changeset
1055 /*adjust_parent_depends_on=*/true,
kono
parents: 0
diff changeset
1056 task->parent_depends_on);
kono
parents: 0
diff changeset
1057 if (parent->taskwait)
kono
parents: 0
diff changeset
1058 {
kono
parents: 0
diff changeset
1059 if (parent->taskwait->in_taskwait)
kono
parents: 0
diff changeset
1060 {
kono
parents: 0
diff changeset
1061 /* One more task has had its dependencies met.
kono
parents: 0
diff changeset
1062 Inform any waiters. */
kono
parents: 0
diff changeset
1063 parent->taskwait->in_taskwait = false;
kono
parents: 0
diff changeset
1064 gomp_sem_post (&parent->taskwait->taskwait_sem);
kono
parents: 0
diff changeset
1065 }
kono
parents: 0
diff changeset
1066 else if (parent->taskwait->in_depend_wait)
kono
parents: 0
diff changeset
1067 {
kono
parents: 0
diff changeset
1068 /* One more task has had its dependencies met.
kono
parents: 0
diff changeset
1069 Inform any waiters. */
kono
parents: 0
diff changeset
1070 parent->taskwait->in_depend_wait = false;
kono
parents: 0
diff changeset
1071 gomp_sem_post (&parent->taskwait->taskwait_sem);
kono
parents: 0
diff changeset
1072 }
kono
parents: 0
diff changeset
1073 }
kono
parents: 0
diff changeset
1074 }
kono
parents: 0
diff changeset
1075 if (taskgroup)
kono
parents: 0
diff changeset
1076 {
kono
parents: 0
diff changeset
1077 priority_queue_insert (PQ_TASKGROUP, &taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
1078 task, task->priority,
kono
parents: 0
diff changeset
1079 PRIORITY_INSERT_BEGIN,
kono
parents: 0
diff changeset
1080 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
1081 task->parent_depends_on);
kono
parents: 0
diff changeset
1082 if (taskgroup->in_taskgroup_wait)
kono
parents: 0
diff changeset
1083 {
kono
parents: 0
diff changeset
1084 /* One more task has had its dependencies met.
kono
parents: 0
diff changeset
1085 Inform any waiters. */
kono
parents: 0
diff changeset
1086 taskgroup->in_taskgroup_wait = false;
kono
parents: 0
diff changeset
1087 gomp_sem_post (&taskgroup->taskgroup_sem);
kono
parents: 0
diff changeset
1088 }
kono
parents: 0
diff changeset
1089 }
kono
parents: 0
diff changeset
1090 priority_queue_insert (PQ_TEAM, &team->task_queue,
kono
parents: 0
diff changeset
1091 task, task->priority,
kono
parents: 0
diff changeset
1092 PRIORITY_INSERT_END,
kono
parents: 0
diff changeset
1093 /*adjust_parent_depends_on=*/false,
kono
parents: 0
diff changeset
1094 task->parent_depends_on);
kono
parents: 0
diff changeset
1095 ++team->task_count;
kono
parents: 0
diff changeset
1096 ++team->task_queued_count;
kono
parents: 0
diff changeset
1097 ++ret;
kono
parents: 0
diff changeset
1098 }
kono
parents: 0
diff changeset
1099 free (child_task->dependers);
kono
parents: 0
diff changeset
1100 child_task->dependers = NULL;
kono
parents: 0
diff changeset
1101 if (ret > 1)
kono
parents: 0
diff changeset
1102 gomp_team_barrier_set_task_pending (&team->barrier);
kono
parents: 0
diff changeset
1103 return ret;
kono
parents: 0
diff changeset
1104 }
kono
parents: 0
diff changeset
1105
kono
parents: 0
diff changeset
1106 static inline size_t
kono
parents: 0
diff changeset
1107 gomp_task_run_post_handle_depend (struct gomp_task *child_task,
kono
parents: 0
diff changeset
1108 struct gomp_team *team)
kono
parents: 0
diff changeset
1109 {
kono
parents: 0
diff changeset
1110 if (child_task->depend_count == 0)
kono
parents: 0
diff changeset
1111 return 0;
kono
parents: 0
diff changeset
1112
kono
parents: 0
diff changeset
1113 /* If parent is gone already, the hash table is freed and nothing
kono
parents: 0
diff changeset
1114 will use the hash table anymore, no need to remove anything from it. */
kono
parents: 0
diff changeset
1115 if (child_task->parent != NULL)
kono
parents: 0
diff changeset
1116 gomp_task_run_post_handle_depend_hash (child_task);
kono
parents: 0
diff changeset
1117
kono
parents: 0
diff changeset
1118 if (child_task->dependers == NULL)
kono
parents: 0
diff changeset
1119 return 0;
kono
parents: 0
diff changeset
1120
kono
parents: 0
diff changeset
1121 return gomp_task_run_post_handle_dependers (child_task, team);
kono
parents: 0
diff changeset
1122 }
kono
parents: 0
diff changeset
1123
kono
parents: 0
diff changeset
1124 /* Remove CHILD_TASK from its parent. */
kono
parents: 0
diff changeset
1125
kono
parents: 0
diff changeset
1126 static inline void
kono
parents: 0
diff changeset
1127 gomp_task_run_post_remove_parent (struct gomp_task *child_task)
kono
parents: 0
diff changeset
1128 {
kono
parents: 0
diff changeset
1129 struct gomp_task *parent = child_task->parent;
kono
parents: 0
diff changeset
1130 if (parent == NULL)
kono
parents: 0
diff changeset
1131 return;
kono
parents: 0
diff changeset
1132
kono
parents: 0
diff changeset
1133 /* If this was the last task the parent was depending on,
kono
parents: 0
diff changeset
1134 synchronize with gomp_task_maybe_wait_for_dependencies so it can
kono
parents: 0
diff changeset
1135 clean up and return. */
kono
parents: 0
diff changeset
1136 if (__builtin_expect (child_task->parent_depends_on, 0)
kono
parents: 0
diff changeset
1137 && --parent->taskwait->n_depend == 0
kono
parents: 0
diff changeset
1138 && parent->taskwait->in_depend_wait)
kono
parents: 0
diff changeset
1139 {
kono
parents: 0
diff changeset
1140 parent->taskwait->in_depend_wait = false;
kono
parents: 0
diff changeset
1141 gomp_sem_post (&parent->taskwait->taskwait_sem);
kono
parents: 0
diff changeset
1142 }
kono
parents: 0
diff changeset
1143
kono
parents: 0
diff changeset
1144 if (priority_queue_remove (PQ_CHILDREN, &parent->children_queue,
kono
parents: 0
diff changeset
1145 child_task, MEMMODEL_RELEASE)
kono
parents: 0
diff changeset
1146 && parent->taskwait && parent->taskwait->in_taskwait)
kono
parents: 0
diff changeset
1147 {
kono
parents: 0
diff changeset
1148 parent->taskwait->in_taskwait = false;
kono
parents: 0
diff changeset
1149 gomp_sem_post (&parent->taskwait->taskwait_sem);
kono
parents: 0
diff changeset
1150 }
kono
parents: 0
diff changeset
1151 child_task->pnode[PQ_CHILDREN].next = NULL;
kono
parents: 0
diff changeset
1152 child_task->pnode[PQ_CHILDREN].prev = NULL;
kono
parents: 0
diff changeset
1153 }
kono
parents: 0
diff changeset
1154
kono
parents: 0
diff changeset
1155 /* Remove CHILD_TASK from its taskgroup. */
kono
parents: 0
diff changeset
1156
kono
parents: 0
diff changeset
1157 static inline void
kono
parents: 0
diff changeset
1158 gomp_task_run_post_remove_taskgroup (struct gomp_task *child_task)
kono
parents: 0
diff changeset
1159 {
kono
parents: 0
diff changeset
1160 struct gomp_taskgroup *taskgroup = child_task->taskgroup;
kono
parents: 0
diff changeset
1161 if (taskgroup == NULL)
kono
parents: 0
diff changeset
1162 return;
kono
parents: 0
diff changeset
1163 bool empty = priority_queue_remove (PQ_TASKGROUP,
kono
parents: 0
diff changeset
1164 &taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
1165 child_task, MEMMODEL_RELAXED);
kono
parents: 0
diff changeset
1166 child_task->pnode[PQ_TASKGROUP].next = NULL;
kono
parents: 0
diff changeset
1167 child_task->pnode[PQ_TASKGROUP].prev = NULL;
kono
parents: 0
diff changeset
1168 if (taskgroup->num_children > 1)
kono
parents: 0
diff changeset
1169 --taskgroup->num_children;
kono
parents: 0
diff changeset
1170 else
kono
parents: 0
diff changeset
1171 {
kono
parents: 0
diff changeset
1172 /* We access taskgroup->num_children in GOMP_taskgroup_end
kono
parents: 0
diff changeset
1173 outside of the task lock mutex region, so
kono
parents: 0
diff changeset
1174 need a release barrier here to ensure memory
kono
parents: 0
diff changeset
1175 written by child_task->fn above is flushed
kono
parents: 0
diff changeset
1176 before the NULL is written. */
kono
parents: 0
diff changeset
1177 __atomic_store_n (&taskgroup->num_children, 0, MEMMODEL_RELEASE);
kono
parents: 0
diff changeset
1178 }
kono
parents: 0
diff changeset
1179 if (empty && taskgroup->in_taskgroup_wait)
kono
parents: 0
diff changeset
1180 {
kono
parents: 0
diff changeset
1181 taskgroup->in_taskgroup_wait = false;
kono
parents: 0
diff changeset
1182 gomp_sem_post (&taskgroup->taskgroup_sem);
kono
parents: 0
diff changeset
1183 }
kono
parents: 0
diff changeset
1184 }
kono
parents: 0
diff changeset
1185
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1186 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1187 gomp_barrier_handle_tasks (gomp_barrier_state_t state)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1188 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1189 struct gomp_thread *thr = gomp_thread ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1190 struct gomp_team *team = thr->ts.team;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1191 struct gomp_task *task = thr->task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1192 struct gomp_task *child_task = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1193 struct gomp_task *to_free = NULL;
111
kono
parents: 0
diff changeset
1194 int do_wake = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1195
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1196 gomp_mutex_lock (&team->task_lock);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1197 if (gomp_barrier_last_thread (state))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1198 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1199 if (team->task_count == 0)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1200 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1201 gomp_team_barrier_done (&team->barrier, state);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1202 gomp_mutex_unlock (&team->task_lock);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1203 gomp_team_barrier_wake (&team->barrier, 0);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1204 return;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1205 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1206 gomp_team_barrier_set_waiting_for_tasks (&team->barrier);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1207 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1208
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1209 while (1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1210 {
111
kono
parents: 0
diff changeset
1211 bool cancelled = false;
kono
parents: 0
diff changeset
1212 if (!priority_queue_empty_p (&team->task_queue, MEMMODEL_RELAXED))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1213 {
111
kono
parents: 0
diff changeset
1214 bool ignored;
kono
parents: 0
diff changeset
1215 child_task
kono
parents: 0
diff changeset
1216 = priority_queue_next_task (PQ_TEAM, &team->task_queue,
kono
parents: 0
diff changeset
1217 PQ_IGNORED, NULL,
kono
parents: 0
diff changeset
1218 &ignored);
kono
parents: 0
diff changeset
1219 cancelled = gomp_task_run_pre (child_task, child_task->parent,
kono
parents: 0
diff changeset
1220 team);
kono
parents: 0
diff changeset
1221 if (__builtin_expect (cancelled, 0))
kono
parents: 0
diff changeset
1222 {
kono
parents: 0
diff changeset
1223 if (to_free)
kono
parents: 0
diff changeset
1224 {
kono
parents: 0
diff changeset
1225 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1226 free (to_free);
kono
parents: 0
diff changeset
1227 to_free = NULL;
kono
parents: 0
diff changeset
1228 }
kono
parents: 0
diff changeset
1229 goto finish_cancelled;
kono
parents: 0
diff changeset
1230 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1231 team->task_running_count++;
111
kono
parents: 0
diff changeset
1232 child_task->in_tied_task = true;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1233 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1234 gomp_mutex_unlock (&team->task_lock);
111
kono
parents: 0
diff changeset
1235 if (do_wake)
kono
parents: 0
diff changeset
1236 {
kono
parents: 0
diff changeset
1237 gomp_team_barrier_wake (&team->barrier, do_wake);
kono
parents: 0
diff changeset
1238 do_wake = 0;
kono
parents: 0
diff changeset
1239 }
kono
parents: 0
diff changeset
1240 if (to_free)
kono
parents: 0
diff changeset
1241 {
kono
parents: 0
diff changeset
1242 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1243 free (to_free);
kono
parents: 0
diff changeset
1244 to_free = NULL;
kono
parents: 0
diff changeset
1245 }
kono
parents: 0
diff changeset
1246 if (child_task)
kono
parents: 0
diff changeset
1247 {
kono
parents: 0
diff changeset
1248 thr->task = child_task;
kono
parents: 0
diff changeset
1249 if (__builtin_expect (child_task->fn == NULL, 0))
kono
parents: 0
diff changeset
1250 {
kono
parents: 0
diff changeset
1251 if (gomp_target_task_fn (child_task->fn_data))
kono
parents: 0
diff changeset
1252 {
kono
parents: 0
diff changeset
1253 thr->task = task;
kono
parents: 0
diff changeset
1254 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1255 child_task->kind = GOMP_TASK_ASYNC_RUNNING;
kono
parents: 0
diff changeset
1256 team->task_running_count--;
kono
parents: 0
diff changeset
1257 struct gomp_target_task *ttask
kono
parents: 0
diff changeset
1258 = (struct gomp_target_task *) child_task->fn_data;
kono
parents: 0
diff changeset
1259 /* If GOMP_PLUGIN_target_task_completion has run already
kono
parents: 0
diff changeset
1260 in between gomp_target_task_fn and the mutex lock,
kono
parents: 0
diff changeset
1261 perform the requeuing here. */
kono
parents: 0
diff changeset
1262 if (ttask->state == GOMP_TARGET_TASK_FINISHED)
kono
parents: 0
diff changeset
1263 gomp_target_task_completion (team, child_task);
kono
parents: 0
diff changeset
1264 else
kono
parents: 0
diff changeset
1265 ttask->state = GOMP_TARGET_TASK_RUNNING;
kono
parents: 0
diff changeset
1266 child_task = NULL;
kono
parents: 0
diff changeset
1267 continue;
kono
parents: 0
diff changeset
1268 }
kono
parents: 0
diff changeset
1269 }
kono
parents: 0
diff changeset
1270 else
kono
parents: 0
diff changeset
1271 child_task->fn (child_task->fn_data);
kono
parents: 0
diff changeset
1272 thr->task = task;
kono
parents: 0
diff changeset
1273 }
kono
parents: 0
diff changeset
1274 else
kono
parents: 0
diff changeset
1275 return;
kono
parents: 0
diff changeset
1276 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1277 if (child_task)
kono
parents: 0
diff changeset
1278 {
kono
parents: 0
diff changeset
1279 finish_cancelled:;
kono
parents: 0
diff changeset
1280 size_t new_tasks
kono
parents: 0
diff changeset
1281 = gomp_task_run_post_handle_depend (child_task, team);
kono
parents: 0
diff changeset
1282 gomp_task_run_post_remove_parent (child_task);
kono
parents: 0
diff changeset
1283 gomp_clear_parent (&child_task->children_queue);
kono
parents: 0
diff changeset
1284 gomp_task_run_post_remove_taskgroup (child_task);
kono
parents: 0
diff changeset
1285 to_free = child_task;
kono
parents: 0
diff changeset
1286 child_task = NULL;
kono
parents: 0
diff changeset
1287 if (!cancelled)
kono
parents: 0
diff changeset
1288 team->task_running_count--;
kono
parents: 0
diff changeset
1289 if (new_tasks > 1)
kono
parents: 0
diff changeset
1290 {
kono
parents: 0
diff changeset
1291 do_wake = team->nthreads - team->task_running_count;
kono
parents: 0
diff changeset
1292 if (do_wake > new_tasks)
kono
parents: 0
diff changeset
1293 do_wake = new_tasks;
kono
parents: 0
diff changeset
1294 }
kono
parents: 0
diff changeset
1295 if (--team->task_count == 0
kono
parents: 0
diff changeset
1296 && gomp_team_barrier_waiting_for_tasks (&team->barrier))
kono
parents: 0
diff changeset
1297 {
kono
parents: 0
diff changeset
1298 gomp_team_barrier_done (&team->barrier, state);
kono
parents: 0
diff changeset
1299 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
1300 gomp_team_barrier_wake (&team->barrier, 0);
kono
parents: 0
diff changeset
1301 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1302 }
kono
parents: 0
diff changeset
1303 }
kono
parents: 0
diff changeset
1304 }
kono
parents: 0
diff changeset
1305 }
kono
parents: 0
diff changeset
1306
kono
parents: 0
diff changeset
1307 /* Called when encountering a taskwait directive.
kono
parents: 0
diff changeset
1308
kono
parents: 0
diff changeset
1309 Wait for all children of the current task. */
kono
parents: 0
diff changeset
1310
kono
parents: 0
diff changeset
1311 void
kono
parents: 0
diff changeset
1312 GOMP_taskwait (void)
kono
parents: 0
diff changeset
1313 {
kono
parents: 0
diff changeset
1314 struct gomp_thread *thr = gomp_thread ();
kono
parents: 0
diff changeset
1315 struct gomp_team *team = thr->ts.team;
kono
parents: 0
diff changeset
1316 struct gomp_task *task = thr->task;
kono
parents: 0
diff changeset
1317 struct gomp_task *child_task = NULL;
kono
parents: 0
diff changeset
1318 struct gomp_task *to_free = NULL;
kono
parents: 0
diff changeset
1319 struct gomp_taskwait taskwait;
kono
parents: 0
diff changeset
1320 int do_wake = 0;
kono
parents: 0
diff changeset
1321
kono
parents: 0
diff changeset
1322 /* The acquire barrier on load of task->children here synchronizes
kono
parents: 0
diff changeset
1323 with the write of a NULL in gomp_task_run_post_remove_parent. It is
kono
parents: 0
diff changeset
1324 not necessary that we synchronize with other non-NULL writes at
kono
parents: 0
diff changeset
1325 this point, but we must ensure that all writes to memory by a
kono
parents: 0
diff changeset
1326 child thread task work function are seen before we exit from
kono
parents: 0
diff changeset
1327 GOMP_taskwait. */
kono
parents: 0
diff changeset
1328 if (task == NULL
kono
parents: 0
diff changeset
1329 || priority_queue_empty_p (&task->children_queue, MEMMODEL_ACQUIRE))
kono
parents: 0
diff changeset
1330 return;
kono
parents: 0
diff changeset
1331
kono
parents: 0
diff changeset
1332 memset (&taskwait, 0, sizeof (taskwait));
kono
parents: 0
diff changeset
1333 bool child_q = false;
kono
parents: 0
diff changeset
1334 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1335 while (1)
kono
parents: 0
diff changeset
1336 {
kono
parents: 0
diff changeset
1337 bool cancelled = false;
kono
parents: 0
diff changeset
1338 if (priority_queue_empty_p (&task->children_queue, MEMMODEL_RELAXED))
kono
parents: 0
diff changeset
1339 {
kono
parents: 0
diff changeset
1340 bool destroy_taskwait = task->taskwait != NULL;
kono
parents: 0
diff changeset
1341 task->taskwait = NULL;
kono
parents: 0
diff changeset
1342 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
1343 if (to_free)
kono
parents: 0
diff changeset
1344 {
kono
parents: 0
diff changeset
1345 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1346 free (to_free);
kono
parents: 0
diff changeset
1347 }
kono
parents: 0
diff changeset
1348 if (destroy_taskwait)
kono
parents: 0
diff changeset
1349 gomp_sem_destroy (&taskwait.taskwait_sem);
kono
parents: 0
diff changeset
1350 return;
kono
parents: 0
diff changeset
1351 }
kono
parents: 0
diff changeset
1352 struct gomp_task *next_task
kono
parents: 0
diff changeset
1353 = priority_queue_next_task (PQ_CHILDREN, &task->children_queue,
kono
parents: 0
diff changeset
1354 PQ_TEAM, &team->task_queue, &child_q);
kono
parents: 0
diff changeset
1355 if (next_task->kind == GOMP_TASK_WAITING)
kono
parents: 0
diff changeset
1356 {
kono
parents: 0
diff changeset
1357 child_task = next_task;
kono
parents: 0
diff changeset
1358 cancelled
kono
parents: 0
diff changeset
1359 = gomp_task_run_pre (child_task, task, team);
kono
parents: 0
diff changeset
1360 if (__builtin_expect (cancelled, 0))
kono
parents: 0
diff changeset
1361 {
kono
parents: 0
diff changeset
1362 if (to_free)
kono
parents: 0
diff changeset
1363 {
kono
parents: 0
diff changeset
1364 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1365 free (to_free);
kono
parents: 0
diff changeset
1366 to_free = NULL;
kono
parents: 0
diff changeset
1367 }
kono
parents: 0
diff changeset
1368 goto finish_cancelled;
kono
parents: 0
diff changeset
1369 }
kono
parents: 0
diff changeset
1370 }
kono
parents: 0
diff changeset
1371 else
kono
parents: 0
diff changeset
1372 {
kono
parents: 0
diff changeset
1373 /* All tasks we are waiting for are either running in other
kono
parents: 0
diff changeset
1374 threads, or they are tasks that have not had their
kono
parents: 0
diff changeset
1375 dependencies met (so they're not even in the queue). Wait
kono
parents: 0
diff changeset
1376 for them. */
kono
parents: 0
diff changeset
1377 if (task->taskwait == NULL)
kono
parents: 0
diff changeset
1378 {
kono
parents: 0
diff changeset
1379 taskwait.in_depend_wait = false;
kono
parents: 0
diff changeset
1380 gomp_sem_init (&taskwait.taskwait_sem, 0);
kono
parents: 0
diff changeset
1381 task->taskwait = &taskwait;
kono
parents: 0
diff changeset
1382 }
kono
parents: 0
diff changeset
1383 taskwait.in_taskwait = true;
kono
parents: 0
diff changeset
1384 }
kono
parents: 0
diff changeset
1385 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
1386 if (do_wake)
kono
parents: 0
diff changeset
1387 {
kono
parents: 0
diff changeset
1388 gomp_team_barrier_wake (&team->barrier, do_wake);
kono
parents: 0
diff changeset
1389 do_wake = 0;
kono
parents: 0
diff changeset
1390 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1391 if (to_free)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1392 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1393 gomp_finish_task (to_free);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1394 free (to_free);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1395 to_free = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1396 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1397 if (child_task)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1398 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1399 thr->task = child_task;
111
kono
parents: 0
diff changeset
1400 if (__builtin_expect (child_task->fn == NULL, 0))
kono
parents: 0
diff changeset
1401 {
kono
parents: 0
diff changeset
1402 if (gomp_target_task_fn (child_task->fn_data))
kono
parents: 0
diff changeset
1403 {
kono
parents: 0
diff changeset
1404 thr->task = task;
kono
parents: 0
diff changeset
1405 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1406 child_task->kind = GOMP_TASK_ASYNC_RUNNING;
kono
parents: 0
diff changeset
1407 struct gomp_target_task *ttask
kono
parents: 0
diff changeset
1408 = (struct gomp_target_task *) child_task->fn_data;
kono
parents: 0
diff changeset
1409 /* If GOMP_PLUGIN_target_task_completion has run already
kono
parents: 0
diff changeset
1410 in between gomp_target_task_fn and the mutex lock,
kono
parents: 0
diff changeset
1411 perform the requeuing here. */
kono
parents: 0
diff changeset
1412 if (ttask->state == GOMP_TARGET_TASK_FINISHED)
kono
parents: 0
diff changeset
1413 gomp_target_task_completion (team, child_task);
kono
parents: 0
diff changeset
1414 else
kono
parents: 0
diff changeset
1415 ttask->state = GOMP_TARGET_TASK_RUNNING;
kono
parents: 0
diff changeset
1416 child_task = NULL;
kono
parents: 0
diff changeset
1417 continue;
kono
parents: 0
diff changeset
1418 }
kono
parents: 0
diff changeset
1419 }
kono
parents: 0
diff changeset
1420 else
kono
parents: 0
diff changeset
1421 child_task->fn (child_task->fn_data);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1422 thr->task = task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1423 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1424 else
111
kono
parents: 0
diff changeset
1425 gomp_sem_wait (&taskwait.taskwait_sem);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1426 gomp_mutex_lock (&team->task_lock);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1427 if (child_task)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1428 {
111
kono
parents: 0
diff changeset
1429 finish_cancelled:;
kono
parents: 0
diff changeset
1430 size_t new_tasks
kono
parents: 0
diff changeset
1431 = gomp_task_run_post_handle_depend (child_task, team);
kono
parents: 0
diff changeset
1432
kono
parents: 0
diff changeset
1433 if (child_q)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1434 {
111
kono
parents: 0
diff changeset
1435 priority_queue_remove (PQ_CHILDREN, &task->children_queue,
kono
parents: 0
diff changeset
1436 child_task, MEMMODEL_RELAXED);
kono
parents: 0
diff changeset
1437 child_task->pnode[PQ_CHILDREN].next = NULL;
kono
parents: 0
diff changeset
1438 child_task->pnode[PQ_CHILDREN].prev = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1439 }
111
kono
parents: 0
diff changeset
1440
kono
parents: 0
diff changeset
1441 gomp_clear_parent (&child_task->children_queue);
kono
parents: 0
diff changeset
1442
kono
parents: 0
diff changeset
1443 gomp_task_run_post_remove_taskgroup (child_task);
kono
parents: 0
diff changeset
1444
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1445 to_free = child_task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1446 child_task = NULL;
111
kono
parents: 0
diff changeset
1447 team->task_count--;
kono
parents: 0
diff changeset
1448 if (new_tasks > 1)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1449 {
111
kono
parents: 0
diff changeset
1450 do_wake = team->nthreads - team->task_running_count
kono
parents: 0
diff changeset
1451 - !task->in_tied_task;
kono
parents: 0
diff changeset
1452 if (do_wake > new_tasks)
kono
parents: 0
diff changeset
1453 do_wake = new_tasks;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1454 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1455 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1456 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1457 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1458
111
kono
parents: 0
diff changeset
1459 /* An undeferred task is about to run. Wait for all tasks that this
kono
parents: 0
diff changeset
1460 undeferred task depends on.
kono
parents: 0
diff changeset
1461
kono
parents: 0
diff changeset
1462 This is done by first putting all known ready dependencies
kono
parents: 0
diff changeset
1463 (dependencies that have their own dependencies met) at the top of
kono
parents: 0
diff changeset
1464 the scheduling queues. Then we iterate through these imminently
kono
parents: 0
diff changeset
1465 ready tasks (and possibly other high priority tasks), and run them.
kono
parents: 0
diff changeset
1466 If we run out of ready dependencies to execute, we either wait for
kono
parents: 0
diff changeset
1467 the reamining dependencies to finish, or wait for them to get
kono
parents: 0
diff changeset
1468 scheduled so we can run them.
kono
parents: 0
diff changeset
1469
kono
parents: 0
diff changeset
1470 DEPEND is as in GOMP_task. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1471
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1472 void
111
kono
parents: 0
diff changeset
1473 gomp_task_maybe_wait_for_dependencies (void **depend)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1474 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1475 struct gomp_thread *thr = gomp_thread ();
111
kono
parents: 0
diff changeset
1476 struct gomp_task *task = thr->task;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1477 struct gomp_team *team = thr->ts.team;
111
kono
parents: 0
diff changeset
1478 struct gomp_task_depend_entry elem, *ent = NULL;
kono
parents: 0
diff changeset
1479 struct gomp_taskwait taskwait;
kono
parents: 0
diff changeset
1480 size_t ndepend = (uintptr_t) depend[0];
kono
parents: 0
diff changeset
1481 size_t nout = (uintptr_t) depend[1];
kono
parents: 0
diff changeset
1482 size_t i;
kono
parents: 0
diff changeset
1483 size_t num_awaited = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1484 struct gomp_task *child_task = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1485 struct gomp_task *to_free = NULL;
111
kono
parents: 0
diff changeset
1486 int do_wake = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1487
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1488 gomp_mutex_lock (&team->task_lock);
111
kono
parents: 0
diff changeset
1489 for (i = 0; i < ndepend; i++)
kono
parents: 0
diff changeset
1490 {
kono
parents: 0
diff changeset
1491 elem.addr = depend[i + 2];
kono
parents: 0
diff changeset
1492 ent = htab_find (task->depend_hash, &elem);
kono
parents: 0
diff changeset
1493 for (; ent; ent = ent->next)
kono
parents: 0
diff changeset
1494 if (i >= nout && ent->is_in)
kono
parents: 0
diff changeset
1495 continue;
kono
parents: 0
diff changeset
1496 else
kono
parents: 0
diff changeset
1497 {
kono
parents: 0
diff changeset
1498 struct gomp_task *tsk = ent->task;
kono
parents: 0
diff changeset
1499 if (!tsk->parent_depends_on)
kono
parents: 0
diff changeset
1500 {
kono
parents: 0
diff changeset
1501 tsk->parent_depends_on = true;
kono
parents: 0
diff changeset
1502 ++num_awaited;
kono
parents: 0
diff changeset
1503 /* If depenency TSK itself has no dependencies and is
kono
parents: 0
diff changeset
1504 ready to run, move it up front so that we run it as
kono
parents: 0
diff changeset
1505 soon as possible. */
kono
parents: 0
diff changeset
1506 if (tsk->num_dependees == 0 && tsk->kind == GOMP_TASK_WAITING)
kono
parents: 0
diff changeset
1507 priority_queue_upgrade_task (tsk, task);
kono
parents: 0
diff changeset
1508 }
kono
parents: 0
diff changeset
1509 }
kono
parents: 0
diff changeset
1510 }
kono
parents: 0
diff changeset
1511 if (num_awaited == 0)
kono
parents: 0
diff changeset
1512 {
kono
parents: 0
diff changeset
1513 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
1514 return;
kono
parents: 0
diff changeset
1515 }
kono
parents: 0
diff changeset
1516
kono
parents: 0
diff changeset
1517 memset (&taskwait, 0, sizeof (taskwait));
kono
parents: 0
diff changeset
1518 taskwait.n_depend = num_awaited;
kono
parents: 0
diff changeset
1519 gomp_sem_init (&taskwait.taskwait_sem, 0);
kono
parents: 0
diff changeset
1520 task->taskwait = &taskwait;
kono
parents: 0
diff changeset
1521
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1522 while (1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1523 {
111
kono
parents: 0
diff changeset
1524 bool cancelled = false;
kono
parents: 0
diff changeset
1525 if (taskwait.n_depend == 0)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1526 {
111
kono
parents: 0
diff changeset
1527 task->taskwait = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1528 gomp_mutex_unlock (&team->task_lock);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1529 if (to_free)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1530 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1531 gomp_finish_task (to_free);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1532 free (to_free);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1533 }
111
kono
parents: 0
diff changeset
1534 gomp_sem_destroy (&taskwait.taskwait_sem);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1535 return;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1536 }
111
kono
parents: 0
diff changeset
1537
kono
parents: 0
diff changeset
1538 /* Theoretically when we have multiple priorities, we should
kono
parents: 0
diff changeset
1539 chose between the highest priority item in
kono
parents: 0
diff changeset
1540 task->children_queue and team->task_queue here, so we should
kono
parents: 0
diff changeset
1541 use priority_queue_next_task(). However, since we are
kono
parents: 0
diff changeset
1542 running an undeferred task, perhaps that makes all tasks it
kono
parents: 0
diff changeset
1543 depends on undeferred, thus a priority of INF? This would
kono
parents: 0
diff changeset
1544 make it unnecessary to take anything into account here,
kono
parents: 0
diff changeset
1545 but the dependencies.
kono
parents: 0
diff changeset
1546
kono
parents: 0
diff changeset
1547 On the other hand, if we want to use priority_queue_next_task(),
kono
parents: 0
diff changeset
1548 care should be taken to only use priority_queue_remove()
kono
parents: 0
diff changeset
1549 below if the task was actually removed from the children
kono
parents: 0
diff changeset
1550 queue. */
kono
parents: 0
diff changeset
1551 bool ignored;
kono
parents: 0
diff changeset
1552 struct gomp_task *next_task
kono
parents: 0
diff changeset
1553 = priority_queue_next_task (PQ_CHILDREN, &task->children_queue,
kono
parents: 0
diff changeset
1554 PQ_IGNORED, NULL, &ignored);
kono
parents: 0
diff changeset
1555
kono
parents: 0
diff changeset
1556 if (next_task->kind == GOMP_TASK_WAITING)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1557 {
111
kono
parents: 0
diff changeset
1558 child_task = next_task;
kono
parents: 0
diff changeset
1559 cancelled
kono
parents: 0
diff changeset
1560 = gomp_task_run_pre (child_task, task, team);
kono
parents: 0
diff changeset
1561 if (__builtin_expect (cancelled, 0))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1562 {
111
kono
parents: 0
diff changeset
1563 if (to_free)
kono
parents: 0
diff changeset
1564 {
kono
parents: 0
diff changeset
1565 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1566 free (to_free);
kono
parents: 0
diff changeset
1567 to_free = NULL;
kono
parents: 0
diff changeset
1568 }
kono
parents: 0
diff changeset
1569 goto finish_cancelled;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1570 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1571 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1572 else
111
kono
parents: 0
diff changeset
1573 /* All tasks we are waiting for are either running in other
kono
parents: 0
diff changeset
1574 threads, or they are tasks that have not had their
kono
parents: 0
diff changeset
1575 dependencies met (so they're not even in the queue). Wait
kono
parents: 0
diff changeset
1576 for them. */
kono
parents: 0
diff changeset
1577 taskwait.in_depend_wait = true;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1578 gomp_mutex_unlock (&team->task_lock);
111
kono
parents: 0
diff changeset
1579 if (do_wake)
kono
parents: 0
diff changeset
1580 {
kono
parents: 0
diff changeset
1581 gomp_team_barrier_wake (&team->barrier, do_wake);
kono
parents: 0
diff changeset
1582 do_wake = 0;
kono
parents: 0
diff changeset
1583 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1584 if (to_free)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1585 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1586 gomp_finish_task (to_free);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1587 free (to_free);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1588 to_free = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1589 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1590 if (child_task)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1591 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1592 thr->task = child_task;
111
kono
parents: 0
diff changeset
1593 if (__builtin_expect (child_task->fn == NULL, 0))
kono
parents: 0
diff changeset
1594 {
kono
parents: 0
diff changeset
1595 if (gomp_target_task_fn (child_task->fn_data))
kono
parents: 0
diff changeset
1596 {
kono
parents: 0
diff changeset
1597 thr->task = task;
kono
parents: 0
diff changeset
1598 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1599 child_task->kind = GOMP_TASK_ASYNC_RUNNING;
kono
parents: 0
diff changeset
1600 struct gomp_target_task *ttask
kono
parents: 0
diff changeset
1601 = (struct gomp_target_task *) child_task->fn_data;
kono
parents: 0
diff changeset
1602 /* If GOMP_PLUGIN_target_task_completion has run already
kono
parents: 0
diff changeset
1603 in between gomp_target_task_fn and the mutex lock,
kono
parents: 0
diff changeset
1604 perform the requeuing here. */
kono
parents: 0
diff changeset
1605 if (ttask->state == GOMP_TARGET_TASK_FINISHED)
kono
parents: 0
diff changeset
1606 gomp_target_task_completion (team, child_task);
kono
parents: 0
diff changeset
1607 else
kono
parents: 0
diff changeset
1608 ttask->state = GOMP_TARGET_TASK_RUNNING;
kono
parents: 0
diff changeset
1609 child_task = NULL;
kono
parents: 0
diff changeset
1610 continue;
kono
parents: 0
diff changeset
1611 }
kono
parents: 0
diff changeset
1612 }
kono
parents: 0
diff changeset
1613 else
kono
parents: 0
diff changeset
1614 child_task->fn (child_task->fn_data);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1615 thr->task = task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1616 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1617 else
111
kono
parents: 0
diff changeset
1618 gomp_sem_wait (&taskwait.taskwait_sem);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1619 gomp_mutex_lock (&team->task_lock);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1620 if (child_task)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1621 {
111
kono
parents: 0
diff changeset
1622 finish_cancelled:;
kono
parents: 0
diff changeset
1623 size_t new_tasks
kono
parents: 0
diff changeset
1624 = gomp_task_run_post_handle_depend (child_task, team);
kono
parents: 0
diff changeset
1625 if (child_task->parent_depends_on)
kono
parents: 0
diff changeset
1626 --taskwait.n_depend;
kono
parents: 0
diff changeset
1627
kono
parents: 0
diff changeset
1628 priority_queue_remove (PQ_CHILDREN, &task->children_queue,
kono
parents: 0
diff changeset
1629 child_task, MEMMODEL_RELAXED);
kono
parents: 0
diff changeset
1630 child_task->pnode[PQ_CHILDREN].next = NULL;
kono
parents: 0
diff changeset
1631 child_task->pnode[PQ_CHILDREN].prev = NULL;
kono
parents: 0
diff changeset
1632
kono
parents: 0
diff changeset
1633 gomp_clear_parent (&child_task->children_queue);
kono
parents: 0
diff changeset
1634 gomp_task_run_post_remove_taskgroup (child_task);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1635 to_free = child_task;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1636 child_task = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1637 team->task_count--;
111
kono
parents: 0
diff changeset
1638 if (new_tasks > 1)
kono
parents: 0
diff changeset
1639 {
kono
parents: 0
diff changeset
1640 do_wake = team->nthreads - team->task_running_count
kono
parents: 0
diff changeset
1641 - !task->in_tied_task;
kono
parents: 0
diff changeset
1642 if (do_wake > new_tasks)
kono
parents: 0
diff changeset
1643 do_wake = new_tasks;
kono
parents: 0
diff changeset
1644 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1645 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1646 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1647 }
111
kono
parents: 0
diff changeset
1648
kono
parents: 0
diff changeset
1649 /* Called when encountering a taskyield directive. */
kono
parents: 0
diff changeset
1650
kono
parents: 0
diff changeset
1651 void
kono
parents: 0
diff changeset
1652 GOMP_taskyield (void)
kono
parents: 0
diff changeset
1653 {
kono
parents: 0
diff changeset
1654 /* Nothing at the moment. */
kono
parents: 0
diff changeset
1655 }
kono
parents: 0
diff changeset
1656
kono
parents: 0
diff changeset
1657 void
kono
parents: 0
diff changeset
1658 GOMP_taskgroup_start (void)
kono
parents: 0
diff changeset
1659 {
kono
parents: 0
diff changeset
1660 struct gomp_thread *thr = gomp_thread ();
kono
parents: 0
diff changeset
1661 struct gomp_team *team = thr->ts.team;
kono
parents: 0
diff changeset
1662 struct gomp_task *task = thr->task;
kono
parents: 0
diff changeset
1663 struct gomp_taskgroup *taskgroup;
kono
parents: 0
diff changeset
1664
kono
parents: 0
diff changeset
1665 /* If team is NULL, all tasks are executed as
kono
parents: 0
diff changeset
1666 GOMP_TASK_UNDEFERRED tasks and thus all children tasks of
kono
parents: 0
diff changeset
1667 taskgroup and their descendant tasks will be finished
kono
parents: 0
diff changeset
1668 by the time GOMP_taskgroup_end is called. */
kono
parents: 0
diff changeset
1669 if (team == NULL)
kono
parents: 0
diff changeset
1670 return;
kono
parents: 0
diff changeset
1671 taskgroup = gomp_malloc (sizeof (struct gomp_taskgroup));
kono
parents: 0
diff changeset
1672 taskgroup->prev = task->taskgroup;
kono
parents: 0
diff changeset
1673 priority_queue_init (&taskgroup->taskgroup_queue);
kono
parents: 0
diff changeset
1674 taskgroup->in_taskgroup_wait = false;
kono
parents: 0
diff changeset
1675 taskgroup->cancelled = false;
kono
parents: 0
diff changeset
1676 taskgroup->num_children = 0;
kono
parents: 0
diff changeset
1677 gomp_sem_init (&taskgroup->taskgroup_sem, 0);
kono
parents: 0
diff changeset
1678 task->taskgroup = taskgroup;
kono
parents: 0
diff changeset
1679 }
kono
parents: 0
diff changeset
1680
kono
parents: 0
diff changeset
1681 void
kono
parents: 0
diff changeset
1682 GOMP_taskgroup_end (void)
kono
parents: 0
diff changeset
1683 {
kono
parents: 0
diff changeset
1684 struct gomp_thread *thr = gomp_thread ();
kono
parents: 0
diff changeset
1685 struct gomp_team *team = thr->ts.team;
kono
parents: 0
diff changeset
1686 struct gomp_task *task = thr->task;
kono
parents: 0
diff changeset
1687 struct gomp_taskgroup *taskgroup;
kono
parents: 0
diff changeset
1688 struct gomp_task *child_task = NULL;
kono
parents: 0
diff changeset
1689 struct gomp_task *to_free = NULL;
kono
parents: 0
diff changeset
1690 int do_wake = 0;
kono
parents: 0
diff changeset
1691
kono
parents: 0
diff changeset
1692 if (team == NULL)
kono
parents: 0
diff changeset
1693 return;
kono
parents: 0
diff changeset
1694 taskgroup = task->taskgroup;
kono
parents: 0
diff changeset
1695 if (__builtin_expect (taskgroup == NULL, 0)
kono
parents: 0
diff changeset
1696 && thr->ts.level == 0)
kono
parents: 0
diff changeset
1697 {
kono
parents: 0
diff changeset
1698 /* This can happen if GOMP_taskgroup_start is called when
kono
parents: 0
diff changeset
1699 thr->ts.team == NULL, but inside of the taskgroup there
kono
parents: 0
diff changeset
1700 is #pragma omp target nowait that creates an implicit
kono
parents: 0
diff changeset
1701 team with a single thread. In this case, we want to wait
kono
parents: 0
diff changeset
1702 for all outstanding tasks in this team. */
kono
parents: 0
diff changeset
1703 gomp_team_barrier_wait (&team->barrier);
kono
parents: 0
diff changeset
1704 return;
kono
parents: 0
diff changeset
1705 }
kono
parents: 0
diff changeset
1706
kono
parents: 0
diff changeset
1707 /* The acquire barrier on load of taskgroup->num_children here
kono
parents: 0
diff changeset
1708 synchronizes with the write of 0 in gomp_task_run_post_remove_taskgroup.
kono
parents: 0
diff changeset
1709 It is not necessary that we synchronize with other non-0 writes at
kono
parents: 0
diff changeset
1710 this point, but we must ensure that all writes to memory by a
kono
parents: 0
diff changeset
1711 child thread task work function are seen before we exit from
kono
parents: 0
diff changeset
1712 GOMP_taskgroup_end. */
kono
parents: 0
diff changeset
1713 if (__atomic_load_n (&taskgroup->num_children, MEMMODEL_ACQUIRE) == 0)
kono
parents: 0
diff changeset
1714 goto finish;
kono
parents: 0
diff changeset
1715
kono
parents: 0
diff changeset
1716 bool unused;
kono
parents: 0
diff changeset
1717 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1718 while (1)
kono
parents: 0
diff changeset
1719 {
kono
parents: 0
diff changeset
1720 bool cancelled = false;
kono
parents: 0
diff changeset
1721 if (priority_queue_empty_p (&taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
1722 MEMMODEL_RELAXED))
kono
parents: 0
diff changeset
1723 {
kono
parents: 0
diff changeset
1724 if (taskgroup->num_children)
kono
parents: 0
diff changeset
1725 {
kono
parents: 0
diff changeset
1726 if (priority_queue_empty_p (&task->children_queue,
kono
parents: 0
diff changeset
1727 MEMMODEL_RELAXED))
kono
parents: 0
diff changeset
1728 goto do_wait;
kono
parents: 0
diff changeset
1729 child_task
kono
parents: 0
diff changeset
1730 = priority_queue_next_task (PQ_CHILDREN, &task->children_queue,
kono
parents: 0
diff changeset
1731 PQ_TEAM, &team->task_queue,
kono
parents: 0
diff changeset
1732 &unused);
kono
parents: 0
diff changeset
1733 }
kono
parents: 0
diff changeset
1734 else
kono
parents: 0
diff changeset
1735 {
kono
parents: 0
diff changeset
1736 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
1737 if (to_free)
kono
parents: 0
diff changeset
1738 {
kono
parents: 0
diff changeset
1739 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1740 free (to_free);
kono
parents: 0
diff changeset
1741 }
kono
parents: 0
diff changeset
1742 goto finish;
kono
parents: 0
diff changeset
1743 }
kono
parents: 0
diff changeset
1744 }
kono
parents: 0
diff changeset
1745 else
kono
parents: 0
diff changeset
1746 child_task
kono
parents: 0
diff changeset
1747 = priority_queue_next_task (PQ_TASKGROUP, &taskgroup->taskgroup_queue,
kono
parents: 0
diff changeset
1748 PQ_TEAM, &team->task_queue, &unused);
kono
parents: 0
diff changeset
1749 if (child_task->kind == GOMP_TASK_WAITING)
kono
parents: 0
diff changeset
1750 {
kono
parents: 0
diff changeset
1751 cancelled
kono
parents: 0
diff changeset
1752 = gomp_task_run_pre (child_task, child_task->parent, team);
kono
parents: 0
diff changeset
1753 if (__builtin_expect (cancelled, 0))
kono
parents: 0
diff changeset
1754 {
kono
parents: 0
diff changeset
1755 if (to_free)
kono
parents: 0
diff changeset
1756 {
kono
parents: 0
diff changeset
1757 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1758 free (to_free);
kono
parents: 0
diff changeset
1759 to_free = NULL;
kono
parents: 0
diff changeset
1760 }
kono
parents: 0
diff changeset
1761 goto finish_cancelled;
kono
parents: 0
diff changeset
1762 }
kono
parents: 0
diff changeset
1763 }
kono
parents: 0
diff changeset
1764 else
kono
parents: 0
diff changeset
1765 {
kono
parents: 0
diff changeset
1766 child_task = NULL;
kono
parents: 0
diff changeset
1767 do_wait:
kono
parents: 0
diff changeset
1768 /* All tasks we are waiting for are either running in other
kono
parents: 0
diff changeset
1769 threads, or they are tasks that have not had their
kono
parents: 0
diff changeset
1770 dependencies met (so they're not even in the queue). Wait
kono
parents: 0
diff changeset
1771 for them. */
kono
parents: 0
diff changeset
1772 taskgroup->in_taskgroup_wait = true;
kono
parents: 0
diff changeset
1773 }
kono
parents: 0
diff changeset
1774 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
1775 if (do_wake)
kono
parents: 0
diff changeset
1776 {
kono
parents: 0
diff changeset
1777 gomp_team_barrier_wake (&team->barrier, do_wake);
kono
parents: 0
diff changeset
1778 do_wake = 0;
kono
parents: 0
diff changeset
1779 }
kono
parents: 0
diff changeset
1780 if (to_free)
kono
parents: 0
diff changeset
1781 {
kono
parents: 0
diff changeset
1782 gomp_finish_task (to_free);
kono
parents: 0
diff changeset
1783 free (to_free);
kono
parents: 0
diff changeset
1784 to_free = NULL;
kono
parents: 0
diff changeset
1785 }
kono
parents: 0
diff changeset
1786 if (child_task)
kono
parents: 0
diff changeset
1787 {
kono
parents: 0
diff changeset
1788 thr->task = child_task;
kono
parents: 0
diff changeset
1789 if (__builtin_expect (child_task->fn == NULL, 0))
kono
parents: 0
diff changeset
1790 {
kono
parents: 0
diff changeset
1791 if (gomp_target_task_fn (child_task->fn_data))
kono
parents: 0
diff changeset
1792 {
kono
parents: 0
diff changeset
1793 thr->task = task;
kono
parents: 0
diff changeset
1794 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1795 child_task->kind = GOMP_TASK_ASYNC_RUNNING;
kono
parents: 0
diff changeset
1796 struct gomp_target_task *ttask
kono
parents: 0
diff changeset
1797 = (struct gomp_target_task *) child_task->fn_data;
kono
parents: 0
diff changeset
1798 /* If GOMP_PLUGIN_target_task_completion has run already
kono
parents: 0
diff changeset
1799 in between gomp_target_task_fn and the mutex lock,
kono
parents: 0
diff changeset
1800 perform the requeuing here. */
kono
parents: 0
diff changeset
1801 if (ttask->state == GOMP_TARGET_TASK_FINISHED)
kono
parents: 0
diff changeset
1802 gomp_target_task_completion (team, child_task);
kono
parents: 0
diff changeset
1803 else
kono
parents: 0
diff changeset
1804 ttask->state = GOMP_TARGET_TASK_RUNNING;
kono
parents: 0
diff changeset
1805 child_task = NULL;
kono
parents: 0
diff changeset
1806 continue;
kono
parents: 0
diff changeset
1807 }
kono
parents: 0
diff changeset
1808 }
kono
parents: 0
diff changeset
1809 else
kono
parents: 0
diff changeset
1810 child_task->fn (child_task->fn_data);
kono
parents: 0
diff changeset
1811 thr->task = task;
kono
parents: 0
diff changeset
1812 }
kono
parents: 0
diff changeset
1813 else
kono
parents: 0
diff changeset
1814 gomp_sem_wait (&taskgroup->taskgroup_sem);
kono
parents: 0
diff changeset
1815 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
1816 if (child_task)
kono
parents: 0
diff changeset
1817 {
kono
parents: 0
diff changeset
1818 finish_cancelled:;
kono
parents: 0
diff changeset
1819 size_t new_tasks
kono
parents: 0
diff changeset
1820 = gomp_task_run_post_handle_depend (child_task, team);
kono
parents: 0
diff changeset
1821 gomp_task_run_post_remove_parent (child_task);
kono
parents: 0
diff changeset
1822 gomp_clear_parent (&child_task->children_queue);
kono
parents: 0
diff changeset
1823 gomp_task_run_post_remove_taskgroup (child_task);
kono
parents: 0
diff changeset
1824 to_free = child_task;
kono
parents: 0
diff changeset
1825 child_task = NULL;
kono
parents: 0
diff changeset
1826 team->task_count--;
kono
parents: 0
diff changeset
1827 if (new_tasks > 1)
kono
parents: 0
diff changeset
1828 {
kono
parents: 0
diff changeset
1829 do_wake = team->nthreads - team->task_running_count
kono
parents: 0
diff changeset
1830 - !task->in_tied_task;
kono
parents: 0
diff changeset
1831 if (do_wake > new_tasks)
kono
parents: 0
diff changeset
1832 do_wake = new_tasks;
kono
parents: 0
diff changeset
1833 }
kono
parents: 0
diff changeset
1834 }
kono
parents: 0
diff changeset
1835 }
kono
parents: 0
diff changeset
1836
kono
parents: 0
diff changeset
1837 finish:
kono
parents: 0
diff changeset
1838 task->taskgroup = taskgroup->prev;
kono
parents: 0
diff changeset
1839 gomp_sem_destroy (&taskgroup->taskgroup_sem);
kono
parents: 0
diff changeset
1840 free (taskgroup);
kono
parents: 0
diff changeset
1841 }
kono
parents: 0
diff changeset
1842
kono
parents: 0
diff changeset
1843 int
kono
parents: 0
diff changeset
1844 omp_in_final (void)
kono
parents: 0
diff changeset
1845 {
kono
parents: 0
diff changeset
1846 struct gomp_thread *thr = gomp_thread ();
kono
parents: 0
diff changeset
1847 return thr->task && thr->task->final_task;
kono
parents: 0
diff changeset
1848 }
kono
parents: 0
diff changeset
1849
kono
parents: 0
diff changeset
1850 ialias (omp_in_final)