task_02.c 5.4 KB

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