parallel_for_ordered_01.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014,2015,2017,2019 CNRS
  4. * Copyright (C) 2014,2016 Inria
  5. * Copyright (C) 2017 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 <pthread.h>
  19. #include <starpu.h>
  20. #include "../helper.h"
  21. #include <stdio.h>
  22. /*
  23. * Check the OpenMP ordered parallel for support.
  24. */
  25. #if !defined(STARPU_OPENMP)
  26. int main(void)
  27. {
  28. return STARPU_TEST_SKIPPED;
  29. }
  30. #else
  31. #define NB_ITERS 256
  32. #define CHUNK 16
  33. unsigned long long array[NB_ITERS];
  34. __attribute__((constructor))
  35. static void omp_constructor(void)
  36. {
  37. int ret = starpu_omp_init();
  38. if (ret == -EINVAL) exit(STARPU_TEST_SKIPPED);
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_omp_init");
  40. }
  41. __attribute__((destructor))
  42. static void omp_destructor(void)
  43. {
  44. starpu_omp_shutdown();
  45. }
  46. struct s_ordered_arg
  47. {
  48. const char *msg;
  49. unsigned long long i;
  50. };
  51. void ordered_f(void *_arg)
  52. {
  53. struct s_ordered_arg *arg = _arg;
  54. int worker_id;
  55. pthread_t tid;
  56. tid = pthread_self();
  57. worker_id = starpu_worker_get_id();
  58. printf("[tid %p] task thread = %d, for [%s] iteration (ordered) %llu\n", (void *)tid, worker_id, arg->msg, arg->i);
  59. }
  60. void for_g(unsigned long long i, unsigned long long nb_i, void *arg)
  61. {
  62. int worker_id;
  63. pthread_t tid;
  64. tid = pthread_self();
  65. worker_id = starpu_worker_get_id();
  66. printf("[tid %p] task thread = %d, for [%s] iterations first=%llu:nb=%llu\n", (void *)tid, worker_id, (const char *)arg, i, nb_i);
  67. for (; nb_i > 0; i++, nb_i--)
  68. {
  69. struct s_ordered_arg ordered_arg = { arg, i };
  70. array[i] = 1;
  71. starpu_omp_ordered(ordered_f, &ordered_arg);
  72. }
  73. }
  74. void parallel_region_1_f(void *buffers[], void *args)
  75. {
  76. (void) buffers;
  77. (void) args;
  78. int worker_id;
  79. pthread_t tid;
  80. tid = pthread_self();
  81. worker_id = starpu_worker_get_id();
  82. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  83. starpu_omp_for(for_g, (void*)"static chunk", NB_ITERS, CHUNK, starpu_omp_sched_static, 1, 0);
  84. }
  85. void parallel_region_2_f(void *buffers[], void *args)
  86. {
  87. (void) buffers;
  88. (void) args;
  89. int worker_id;
  90. pthread_t tid;
  91. tid = pthread_self();
  92. worker_id = starpu_worker_get_id();
  93. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  94. starpu_omp_for(for_g, (void*)"static nochunk", NB_ITERS, 0, starpu_omp_sched_static, 1, 0);
  95. }
  96. void parallel_region_3_f(void *buffers[], void *args)
  97. {
  98. (void) buffers;
  99. (void) args;
  100. int worker_id;
  101. pthread_t tid;
  102. tid = pthread_self();
  103. worker_id = starpu_worker_get_id();
  104. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  105. starpu_omp_for(for_g, (void*)"dynamic chunk", NB_ITERS, CHUNK, starpu_omp_sched_dynamic, 1, 0);
  106. }
  107. void parallel_region_4_f(void *buffers[], void *args)
  108. {
  109. (void) buffers;
  110. (void) args;
  111. int worker_id;
  112. pthread_t tid;
  113. tid = pthread_self();
  114. worker_id = starpu_worker_get_id();
  115. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  116. starpu_omp_for(for_g, (void*)"dynamic nochunk", NB_ITERS, 0, starpu_omp_sched_dynamic, 1, 0);
  117. }
  118. void parallel_region_5_f(void *buffers[], void *args)
  119. {
  120. (void) buffers;
  121. (void) args;
  122. int worker_id;
  123. pthread_t tid;
  124. tid = pthread_self();
  125. worker_id = starpu_worker_get_id();
  126. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  127. starpu_omp_for(for_g, (void*)"guided nochunk", NB_ITERS, 0, starpu_omp_sched_guided, 1, 0);
  128. }
  129. void parallel_region_6_f(void *buffers[], void *args)
  130. {
  131. (void) buffers;
  132. (void) args;
  133. int worker_id;
  134. pthread_t tid;
  135. tid = pthread_self();
  136. worker_id = starpu_worker_get_id();
  137. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  138. starpu_omp_for(for_g, (void*)"guided nochunk", NB_ITERS, 0, starpu_omp_sched_guided, 1, 0);
  139. }
  140. static void clear_array(void)
  141. {
  142. memset(array, 0, NB_ITERS*sizeof(unsigned long long));
  143. }
  144. static void check_array(void)
  145. {
  146. unsigned long long i;
  147. unsigned long long s = 0;
  148. for (i = 0; i < NB_ITERS; i++)
  149. {
  150. s += array[i];
  151. }
  152. if (s != NB_ITERS)
  153. {
  154. printf("missing iterations\n");
  155. exit(1);
  156. }
  157. }
  158. int
  159. main (void)
  160. {
  161. struct starpu_omp_parallel_region_attr attr;
  162. memset(&attr, 0, sizeof(attr));
  163. #ifdef STARPU_SIMGRID
  164. attr.cl.model = &starpu_perfmodel_nop;
  165. #endif
  166. attr.cl.flags = STARPU_CODELET_SIMGRID_EXECUTE;
  167. attr.cl.where = STARPU_CPU;
  168. attr.if_clause = 1;
  169. clear_array();
  170. attr.cl.cpu_funcs[0] = parallel_region_1_f;
  171. starpu_omp_parallel_region(&attr);
  172. check_array();
  173. clear_array();
  174. attr.cl.cpu_funcs[0] = parallel_region_2_f;
  175. starpu_omp_parallel_region(&attr);
  176. check_array();
  177. clear_array();
  178. attr.cl.cpu_funcs[0] = parallel_region_3_f;
  179. starpu_omp_parallel_region(&attr);
  180. check_array();
  181. clear_array();
  182. attr.cl.cpu_funcs[0] = parallel_region_4_f;
  183. starpu_omp_parallel_region(&attr);
  184. check_array();
  185. clear_array();
  186. attr.cl.cpu_funcs[0] = parallel_region_5_f;
  187. starpu_omp_parallel_region(&attr);
  188. check_array();
  189. clear_array();
  190. attr.cl.cpu_funcs[0] = parallel_region_6_f;
  191. starpu_omp_parallel_region(&attr);
  192. check_array();
  193. return 0;
  194. }
  195. #endif