simple_cpu_gpu_sched.c 7.4 KB

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