gpu_register.c 7.1 KB

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