array_slice_01.c 6.8 KB

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