dummy_modular_sched.c 7.2 KB

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