simple_cpu_gpu_sched.c 7.4 KB

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