icv.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 defines the OpenMP API entry points that operate on internal
  21. control variables. */
  22. #include "libgomp.h"
  23. #include "gomp-constants.h"
  24. #include <limits.h>
  25. ialias_redirect (omp_get_active_level)
  26. void
  27. omp_set_num_threads (int n)
  28. {
  29. struct gomp_task_icv *icv = gomp_icv (true);
  30. icv->nthreads_var = (n > 0 ? n : 1);
  31. }
  32. void
  33. omp_set_dynamic (int val)
  34. {
  35. struct gomp_task_icv *icv = gomp_icv (true);
  36. icv->dyn_var = val;
  37. }
  38. int
  39. omp_get_dynamic (void)
  40. {
  41. struct gomp_task_icv *icv = gomp_icv (false);
  42. return icv->dyn_var;
  43. }
  44. #pragma GCC diagnostic push
  45. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  46. void
  47. omp_set_nested (int val)
  48. {
  49. struct gomp_task_icv *icv = gomp_icv (true);
  50. if (val)
  51. icv->max_active_levels_var = gomp_supported_active_levels;
  52. else if (icv->max_active_levels_var > 1)
  53. icv->max_active_levels_var = 1;
  54. }
  55. int
  56. omp_get_nested (void)
  57. {
  58. struct gomp_task_icv *icv = gomp_icv (false);
  59. return (icv->max_active_levels_var > 1
  60. && icv->max_active_levels_var > omp_get_active_level ());
  61. }
  62. #pragma GCC diagnostic pop
  63. void
  64. omp_set_schedule (omp_sched_t kind, int chunk_size)
  65. {
  66. struct gomp_task_icv *icv = gomp_icv (true);
  67. switch (kind & ~omp_sched_monotonic)
  68. {
  69. case omp_sched_static:
  70. if (chunk_size < 1)
  71. chunk_size = 0;
  72. icv->run_sched_chunk_size = chunk_size;
  73. break;
  74. case omp_sched_dynamic:
  75. case omp_sched_guided:
  76. if (chunk_size < 1)
  77. chunk_size = 1;
  78. icv->run_sched_chunk_size = chunk_size;
  79. break;
  80. case omp_sched_auto:
  81. break;
  82. default:
  83. return;
  84. }
  85. icv->run_sched_var = kind;
  86. }
  87. void
  88. omp_get_schedule (omp_sched_t *kind, int *chunk_size)
  89. {
  90. struct gomp_task_icv *icv = gomp_icv (false);
  91. *kind = icv->run_sched_var;
  92. *chunk_size = icv->run_sched_chunk_size;
  93. }
  94. int
  95. omp_get_max_threads (void)
  96. {
  97. struct gomp_task_icv *icv = gomp_icv (false);
  98. return icv->nthreads_var;
  99. }
  100. int
  101. omp_get_thread_limit (void)
  102. {
  103. struct gomp_task_icv *icv = gomp_icv (false);
  104. return icv->thread_limit_var > INT_MAX ? INT_MAX : icv->thread_limit_var;
  105. }
  106. void
  107. omp_set_max_active_levels (int max_levels)
  108. {
  109. if (max_levels >= 0)
  110. {
  111. struct gomp_task_icv *icv = gomp_icv (true);
  112. if (max_levels <= gomp_supported_active_levels)
  113. icv->max_active_levels_var = max_levels;
  114. else
  115. icv->max_active_levels_var = gomp_supported_active_levels;
  116. }
  117. }
  118. int
  119. omp_get_max_active_levels (void)
  120. {
  121. struct gomp_task_icv *icv = gomp_icv (false);
  122. return icv->max_active_levels_var;
  123. }
  124. int
  125. omp_get_supported_active_levels (void)
  126. {
  127. return gomp_supported_active_levels;
  128. }
  129. void
  130. omp_set_num_teams (int num_teams)
  131. {
  132. if (num_teams >= 0)
  133. gomp_nteams_var = num_teams;
  134. }
  135. int
  136. omp_get_max_teams (void)
  137. {
  138. return gomp_nteams_var;
  139. }
  140. void
  141. omp_set_teams_thread_limit (int thread_limit)
  142. {
  143. if (thread_limit >= 0)
  144. gomp_teams_thread_limit_var = thread_limit;
  145. }
  146. int
  147. omp_get_teams_thread_limit (void)
  148. {
  149. return gomp_teams_thread_limit_var;
  150. }
  151. int
  152. omp_get_cancellation (void)
  153. {
  154. return gomp_cancel_var;
  155. }
  156. int
  157. omp_get_max_task_priority (void)
  158. {
  159. return gomp_max_task_priority_var;
  160. }
  161. omp_proc_bind_t
  162. omp_get_proc_bind (void)
  163. {
  164. struct gomp_task_icv *icv = gomp_icv (false);
  165. return icv->bind_var;
  166. }
  167. int
  168. omp_get_num_places (void)
  169. {
  170. return gomp_places_list_len;
  171. }
  172. int
  173. omp_get_place_num (void)
  174. {
  175. if (gomp_places_list == NULL)
  176. return -1;
  177. struct gomp_thread *thr = gomp_thread ();
  178. if (thr->place == 0)
  179. gomp_init_affinity ();
  180. return (int) thr->place - 1;
  181. }
  182. int
  183. omp_get_partition_num_places (void)
  184. {
  185. if (gomp_places_list == NULL)
  186. return 0;
  187. struct gomp_thread *thr = gomp_thread ();
  188. if (thr->place == 0)
  189. gomp_init_affinity ();
  190. return thr->ts.place_partition_len;
  191. }
  192. void
  193. omp_get_partition_place_nums (int *place_nums)
  194. {
  195. if (gomp_places_list == NULL)
  196. return;
  197. struct gomp_thread *thr = gomp_thread ();
  198. if (thr->place == 0)
  199. gomp_init_affinity ();
  200. unsigned int i;
  201. for (i = 0; i < thr->ts.place_partition_len; i++)
  202. *place_nums++ = thr->ts.place_partition_off + i;
  203. }
  204. void
  205. omp_set_default_allocator (omp_allocator_handle_t allocator)
  206. {
  207. struct gomp_thread *thr = gomp_thread ();
  208. if (allocator == omp_null_allocator)
  209. allocator = omp_default_mem_alloc;
  210. thr->ts.def_allocator = (uintptr_t) allocator;
  211. }
  212. omp_allocator_handle_t
  213. omp_get_default_allocator (void)
  214. {
  215. struct gomp_thread *thr = gomp_thread ();
  216. if (thr->ts.def_allocator == omp_null_allocator)
  217. return (omp_allocator_handle_t) gomp_def_allocator;
  218. else
  219. return (omp_allocator_handle_t) thr->ts.def_allocator;
  220. }
  221. ialias (omp_set_dynamic)
  222. ialias (omp_get_dynamic)
  223. #pragma GCC diagnostic push
  224. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  225. ialias (omp_set_nested)
  226. ialias (omp_get_nested)
  227. #pragma GCC diagnostic pop
  228. ialias (omp_set_num_threads)
  229. ialias (omp_set_schedule)
  230. ialias (omp_get_schedule)
  231. ialias (omp_get_max_threads)
  232. ialias (omp_get_thread_limit)
  233. ialias (omp_set_max_active_levels)
  234. ialias (omp_get_max_active_levels)
  235. ialias (omp_get_supported_active_levels)
  236. ialias (omp_set_num_teams)
  237. ialias (omp_get_max_teams)
  238. ialias (omp_set_teams_thread_limit)
  239. ialias (omp_get_teams_thread_limit)
  240. ialias (omp_get_cancellation)
  241. ialias (omp_get_proc_bind)
  242. ialias (omp_get_max_task_priority)
  243. ialias (omp_get_num_places)
  244. ialias (omp_get_place_num)
  245. ialias (omp_get_partition_num_places)
  246. ialias (omp_get_partition_place_nums)
  247. ialias (omp_set_default_allocator)
  248. ialias (omp_get_default_allocator)