init_run_deinit.c 5.9 KB

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