array_slice_01.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.cpu_funcs[0] = task_region_h;
  104. attr.cl.where = STARPU_CPU;
  105. attr.cl.nbuffers = 1;
  106. attr.cl.modes[0] = STARPU_RW;
  107. attr.cl_arg_size = 3*sizeof(void *);
  108. attr.cl_arg_free = 0;
  109. attr.if_clause = 1;
  110. attr.final_clause = 0;
  111. attr.untied_clause = 1;
  112. attr.mergeable_clause = 0;
  113. i = 0;
  114. cl_arg_1[0] = (void *)(intptr_t)i++;
  115. cl_arg_1[1] = (void *)(intptr_t)0;
  116. cl_arg_1[2] = (void *)(intptr_t)half_nx;
  117. attr.cl_arg = cl_arg_1;
  118. attr.handles = &vector_slice_1_handle;
  119. starpu_omp_task_region(&attr);
  120. cl_arg_2[0] = (void *)(intptr_t)i++;
  121. cl_arg_2[1] = (void *)(intptr_t)half_nx;
  122. cl_arg_2[2] = (void *)(intptr_t)nx;
  123. attr.cl_arg = cl_arg_2;
  124. attr.handles = &vector_slice_2_handle;
  125. starpu_omp_task_region(&attr);
  126. }
  127. starpu_omp_taskwait();
  128. }
  129. void master_g1(void *arg)
  130. {
  131. (void)arg;
  132. starpu_data_handle_t region_vector_handle;
  133. int i;
  134. printf("master_g1: vector ptr = %p\n", global_vector);
  135. for (i = 0; i < NX; i++)
  136. {
  137. global_vector[i] = 1;
  138. }
  139. starpu_vector_data_register(&region_vector_handle, STARPU_MAIN_RAM, (uintptr_t)global_vector, NX, sizeof(global_vector[0]));
  140. printf("master_g1: region_vector_handle = %p\n", region_vector_handle);
  141. }
  142. void master_g2(void *arg)
  143. {
  144. (void)arg;
  145. starpu_data_handle_t region_vector_handle;
  146. struct starpu_omp_task_region_attr attr;
  147. int i;
  148. region_vector_handle = starpu_data_lookup(global_vector);
  149. printf("master_g2: region_vector_handle = %p\n", region_vector_handle);
  150. memset(&attr, 0, sizeof(attr));
  151. #ifdef STARPU_SIMGRID
  152. attr.cl.model = &starpu_perfmodel_nop;
  153. #endif
  154. attr.cl.cpu_funcs[0] = task_region_g;
  155. attr.cl.where = STARPU_CPU;
  156. attr.cl.nbuffers = 1;
  157. attr.cl.modes[0] = STARPU_RW;
  158. attr.handles = &region_vector_handle;
  159. attr.cl_arg_size = sizeof(void *);
  160. attr.cl_arg_free = 0;
  161. attr.if_clause = 1;
  162. attr.final_clause = 0;
  163. attr.untied_clause = 1;
  164. attr.mergeable_clause = 0;
  165. i = 0;
  166. attr.cl_arg = (void *)(intptr_t)i++;
  167. starpu_omp_task_region(&attr);
  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. }
  175. void parallel_region_f(void *buffers[], void *args)
  176. {
  177. (void)buffers;
  178. (void)args;
  179. starpu_omp_master(master_g1, NULL);
  180. starpu_omp_barrier();
  181. {
  182. starpu_data_handle_t region_vector_handle;
  183. region_vector_handle = starpu_data_lookup(global_vector);
  184. printf("parallel_region block 1: region_vector_handle = %p\n", region_vector_handle);
  185. }
  186. starpu_omp_barrier();
  187. starpu_omp_master(master_g2, NULL);
  188. starpu_omp_barrier();
  189. {
  190. starpu_data_handle_t region_vector_handle;
  191. region_vector_handle = starpu_data_lookup(global_vector);
  192. printf("parallel_region block 2: region_vector_handle = %p\n", region_vector_handle);
  193. }
  194. }
  195. int
  196. main (void)
  197. {
  198. struct starpu_omp_parallel_region_attr attr;
  199. assert(NX >= 2);
  200. memset(&attr, 0, sizeof(attr));
  201. #ifdef STARPU_SIMGRID
  202. attr.cl.model = &starpu_perfmodel_nop;
  203. #endif
  204. attr.cl.cpu_funcs[0] = parallel_region_f;
  205. attr.cl.where = STARPU_CPU;
  206. attr.if_clause = 1;
  207. starpu_omp_parallel_region(&attr);
  208. return 0;
  209. }
  210. #endif