annotate libgomp/priority_queue.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1 /* Copyright (C) 2015-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2 Contributed by Aldy Hernandez <aldyh@redhat.com>.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of the GNU Offloading and Multi Processing Library
kono
parents:
diff changeset
5 (libgomp).
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 Libgomp is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
8 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
10 any later version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
kono
parents:
diff changeset
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kono
parents:
diff changeset
15 more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 /* Priority queue implementation of GOMP tasks. */
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 #include "libgomp.h"
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 #if _LIBGOMP_CHECKING_
kono
parents:
diff changeset
31 #include <stdio.h>
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 /* Sanity check to verify whether a TASK is in LIST. Return TRUE if
kono
parents:
diff changeset
34 found, FALSE otherwise.
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 TYPE is the type of priority queue this task resides in. */
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 static inline bool
kono
parents:
diff changeset
39 priority_queue_task_in_list_p (enum priority_queue_type type,
kono
parents:
diff changeset
40 struct priority_list *list,
kono
parents:
diff changeset
41 struct gomp_task *task)
kono
parents:
diff changeset
42 {
kono
parents:
diff changeset
43 struct priority_node *p = list->tasks;
kono
parents:
diff changeset
44 do
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 if (priority_node_to_task (type, p) == task)
kono
parents:
diff changeset
47 return true;
kono
parents:
diff changeset
48 p = p->next;
kono
parents:
diff changeset
49 }
kono
parents:
diff changeset
50 while (p != list->tasks);
kono
parents:
diff changeset
51 return false;
kono
parents:
diff changeset
52 }
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 /* Tree version of priority_queue_task_in_list_p. */
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 static inline bool
kono
parents:
diff changeset
57 priority_queue_task_in_tree_p (enum priority_queue_type type,
kono
parents:
diff changeset
58 struct priority_queue *head,
kono
parents:
diff changeset
59 struct gomp_task *task)
kono
parents:
diff changeset
60 {
kono
parents:
diff changeset
61 struct priority_list *list
kono
parents:
diff changeset
62 = priority_queue_lookup_priority (head, task->priority);
kono
parents:
diff changeset
63 if (!list)
kono
parents:
diff changeset
64 return false;
kono
parents:
diff changeset
65 return priority_queue_task_in_list_p (type, list, task);
kono
parents:
diff changeset
66 }
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 /* Generic version of priority_queue_task_in_list_p that works for
kono
parents:
diff changeset
69 trees or lists. */
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 bool
kono
parents:
diff changeset
72 priority_queue_task_in_queue_p (enum priority_queue_type type,
kono
parents:
diff changeset
73 struct priority_queue *head,
kono
parents:
diff changeset
74 struct gomp_task *task)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 if (priority_queue_empty_p (head, MEMMODEL_RELAXED))
kono
parents:
diff changeset
77 return false;
kono
parents:
diff changeset
78 if (priority_queue_multi_p (head))
kono
parents:
diff changeset
79 return priority_queue_task_in_tree_p (type, head, task);
kono
parents:
diff changeset
80 else
kono
parents:
diff changeset
81 return priority_queue_task_in_list_p (type, &head->l, task);
kono
parents:
diff changeset
82 }
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 /* Sanity check LIST to make sure the tasks therein are in the right
kono
parents:
diff changeset
85 order. LIST is a priority list of type TYPE.
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 The expected order is that GOMP_TASK_WAITING tasks come before
kono
parents:
diff changeset
88 GOMP_TASK_TIED/GOMP_TASK_ASYNC_RUNNING ones.
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 If CHECK_DEPS is TRUE, we also check that parent_depends_on WAITING
kono
parents:
diff changeset
91 tasks come before !parent_depends_on WAITING tasks. This is only
kono
parents:
diff changeset
92 applicable to the children queue, and the caller is expected to
kono
parents:
diff changeset
93 ensure that we are verifying the children queue. */
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 static void
kono
parents:
diff changeset
96 priority_list_verify (enum priority_queue_type type,
kono
parents:
diff changeset
97 struct priority_list *list, bool check_deps)
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 bool seen_tied = false;
kono
parents:
diff changeset
100 bool seen_plain_waiting = false;
kono
parents:
diff changeset
101 struct priority_node *p = list->tasks;
kono
parents:
diff changeset
102 while (1)
kono
parents:
diff changeset
103 {
kono
parents:
diff changeset
104 struct gomp_task *t = priority_node_to_task (type, p);
kono
parents:
diff changeset
105 if (seen_tied && t->kind == GOMP_TASK_WAITING)
kono
parents:
diff changeset
106 gomp_fatal ("priority_queue_verify: WAITING task after TIED");
kono
parents:
diff changeset
107 if (t->kind >= GOMP_TASK_TIED)
kono
parents:
diff changeset
108 seen_tied = true;
kono
parents:
diff changeset
109 else if (check_deps && t->kind == GOMP_TASK_WAITING)
kono
parents:
diff changeset
110 {
kono
parents:
diff changeset
111 if (t->parent_depends_on)
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 if (seen_plain_waiting)
kono
parents:
diff changeset
114 gomp_fatal ("priority_queue_verify: "
kono
parents:
diff changeset
115 "parent_depends_on after !parent_depends_on");
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117 else
kono
parents:
diff changeset
118 seen_plain_waiting = true;
kono
parents:
diff changeset
119 }
kono
parents:
diff changeset
120 p = p->next;
kono
parents:
diff changeset
121 if (p == list->tasks)
kono
parents:
diff changeset
122 break;
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 /* Callback type for priority_tree_verify_callback. */
kono
parents:
diff changeset
127 struct cbtype
kono
parents:
diff changeset
128 {
kono
parents:
diff changeset
129 enum priority_queue_type type;
kono
parents:
diff changeset
130 bool check_deps;
kono
parents:
diff changeset
131 };
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 /* Verify every task in NODE.
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 Callback for splay_tree_foreach. */
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 static void
kono
parents:
diff changeset
138 priority_tree_verify_callback (prio_splay_tree_key key, void *data)
kono
parents:
diff changeset
139 {
kono
parents:
diff changeset
140 struct cbtype *cb = (struct cbtype *) data;
kono
parents:
diff changeset
141 priority_list_verify (cb->type, &key->l, cb->check_deps);
kono
parents:
diff changeset
142 }
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 /* Generic version of priority_list_verify.
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 Sanity check HEAD to make sure the tasks therein are in the right
kono
parents:
diff changeset
147 order. The priority_queue holds tasks of type TYPE.
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 If CHECK_DEPS is TRUE, we also check that parent_depends_on WAITING
kono
parents:
diff changeset
150 tasks come before !parent_depends_on WAITING tasks. This is only
kono
parents:
diff changeset
151 applicable to the children queue, and the caller is expected to
kono
parents:
diff changeset
152 ensure that we are verifying the children queue. */
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 void
kono
parents:
diff changeset
155 priority_queue_verify (enum priority_queue_type type,
kono
parents:
diff changeset
156 struct priority_queue *head, bool check_deps)
kono
parents:
diff changeset
157 {
kono
parents:
diff changeset
158 if (priority_queue_empty_p (head, MEMMODEL_RELAXED))
kono
parents:
diff changeset
159 return;
kono
parents:
diff changeset
160 if (priority_queue_multi_p (head))
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 struct cbtype cb = { type, check_deps };
kono
parents:
diff changeset
163 prio_splay_tree_foreach (&head->t,
kono
parents:
diff changeset
164 priority_tree_verify_callback, &cb);
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166 else
kono
parents:
diff changeset
167 priority_list_verify (type, &head->l, check_deps);
kono
parents:
diff changeset
168 }
kono
parents:
diff changeset
169 #endif /* _LIBGOMP_CHECKING_ */
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 /* Remove NODE from priority queue HEAD, wherever it may be inside the
kono
parents:
diff changeset
172 tree. HEAD contains tasks of type TYPE. */
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 void
kono
parents:
diff changeset
175 priority_tree_remove (enum priority_queue_type type,
kono
parents:
diff changeset
176 struct priority_queue *head,
kono
parents:
diff changeset
177 struct priority_node *node)
kono
parents:
diff changeset
178 {
kono
parents:
diff changeset
179 /* ?? The only reason this function is not inlined is because we
kono
parents:
diff changeset
180 need to find the priority within gomp_task (which has not been
kono
parents:
diff changeset
181 completely defined in the header file). If the lack of inlining
kono
parents:
diff changeset
182 is a concern, we could pass the priority number as a
kono
parents:
diff changeset
183 parameter, or we could move this to libgomp.h. */
kono
parents:
diff changeset
184 int priority = priority_node_to_task (type, node)->priority;
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 /* ?? We could avoid this lookup by keeping a pointer to the key in
kono
parents:
diff changeset
187 the priority_node. */
kono
parents:
diff changeset
188 struct priority_list *list
kono
parents:
diff changeset
189 = priority_queue_lookup_priority (head, priority);
kono
parents:
diff changeset
190 #if _LIBGOMP_CHECKING_
kono
parents:
diff changeset
191 if (!list)
kono
parents:
diff changeset
192 gomp_fatal ("Unable to find priority %d", priority);
kono
parents:
diff changeset
193 #endif
kono
parents:
diff changeset
194 /* If NODE was the last in its priority, clean up the priority. */
kono
parents:
diff changeset
195 if (priority_list_remove (list, node, MEMMODEL_RELAXED))
kono
parents:
diff changeset
196 {
kono
parents:
diff changeset
197 prio_splay_tree_remove (&head->t, (prio_splay_tree_key) list);
kono
parents:
diff changeset
198 list->tasks = NULL;
kono
parents:
diff changeset
199 #if _LIBGOMP_CHECKING_
kono
parents:
diff changeset
200 memset (list, 0xaf, sizeof (*list));
kono
parents:
diff changeset
201 #endif
kono
parents:
diff changeset
202 free (list);
kono
parents:
diff changeset
203 }
kono
parents:
diff changeset
204 }
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 /* Return the highest priority WAITING task in a splay tree NODE. If
kono
parents:
diff changeset
207 there are no WAITING tasks available, return NULL.
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 NODE is a priority list containing tasks of type TYPE.
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 The right most node in a tree contains the highest priority.
kono
parents:
diff changeset
212 Recurse down to find such a node. If the task at that max node is
kono
parents:
diff changeset
213 not WAITING, bubble back up and look at the remaining tasks
kono
parents:
diff changeset
214 in-order. */
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 static struct gomp_task *
kono
parents:
diff changeset
217 priority_tree_next_task_1 (enum priority_queue_type type,
kono
parents:
diff changeset
218 prio_splay_tree_node node)
kono
parents:
diff changeset
219 {
kono
parents:
diff changeset
220 again:
kono
parents:
diff changeset
221 if (!node)
kono
parents:
diff changeset
222 return NULL;
kono
parents:
diff changeset
223 struct gomp_task *ret = priority_tree_next_task_1 (type, node->right);
kono
parents:
diff changeset
224 if (ret)
kono
parents:
diff changeset
225 return ret;
kono
parents:
diff changeset
226 ret = priority_node_to_task (type, node->key.l.tasks);
kono
parents:
diff changeset
227 if (ret->kind == GOMP_TASK_WAITING)
kono
parents:
diff changeset
228 return ret;
kono
parents:
diff changeset
229 node = node->left;
kono
parents:
diff changeset
230 goto again;
kono
parents:
diff changeset
231 }
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 /* Return the highest priority WAITING task from within Q1 and Q2,
kono
parents:
diff changeset
234 while giving preference to tasks from Q1. Q1 is a queue containing
kono
parents:
diff changeset
235 items of type TYPE1. Q2 is a queue containing items of type TYPE2.
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 Since we are mostly interested in Q1, if there are no WAITING tasks
kono
parents:
diff changeset
238 in Q1, we don't bother checking Q2, and just return NULL.
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 As a special case, Q2 can be NULL, in which case, we just choose
kono
parents:
diff changeset
241 the highest priority WAITING task in Q1. This is an optimization
kono
parents:
diff changeset
242 to speed up looking through only one queue.
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 If the returned task is chosen from Q1, *Q1_CHOSEN_P is set to
kono
parents:
diff changeset
245 TRUE, otherwise it is set to FALSE. */
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 struct gomp_task *
kono
parents:
diff changeset
248 priority_tree_next_task (enum priority_queue_type type1,
kono
parents:
diff changeset
249 struct priority_queue *q1,
kono
parents:
diff changeset
250 enum priority_queue_type type2,
kono
parents:
diff changeset
251 struct priority_queue *q2,
kono
parents:
diff changeset
252 bool *q1_chosen_p)
kono
parents:
diff changeset
253 {
kono
parents:
diff changeset
254 struct gomp_task *t1 = priority_tree_next_task_1 (type1, q1->t.root);
kono
parents:
diff changeset
255 if (!t1
kono
parents:
diff changeset
256 /* Special optimization when only searching through one queue. */
kono
parents:
diff changeset
257 || !q2)
kono
parents:
diff changeset
258 {
kono
parents:
diff changeset
259 *q1_chosen_p = true;
kono
parents:
diff changeset
260 return t1;
kono
parents:
diff changeset
261 }
kono
parents:
diff changeset
262 struct gomp_task *t2 = priority_tree_next_task_1 (type2, q2->t.root);
kono
parents:
diff changeset
263 if (!t2 || t1->priority > t2->priority)
kono
parents:
diff changeset
264 {
kono
parents:
diff changeset
265 *q1_chosen_p = true;
kono
parents:
diff changeset
266 return t1;
kono
parents:
diff changeset
267 }
kono
parents:
diff changeset
268 if (t2->priority > t1->priority)
kono
parents:
diff changeset
269 {
kono
parents:
diff changeset
270 *q1_chosen_p = false;
kono
parents:
diff changeset
271 return t2;
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273 /* If we get here, the priorities are the same, so we must look at
kono
parents:
diff changeset
274 parent_depends_on to make our decision. */
kono
parents:
diff changeset
275 #if _LIBGOMP_CHECKING_
kono
parents:
diff changeset
276 if (t1 != t2)
kono
parents:
diff changeset
277 gomp_fatal ("priority_tree_next_task: t1 != t2");
kono
parents:
diff changeset
278 #endif
kono
parents:
diff changeset
279 if (t2->parent_depends_on && !t1->parent_depends_on)
kono
parents:
diff changeset
280 {
kono
parents:
diff changeset
281 *q1_chosen_p = false;
kono
parents:
diff changeset
282 return t2;
kono
parents:
diff changeset
283 }
kono
parents:
diff changeset
284 *q1_chosen_p = true;
kono
parents:
diff changeset
285 return t1;
kono
parents:
diff changeset
286 }
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 /* Priority splay trees comparison function. */
kono
parents:
diff changeset
289 static inline int
kono
parents:
diff changeset
290 prio_splay_compare (prio_splay_tree_key x, prio_splay_tree_key y)
kono
parents:
diff changeset
291 {
kono
parents:
diff changeset
292 if (x->l.priority == y->l.priority)
kono
parents:
diff changeset
293 return 0;
kono
parents:
diff changeset
294 return x->l.priority < y->l.priority ? -1 : 1;
kono
parents:
diff changeset
295 }
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 /* Define another splay tree instantiation, for priority_list's. */
kono
parents:
diff changeset
298 #define splay_tree_prefix prio
kono
parents:
diff changeset
299 #define splay_tree_c
kono
parents:
diff changeset
300 #include "splay-tree.h"