init_run_deinit.c 6.0 KB

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