array_slice_01.c 6.4 KB

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