work.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Copyright (C) 2005-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 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. ws = gomp_malloc (team->work_share_chunk * sizeof (struct gomp_work_share));
  66. ws->next_alloc = team->work_shares[0].next_alloc;
  67. team->work_shares[0].next_alloc = ws;
  68. team->work_share_list_alloc = &ws[1];
  69. for (i = 1; i < team->work_share_chunk - 1; i++)
  70. ws[i].next_free = &ws[i + 1];
  71. ws[i].next_free = NULL;
  72. return ws;
  73. }
  74. /* Initialize an already allocated struct gomp_work_share.
  75. This shouldn't touch the next_alloc field. */
  76. void
  77. gomp_init_work_share (struct gomp_work_share *ws, bool ordered,
  78. unsigned nthreads)
  79. {
  80. gomp_mutex_init (&ws->lock);
  81. if (__builtin_expect (ordered, 0))
  82. {
  83. #define INLINE_ORDERED_TEAM_IDS_CNT \
  84. ((sizeof (struct gomp_work_share) \
  85. - offsetof (struct gomp_work_share, inline_ordered_team_ids)) \
  86. / sizeof (((struct gomp_work_share *) 0)->inline_ordered_team_ids[0]))
  87. if (nthreads > INLINE_ORDERED_TEAM_IDS_CNT)
  88. ws->ordered_team_ids
  89. = gomp_malloc (nthreads * sizeof (*ws->ordered_team_ids));
  90. else
  91. ws->ordered_team_ids = ws->inline_ordered_team_ids;
  92. memset (ws->ordered_team_ids, '\0',
  93. nthreads * sizeof (*ws->ordered_team_ids));
  94. ws->ordered_num_used = 0;
  95. ws->ordered_owner = -1;
  96. ws->ordered_cur = 0;
  97. }
  98. else
  99. ws->ordered_team_ids = NULL;
  100. gomp_ptrlock_init (&ws->next_ws, NULL);
  101. ws->threads_completed = 0;
  102. }
  103. /* Do any needed destruction of gomp_work_share fields before it
  104. is put back into free gomp_work_share cache or freed. */
  105. void
  106. gomp_fini_work_share (struct gomp_work_share *ws)
  107. {
  108. gomp_mutex_destroy (&ws->lock);
  109. if (ws->ordered_team_ids != ws->inline_ordered_team_ids)
  110. free (ws->ordered_team_ids);
  111. gomp_ptrlock_destroy (&ws->next_ws);
  112. }
  113. /* Free a work share struct, if not orphaned, put it into current
  114. team's free gomp_work_share cache. */
  115. static inline void
  116. free_work_share (struct gomp_team *team, struct gomp_work_share *ws)
  117. {
  118. gomp_fini_work_share (ws);
  119. if (__builtin_expect (team == NULL, 0))
  120. free (ws);
  121. else
  122. {
  123. struct gomp_work_share *next_ws;
  124. #ifdef HAVE_SYNC_BUILTINS
  125. do
  126. {
  127. next_ws = team->work_share_list_free;
  128. ws->next_free = next_ws;
  129. }
  130. while (!__sync_bool_compare_and_swap (&team->work_share_list_free,
  131. next_ws, ws));
  132. #else
  133. gomp_mutex_lock (&team->work_share_list_free_lock);
  134. next_ws = team->work_share_list_free;
  135. ws->next_free = next_ws;
  136. team->work_share_list_free = ws;
  137. gomp_mutex_unlock (&team->work_share_list_free_lock);
  138. #endif
  139. }
  140. }
  141. /* The current thread is ready to begin the next work sharing construct.
  142. In all cases, thr->ts.work_share is updated to point to the new
  143. structure. In all cases the work_share lock is locked. Return true
  144. if this was the first thread to reach this point. */
  145. bool
  146. gomp_work_share_start (bool ordered)
  147. {
  148. struct gomp_thread *thr = gomp_thread ();
  149. struct gomp_team *team = thr->ts.team;
  150. struct gomp_work_share *ws;
  151. /* Work sharing constructs can be orphaned. */
  152. if (team == NULL)
  153. {
  154. ws = gomp_malloc (sizeof (*ws));
  155. gomp_init_work_share (ws, ordered, 1);
  156. thr->ts.work_share = ws;
  157. return ws;
  158. }
  159. ws = thr->ts.work_share;
  160. thr->ts.last_work_share = ws;
  161. ws = gomp_ptrlock_get (&ws->next_ws);
  162. if (ws == NULL)
  163. {
  164. /* This thread encountered a new ws first. */
  165. struct gomp_work_share *ws = alloc_work_share (team);
  166. gomp_init_work_share (ws, ordered, team->nthreads);
  167. thr->ts.work_share = ws;
  168. return true;
  169. }
  170. else
  171. {
  172. thr->ts.work_share = ws;
  173. return false;
  174. }
  175. }
  176. /* The current thread is done with its current work sharing construct.
  177. This version does imply a barrier at the end of the work-share. */
  178. void
  179. gomp_work_share_end (void)
  180. {
  181. struct gomp_thread *thr = gomp_thread ();
  182. struct gomp_team *team = thr->ts.team;
  183. gomp_barrier_state_t bstate;
  184. /* Work sharing constructs can be orphaned. */
  185. if (team == NULL)
  186. {
  187. free_work_share (NULL, thr->ts.work_share);
  188. thr->ts.work_share = NULL;
  189. return;
  190. }
  191. bstate = gomp_barrier_wait_start (&team->barrier);
  192. if (gomp_barrier_last_thread (bstate))
  193. {
  194. if (__builtin_expect (thr->ts.last_work_share != NULL, 1))
  195. {
  196. team->work_shares_to_free = thr->ts.work_share;
  197. free_work_share (team, thr->ts.last_work_share);
  198. }
  199. }
  200. gomp_team_barrier_wait_end (&team->barrier, bstate);
  201. thr->ts.last_work_share = NULL;
  202. }
  203. /* The current thread is done with its current work sharing construct.
  204. This version implies a cancellable barrier at the end of the work-share. */
  205. bool
  206. gomp_work_share_end_cancel (void)
  207. {
  208. struct gomp_thread *thr = gomp_thread ();
  209. struct gomp_team *team = thr->ts.team;
  210. gomp_barrier_state_t bstate;
  211. /* Cancellable work sharing constructs cannot be orphaned. */
  212. bstate = gomp_barrier_wait_cancel_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. thr->ts.last_work_share = NULL;
  222. return gomp_team_barrier_wait_cancel_end (&team->barrier, bstate);
  223. }
  224. /* The current thread is done with its current work sharing construct.
  225. This version does NOT imply a barrier at the end of the work-share. */
  226. void
  227. gomp_work_share_end_nowait (void)
  228. {
  229. struct gomp_thread *thr = gomp_thread ();
  230. struct gomp_team *team = thr->ts.team;
  231. struct gomp_work_share *ws = thr->ts.work_share;
  232. unsigned completed;
  233. /* Work sharing constructs can be orphaned. */
  234. if (team == NULL)
  235. {
  236. free_work_share (NULL, ws);
  237. thr->ts.work_share = NULL;
  238. return;
  239. }
  240. if (__builtin_expect (thr->ts.last_work_share == NULL, 0))
  241. return;
  242. #ifdef HAVE_SYNC_BUILTINS
  243. completed = __sync_add_and_fetch (&ws->threads_completed, 1);
  244. #else
  245. gomp_mutex_lock (&ws->lock);
  246. completed = ++ws->threads_completed;
  247. gomp_mutex_unlock (&ws->lock);
  248. #endif
  249. if (completed == team->nthreads)
  250. {
  251. team->work_shares_to_free = thr->ts.work_share;
  252. free_work_share (team, thr->ts.last_work_share);
  253. }
  254. thr->ts.last_work_share = NULL;
  255. }