run_driver.c 6.0 KB

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