init_run_deinit.c 5.6 KB

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