dummy_modular_sched.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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. /*
  17. * This is an example of an application-defined scheduler.
  18. * This is a mere eager scheduler with a centralized list of tasks to schedule:
  19. * when a task becomes ready (push) it is put on the list. When a device
  20. * becomes ready (pop), a task is taken from the list.
  21. */
  22. #include <starpu.h>
  23. #include <starpu_scheduler.h>
  24. #include <starpu_sched_component.h>
  25. #ifdef STARPU_QUICK_CHECK
  26. #define NTASKS 320
  27. #elif !defined(STARPU_LONG_CHECK)
  28. #define NTASKS 3200
  29. #else
  30. #define NTASKS 32000
  31. #endif
  32. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  33. struct dummy_sched_params
  34. {
  35. int verbose;
  36. };
  37. struct dummy_sched_data
  38. {
  39. int verbose;
  40. struct starpu_task_list sched_list;
  41. starpu_pthread_mutex_t policy_mutex;
  42. };
  43. static void dummy_deinit_data(struct starpu_sched_component * component)
  44. {
  45. struct dummy_sched_data *data = component->data;
  46. STARPU_ASSERT(starpu_task_list_empty(&data->sched_list));
  47. if (data->verbose)
  48. fprintf(stderr, "Destroying Dummy scheduler\n");
  49. STARPU_PTHREAD_MUTEX_DESTROY(&data->policy_mutex);
  50. free(data);
  51. }
  52. static int dummy_push_task(struct starpu_sched_component *component, struct starpu_task *task)
  53. {
  54. struct dummy_sched_data *data = component->data;
  55. if (data->verbose)
  56. fprintf(stderr, "pushing task %p\n", task);
  57. /* NB: In this simplistic strategy, we assume that the context in which
  58. we push task has at least one worker*/
  59. /* lock all workers when pushing tasks on a list where all
  60. of them would pop for tasks */
  61. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  62. starpu_task_list_push_front(&data->sched_list, task);
  63. starpu_push_task_end(task);
  64. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  65. /* Tell below that they can now pull */
  66. component->can_pull(component);
  67. return 0;
  68. }
  69. static struct starpu_task *dummy_pull_task(struct starpu_sched_component *component, struct starpu_sched_component *to)
  70. {
  71. struct dummy_sched_data *data = component->data;
  72. if (data->verbose)
  73. fprintf(stderr, "%p pulling for a task\n", to);
  74. #ifdef STARPU_NON_BLOCKING_DRIVERS
  75. if (starpu_task_list_empty(&data->sched_list))
  76. return NULL;
  77. #endif
  78. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  79. struct starpu_task *task = NULL;
  80. if (!starpu_task_list_empty(&data->sched_list))
  81. task = starpu_task_list_pop_back(&data->sched_list);
  82. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  83. return task;
  84. }
  85. static int dummy_can_push(struct starpu_sched_component * component, struct starpu_sched_component * to)
  86. {
  87. struct dummy_sched_data *data = component->data;
  88. int didwork = 0;
  89. if (data->verbose)
  90. fprintf(stderr, "%p tells me I can push to him\n", to);
  91. struct starpu_task *task;
  92. task = starpu_sched_component_pump_to(component, to, &didwork);
  93. if (task)
  94. {
  95. if (data->verbose)
  96. fprintf(stderr, "oops, %p couldn't take our task\n", to);
  97. /* Oops, we couldn't push everything, put back this task */
  98. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  99. starpu_task_list_push_back(&data->sched_list, task);
  100. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  101. }
  102. else
  103. {
  104. if (data->verbose)
  105. {
  106. if (didwork)
  107. fprintf(stderr, "pushed some tasks to %p\n", to);
  108. else
  109. fprintf(stderr, "I didn't have anything for %p\n", to);
  110. }
  111. }
  112. /* There is room now */
  113. return didwork || starpu_sched_component_can_push(component, to);
  114. }
  115. static int dummy_can_pull(struct starpu_sched_component * component)
  116. {
  117. struct dummy_sched_data *data = component->data;
  118. if (data->verbose)
  119. fprintf(stderr,"telling below they can pull\n");
  120. return starpu_sched_component_can_pull(component);
  121. }
  122. struct starpu_sched_component *dummy_create(struct starpu_sched_tree *tree, struct dummy_sched_params *params)
  123. {
  124. struct starpu_sched_component *component = starpu_sched_component_create(tree, "dummy");
  125. struct dummy_sched_data *data = malloc(sizeof(*data));
  126. STARPU_PTHREAD_MUTEX_INIT(&data->policy_mutex, NULL);
  127. /* Create a linked-list of tasks and a condition variable to protect it */
  128. starpu_task_list_init(&data->sched_list);
  129. data->verbose = params->verbose;
  130. component->data = data;
  131. component->push_task = dummy_push_task;
  132. component->pull_task = dummy_pull_task;
  133. component->can_push = dummy_can_push;
  134. component->can_pull = dummy_can_pull;
  135. component->deinit_data = dummy_deinit_data;
  136. return component;
  137. }
  138. static void init_dummy_sched(unsigned sched_ctx_id)
  139. {
  140. FPRINTF(stderr, "Initialising Dummy scheduler\n");
  141. struct dummy_sched_params params =
  142. {
  143. .verbose = 0,
  144. };
  145. starpu_sched_component_initialize_simple_scheduler((starpu_sched_component_create_t) dummy_create, &params,
  146. STARPU_SCHED_SIMPLE_DECIDE_WORKERS |
  147. STARPU_SCHED_SIMPLE_FIFOS_BELOW |
  148. STARPU_SCHED_SIMPLE_FIFOS_BELOW_PRIO,
  149. sched_ctx_id);
  150. }
  151. static void deinit_dummy_sched(unsigned sched_ctx_id)
  152. {
  153. struct starpu_sched_tree *t = (struct starpu_sched_tree*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  154. starpu_sched_tree_destroy(t);
  155. }
  156. static struct starpu_sched_policy dummy_sched_policy =
  157. {
  158. .init_sched = init_dummy_sched,
  159. .deinit_sched = deinit_dummy_sched,
  160. .add_workers = starpu_sched_tree_add_workers,
  161. .remove_workers = starpu_sched_tree_remove_workers,
  162. .push_task = starpu_sched_tree_push_task,
  163. .pop_task = starpu_sched_tree_pop_task,
  164. .pre_exec_hook = starpu_sched_component_worker_pre_exec_hook,
  165. .post_exec_hook = starpu_sched_component_worker_post_exec_hook,
  166. .pop_every_task = NULL,
  167. .policy_name = "dummy",
  168. .policy_description = "dummy modular scheduling strategy",
  169. .worker_type = STARPU_WORKER_LIST,
  170. };
  171. void dummy_func(void *descr[], void *arg)
  172. {
  173. (void)descr;
  174. (void)arg;
  175. }
  176. static struct starpu_codelet dummy_codelet =
  177. {
  178. .cpu_funcs = {dummy_func},
  179. .cpu_funcs_name = {"dummy_func"},
  180. .cuda_funcs = {dummy_func},
  181. .opencl_funcs = {dummy_func},
  182. .fpga_funcs = {dummy_func},
  183. .model = &starpu_perfmodel_nop,
  184. .nbuffers = 0,
  185. .name = "dummy",
  186. };
  187. int main(void)
  188. {
  189. int ntasks = NTASKS;
  190. int ret;
  191. struct starpu_conf conf;
  192. char *sched = getenv("STARPU_SCHED");
  193. if (sched && sched[0])
  194. /* Testing a specific scheduler, no need to run this */
  195. return 77;
  196. starpu_conf_init(&conf);
  197. conf.sched_policy = &dummy_sched_policy,
  198. ret = starpu_init(&conf);
  199. if (ret == -ENODEV)
  200. return 77;
  201. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  202. #ifdef STARPU_QUICK_CHECK
  203. ntasks /= 100;
  204. #endif
  205. int i;
  206. for (i = 0; i < ntasks; i++)
  207. {
  208. struct starpu_task *task = starpu_task_create();
  209. task->cl = &dummy_codelet;
  210. task->cl_arg = NULL;
  211. ret = starpu_task_submit(task);
  212. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  213. }
  214. starpu_task_wait_for_all();
  215. starpu_shutdown();
  216. return 0;
  217. }