run_driver.c 7.0 KB

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