array_slice_01.c 7.0 KB

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