parallel_for_01.c 4.9 KB

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