gpu_ptr_register.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2013,2015-2017 CNRS
  4. * Copyright (C) 2011-2014,2016,2019 Université de Bordeaux
  5. * Copyright (C) 2012 Inria
  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 <starpu.h>
  19. #include "../helper.h"
  20. #include "scal.h"
  21. /*
  22. * Register the GPU buffer to be used for an existing data, and perform
  23. * a partitioned operation
  24. */
  25. #if ! (defined(STARPU_USE_OPENCL) || defined(STARPU_USE_CUDA))
  26. int main(void)
  27. {
  28. return STARPU_TEST_SKIPPED;
  29. }
  30. #else
  31. static int
  32. submit_tasks(starpu_data_handle_t handle, int pieces, int n)
  33. {
  34. int i, ret;
  35. for (i = 0; i < pieces; i++)
  36. {
  37. struct starpu_task *task = starpu_task_create();
  38. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  39. task->cl = &scal_codelet;
  40. task->execute_on_a_specific_worker = 1;
  41. task->workerid = i%n;
  42. ret = starpu_task_submit(task);
  43. if (ret == -ENODEV)
  44. return -ENODEV;
  45. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  46. }
  47. ret = starpu_task_wait_for_all();
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  49. return 0;
  50. }
  51. static int
  52. find_a_worker(enum starpu_worker_archtype type)
  53. {
  54. int worker[STARPU_NMAXWORKERS];
  55. int ret = starpu_worker_get_ids_by_type(type, worker, STARPU_NMAXWORKERS);
  56. if (ret == 0)
  57. return -ENODEV;
  58. if (ret == -ERANGE)
  59. return worker[STARPU_NMAXWORKERS-1];
  60. return worker[ret-1];
  61. }
  62. static int
  63. check_result(unsigned *t, size_t size)
  64. {
  65. unsigned i;
  66. for (i = 0; i < size; i++)
  67. {
  68. if (t[i] != i*2)
  69. {
  70. FPRINTF(stderr,"t[%u] is %u instead of %u\n", i, t[i], 2*i);
  71. return 1;
  72. }
  73. }
  74. return 0;
  75. }
  76. #ifdef STARPU_USE_CUDA
  77. #ifdef STARPU_HAVE_CUDA_MEMCPY_PEER
  78. static int
  79. test_cuda(void)
  80. {
  81. int ret;
  82. unsigned *foo_gpu;
  83. unsigned *foo;
  84. int n, i, size, pieces;
  85. int devid;
  86. int chosen;
  87. cudaError_t cures;
  88. starpu_data_handle_t handle;
  89. /* Find a CUDA worker */
  90. chosen = find_a_worker(STARPU_CUDA_WORKER);
  91. if (chosen == -ENODEV)
  92. return -ENODEV;
  93. n = starpu_worker_get_count();
  94. size = 10 * n;
  95. devid = starpu_worker_get_devid(chosen);
  96. foo_gpu = (void*) starpu_malloc_on_node(starpu_worker_get_memory_node(chosen), size * sizeof(*foo_gpu));
  97. foo = calloc(size, sizeof(*foo));
  98. for (i = 0; i < size; i++)
  99. foo[i] = i;
  100. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)foo, size, sizeof(*foo));
  101. starpu_vector_ptr_register(handle, starpu_worker_get_memory_node(chosen), (uintptr_t)foo_gpu, (uintptr_t)foo_gpu, 0);
  102. /* Broadcast the data to force in-place partitioning */
  103. for (i = 0; i < n; i++)
  104. starpu_data_prefetch_on_node(handle, starpu_worker_get_memory_node(i), 0);
  105. /* Even with just one worker, split in at least two */
  106. if (n == 1)
  107. pieces = 2;
  108. else
  109. pieces = n;
  110. struct starpu_data_filter f =
  111. {
  112. .filter_func = starpu_vector_filter_block,
  113. .nchildren = pieces,
  114. };
  115. starpu_data_partition(handle, &f);
  116. ret = submit_tasks(handle, pieces, n);
  117. if (ret == -ENODEV)
  118. return -ENODEV;
  119. starpu_data_unpartition(handle, starpu_worker_get_memory_node(chosen));
  120. starpu_data_prefetch_on_node(handle, starpu_worker_get_memory_node(chosen), 0);
  121. starpu_data_unregister(handle);
  122. starpu_cuda_set_device(devid);
  123. cures = cudaMemcpy(foo, foo_gpu, size * sizeof(*foo_gpu), cudaMemcpyDeviceToHost);
  124. if (!cures)
  125. cures = cudaDeviceSynchronize();
  126. if (STARPU_UNLIKELY(cures))
  127. STARPU_CUDA_REPORT_ERROR(cures);
  128. return check_result(foo, size);
  129. }
  130. #endif
  131. #endif
  132. #ifdef STARPU_USE_OPENCL
  133. static int
  134. test_opencl(void)
  135. {
  136. int i;
  137. int ret;
  138. int chosen;
  139. int n;
  140. int size;
  141. int pieces;
  142. cl_mem foo_gpu;
  143. starpu_data_handle_t handle;
  144. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/scal_opencl.cl", &opencl_program, NULL);
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  146. /* Find an OpenCL worker */
  147. chosen = find_a_worker(STARPU_OPENCL_WORKER);
  148. if (chosen == -ENODEV)
  149. return -ENODEV;
  150. n = starpu_worker_get_count();
  151. size = 10 * n;
  152. int devid;
  153. cl_int err;
  154. cl_context context;
  155. cl_command_queue queue;
  156. devid = starpu_worker_get_devid(chosen);
  157. starpu_opencl_get_context(devid, &context);
  158. starpu_opencl_get_queue(devid, &queue);
  159. foo_gpu = (void*) starpu_malloc_on_node(starpu_worker_get_memory_node(chosen), size * sizeof(int));
  160. unsigned int *foo = malloc(size*sizeof(*foo));
  161. for (i = 0; i < size; i++)
  162. foo[i] = i;
  163. starpu_vector_data_register(&handle,
  164. STARPU_MAIN_RAM,
  165. (uintptr_t)foo,
  166. size,
  167. sizeof(int));
  168. starpu_vector_ptr_register(handle,
  169. starpu_worker_get_memory_node(chosen),
  170. (uintptr_t)foo_gpu,
  171. (uintptr_t)foo_gpu,
  172. 0);
  173. /* Broadcast the data to force in-place partitioning */
  174. for (i = 0; i < n; i++)
  175. starpu_data_prefetch_on_node(handle, starpu_worker_get_memory_node(i), 0);
  176. /* Even with just one worker, split in at least two */
  177. if (n == 1)
  178. pieces = 2;
  179. else
  180. pieces = n;
  181. struct starpu_data_filter f =
  182. {
  183. .filter_func = starpu_vector_filter_block,
  184. .nchildren = pieces,
  185. };
  186. starpu_data_partition(handle, &f);
  187. ret = submit_tasks(handle, pieces, n);
  188. if (ret == -ENODEV)
  189. return -ENODEV;
  190. starpu_data_unpartition(handle, starpu_worker_get_memory_node(chosen));
  191. starpu_data_prefetch_on_node(handle, starpu_worker_get_memory_node(chosen), 0);
  192. starpu_data_unregister(handle);
  193. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  194. ret = starpu_opencl_unload_opencl(&opencl_program);
  195. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  196. err = clEnqueueReadBuffer(queue,
  197. foo_gpu,
  198. CL_FALSE,
  199. 0,
  200. size*sizeof(*foo),
  201. foo,
  202. 0,
  203. NULL,
  204. NULL);
  205. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  206. STARPU_OPENCL_REPORT_ERROR(err);
  207. clFinish(queue);
  208. return check_result(foo, size);
  209. }
  210. #endif /* !STARPU_USE_OPENCL */
  211. int main(int argc, char **argv)
  212. {
  213. int skipped_cuda = 1, skipped_opencl = 1;
  214. int ret;
  215. ret = starpu_initialize(NULL, &argc, &argv);
  216. if (ret == -ENODEV)
  217. return STARPU_TEST_SKIPPED;
  218. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  219. #ifdef STARPU_USE_OPENCL
  220. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/scal_opencl.cl", &opencl_program, NULL);
  221. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  222. #endif
  223. #ifdef STARPU_USE_CUDA
  224. #ifdef STARPU_HAVE_CUDA_MEMCPY_PEER
  225. ret = test_cuda();
  226. if (ret == 1)
  227. goto fail;
  228. else if (ret == 0)
  229. skipped_cuda = 0;
  230. #endif
  231. #endif
  232. #ifdef STARPU_USE_OPENCL
  233. ret = test_opencl();
  234. if (ret == 1)
  235. goto fail;
  236. else if (ret == 0)
  237. skipped_opencl = 0;
  238. #endif
  239. starpu_shutdown();
  240. if (skipped_cuda == 1 && skipped_opencl == 1)
  241. return STARPU_TEST_SKIPPED;
  242. return EXIT_SUCCESS;
  243. fail:
  244. starpu_shutdown();
  245. return EXIT_FAILURE;
  246. }
  247. #endif /* defined(STARPU_USE_OPENCL) || defined(STARPU_USE_CUDA) */