init_run_deinit.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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;
  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. return STARPU_TEST_SKIPPED;
  76. init_driver(&d);
  77. int i;
  78. for (i = 0; i < NTASKS; i++)
  79. {
  80. struct starpu_task *task;
  81. task = starpu_task_create();
  82. cl.where = STARPU_CPU;
  83. task->cl = &cl;
  84. task->cl_arg = &var;
  85. run(task, &d);
  86. }
  87. deinit_driver(&d);
  88. starpu_task_wait_for_all();
  89. starpu_shutdown();
  90. FPRINTF(stderr, "[CPU] Var is %d\n", var);
  91. return !!(var != NTASKS);
  92. }
  93. #endif /* STARPU_USE_CPU */
  94. #ifdef STARPU_USE_CUDA
  95. static int
  96. test_cuda(void)
  97. {
  98. int var = 0, ret, ncuda;
  99. struct starpu_conf conf;
  100. ret = starpu_conf_init(&conf);
  101. if (ret == -EINVAL)
  102. return 1;
  103. struct starpu_driver d =
  104. {
  105. .type = STARPU_CUDA_WORKER,
  106. .id.cuda_id = 0
  107. };
  108. conf.ncuda = 1;
  109. conf.nopencl = 0;
  110. conf.not_launched_drivers = &d;
  111. conf.n_not_launched_drivers = 1;
  112. ret = starpu_init(&conf);
  113. if (ret == -ENODEV)
  114. return STARPU_TEST_SKIPPED;
  115. ncuda = starpu_cuda_worker_get_count();
  116. if (ncuda == 0)
  117. return STARPU_TEST_SKIPPED;
  118. init_driver(&d);
  119. int i;
  120. for (i = 0; i < NTASKS; i++)
  121. {
  122. struct starpu_task *task;
  123. task = starpu_task_create();
  124. cl.where = STARPU_CUDA;
  125. task->cl = &cl;
  126. task->cl_arg = &var;
  127. run(task, &d);
  128. }
  129. deinit_driver(&d);
  130. starpu_task_wait_for_all();
  131. starpu_shutdown();
  132. FPRINTF(stderr, "[CUDA] Var is %d\n", var);
  133. return !!(var != NTASKS);
  134. }
  135. #endif /* STARPU_USE_CUDA */
  136. #ifdef STARPU_USE_OPENCL
  137. static int
  138. test_opencl(void)
  139. {
  140. cl_int err;
  141. cl_platform_id platform;
  142. cl_uint dummy;
  143. int nopencl;
  144. err = clGetPlatformIDs(1, &platform, &dummy);
  145. if (err != CL_SUCCESS)
  146. {
  147. return STARPU_TEST_SKIPPED;
  148. }
  149. cl_device_id device_id;
  150. err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
  151. if (err != CL_SUCCESS)
  152. {
  153. return STARPU_TEST_SKIPPED;
  154. }
  155. int var = 0, ret;
  156. struct starpu_conf conf;
  157. ret = starpu_conf_init(&conf);
  158. if (ret == -EINVAL)
  159. return 1;
  160. struct starpu_driver d =
  161. {
  162. .type = STARPU_OPENCL_WORKER,
  163. .id.opencl_id = device_id
  164. };
  165. conf.ncuda = 0;
  166. conf.nopencl = 1;
  167. conf.not_launched_drivers = &d;
  168. conf.n_not_launched_drivers = 1;
  169. ret = starpu_init(&conf);
  170. if (ret == -ENODEV)
  171. return STARPU_TEST_SKIPPED;
  172. nopencl = starpu_opencl_worker_get_count();
  173. if (nopencl == 0)
  174. return STARPU_TEST_SKIPPED;
  175. init_driver(&d);
  176. int i;
  177. for (i = 0; i < NTASKS; i++)
  178. {
  179. struct starpu_task *task;
  180. task = starpu_task_create();
  181. cl.where = STARPU_OPENCL;
  182. task->cl = &cl;
  183. task->cl_arg = &var;
  184. run(task, &d);
  185. }
  186. deinit_driver(&d);
  187. starpu_task_wait_for_all();
  188. starpu_shutdown();
  189. FPRINTF(stderr, "[OpenCL] Var is %d\n", var);
  190. return !!(var != NTASKS);
  191. }
  192. #endif /* STARPU_USE_OPENCL */
  193. int
  194. main(void)
  195. {
  196. int ret = STARPU_TEST_SKIPPED;
  197. #ifdef STARPU_USE_CPU
  198. ret = test_cpu();
  199. if (ret == 1)
  200. return ret;
  201. #endif
  202. #ifdef STARPU_USE_CUDA
  203. ret = test_cuda();
  204. if (ret == 1)
  205. return ret;
  206. #endif
  207. #ifdef STARPU_USE_OPENCL
  208. ret = test_opencl();
  209. if (ret == 1)
  210. return ret;
  211. #endif
  212. return ret;
  213. }