openmp_runtime_support_omp_api.c 7.4 KB

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