run_driver.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 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 <unistd.h>
  18. #include "../../helper.h"
  19. /*
  20. * Users can directly control drivers by using the starpu_driver* functions.
  21. *
  22. * This test makes sure that the starpu_driver_run function works for CPU, CUDA
  23. * and OpenCL drivers, and that the starpu_drivers_request_termination function
  24. * correctly shuts down all drivers.
  25. *
  26. * The test_* functions can return:
  27. * - 0 (success)
  28. * - 1 (failure)
  29. * - STARPU_TEST_SKIPPED (non-critical errors)
  30. */
  31. #if defined(STARPU_USE_CPU) || defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  32. static void
  33. dummy(void *buffers[], void *args)
  34. {
  35. (void) buffers;
  36. (*(int *)args)++;
  37. usleep(1000000);
  38. }
  39. static struct starpu_codelet cl =
  40. {
  41. .cpu_funcs = { dummy },
  42. .cuda_funcs = { dummy },
  43. .opencl_funcs = { dummy },
  44. .nbuffers = 0
  45. };
  46. static void *
  47. run_driver(void *arg)
  48. {
  49. struct starpu_driver *d = (struct starpu_driver *) arg;
  50. int ret = starpu_driver_run(d);
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_driver_run");
  52. return NULL;
  53. }
  54. #endif /* STARPU_USE_CPU || STARPU_USE_CUDA || STARPU_USE_OPENCL */
  55. #ifdef STARPU_USE_CPU
  56. static int
  57. test_cpu(void)
  58. {
  59. int ret, var = 0;
  60. static starpu_pthread_t driver_thread;
  61. struct starpu_conf conf;
  62. struct starpu_driver d =
  63. {
  64. .type = STARPU_CPU_WORKER,
  65. .id.cpu_id = 0
  66. };
  67. starpu_conf_init(&conf);
  68. conf.n_not_launched_drivers = 1;
  69. conf.not_launched_drivers = &d;
  70. conf.ncpus = 1;
  71. ret = starpu_init(&conf);
  72. if (ret == -ENODEV || starpu_cpu_worker_get_count() == 0)
  73. {
  74. FPRINTF(stderr, "WARNING: No CPU worker found\n");
  75. if (ret == 0)
  76. starpu_shutdown();
  77. return STARPU_TEST_SKIPPED;
  78. }
  79. ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
  80. if (ret != 0)
  81. {
  82. ret = 1;
  83. goto out2;
  84. }
  85. struct starpu_task *task;
  86. task = starpu_task_create();
  87. cl.where = STARPU_CPU;
  88. task->cl = &cl;
  89. task->cl_arg = &var;
  90. task->synchronous = 1;
  91. ret = starpu_task_submit(task);
  92. if (ret == -ENODEV)
  93. {
  94. FPRINTF(stderr, "WARNING: No worker can execute this task\n");
  95. ret = STARPU_TEST_SKIPPED;
  96. goto out;
  97. }
  98. FPRINTF(stderr, "[CPU] Var = %d (expected value: 1)\n", var);
  99. ret = !!(var != 1);
  100. out:
  101. starpu_drivers_request_termination();
  102. if (starpu_pthread_join(driver_thread, NULL) != 0)
  103. return 1;
  104. out2:
  105. starpu_shutdown();
  106. return ret;
  107. }
  108. #endif /* STARPU_USE_CPU */
  109. #ifdef STARPU_USE_CUDA
  110. static int
  111. test_cuda(void)
  112. {
  113. int ret, var = 0;
  114. static starpu_pthread_t driver_thread;
  115. struct starpu_conf conf;
  116. struct starpu_driver d =
  117. {
  118. .type = STARPU_CUDA_WORKER,
  119. .id.cuda_id = 0
  120. };
  121. starpu_conf_init(&conf);
  122. conf.n_not_launched_drivers = 1;
  123. conf.not_launched_drivers = &d;
  124. conf.ncuda = 1;
  125. ret = starpu_init(&conf);
  126. if (ret == -ENODEV || starpu_cuda_worker_get_count() == 0)
  127. {
  128. FPRINTF(stderr, "WARNING: No CUDA worker found\n");
  129. if (ret == 0)
  130. starpu_shutdown();
  131. return STARPU_TEST_SKIPPED;
  132. }
  133. ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
  134. if (ret == -1)
  135. {
  136. ret = 1;
  137. goto out;
  138. }
  139. struct starpu_task *task;
  140. task = starpu_task_create();
  141. cl.where = STARPU_CUDA;
  142. task->cl = &cl;
  143. task->cl_arg = &var;
  144. task->synchronous = 1;
  145. ret = starpu_task_submit(task);
  146. if (ret == -ENODEV)
  147. {
  148. FPRINTF(stderr, "WARNING: No worker can execute this task\n");
  149. ret = STARPU_TEST_SKIPPED;
  150. goto out;
  151. }
  152. out:
  153. starpu_drivers_request_termination();
  154. if (starpu_pthread_join(driver_thread, NULL) != 0)
  155. return 1;
  156. starpu_shutdown();
  157. FPRINTF(stderr, "[CUDA] Var = %d (expected value: 1)\n", var);
  158. ret = !!(var != 1);
  159. return ret;
  160. }
  161. #endif /* STARPU_USE_CUDA */
  162. #ifdef STARPU_USE_OPENCL
  163. static int
  164. test_opencl(void)
  165. {
  166. int ret, var = 0;
  167. static starpu_pthread_t driver_thread;
  168. struct starpu_conf conf;
  169. cl_int err;
  170. cl_uint pdummy;
  171. cl_platform_id platform;
  172. err = clGetPlatformIDs(1, &platform, &pdummy);
  173. if (err != CL_SUCCESS)
  174. {
  175. FPRINTF(stderr, "WARNING: No OpenCL platform found\n");
  176. return STARPU_TEST_SKIPPED;
  177. }
  178. cl_device_type device_type = CL_DEVICE_TYPE_GPU|CL_DEVICE_TYPE_ACCELERATOR;
  179. if (starpu_get_env_number("STARPU_OPENCL_ON_CPUS") > 0)
  180. device_type |= CL_DEVICE_TYPE_CPU;
  181. if (starpu_get_env_number("STARPU_OPENCL_ONLY_ON_CPUS") > 0)
  182. device_type = CL_DEVICE_TYPE_CPU;
  183. cl_device_id device_id;
  184. err = clGetDeviceIDs(platform, device_type, 1, &device_id, NULL);
  185. if (err != CL_SUCCESS)
  186. {
  187. FPRINTF(stderr, "WARNING: No GPU devices found on OpenCL platform\n");
  188. return STARPU_TEST_SKIPPED;
  189. }
  190. struct starpu_driver d =
  191. {
  192. .type = STARPU_OPENCL_WORKER,
  193. .id.opencl_id = device_id
  194. };
  195. starpu_conf_init(&conf);
  196. conf.n_not_launched_drivers = 1;
  197. conf.not_launched_drivers = &d;
  198. conf.ncuda = 0;
  199. conf.nopencl = 1;
  200. ret = starpu_init(&conf);
  201. if (ret == -ENODEV || starpu_opencl_worker_get_count() == 0)
  202. {
  203. FPRINTF(stderr, "WARNING: No OpenCL workers found\n");
  204. if (ret == 0)
  205. starpu_shutdown();
  206. return STARPU_TEST_SKIPPED;
  207. }
  208. ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
  209. if (ret == -1)
  210. {
  211. ret = 1;
  212. goto out;
  213. }
  214. struct starpu_task *task;
  215. task = starpu_task_create();
  216. cl.where = STARPU_OPENCL;
  217. task->cl = &cl;
  218. task->cl_arg = &var;
  219. task->synchronous = 1;
  220. ret = starpu_task_submit(task);
  221. if (ret == -ENODEV)
  222. {
  223. FPRINTF(stderr, "WARNING: No worker can execute the task\n");
  224. ret = STARPU_TEST_SKIPPED;
  225. goto out;
  226. }
  227. out:
  228. starpu_drivers_request_termination();
  229. if (starpu_pthread_join(driver_thread, NULL) != 0)
  230. return 1;
  231. starpu_shutdown();
  232. FPRINTF(stderr, "[OpenCL] Var = %d (expected value: 1)\n", var);
  233. ret = !!(var != 1);
  234. return ret;
  235. }
  236. #endif /* STARPU_USE_OPENCL */
  237. int
  238. main(void)
  239. {
  240. int ret = STARPU_TEST_SKIPPED;
  241. #ifdef STARPU_USE_CPU
  242. ret = test_cpu();
  243. if (ret == 1)
  244. return 1;
  245. #endif
  246. #ifdef STARPU_USE_CUDA
  247. ret = test_cuda();
  248. if (ret == 1)
  249. return 1;
  250. #endif
  251. #ifdef STARPU_USE_OPENCL
  252. ret = test_opencl();
  253. if (ret == 1)
  254. return 1;
  255. #endif
  256. return ret;
  257. }