openmp_runtime_support_omp_api.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. static int _starpu_omp_get_region_thread_num(const struct starpu_omp_region * const region)
  39. {
  40. struct starpu_omp_thread *thread = _starpu_omp_get_thread();
  41. STARPU_ASSERT(thread != NULL);
  42. if (thread == region->master_thread)
  43. return 0;
  44. int tid = starpu_omp_thread_list_member(&region->thread_list, thread);
  45. if (tid >= 0)
  46. return tid+1;
  47. _STARPU_ERROR("unrecognized omp thread\n");
  48. }
  49. int starpu_omp_get_thread_num()
  50. {
  51. struct starpu_omp_task *task = _starpu_omp_get_task();
  52. if (task == NULL)
  53. return 0;
  54. return _starpu_omp_get_region_thread_num(task->owner_region);
  55. }
  56. int starpu_omp_get_max_threads()
  57. {
  58. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  59. int max_threads = parallel_region->icvs.nthreads_var[0];
  60. /* TODO: for now, nested parallel sections are not supported, thus we
  61. * open an active parallel section only if the generating region is the
  62. * initial region */
  63. if (parallel_region->level > 0)
  64. {
  65. max_threads = 1;
  66. }
  67. return max_threads;
  68. }
  69. int starpu_omp_get_num_procs (void)
  70. {
  71. /* starpu_cpu_worker_get_count defined as topology.ncpus */
  72. return starpu_cpu_worker_get_count();
  73. }
  74. int starpu_omp_in_parallel (void)
  75. {
  76. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  77. return parallel_region->icvs.active_levels_var > 0;
  78. }
  79. void starpu_omp_set_dynamic (int dynamic_threads)
  80. {
  81. (void) dynamic_threads;
  82. /* TODO: dynamic adjustment of the number of threads is not supported for now */
  83. }
  84. int starpu_omp_get_dynamic (void)
  85. {
  86. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  87. return parallel_region->icvs.dyn_var;
  88. }
  89. void starpu_omp_set_nested (int nested)
  90. {
  91. (void) nested;
  92. /* TODO: nested parallelism not supported for now */
  93. }
  94. int starpu_omp_get_nested (void)
  95. {
  96. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  97. return parallel_region->icvs.nest_var;
  98. }
  99. int starpu_omp_get_cancellation(void)
  100. {
  101. return _starpu_omp_global_state->icvs.cancel_var;
  102. }
  103. void starpu_omp_set_schedule (enum starpu_omp_sched_value kind, int modifier)
  104. {
  105. struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  106. STARPU_ASSERT( kind == starpu_omp_sched_static
  107. || kind == starpu_omp_sched_dynamic
  108. || kind == starpu_omp_sched_guided
  109. || kind == starpu_omp_sched_auto);
  110. STARPU_ASSERT(modifier >= 0);
  111. parallel_region->icvs.run_sched_var = kind;
  112. parallel_region->icvs.run_sched_chunk_var = (unsigned long long)modifier;
  113. }
  114. void starpu_omp_get_schedule (enum starpu_omp_sched_value *kind, int *modifier)
  115. {
  116. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  117. *kind = parallel_region->icvs.run_sched_var;
  118. *modifier = (int)parallel_region->icvs.run_sched_chunk_var;
  119. }
  120. int starpu_omp_get_thread_limit (void)
  121. {
  122. return starpu_cpu_worker_get_count();
  123. }
  124. void starpu_omp_set_max_active_levels (int max_levels)
  125. {
  126. struct starpu_omp_device * const device = _starpu_omp_get_task()->owner_region->owner_device;
  127. if (max_levels > 1)
  128. {
  129. /* TODO: nested parallelism not supported for now */
  130. max_levels = 1;
  131. }
  132. device->icvs.max_active_levels_var = max_levels;
  133. }
  134. int starpu_omp_get_max_active_levels (void)
  135. {
  136. const struct starpu_omp_device * const device = _starpu_omp_get_task()->owner_region->owner_device;
  137. return device->icvs.max_active_levels_var;
  138. }
  139. int starpu_omp_get_level (void)
  140. {
  141. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  142. return parallel_region->icvs.levels_var;
  143. }
  144. int starpu_omp_get_ancestor_thread_num (int level)
  145. {
  146. if (level == 0)
  147. return 0;
  148. const struct starpu_omp_task *task = _starpu_omp_get_task();
  149. if (task == NULL)
  150. return -1;
  151. const struct starpu_omp_region *parallel_region = task->owner_region;
  152. if (level < 0 || level > parallel_region->icvs.levels_var)
  153. return -1;
  154. while (level < parallel_region->icvs.levels_var)
  155. {
  156. parallel_region = parallel_region->parent_region;
  157. }
  158. return _starpu_omp_get_region_thread_num(parallel_region);
  159. }
  160. int starpu_omp_get_team_size (int level)
  161. {
  162. if (level == 0)
  163. return 1;
  164. const struct starpu_omp_task *task = _starpu_omp_get_task();
  165. if (task == NULL)
  166. return -1;
  167. const struct starpu_omp_region *parallel_region = task->owner_region;
  168. if (level < 0 || level > parallel_region->icvs.levels_var)
  169. return -1;
  170. while (level < parallel_region->icvs.levels_var)
  171. {
  172. parallel_region = parallel_region->parent_region;
  173. }
  174. return parallel_region->nb_threads;
  175. }
  176. int starpu_omp_get_active_level (void)
  177. {
  178. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  179. return parallel_region->icvs.active_levels_var;
  180. }
  181. int starpu_omp_in_final(void)
  182. {
  183. const struct starpu_omp_task *task = _starpu_omp_get_task();
  184. return task->flags & STARPU_OMP_TASK_FLAGS_FINAL;
  185. }
  186. enum starpu_omp_proc_bind_value starpu_omp_get_proc_bind(void)
  187. {
  188. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  189. int proc_bind = parallel_region->icvs.bind_var[0];
  190. return proc_bind;
  191. }
  192. int starpu_omp_get_num_places(void)
  193. {
  194. struct starpu_omp_place *places = &_starpu_omp_initial_icv_values->places;
  195. return places->nb_numeric_places;
  196. }
  197. int starpu_omp_get_place_num_procs(int place_num)
  198. {
  199. /* TODO */
  200. return 0;
  201. }
  202. void starpu_omp_get_place_proc_ids(int place_num, int *ids)
  203. {
  204. /* TODO */
  205. }
  206. int starpu_omp_get_place_num(void)
  207. {
  208. /* TODO */
  209. return -1;
  210. }
  211. int starpu_omp_get_partition_num_places(void)
  212. {
  213. /* TODO */
  214. return 0;
  215. }
  216. void starpu_omp_get_partition_place_nums(int *place_nums)
  217. {
  218. /* TODO */
  219. }
  220. void starpu_omp_set_default_device(int device_num)
  221. {
  222. (void) device_num;
  223. /* TODO: set_default_device not supported for now */
  224. }
  225. int starpu_omp_get_default_device(void)
  226. {
  227. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  228. return parallel_region->icvs.default_device_var;
  229. }
  230. int starpu_omp_get_num_devices(void)
  231. {
  232. /* TODO: get_num_devices not supported for now
  233. * assume 1 device */
  234. return 1;
  235. }
  236. int starpu_omp_get_num_teams(void)
  237. {
  238. /* TODO: num_teams not supported for now
  239. * assume 1 team */
  240. return 1;
  241. }
  242. int starpu_omp_get_team_num(void)
  243. {
  244. /* TODO: team_num not supported for now
  245. * assume team_num 0 */
  246. return 0;
  247. }
  248. int starpu_omp_is_initial_device(void)
  249. {
  250. const struct starpu_omp_device * const device = _starpu_omp_get_task()->owner_region->owner_device;
  251. return device == _starpu_omp_global_state->initial_device;
  252. }
  253. int starpu_omp_get_initial_device(void)
  254. {
  255. /* Assume only one device for now. */
  256. return 0;
  257. }
  258. int starpu_omp_get_max_task_priority(void)
  259. {
  260. const struct starpu_omp_region * const parallel_region = _starpu_omp_get_task()->owner_region;
  261. return parallel_region->icvs.max_task_priority_var;
  262. }
  263. double starpu_omp_get_wtime (void)
  264. {
  265. return 1e-6 * (starpu_timing_now() - _starpu_omp_clock_ref);
  266. }
  267. double starpu_omp_get_wtick (void)
  268. {
  269. /* arbitrary precision value */
  270. return 1e-6;
  271. }
  272. #endif /* STARPU_OPENMP */