openmp_runtime_support_omp_api.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2015,2017 CNRS
  4. * Copyright (C) 2014-2016 Inria
  5. * Copyright (C) 2015 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #ifdef STARPU_OPENMP
  20. #include <util/openmp_runtime_support.h>
  21. void starpu_omp_set_num_threads(int threads)
  22. {
  23. STARPU_ASSERT(threads > 0);
  24. struct starpu_omp_task *task = _starpu_omp_get_task();
  25. STARPU_ASSERT(task != NULL);
  26. struct starpu_omp_region *region;
  27. region = task->owner_region;
  28. STARPU_ASSERT(region != NULL);
  29. region->icvs.nthreads_var[0] = threads;
  30. }
  31. int starpu_omp_get_num_threads()
  32. {
  33. struct starpu_omp_task *task = _starpu_omp_get_task();
  34. struct starpu_omp_region *region;
  35. if (task == NULL)
  36. return 1;
  37. region = task->owner_region;
  38. return region->nb_threads;
  39. }
  40. int starpu_omp_get_thread_num()
  41. {
  42. struct starpu_omp_task *task = _starpu_omp_get_task();
  43. if (task == NULL)
  44. return 0;
  45. return _starpu_omp_get_region_thread_num(task->owner_region);
  46. }
  47. int starpu_omp_get_max_threads()
  48. {
  49. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  50. int max_threads = parallel_region->icvs.nthreads_var[0];
  51. /* TODO: for now, nested parallel sections are not supported, thus we
  52. * open an active parallel section only if the generating region is the
  53. * initial region */
  54. if (parallel_region->level > 0)
  55. {
  56. max_threads = 1;
  57. }
  58. return max_threads;
  59. }
  60. int starpu_omp_get_num_procs (void)
  61. {
  62. /* starpu_cpu_worker_get_count defined as topology.ncpus */
  63. return starpu_cpu_worker_get_count();
  64. }
  65. int starpu_omp_in_parallel (void)
  66. {
  67. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  68. return parallel_region->icvs.active_levels_var > 0;
  69. }
  70. void starpu_omp_set_dynamic (int dynamic_threads)
  71. {
  72. (void) dynamic_threads;
  73. /* TODO: dynamic adjustment of the number of threads is not supported for now */
  74. }
  75. int starpu_omp_get_dynamic (void)
  76. {
  77. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  78. return parallel_region->icvs.dyn_var;
  79. }
  80. void starpu_omp_set_nested (int nested)
  81. {
  82. (void) nested;
  83. /* TODO: nested parallelism not supported for now */
  84. }
  85. int starpu_omp_get_nested (void)
  86. {
  87. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  88. return parallel_region->icvs.nest_var;
  89. }
  90. int starpu_omp_get_cancellation(void)
  91. {
  92. return _starpu_omp_global_state->icvs.cancel_var;
  93. }
  94. void starpu_omp_set_schedule (enum starpu_omp_sched_value kind, int modifier)
  95. {
  96. struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  97. STARPU_ASSERT( kind == starpu_omp_sched_static
  98. || kind == starpu_omp_sched_dynamic
  99. || kind == starpu_omp_sched_guided
  100. || kind == starpu_omp_sched_auto);
  101. STARPU_ASSERT(modifier >= 0);
  102. parallel_region->icvs.run_sched_var = kind;
  103. parallel_region->icvs.run_sched_chunk_var = (unsigned long long)modifier;
  104. }
  105. void starpu_omp_get_schedule (enum starpu_omp_sched_value *kind, int *modifier)
  106. {
  107. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  108. *kind = parallel_region->icvs.run_sched_var;
  109. *modifier = (int)parallel_region->icvs.run_sched_chunk_var;
  110. }
  111. int starpu_omp_get_thread_limit (void)
  112. {
  113. return starpu_cpu_worker_get_count();
  114. }
  115. void starpu_omp_set_max_active_levels (int max_levels)
  116. {
  117. struct starpu_omp_device * const device = _starpu_omp_get_task()->owner_region->owner_device;
  118. if (max_levels > 1)
  119. {
  120. /* TODO: nested parallelism not supported for now */
  121. max_levels = 1;
  122. }
  123. device->icvs.max_active_levels_var = max_levels;
  124. }
  125. int starpu_omp_get_max_active_levels (void)
  126. {
  127. const struct starpu_omp_device * const device = _starpu_omp_get_task()->owner_region->owner_device;
  128. return device->icvs.max_active_levels_var;
  129. }
  130. int starpu_omp_get_level (void)
  131. {
  132. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  133. return parallel_region->icvs.levels_var;
  134. }
  135. int starpu_omp_get_ancestor_thread_num (int level)
  136. {
  137. struct starpu_omp_region *parallel_region;
  138. if (level == 0)
  139. return 0;
  140. parallel_region = _starpu_omp_get_region_at_level(level);
  141. if (!parallel_region)
  142. return -1;
  143. return _starpu_omp_get_region_thread_num(parallel_region);
  144. }
  145. int starpu_omp_get_team_size (int level)
  146. {
  147. struct starpu_omp_region *parallel_region;
  148. if (level == 0)
  149. return 1;
  150. parallel_region = _starpu_omp_get_region_at_level(level);
  151. if (!parallel_region)
  152. return -1;
  153. return parallel_region->nb_threads;
  154. }
  155. int starpu_omp_get_active_level (void)
  156. {
  157. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  158. return parallel_region->icvs.active_levels_var;
  159. }
  160. int starpu_omp_in_final(void)
  161. {
  162. const struct starpu_omp_task *task = _starpu_omp_get_task();
  163. return task->flags & STARPU_OMP_TASK_FLAGS_FINAL;
  164. }
  165. enum starpu_omp_proc_bind_value starpu_omp_get_proc_bind(void)
  166. {
  167. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  168. int proc_bind = parallel_region->icvs.bind_var[0];
  169. return proc_bind;
  170. }
  171. int starpu_omp_get_num_places(void)
  172. {
  173. struct starpu_omp_place *places = &_starpu_omp_initial_icv_values->places;
  174. return places->nb_numeric_places;
  175. }
  176. int starpu_omp_get_place_num_procs(int place_num)
  177. {
  178. /* TODO */
  179. return 0;
  180. }
  181. void starpu_omp_get_place_proc_ids(int place_num, int *ids)
  182. {
  183. /* TODO */
  184. }
  185. int starpu_omp_get_place_num(void)
  186. {
  187. /* TODO */
  188. return -1;
  189. }
  190. int starpu_omp_get_partition_num_places(void)
  191. {
  192. /* TODO */
  193. return 0;
  194. }
  195. void starpu_omp_get_partition_place_nums(int *place_nums)
  196. {
  197. /* TODO */
  198. }
  199. void starpu_omp_set_default_device(int device_num)
  200. {
  201. (void) device_num;
  202. /* TODO: set_default_device not supported for now */
  203. }
  204. int starpu_omp_get_default_device(void)
  205. {
  206. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  207. return parallel_region->icvs.default_device_var;
  208. }
  209. int starpu_omp_get_num_devices(void)
  210. {
  211. /* TODO: get_num_devices not supported for now
  212. * assume 1 device */
  213. return 1;
  214. }
  215. int starpu_omp_get_num_teams(void)
  216. {
  217. /* TODO: num_teams not supported for now
  218. * assume 1 team */
  219. return 1;
  220. }
  221. int starpu_omp_get_team_num(void)
  222. {
  223. /* TODO: team_num not supported for now
  224. * assume team_num 0 */
  225. return 0;
  226. }
  227. int starpu_omp_is_initial_device(void)
  228. {
  229. struct starpu_omp_task *task = _starpu_omp_get_task();
  230. if (!task)
  231. return 0;
  232. const struct starpu_omp_device * const device = task->owner_region->owner_device;
  233. return device == _starpu_omp_global_state->initial_device;
  234. }
  235. int starpu_omp_get_initial_device(void)
  236. {
  237. /* Assume only one device for now. */
  238. return 0;
  239. }
  240. int starpu_omp_get_max_task_priority(void)
  241. {
  242. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  243. return parallel_region->icvs.max_task_priority_var;
  244. }
  245. double starpu_omp_get_wtime (void)
  246. {
  247. return 1e-6 * (starpu_timing_now() - _starpu_omp_clock_ref);
  248. }
  249. double starpu_omp_get_wtick (void)
  250. {
  251. /* arbitrary precision value */
  252. return 1e-6;
  253. }
  254. #endif /* STARPU_OPENMP */