init_run_deinit.c 6.0 KB

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