parallel_for_01.c 5.3 KB

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