run_driver.c 6.9 KB

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