work.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 contains routines to manage the work-share queue for a team
  21. of threads. */
  22. #include "libgomp.h"
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. /* Allocate a new work share structure, preferably from current team's
  27. free gomp_work_share cache. */
  28. static struct gomp_work_share *
  29. alloc_work_share (struct gomp_team *team)
  30. {
  31. struct gomp_work_share *ws;
  32. unsigned int i;
  33. /* This is called in a critical section. */
  34. if (team->work_share_list_alloc != NULL)
  35. {
  36. ws = team->work_share_list_alloc;
  37. team->work_share_list_alloc = ws->next_free;
  38. return ws;
  39. }
  40. #ifdef HAVE_SYNC_BUILTINS
  41. ws = team->work_share_list_free;
  42. /* We need atomic read from work_share_list_free,
  43. as free_work_share can be called concurrently. */
  44. __asm ("" : "+r" (ws));
  45. if (ws && ws->next_free)
  46. {
  47. struct gomp_work_share *next = ws->next_free;
  48. ws->next_free = NULL;
  49. team->work_share_list_alloc = next->next_free;
  50. return next;
  51. }
  52. #else
  53. gomp_mutex_lock (&team->work_share_list_free_lock);
  54. ws = team->work_share_list_free;
  55. if (ws)
  56. {
  57. team->work_share_list_alloc = ws->next_free;
  58. team->work_share_list_free = NULL;
  59. gomp_mutex_unlock (&team->work_share_list_free_lock);
  60. return ws;
  61. }
  62. gomp_mutex_unlock (&team->work_share_list_free_lock);
  63. #endif
  64. team->work_share_chunk *= 2;
  65. /* Allocating gomp_work_share structures aligned is just an
  66. optimization, don't do it when using the fallback method. */
  67. #ifdef GOMP_USE_ALIGNED_WORK_SHARES
  68. ws = gomp_aligned_alloc (__alignof (struct gomp_work_share),
  69. team->work_share_chunk
  70. * sizeof (struct gomp_work_share));
  71. #else
  72. ws = gomp_malloc (team->work_share_chunk * sizeof (struct gomp_work_share));
  73. #endif
  74. ws->next_alloc = team->work_shares[0].next_alloc;
  75. team->work_shares[0].next_alloc = ws;
  76. team->work_share_list_alloc = &ws[1];
  77. for (i = 1; i < team->work_share_chunk - 1; i++)
  78. ws[i].next_free = &ws[i + 1];
  79. ws[i].next_free = NULL;
  80. return ws;
  81. }
  82. /* Initialize an already allocated struct gomp_work_share.
  83. This shouldn't touch the next_alloc field. */
  84. void
  85. gomp_init_work_share (struct gomp_work_share *ws, size_t ordered,
  86. unsigned nthreads)
  87. {
  88. gomp_mutex_init (&ws->lock);
  89. if (__builtin_expect (ordered, 0))
  90. {
  91. #define INLINE_ORDERED_TEAM_IDS_SIZE \
  92. (sizeof (struct gomp_work_share) \
  93. - offsetof (struct gomp_work_share, inline_ordered_team_ids))
  94. if (__builtin_expect (ordered != 1, 0))
  95. {
  96. size_t o = nthreads * sizeof (*ws->ordered_team_ids);
  97. o += __alignof__ (long long) - 1;
  98. if ((offsetof (struct gomp_work_share, inline_ordered_team_ids)
  99. & (__alignof__ (long long) - 1)) == 0)
  100. o &= ~(__alignof__ (long long) - 1);
  101. ordered += o - 1;
  102. }
  103. else
  104. ordered = nthreads * sizeof (*ws->ordered_team_ids);
  105. if (ordered > INLINE_ORDERED_TEAM_IDS_SIZE)
  106. ws->ordered_team_ids = team_malloc (ordered);
  107. else
  108. ws->ordered_team_ids = ws->inline_ordered_team_ids;
  109. memset (ws->ordered_team_ids, '\0', ordered);
  110. ws->ordered_num_used = 0;
  111. ws->ordered_owner = -1;
  112. ws->ordered_cur = 0;
  113. }
  114. else
  115. ws->ordered_team_ids = ws->inline_ordered_team_ids;
  116. gomp_ptrlock_init (&ws->next_ws, NULL);
  117. ws->threads_completed = 0;
  118. }
  119. /* Do any needed destruction of gomp_work_share fields before it
  120. is put back into free gomp_work_share cache or freed. */
  121. void
  122. gomp_fini_work_share (struct gomp_work_share *ws)
  123. {
  124. gomp_mutex_destroy (&ws->lock);
  125. if (ws->ordered_team_ids != ws->inline_ordered_team_ids)
  126. team_free (ws->ordered_team_ids);
  127. gomp_ptrlock_destroy (&ws->next_ws);
  128. }
  129. /* Free a work share struct, if not orphaned, put it into current
  130. team's free gomp_work_share cache. */
  131. static inline void
  132. free_work_share (struct gomp_team *team, struct gomp_work_share *ws)
  133. {
  134. gomp_fini_work_share (ws);
  135. if (__builtin_expect (team == NULL, 0))
  136. free (ws);
  137. else
  138. {
  139. struct gomp_work_share *next_ws;
  140. #ifdef HAVE_SYNC_BUILTINS
  141. do
  142. {
  143. next_ws = team->work_share_list_free;
  144. ws->next_free = next_ws;
  145. }
  146. while (!__sync_bool_compare_and_swap (&team->work_share_list_free,
  147. next_ws, ws));
  148. #else
  149. gomp_mutex_lock (&team->work_share_list_free_lock);
  150. next_ws = team->work_share_list_free;
  151. ws->next_free = next_ws;
  152. team->work_share_list_free = ws;
  153. gomp_mutex_unlock (&team->work_share_list_free_lock);
  154. #endif
  155. }
  156. }
  157. /* The current thread is ready to begin the next work sharing construct.
  158. In all cases, thr->ts.work_share is updated to point to the new
  159. structure. In all cases the work_share lock is locked. Return true
  160. if this was the first thread to reach this point. */
  161. bool
  162. gomp_work_share_start (size_t ordered)
  163. {
  164. struct gomp_thread *thr = gomp_thread ();
  165. struct gomp_team *team = thr->ts.team;
  166. struct gomp_work_share *ws;
  167. /* Work sharing constructs can be orphaned. */
  168. if (team == NULL)
  169. {
  170. #ifdef GOMP_USE_ALIGNED_WORK_SHARES
  171. ws = gomp_aligned_alloc (__alignof (struct gomp_work_share),
  172. sizeof (*ws));
  173. #else
  174. ws = gomp_malloc (sizeof (*ws));
  175. #endif
  176. gomp_init_work_share (ws, ordered, 1);
  177. thr->ts.work_share = ws;
  178. return true;
  179. }
  180. ws = thr->ts.work_share;
  181. thr->ts.last_work_share = ws;
  182. ws = gomp_ptrlock_get (&ws->next_ws);
  183. if (ws == NULL)
  184. {
  185. /* This thread encountered a new ws first. */
  186. struct gomp_work_share *ws = alloc_work_share (team);
  187. gomp_init_work_share (ws, ordered, team->nthreads);
  188. thr->ts.work_share = ws;
  189. return true;
  190. }
  191. else
  192. {
  193. thr->ts.work_share = ws;
  194. return false;
  195. }
  196. }
  197. /* The current thread is done with its current work sharing construct.
  198. This version does imply a barrier at the end of the work-share. */
  199. void
  200. gomp_work_share_end (void)
  201. {
  202. struct gomp_thread *thr = gomp_thread ();
  203. struct gomp_team *team = thr->ts.team;
  204. gomp_barrier_state_t bstate;
  205. /* Work sharing constructs can be orphaned. */
  206. if (team == NULL)
  207. {
  208. free_work_share (NULL, thr->ts.work_share);
  209. thr->ts.work_share = NULL;
  210. return;
  211. }
  212. bstate = gomp_barrier_wait_start (&team->barrier);
  213. if (gomp_barrier_last_thread (bstate))
  214. {
  215. if (__builtin_expect (thr->ts.last_work_share != NULL, 1))
  216. {
  217. team->work_shares_to_free = thr->ts.work_share;
  218. free_work_share (team, thr->ts.last_work_share);
  219. }
  220. }
  221. gomp_team_barrier_wait_end (&team->barrier, bstate);
  222. thr->ts.last_work_share = NULL;
  223. }
  224. /* The current thread is done with its current work sharing construct.
  225. This version implies a cancellable barrier at the end of the work-share. */
  226. bool
  227. gomp_work_share_end_cancel (void)
  228. {
  229. struct gomp_thread *thr = gomp_thread ();
  230. struct gomp_team *team = thr->ts.team;
  231. gomp_barrier_state_t bstate;
  232. /* Cancellable work sharing constructs cannot be orphaned. */
  233. bstate = gomp_barrier_wait_cancel_start (&team->barrier);
  234. if (gomp_barrier_last_thread (bstate))
  235. {
  236. if (__builtin_expect (thr->ts.last_work_share != NULL, 1))
  237. {
  238. team->work_shares_to_free = thr->ts.work_share;
  239. free_work_share (team, thr->ts.last_work_share);
  240. }
  241. }
  242. thr->ts.last_work_share = NULL;
  243. return gomp_team_barrier_wait_cancel_end (&team->barrier, bstate);
  244. }
  245. /* The current thread is done with its current work sharing construct.
  246. This version does NOT imply a barrier at the end of the work-share. */
  247. void
  248. gomp_work_share_end_nowait (void)
  249. {
  250. struct gomp_thread *thr = gomp_thread ();
  251. struct gomp_team *team = thr->ts.team;
  252. struct gomp_work_share *ws = thr->ts.work_share;
  253. unsigned completed;
  254. /* Work sharing constructs can be orphaned. */
  255. if (team == NULL)
  256. {
  257. free_work_share (NULL, ws);
  258. thr->ts.work_share = NULL;
  259. return;
  260. }
  261. if (__builtin_expect (thr->ts.last_work_share == NULL, 0))
  262. return;
  263. #ifdef HAVE_SYNC_BUILTINS
  264. completed = __sync_add_and_fetch (&ws->threads_completed, 1);
  265. #else
  266. gomp_mutex_lock (&ws->lock);
  267. completed = ++ws->threads_completed;
  268. gomp_mutex_unlock (&ws->lock);
  269. #endif
  270. if (completed == team->nthreads)
  271. {
  272. team->work_shares_to_free = thr->ts.work_share;
  273. free_work_share (team, thr->ts.last_work_share);
  274. }
  275. thr->ts.last_work_share = NULL;
  276. }