gpu_register.c 7.6 KB

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