gpu_register.c 7.5 KB

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