task_02.c 5.0 KB

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