run_driver.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. return STARPU_TEST_SKIPPED;
  73. ret = pthread_create(&driver_thread, NULL, run_driver, &d);
  74. if (ret != 0)
  75. {
  76. ret = 1;
  77. goto out;
  78. }
  79. struct starpu_task *task;
  80. task = starpu_task_create();
  81. cl.where = STARPU_CPU;
  82. task->cl = &cl;
  83. task->cl_arg = &var;
  84. task->synchronous = 1;
  85. ret = starpu_task_submit(task);
  86. if (ret == -ENODEV)
  87. {
  88. ret = STARPU_TEST_SKIPPED;
  89. goto out;
  90. }
  91. FPRINTF(stderr, "[CPU] Var = %d (expected value: 1)\n", var);
  92. ret = !!(var != 1);
  93. out:
  94. starpu_drivers_request_termination();
  95. if (pthread_join(driver_thread, NULL) != 0)
  96. return 1;
  97. starpu_shutdown();
  98. return ret;
  99. }
  100. #endif /* STARPU_USE_CPU */
  101. #ifdef STARPU_USE_CUDA
  102. static int
  103. test_cuda(void)
  104. {
  105. int ret, var = 0;
  106. static pthread_t driver_thread;
  107. struct starpu_conf conf;
  108. struct starpu_driver d =
  109. {
  110. .type = STARPU_CUDA_WORKER,
  111. .id.cuda_id = 0
  112. };
  113. starpu_conf_init(&conf);
  114. conf.n_not_launched_drivers = 1;
  115. conf.not_launched_drivers = &d;
  116. conf.ncuda = 1;
  117. ret = starpu_init(&conf);
  118. if (ret == -ENODEV || starpu_cuda_worker_get_count() == 0)
  119. return STARPU_TEST_SKIPPED;
  120. ret = pthread_create(&driver_thread, NULL, run_driver, &d);
  121. if (ret == -1)
  122. {
  123. ret = 1;
  124. goto out;
  125. }
  126. struct starpu_task *task;
  127. task = starpu_task_create();
  128. cl.where = STARPU_CUDA;
  129. task->cl = &cl;
  130. task->cl_arg = &var;
  131. task->synchronous = 1;
  132. ret = starpu_task_submit(task);
  133. if (ret == -ENODEV)
  134. {
  135. ret = STARPU_TEST_SKIPPED;
  136. goto out;
  137. }
  138. FPRINTF(stderr, "[CUDA] Var = %d (expected value: 1)\n", var);
  139. ret = !!(var != 1);
  140. out:
  141. starpu_drivers_request_termination();
  142. if (pthread_join(driver_thread, NULL) != 0)
  143. return 1;
  144. starpu_shutdown();
  145. return ret;
  146. }
  147. #endif /* STARPU_USE_CUDA */
  148. #ifdef STARPU_USE_OPENCL
  149. static int
  150. test_opencl(void)
  151. {
  152. int ret, var = 0;
  153. static pthread_t driver_thread;
  154. struct starpu_conf conf;
  155. cl_int err;
  156. cl_uint dummy;
  157. cl_platform_id platform;
  158. err = clGetPlatformIDs(1, &platform, &dummy);
  159. if (err != CL_SUCCESS)
  160. return STARPU_TEST_SKIPPED;
  161. cl_device_id device_id;
  162. int device_type = CL_DEVICE_TYPE_GPU; /* TODO Support CPU */
  163. err = clGetDeviceIDs(platform, device_type, 1, &device_id, NULL);
  164. if (err != CL_SUCCESS)
  165. return STARPU_TEST_SKIPPED;
  166. struct starpu_driver d =
  167. {
  168. .type = STARPU_OPENCL_WORKER,
  169. .id.opencl_id = device_id
  170. };
  171. starpu_conf_init(&conf);
  172. conf.n_not_launched_drivers = 1;
  173. conf.not_launched_drivers = &d;
  174. conf.ncuda = 0;
  175. conf.nopencl = 1;
  176. ret = starpu_init(&conf);
  177. if (ret == -ENODEV || starpu_opencl_worker_get_count() == 0)
  178. return STARPU_TEST_SKIPPED;
  179. ret = pthread_create(&driver_thread, NULL, run_driver, &d);
  180. if (ret == -1)
  181. {
  182. ret = 1;
  183. goto out;
  184. }
  185. struct starpu_task *task;
  186. task = starpu_task_create();
  187. cl.where = STARPU_OPENCL;
  188. task->cl = &cl;
  189. task->cl_arg = &var;
  190. task->synchronous = 1;
  191. ret = starpu_task_submit(task);
  192. if (ret == -ENODEV)
  193. {
  194. ret = STARPU_TEST_SKIPPED;
  195. goto out;
  196. }
  197. FPRINTF(stderr, "[OpenCL] Var = %d (expected value: 1)\n", var);
  198. ret = !!(var != 1);
  199. out:
  200. starpu_drivers_request_termination();
  201. if (pthread_join(driver_thread, NULL) != 0)
  202. return 1;
  203. starpu_shutdown();
  204. return ret;
  205. }
  206. #endif /* STARPU_USE_OPENCL */
  207. int
  208. main(void)
  209. {
  210. int ret = STARPU_TEST_SKIPPED;
  211. #ifdef STARPU_USE_CPU
  212. ret = test_cpu();
  213. if (ret == 1)
  214. return 1;
  215. #endif
  216. #ifdef STARPU_USE_CUDA
  217. ret = test_cuda();
  218. if (ret == 1)
  219. return 1;
  220. #endif
  221. #ifdef STARPU_USE_OPENCL
  222. ret = test_opencl();
  223. if (ret == 1)
  224. return 1;
  225. #endif
  226. return ret;
  227. }