cuda_task_01.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015,2017,2019 CNRS
  4. * Copyright (C) 2014-2016 Inria
  5. * Copyright (C) 2017,2019 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 executing a CUDA target task.
  24. */
  25. #if !defined(STARPU_OPENMP) || !defined(STARPU_USE_CUDA)
  26. int main(void)
  27. {
  28. return STARPU_TEST_SKIPPED;
  29. }
  30. #else
  31. #define NX 64
  32. int global_vector_1[NX];
  33. int global_vector_2[NX];
  34. __attribute__((constructor))
  35. static void omp_constructor(void)
  36. {
  37. int ret = starpu_omp_init();
  38. if (ret == -EINVAL) exit(STARPU_TEST_SKIPPED);
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_omp_init");
  40. }
  41. __attribute__((destructor))
  42. static void omp_destructor(void)
  43. {
  44. starpu_omp_shutdown();
  45. }
  46. void task_region_g(void *buffers[], void *args)
  47. {
  48. struct starpu_vector_interface *_vector_1 = buffers[0];
  49. int nx1 = STARPU_VECTOR_GET_NX(_vector_1);
  50. int *v1 = (int *)STARPU_VECTOR_GET_PTR(_vector_1);
  51. struct starpu_vector_interface *_vector_2 = buffers[1];
  52. int nx2 = STARPU_VECTOR_GET_NX(_vector_2);
  53. int *v2 = (int *)STARPU_VECTOR_GET_PTR(_vector_2);
  54. int f = (int)(intptr_t)args;
  55. STARPU_ASSERT(nx1 == nx2);
  56. printf("depth 1 task, entry: vector_1 ptr = %p\n", v1);
  57. printf("depth 1 task, entry: vector_2 ptr = %p\n", v2);
  58. printf("depth 1 task, entry: f = %d\n", f);
  59. fprintf(stderr, "cudaMemcpy: -->\n");
  60. cudaMemcpyAsync(v2,v1,nx1*sizeof(*_vector_1), cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  61. fprintf(stderr, "cudaMemcpy: <--\n");
  62. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  63. }
  64. void master_g1(void *arg)
  65. {
  66. (void)arg;
  67. {
  68. starpu_data_handle_t region_vector_handle;
  69. int i;
  70. printf("master_g1: vector ptr = %p\n", global_vector_1);
  71. for (i = 0; i < NX; i++)
  72. {
  73. global_vector_1[i] = 1;
  74. }
  75. starpu_vector_data_register(&region_vector_handle, STARPU_MAIN_RAM, (uintptr_t)global_vector_1, NX, sizeof(global_vector_1[0]));
  76. printf("master_g1: region_vector_handle = %p\n", region_vector_handle);
  77. }
  78. {
  79. starpu_data_handle_t region_vector_handle;
  80. int i;
  81. printf("master_g1: vector ptr = %p\n", global_vector_2);
  82. for (i = 0; i < NX; i++)
  83. {
  84. global_vector_2[i] = 0;
  85. }
  86. starpu_vector_data_register(&region_vector_handle, STARPU_MAIN_RAM, (uintptr_t)global_vector_2, NX, sizeof(global_vector_2[0]));
  87. printf("master_g1: region_vector_handle = %p\n", region_vector_handle);
  88. }
  89. }
  90. void master_g2(void *arg)
  91. {
  92. (void)arg;
  93. starpu_data_handle_t region_vector_handles[2];
  94. struct starpu_omp_task_region_attr attr;
  95. int i;
  96. region_vector_handles[0] = starpu_data_lookup(global_vector_1);
  97. printf("master_g2: region_vector_handles[0] = %p\n", region_vector_handles[0]);
  98. region_vector_handles[1] = starpu_data_lookup(global_vector_2);
  99. printf("master_g2: region_vector_handles[1] = %p\n", region_vector_handles[1]);
  100. memset(&attr, 0, sizeof(attr));
  101. #ifdef STARPU_SIMGRID
  102. attr.cl.model = &starpu_perfmodel_nop;
  103. attr.cl.flags = STARPU_CODELET_SIMGRID_EXECUTE;
  104. #endif
  105. attr.cl.cpu_funcs[0] = NULL;
  106. attr.cl.cuda_funcs[0] = task_region_g;
  107. attr.cl.where = STARPU_CUDA;
  108. attr.cl.nbuffers = 2;
  109. attr.cl.modes[0] = STARPU_R;
  110. attr.cl.modes[1] = STARPU_W;
  111. attr.handles = region_vector_handles;
  112. attr.cl_arg_size = sizeof(void *);
  113. attr.cl_arg_free = 0;
  114. attr.if_clause = 1;
  115. attr.final_clause = 0;
  116. attr.untied_clause = 1;
  117. attr.mergeable_clause = 0;
  118. i = 0;
  119. attr.cl_arg = (void *)(intptr_t)i;
  120. starpu_omp_task_region(&attr);
  121. }
  122. void parallel_region_f(void *buffers[], void *args)
  123. {
  124. (void)buffers;
  125. (void)args;
  126. starpu_omp_master(master_g1, NULL);
  127. starpu_omp_barrier();
  128. {
  129. starpu_data_handle_t region_vector_handle_1;
  130. region_vector_handle_1 = starpu_data_lookup(global_vector_1);
  131. printf("parallel_region block 1: region_vector_handle_1 = %p\n", region_vector_handle_1);
  132. }
  133. {
  134. starpu_data_handle_t region_vector_handle_2;
  135. region_vector_handle_2 = starpu_data_lookup(global_vector_2);
  136. printf("parallel_region block 1: region_vector_handle_2 = %p\n", region_vector_handle_2);
  137. }
  138. starpu_omp_barrier();
  139. starpu_omp_master(master_g2, NULL);
  140. starpu_omp_barrier();
  141. {
  142. starpu_data_handle_t region_vector_handle_1;
  143. region_vector_handle_1 = starpu_data_lookup(global_vector_1);
  144. printf("parallel_region block 2: region_vector_handle_1 = %p\n", region_vector_handle_1);
  145. }
  146. {
  147. starpu_data_handle_t region_vector_handle_2;
  148. region_vector_handle_2 = starpu_data_lookup(global_vector_2);
  149. printf("parallel_region block 2: region_vector_handle_2 = %p\n", region_vector_handle_2);
  150. }
  151. }
  152. int
  153. main (void)
  154. {
  155. struct starpu_omp_parallel_region_attr attr;
  156. if (starpu_cuda_worker_get_count() < 1)
  157. {
  158. return STARPU_TEST_SKIPPED;
  159. }
  160. memset(&attr, 0, sizeof(attr));
  161. #ifdef STARPU_SIMGRID
  162. attr.cl.model = &starpu_perfmodel_nop;
  163. #endif
  164. attr.cl.flags = STARPU_CODELET_SIMGRID_EXECUTE;
  165. attr.cl.cpu_funcs[0] = parallel_region_f;
  166. attr.cl.where = STARPU_CPU;
  167. attr.if_clause = 1;
  168. starpu_omp_parallel_region(&attr);
  169. int i;
  170. for (i = 0; i < NX; i++)
  171. {
  172. if (global_vector_1[i] != global_vector_2[i])
  173. {
  174. fprintf(stderr, "check failed: global_vector_1[%d] = %d, global_vector_2[%d] = %d\n", i, global_vector_1[i], i, global_vector_2[i]);
  175. return EXIT_FAILURE;
  176. }
  177. }
  178. return 0;
  179. }
  180. #endif