simple_cpu_gpu_sched.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2014,2017 Inria
  4. * Copyright (C) 2012-2017 CNRS
  5. * Copyright (C) 2013,2014,2016 Université de Bordeaux
  6. * Copyright (C) 2013 Thibaut Lambert
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <starpu_scheduler.h>
  21. #include "../helper.h"
  22. #include <core/perfmodel/perfmodel.h>
  23. /*
  24. * Schedulers that are aware of the expected task length provided by the
  25. * perfmodels must make sure that :
  26. * - cpu_task is cheduled on a CPU.
  27. * - gpu_task is scheduled on a GPU.
  28. *
  29. * Applies to : dmda and to what other schedulers ?
  30. */
  31. void dummy(void *buffers[], void *args)
  32. {
  33. (void) buffers;
  34. (void) args;
  35. }
  36. /*
  37. * Fake cost functions.
  38. */
  39. static double
  40. cpu_task_cpu(struct starpu_task *task,
  41. struct starpu_perfmodel_arch* arch,
  42. unsigned nimpl)
  43. {
  44. (void) task;
  45. (void) arch;
  46. (void) nimpl;
  47. return 1.0;
  48. }
  49. static double
  50. cpu_task_gpu(struct starpu_task *task,
  51. struct starpu_perfmodel_arch* arch,
  52. unsigned nimpl)
  53. {
  54. (void) task;
  55. (void) arch;
  56. (void) nimpl;
  57. return 10000000.0;
  58. }
  59. static double
  60. gpu_task_cpu(struct starpu_task *task,
  61. struct starpu_perfmodel_arch* arch,
  62. unsigned nimpl)
  63. {
  64. (void) task;
  65. (void) arch;
  66. (void) nimpl;
  67. return 10000000.0;
  68. }
  69. static double
  70. gpu_task_gpu(struct starpu_task *task,
  71. struct starpu_perfmodel_arch* arch,
  72. unsigned nimpl)
  73. {
  74. (void) task;
  75. (void) arch;
  76. (void) nimpl;
  77. return 1.0;
  78. }
  79. static struct starpu_perfmodel model_cpu_task =
  80. {
  81. .type = STARPU_PER_ARCH,
  82. .symbol = "model_cpu_task"
  83. };
  84. static struct starpu_perfmodel model_gpu_task =
  85. {
  86. .type = STARPU_PER_ARCH,
  87. .symbol = "model_gpu_task"
  88. };
  89. static void
  90. init_perfmodels_gpu(int gpu_type)
  91. {
  92. int nb_worker_gpu = starpu_worker_get_count_by_type(gpu_type);
  93. int *worker_gpu_ids = malloc(nb_worker_gpu * sizeof(int));
  94. int worker_gpu;
  95. starpu_worker_get_ids_by_type(gpu_type, worker_gpu_ids, nb_worker_gpu);
  96. for(worker_gpu = 0 ; worker_gpu < nb_worker_gpu ; worker_gpu ++)
  97. {
  98. starpu_perfmodel_set_per_devices_cost_function(&model_cpu_task, 0, cpu_task_gpu,
  99. gpu_type, starpu_worker_get_devid(worker_gpu_ids[worker_gpu]), 1,
  100. -1);
  101. starpu_perfmodel_set_per_devices_cost_function(&model_gpu_task, 0, gpu_task_gpu,
  102. gpu_type, starpu_worker_get_devid(worker_gpu_ids[worker_gpu]), 1,
  103. -1);
  104. }
  105. free(worker_gpu_ids);
  106. }
  107. static void
  108. init_perfmodels(void)
  109. {
  110. starpu_perfmodel_init(&model_cpu_task);
  111. starpu_perfmodel_init(&model_gpu_task);
  112. starpu_perfmodel_set_per_devices_cost_function(&model_cpu_task, 0, cpu_task_cpu, STARPU_CPU_WORKER, 0, 1, -1);
  113. starpu_perfmodel_set_per_devices_cost_function(&model_gpu_task, 0, gpu_task_cpu, STARPU_CPU_WORKER, 0, 1, -1);
  114. // We need to set the cost function for each combination with a CUDA or a OpenCL worker
  115. init_perfmodels_gpu(STARPU_CUDA_WORKER);
  116. init_perfmodels_gpu(STARPU_OPENCL_WORKER);
  117. }
  118. /*
  119. * Dummy codelets.
  120. */
  121. static struct starpu_codelet cpu_cl =
  122. {
  123. .cpu_funcs = { dummy },
  124. .cuda_funcs = { dummy },
  125. .opencl_funcs = { dummy },
  126. .nbuffers = 0,
  127. .model = &model_cpu_task
  128. };
  129. static struct starpu_codelet gpu_cl =
  130. {
  131. .cpu_funcs = { dummy },
  132. .cuda_funcs = { dummy },
  133. .opencl_funcs = { dummy },
  134. .nbuffers = 0,
  135. .model = &model_gpu_task
  136. };
  137. static int
  138. run(struct starpu_sched_policy *policy)
  139. {
  140. struct starpu_conf conf;
  141. starpu_conf_init(&conf);
  142. conf.sched_policy = policy;
  143. int ret = starpu_init(&conf);
  144. if (ret == -ENODEV)
  145. exit(STARPU_TEST_SKIPPED);
  146. /* At least 1 CPU and 1 GPU are needed. */
  147. if (starpu_cpu_worker_get_count() == 0)
  148. {
  149. starpu_shutdown();
  150. exit(STARPU_TEST_SKIPPED);
  151. }
  152. if (starpu_cuda_worker_get_count() == 0 && starpu_opencl_worker_get_count() == 0)
  153. {
  154. starpu_shutdown();
  155. exit(STARPU_TEST_SKIPPED);
  156. }
  157. starpu_profiling_status_set(1);
  158. init_perfmodels();
  159. struct starpu_task *cpu_task = starpu_task_create();
  160. cpu_task->cl = &cpu_cl;
  161. cpu_task->destroy = 0;
  162. struct starpu_task *gpu_task = starpu_task_create();
  163. gpu_task->cl = &gpu_cl;
  164. gpu_task->destroy = 0;
  165. ret = starpu_task_submit(cpu_task);
  166. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  167. ret = starpu_task_submit(gpu_task);
  168. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  169. starpu_task_wait_for_all();
  170. enum starpu_worker_archtype cpu_task_worker, gpu_task_worker;
  171. cpu_task_worker = starpu_worker_get_type(cpu_task->profiling_info->workerid);
  172. gpu_task_worker = starpu_worker_get_type(gpu_task->profiling_info->workerid);
  173. if (cpu_task_worker != STARPU_CPU_WORKER || (gpu_task_worker != STARPU_CUDA_WORKER && gpu_task_worker != STARPU_OPENCL_WORKER))
  174. {
  175. FPRINTF(stderr, "Tasks did not execute on expected worker\n");
  176. if (cpu_task_worker != STARPU_CPU_WORKER)
  177. {
  178. FPRINTF(stderr, "The CPU task did not run on a CPU worker\n");
  179. }
  180. if (gpu_task_worker != STARPU_CUDA_WORKER && gpu_task_worker != STARPU_OPENCL_WORKER)
  181. {
  182. FPRINTF(stderr, "The GPU task did not run on a Cuda or OpenCL worker\n");
  183. }
  184. ret = 1;
  185. }
  186. else
  187. {
  188. FPRINTF(stderr, "Tasks DID execute on expected worker\n");
  189. ret = 0;
  190. }
  191. starpu_task_destroy(cpu_task);
  192. starpu_task_destroy(gpu_task);
  193. starpu_shutdown();
  194. return ret;
  195. }
  196. /*
  197. extern struct starpu_sched_policy _starpu_sched_ws_policy;
  198. extern struct starpu_sched_policy _starpu_sched_prio_policy;
  199. extern struct starpu_sched_policy _starpu_sched_random_policy;
  200. extern struct starpu_sched_policy _starpu_sched_dm_policy;
  201. extern struct starpu_sched_policy _starpu_sched_dmda_ready_policy;
  202. extern struct starpu_sched_policy _starpu_sched_dmda_sorted_policy;
  203. extern struct starpu_sched_policy _starpu_sched_eager_policy;
  204. extern struct starpu_sched_policy _starpu_sched_parallel_heft_policy;
  205. extern struct starpu_sched_policy _starpu_sched_peager_policy;
  206. */
  207. extern struct starpu_sched_policy _starpu_sched_dmda_policy;
  208. /* XXX: what policies are we interested in ? */
  209. static struct starpu_sched_policy *policies[] =
  210. {
  211. //&_starpu_sched_ws_policy,
  212. //&_starpu_sched_prio_policy,
  213. //&_starpu_sched_dm_policy,
  214. &_starpu_sched_dmda_policy,
  215. //&_starpu_sched_dmda_ready_policy,
  216. //&_starpu_sched_dmda_sorted_policy,
  217. //&_starpu_sched_random_policy,
  218. //&_starpu_sched_eager_policy,
  219. //&_starpu_sched_parallel_heft_policy,
  220. //&_starpu_sched_peager_policy
  221. };
  222. int main(void)
  223. {
  224. #ifndef STARPU_HAVE_SETENV
  225. /* XXX: is this macro used by all the schedulers we are interested in ? */
  226. #warning "setenv() is not available, skipping this test"
  227. return STARPU_TEST_SKIPPED;
  228. #else
  229. setenv("STARPU_SCHED_BETA", "0", 1);
  230. #ifdef STARPU_HAVE_UNSETENV
  231. unsetenv("STARPU_SCHED");
  232. #endif
  233. if (starpu_get_env_number_default("STARPU_NWORKER_PER_CUDA", 1) != 1)
  234. return STARPU_TEST_SKIPPED;
  235. int i;
  236. int n_policies = sizeof(policies)/sizeof(policies[0]);
  237. for (i = 0; i < n_policies; ++i)
  238. {
  239. struct starpu_sched_policy *policy = policies[i];
  240. FPRINTF(stdout, "Running with policy %s.\n",
  241. policy->policy_name);
  242. int ret;
  243. ret = run(policy);
  244. if (ret == 1)
  245. return EXIT_FAILURE;
  246. }
  247. return EXIT_SUCCESS;
  248. #endif
  249. }