simple_cpu_gpu_sched.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_scheduler.h>
  18. #include "../helper.h"
  19. /*
  20. * Schedulers that are aware of the expected task length provided by the
  21. * perfmodels must make sure that :
  22. * - cpu_task is cheduled on a CPU.
  23. * - gpu_task is scheduled on a GPU.
  24. *
  25. * Applies to : dmda and to what other schedulers ?
  26. */
  27. void dummy(void *buffers[], void *args)
  28. {
  29. (void) buffers;
  30. (void) args;
  31. }
  32. /*
  33. * Fake cost functions.
  34. */
  35. static double
  36. cpu_task_cpu(struct starpu_task *task,
  37. struct starpu_perfmodel_arch* arch,
  38. unsigned nimpl)
  39. {
  40. (void) task;
  41. (void) arch;
  42. (void) nimpl;
  43. return 1.0;
  44. }
  45. static double
  46. cpu_task_gpu(struct starpu_task *task,
  47. struct starpu_perfmodel_arch* arch,
  48. unsigned nimpl)
  49. {
  50. (void) task;
  51. (void) arch;
  52. (void) nimpl;
  53. return 1000.0;
  54. }
  55. static double
  56. gpu_task_cpu(struct starpu_task *task,
  57. struct starpu_perfmodel_arch* arch,
  58. unsigned nimpl)
  59. {
  60. (void) task;
  61. (void) arch;
  62. (void) nimpl;
  63. return 1000.0;
  64. }
  65. static double
  66. gpu_task_gpu(struct starpu_task *task,
  67. struct starpu_perfmodel_arch* arch,
  68. unsigned nimpl)
  69. {
  70. (void) task;
  71. (void) arch;
  72. (void) nimpl;
  73. return 1.0;
  74. }
  75. static struct starpu_perfmodel model_cpu_task =
  76. {
  77. .type = STARPU_PER_ARCH,
  78. .symbol = "model_cpu_task"
  79. };
  80. static struct starpu_perfmodel model_gpu_task =
  81. {
  82. .type = STARPU_PER_ARCH,
  83. .symbol = "model_gpu_task"
  84. };
  85. static void
  86. init_perfmodels(void)
  87. {
  88. unsigned devid, ncore;
  89. starpu_perfmodel_init(NULL, &model_cpu_task);
  90. starpu_perfmodel_init(NULL, &model_gpu_task);
  91. struct starpu_perfmodel_arch arch_cpu;
  92. arch_cpu.ndevices = 1;
  93. arch_cpu.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  94. arch_cpu.devices[0].type = STARPU_CPU_WORKER;
  95. arch_cpu.devices[0].devid = 0;
  96. arch_cpu.devices[0].ncores = 1;
  97. int comb_cpu = starpu_get_arch_comb(arch_cpu.ndevices, arch_cpu.devices);
  98. if(comb_cpu == -1)
  99. comb_cpu = starpu_add_arch_comb(arch_cpu.ndevices, arch_cpu.devices);
  100. model_cpu_task.per_arch[comb_cpu] = (struct starpu_perfmodel_per_arch*)malloc(sizeof(struct starpu_perfmodel_per_arch));
  101. memset(&model_cpu_task.per_arch[comb_cpu][0], 0, sizeof(struct starpu_perfmodel_per_arch));
  102. model_cpu_task.nimpls[comb_cpu] = 1;
  103. model_cpu_task.per_arch[comb_cpu][0].cost_function = cpu_task_cpu;
  104. model_gpu_task.per_arch[comb_cpu] = (struct starpu_perfmodel_per_arch*)malloc(sizeof(struct starpu_perfmodel_per_arch));
  105. memset(&model_gpu_task.per_arch[comb_cpu][0], 0, sizeof(struct starpu_perfmodel_per_arch));
  106. model_gpu_task.nimpls[comb_cpu] = 1;
  107. model_gpu_task.per_arch[comb_cpu][0].cost_function = gpu_task_cpu;
  108. struct starpu_perfmodel_arch arch_cuda;
  109. arch_cuda.ndevices = 1;
  110. arch_cuda.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  111. arch_cuda.devices[0].type = STARPU_CUDA_WORKER;
  112. arch_cuda.devices[0].devid = 0;
  113. arch_cuda.devices[0].ncores = 1;
  114. int comb_cuda = starpu_get_arch_comb(arch_cuda.ndevices, arch_cuda.devices);
  115. if(comb_cuda == -1)
  116. comb_cuda = starpu_add_arch_comb(arch_cuda.ndevices, arch_cuda.devices);
  117. model_cpu_task.per_arch[comb_cuda] = (struct starpu_perfmodel_per_arch*)malloc(sizeof(struct starpu_perfmodel_per_arch));
  118. memset(&model_cpu_task.per_arch[comb_cuda][0], 0, sizeof(struct starpu_perfmodel_per_arch));
  119. model_cpu_task.nimpls[comb_cuda] = 1;
  120. model_cpu_task.per_arch[comb_cuda][0].cost_function = cpu_task_cpu;
  121. model_gpu_task.per_arch[comb_cuda] = (struct starpu_perfmodel_per_arch*)malloc(sizeof(struct starpu_perfmodel_per_arch));
  122. memset(&model_gpu_task.per_arch[comb_cuda][0], 0, sizeof(struct starpu_perfmodel_per_arch));
  123. model_gpu_task.nimpls[comb_cuda] = 1;
  124. model_gpu_task.per_arch[comb_cuda][0].cost_function = gpu_task_cpu;
  125. /* if(model_cpu_task.per_arch[STARPU_CPU_WORKER] != NULL) */
  126. /* { */
  127. /* for(devid=0; model_cpu_task.per_arch[STARPU_CPU_WORKER][devid] != NULL; devid++) */
  128. /* { */
  129. /* for(ncore=0; model_cpu_task.per_arch[STARPU_CPU_WORKER][devid][ncore] != NULL; ncore++) */
  130. /* { */
  131. /* model_cpu_task.per_arch[STARPU_CPU_WORKER][devid][ncore][0].cost_function = cpu_task_cpu; */
  132. /* model_gpu_task.per_arch[STARPU_CPU_WORKER][devid][ncore][0].cost_function = gpu_task_cpu; */
  133. /* } */
  134. /* } */
  135. /* } */
  136. /* if(model_cpu_task.per_arch[STARPU_CUDA_WORKER] != NULL) */
  137. /* { */
  138. /* for(devid=0; model_cpu_task.per_arch[STARPU_CUDA_WORKER][devid] != NULL; devid++) */
  139. /* { */
  140. /* for(ncore=0; model_cpu_task.per_arch[STARPU_CUDA_WORKER][devid][ncore] != NULL; ncore++) */
  141. /* { */
  142. /* model_cpu_task.per_arch[STARPU_CUDA_WORKER][devid][ncore][0].cost_function = cpu_task_gpu; */
  143. /* model_gpu_task.per_arch[STARPU_CUDA_WORKER][devid][ncore][0].cost_function = gpu_task_gpu; */
  144. /* } */
  145. /* } */
  146. /* } */
  147. /* if(model_cpu_task.per_arch[STARPU_OPENCL_WORKER] != NULL) */
  148. /* { */
  149. /* for(devid=0; model_cpu_task.per_arch[STARPU_OPENCL_WORKER][devid] != NULL; devid++) */
  150. /* { */
  151. /* for(ncore=0; model_cpu_task.per_arch[STARPU_OPENCL_WORKER][devid][ncore] != NULL; ncore++) */
  152. /* { */
  153. /* model_cpu_task.per_arch[STARPU_OPENCL_WORKER][devid][ncore][0].cost_function = cpu_task_gpu; */
  154. /* model_gpu_task.per_arch[STARPU_OPENCL_WORKER][devid][ncore][0].cost_function = gpu_task_gpu; */
  155. /* } */
  156. /* } */
  157. /* } */
  158. }
  159. /*
  160. * Dummy codelets.
  161. */
  162. static struct starpu_codelet cpu_cl =
  163. {
  164. .cpu_funcs = { dummy, NULL },
  165. .cuda_funcs = { dummy, NULL },
  166. .opencl_funcs = { dummy, NULL },
  167. .nbuffers = 0,
  168. .model = &model_cpu_task
  169. };
  170. static struct starpu_codelet gpu_cl =
  171. {
  172. .cpu_funcs = { dummy, NULL },
  173. .cuda_funcs = { dummy, NULL },
  174. .opencl_funcs = { dummy, NULL },
  175. .nbuffers = 0,
  176. .model = &model_gpu_task
  177. };
  178. static int
  179. run(struct starpu_sched_policy *policy)
  180. {
  181. struct starpu_conf conf;
  182. starpu_conf_init(&conf);
  183. conf.sched_policy = policy;
  184. int ret = starpu_init(&conf);
  185. if (ret == -ENODEV)
  186. exit(STARPU_TEST_SKIPPED);
  187. /* At least 1 CPU and 1 GPU are needed. */
  188. if (starpu_cpu_worker_get_count() == 0) {
  189. starpu_shutdown();
  190. exit(STARPU_TEST_SKIPPED);
  191. }
  192. if (starpu_cuda_worker_get_count() == 0 &&
  193. starpu_opencl_worker_get_count() == 0) {
  194. starpu_shutdown();
  195. exit(STARPU_TEST_SKIPPED);
  196. }
  197. starpu_profiling_status_set(1);
  198. init_perfmodels();
  199. struct starpu_task *cpu_task = starpu_task_create();
  200. cpu_task->cl = &cpu_cl;
  201. cpu_task->destroy = 0;
  202. struct starpu_task *gpu_task = starpu_task_create();
  203. gpu_task->cl = &gpu_cl;
  204. gpu_task->destroy = 0;
  205. ret = starpu_task_submit(cpu_task);
  206. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  207. ret = starpu_task_submit(gpu_task);
  208. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  209. starpu_task_wait_for_all();
  210. enum starpu_worker_archtype cpu_task_worker, gpu_task_worker;
  211. cpu_task_worker = starpu_worker_get_type(cpu_task->profiling_info->workerid);
  212. gpu_task_worker = starpu_worker_get_type(gpu_task->profiling_info->workerid);
  213. if (cpu_task_worker != STARPU_CPU_WORKER ||
  214. (gpu_task_worker != STARPU_CUDA_WORKER &&
  215. gpu_task_worker != STARPU_OPENCL_WORKER))
  216. ret = 1;
  217. else
  218. ret = 0;
  219. starpu_task_destroy(cpu_task);
  220. starpu_task_destroy(gpu_task);
  221. starpu_shutdown();
  222. return ret;
  223. }
  224. /*
  225. extern struct starpu_sched_policy _starpu_sched_ws_policy;
  226. extern struct starpu_sched_policy _starpu_sched_prio_policy;
  227. extern struct starpu_sched_policy _starpu_sched_random_policy;
  228. extern struct starpu_sched_policy _starpu_sched_dm_policy;
  229. extern struct starpu_sched_policy _starpu_sched_dmda_ready_policy;
  230. extern struct starpu_sched_policy _starpu_sched_dmda_sorted_policy;
  231. extern struct starpu_sched_policy _starpu_sched_eager_policy;
  232. extern struct starpu_sched_policy _starpu_sched_parallel_heft_policy;
  233. extern struct starpu_sched_policy _starpu_sched_peager_policy;
  234. */
  235. extern struct starpu_sched_policy _starpu_sched_dmda_policy;
  236. /* XXX: what policies are we interested in ? */
  237. static struct starpu_sched_policy *policies[] =
  238. {
  239. //&_starpu_sched_ws_policy,
  240. //&_starpu_sched_prio_policy,
  241. //&_starpu_sched_dm_policy,
  242. &_starpu_sched_dmda_policy,
  243. //&_starpu_sched_dmda_ready_policy,
  244. //&_starpu_sched_dmda_sorted_policy,
  245. //&_starpu_sched_random_policy,
  246. //&_starpu_sched_eager_policy,
  247. //&_starpu_sched_parallel_heft_policy,
  248. //&_starpu_sched_peager_policy
  249. };
  250. int
  251. main(void)
  252. {
  253. #ifndef STARPU_HAVE_SETENV
  254. /* XXX: is this macro used by all the schedulers we are interested in ? */
  255. #warning "setenv() is not available, skipping this test"
  256. return STARPU_TEST_SKIPPED;
  257. #else
  258. setenv("STARPU_SCHED_BETA", "0", 1);
  259. int i;
  260. int n_policies = sizeof(policies)/sizeof(policies[0]);
  261. for (i = 0; i < n_policies; ++i)
  262. {
  263. struct starpu_sched_policy *policy = policies[i];
  264. FPRINTF(stdout, "Running with policy %s.\n",
  265. policy->policy_name);
  266. int ret;
  267. ret = run(policy);
  268. if (ret == 1)
  269. return EXIT_FAILURE;
  270. }
  271. return EXIT_SUCCESS;
  272. #endif
  273. }