parallel_for_01.c 5.0 KB

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