parallel_for_ordered_01.c 5.4 KB

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