task.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /* Copyright (C) 2007-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file handles the maintainence of tasks in response to task
  21. creation and termination. */
  22. #include "libgomp.h"
  23. #include <stdlib.h>
  24. #include <string.h>
  25. typedef struct gomp_task_depend_entry *hash_entry_type;
  26. static inline void *
  27. htab_alloc (size_t size)
  28. {
  29. return gomp_malloc (size);
  30. }
  31. static inline void
  32. htab_free (void *ptr)
  33. {
  34. free (ptr);
  35. }
  36. #include "hashtab.h"
  37. static inline hashval_t
  38. htab_hash (hash_entry_type element)
  39. {
  40. return hash_pointer (element->addr);
  41. }
  42. static inline bool
  43. htab_eq (hash_entry_type x, hash_entry_type y)
  44. {
  45. return x->addr == y->addr;
  46. }
  47. /* Create a new task data structure. */
  48. void
  49. gomp_init_task (struct gomp_task *task, struct gomp_task *parent_task,
  50. struct gomp_task_icv *prev_icv)
  51. {
  52. task->parent = parent_task;
  53. task->icv = *prev_icv;
  54. task->kind = GOMP_TASK_IMPLICIT;
  55. task->taskwait = NULL;
  56. task->in_tied_task = false;
  57. task->final_task = false;
  58. task->copy_ctors_done = false;
  59. task->parent_depends_on = false;
  60. task->children = NULL;
  61. task->taskgroup = NULL;
  62. task->dependers = NULL;
  63. task->depend_hash = NULL;
  64. task->depend_count = 0;
  65. }
  66. /* Clean up a task, after completing it. */
  67. void
  68. gomp_end_task (void)
  69. {
  70. struct gomp_thread *thr = gomp_thread ();
  71. struct gomp_task *task = thr->task;
  72. gomp_finish_task (task);
  73. thr->task = task->parent;
  74. }
  75. static inline void
  76. gomp_clear_parent (struct gomp_task *children)
  77. {
  78. struct gomp_task *task = children;
  79. if (task)
  80. do
  81. {
  82. task->parent = NULL;
  83. task = task->next_child;
  84. }
  85. while (task != children);
  86. }
  87. static void gomp_task_maybe_wait_for_dependencies (void **depend);
  88. /* Called when encountering an explicit task directive. If IF_CLAUSE is
  89. false, then we must not delay in executing the task. If UNTIED is true,
  90. then the task may be executed by any member of the team. */
  91. void
  92. GOMP_task (void (*fn) (void *), void *data, void (*cpyfn) (void *, void *),
  93. long arg_size, long arg_align, bool if_clause, unsigned flags,
  94. void **depend)
  95. {
  96. struct gomp_thread *thr = gomp_thread ();
  97. struct gomp_team *team = thr->ts.team;
  98. #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
  99. /* If pthread_mutex_* is used for omp_*lock*, then each task must be
  100. tied to one thread all the time. This means UNTIED tasks must be
  101. tied and if CPYFN is non-NULL IF(0) must be forced, as CPYFN
  102. might be running on different thread than FN. */
  103. if (cpyfn)
  104. if_clause = false;
  105. if (flags & 1)
  106. flags &= ~1;
  107. #endif
  108. /* If parallel or taskgroup has been cancelled, don't start new tasks. */
  109. if (team
  110. && (gomp_team_barrier_cancelled (&team->barrier)
  111. || (thr->task->taskgroup && thr->task->taskgroup->cancelled)))
  112. return;
  113. if (!if_clause || team == NULL
  114. || (thr->task && thr->task->final_task)
  115. || team->task_count > 64 * team->nthreads)
  116. {
  117. struct gomp_task task;
  118. /* If there are depend clauses and earlier deferred sibling tasks
  119. with depend clauses, check if there isn't a dependency. If there
  120. is, we need to wait for them. There is no need to handle
  121. depend clauses for non-deferred tasks other than this, because
  122. the parent task is suspended until the child task finishes and thus
  123. it can't start further child tasks. */
  124. if ((flags & 8) && thr->task && thr->task->depend_hash)
  125. gomp_task_maybe_wait_for_dependencies (depend);
  126. gomp_init_task (&task, thr->task, gomp_icv (false));
  127. task.kind = GOMP_TASK_IFFALSE;
  128. task.final_task = (thr->task && thr->task->final_task) || (flags & 2);
  129. if (thr->task)
  130. {
  131. task.in_tied_task = thr->task->in_tied_task;
  132. task.taskgroup = thr->task->taskgroup;
  133. }
  134. thr->task = &task;
  135. if (__builtin_expect (cpyfn != NULL, 0))
  136. {
  137. char buf[arg_size + arg_align - 1];
  138. char *arg = (char *) (((uintptr_t) buf + arg_align - 1)
  139. & ~(uintptr_t) (arg_align - 1));
  140. cpyfn (arg, data);
  141. fn (arg);
  142. }
  143. else
  144. fn (data);
  145. /* Access to "children" is normally done inside a task_lock
  146. mutex region, but the only way this particular task.children
  147. can be set is if this thread's task work function (fn)
  148. creates children. So since the setter is *this* thread, we
  149. need no barriers here when testing for non-NULL. We can have
  150. task.children set by the current thread then changed by a
  151. child thread, but seeing a stale non-NULL value is not a
  152. problem. Once past the task_lock acquisition, this thread
  153. will see the real value of task.children. */
  154. if (task.children != NULL)
  155. {
  156. gomp_mutex_lock (&team->task_lock);
  157. gomp_clear_parent (task.children);
  158. gomp_mutex_unlock (&team->task_lock);
  159. }
  160. gomp_end_task ();
  161. }
  162. else
  163. {
  164. struct gomp_task *task;
  165. struct gomp_task *parent = thr->task;
  166. struct gomp_taskgroup *taskgroup = parent->taskgroup;
  167. char *arg;
  168. bool do_wake;
  169. size_t depend_size = 0;
  170. if (flags & 8)
  171. depend_size = ((uintptr_t) depend[0]
  172. * sizeof (struct gomp_task_depend_entry));
  173. task = gomp_malloc (sizeof (*task) + depend_size
  174. + arg_size + arg_align - 1);
  175. arg = (char *) (((uintptr_t) (task + 1) + depend_size + arg_align - 1)
  176. & ~(uintptr_t) (arg_align - 1));
  177. gomp_init_task (task, parent, gomp_icv (false));
  178. task->kind = GOMP_TASK_IFFALSE;
  179. task->in_tied_task = parent->in_tied_task;
  180. task->taskgroup = taskgroup;
  181. thr->task = task;
  182. if (cpyfn)
  183. {
  184. cpyfn (arg, data);
  185. task->copy_ctors_done = true;
  186. }
  187. else
  188. memcpy (arg, data, arg_size);
  189. thr->task = parent;
  190. task->kind = GOMP_TASK_WAITING;
  191. task->fn = fn;
  192. task->fn_data = arg;
  193. task->final_task = (flags & 2) >> 1;
  194. gomp_mutex_lock (&team->task_lock);
  195. /* If parallel or taskgroup has been cancelled, don't start new
  196. tasks. */
  197. if (__builtin_expect ((gomp_team_barrier_cancelled (&team->barrier)
  198. || (taskgroup && taskgroup->cancelled))
  199. && !task->copy_ctors_done, 0))
  200. {
  201. gomp_mutex_unlock (&team->task_lock);
  202. gomp_finish_task (task);
  203. free (task);
  204. return;
  205. }
  206. if (taskgroup)
  207. taskgroup->num_children++;
  208. if (depend_size)
  209. {
  210. size_t ndepend = (uintptr_t) depend[0];
  211. size_t nout = (uintptr_t) depend[1];
  212. size_t i;
  213. hash_entry_type ent;
  214. task->depend_count = ndepend;
  215. task->num_dependees = 0;
  216. if (parent->depend_hash == NULL)
  217. parent->depend_hash
  218. = htab_create (2 * ndepend > 12 ? 2 * ndepend : 12);
  219. for (i = 0; i < ndepend; i++)
  220. {
  221. task->depend[i].addr = depend[2 + i];
  222. task->depend[i].next = NULL;
  223. task->depend[i].prev = NULL;
  224. task->depend[i].task = task;
  225. task->depend[i].is_in = i >= nout;
  226. task->depend[i].redundant = false;
  227. task->depend[i].redundant_out = false;
  228. hash_entry_type *slot
  229. = htab_find_slot (&parent->depend_hash, &task->depend[i],
  230. INSERT);
  231. hash_entry_type out = NULL, last = NULL;
  232. if (*slot)
  233. {
  234. /* If multiple depends on the same task are the
  235. same, all but the first one are redundant.
  236. As inout/out come first, if any of them is
  237. inout/out, it will win, which is the right
  238. semantics. */
  239. if ((*slot)->task == task)
  240. {
  241. task->depend[i].redundant = true;
  242. continue;
  243. }
  244. for (ent = *slot; ent; ent = ent->next)
  245. {
  246. if (ent->redundant_out)
  247. break;
  248. last = ent;
  249. /* depend(in:...) doesn't depend on earlier
  250. depend(in:...). */
  251. if (i >= nout && ent->is_in)
  252. continue;
  253. if (!ent->is_in)
  254. out = ent;
  255. struct gomp_task *tsk = ent->task;
  256. if (tsk->dependers == NULL)
  257. {
  258. tsk->dependers
  259. = gomp_malloc (sizeof (struct gomp_dependers_vec)
  260. + 6 * sizeof (struct gomp_task *));
  261. tsk->dependers->n_elem = 1;
  262. tsk->dependers->allocated = 6;
  263. tsk->dependers->elem[0] = task;
  264. task->num_dependees++;
  265. continue;
  266. }
  267. /* We already have some other dependency on tsk
  268. from earlier depend clause. */
  269. else if (tsk->dependers->n_elem
  270. && (tsk->dependers->elem[tsk->dependers->n_elem
  271. - 1]
  272. == task))
  273. continue;
  274. else if (tsk->dependers->n_elem
  275. == tsk->dependers->allocated)
  276. {
  277. tsk->dependers->allocated
  278. = tsk->dependers->allocated * 2 + 2;
  279. tsk->dependers
  280. = gomp_realloc (tsk->dependers,
  281. sizeof (struct gomp_dependers_vec)
  282. + (tsk->dependers->allocated
  283. * sizeof (struct gomp_task *)));
  284. }
  285. tsk->dependers->elem[tsk->dependers->n_elem++] = task;
  286. task->num_dependees++;
  287. }
  288. task->depend[i].next = *slot;
  289. (*slot)->prev = &task->depend[i];
  290. }
  291. *slot = &task->depend[i];
  292. /* There is no need to store more than one depend({,in}out:)
  293. task per address in the hash table chain for the purpose
  294. of creation of deferred tasks, because each out
  295. depends on all earlier outs, thus it is enough to record
  296. just the last depend({,in}out:). For depend(in:), we need
  297. to keep all of the previous ones not terminated yet, because
  298. a later depend({,in}out:) might need to depend on all of
  299. them. So, if the new task's clause is depend({,in}out:),
  300. we know there is at most one other depend({,in}out:) clause
  301. in the list (out). For non-deferred tasks we want to see
  302. all outs, so they are moved to the end of the chain,
  303. after first redundant_out entry all following entries
  304. should be redundant_out. */
  305. if (!task->depend[i].is_in && out)
  306. {
  307. if (out != last)
  308. {
  309. out->next->prev = out->prev;
  310. out->prev->next = out->next;
  311. out->next = last->next;
  312. out->prev = last;
  313. last->next = out;
  314. if (out->next)
  315. out->next->prev = out;
  316. }
  317. out->redundant_out = true;
  318. }
  319. }
  320. if (task->num_dependees)
  321. {
  322. gomp_mutex_unlock (&team->task_lock);
  323. return;
  324. }
  325. }
  326. if (parent->children)
  327. {
  328. task->next_child = parent->children;
  329. task->prev_child = parent->children->prev_child;
  330. task->next_child->prev_child = task;
  331. task->prev_child->next_child = task;
  332. }
  333. else
  334. {
  335. task->next_child = task;
  336. task->prev_child = task;
  337. }
  338. parent->children = task;
  339. if (taskgroup)
  340. {
  341. if (taskgroup->children)
  342. {
  343. task->next_taskgroup = taskgroup->children;
  344. task->prev_taskgroup = taskgroup->children->prev_taskgroup;
  345. task->next_taskgroup->prev_taskgroup = task;
  346. task->prev_taskgroup->next_taskgroup = task;
  347. }
  348. else
  349. {
  350. task->next_taskgroup = task;
  351. task->prev_taskgroup = task;
  352. }
  353. taskgroup->children = task;
  354. }
  355. if (team->task_queue)
  356. {
  357. task->next_queue = team->task_queue;
  358. task->prev_queue = team->task_queue->prev_queue;
  359. task->next_queue->prev_queue = task;
  360. task->prev_queue->next_queue = task;
  361. }
  362. else
  363. {
  364. task->next_queue = task;
  365. task->prev_queue = task;
  366. team->task_queue = task;
  367. }
  368. ++team->task_count;
  369. ++team->task_queued_count;
  370. gomp_team_barrier_set_task_pending (&team->barrier);
  371. do_wake = team->task_running_count + !parent->in_tied_task
  372. < team->nthreads;
  373. gomp_mutex_unlock (&team->task_lock);
  374. if (do_wake)
  375. gomp_team_barrier_wake (&team->barrier, 1);
  376. }
  377. }
  378. static inline bool
  379. gomp_task_run_pre (struct gomp_task *child_task, struct gomp_task *parent,
  380. struct gomp_taskgroup *taskgroup, struct gomp_team *team)
  381. {
  382. if (parent)
  383. {
  384. if (parent->children == child_task)
  385. parent->children = child_task->next_child;
  386. if (__builtin_expect (child_task->parent_depends_on, 0)
  387. && parent->taskwait->last_parent_depends_on == child_task)
  388. {
  389. if (child_task->prev_child->kind == GOMP_TASK_WAITING
  390. && child_task->prev_child->parent_depends_on)
  391. parent->taskwait->last_parent_depends_on = child_task->prev_child;
  392. else
  393. parent->taskwait->last_parent_depends_on = NULL;
  394. }
  395. }
  396. if (taskgroup && taskgroup->children == child_task)
  397. taskgroup->children = child_task->next_taskgroup;
  398. child_task->prev_queue->next_queue = child_task->next_queue;
  399. child_task->next_queue->prev_queue = child_task->prev_queue;
  400. if (team->task_queue == child_task)
  401. {
  402. if (child_task->next_queue != child_task)
  403. team->task_queue = child_task->next_queue;
  404. else
  405. team->task_queue = NULL;
  406. }
  407. child_task->kind = GOMP_TASK_TIED;
  408. if (--team->task_queued_count == 0)
  409. gomp_team_barrier_clear_task_pending (&team->barrier);
  410. if ((gomp_team_barrier_cancelled (&team->barrier)
  411. || (taskgroup && taskgroup->cancelled))
  412. && !child_task->copy_ctors_done)
  413. return true;
  414. return false;
  415. }
  416. static void
  417. gomp_task_run_post_handle_depend_hash (struct gomp_task *child_task)
  418. {
  419. struct gomp_task *parent = child_task->parent;
  420. size_t i;
  421. for (i = 0; i < child_task->depend_count; i++)
  422. if (!child_task->depend[i].redundant)
  423. {
  424. if (child_task->depend[i].next)
  425. child_task->depend[i].next->prev = child_task->depend[i].prev;
  426. if (child_task->depend[i].prev)
  427. child_task->depend[i].prev->next = child_task->depend[i].next;
  428. else
  429. {
  430. hash_entry_type *slot
  431. = htab_find_slot (&parent->depend_hash, &child_task->depend[i],
  432. NO_INSERT);
  433. if (*slot != &child_task->depend[i])
  434. abort ();
  435. if (child_task->depend[i].next)
  436. *slot = child_task->depend[i].next;
  437. else
  438. htab_clear_slot (parent->depend_hash, slot);
  439. }
  440. }
  441. }
  442. static size_t
  443. gomp_task_run_post_handle_dependers (struct gomp_task *child_task,
  444. struct gomp_team *team)
  445. {
  446. struct gomp_task *parent = child_task->parent;
  447. size_t i, count = child_task->dependers->n_elem, ret = 0;
  448. for (i = 0; i < count; i++)
  449. {
  450. struct gomp_task *task = child_task->dependers->elem[i];
  451. if (--task->num_dependees != 0)
  452. continue;
  453. struct gomp_taskgroup *taskgroup = task->taskgroup;
  454. if (parent)
  455. {
  456. if (parent->children)
  457. {
  458. /* If parent is in gomp_task_maybe_wait_for_dependencies
  459. and it doesn't need to wait for this task, put it after
  460. all ready to run tasks it needs to wait for. */
  461. if (parent->taskwait && parent->taskwait->last_parent_depends_on
  462. && !task->parent_depends_on)
  463. {
  464. struct gomp_task *last_parent_depends_on
  465. = parent->taskwait->last_parent_depends_on;
  466. task->next_child = last_parent_depends_on->next_child;
  467. task->prev_child = last_parent_depends_on;
  468. }
  469. else
  470. {
  471. task->next_child = parent->children;
  472. task->prev_child = parent->children->prev_child;
  473. parent->children = task;
  474. }
  475. task->next_child->prev_child = task;
  476. task->prev_child->next_child = task;
  477. }
  478. else
  479. {
  480. task->next_child = task;
  481. task->prev_child = task;
  482. parent->children = task;
  483. }
  484. if (parent->taskwait)
  485. {
  486. if (parent->taskwait->in_taskwait)
  487. {
  488. parent->taskwait->in_taskwait = false;
  489. gomp_sem_post (&parent->taskwait->taskwait_sem);
  490. }
  491. else if (parent->taskwait->in_depend_wait)
  492. {
  493. parent->taskwait->in_depend_wait = false;
  494. gomp_sem_post (&parent->taskwait->taskwait_sem);
  495. }
  496. if (parent->taskwait->last_parent_depends_on == NULL
  497. && task->parent_depends_on)
  498. parent->taskwait->last_parent_depends_on = task;
  499. }
  500. }
  501. if (taskgroup)
  502. {
  503. if (taskgroup->children)
  504. {
  505. task->next_taskgroup = taskgroup->children;
  506. task->prev_taskgroup = taskgroup->children->prev_taskgroup;
  507. task->next_taskgroup->prev_taskgroup = task;
  508. task->prev_taskgroup->next_taskgroup = task;
  509. }
  510. else
  511. {
  512. task->next_taskgroup = task;
  513. task->prev_taskgroup = task;
  514. }
  515. taskgroup->children = task;
  516. if (taskgroup->in_taskgroup_wait)
  517. {
  518. taskgroup->in_taskgroup_wait = false;
  519. gomp_sem_post (&taskgroup->taskgroup_sem);
  520. }
  521. }
  522. if (team->task_queue)
  523. {
  524. task->next_queue = team->task_queue;
  525. task->prev_queue = team->task_queue->prev_queue;
  526. task->next_queue->prev_queue = task;
  527. task->prev_queue->next_queue = task;
  528. }
  529. else
  530. {
  531. task->next_queue = task;
  532. task->prev_queue = task;
  533. team->task_queue = task;
  534. }
  535. ++team->task_count;
  536. ++team->task_queued_count;
  537. ++ret;
  538. }
  539. free (child_task->dependers);
  540. child_task->dependers = NULL;
  541. if (ret > 1)
  542. gomp_team_barrier_set_task_pending (&team->barrier);
  543. return ret;
  544. }
  545. static inline size_t
  546. gomp_task_run_post_handle_depend (struct gomp_task *child_task,
  547. struct gomp_team *team)
  548. {
  549. if (child_task->depend_count == 0)
  550. return 0;
  551. /* If parent is gone already, the hash table is freed and nothing
  552. will use the hash table anymore, no need to remove anything from it. */
  553. if (child_task->parent != NULL)
  554. gomp_task_run_post_handle_depend_hash (child_task);
  555. if (child_task->dependers == NULL)
  556. return 0;
  557. return gomp_task_run_post_handle_dependers (child_task, team);
  558. }
  559. static inline void
  560. gomp_task_run_post_remove_parent (struct gomp_task *child_task)
  561. {
  562. struct gomp_task *parent = child_task->parent;
  563. if (parent == NULL)
  564. return;
  565. if (__builtin_expect (child_task->parent_depends_on, 0)
  566. && --parent->taskwait->n_depend == 0
  567. && parent->taskwait->in_depend_wait)
  568. {
  569. parent->taskwait->in_depend_wait = false;
  570. gomp_sem_post (&parent->taskwait->taskwait_sem);
  571. }
  572. child_task->prev_child->next_child = child_task->next_child;
  573. child_task->next_child->prev_child = child_task->prev_child;
  574. if (parent->children != child_task)
  575. return;
  576. if (child_task->next_child != child_task)
  577. parent->children = child_task->next_child;
  578. else
  579. {
  580. /* We access task->children in GOMP_taskwait
  581. outside of the task lock mutex region, so
  582. need a release barrier here to ensure memory
  583. written by child_task->fn above is flushed
  584. before the NULL is written. */
  585. __atomic_store_n (&parent->children, NULL, MEMMODEL_RELEASE);
  586. if (parent->taskwait && parent->taskwait->in_taskwait)
  587. {
  588. parent->taskwait->in_taskwait = false;
  589. gomp_sem_post (&parent->taskwait->taskwait_sem);
  590. }
  591. }
  592. }
  593. static inline void
  594. gomp_task_run_post_remove_taskgroup (struct gomp_task *child_task)
  595. {
  596. struct gomp_taskgroup *taskgroup = child_task->taskgroup;
  597. if (taskgroup == NULL)
  598. return;
  599. child_task->prev_taskgroup->next_taskgroup = child_task->next_taskgroup;
  600. child_task->next_taskgroup->prev_taskgroup = child_task->prev_taskgroup;
  601. if (taskgroup->num_children > 1)
  602. --taskgroup->num_children;
  603. else
  604. {
  605. /* We access taskgroup->num_children in GOMP_taskgroup_end
  606. outside of the task lock mutex region, so
  607. need a release barrier here to ensure memory
  608. written by child_task->fn above is flushed
  609. before the NULL is written. */
  610. __atomic_store_n (&taskgroup->num_children, 0, MEMMODEL_RELEASE);
  611. }
  612. if (taskgroup->children != child_task)
  613. return;
  614. if (child_task->next_taskgroup != child_task)
  615. taskgroup->children = child_task->next_taskgroup;
  616. else
  617. {
  618. taskgroup->children = NULL;
  619. if (taskgroup->in_taskgroup_wait)
  620. {
  621. taskgroup->in_taskgroup_wait = false;
  622. gomp_sem_post (&taskgroup->taskgroup_sem);
  623. }
  624. }
  625. }
  626. void
  627. gomp_barrier_handle_tasks (gomp_barrier_state_t state)
  628. {
  629. struct gomp_thread *thr = gomp_thread ();
  630. struct gomp_team *team = thr->ts.team;
  631. struct gomp_task *task = thr->task;
  632. struct gomp_task *child_task = NULL;
  633. struct gomp_task *to_free = NULL;
  634. int do_wake = 0;
  635. gomp_mutex_lock (&team->task_lock);
  636. if (gomp_barrier_last_thread (state))
  637. {
  638. if (team->task_count == 0)
  639. {
  640. gomp_team_barrier_done (&team->barrier, state);
  641. gomp_mutex_unlock (&team->task_lock);
  642. gomp_team_barrier_wake (&team->barrier, 0);
  643. return;
  644. }
  645. gomp_team_barrier_set_waiting_for_tasks (&team->barrier);
  646. }
  647. while (1)
  648. {
  649. bool cancelled = false;
  650. if (team->task_queue != NULL)
  651. {
  652. child_task = team->task_queue;
  653. cancelled = gomp_task_run_pre (child_task, child_task->parent,
  654. child_task->taskgroup, team);
  655. if (__builtin_expect (cancelled, 0))
  656. {
  657. if (to_free)
  658. {
  659. gomp_finish_task (to_free);
  660. free (to_free);
  661. to_free = NULL;
  662. }
  663. goto finish_cancelled;
  664. }
  665. team->task_running_count++;
  666. child_task->in_tied_task = true;
  667. }
  668. gomp_mutex_unlock (&team->task_lock);
  669. if (do_wake)
  670. {
  671. gomp_team_barrier_wake (&team->barrier, do_wake);
  672. do_wake = 0;
  673. }
  674. if (to_free)
  675. {
  676. gomp_finish_task (to_free);
  677. free (to_free);
  678. to_free = NULL;
  679. }
  680. if (child_task)
  681. {
  682. thr->task = child_task;
  683. child_task->fn (child_task->fn_data);
  684. thr->task = task;
  685. }
  686. else
  687. return;
  688. gomp_mutex_lock (&team->task_lock);
  689. if (child_task)
  690. {
  691. finish_cancelled:;
  692. size_t new_tasks
  693. = gomp_task_run_post_handle_depend (child_task, team);
  694. gomp_task_run_post_remove_parent (child_task);
  695. gomp_clear_parent (child_task->children);
  696. gomp_task_run_post_remove_taskgroup (child_task);
  697. to_free = child_task;
  698. child_task = NULL;
  699. if (!cancelled)
  700. team->task_running_count--;
  701. if (new_tasks > 1)
  702. {
  703. do_wake = team->nthreads - team->task_running_count;
  704. if (do_wake > new_tasks)
  705. do_wake = new_tasks;
  706. }
  707. if (--team->task_count == 0
  708. && gomp_team_barrier_waiting_for_tasks (&team->barrier))
  709. {
  710. gomp_team_barrier_done (&team->barrier, state);
  711. gomp_mutex_unlock (&team->task_lock);
  712. gomp_team_barrier_wake (&team->barrier, 0);
  713. gomp_mutex_lock (&team->task_lock);
  714. }
  715. }
  716. }
  717. }
  718. /* Called when encountering a taskwait directive. */
  719. void
  720. GOMP_taskwait (void)
  721. {
  722. struct gomp_thread *thr = gomp_thread ();
  723. struct gomp_team *team = thr->ts.team;
  724. struct gomp_task *task = thr->task;
  725. struct gomp_task *child_task = NULL;
  726. struct gomp_task *to_free = NULL;
  727. struct gomp_taskwait taskwait;
  728. int do_wake = 0;
  729. /* The acquire barrier on load of task->children here synchronizes
  730. with the write of a NULL in gomp_task_run_post_remove_parent. It is
  731. not necessary that we synchronize with other non-NULL writes at
  732. this point, but we must ensure that all writes to memory by a
  733. child thread task work function are seen before we exit from
  734. GOMP_taskwait. */
  735. if (task == NULL
  736. || __atomic_load_n (&task->children, MEMMODEL_ACQUIRE) == NULL)
  737. return;
  738. memset (&taskwait, 0, sizeof (taskwait));
  739. gomp_mutex_lock (&team->task_lock);
  740. while (1)
  741. {
  742. bool cancelled = false;
  743. if (task->children == NULL)
  744. {
  745. bool destroy_taskwait = task->taskwait != NULL;
  746. task->taskwait = NULL;
  747. gomp_mutex_unlock (&team->task_lock);
  748. if (to_free)
  749. {
  750. gomp_finish_task (to_free);
  751. free (to_free);
  752. }
  753. if (destroy_taskwait)
  754. gomp_sem_destroy (&taskwait.taskwait_sem);
  755. return;
  756. }
  757. if (task->children->kind == GOMP_TASK_WAITING)
  758. {
  759. child_task = task->children;
  760. cancelled
  761. = gomp_task_run_pre (child_task, task, child_task->taskgroup,
  762. team);
  763. if (__builtin_expect (cancelled, 0))
  764. {
  765. if (to_free)
  766. {
  767. gomp_finish_task (to_free);
  768. free (to_free);
  769. to_free = NULL;
  770. }
  771. goto finish_cancelled;
  772. }
  773. }
  774. else
  775. {
  776. /* All tasks we are waiting for are already running
  777. in other threads. Wait for them. */
  778. if (task->taskwait == NULL)
  779. {
  780. taskwait.in_depend_wait = false;
  781. gomp_sem_init (&taskwait.taskwait_sem, 0);
  782. task->taskwait = &taskwait;
  783. }
  784. taskwait.in_taskwait = true;
  785. }
  786. gomp_mutex_unlock (&team->task_lock);
  787. if (do_wake)
  788. {
  789. gomp_team_barrier_wake (&team->barrier, do_wake);
  790. do_wake = 0;
  791. }
  792. if (to_free)
  793. {
  794. gomp_finish_task (to_free);
  795. free (to_free);
  796. to_free = NULL;
  797. }
  798. if (child_task)
  799. {
  800. thr->task = child_task;
  801. child_task->fn (child_task->fn_data);
  802. thr->task = task;
  803. }
  804. else
  805. gomp_sem_wait (&taskwait.taskwait_sem);
  806. gomp_mutex_lock (&team->task_lock);
  807. if (child_task)
  808. {
  809. finish_cancelled:;
  810. size_t new_tasks
  811. = gomp_task_run_post_handle_depend (child_task, team);
  812. child_task->prev_child->next_child = child_task->next_child;
  813. child_task->next_child->prev_child = child_task->prev_child;
  814. if (task->children == child_task)
  815. {
  816. if (child_task->next_child != child_task)
  817. task->children = child_task->next_child;
  818. else
  819. task->children = NULL;
  820. }
  821. gomp_clear_parent (child_task->children);
  822. gomp_task_run_post_remove_taskgroup (child_task);
  823. to_free = child_task;
  824. child_task = NULL;
  825. team->task_count--;
  826. if (new_tasks > 1)
  827. {
  828. do_wake = team->nthreads - team->task_running_count
  829. - !task->in_tied_task;
  830. if (do_wake > new_tasks)
  831. do_wake = new_tasks;
  832. }
  833. }
  834. }
  835. }
  836. /* This is like GOMP_taskwait, but we only wait for tasks that the
  837. upcoming task depends on. */
  838. static void
  839. gomp_task_maybe_wait_for_dependencies (void **depend)
  840. {
  841. struct gomp_thread *thr = gomp_thread ();
  842. struct gomp_task *task = thr->task;
  843. struct gomp_team *team = thr->ts.team;
  844. struct gomp_task_depend_entry elem, *ent = NULL;
  845. struct gomp_taskwait taskwait;
  846. struct gomp_task *last_parent_depends_on = NULL;
  847. size_t ndepend = (uintptr_t) depend[0];
  848. size_t nout = (uintptr_t) depend[1];
  849. size_t i;
  850. size_t num_awaited = 0;
  851. struct gomp_task *child_task = NULL;
  852. struct gomp_task *to_free = NULL;
  853. int do_wake = 0;
  854. gomp_mutex_lock (&team->task_lock);
  855. for (i = 0; i < ndepend; i++)
  856. {
  857. elem.addr = depend[i + 2];
  858. ent = htab_find (task->depend_hash, &elem);
  859. for (; ent; ent = ent->next)
  860. if (i >= nout && ent->is_in)
  861. continue;
  862. else
  863. {
  864. struct gomp_task *tsk = ent->task;
  865. if (!tsk->parent_depends_on)
  866. {
  867. tsk->parent_depends_on = true;
  868. ++num_awaited;
  869. if (tsk->num_dependees == 0 && tsk->kind == GOMP_TASK_WAITING)
  870. {
  871. /* If a task we need to wait for is not already
  872. running and is ready to be scheduled, move it
  873. to front, so that we run it as soon as possible. */
  874. if (last_parent_depends_on)
  875. {
  876. tsk->prev_child->next_child = tsk->next_child;
  877. tsk->next_child->prev_child = tsk->prev_child;
  878. tsk->prev_child = last_parent_depends_on;
  879. tsk->next_child = last_parent_depends_on->next_child;
  880. tsk->prev_child->next_child = tsk;
  881. tsk->next_child->prev_child = tsk;
  882. }
  883. else if (tsk != task->children)
  884. {
  885. tsk->prev_child->next_child = tsk->next_child;
  886. tsk->next_child->prev_child = tsk->prev_child;
  887. tsk->prev_child = task->children;
  888. tsk->next_child = task->children->next_child;
  889. task->children = tsk;
  890. tsk->prev_child->next_child = tsk;
  891. tsk->next_child->prev_child = tsk;
  892. }
  893. last_parent_depends_on = tsk;
  894. }
  895. }
  896. }
  897. }
  898. if (num_awaited == 0)
  899. {
  900. gomp_mutex_unlock (&team->task_lock);
  901. return;
  902. }
  903. memset (&taskwait, 0, sizeof (taskwait));
  904. taskwait.n_depend = num_awaited;
  905. taskwait.last_parent_depends_on = last_parent_depends_on;
  906. gomp_sem_init (&taskwait.taskwait_sem, 0);
  907. task->taskwait = &taskwait;
  908. while (1)
  909. {
  910. bool cancelled = false;
  911. if (taskwait.n_depend == 0)
  912. {
  913. task->taskwait = NULL;
  914. gomp_mutex_unlock (&team->task_lock);
  915. if (to_free)
  916. {
  917. gomp_finish_task (to_free);
  918. free (to_free);
  919. }
  920. gomp_sem_destroy (&taskwait.taskwait_sem);
  921. return;
  922. }
  923. if (task->children->kind == GOMP_TASK_WAITING)
  924. {
  925. child_task = task->children;
  926. cancelled
  927. = gomp_task_run_pre (child_task, task, child_task->taskgroup,
  928. team);
  929. if (__builtin_expect (cancelled, 0))
  930. {
  931. if (to_free)
  932. {
  933. gomp_finish_task (to_free);
  934. free (to_free);
  935. to_free = NULL;
  936. }
  937. goto finish_cancelled;
  938. }
  939. }
  940. else
  941. /* All tasks we are waiting for are already running
  942. in other threads. Wait for them. */
  943. taskwait.in_depend_wait = true;
  944. gomp_mutex_unlock (&team->task_lock);
  945. if (do_wake)
  946. {
  947. gomp_team_barrier_wake (&team->barrier, do_wake);
  948. do_wake = 0;
  949. }
  950. if (to_free)
  951. {
  952. gomp_finish_task (to_free);
  953. free (to_free);
  954. to_free = NULL;
  955. }
  956. if (child_task)
  957. {
  958. thr->task = child_task;
  959. child_task->fn (child_task->fn_data);
  960. thr->task = task;
  961. }
  962. else
  963. gomp_sem_wait (&taskwait.taskwait_sem);
  964. gomp_mutex_lock (&team->task_lock);
  965. if (child_task)
  966. {
  967. finish_cancelled:;
  968. size_t new_tasks
  969. = gomp_task_run_post_handle_depend (child_task, team);
  970. if (child_task->parent_depends_on)
  971. --taskwait.n_depend;
  972. child_task->prev_child->next_child = child_task->next_child;
  973. child_task->next_child->prev_child = child_task->prev_child;
  974. if (task->children == child_task)
  975. {
  976. if (child_task->next_child != child_task)
  977. task->children = child_task->next_child;
  978. else
  979. task->children = NULL;
  980. }
  981. gomp_clear_parent (child_task->children);
  982. gomp_task_run_post_remove_taskgroup (child_task);
  983. to_free = child_task;
  984. child_task = NULL;
  985. team->task_count--;
  986. if (new_tasks > 1)
  987. {
  988. do_wake = team->nthreads - team->task_running_count
  989. - !task->in_tied_task;
  990. if (do_wake > new_tasks)
  991. do_wake = new_tasks;
  992. }
  993. }
  994. }
  995. }
  996. /* Called when encountering a taskyield directive. */
  997. void
  998. GOMP_taskyield (void)
  999. {
  1000. /* Nothing at the moment. */
  1001. }
  1002. void
  1003. GOMP_taskgroup_start (void)
  1004. {
  1005. struct gomp_thread *thr = gomp_thread ();
  1006. struct gomp_team *team = thr->ts.team;
  1007. struct gomp_task *task = thr->task;
  1008. struct gomp_taskgroup *taskgroup;
  1009. /* If team is NULL, all tasks are executed as
  1010. GOMP_TASK_IFFALSE tasks and thus all children tasks of
  1011. taskgroup and their descendant tasks will be finished
  1012. by the time GOMP_taskgroup_end is called. */
  1013. if (team == NULL)
  1014. return;
  1015. taskgroup = gomp_malloc (sizeof (struct gomp_taskgroup));
  1016. taskgroup->prev = task->taskgroup;
  1017. taskgroup->children = NULL;
  1018. taskgroup->in_taskgroup_wait = false;
  1019. taskgroup->cancelled = false;
  1020. taskgroup->num_children = 0;
  1021. gomp_sem_init (&taskgroup->taskgroup_sem, 0);
  1022. task->taskgroup = taskgroup;
  1023. }
  1024. void
  1025. GOMP_taskgroup_end (void)
  1026. {
  1027. struct gomp_thread *thr = gomp_thread ();
  1028. struct gomp_team *team = thr->ts.team;
  1029. struct gomp_task *task = thr->task;
  1030. struct gomp_taskgroup *taskgroup;
  1031. struct gomp_task *child_task = NULL;
  1032. struct gomp_task *to_free = NULL;
  1033. int do_wake = 0;
  1034. if (team == NULL)
  1035. return;
  1036. taskgroup = task->taskgroup;
  1037. /* The acquire barrier on load of taskgroup->num_children here
  1038. synchronizes with the write of 0 in gomp_task_run_post_remove_taskgroup.
  1039. It is not necessary that we synchronize with other non-0 writes at
  1040. this point, but we must ensure that all writes to memory by a
  1041. child thread task work function are seen before we exit from
  1042. GOMP_taskgroup_end. */
  1043. if (__atomic_load_n (&taskgroup->num_children, MEMMODEL_ACQUIRE) == 0)
  1044. goto finish;
  1045. gomp_mutex_lock (&team->task_lock);
  1046. while (1)
  1047. {
  1048. bool cancelled = false;
  1049. if (taskgroup->children == NULL)
  1050. {
  1051. if (taskgroup->num_children)
  1052. {
  1053. if (task->children == NULL)
  1054. goto do_wait;
  1055. child_task = task->children;
  1056. }
  1057. else
  1058. {
  1059. gomp_mutex_unlock (&team->task_lock);
  1060. if (to_free)
  1061. {
  1062. gomp_finish_task (to_free);
  1063. free (to_free);
  1064. }
  1065. goto finish;
  1066. }
  1067. }
  1068. else
  1069. child_task = taskgroup->children;
  1070. if (child_task->kind == GOMP_TASK_WAITING)
  1071. {
  1072. cancelled
  1073. = gomp_task_run_pre (child_task, child_task->parent, taskgroup,
  1074. team);
  1075. if (__builtin_expect (cancelled, 0))
  1076. {
  1077. if (to_free)
  1078. {
  1079. gomp_finish_task (to_free);
  1080. free (to_free);
  1081. to_free = NULL;
  1082. }
  1083. goto finish_cancelled;
  1084. }
  1085. }
  1086. else
  1087. {
  1088. child_task = NULL;
  1089. do_wait:
  1090. /* All tasks we are waiting for are already running
  1091. in other threads. Wait for them. */
  1092. taskgroup->in_taskgroup_wait = true;
  1093. }
  1094. gomp_mutex_unlock (&team->task_lock);
  1095. if (do_wake)
  1096. {
  1097. gomp_team_barrier_wake (&team->barrier, do_wake);
  1098. do_wake = 0;
  1099. }
  1100. if (to_free)
  1101. {
  1102. gomp_finish_task (to_free);
  1103. free (to_free);
  1104. to_free = NULL;
  1105. }
  1106. if (child_task)
  1107. {
  1108. thr->task = child_task;
  1109. child_task->fn (child_task->fn_data);
  1110. thr->task = task;
  1111. }
  1112. else
  1113. gomp_sem_wait (&taskgroup->taskgroup_sem);
  1114. gomp_mutex_lock (&team->task_lock);
  1115. if (child_task)
  1116. {
  1117. finish_cancelled:;
  1118. size_t new_tasks
  1119. = gomp_task_run_post_handle_depend (child_task, team);
  1120. gomp_task_run_post_remove_parent (child_task);
  1121. gomp_clear_parent (child_task->children);
  1122. gomp_task_run_post_remove_taskgroup (child_task);
  1123. to_free = child_task;
  1124. child_task = NULL;
  1125. team->task_count--;
  1126. if (new_tasks > 1)
  1127. {
  1128. do_wake = team->nthreads - team->task_running_count
  1129. - !task->in_tied_task;
  1130. if (do_wake > new_tasks)
  1131. do_wake = new_tasks;
  1132. }
  1133. }
  1134. }
  1135. finish:
  1136. task->taskgroup = taskgroup->prev;
  1137. gomp_sem_destroy (&taskgroup->taskgroup_sem);
  1138. free (taskgroup);
  1139. }
  1140. int
  1141. omp_in_final (void)
  1142. {
  1143. struct gomp_thread *thr = gomp_thread ();
  1144. return thr->task && thr->task->final_task;
  1145. }
  1146. ialias (omp_in_final)