cuda_task_01.c 5.2 KB

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