cuda_task_01.c 5.8 KB

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