starpu_scheduler.h 10 KB

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