init_run_deinit.c 4.9 KB

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