run_driver.c 6.5 KB

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