cuda_task_01.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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(int argc, char **argv)
  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. attr.cl.cpu_funcs[0] = NULL;
  98. attr.cl.cuda_funcs[0] = task_region_g;
  99. attr.cl.where = STARPU_CUDA;
  100. attr.cl.nbuffers = 2;
  101. attr.cl.modes[0] = STARPU_R;
  102. attr.cl.modes[1] = STARPU_W;
  103. attr.handles = region_vector_handles;
  104. attr.cl_arg_size = sizeof(void *);
  105. attr.cl_arg_free = 0;
  106. attr.if_clause = 1;
  107. attr.final_clause = 0;
  108. attr.untied_clause = 1;
  109. attr.mergeable_clause = 0;
  110. i = 0;
  111. attr.cl_arg = (void *)(intptr_t)i;
  112. starpu_omp_task_region(&attr);
  113. }
  114. void parallel_region_f(void *buffers[], void *args)
  115. {
  116. (void)buffers;
  117. (void)args;
  118. starpu_omp_master(master_g1, NULL);
  119. starpu_omp_barrier();
  120. {
  121. starpu_data_handle_t region_vector_handle_1;
  122. region_vector_handle_1 = starpu_data_lookup(global_vector_1);
  123. printf("parallel_region block 1: region_vector_handle_1 = %p\n", region_vector_handle_1);
  124. }
  125. {
  126. starpu_data_handle_t region_vector_handle_2;
  127. region_vector_handle_2 = starpu_data_lookup(global_vector_2);
  128. printf("parallel_region block 1: region_vector_handle_2 = %p\n", region_vector_handle_2);
  129. }
  130. starpu_omp_barrier();
  131. starpu_omp_master(master_g2, NULL);
  132. starpu_omp_barrier();
  133. {
  134. starpu_data_handle_t region_vector_handle_1;
  135. region_vector_handle_1 = starpu_data_lookup(global_vector_1);
  136. printf("parallel_region block 2: region_vector_handle_1 = %p\n", region_vector_handle_1);
  137. }
  138. {
  139. starpu_data_handle_t region_vector_handle_2;
  140. region_vector_handle_2 = starpu_data_lookup(global_vector_2);
  141. printf("parallel_region block 2: region_vector_handle_2 = %p\n", region_vector_handle_2);
  142. }
  143. }
  144. int
  145. main (int argc, char *argv[])
  146. {
  147. (void)argc;
  148. (void)argv;
  149. struct starpu_omp_parallel_region_attr attr;
  150. if (starpu_cuda_worker_get_count() < 1)
  151. {
  152. return STARPU_TEST_SKIPPED;
  153. }
  154. memset(&attr, 0, sizeof(attr));
  155. attr.cl.cpu_funcs[0] = parallel_region_f;
  156. attr.cl.where = STARPU_CPU;
  157. attr.if_clause = 1;
  158. starpu_omp_parallel_region(&attr);
  159. int i;
  160. for (i = 0; i < NX; i++)
  161. {
  162. if (global_vector_1[i] != global_vector_2[i])
  163. {
  164. 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]);
  165. return EXIT_FAILURE;
  166. }
  167. }
  168. return 0;
  169. }
  170. #endif