task_02.c 5.6 KB

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