ordered.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /* Copyright (C) 2005-2022 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 ORDERED construct. */
  21. #include "libgomp.h"
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include "doacross.h"
  25. /* This function is called when first allocating an iteration block. That
  26. is, the thread is not currently on the queue. The work-share lock must
  27. be held on entry. */
  28. void
  29. gomp_ordered_first (void)
  30. {
  31. struct gomp_thread *thr = gomp_thread ();
  32. struct gomp_team *team = thr->ts.team;
  33. struct gomp_work_share *ws = thr->ts.work_share;
  34. unsigned index;
  35. /* Work share constructs can be orphaned. */
  36. if (team == NULL || team->nthreads == 1)
  37. return;
  38. index = ws->ordered_cur + ws->ordered_num_used;
  39. if (index >= team->nthreads)
  40. index -= team->nthreads;
  41. ws->ordered_team_ids[index] = thr->ts.team_id;
  42. /* If this is the first and only thread in the queue, then there is
  43. no one to release us when we get to our ordered section. Post to
  44. our own release queue now so that we won't block later. */
  45. if (ws->ordered_num_used++ == 0)
  46. gomp_sem_post (team->ordered_release[thr->ts.team_id]);
  47. }
  48. /* This function is called when completing the last iteration block. That
  49. is, there are no more iterations to perform and so the thread should be
  50. removed from the queue entirely. Because of the way ORDERED blocks are
  51. managed, it follows that we currently own access to the ORDERED block,
  52. and should now pass it on to the next thread. The work-share lock must
  53. be held on entry. */
  54. void
  55. gomp_ordered_last (void)
  56. {
  57. struct gomp_thread *thr = gomp_thread ();
  58. struct gomp_team *team = thr->ts.team;
  59. struct gomp_work_share *ws = thr->ts.work_share;
  60. unsigned next_id;
  61. /* Work share constructs can be orphaned. */
  62. if (team == NULL || team->nthreads == 1)
  63. return;
  64. /* We're no longer the owner. */
  65. ws->ordered_owner = -1;
  66. /* If we're not the last thread in the queue, then wake the next. */
  67. if (--ws->ordered_num_used > 0)
  68. {
  69. unsigned next = ws->ordered_cur + 1;
  70. if (next == team->nthreads)
  71. next = 0;
  72. ws->ordered_cur = next;
  73. next_id = ws->ordered_team_ids[next];
  74. gomp_sem_post (team->ordered_release[next_id]);
  75. }
  76. }
  77. /* This function is called when allocating a subsequent allocation block.
  78. That is, we're done with the current iteration block and we're allocating
  79. another. This is the logical combination of a call to gomp_ordered_last
  80. followed by a call to gomp_ordered_first. The work-share lock must be
  81. held on entry. */
  82. void
  83. gomp_ordered_next (void)
  84. {
  85. struct gomp_thread *thr = gomp_thread ();
  86. struct gomp_team *team = thr->ts.team;
  87. struct gomp_work_share *ws = thr->ts.work_share;
  88. unsigned index, next_id;
  89. /* Work share constructs can be orphaned. */
  90. if (team == NULL || team->nthreads == 1)
  91. return;
  92. /* We're no longer the owner. */
  93. ws->ordered_owner = -1;
  94. /* If there's only one thread in the queue, that must be us. */
  95. if (ws->ordered_num_used == 1)
  96. {
  97. /* We have a similar situation as in gomp_ordered_first
  98. where we need to post to our own release semaphore. */
  99. gomp_sem_post (team->ordered_release[thr->ts.team_id]);
  100. return;
  101. }
  102. /* If the queue is entirely full, then we move ourself to the end of
  103. the queue merely by incrementing ordered_cur. Only if it's not
  104. full do we have to write our id. */
  105. if (ws->ordered_num_used < team->nthreads)
  106. {
  107. index = ws->ordered_cur + ws->ordered_num_used;
  108. if (index >= team->nthreads)
  109. index -= team->nthreads;
  110. ws->ordered_team_ids[index] = thr->ts.team_id;
  111. }
  112. index = ws->ordered_cur + 1;
  113. if (index == team->nthreads)
  114. index = 0;
  115. ws->ordered_cur = index;
  116. next_id = ws->ordered_team_ids[index];
  117. gomp_sem_post (team->ordered_release[next_id]);
  118. }
  119. /* This function is called when a statically scheduled loop is first
  120. being created. */
  121. void
  122. gomp_ordered_static_init (void)
  123. {
  124. struct gomp_thread *thr = gomp_thread ();
  125. struct gomp_team *team = thr->ts.team;
  126. if (team == NULL || team->nthreads == 1)
  127. return;
  128. gomp_sem_post (team->ordered_release[0]);
  129. }
  130. /* This function is called when a statically scheduled loop is moving to
  131. the next allocation block. Static schedules are not first come first
  132. served like the others, so we're to move to the numerically next thread,
  133. not the next thread on a list. The work-share lock should *not* be held
  134. on entry. */
  135. void
  136. gomp_ordered_static_next (void)
  137. {
  138. struct gomp_thread *thr = gomp_thread ();
  139. struct gomp_team *team = thr->ts.team;
  140. struct gomp_work_share *ws = thr->ts.work_share;
  141. unsigned id = thr->ts.team_id;
  142. if (team == NULL || team->nthreads == 1)
  143. return;
  144. ws->ordered_owner = -1;
  145. /* This thread currently owns the lock. Increment the owner. */
  146. if (++id == team->nthreads)
  147. id = 0;
  148. ws->ordered_team_ids[0] = id;
  149. gomp_sem_post (team->ordered_release[id]);
  150. }
  151. /* This function is called when we need to assert that the thread owns the
  152. ordered section. Due to the problem of posted-but-not-waited semaphores,
  153. this needs to happen before completing a loop iteration. */
  154. void
  155. gomp_ordered_sync (void)
  156. {
  157. struct gomp_thread *thr = gomp_thread ();
  158. struct gomp_team *team = thr->ts.team;
  159. struct gomp_work_share *ws = thr->ts.work_share;
  160. /* Work share constructs can be orphaned. But this clearly means that
  161. we are the only thread, and so we automatically own the section. */
  162. if (team == NULL || team->nthreads == 1)
  163. return;
  164. /* ??? I believe it to be safe to access this data without taking the
  165. ws->lock. The only presumed race condition is with the previous
  166. thread on the queue incrementing ordered_cur such that it points
  167. to us, concurrently with our check below. But our team_id is
  168. already present in the queue, and the other thread will always
  169. post to our release semaphore. So the two cases are that we will
  170. either win the race an momentarily block on the semaphore, or lose
  171. the race and find the semaphore already unlocked and so not block.
  172. Either way we get correct results.
  173. However, there is an implicit flush on entry to an ordered region,
  174. so we do need to have a barrier here. If we were taking a lock
  175. this could be MEMMODEL_RELEASE since the acquire would be covered
  176. by the lock. */
  177. __atomic_thread_fence (MEMMODEL_ACQ_REL);
  178. if (ws->ordered_owner != thr->ts.team_id)
  179. {
  180. gomp_sem_wait (team->ordered_release[thr->ts.team_id]);
  181. ws->ordered_owner = thr->ts.team_id;
  182. }
  183. }
  184. /* This function is called by user code when encountering the start of an
  185. ORDERED block. We must check to see if the current thread is at the
  186. head of the queue, and if not, block. */
  187. #ifdef HAVE_ATTRIBUTE_ALIAS
  188. extern void GOMP_ordered_start (void)
  189. __attribute__((alias ("gomp_ordered_sync")));
  190. #else
  191. void
  192. GOMP_ordered_start (void)
  193. {
  194. gomp_ordered_sync ();
  195. }
  196. #endif
  197. /* This function is called by user code when encountering the end of an
  198. ORDERED block. With the current ORDERED implementation there's nothing
  199. for us to do.
  200. However, the current implementation has a flaw in that it does not allow
  201. the next thread into the ORDERED section immediately after the current
  202. thread exits the ORDERED section in its last iteration. The existence
  203. of this function allows the implementation to change. */
  204. void
  205. GOMP_ordered_end (void)
  206. {
  207. }
  208. /* DOACROSS initialization. */
  209. #define MAX_COLLAPSED_BITS (__SIZEOF_LONG__ * __CHAR_BIT__)
  210. void
  211. gomp_doacross_init (unsigned ncounts, long *counts, long chunk_size,
  212. size_t extra)
  213. {
  214. struct gomp_thread *thr = gomp_thread ();
  215. struct gomp_team *team = thr->ts.team;
  216. struct gomp_work_share *ws = thr->ts.work_share;
  217. unsigned int i, bits[MAX_COLLAPSED_BITS], num_bits = 0;
  218. unsigned long ent, num_ents, elt_sz, shift_sz;
  219. struct gomp_doacross_work_share *doacross;
  220. if (team == NULL || team->nthreads == 1)
  221. {
  222. empty:
  223. if (!extra)
  224. ws->doacross = NULL;
  225. else
  226. {
  227. doacross = gomp_malloc_cleared (sizeof (*doacross) + extra);
  228. doacross->extra = (void *) (doacross + 1);
  229. ws->doacross = doacross;
  230. }
  231. return;
  232. }
  233. for (i = 0; i < ncounts; i++)
  234. {
  235. /* If any count is 0, GOMP_doacross_{post,wait} can't be called. */
  236. if (counts[i] == 0)
  237. goto empty;
  238. if (num_bits <= MAX_COLLAPSED_BITS)
  239. {
  240. unsigned int this_bits;
  241. if (counts[i] == 1)
  242. this_bits = 1;
  243. else
  244. this_bits = __SIZEOF_LONG__ * __CHAR_BIT__
  245. - __builtin_clzl (counts[i] - 1);
  246. if (num_bits + this_bits <= MAX_COLLAPSED_BITS)
  247. {
  248. bits[i] = this_bits;
  249. num_bits += this_bits;
  250. }
  251. else
  252. num_bits = MAX_COLLAPSED_BITS + 1;
  253. }
  254. }
  255. if (ws->sched == GFS_STATIC)
  256. num_ents = team->nthreads;
  257. else if (ws->sched == GFS_GUIDED)
  258. num_ents = counts[0];
  259. else
  260. num_ents = (counts[0] - 1) / chunk_size + 1;
  261. if (num_bits <= MAX_COLLAPSED_BITS)
  262. {
  263. elt_sz = sizeof (unsigned long);
  264. shift_sz = ncounts * sizeof (unsigned int);
  265. }
  266. else
  267. {
  268. elt_sz = sizeof (unsigned long) * ncounts;
  269. shift_sz = 0;
  270. }
  271. elt_sz = (elt_sz + 63) & ~63UL;
  272. doacross = gomp_malloc (sizeof (*doacross) + 63 + num_ents * elt_sz
  273. + shift_sz + extra);
  274. doacross->chunk_size = chunk_size;
  275. doacross->elt_sz = elt_sz;
  276. doacross->ncounts = ncounts;
  277. doacross->flattened = false;
  278. doacross->array = (unsigned char *)
  279. ((((uintptr_t) (doacross + 1)) + 63 + shift_sz)
  280. & ~(uintptr_t) 63);
  281. if (extra)
  282. {
  283. doacross->extra = doacross->array + num_ents * elt_sz;
  284. memset (doacross->extra, '\0', extra);
  285. }
  286. else
  287. doacross->extra = NULL;
  288. if (num_bits <= MAX_COLLAPSED_BITS)
  289. {
  290. unsigned int shift_count = 0;
  291. doacross->flattened = true;
  292. for (i = ncounts; i > 0; i--)
  293. {
  294. doacross->shift_counts[i - 1] = shift_count;
  295. shift_count += bits[i - 1];
  296. }
  297. for (ent = 0; ent < num_ents; ent++)
  298. *(unsigned long *) (doacross->array + ent * elt_sz) = 0;
  299. }
  300. else
  301. for (ent = 0; ent < num_ents; ent++)
  302. memset (doacross->array + ent * elt_sz, '\0',
  303. sizeof (unsigned long) * ncounts);
  304. if (ws->sched == GFS_STATIC && chunk_size == 0)
  305. {
  306. unsigned long q = counts[0] / num_ents;
  307. unsigned long t = counts[0] % num_ents;
  308. doacross->boundary = t * (q + 1);
  309. doacross->q = q;
  310. doacross->t = t;
  311. }
  312. ws->doacross = doacross;
  313. }
  314. /* DOACROSS POST operation. */
  315. void
  316. GOMP_doacross_post (long *counts)
  317. {
  318. struct gomp_thread *thr = gomp_thread ();
  319. struct gomp_work_share *ws = thr->ts.work_share;
  320. struct gomp_doacross_work_share *doacross = ws->doacross;
  321. unsigned long ent;
  322. unsigned int i;
  323. if (__builtin_expect (doacross == NULL, 0)
  324. || __builtin_expect (doacross->array == NULL, 0))
  325. {
  326. __sync_synchronize ();
  327. return;
  328. }
  329. if (__builtin_expect (ws->sched == GFS_STATIC, 1))
  330. ent = thr->ts.team_id;
  331. else if (ws->sched == GFS_GUIDED)
  332. ent = counts[0];
  333. else
  334. ent = counts[0] / doacross->chunk_size;
  335. unsigned long *array = (unsigned long *) (doacross->array
  336. + ent * doacross->elt_sz);
  337. if (__builtin_expect (doacross->flattened, 1))
  338. {
  339. unsigned long flattened
  340. = (unsigned long) counts[0] << doacross->shift_counts[0];
  341. for (i = 1; i < doacross->ncounts; i++)
  342. flattened |= (unsigned long) counts[i]
  343. << doacross->shift_counts[i];
  344. flattened++;
  345. if (flattened == __atomic_load_n (array, MEMMODEL_ACQUIRE))
  346. __atomic_thread_fence (MEMMODEL_RELEASE);
  347. else
  348. __atomic_store_n (array, flattened, MEMMODEL_RELEASE);
  349. return;
  350. }
  351. __atomic_thread_fence (MEMMODEL_ACQUIRE);
  352. for (i = doacross->ncounts; i-- > 0; )
  353. {
  354. if (counts[i] + 1UL != __atomic_load_n (&array[i], MEMMODEL_RELAXED))
  355. __atomic_store_n (&array[i], counts[i] + 1UL, MEMMODEL_RELEASE);
  356. }
  357. }
  358. /* DOACROSS WAIT operation. */
  359. void
  360. GOMP_doacross_wait (long first, ...)
  361. {
  362. struct gomp_thread *thr = gomp_thread ();
  363. struct gomp_work_share *ws = thr->ts.work_share;
  364. struct gomp_doacross_work_share *doacross = ws->doacross;
  365. va_list ap;
  366. unsigned long ent;
  367. unsigned int i;
  368. if (__builtin_expect (doacross == NULL, 0)
  369. || __builtin_expect (doacross->array == NULL, 0))
  370. {
  371. __sync_synchronize ();
  372. return;
  373. }
  374. if (__builtin_expect (ws->sched == GFS_STATIC, 1))
  375. {
  376. if (ws->chunk_size == 0)
  377. {
  378. if (first < doacross->boundary)
  379. ent = first / (doacross->q + 1);
  380. else
  381. ent = (first - doacross->boundary) / doacross->q
  382. + doacross->t;
  383. }
  384. else
  385. ent = first / ws->chunk_size % thr->ts.team->nthreads;
  386. }
  387. else if (ws->sched == GFS_GUIDED)
  388. ent = first;
  389. else
  390. ent = first / doacross->chunk_size;
  391. unsigned long *array = (unsigned long *) (doacross->array
  392. + ent * doacross->elt_sz);
  393. if (__builtin_expect (doacross->flattened, 1))
  394. {
  395. unsigned long flattened
  396. = (unsigned long) first << doacross->shift_counts[0];
  397. unsigned long cur;
  398. va_start (ap, first);
  399. for (i = 1; i < doacross->ncounts; i++)
  400. flattened |= (unsigned long) va_arg (ap, long)
  401. << doacross->shift_counts[i];
  402. cur = __atomic_load_n (array, MEMMODEL_ACQUIRE);
  403. if (flattened < cur)
  404. {
  405. __atomic_thread_fence (MEMMODEL_RELEASE);
  406. va_end (ap);
  407. return;
  408. }
  409. doacross_spin (array, flattened, cur);
  410. __atomic_thread_fence (MEMMODEL_RELEASE);
  411. va_end (ap);
  412. return;
  413. }
  414. do
  415. {
  416. va_start (ap, first);
  417. for (i = 0; i < doacross->ncounts; i++)
  418. {
  419. unsigned long thisv
  420. = (unsigned long) (i ? va_arg (ap, long) : first) + 1;
  421. unsigned long cur = __atomic_load_n (&array[i], MEMMODEL_RELAXED);
  422. if (thisv < cur)
  423. {
  424. i = doacross->ncounts;
  425. break;
  426. }
  427. if (thisv > cur)
  428. break;
  429. }
  430. va_end (ap);
  431. if (i == doacross->ncounts)
  432. break;
  433. cpu_relax ();
  434. }
  435. while (1);
  436. __sync_synchronize ();
  437. }
  438. typedef unsigned long long gomp_ull;
  439. void
  440. gomp_doacross_ull_init (unsigned ncounts, gomp_ull *counts,
  441. gomp_ull chunk_size, size_t extra)
  442. {
  443. struct gomp_thread *thr = gomp_thread ();
  444. struct gomp_team *team = thr->ts.team;
  445. struct gomp_work_share *ws = thr->ts.work_share;
  446. unsigned int i, bits[MAX_COLLAPSED_BITS], num_bits = 0;
  447. unsigned long ent, num_ents, elt_sz, shift_sz;
  448. struct gomp_doacross_work_share *doacross;
  449. if (team == NULL || team->nthreads == 1)
  450. {
  451. empty:
  452. if (!extra)
  453. ws->doacross = NULL;
  454. else
  455. {
  456. doacross = gomp_malloc_cleared (sizeof (*doacross) + extra);
  457. doacross->extra = (void *) (doacross + 1);
  458. ws->doacross = doacross;
  459. }
  460. return;
  461. }
  462. for (i = 0; i < ncounts; i++)
  463. {
  464. /* If any count is 0, GOMP_doacross_{post,wait} can't be called. */
  465. if (counts[i] == 0)
  466. goto empty;
  467. if (num_bits <= MAX_COLLAPSED_BITS)
  468. {
  469. unsigned int this_bits;
  470. if (counts[i] == 1)
  471. this_bits = 1;
  472. else
  473. this_bits = __SIZEOF_LONG_LONG__ * __CHAR_BIT__
  474. - __builtin_clzll (counts[i] - 1);
  475. if (num_bits + this_bits <= MAX_COLLAPSED_BITS)
  476. {
  477. bits[i] = this_bits;
  478. num_bits += this_bits;
  479. }
  480. else
  481. num_bits = MAX_COLLAPSED_BITS + 1;
  482. }
  483. }
  484. if (ws->sched == GFS_STATIC)
  485. num_ents = team->nthreads;
  486. else if (ws->sched == GFS_GUIDED)
  487. num_ents = counts[0];
  488. else
  489. num_ents = (counts[0] - 1) / chunk_size + 1;
  490. if (num_bits <= MAX_COLLAPSED_BITS)
  491. {
  492. elt_sz = sizeof (unsigned long);
  493. shift_sz = ncounts * sizeof (unsigned int);
  494. }
  495. else
  496. {
  497. if (sizeof (gomp_ull) == sizeof (unsigned long))
  498. elt_sz = sizeof (gomp_ull) * ncounts;
  499. else if (sizeof (gomp_ull) == 2 * sizeof (unsigned long))
  500. elt_sz = sizeof (unsigned long) * 2 * ncounts;
  501. else
  502. abort ();
  503. shift_sz = 0;
  504. }
  505. elt_sz = (elt_sz + 63) & ~63UL;
  506. doacross = gomp_malloc (sizeof (*doacross) + 63 + num_ents * elt_sz
  507. + shift_sz);
  508. doacross->chunk_size_ull = chunk_size;
  509. doacross->elt_sz = elt_sz;
  510. doacross->ncounts = ncounts;
  511. doacross->flattened = false;
  512. doacross->boundary = 0;
  513. doacross->array = (unsigned char *)
  514. ((((uintptr_t) (doacross + 1)) + 63 + shift_sz)
  515. & ~(uintptr_t) 63);
  516. if (extra)
  517. {
  518. doacross->extra = doacross->array + num_ents * elt_sz;
  519. memset (doacross->extra, '\0', extra);
  520. }
  521. else
  522. doacross->extra = NULL;
  523. if (num_bits <= MAX_COLLAPSED_BITS)
  524. {
  525. unsigned int shift_count = 0;
  526. doacross->flattened = true;
  527. for (i = ncounts; i > 0; i--)
  528. {
  529. doacross->shift_counts[i - 1] = shift_count;
  530. shift_count += bits[i - 1];
  531. }
  532. for (ent = 0; ent < num_ents; ent++)
  533. *(unsigned long *) (doacross->array + ent * elt_sz) = 0;
  534. }
  535. else
  536. for (ent = 0; ent < num_ents; ent++)
  537. memset (doacross->array + ent * elt_sz, '\0',
  538. sizeof (unsigned long) * ncounts);
  539. if (ws->sched == GFS_STATIC && chunk_size == 0)
  540. {
  541. gomp_ull q = counts[0] / num_ents;
  542. gomp_ull t = counts[0] % num_ents;
  543. doacross->boundary_ull = t * (q + 1);
  544. doacross->q_ull = q;
  545. doacross->t = t;
  546. }
  547. ws->doacross = doacross;
  548. }
  549. /* DOACROSS POST operation. */
  550. void
  551. GOMP_doacross_ull_post (gomp_ull *counts)
  552. {
  553. struct gomp_thread *thr = gomp_thread ();
  554. struct gomp_work_share *ws = thr->ts.work_share;
  555. struct gomp_doacross_work_share *doacross = ws->doacross;
  556. unsigned long ent;
  557. unsigned int i;
  558. if (__builtin_expect (doacross == NULL, 0)
  559. || __builtin_expect (doacross->array == NULL, 0))
  560. {
  561. __sync_synchronize ();
  562. return;
  563. }
  564. if (__builtin_expect (ws->sched == GFS_STATIC, 1))
  565. ent = thr->ts.team_id;
  566. else if (ws->sched == GFS_GUIDED)
  567. ent = counts[0];
  568. else
  569. ent = counts[0] / doacross->chunk_size_ull;
  570. if (__builtin_expect (doacross->flattened, 1))
  571. {
  572. unsigned long *array = (unsigned long *) (doacross->array
  573. + ent * doacross->elt_sz);
  574. gomp_ull flattened
  575. = counts[0] << doacross->shift_counts[0];
  576. for (i = 1; i < doacross->ncounts; i++)
  577. flattened |= counts[i] << doacross->shift_counts[i];
  578. flattened++;
  579. if (flattened == __atomic_load_n (array, MEMMODEL_ACQUIRE))
  580. __atomic_thread_fence (MEMMODEL_RELEASE);
  581. else
  582. __atomic_store_n (array, flattened, MEMMODEL_RELEASE);
  583. return;
  584. }
  585. __atomic_thread_fence (MEMMODEL_ACQUIRE);
  586. if (sizeof (gomp_ull) == sizeof (unsigned long))
  587. {
  588. gomp_ull *array = (gomp_ull *) (doacross->array
  589. + ent * doacross->elt_sz);
  590. for (i = doacross->ncounts; i-- > 0; )
  591. {
  592. if (counts[i] + 1UL != __atomic_load_n (&array[i], MEMMODEL_RELAXED))
  593. __atomic_store_n (&array[i], counts[i] + 1UL, MEMMODEL_RELEASE);
  594. }
  595. }
  596. else
  597. {
  598. unsigned long *array = (unsigned long *) (doacross->array
  599. + ent * doacross->elt_sz);
  600. for (i = doacross->ncounts; i-- > 0; )
  601. {
  602. gomp_ull cull = counts[i] + 1UL;
  603. unsigned long c = (unsigned long) cull;
  604. if (c != __atomic_load_n (&array[2 * i + 1], MEMMODEL_RELAXED))
  605. __atomic_store_n (&array[2 * i + 1], c, MEMMODEL_RELEASE);
  606. c = cull >> (__SIZEOF_LONG_LONG__ * __CHAR_BIT__ / 2);
  607. if (c != __atomic_load_n (&array[2 * i], MEMMODEL_RELAXED))
  608. __atomic_store_n (&array[2 * i], c, MEMMODEL_RELEASE);
  609. }
  610. }
  611. }
  612. /* DOACROSS WAIT operation. */
  613. void
  614. GOMP_doacross_ull_wait (gomp_ull first, ...)
  615. {
  616. struct gomp_thread *thr = gomp_thread ();
  617. struct gomp_work_share *ws = thr->ts.work_share;
  618. struct gomp_doacross_work_share *doacross = ws->doacross;
  619. va_list ap;
  620. unsigned long ent;
  621. unsigned int i;
  622. if (__builtin_expect (doacross == NULL, 0)
  623. || __builtin_expect (doacross->array == NULL, 0))
  624. {
  625. __sync_synchronize ();
  626. return;
  627. }
  628. if (__builtin_expect (ws->sched == GFS_STATIC, 1))
  629. {
  630. if (ws->chunk_size_ull == 0)
  631. {
  632. if (first < doacross->boundary_ull)
  633. ent = first / (doacross->q_ull + 1);
  634. else
  635. ent = (first - doacross->boundary_ull) / doacross->q_ull
  636. + doacross->t;
  637. }
  638. else
  639. ent = first / ws->chunk_size_ull % thr->ts.team->nthreads;
  640. }
  641. else if (ws->sched == GFS_GUIDED)
  642. ent = first;
  643. else
  644. ent = first / doacross->chunk_size_ull;
  645. if (__builtin_expect (doacross->flattened, 1))
  646. {
  647. unsigned long *array = (unsigned long *) (doacross->array
  648. + ent * doacross->elt_sz);
  649. gomp_ull flattened = first << doacross->shift_counts[0];
  650. unsigned long cur;
  651. va_start (ap, first);
  652. for (i = 1; i < doacross->ncounts; i++)
  653. flattened |= va_arg (ap, gomp_ull)
  654. << doacross->shift_counts[i];
  655. cur = __atomic_load_n (array, MEMMODEL_ACQUIRE);
  656. if (flattened < cur)
  657. {
  658. __atomic_thread_fence (MEMMODEL_RELEASE);
  659. va_end (ap);
  660. return;
  661. }
  662. doacross_spin (array, flattened, cur);
  663. __atomic_thread_fence (MEMMODEL_RELEASE);
  664. va_end (ap);
  665. return;
  666. }
  667. if (sizeof (gomp_ull) == sizeof (unsigned long))
  668. {
  669. gomp_ull *array = (gomp_ull *) (doacross->array
  670. + ent * doacross->elt_sz);
  671. do
  672. {
  673. va_start (ap, first);
  674. for (i = 0; i < doacross->ncounts; i++)
  675. {
  676. gomp_ull thisv
  677. = (i ? va_arg (ap, gomp_ull) : first) + 1;
  678. gomp_ull cur = __atomic_load_n (&array[i], MEMMODEL_RELAXED);
  679. if (thisv < cur)
  680. {
  681. i = doacross->ncounts;
  682. break;
  683. }
  684. if (thisv > cur)
  685. break;
  686. }
  687. va_end (ap);
  688. if (i == doacross->ncounts)
  689. break;
  690. cpu_relax ();
  691. }
  692. while (1);
  693. }
  694. else
  695. {
  696. unsigned long *array = (unsigned long *) (doacross->array
  697. + ent * doacross->elt_sz);
  698. do
  699. {
  700. va_start (ap, first);
  701. for (i = 0; i < doacross->ncounts; i++)
  702. {
  703. gomp_ull thisv
  704. = (i ? va_arg (ap, gomp_ull) : first) + 1;
  705. unsigned long t
  706. = thisv >> (__SIZEOF_LONG_LONG__ * __CHAR_BIT__ / 2);
  707. unsigned long cur
  708. = __atomic_load_n (&array[2 * i], MEMMODEL_RELAXED);
  709. if (t < cur)
  710. {
  711. i = doacross->ncounts;
  712. break;
  713. }
  714. if (t > cur)
  715. break;
  716. t = thisv;
  717. cur = __atomic_load_n (&array[2 * i + 1], MEMMODEL_RELAXED);
  718. if (t < cur)
  719. {
  720. i = doacross->ncounts;
  721. break;
  722. }
  723. if (t > cur)
  724. break;
  725. }
  726. va_end (ap);
  727. if (i == doacross->ncounts)
  728. break;
  729. cpu_relax ();
  730. }
  731. while (1);
  732. }
  733. __sync_synchronize ();
  734. }