gpu_register.c 6.8 KB

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