task_02.c 5.6 KB

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