simple_cpu_gpu_sched.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /*
  19. * Schedulers that are aware of the expected task length provided by the
  20. * perfmodels must make sure that :
  21. * - cpu_task is cheduled on a CPU.
  22. * - gpu_task is scheduled on a GPU.
  23. *
  24. * Applies to : heft, XXX : and to what other schedulers ?
  25. */
  26. static void
  27. 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. enum starpu_perf_archtype 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. enum starpu_perf_archtype 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. enum starpu_perf_archtype 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. enum starpu_perf_archtype 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. int i;
  89. for (i = STARPU_CPU_DEFAULT; i < STARPU_CUDA_DEFAULT; i++)
  90. {
  91. model_cpu_task.per_arch[i][0].cost_function = cpu_task_cpu;
  92. model_gpu_task.per_arch[i][0].cost_function = gpu_task_cpu;
  93. }
  94. for (i = STARPU_CUDA_DEFAULT; i < STARPU_GORDON_DEFAULT; i++)
  95. {
  96. model_cpu_task.per_arch[i][0].cost_function = cpu_task_gpu;
  97. model_gpu_task.per_arch[i][0].cost_function = gpu_task_gpu;
  98. }
  99. }
  100. /*
  101. * Dummy codelets.
  102. */
  103. static struct starpu_codelet cpu_cl =
  104. {
  105. .cpu_funcs = { dummy, NULL },
  106. .cuda_funcs = { dummy, NULL },
  107. .opencl_funcs = { dummy, NULL },
  108. .nbuffers = 0,
  109. .model = &model_cpu_task
  110. };
  111. static struct starpu_codelet gpu_cl =
  112. {
  113. .cpu_funcs = { dummy, NULL },
  114. .cuda_funcs = { dummy, NULL },
  115. .opencl_funcs = { dummy, NULL },
  116. .nbuffers = 0,
  117. .model = &model_gpu_task
  118. };
  119. static int
  120. run(struct starpu_sched_policy *policy)
  121. {
  122. struct starpu_conf conf;
  123. starpu_conf_init(&conf);
  124. conf.sched_policy = policy;
  125. int ret = starpu_init(&conf);
  126. if (ret == -ENODEV)
  127. exit(STARPU_TEST_SKIPPED);
  128. /* At least 1 CPU and 1 GPU are needed. */
  129. if (starpu_cpu_worker_get_count() == 0)
  130. exit(STARPU_TEST_SKIPPED);
  131. if (starpu_cuda_worker_get_count() == 0 &&
  132. starpu_opencl_worker_get_count() == 0)
  133. exit(STARPU_TEST_SKIPPED);
  134. starpu_profiling_status_set(1);
  135. init_perfmodels();
  136. struct starpu_task *cpu_task = starpu_task_create();
  137. cpu_task->cl = &cpu_cl;
  138. cpu_task->destroy = 0;
  139. struct starpu_task *gpu_task = starpu_task_create();
  140. gpu_task->cl = &gpu_cl;
  141. gpu_task->destroy = 0;
  142. ret = starpu_task_submit(cpu_task);
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  144. ret = starpu_task_submit(gpu_task);
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  146. starpu_task_wait_for_all();
  147. enum starpu_archtype cpu_task_worker, gpu_task_worker;
  148. cpu_task_worker = starpu_worker_get_type(cpu_task->profiling_info->workerid);
  149. gpu_task_worker = starpu_worker_get_type(gpu_task->profiling_info->workerid);
  150. if (cpu_task_worker != STARPU_CPU_WORKER ||
  151. (gpu_task_worker != STARPU_CUDA_WORKER &&
  152. gpu_task_worker != STARPU_OPENCL_WORKER))
  153. ret = 1;
  154. else
  155. ret = 0;
  156. starpu_task_destroy(cpu_task);
  157. starpu_task_destroy(gpu_task);
  158. starpu_shutdown();
  159. return ret;
  160. }
  161. /*
  162. extern struct starpu_sched_policy _starpu_sched_ws_policy;
  163. extern struct starpu_sched_policy _starpu_sched_prio_policy;
  164. extern struct starpu_sched_policy _starpu_sched_random_policy;
  165. extern struct starpu_sched_policy _starpu_sched_dm_policy;
  166. extern struct starpu_sched_policy _starpu_sched_dmda_policy;
  167. extern struct starpu_sched_policy _starpu_sched_dmda_ready_policy;
  168. extern struct starpu_sched_policy _starpu_sched_dmda_sorted_policy;
  169. extern struct starpu_sched_policy _starpu_sched_eager_policy;
  170. extern struct starpu_sched_policy _starpu_sched_parallel_heft_policy;
  171. extern struct starpu_sched_policy _starpu_sched_pgreedy_policy;
  172. */
  173. extern struct starpu_sched_policy _starpu_sched_heft_policy;
  174. /* XXX: what policies are we interested in ? */
  175. static struct starpu_sched_policy *policies[] =
  176. {
  177. //&_starpu_sched_ws_policy,
  178. //&_starpu_sched_prio_policy,
  179. //&_starpu_sched_dm_policy,
  180. //&_starpu_sched_dmda_policy,
  181. &_starpu_sched_heft_policy,
  182. //&_starpu_sched_dmda_ready_policy,
  183. //&_starpu_sched_dmda_sorted_policy,
  184. //&_starpu_sched_random_policy,
  185. //&_starpu_sched_eager_policy,
  186. //&_starpu_sched_parallel_heft_policy,
  187. //&_starpu_sched_pgreedy_policy
  188. };
  189. int
  190. main(void)
  191. {
  192. #ifndef STARPU_HAVE_SETENV
  193. /* XXX: is this macro used by all the schedulers we are interested in ? */
  194. #warning "setenv() is not available, skipping this test"
  195. return STARPU_TEST_SKIPPED;
  196. #else
  197. setenv("STARPU_SCHED_BETA", "0", 1);
  198. int i;
  199. int n_policies = sizeof(policies)/sizeof(policies[0]);
  200. for (i = 0; i < n_policies; ++i)
  201. {
  202. struct starpu_sched_policy *policy = policies[i];
  203. FPRINTF(stdout, "Running with policy %s.\n",
  204. policy->policy_name);
  205. int ret;
  206. ret = run(policy);
  207. if (ret == 1)
  208. return EXIT_FAILURE;
  209. }
  210. return EXIT_SUCCESS;
  211. #endif
  212. }