init_run_deinit.c 5.5 KB

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