run_driver.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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 dummy(void *buffers[], void *args)
  33. {
  34. (void) buffers;
  35. (*(int *)args)++;
  36. usleep(100000);
  37. }
  38. static struct starpu_codelet cl =
  39. {
  40. .cpu_funcs = { dummy },
  41. .cuda_funcs = { dummy },
  42. .opencl_funcs = { dummy },
  43. .nbuffers = 0
  44. };
  45. static void *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 test_cpu(void)
  55. {
  56. int ret, var = 0;
  57. static starpu_pthread_t driver_thread;
  58. struct starpu_conf conf;
  59. struct starpu_driver d =
  60. {
  61. .type = STARPU_CPU_WORKER,
  62. .id.cpu_id = 0
  63. };
  64. starpu_conf_init(&conf);
  65. conf.precedence_over_environment_variables = 1;
  66. conf.n_not_launched_drivers = 1;
  67. conf.not_launched_drivers = &d;
  68. starpu_conf_noworker(&conf);
  69. conf.ncpus = 1;
  70. ret = starpu_init(&conf);
  71. if (ret == -ENODEV || starpu_cpu_worker_get_count() == 0)
  72. {
  73. FPRINTF(stderr, "WARNING: No CPU worker found\n");
  74. if (ret == 0)
  75. starpu_shutdown();
  76. return STARPU_TEST_SKIPPED;
  77. }
  78. ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
  79. if (ret != 0)
  80. {
  81. ret = 1;
  82. goto out2;
  83. }
  84. struct starpu_task *task;
  85. task = starpu_task_create();
  86. cl.where = STARPU_CPU;
  87. task->cl = &cl;
  88. task->cl_arg = &var;
  89. task->synchronous = 1;
  90. ret = starpu_task_submit(task);
  91. if (ret == -ENODEV)
  92. {
  93. FPRINTF(stderr, "WARNING: No worker can execute this task\n");
  94. ret = STARPU_TEST_SKIPPED;
  95. goto out;
  96. }
  97. FPRINTF(stderr, "[CPU] Var = %d (expected value: 1)\n", var);
  98. ret = !!(var != 1);
  99. out:
  100. starpu_drivers_request_termination();
  101. if (starpu_pthread_join(driver_thread, NULL) != 0)
  102. return 1;
  103. out2:
  104. starpu_shutdown();
  105. return ret;
  106. }
  107. #endif /* STARPU_USE_CPU */
  108. #ifdef STARPU_USE_CUDA
  109. static int test_cuda(void)
  110. {
  111. int ret, var = 0;
  112. static starpu_pthread_t driver_thread;
  113. struct starpu_conf conf;
  114. struct starpu_driver d =
  115. {
  116. .type = STARPU_CUDA_WORKER,
  117. .id.cuda_id = 0
  118. };
  119. starpu_conf_init(&conf);
  120. conf.precedence_over_environment_variables = 1;
  121. conf.n_not_launched_drivers = 1;
  122. conf.not_launched_drivers = &d;
  123. starpu_conf_noworker(&conf);
  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. if (starpu_cuda_worker_get_count() > 1)
  134. {
  135. FPRINTF(stderr, "WARNING: More than one worker, this is not supported by this test\n");
  136. if (ret == 0)
  137. starpu_shutdown();
  138. return STARPU_TEST_SKIPPED;
  139. }
  140. ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
  141. if (ret == -1)
  142. goto out;
  143. struct starpu_task *task;
  144. task = starpu_task_create();
  145. cl.where = STARPU_CUDA;
  146. task->cl = &cl;
  147. task->cl_arg = &var;
  148. task->synchronous = 1;
  149. ret = starpu_task_submit(task);
  150. if (ret == -ENODEV)
  151. {
  152. FPRINTF(stderr, "WARNING: No worker can execute this task\n");
  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 test_opencl(void)
  167. {
  168. int ret, var = 0;
  169. static starpu_pthread_t driver_thread;
  170. struct starpu_conf conf;
  171. cl_int err;
  172. cl_uint pdummy;
  173. cl_platform_id platform;
  174. err = clGetPlatformIDs(1, &platform, &pdummy);
  175. if (err != CL_SUCCESS)
  176. {
  177. FPRINTF(stderr, "WARNING: No OpenCL platform found\n");
  178. return STARPU_TEST_SKIPPED;
  179. }
  180. cl_device_type device_type = CL_DEVICE_TYPE_GPU|CL_DEVICE_TYPE_ACCELERATOR;
  181. if (starpu_get_env_number("STARPU_OPENCL_ON_CPUS") > 0)
  182. device_type |= CL_DEVICE_TYPE_CPU;
  183. if (starpu_get_env_number("STARPU_OPENCL_ONLY_ON_CPUS") > 0)
  184. device_type = CL_DEVICE_TYPE_CPU;
  185. cl_device_id device_id;
  186. err = clGetDeviceIDs(platform, device_type, 1, &device_id, NULL);
  187. if (err != CL_SUCCESS)
  188. {
  189. FPRINTF(stderr, "WARNING: No GPU devices found on OpenCL platform\n");
  190. return STARPU_TEST_SKIPPED;
  191. }
  192. struct starpu_driver d =
  193. {
  194. .type = STARPU_OPENCL_WORKER,
  195. .id.opencl_id = device_id
  196. };
  197. starpu_conf_init(&conf);
  198. conf.precedence_over_environment_variables = 1;
  199. conf.n_not_launched_drivers = 1;
  200. conf.not_launched_drivers = &d;
  201. starpu_conf_noworker(&conf);
  202. conf.ncpus = 1;
  203. conf.nopencl = 1;
  204. ret = starpu_init(&conf);
  205. if (ret == -ENODEV || starpu_opencl_worker_get_count() == 0)
  206. {
  207. FPRINTF(stderr, "WARNING: No OpenCL workers found\n");
  208. if (ret == 0)
  209. starpu_shutdown();
  210. return STARPU_TEST_SKIPPED;
  211. }
  212. ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
  213. if (ret == -1)
  214. goto out;
  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. task->synchronous = 1;
  221. ret = starpu_task_submit(task);
  222. if (ret == -ENODEV)
  223. {
  224. FPRINTF(stderr, "WARNING: No worker can execute the task\n");
  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 main(void)
  238. {
  239. int ret = STARPU_TEST_SKIPPED;
  240. #ifdef STARPU_USE_CPU
  241. ret = test_cpu();
  242. if (ret == 1)
  243. return 1;
  244. #endif
  245. #ifdef STARPU_USE_CUDA
  246. ret = test_cuda();
  247. if (ret == 1)
  248. return 1;
  249. #endif
  250. #ifdef STARPU_USE_OPENCL
  251. ret = test_opencl();
  252. if (ret == 1)
  253. return 1;
  254. #endif
  255. return ret;
  256. }