parallel_for_01.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 parallel for support, with multiple schedule and chunk settings.
  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. void for_g(unsigned long long i, unsigned long long nb_i, void *arg)
  47. {
  48. int worker_id;
  49. pthread_t tid;
  50. tid = pthread_self();
  51. worker_id = starpu_worker_get_id();
  52. printf("[tid %p] task thread = %d, for [%s] iterations first=%llu:nb=%llu\n", (void *)tid, worker_id, (const char *)arg, i, nb_i);
  53. for (; nb_i > 0; i++, nb_i--)
  54. {
  55. array[i] = 1;
  56. }
  57. }
  58. void parallel_region_1_f(void *buffers[], void *args)
  59. {
  60. (void) buffers;
  61. (void) args;
  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\n", (void *)tid, worker_id);
  67. starpu_omp_for(for_g, (void*)"static chunk", NB_ITERS, CHUNK, starpu_omp_sched_static, 0, 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. void parallel_region_3_f(void *buffers[], void *args)
  81. {
  82. (void) buffers;
  83. (void) args;
  84. int worker_id;
  85. pthread_t tid;
  86. tid = pthread_self();
  87. worker_id = starpu_worker_get_id();
  88. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  89. starpu_omp_for(for_g, (void*)"dynamic chunk", NB_ITERS, CHUNK, starpu_omp_sched_dynamic, 0, 0);
  90. }
  91. void parallel_region_4_f(void *buffers[], void *args)
  92. {
  93. (void) buffers;
  94. (void) args;
  95. int worker_id;
  96. pthread_t tid;
  97. tid = pthread_self();
  98. worker_id = starpu_worker_get_id();
  99. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  100. starpu_omp_for(for_g, (void*)"dynamic nochunk", NB_ITERS, 0, starpu_omp_sched_dynamic, 0, 0);
  101. }
  102. void parallel_region_5_f(void *buffers[], void *args)
  103. {
  104. (void) buffers;
  105. (void) args;
  106. int worker_id;
  107. pthread_t tid;
  108. tid = pthread_self();
  109. worker_id = starpu_worker_get_id();
  110. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  111. starpu_omp_for(for_g, (void*)"guided nochunk", NB_ITERS, 0, starpu_omp_sched_guided, 0, 0);
  112. }
  113. void parallel_region_6_f(void *buffers[], void *args)
  114. {
  115. (void) buffers;
  116. (void) args;
  117. int worker_id;
  118. pthread_t tid;
  119. tid = pthread_self();
  120. worker_id = starpu_worker_get_id();
  121. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  122. starpu_omp_for(for_g, (void*)"guided nochunk", NB_ITERS, 0, starpu_omp_sched_guided, 0, 0);
  123. }
  124. static void clear_array(void)
  125. {
  126. memset(array, 0, NB_ITERS*sizeof(unsigned long long));
  127. }
  128. static void check_array(void)
  129. {
  130. unsigned long long i;
  131. unsigned long long s = 0;
  132. for (i = 0; i < NB_ITERS; i++)
  133. {
  134. s += array[i];
  135. }
  136. if (s != NB_ITERS)
  137. {
  138. printf("missing iterations\n");
  139. exit(1);
  140. }
  141. }
  142. int
  143. main (void)
  144. {
  145. struct starpu_omp_parallel_region_attr attr;
  146. memset(&attr, 0, sizeof(attr));
  147. #ifdef STARPU_SIMGRID
  148. attr.cl.model = &starpu_perfmodel_nop;
  149. #endif
  150. attr.cl.flags = STARPU_CODELET_SIMGRID_EXECUTE;
  151. attr.cl.where = STARPU_CPU;
  152. attr.if_clause = 1;
  153. clear_array();
  154. attr.cl.cpu_funcs[0] = parallel_region_1_f;
  155. starpu_omp_parallel_region(&attr);
  156. check_array();
  157. clear_array();
  158. attr.cl.cpu_funcs[0] = parallel_region_2_f;
  159. starpu_omp_parallel_region(&attr);
  160. check_array();
  161. clear_array();
  162. attr.cl.cpu_funcs[0] = parallel_region_3_f;
  163. starpu_omp_parallel_region(&attr);
  164. check_array();
  165. clear_array();
  166. attr.cl.cpu_funcs[0] = parallel_region_4_f;
  167. starpu_omp_parallel_region(&attr);
  168. check_array();
  169. clear_array();
  170. attr.cl.cpu_funcs[0] = parallel_region_5_f;
  171. starpu_omp_parallel_region(&attr);
  172. check_array();
  173. clear_array();
  174. attr.cl.cpu_funcs[0] = parallel_region_6_f;
  175. starpu_omp_parallel_region(&attr);
  176. check_array();
  177. return 0;
  178. }
  179. #endif