run_driver.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifdef STARPU_USE_OPENCL
  19. #include <starpu_opencl.h>
  20. #endif
  21. #include "../../helper.h"
  22. /*
  23. * Users can directly control drivers by using the starpu_driver* functions.
  24. *
  25. * This test makes sure that the starpu_driver_run function works for CPU, CUDA
  26. * and OpenCL drivers, and that the starpu_drivers_request_termination function
  27. * correctly shuts down all drivers.
  28. *
  29. * The test_* functions can return:
  30. * - 0 (success)
  31. * - 1 (failure)
  32. * - STARPU_TEST_SKIPPED (non-critical errors)
  33. */
  34. #if defined(STARPU_USE_CPU) || defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  35. static void
  36. dummy(void *buffers[], void *args)
  37. {
  38. (void) buffers;
  39. (*(int *)args)++;
  40. }
  41. static struct starpu_codelet cl =
  42. {
  43. .cpu_funcs = { dummy, NULL },
  44. .cuda_funcs = { dummy, NULL },
  45. .opencl_funcs = { dummy, NULL },
  46. .nbuffers = 0
  47. };
  48. static void *
  49. run_driver(void *arg)
  50. {
  51. struct starpu_driver *d = (struct starpu_driver *) arg;
  52. int ret = starpu_driver_run(d);
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_driver_run");
  54. return NULL;
  55. }
  56. #endif /* STARPU_USE_CPU || STARPU_USE_CUDA || STARPU_USE_OPENCL */
  57. #ifdef STARPU_USE_CPU
  58. static int
  59. test_cpu(void)
  60. {
  61. int ret, var = 0;
  62. static pthread_t driver_thread;
  63. struct starpu_conf conf;
  64. struct starpu_driver d =
  65. {
  66. .type = STARPU_CPU_WORKER,
  67. .id.cpu_id = 0
  68. };
  69. starpu_conf_init(&conf);
  70. conf.n_not_launched_drivers = 1;
  71. conf.not_launched_drivers = &d;
  72. conf.ncpus = 1;
  73. ret = starpu_init(&conf);
  74. if (ret == -ENODEV || starpu_cpu_worker_get_count() == 0)
  75. return STARPU_TEST_SKIPPED;
  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. ret = STARPU_TEST_SKIPPED;
  92. goto out;
  93. }
  94. FPRINTF(stderr, "[CPU] Var = %d\n", var);
  95. ret = !!(var != 1);
  96. out:
  97. starpu_drivers_request_termination();
  98. if (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 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. return STARPU_TEST_SKIPPED;
  123. ret = pthread_create(&driver_thread, NULL, run_driver, &d);
  124. if (ret == -1)
  125. {
  126. ret = 1;
  127. goto out;
  128. }
  129. struct starpu_task *task;
  130. task = starpu_task_create();
  131. cl.where = STARPU_CUDA;
  132. task->cl = &cl;
  133. task->cl_arg = &var;
  134. task->synchronous = 1;
  135. ret = starpu_task_submit(task);
  136. if (ret == -ENODEV)
  137. {
  138. ret = STARPU_TEST_SKIPPED;
  139. goto out;
  140. }
  141. FPRINTF(stderr, "[CUDA] Var = %d\n", var);
  142. ret = !!(var != 1);
  143. out:
  144. starpu_drivers_request_termination();
  145. if (pthread_join(driver_thread, NULL) != 0)
  146. return 1;
  147. starpu_shutdown();
  148. return ret;
  149. }
  150. #endif /* STARPU_USE_CUDA */
  151. #ifdef STARPU_USE_OPENCL
  152. static int
  153. test_opencl(void)
  154. {
  155. int ret, var = 0;
  156. static pthread_t driver_thread;
  157. struct starpu_conf conf;
  158. cl_int err;
  159. cl_uint dummy;
  160. cl_platform_id platform;
  161. err = clGetPlatformIDs(1, &platform, &dummy);
  162. if (err != CL_SUCCESS)
  163. return STARPU_TEST_SKIPPED;
  164. cl_device_id device_id;
  165. int device_type = CL_DEVICE_TYPE_GPU; /* TODO Support CPU */
  166. err = clGetDeviceIDs(platform, device_type, 1, &device_id, NULL);
  167. if (err != CL_SUCCESS)
  168. return STARPU_TEST_SKIPPED;
  169. struct starpu_driver d =
  170. {
  171. .type = STARPU_OPENCL_WORKER,
  172. .id.opencl_id = device_id
  173. };
  174. starpu_conf_init(&conf);
  175. conf.n_not_launched_drivers = 1;
  176. conf.not_launched_drivers = &d;
  177. conf.ncuda = 0;
  178. conf.nopencl = 1;
  179. ret = starpu_init(&conf);
  180. if (ret == -ENODEV || starpu_opencl_worker_get_count() == 0)
  181. return STARPU_TEST_SKIPPED;
  182. ret = pthread_create(&driver_thread, NULL, run_driver, &d);
  183. if (ret == -1)
  184. {
  185. ret = 1;
  186. goto out;
  187. }
  188. struct starpu_task *task;
  189. task = starpu_task_create();
  190. cl.where = STARPU_OPENCL;
  191. task->cl = &cl;
  192. task->cl_arg = &var;
  193. task->synchronous = 1;
  194. ret = starpu_task_submit(task);
  195. if (ret == -ENODEV)
  196. {
  197. ret = STARPU_TEST_SKIPPED;
  198. goto out;
  199. }
  200. FPRINTF(stderr, "[OpenCL] Var = %d\n", var);
  201. ret = !!(var != 1);
  202. out:
  203. starpu_drivers_request_termination();
  204. if (pthread_join(driver_thread, NULL) != 0)
  205. return 1;
  206. starpu_shutdown();
  207. return ret;
  208. }
  209. #endif /* STARPU_USE_OPENCL */
  210. int
  211. main(void)
  212. {
  213. int ret = STARPU_TEST_SKIPPED;
  214. #ifdef STARPU_USE_CPU
  215. ret = test_cpu();
  216. if (ret == 1)
  217. return 1;
  218. #endif
  219. #ifdef STARPU_USE_CUDA
  220. ret = test_cuda();
  221. if (ret == 1)
  222. return 1;
  223. #endif
  224. #ifdef STARPU_USE_OPENCL
  225. ret = test_opencl();
  226. if (ret == 1)
  227. return 1;
  228. #endif
  229. return ret;
  230. }