parallel.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 handles the (bare) PARALLEL construct. */
  21. #include "libgomp.h"
  22. #include <limits.h>
  23. /* Determine the number of threads to be launched for a PARALLEL construct.
  24. This algorithm is explicitly described in OpenMP 3.0 section 2.4.1.
  25. SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
  26. If the IF clause is false, SPECIFIED is forced to 1. When NUM_THREADS
  27. is not present, SPECIFIED is 0. */
  28. unsigned
  29. gomp_resolve_num_threads (unsigned specified, unsigned count)
  30. {
  31. struct gomp_thread *thr = gomp_thread ();
  32. struct gomp_task_icv *icv;
  33. unsigned threads_requested, max_num_threads, num_threads;
  34. unsigned long busy;
  35. struct gomp_thread_pool *pool;
  36. icv = gomp_icv (false);
  37. if (specified == 1)
  38. return 1;
  39. else if (thr->ts.active_level >= 1 && !icv->nest_var)
  40. return 1;
  41. else if (thr->ts.active_level >= gomp_max_active_levels_var)
  42. return 1;
  43. /* If NUM_THREADS not specified, use nthreads_var. */
  44. if (specified == 0)
  45. threads_requested = icv->nthreads_var;
  46. else
  47. threads_requested = specified;
  48. max_num_threads = threads_requested;
  49. /* If dynamic threads are enabled, bound the number of threads
  50. that we launch. */
  51. if (icv->dyn_var)
  52. {
  53. unsigned dyn = gomp_dynamic_max_threads ();
  54. if (dyn < max_num_threads)
  55. max_num_threads = dyn;
  56. /* Optimization for parallel sections. */
  57. if (count && count < max_num_threads)
  58. max_num_threads = count;
  59. }
  60. /* UINT_MAX stands for infinity. */
  61. if (__builtin_expect (icv->thread_limit_var == UINT_MAX, 1)
  62. || max_num_threads == 1)
  63. return max_num_threads;
  64. /* The threads_busy counter lives in thread_pool, if there
  65. isn't a thread_pool yet, there must be just one thread
  66. in the contention group. If thr->team is NULL, this isn't
  67. nested parallel, so there is just one thread in the
  68. contention group as well, no need to handle it atomically. */
  69. pool = thr->thread_pool;
  70. if (thr->ts.team == NULL)
  71. {
  72. num_threads = max_num_threads;
  73. if (num_threads > icv->thread_limit_var)
  74. num_threads = icv->thread_limit_var;
  75. if (pool)
  76. pool->threads_busy = num_threads;
  77. return num_threads;
  78. }
  79. #ifdef HAVE_SYNC_BUILTINS
  80. do
  81. {
  82. busy = pool->threads_busy;
  83. num_threads = max_num_threads;
  84. if (icv->thread_limit_var - busy + 1 < num_threads)
  85. num_threads = icv->thread_limit_var - busy + 1;
  86. }
  87. while (__sync_val_compare_and_swap (&pool->threads_busy,
  88. busy, busy + num_threads - 1)
  89. != busy);
  90. #else
  91. gomp_mutex_lock (&gomp_managed_threads_lock);
  92. num_threads = max_num_threads;
  93. busy = pool->threads_busy;
  94. if (icv->thread_limit_var - busy + 1 < num_threads)
  95. num_threads = icv->thread_limit_var - busy + 1;
  96. pool->threads_busy += num_threads - 1;
  97. gomp_mutex_unlock (&gomp_managed_threads_lock);
  98. #endif
  99. return num_threads;
  100. }
  101. void
  102. GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
  103. {
  104. num_threads = gomp_resolve_num_threads (num_threads, 0);
  105. gomp_team_start (fn, data, num_threads, 0, gomp_new_team (num_threads));
  106. }
  107. void
  108. GOMP_parallel_end (void)
  109. {
  110. struct gomp_task_icv *icv = gomp_icv (false);
  111. if (__builtin_expect (icv->thread_limit_var != UINT_MAX, 0))
  112. {
  113. struct gomp_thread *thr = gomp_thread ();
  114. struct gomp_team *team = thr->ts.team;
  115. unsigned int nthreads = team ? team->nthreads : 1;
  116. gomp_team_end ();
  117. if (nthreads > 1)
  118. {
  119. /* If not nested, there is just one thread in the
  120. contention group left, no need for atomicity. */
  121. if (thr->ts.team == NULL)
  122. thr->thread_pool->threads_busy = 1;
  123. else
  124. {
  125. #ifdef HAVE_SYNC_BUILTINS
  126. __sync_fetch_and_add (&thr->thread_pool->threads_busy,
  127. 1UL - nthreads);
  128. #else
  129. gomp_mutex_lock (&gomp_managed_threads_lock);
  130. thr->thread_pool->threads_busy -= nthreads - 1;
  131. gomp_mutex_unlock (&gomp_managed_threads_lock);
  132. #endif
  133. }
  134. }
  135. }
  136. else
  137. gomp_team_end ();
  138. }
  139. ialias (GOMP_parallel_end)
  140. void
  141. GOMP_parallel (void (*fn) (void *), void *data, unsigned num_threads, unsigned int flags)
  142. {
  143. num_threads = gomp_resolve_num_threads (num_threads, 0);
  144. gomp_team_start (fn, data, num_threads, flags, gomp_new_team (num_threads));
  145. fn (data);
  146. ialias_call (GOMP_parallel_end) ();
  147. }
  148. bool
  149. GOMP_cancellation_point (int which)
  150. {
  151. if (!gomp_cancel_var)
  152. return false;
  153. struct gomp_thread *thr = gomp_thread ();
  154. struct gomp_team *team = thr->ts.team;
  155. if (which & (GOMP_CANCEL_LOOP | GOMP_CANCEL_SECTIONS))
  156. {
  157. if (team == NULL)
  158. return false;
  159. return team->work_share_cancelled != 0;
  160. }
  161. else if (which & GOMP_CANCEL_TASKGROUP)
  162. {
  163. if (thr->task->taskgroup && thr->task->taskgroup->cancelled)
  164. return true;
  165. /* FALLTHRU into the GOMP_CANCEL_PARALLEL case,
  166. as #pragma omp cancel parallel also cancels all explicit
  167. tasks. */
  168. }
  169. if (team)
  170. return gomp_team_barrier_cancelled (&team->barrier);
  171. return false;
  172. }
  173. ialias (GOMP_cancellation_point)
  174. bool
  175. GOMP_cancel (int which, bool do_cancel)
  176. {
  177. if (!gomp_cancel_var)
  178. return false;
  179. if (!do_cancel)
  180. return ialias_call (GOMP_cancellation_point) (which);
  181. struct gomp_thread *thr = gomp_thread ();
  182. struct gomp_team *team = thr->ts.team;
  183. if (which & (GOMP_CANCEL_LOOP | GOMP_CANCEL_SECTIONS))
  184. {
  185. /* In orphaned worksharing region, all we want to cancel
  186. is current thread. */
  187. if (team != NULL)
  188. team->work_share_cancelled = 1;
  189. return true;
  190. }
  191. else if (which & GOMP_CANCEL_TASKGROUP)
  192. {
  193. if (thr->task->taskgroup && !thr->task->taskgroup->cancelled)
  194. {
  195. gomp_mutex_lock (&team->task_lock);
  196. thr->task->taskgroup->cancelled = true;
  197. gomp_mutex_unlock (&team->task_lock);
  198. }
  199. return true;
  200. }
  201. team->team_cancelled = 1;
  202. gomp_team_barrier_cancel (team);
  203. return true;
  204. }
  205. /* The public OpenMP API for thread and team related inquiries. */
  206. int
  207. omp_get_num_threads (void)
  208. {
  209. struct gomp_team *team = gomp_thread ()->ts.team;
  210. return team ? team->nthreads : 1;
  211. }
  212. int
  213. omp_get_thread_num (void)
  214. {
  215. return gomp_thread ()->ts.team_id;
  216. }
  217. /* This wasn't right for OpenMP 2.5. Active region used to be non-zero
  218. when the IF clause doesn't evaluate to false, starting with OpenMP 3.0
  219. it is non-zero with more than one thread in the team. */
  220. int
  221. omp_in_parallel (void)
  222. {
  223. return gomp_thread ()->ts.active_level > 0;
  224. }
  225. int
  226. omp_get_level (void)
  227. {
  228. return gomp_thread ()->ts.level;
  229. }
  230. int
  231. omp_get_ancestor_thread_num (int level)
  232. {
  233. struct gomp_team_state *ts = &gomp_thread ()->ts;
  234. if (level < 0 || level > ts->level)
  235. return -1;
  236. for (level = ts->level - level; level > 0; --level)
  237. ts = &ts->team->prev_ts;
  238. return ts->team_id;
  239. }
  240. int
  241. omp_get_team_size (int level)
  242. {
  243. struct gomp_team_state *ts = &gomp_thread ()->ts;
  244. if (level < 0 || level > ts->level)
  245. return -1;
  246. for (level = ts->level - level; level > 0; --level)
  247. ts = &ts->team->prev_ts;
  248. if (ts->team == NULL)
  249. return 1;
  250. else
  251. return ts->team->nthreads;
  252. }
  253. int
  254. omp_get_active_level (void)
  255. {
  256. return gomp_thread ()->ts.active_level;
  257. }
  258. ialias (omp_get_num_threads)
  259. ialias (omp_get_thread_num)
  260. ialias (omp_in_parallel)
  261. ialias (omp_get_level)
  262. ialias (omp_get_ancestor_thread_num)
  263. ialias (omp_get_team_size)
  264. ialias (omp_get_active_level)