cuda_task_01.c 5.5 KB

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