iter.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 for managing work-share iteration, both
  21. for loops and sections. */
  22. #include "libgomp.h"
  23. #include <stdlib.h>
  24. /* This function implements the STATIC scheduling method. The caller should
  25. iterate *pstart <= x < *pend. Return zero if there are more iterations
  26. to perform; nonzero if not. Return less than 0 if this thread had
  27. received the absolutely last iteration. */
  28. int
  29. gomp_iter_static_next (long *pstart, long *pend)
  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 long nthreads = team ? team->nthreads : 1;
  35. if (thr->ts.static_trip == -1)
  36. return -1;
  37. /* Quick test for degenerate teams and orphaned constructs. */
  38. if (nthreads == 1)
  39. {
  40. *pstart = ws->next;
  41. *pend = ws->end;
  42. thr->ts.static_trip = -1;
  43. return ws->next == ws->end;
  44. }
  45. /* We interpret chunk_size zero as "unspecified", which means that we
  46. should break up the iterations such that each thread makes only one
  47. trip through the outer loop. */
  48. if (ws->chunk_size == 0)
  49. {
  50. unsigned long n, q, i, t;
  51. unsigned long s0, e0;
  52. long s, e;
  53. if (thr->ts.static_trip > 0)
  54. return 1;
  55. /* Compute the total number of iterations. */
  56. s = ws->incr + (ws->incr > 0 ? -1 : 1);
  57. n = (ws->end - ws->next + s) / ws->incr;
  58. i = thr->ts.team_id;
  59. /* Compute the "zero-based" start and end points. That is, as
  60. if the loop began at zero and incremented by one. */
  61. q = n / nthreads;
  62. t = n % nthreads;
  63. if (i < t)
  64. {
  65. t = 0;
  66. q++;
  67. }
  68. s0 = q * i + t;
  69. e0 = s0 + q;
  70. /* Notice when no iterations allocated for this thread. */
  71. if (s0 >= e0)
  72. {
  73. thr->ts.static_trip = 1;
  74. return 1;
  75. }
  76. /* Transform these to the actual start and end numbers. */
  77. s = (long)s0 * ws->incr + ws->next;
  78. e = (long)e0 * ws->incr + ws->next;
  79. *pstart = s;
  80. *pend = e;
  81. thr->ts.static_trip = (e0 == n ? -1 : 1);
  82. return 0;
  83. }
  84. else
  85. {
  86. unsigned long n, s0, e0, i, c;
  87. long s, e;
  88. /* Otherwise, each thread gets exactly chunk_size iterations
  89. (if available) each time through the loop. */
  90. s = ws->incr + (ws->incr > 0 ? -1 : 1);
  91. n = (ws->end - ws->next + s) / ws->incr;
  92. i = thr->ts.team_id;
  93. c = ws->chunk_size;
  94. /* Initial guess is a C sized chunk positioned nthreads iterations
  95. in, offset by our thread number. */
  96. s0 = (thr->ts.static_trip * nthreads + i) * c;
  97. e0 = s0 + c;
  98. /* Detect overflow. */
  99. if (s0 >= n)
  100. return 1;
  101. if (e0 > n)
  102. e0 = n;
  103. /* Transform these to the actual start and end numbers. */
  104. s = (long)s0 * ws->incr + ws->next;
  105. e = (long)e0 * ws->incr + ws->next;
  106. *pstart = s;
  107. *pend = e;
  108. if (e0 == n)
  109. thr->ts.static_trip = -1;
  110. else
  111. thr->ts.static_trip++;
  112. return 0;
  113. }
  114. }
  115. /* This function implements the DYNAMIC scheduling method. Arguments are
  116. as for gomp_iter_static_next. This function must be called with ws->lock
  117. held. */
  118. bool
  119. gomp_iter_dynamic_next_locked (long *pstart, long *pend)
  120. {
  121. struct gomp_thread *thr = gomp_thread ();
  122. struct gomp_work_share *ws = thr->ts.work_share;
  123. long start, end, chunk, left;
  124. start = ws->next;
  125. if (start == ws->end)
  126. return false;
  127. chunk = ws->chunk_size;
  128. left = ws->end - start;
  129. if (ws->incr < 0)
  130. {
  131. if (chunk < left)
  132. chunk = left;
  133. }
  134. else
  135. {
  136. if (chunk > left)
  137. chunk = left;
  138. }
  139. end = start + chunk;
  140. ws->next = end;
  141. *pstart = start;
  142. *pend = end;
  143. return true;
  144. }
  145. #ifdef HAVE_SYNC_BUILTINS
  146. /* Similar, but doesn't require the lock held, and uses compare-and-swap
  147. instead. Note that the only memory value that changes is ws->next. */
  148. bool
  149. gomp_iter_dynamic_next (long *pstart, long *pend)
  150. {
  151. struct gomp_thread *thr = gomp_thread ();
  152. struct gomp_work_share *ws = thr->ts.work_share;
  153. long start, end, nend, chunk, incr;
  154. end = ws->end;
  155. incr = ws->incr;
  156. chunk = ws->chunk_size;
  157. if (__builtin_expect (ws->mode, 1))
  158. {
  159. long tmp = __sync_fetch_and_add (&ws->next, chunk);
  160. if (incr > 0)
  161. {
  162. if (tmp >= end)
  163. return false;
  164. nend = tmp + chunk;
  165. if (nend > end)
  166. nend = end;
  167. *pstart = tmp;
  168. *pend = nend;
  169. return true;
  170. }
  171. else
  172. {
  173. if (tmp <= end)
  174. return false;
  175. nend = tmp + chunk;
  176. if (nend < end)
  177. nend = end;
  178. *pstart = tmp;
  179. *pend = nend;
  180. return true;
  181. }
  182. }
  183. start = __atomic_load_n (&ws->next, MEMMODEL_RELAXED);
  184. while (1)
  185. {
  186. long left = end - start;
  187. long tmp;
  188. if (start == end)
  189. return false;
  190. if (incr < 0)
  191. {
  192. if (chunk < left)
  193. chunk = left;
  194. }
  195. else
  196. {
  197. if (chunk > left)
  198. chunk = left;
  199. }
  200. nend = start + chunk;
  201. tmp = __sync_val_compare_and_swap (&ws->next, start, nend);
  202. if (__builtin_expect (tmp == start, 1))
  203. break;
  204. start = tmp;
  205. }
  206. *pstart = start;
  207. *pend = nend;
  208. return true;
  209. }
  210. #endif /* HAVE_SYNC_BUILTINS */
  211. /* This function implements the GUIDED scheduling method. Arguments are
  212. as for gomp_iter_static_next. This function must be called with the
  213. work share lock held. */
  214. bool
  215. gomp_iter_guided_next_locked (long *pstart, long *pend)
  216. {
  217. struct gomp_thread *thr = gomp_thread ();
  218. struct gomp_work_share *ws = thr->ts.work_share;
  219. struct gomp_team *team = thr->ts.team;
  220. unsigned long nthreads = team ? team->nthreads : 1;
  221. unsigned long n, q;
  222. long start, end;
  223. if (ws->next == ws->end)
  224. return false;
  225. start = ws->next;
  226. n = (ws->end - start) / ws->incr;
  227. q = (n + nthreads - 1) / nthreads;
  228. if (q < ws->chunk_size)
  229. q = ws->chunk_size;
  230. if (q <= n)
  231. end = start + q * ws->incr;
  232. else
  233. end = ws->end;
  234. ws->next = end;
  235. *pstart = start;
  236. *pend = end;
  237. return true;
  238. }
  239. #ifdef HAVE_SYNC_BUILTINS
  240. /* Similar, but doesn't require the lock held, and uses compare-and-swap
  241. instead. Note that the only memory value that changes is ws->next. */
  242. bool
  243. gomp_iter_guided_next (long *pstart, long *pend)
  244. {
  245. struct gomp_thread *thr = gomp_thread ();
  246. struct gomp_work_share *ws = thr->ts.work_share;
  247. struct gomp_team *team = thr->ts.team;
  248. unsigned long nthreads = team ? team->nthreads : 1;
  249. long start, end, nend, incr;
  250. unsigned long chunk_size;
  251. start = __atomic_load_n (&ws->next, MEMMODEL_RELAXED);
  252. end = ws->end;
  253. incr = ws->incr;
  254. chunk_size = ws->chunk_size;
  255. while (1)
  256. {
  257. unsigned long n, q;
  258. long tmp;
  259. if (start == end)
  260. return false;
  261. n = (end - start) / incr;
  262. q = (n + nthreads - 1) / nthreads;
  263. if (q < chunk_size)
  264. q = chunk_size;
  265. if (__builtin_expect (q <= n, 1))
  266. nend = start + q * incr;
  267. else
  268. nend = end;
  269. tmp = __sync_val_compare_and_swap (&ws->next, start, nend);
  270. if (__builtin_expect (tmp == start, 1))
  271. break;
  272. start = tmp;
  273. }
  274. *pstart = start;
  275. *pend = nend;
  276. return true;
  277. }
  278. #endif /* HAVE_SYNC_BUILTINS */