array_slice_01.c 7.0 KB

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