cuda_task_01.c 5.7 KB

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