1 /* Copyright (C) 2015-2017 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This file handles the taskloop construct. It is included twice, once
27 for the long and once for unsigned long long variant. */
29 /* Called when encountering an explicit task directive. If IF_CLAUSE is
30 false, then we must not delay in executing the task. If UNTIED is true,
31 then the task may be executed by any member of the team. */
34 GOMP_taskloop (void (*fn
) (void *), void *data
, void (*cpyfn
) (void *, void *),
35 long arg_size
, long arg_align
, unsigned flags
,
36 unsigned long num_tasks
, int priority
,
37 TYPE start
, TYPE end
, TYPE step
)
39 struct gomp_thread
*thr
= gomp_thread ();
40 struct gomp_team
*team
= thr
->ts
.team
;
42 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
43 /* If pthread_mutex_* is used for omp_*lock*, then each task must be
44 tied to one thread all the time. This means UNTIED tasks must be
45 tied and if CPYFN is non-NULL IF(0) must be forced, as CPYFN
46 might be running on different thread than FN. */
48 flags
&= ~GOMP_TASK_FLAG_IF
;
49 flags
&= ~GOMP_TASK_FLAG_UNTIED
;
52 /* If parallel or taskgroup has been cancelled, don't start new tasks. */
53 if (team
&& gomp_team_barrier_cancelled (&team
->barrier
))
70 UTYPE n
= (end
- start
+ s
) / step
;
73 if (flags
& GOMP_TASK_FLAG_UP
)
77 n
= (end
- start
+ step
- 1) / step
;
83 n
= (start
- end
- step
- 1) / -step
;
87 TYPE task_step
= step
;
88 unsigned long nfirst
= n
;
89 if (flags
& GOMP_TASK_FLAG_GRAINSIZE
)
91 unsigned long grainsize
= num_tasks
;
93 num_tasks
= n
/ grainsize
;
95 UTYPE ndiv
= n
/ grainsize
;
97 if (num_tasks
!= ndiv
)
103 task_step
= end
- start
;
105 else if (num_tasks
>= grainsize
111 UTYPE mul
= num_tasks
* grainsize
;
112 task_step
= (TYPE
) grainsize
* step
;
116 nfirst
= n
- mul
- 1;
121 UTYPE div
= n
/ num_tasks
;
122 UTYPE mod
= n
% num_tasks
;
123 task_step
= (TYPE
) div
* step
;
134 num_tasks
= team
? team
->nthreads
: 1;
139 UTYPE div
= n
/ num_tasks
;
140 UTYPE mod
= n
% num_tasks
;
141 task_step
= (TYPE
) div
* step
;
150 if (flags
& GOMP_TASK_FLAG_NOGROUP
)
152 if (thr
->task
&& thr
->task
->taskgroup
&& thr
->task
->taskgroup
->cancelled
)
156 ialias_call (GOMP_taskgroup_start
) ();
158 if (priority
> gomp_max_task_priority_var
)
159 priority
= gomp_max_task_priority_var
;
161 if ((flags
& GOMP_TASK_FLAG_IF
) == 0 || team
== NULL
162 || (thr
->task
&& thr
->task
->final_task
)
163 || team
->task_count
+ num_tasks
> 64 * team
->nthreads
)
166 if (__builtin_expect (cpyfn
!= NULL
, 0))
168 struct gomp_task task
[num_tasks
];
169 struct gomp_task
*parent
= thr
->task
;
170 arg_size
= (arg_size
+ arg_align
- 1) & ~(arg_align
- 1);
171 char buf
[num_tasks
* arg_size
+ arg_align
- 1];
172 char *arg
= (char *) (((uintptr_t) buf
+ arg_align
- 1)
173 & ~(uintptr_t) (arg_align
- 1));
174 char *orig_arg
= arg
;
175 for (i
= 0; i
< num_tasks
; i
++)
177 gomp_init_task (&task
[i
], parent
, gomp_icv (false));
178 task
[i
].priority
= priority
;
179 task
[i
].kind
= GOMP_TASK_UNDEFERRED
;
180 task
[i
].final_task
= (thr
->task
&& thr
->task
->final_task
)
181 || (flags
& GOMP_TASK_FLAG_FINAL
);
184 task
[i
].in_tied_task
= thr
->task
->in_tied_task
;
185 task
[i
].taskgroup
= thr
->task
->taskgroup
;
187 thr
->task
= &task
[i
];
192 for (i
= 0; i
< num_tasks
; i
++)
194 thr
->task
= &task
[i
];
195 ((TYPE
*)arg
)[0] = start
;
197 ((TYPE
*)arg
)[1] = start
;
202 if (!priority_queue_empty_p (&task
[i
].children_queue
,
205 gomp_mutex_lock (&team
->task_lock
);
206 gomp_clear_parent (&task
[i
].children_queue
);
207 gomp_mutex_unlock (&team
->task_lock
);
213 for (i
= 0; i
< num_tasks
; i
++)
215 struct gomp_task task
;
217 gomp_init_task (&task
, thr
->task
, gomp_icv (false));
218 task
.priority
= priority
;
219 task
.kind
= GOMP_TASK_UNDEFERRED
;
220 task
.final_task
= (thr
->task
&& thr
->task
->final_task
)
221 || (flags
& GOMP_TASK_FLAG_FINAL
);
224 task
.in_tied_task
= thr
->task
->in_tied_task
;
225 task
.taskgroup
= thr
->task
->taskgroup
;
228 ((TYPE
*)data
)[0] = start
;
230 ((TYPE
*)data
)[1] = start
;
234 if (!priority_queue_empty_p (&task
.children_queue
,
237 gomp_mutex_lock (&team
->task_lock
);
238 gomp_clear_parent (&task
.children_queue
);
239 gomp_mutex_unlock (&team
->task_lock
);
246 struct gomp_task
*tasks
[num_tasks
];
247 struct gomp_task
*parent
= thr
->task
;
248 struct gomp_taskgroup
*taskgroup
= parent
->taskgroup
;
253 for (i
= 0; i
< num_tasks
; i
++)
255 struct gomp_task
*task
256 = gomp_malloc (sizeof (*task
) + arg_size
+ arg_align
- 1);
258 arg
= (char *) (((uintptr_t) (task
+ 1) + arg_align
- 1)
259 & ~(uintptr_t) (arg_align
- 1));
260 gomp_init_task (task
, parent
, gomp_icv (false));
261 task
->priority
= priority
;
262 task
->kind
= GOMP_TASK_UNDEFERRED
;
263 task
->in_tied_task
= parent
->in_tied_task
;
264 task
->taskgroup
= taskgroup
;
269 task
->copy_ctors_done
= true;
272 memcpy (arg
, data
, arg_size
);
273 ((TYPE
*)arg
)[0] = start
;
275 ((TYPE
*)arg
)[1] = start
;
279 task
->kind
= GOMP_TASK_WAITING
;
282 task
->final_task
= (flags
& GOMP_TASK_FLAG_FINAL
) >> 1;
284 gomp_mutex_lock (&team
->task_lock
);
285 /* If parallel or taskgroup has been cancelled, don't start new
287 if (__builtin_expect ((gomp_team_barrier_cancelled (&team
->barrier
)
288 || (taskgroup
&& taskgroup
->cancelled
))
289 && cpyfn
== NULL
, 0))
291 gomp_mutex_unlock (&team
->task_lock
);
292 for (i
= 0; i
< num_tasks
; i
++)
294 gomp_finish_task (tasks
[i
]);
297 if ((flags
& GOMP_TASK_FLAG_NOGROUP
) == 0)
298 ialias_call (GOMP_taskgroup_end
) ();
302 taskgroup
->num_children
+= num_tasks
;
303 for (i
= 0; i
< num_tasks
; i
++)
305 struct gomp_task
*task
= tasks
[i
];
306 priority_queue_insert (PQ_CHILDREN
, &parent
->children_queue
,
308 PRIORITY_INSERT_BEGIN
,
309 /*last_parent_depends_on=*/false,
310 task
->parent_depends_on
);
312 priority_queue_insert (PQ_TASKGROUP
, &taskgroup
->taskgroup_queue
,
313 task
, priority
, PRIORITY_INSERT_BEGIN
,
314 /*last_parent_depends_on=*/false,
315 task
->parent_depends_on
);
316 priority_queue_insert (PQ_TEAM
, &team
->task_queue
, task
, priority
,
318 /*last_parent_depends_on=*/false,
319 task
->parent_depends_on
);
321 ++team
->task_queued_count
;
323 gomp_team_barrier_set_task_pending (&team
->barrier
);
324 if (team
->task_running_count
+ !parent
->in_tied_task
327 do_wake
= team
->nthreads
- team
->task_running_count
328 - !parent
->in_tied_task
;
329 if ((unsigned long) do_wake
> num_tasks
)
334 gomp_mutex_unlock (&team
->task_lock
);
336 gomp_team_barrier_wake (&team
->barrier
, do_wake
);
338 if ((flags
& GOMP_TASK_FLAG_NOGROUP
) == 0)
339 ialias_call (GOMP_taskgroup_end
) ();