starpu_scheduler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2011 Télécom-SudParis
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #ifndef __STARPU_SCHEDULER_H__
  18. #define __STARPU_SCHEDULER_H__
  19. #include <starpu.h>
  20. #if ! defined(_MSC_VER)
  21. # include <pthread.h>
  22. #endif
  23. #ifdef STARPU_HAVE_HWLOC
  24. #include <hwloc.h>
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. struct starpu_task;
  31. struct starpu_machine_topology
  32. {
  33. unsigned nworkers;
  34. unsigned ncombinedworkers;
  35. unsigned nsched_ctxs;
  36. #ifdef STARPU_HAVE_HWLOC
  37. hwloc_topology_t hwtopology;
  38. #else
  39. /* We maintain ABI compatibility with and without hwloc */
  40. void *dummy;
  41. #endif
  42. unsigned nhwcpus;
  43. unsigned nhwcudagpus;
  44. unsigned nhwopenclgpus;
  45. unsigned ncpus;
  46. unsigned ncudagpus;
  47. unsigned nopenclgpus;
  48. unsigned ngordon_spus;
  49. /* Where to bind workers ? */
  50. unsigned workers_bindid[STARPU_NMAXWORKERS];
  51. /* Which GPU(s) do we use for CUDA ? */
  52. unsigned workers_cuda_gpuid[STARPU_NMAXWORKERS];
  53. /* Which GPU(s) do we use for OpenCL ? */
  54. unsigned workers_opencl_gpuid[STARPU_NMAXWORKERS];
  55. };
  56. /* This structure contains all the methods that implement a scheduling policy.
  57. * An application may specify which scheduling strategy in the "sched_policy"
  58. * field of the starpu_conf structure passed to the starpu_init function. */
  59. struct starpu_sched_policy
  60. {
  61. /* Initialize the scheduling policy. */
  62. void (*init_sched)(unsigned sched_ctx_id);
  63. /* Cleanup the scheduling policy. */
  64. void (*deinit_sched)(unsigned sched_ctx_id);
  65. /* Insert a task into the scheduler. */
  66. int (*push_task)(struct starpu_task *);
  67. /* Notify the scheduler that a task was directly pushed to the worker
  68. * without going through the scheduler. This method is called when a
  69. * task is explicitely assigned to a worker. This method therefore
  70. * permits to keep the timing state of the scheduler coherent even
  71. * when StarPU bypasses the scheduling strategy. */
  72. void (*push_task_notify)(struct starpu_task *, int workerid);
  73. /* Get a task from the scheduler. The mutex associated to the worker is
  74. * already taken when this method is called. */
  75. struct starpu_task *(*pop_task)(unsigned sched_ctx);
  76. /* Remove all available tasks from the scheduler (tasks are chained by
  77. * the means of the prev and next fields of the starpu_task
  78. * structure). The mutex associated to the worker is already taken
  79. * when this method is called. */
  80. struct starpu_task *(*pop_every_task)(unsigned sched_ctx);
  81. /* This method is called every time a task is starting. (optional) */
  82. void (*pre_exec_hook)(struct starpu_task *);
  83. /* This method is called every time a task has been executed. (optional) */
  84. void (*post_exec_hook)(struct starpu_task *);
  85. /* Initialize scheduling structures corresponding to each worker. */
  86. void (*add_workers)(unsigned sched_ctx_id, int *workerids, unsigned nworkers);
  87. /* Deinitialize scheduling structures corresponding to each worker. */
  88. void (*remove_workers)(unsigned sched_ctx_id, int *workerids, unsigned nworkers);
  89. /* Name of the policy (optionnal) */
  90. const char *policy_name;
  91. /* Description of the policy (optionnal) */
  92. const char *policy_description;
  93. };
  94. /* generic structure used by the scheduling contexts to iterated the workers */
  95. struct worker_collection {
  96. /* hidden data structure used to memorize the workers */
  97. void *workerids;
  98. /* the number of workers in the collection */
  99. unsigned nworkers;
  100. /* the current cursor of the collection*/
  101. pthread_key_t cursor_key;
  102. /* the type of structure (WORKER_LIST,...) */
  103. int type;
  104. /* checks if there is another element in collection */
  105. unsigned (*has_next)(struct worker_collection *workers);
  106. /* return the next element in the collection */
  107. int (*get_next)(struct worker_collection *workers);
  108. /* add a new element in the collection */
  109. int (*add)(struct worker_collection *workers, int worker);
  110. /* remove an element from the collection */
  111. int (*remove)(struct worker_collection *workers, int worker);
  112. /* initialize the structure */
  113. void* (*init)(struct worker_collection *workers);
  114. /* free the structure */
  115. void (*deinit)(struct worker_collection *workers);
  116. /* initialize the cursor if there is one */
  117. void (*init_cursor)(struct worker_collection *workers);
  118. /* free the cursor if there is one */
  119. void (*deinit_cursor)(struct worker_collection *workers);
  120. };
  121. /* types of structures the worker collection can implement */
  122. #define WORKER_LIST 0
  123. struct starpu_performance_counters {
  124. void (*notify_idle_cycle)(unsigned sched_ctx, int worker, double idle_time);
  125. void (*notify_idle_end)(unsigned sched_ctx, int worker);
  126. void (*notify_pushed_task)(unsigned sched_ctx, int worker);
  127. void (*notify_poped_task)(unsigned sched_ctx, int worker, double flops);
  128. void (*notify_post_exec_hook)(unsigned sched_ctx, int taskid);
  129. void (*notify_submitted_job)(struct starpu_task *task, uint32_t footprint);
  130. };
  131. #ifdef STARPU_BUILD_SCHED_CTX_HYPERVISOR
  132. void starpu_set_perf_counters(unsigned sched_ctx_id, struct starpu_performance_counters *perf_counters);
  133. void starpu_call_poped_task_cb(int workerid, unsigned sched_ctx_id, double flops);
  134. void starpu_call_pushed_task_cb(int workerid, unsigned sched_ctx_id);
  135. #endif //STARPU_BUILD_SCHED_CTX_HYPERVISOR
  136. unsigned starpu_create_sched_ctx(const char *policy_name, int *workerids_ctx, int nworkers_ctx, const char *sched_name);
  137. void starpu_delete_sched_ctx(unsigned sched_ctx_id, unsigned inheritor_sched_ctx_id);
  138. void starpu_add_workers_to_sched_ctx(int *workerids_ctx, int nworkers_ctx, unsigned sched_ctx);
  139. void starpu_remove_workers_from_sched_ctx(int *workerids_ctx, int nworkers_ctx, unsigned sched_ctx);
  140. void starpu_set_sched_ctx_policy_data(unsigned sched_ctx, void* policy_data);
  141. void* starpu_get_sched_ctx_policy_data(unsigned sched_ctx);
  142. void starpu_worker_set_sched_condition(unsigned sched_ctx, int workerid, pthread_mutex_t *sched_mutex, pthread_cond_t *sched_cond);
  143. void starpu_worker_get_sched_condition(unsigned sched_ctx, int workerid, pthread_mutex_t **sched_mutex, pthread_cond_t **sched_cond);
  144. void starpu_worker_init_sched_condition(unsigned sched_ctx, int workerid);
  145. void starpu_worker_deinit_sched_condition(unsigned sched_ctx, int workerid);
  146. struct worker_collection* starpu_create_worker_collection_for_sched_ctx(unsigned sched_ctx_id, int type);
  147. void starpu_delete_worker_collection_for_sched_ctx(unsigned sched_ctx_id);
  148. struct worker_collection* starpu_get_worker_collection_of_sched_ctx(unsigned sched_ctx_id);
  149. pthread_mutex_t* starpu_get_changing_ctx_mutex(unsigned sched_ctx_id);
  150. void starpu_set_sched_ctx(unsigned *sched_ctx);
  151. unsigned starpu_get_sched_ctx(void);
  152. void starpu_notify_hypervisor_exists(void);
  153. unsigned starpu_check_if_hypervisor_exists(void);
  154. unsigned starpu_get_nworkers_of_sched_ctx(unsigned sched_ctx);
  155. unsigned starpu_get_nshared_workers(unsigned sched_ctx_id, unsigned sched_ctx_id2);
  156. unsigned starpu_worker_belongs_to_sched_ctx(int workerid, unsigned sched_ctx_id);
  157. void starpu_stop_task_submission(unsigned sched_ctx);
  158. /* Check if the worker specified by workerid can execute the codelet. */
  159. int starpu_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl);
  160. /* The scheduling policy may put tasks directly into a worker's local queue so
  161. * that it is not always necessary to create its own queue when the local queue
  162. * is sufficient. If "back" not null, the task is put at the back of the queue
  163. * where the worker will pop tasks first. Setting "back" to 0 therefore ensures
  164. * a FIFO ordering. */
  165. int starpu_push_local_task(int workerid, struct starpu_task *task, int back);
  166. /*
  167. * Priorities
  168. */
  169. /* Provided for legacy reasons */
  170. #define STARPU_MIN_PRIO (starpu_sched_get_min_priority())
  171. #define STARPU_MAX_PRIO (starpu_sched_get_max_priority())
  172. /* By convention, the default priority level should be 0 so that we can
  173. * statically allocate tasks with a default priority. */
  174. #define STARPU_DEFAULT_PRIO 0
  175. int starpu_sched_get_min_priority(void);
  176. int starpu_sched_get_max_priority(void);
  177. void starpu_sched_set_min_priority(int min_prio);
  178. void starpu_sched_set_max_priority(int max_prio);
  179. /*
  180. * Parallel tasks
  181. */
  182. /* Register a new combined worker and get its identifier */
  183. int starpu_combined_worker_assign_workerid(int nworkers, int workerid_array[]);
  184. /* Get the description of a combined worker */
  185. int starpu_combined_worker_get_description(int workerid, int *worker_size, int **combined_workerid);
  186. /* Variant of starpu_worker_can_execute_task compatible with combined workers */
  187. int starpu_combined_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl);
  188. /*
  189. * Data prefetching
  190. */
  191. /* Whether STARPU_PREFETCH was set */
  192. int starpu_get_prefetch_flag(void);
  193. /* Prefetch data for a given task on a given node */
  194. int starpu_prefetch_task_input_on_node(struct starpu_task *task, uint32_t node);
  195. /*
  196. * Performance predictions
  197. */
  198. /* Return the current date */
  199. double starpu_timing_now(void);
  200. /* Returns expected task duration in µs */
  201. double starpu_task_expected_length(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl);
  202. /* Returns an estimated speedup factor relative to CPU speed */
  203. double starpu_worker_get_relative_speedup(enum starpu_perf_archtype perf_archtype);
  204. /* Returns expected data transfer time in µs */
  205. double starpu_task_expected_data_transfer_time(uint32_t memory_node, struct starpu_task *task);
  206. /* Predict the transfer time (in µs) to move a handle to a memory node */
  207. double starpu_data_expected_transfer_time(starpu_data_handle_t handle, unsigned memory_node, enum starpu_access_mode mode);
  208. /* Returns expected power consumption in J */
  209. double starpu_task_expected_power(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl);
  210. /* Returns expected conversion time in ms (multiformat interface only) */
  211. double starpu_task_expected_conversion_time(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl);
  212. /* Return the expected duration of the entire task bundle in µs. */
  213. double starpu_task_bundle_expected_length(starpu_task_bundle_t bundle, enum starpu_perf_archtype arch, unsigned nimpl);
  214. /* Return the time (in µs) expected to transfer all data used within the bundle */
  215. double starpu_task_bundle_expected_data_transfer_time(starpu_task_bundle_t bundle, unsigned memory_node);
  216. /* Return the expected power consumption of the entire task bundle in J. */
  217. double starpu_task_bundle_expected_power(starpu_task_bundle_t bundle, enum starpu_perf_archtype arch, unsigned nimpl);
  218. #ifdef __cplusplus
  219. }
  220. #endif
  221. /* /\* Waits until all the tasks of a worker, already submitted, have been executed *\/ */
  222. /* int starpu_wait_for_all_tasks_of_worker(int workerid); */
  223. /* /\* Waits until all the tasks of a bunch of workers have been executed *\/ */
  224. /* int starpu_wait_for_all_tasks_of_workers(int *workerids_ctx, int nworkers_ctx); */
  225. #endif /* __STARPU_SCHEDULER_H__ */