array_slice_01.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * Test recursive OpenMP tasks, data dependences, data slice dependences.
  22. */
  23. #if !defined(STARPU_OPENMP)
  24. int main(void)
  25. {
  26. return STARPU_TEST_SKIPPED;
  27. }
  28. #else
  29. #define NX 64
  30. int global_vector[NX];
  31. __attribute__((constructor))
  32. static void omp_constructor(void)
  33. {
  34. int ret = starpu_omp_init();
  35. if (ret == -EINVAL) exit(STARPU_TEST_SKIPPED);
  36. STARPU_CHECK_RETURN_VALUE(ret, "starpu_omp_init");
  37. }
  38. __attribute__((destructor))
  39. static void omp_destructor(void)
  40. {
  41. starpu_omp_shutdown();
  42. }
  43. void task_region_h(void *buffers[], void *_args)
  44. {
  45. void **args = _args;
  46. struct starpu_vector_interface *_vector = buffers[0];
  47. int nx = STARPU_VECTOR_GET_NX(_vector);
  48. int elemsize = STARPU_VECTOR_GET_ELEMSIZE(_vector);
  49. int slice_base = STARPU_VECTOR_GET_SLICE_BASE(_vector);
  50. int *v = (int *)STARPU_VECTOR_GET_PTR(_vector);
  51. int f = (int)(intptr_t)args[0];
  52. int imin = (int)(intptr_t)args[1];
  53. int imax = (int)(intptr_t)args[2];
  54. int i;
  55. assert(elemsize == sizeof(v[0]));
  56. printf("depth 2 task, entry: vector ptr = %p, slice_base = %d, imin = %d, imax = %d\n", v, slice_base, imin, imax);
  57. for (i = imin; i < imax; i++)
  58. {
  59. assert(i-slice_base>=0);
  60. assert(i-slice_base<NX);
  61. (v-slice_base)[i] += f;
  62. }
  63. printf("depth 2 task ending\n");
  64. }
  65. void task_region_g(void *buffers[], void *args)
  66. {
  67. struct starpu_vector_interface *_vector = buffers[0];
  68. int nx = STARPU_VECTOR_GET_NX(_vector);
  69. int *v = (int *)STARPU_VECTOR_GET_PTR(_vector);
  70. int f = (int)(intptr_t)args;
  71. printf("depth 1 task, entry: vector ptr = %p\n", v);
  72. {
  73. int i;
  74. for (i = 0; i < nx; i++)
  75. {
  76. v[i] += f;
  77. }
  78. }
  79. {
  80. const int half_nx = nx/2;
  81. starpu_data_handle_t vector_slice_1_handle;
  82. starpu_vector_data_register(&vector_slice_1_handle, STARPU_MAIN_RAM, (uintptr_t)&v[0], half_nx, sizeof(v[0]));
  83. printf("depth 1 task, block 1: vector_slice_1_handle = %p\n", vector_slice_1_handle);
  84. starpu_data_handle_t vector_slice_2_handle;
  85. starpu_vector_data_register(&vector_slice_2_handle, STARPU_MAIN_RAM, (uintptr_t)&v[half_nx], nx-half_nx, sizeof(v[0]));
  86. /* set slice base */
  87. starpu_omp_vector_annotate(vector_slice_2_handle, half_nx);
  88. printf("depth 1 task, block 1: vector_slice_2_handle = %p\n", vector_slice_2_handle);
  89. }
  90. void *cl_arg_1[3];
  91. void *cl_arg_2[3];
  92. {
  93. struct starpu_omp_task_region_attr attr;
  94. const int half_nx = nx/2;
  95. int i;
  96. starpu_data_handle_t vector_slice_1_handle = starpu_data_lookup(&v[0]);
  97. printf("depth 1 task, block 2: vector_slice_1_handle = %p\n", vector_slice_1_handle);
  98. starpu_data_handle_t vector_slice_2_handle = starpu_data_lookup(&v[half_nx]);
  99. printf("depth 1 task, block 2: vector_slice_2_handle = %p\n", vector_slice_2_handle);
  100. memset(&attr, 0, sizeof(attr));
  101. #ifdef STARPU_SIMGRID
  102. attr.cl.model = &starpu_perfmodel_nop;
  103. #endif
  104. attr.cl.flags = STARPU_CODELET_SIMGRID_EXECUTE;
  105. attr.cl.cpu_funcs[0] = task_region_h;
  106. attr.cl.where = STARPU_CPU;
  107. attr.cl.nbuffers = 1;
  108. attr.cl.modes[0] = STARPU_RW;
  109. attr.cl_arg_size = 3*sizeof(void *);
  110. attr.cl_arg_free = 0;
  111. attr.if_clause = 1;
  112. attr.final_clause = 0;
  113. attr.untied_clause = 1;
  114. attr.mergeable_clause = 0;
  115. i = 0;
  116. cl_arg_1[0] = (void *)(intptr_t)i++;
  117. cl_arg_1[1] = (void *)(intptr_t)0;
  118. cl_arg_1[2] = (void *)(intptr_t)half_nx;
  119. attr.cl_arg = cl_arg_1;
  120. attr.handles = &vector_slice_1_handle;
  121. starpu_omp_task_region(&attr);
  122. cl_arg_2[0] = (void *)(intptr_t)i++;
  123. cl_arg_2[1] = (void *)(intptr_t)half_nx;
  124. cl_arg_2[2] = (void *)(intptr_t)nx;
  125. attr.cl_arg = cl_arg_2;
  126. attr.handles = &vector_slice_2_handle;
  127. starpu_omp_task_region(&attr);
  128. }
  129. starpu_omp_taskwait();
  130. }
  131. void master_g1(void *arg)
  132. {
  133. (void)arg;
  134. starpu_data_handle_t region_vector_handle;
  135. int i;
  136. printf("master_g1: vector ptr = %p\n", global_vector);
  137. for (i = 0; i < NX; i++)
  138. {
  139. global_vector[i] = 1;
  140. }
  141. starpu_vector_data_register(&region_vector_handle, STARPU_MAIN_RAM, (uintptr_t)global_vector, NX, sizeof(global_vector[0]));
  142. printf("master_g1: region_vector_handle = %p\n", region_vector_handle);
  143. }
  144. void master_g2(void *arg)
  145. {
  146. (void)arg;
  147. starpu_data_handle_t region_vector_handle;
  148. struct starpu_omp_task_region_attr attr;
  149. int i;
  150. region_vector_handle = starpu_data_lookup(global_vector);
  151. printf("master_g2: region_vector_handle = %p\n", region_vector_handle);
  152. memset(&attr, 0, sizeof(attr));
  153. #ifdef STARPU_SIMGRID
  154. attr.cl.model = &starpu_perfmodel_nop;
  155. #endif
  156. attr.cl.flags = STARPU_CODELET_SIMGRID_EXECUTE;
  157. attr.cl.cpu_funcs[0] = task_region_g;
  158. attr.cl.where = STARPU_CPU;
  159. attr.cl.nbuffers = 1;
  160. attr.cl.modes[0] = STARPU_RW;
  161. attr.handles = &region_vector_handle;
  162. attr.cl_arg_size = sizeof(void *);
  163. attr.cl_arg_free = 0;
  164. attr.if_clause = 1;
  165. attr.final_clause = 0;
  166. attr.untied_clause = 1;
  167. attr.mergeable_clause = 0;
  168. i = 0;
  169. attr.cl_arg = (void *)(intptr_t)i++;
  170. starpu_omp_task_region(&attr);
  171. attr.cl_arg = (void *)(intptr_t)i++;
  172. starpu_omp_task_region(&attr);
  173. attr.cl_arg = (void *)(intptr_t)i++;
  174. starpu_omp_task_region(&attr);
  175. attr.cl_arg = (void *)(intptr_t)i++;
  176. starpu_omp_task_region(&attr);
  177. }
  178. void parallel_region_f(void *buffers[], void *args)
  179. {
  180. (void)buffers;
  181. (void)args;
  182. starpu_omp_master(master_g1, NULL);
  183. starpu_omp_barrier();
  184. {
  185. starpu_data_handle_t region_vector_handle;
  186. region_vector_handle = starpu_data_lookup(global_vector);
  187. printf("parallel_region block 1: region_vector_handle = %p\n", region_vector_handle);
  188. }
  189. starpu_omp_barrier();
  190. starpu_omp_master(master_g2, NULL);
  191. starpu_omp_barrier();
  192. {
  193. starpu_data_handle_t region_vector_handle;
  194. region_vector_handle = starpu_data_lookup(global_vector);
  195. printf("parallel_region block 2: region_vector_handle = %p\n", region_vector_handle);
  196. }
  197. }
  198. int
  199. main (void)
  200. {
  201. struct starpu_omp_parallel_region_attr attr;
  202. assert(NX >= 2);
  203. memset(&attr, 0, sizeof(attr));
  204. #ifdef STARPU_SIMGRID
  205. attr.cl.model = &starpu_perfmodel_nop;
  206. #endif
  207. attr.cl.flags = STARPU_CODELET_SIMGRID_EXECUTE;
  208. attr.cl.cpu_funcs[0] = parallel_region_f;
  209. attr.cl.where = STARPU_CPU;
  210. attr.if_clause = 1;
  211. starpu_omp_parallel_region(&attr);
  212. return 0;
  213. }
  214. #endif