task_02.c 5.5 KB

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