task_02.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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(int argc, char **argv)
  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. attr.cl.cpu_funcs[0] = task_region_h;
  81. attr.cl.where = STARPU_CPU;
  82. attr.cl.nbuffers = 1;
  83. attr.cl.modes[0] = STARPU_RW;
  84. attr.handles = &task_vector_handle;
  85. attr.cl_arg_size = sizeof(void *);
  86. attr.cl_arg_free = 0;
  87. attr.if_clause = 1;
  88. attr.final_clause = 0;
  89. attr.untied_clause = 1;
  90. attr.mergeable_clause = 0;
  91. i = 0;
  92. attr.cl_arg = (void *)(intptr_t)i++;
  93. starpu_omp_task_region(&attr);
  94. attr.cl_arg = (void *)(intptr_t)i++;
  95. starpu_omp_task_region(&attr);
  96. }
  97. starpu_omp_taskwait();
  98. }
  99. void master_g1(void *arg)
  100. {
  101. starpu_data_handle_t region_vector_handle;
  102. int i;
  103. printf("master_g1: vector ptr = %p\n", global_vector);
  104. for (i = 0; i < NX; i++)
  105. {
  106. global_vector[i] = 1;
  107. }
  108. starpu_vector_data_register(&region_vector_handle, STARPU_MAIN_RAM, (uintptr_t)global_vector, NX, sizeof(global_vector[0]));
  109. printf("master_g1: region_vector_handle = %p\n", region_vector_handle);
  110. }
  111. void master_g2(void *arg)
  112. {
  113. starpu_data_handle_t region_vector_handle;
  114. struct starpu_omp_task_region_attr attr;
  115. int i;
  116. region_vector_handle = starpu_data_lookup(global_vector);
  117. printf("master_g2: region_vector_handle = %p\n", region_vector_handle);
  118. memset(&attr, 0, sizeof(attr));
  119. attr.cl.cpu_funcs[0] = task_region_g;
  120. attr.cl.where = STARPU_CPU;
  121. attr.cl.nbuffers = 1;
  122. attr.cl.modes[0] = STARPU_RW;
  123. attr.handles = &region_vector_handle;
  124. attr.cl_arg_size = sizeof(void *);
  125. attr.cl_arg_free = 0;
  126. attr.if_clause = 1;
  127. attr.final_clause = 0;
  128. attr.untied_clause = 1;
  129. attr.mergeable_clause = 0;
  130. i = 0;
  131. attr.cl_arg = (void *)(intptr_t)i++;
  132. starpu_omp_task_region(&attr);
  133. attr.cl_arg = (void *)(intptr_t)i++;
  134. starpu_omp_task_region(&attr);
  135. attr.cl_arg = (void *)(intptr_t)i++;
  136. starpu_omp_task_region(&attr);
  137. attr.cl_arg = (void *)(intptr_t)i++;
  138. starpu_omp_task_region(&attr);
  139. }
  140. void parallel_region_f(void *buffers[], void *args)
  141. {
  142. starpu_omp_master(master_g1, NULL);
  143. starpu_omp_barrier();
  144. {
  145. starpu_data_handle_t region_vector_handle;
  146. region_vector_handle = starpu_data_lookup(global_vector);
  147. printf("parallel_region block 1: region_vector_handle = %p\n", region_vector_handle);
  148. }
  149. starpu_omp_barrier();
  150. starpu_omp_master(master_g2, 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 2: region_vector_handle = %p\n", region_vector_handle);
  156. }
  157. }
  158. int
  159. main (int argc, char *argv[])
  160. {
  161. struct starpu_omp_parallel_region_attr attr;
  162. memset(&attr, 0, sizeof(attr));
  163. attr.cl.cpu_funcs[0] = parallel_region_f;
  164. attr.cl.where = STARPU_CPU;
  165. attr.if_clause = 1;
  166. starpu_omp_parallel_region(&attr);
  167. return 0;
  168. }
  169. #endif