run_driver.c 6.2 KB

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