sched_policy.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <pthread.h>
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <common/utils.h>
  20. #include <core/sched_policy.h>
  21. #include <profiling/profiling.h>
  22. static struct starpu_sched_policy_s policy;
  23. static int use_prefetch = 0;
  24. int starpu_get_prefetch_flag(void)
  25. {
  26. return use_prefetch;
  27. }
  28. /*
  29. * Predefined policies
  30. */
  31. extern struct starpu_sched_policy_s _starpu_sched_ws_policy;
  32. extern struct starpu_sched_policy_s _starpu_sched_prio_policy;
  33. extern struct starpu_sched_policy_s _starpu_sched_no_prio_policy;
  34. extern struct starpu_sched_policy_s _starpu_sched_random_policy;
  35. extern struct starpu_sched_policy_s _starpu_sched_dm_policy;
  36. extern struct starpu_sched_policy_s _starpu_sched_dmda_policy;
  37. extern struct starpu_sched_policy_s _starpu_sched_dmda_ready_policy;
  38. extern struct starpu_sched_policy_s _starpu_sched_dmda_sorted_policy;
  39. extern struct starpu_sched_policy_s _starpu_sched_eager_policy;
  40. extern struct starpu_sched_policy_s _starpu_sched_parallel_heft_policy;
  41. #define NPREDEFINED_POLICIES 10
  42. static struct starpu_sched_policy_s *predefined_policies[NPREDEFINED_POLICIES] = {
  43. &_starpu_sched_ws_policy,
  44. &_starpu_sched_prio_policy,
  45. &_starpu_sched_no_prio_policy,
  46. &_starpu_sched_dm_policy,
  47. &_starpu_sched_dmda_policy,
  48. &_starpu_sched_dmda_ready_policy,
  49. &_starpu_sched_dmda_sorted_policy,
  50. &_starpu_sched_random_policy,
  51. &_starpu_sched_eager_policy,
  52. &_starpu_sched_parallel_heft_policy
  53. };
  54. struct starpu_sched_policy_s *_starpu_get_sched_policy(void)
  55. {
  56. return &policy;
  57. }
  58. /*
  59. * Methods to initialize the scheduling policy
  60. */
  61. static void load_sched_policy(struct starpu_sched_policy_s *sched_policy)
  62. {
  63. STARPU_ASSERT(sched_policy);
  64. #ifdef STARPU_VERBOSE
  65. if (sched_policy->policy_name)
  66. {
  67. if (sched_policy->policy_description)
  68. _STARPU_DEBUG("Use %s scheduler (%s)\n", sched_policy->policy_name, sched_policy->policy_description);
  69. else
  70. _STARPU_DEBUG("Use %s scheduler \n", sched_policy->policy_name);
  71. }
  72. #endif
  73. policy.init_sched = sched_policy->init_sched;
  74. policy.deinit_sched = sched_policy->deinit_sched;
  75. policy.push_task = sched_policy->push_task;
  76. policy.push_prio_task = sched_policy->push_prio_task;
  77. policy.pop_task = sched_policy->pop_task;
  78. policy.post_exec_hook = sched_policy->post_exec_hook;
  79. policy.pop_every_task = sched_policy->pop_every_task;
  80. }
  81. static struct starpu_sched_policy_s *find_sched_policy_from_name(const char *policy_name)
  82. {
  83. if (!policy_name)
  84. return NULL;
  85. unsigned i;
  86. for (i = 0; i < NPREDEFINED_POLICIES; i++)
  87. {
  88. struct starpu_sched_policy_s *p;
  89. p = predefined_policies[i];
  90. if (p->policy_name)
  91. {
  92. if (strcmp(policy_name, p->policy_name) == 0) {
  93. /* we found a policy with the requested name */
  94. return p;
  95. }
  96. }
  97. }
  98. /* nothing was found */
  99. return NULL;
  100. }
  101. static void display_sched_help_message(void)
  102. {
  103. const char *sched_env = getenv("STARPU_SCHED");
  104. if (sched_env && (strcmp(sched_env, "help") == 0)) {
  105. fprintf(stderr, "STARPU_SCHED can be either of\n");
  106. /* display the description of all predefined policies */
  107. unsigned i;
  108. for (i = 0; i < NPREDEFINED_POLICIES; i++)
  109. {
  110. struct starpu_sched_policy_s *p;
  111. p = predefined_policies[i];
  112. fprintf(stderr, "%s\t-> %s\n", p->policy_name, p->policy_description);
  113. }
  114. }
  115. }
  116. static struct starpu_sched_policy_s *select_sched_policy(struct starpu_machine_config_s *config)
  117. {
  118. struct starpu_sched_policy_s *selected_policy = NULL;
  119. struct starpu_conf *user_conf = config->user_conf;
  120. /* First, we check whether the application explicitely gave a scheduling policy or not */
  121. if (user_conf && (user_conf->sched_policy))
  122. return user_conf->sched_policy;
  123. /* Otherwise, we look if the application specified the name of a policy to load */
  124. const char *sched_pol_name;
  125. if (user_conf && (user_conf->sched_policy_name))
  126. {
  127. sched_pol_name = user_conf->sched_policy_name;
  128. }
  129. else {
  130. sched_pol_name = getenv("STARPU_SCHED");
  131. }
  132. if (sched_pol_name)
  133. selected_policy = find_sched_policy_from_name(sched_pol_name);
  134. /* Perhaps there was no policy that matched the name */
  135. if (selected_policy)
  136. return selected_policy;
  137. /* If no policy was specified, we use the greedy policy as a default */
  138. return &_starpu_sched_eager_policy;
  139. }
  140. void _starpu_init_sched_policy(struct starpu_machine_config_s *config)
  141. {
  142. /* Perhaps we have to display some help */
  143. display_sched_help_message();
  144. /* Prefetch is activated by default */
  145. use_prefetch = starpu_get_env_number("STARPU_PREFETCH");
  146. if (use_prefetch == -1)
  147. use_prefetch = 1;
  148. /* By default, we don't calibrate */
  149. unsigned do_calibrate = 0;
  150. if (config->user_conf && (config->user_conf->calibrate != -1))
  151. {
  152. do_calibrate = config->user_conf->calibrate;
  153. }
  154. else {
  155. int res = starpu_get_env_number("STARPU_CALIBRATE");
  156. do_calibrate = (res < 0)?0:(unsigned)res;
  157. }
  158. _starpu_set_calibrate_flag(do_calibrate);
  159. struct starpu_sched_policy_s *selected_policy;
  160. selected_policy = select_sched_policy(config);
  161. load_sched_policy(selected_policy);
  162. policy.init_sched(&config->topology, &policy);
  163. }
  164. void _starpu_deinit_sched_policy(struct starpu_machine_config_s *config)
  165. {
  166. if (policy.deinit_sched)
  167. policy.deinit_sched(&config->topology, &policy);
  168. }
  169. /* Enqueue a task into the list of tasks explicitely attached to a worker. In
  170. * case workerid identifies a combined worker, a task will be enqueued into
  171. * each worker of the combination. */
  172. static int _starpu_push_task_on_specific_worker(struct starpu_task *task, int workerid)
  173. {
  174. int nbasic_workers = (int)starpu_worker_get_count();
  175. /* Is this a basic worker or a combined worker ? */
  176. int is_basic_worker = (workerid < nbasic_workers);
  177. unsigned memory_node;
  178. struct starpu_worker_s *worker;
  179. struct starpu_combined_worker_s *combined_worker;
  180. if (is_basic_worker)
  181. {
  182. worker = _starpu_get_worker_struct(workerid);
  183. memory_node = worker->memory_node;
  184. }
  185. else
  186. {
  187. combined_worker = _starpu_get_combined_worker_struct(workerid);
  188. memory_node = combined_worker->memory_node;
  189. }
  190. if (use_prefetch)
  191. starpu_prefetch_task_input_on_node(task, memory_node);
  192. if (is_basic_worker)
  193. {
  194. return _starpu_push_local_task(worker, task);
  195. }
  196. else {
  197. /* This is a combined worker so we create task aliases */
  198. int worker_size = combined_worker->worker_size;
  199. int *combined_workerid = combined_worker->combined_workerid;
  200. int ret = 0;
  201. int i;
  202. starpu_job_t j = _starpu_get_job_associated_to_task(task);
  203. j->task_size = worker_size;
  204. j->combined_workerid = workerid;
  205. j->active_task_alias_count = 0;
  206. PTHREAD_BARRIER_INIT(&j->before_work_barrier, NULL, worker_size);
  207. PTHREAD_BARRIER_INIT(&j->after_work_barrier, NULL, worker_size);
  208. for (i = 0; i < worker_size; i++)
  209. {
  210. struct starpu_task *alias = _starpu_create_task_alias(task);
  211. worker = _starpu_get_worker_struct(combined_workerid[i]);
  212. ret |= _starpu_push_local_task(worker, alias);
  213. }
  214. return ret;
  215. }
  216. }
  217. /* the generic interface that call the proper underlying implementation */
  218. int _starpu_push_task(starpu_job_t j, unsigned job_is_already_locked)
  219. {
  220. struct starpu_task *task = j->task;
  221. _STARPU_LOG_IN();
  222. task->status = STARPU_TASK_READY;
  223. _starpu_profiling_set_task_push_start_time(task);
  224. /* in case there is no codelet associated to the task (that's a control
  225. * task), we directly execute its callback and enforce the
  226. * corresponding dependencies */
  227. if (task->cl == NULL)
  228. {
  229. _starpu_handle_job_termination(j, job_is_already_locked);
  230. _STARPU_LOG_OUT_TAG("handle_job_termination");
  231. return 0;
  232. }
  233. int ret;
  234. if (STARPU_UNLIKELY(task->execute_on_a_specific_worker))
  235. {
  236. ret = _starpu_push_task_on_specific_worker(task, task->workerid);
  237. }
  238. else {
  239. STARPU_ASSERT(policy.push_task);
  240. ret = policy.push_task(task);
  241. }
  242. _starpu_profiling_set_task_push_end_time(task);
  243. _STARPU_LOG_OUT();
  244. return ret;
  245. }
  246. struct starpu_task *_starpu_pop_task(void)
  247. {
  248. struct starpu_task *task;
  249. /* We can't tell in advance which task will be picked up, so we measure
  250. * a timestamp, and will attribute it afterwards to the task. */
  251. int profiling = starpu_profiling_status_get();
  252. struct timespec pop_start_time;
  253. if (profiling)
  254. starpu_clock_gettime(&pop_start_time);
  255. task = policy.pop_task();
  256. /* Note that we may get a NULL task in case the scheduler was unlocked
  257. * for some reason. */
  258. if (profiling && task)
  259. {
  260. struct starpu_task_profiling_info *profiling_info;
  261. profiling_info = task->profiling_info;
  262. /* The task may have been created before profiling was enabled,
  263. * so we check if the profiling_info structure is available
  264. * even though we already tested if profiling is enabled. */
  265. if (profiling_info)
  266. {
  267. memcpy(&profiling_info->pop_start_time,
  268. &pop_start_time, sizeof(struct timespec));
  269. starpu_clock_gettime(&profiling_info->pop_end_time);
  270. }
  271. }
  272. return task;
  273. }
  274. struct starpu_task *_starpu_pop_every_task(void)
  275. {
  276. STARPU_ASSERT(policy.pop_every_task);
  277. /* TODO set profiling info */
  278. return policy.pop_every_task();
  279. }
  280. void _starpu_sched_post_exec_hook(struct starpu_task *task)
  281. {
  282. if (policy.post_exec_hook)
  283. policy.post_exec_hook(task);
  284. }
  285. void _starpu_wait_on_sched_event(void)
  286. {
  287. struct starpu_worker_s *worker = _starpu_get_local_worker_key();
  288. PTHREAD_MUTEX_LOCK(worker->sched_mutex);
  289. _starpu_handle_all_pending_node_data_requests(worker->memory_node);
  290. if (_starpu_machine_is_running())
  291. {
  292. #ifndef STARPU_NON_BLOCKING_DRIVERS
  293. pthread_cond_wait(worker->sched_cond, worker->sched_mutex);
  294. #endif
  295. }
  296. PTHREAD_MUTEX_UNLOCK(worker->sched_mutex);
  297. }