starpu_task.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2011 INRIA
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #ifndef __STARPU_TASK_H__
  20. #define __STARPU_TASK_H__
  21. #include <starpu.h>
  22. #include <starpu_data.h>
  23. #include <starpu_task_bundle.h>
  24. #include <errno.h>
  25. #if defined STARPU_USE_CUDA && !defined STARPU_DONT_INCLUDE_CUDA_HEADERS
  26. # include <cuda.h>
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #endif
  32. #define STARPU_CPU ((1ULL)<<1)
  33. #define STARPU_CUDA ((1ULL)<<3)
  34. #define STARPU_OPENCL ((1ULL)<<6)
  35. #define STARPU_MIC ((1ULL)<<7)
  36. #define STARPU_SCC ((1ULL)<<8)
  37. /* Codelet types */
  38. enum starpu_codelet_type
  39. {
  40. STARPU_SEQ,
  41. STARPU_SPMD,
  42. STARPU_FORKJOIN
  43. };
  44. /* task status */
  45. enum starpu_task_status
  46. {
  47. STARPU_TASK_INVALID,
  48. #define STARPU_TASK_INVALID 0
  49. STARPU_TASK_BLOCKED,
  50. STARPU_TASK_READY,
  51. STARPU_TASK_RUNNING,
  52. STARPU_TASK_FINISHED,
  53. STARPU_TASK_BLOCKED_ON_TAG,
  54. STARPU_TASK_BLOCKED_ON_TASK,
  55. STARPU_TASK_BLOCKED_ON_DATA
  56. };
  57. typedef uint64_t starpu_tag_t;
  58. typedef void (*starpu_cpu_func_t)(void **, void*); /* CPU core */
  59. typedef void (*starpu_cuda_func_t)(void **, void*); /* NVIDIA CUDA device */
  60. typedef void (*starpu_opencl_func_t)(void **, void*); /* OpenCL CUDA device */
  61. typedef void (*starpu_mic_kernel_t)(void **, void*); /* MIC device */
  62. typedef void (*starpu_scc_kernel_t)(void **, void*); /* SCC device */
  63. typedef starpu_mic_kernel_t (*starpu_mic_func_t)(void);
  64. typedef starpu_scc_kernel_t (*starpu_scc_func_t)(void);
  65. #define STARPU_MULTIPLE_CPU_IMPLEMENTATIONS ((starpu_cpu_func_t) -1)
  66. #define STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS ((starpu_cuda_func_t) -1)
  67. #define STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS ((starpu_opencl_func_t) -1)
  68. /*
  69. * A codelet describes the various function
  70. * that may be called from a worker
  71. */
  72. struct starpu_task;
  73. struct starpu_codelet
  74. {
  75. /* where can it be performed ? */
  76. uint32_t where;
  77. int (*can_execute)(unsigned workerid, struct starpu_task *task, unsigned nimpl);
  78. enum starpu_codelet_type type;
  79. int max_parallelism;
  80. /* the different implementations of the codelet */
  81. starpu_cpu_func_t cpu_func STARPU_DEPRECATED;
  82. starpu_cuda_func_t cuda_func STARPU_DEPRECATED;
  83. starpu_opencl_func_t opencl_func STARPU_DEPRECATED;
  84. starpu_cpu_func_t cpu_funcs[STARPU_MAXIMPLEMENTATIONS];
  85. starpu_cuda_func_t cuda_funcs[STARPU_MAXIMPLEMENTATIONS];
  86. starpu_opencl_func_t opencl_funcs[STARPU_MAXIMPLEMENTATIONS];
  87. starpu_mic_func_t mic_funcs[STARPU_MAXIMPLEMENTATIONS];
  88. starpu_scc_func_t scc_funcs[STARPU_MAXIMPLEMENTATIONS];
  89. char *cpu_funcs_name[STARPU_MAXIMPLEMENTATIONS];
  90. /* how many buffers do the codelet takes as argument ? */
  91. unsigned nbuffers;
  92. /* which are the access modes for these buffers */
  93. enum starpu_data_access_mode modes[STARPU_NMAXBUFS];
  94. enum starpu_data_access_mode *dyn_modes;
  95. /* performance model of the codelet */
  96. struct starpu_perfmodel *model;
  97. /* consumption model of the codelet.
  98. * In the case of parallel codelets, accounts for all units. */
  99. struct starpu_perfmodel *power_model;
  100. /* statistics collected at runtime: this is filled by StarPU and should
  101. * not be accessed directly (use the starpu_codelet_display_stats
  102. * function instead for instance). */
  103. unsigned long per_worker_stats[STARPU_NMAXWORKERS];
  104. const char *name;
  105. };
  106. struct starpu_task
  107. {
  108. struct starpu_codelet *cl;
  109. /* arguments managed by the DSM */
  110. struct starpu_data_descr buffers[STARPU_NMAXBUFS] STARPU_DEPRECATED;
  111. starpu_data_handle_t handles[STARPU_NMAXBUFS];
  112. void *interfaces[STARPU_NMAXBUFS];
  113. starpu_data_handle_t *dyn_handles;
  114. void **dyn_interfaces;
  115. /* arguments not managed by the DSM are given as a buffer */
  116. void *cl_arg;
  117. /* in case the argument buffer has to be uploaded explicitely */
  118. size_t cl_arg_size;
  119. /* must StarPU release cl_arg ? - 0 by default */
  120. unsigned cl_arg_free;
  121. /* when the task is done, callback_func(callback_arg) is called */
  122. void (*callback_func)(void *);
  123. void *callback_arg;
  124. /* Whether tag_id should be considered */
  125. unsigned use_tag;
  126. /* Tag associated with this task */
  127. starpu_tag_t tag_id;
  128. /* Whether we should enforce sequential consistency for this task */
  129. unsigned sequential_consistency;
  130. /* options for the task execution */
  131. unsigned synchronous; /* if set, a call to push is blocking */
  132. int priority; /* STARPU_MAX_PRIO = most important; STARPU_MIN_PRIO = least important */
  133. /* in case the task has to be executed on a specific worker */
  134. unsigned execute_on_a_specific_worker;
  135. unsigned workerid;
  136. /* Bundle including the task */
  137. starpu_task_bundle_t bundle;
  138. /* If this flag is set, it is not possible to synchronize with the task
  139. * by the means of starpu_task_wait later on. Internal data structures
  140. * are only garanteed to be freed once starpu_task_wait is called if
  141. * that flag is not set. */
  142. int detach;
  143. /* If that flag is set, the task structure will automatically be freed,
  144. * either after the execution of the callback if the task is detached,
  145. * or during starpu_task_wait otherwise. If this flag is not set,
  146. * dynamically allocated data structures will not be freed until
  147. * starpu_task_destroy is called explicitely. Setting this flag for a
  148. * statically allocated task structure will result in undefined
  149. * behaviour. */
  150. int destroy;
  151. /* If this flag is set, the task will be re-submitted to StarPU once it
  152. * has been executed. This flag must not be set if the destroy flag is
  153. * set too. */
  154. int regenerate;
  155. enum starpu_task_status status;
  156. /* This gets filled when profiling is enabled by using
  157. * starpu_profiling_status_set */
  158. struct starpu_profiling_task_info *profiling_info;
  159. /* Predicted duration of the task in µs. This field is only valid if the
  160. * scheduling strategy uses performance models. */
  161. double predicted;
  162. /* Predicted data transfer duration for the task in µs. This field is
  163. * only valid if the scheduling strategy uses performance models. */
  164. double predicted_transfer;
  165. /* This field are provided for the convenience of the scheduler. */
  166. struct starpu_task *prev;
  167. struct starpu_task *next;
  168. unsigned int mf_skip;
  169. /* this is private to StarPU, do not modify. If the task is allocated
  170. * by hand (without starpu_task_create), this field should be set to
  171. * NULL. */
  172. void *starpu_private;
  173. /* the magic field is set when initialising the task.
  174. * starpu_task_submit will fail if the field does not have the
  175. * right value. This will hence avoid submitting tasks which
  176. * have not been properly initialised.
  177. */
  178. int magic;
  179. /* Scheduling context */
  180. unsigned sched_ctx;
  181. /* Helps the hypervisor monitor the execution of this task */
  182. int hypervisor_tag;
  183. /* Number of flops computed by this tag, used by resource reallocation for contexts */
  184. double flops;
  185. /* Whether the scheduler has pushed the task on some queue */
  186. unsigned scheduled;
  187. };
  188. /* It is possible to initialize statically allocated tasks with this value.
  189. * This is equivalent to initializing a starpu_task structure with the
  190. * starpu_task_init function. */
  191. #define STARPU_TASK_INITIALIZER \
  192. { \
  193. .cl = NULL, \
  194. .cl_arg = NULL, \
  195. .cl_arg_size = 0, \
  196. .callback_func = NULL, \
  197. .callback_arg = NULL, \
  198. .priority = STARPU_DEFAULT_PRIO, \
  199. .use_tag = 0, \
  200. .synchronous = 0, \
  201. .execute_on_a_specific_worker = 0, \
  202. .bundle = NULL, \
  203. .detach = 1, \
  204. .destroy = 0, \
  205. .regenerate = 0, \
  206. .status = STARPU_TASK_INVALID, \
  207. .profiling_info = NULL, \
  208. .predicted = -1.0, \
  209. .predicted_transfer = -1.0, \
  210. .starpu_private = NULL, \
  211. .magic = 42, \
  212. .sched_ctx = 0, \
  213. .hypervisor_tag = 0, \
  214. .flops = 0.0, \
  215. .scheduled = 0, \
  216. .dyn_handles = NULL, \
  217. .dyn_interfaces = NULL \
  218. }
  219. #define STARPU_TASK_GET_HANDLE(task, i) ((task->dyn_handles) ? task->dyn_handles[i] : task->handles[i])
  220. #define STARPU_TASK_SET_HANDLE(task, handle, i) do { if (task->dyn_handles) task->dyn_handles[i] = handle; else task->handles[i] = handle; } while(0)
  221. #define STARPU_CODELET_GET_MODE(codelet, i) ((codelet->dyn_modes) ? codelet->dyn_modes[i] : codelet->modes[i])
  222. #define STARPU_CODELET_SET_MODE(codelet, mode, i) do { if (codelet->dyn_modes) codelet->dyn_modes[i] = mode; else codelet->modes[i] = mode; } while(0)
  223. /*
  224. * handle task dependencies: it is possible to associate a task with a unique
  225. * "tag" and to express dependencies between tasks by the means of those tags
  226. *
  227. * To do so, fill the tag_id field with a tag number (can be arbitrary) and set
  228. * use_tag to 1.
  229. *
  230. * If starpu_tag_declare_deps is called with that tag number, the task will not
  231. * be started until the task which wears the declared dependency tags are
  232. * complete.
  233. */
  234. /*
  235. * WARNING ! use with caution ...
  236. * In case starpu_tag_declare_deps is passed constant arguments, the caller
  237. * must make sure that the constants are casted to starpu_tag_t. Otherwise,
  238. * due to integer sizes and argument passing on the stack, the C compiler
  239. * might consider the tag * 0x200000003 instead of 0x2 and 0x3 when calling:
  240. * "starpu_tag_declare_deps(0x1, 2, 0x2, 0x3)"
  241. * Using starpu_tag_declare_deps_array is a way to avoid this problem.
  242. */
  243. /* make id depend on the list of ids */
  244. void starpu_tag_declare_deps(starpu_tag_t id, unsigned ndeps, ...);
  245. void starpu_tag_declare_deps_array(starpu_tag_t id, unsigned ndeps, starpu_tag_t *array);
  246. /* task depends on the tasks in task array */
  247. void starpu_task_declare_deps_array(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[]);
  248. int starpu_tag_wait(starpu_tag_t id);
  249. int starpu_tag_wait_array(unsigned ntags, starpu_tag_t *id);
  250. /* The application can feed a tag explicitely */
  251. void starpu_tag_notify_from_apps(starpu_tag_t id);
  252. /* To reuse a tag not associated with a task */
  253. void starpu_tag_restart(starpu_tag_t id);
  254. /* To release resources, tags should be freed after use */
  255. void starpu_tag_remove(starpu_tag_t id);
  256. /* Initialize a task structure with default values. */
  257. void starpu_task_init(struct starpu_task *task);
  258. /* Release all the structures automatically allocated to execute the task. This
  259. * is called implicitely by starpu_task_destroy, but the task structure itself
  260. * is not freed. Values previously set by the user remain unchanged.
  261. * This should be used for statically allocated tasks for instance.
  262. * It should also be used for submitting the same task several times.
  263. */
  264. void starpu_task_clean(struct starpu_task *task);
  265. /* Allocate a task structure and initialize it with default values. Tasks
  266. * allocated dynamically with starpu_task_create are automatically freed when
  267. * the task is terminated. If the destroy flag is explicitely unset, the
  268. * ressources used by the task are freed by calling starpu_task_destroy.
  269. * */
  270. struct starpu_task *starpu_task_create(void);
  271. /* Free the ressource allocated during the execution of the task and deallocate
  272. * the task structure itself. This function can be called automatically after
  273. * the execution of a task by setting the "destroy" flag of the starpu_task
  274. * structure (default behaviour). Calling this function on a statically
  275. * allocated task results in an undefined behaviour. */
  276. void starpu_task_destroy(struct starpu_task *task);
  277. int starpu_task_submit(struct starpu_task *task) STARPU_WARN_UNUSED_RESULT;
  278. int starpu_task_submit_to_ctx(struct starpu_task *task, unsigned sched_ctx_id);
  279. /* This function blocks until the task was executed. It is not possible to
  280. * synchronize with a task more than once. It is not possible to wait
  281. * synchronous or detached tasks.
  282. * Upon successful completion, this function returns 0. Otherwise, -EINVAL
  283. * indicates that the waited task was either synchronous or detached. */
  284. int starpu_task_wait(struct starpu_task *task) STARPU_WARN_UNUSED_RESULT;
  285. /* This function waits until all the tasks that were already submitted
  286. * (to the current context or the global one if there aren't any) have
  287. * been executed. */
  288. int starpu_task_wait_for_all(void);
  289. /* This function waits until all the tasks that were already submitted to the
  290. * context have been executed */
  291. int starpu_task_wait_for_all_in_ctx(unsigned sched_ctx_id);
  292. /* This function waits until there is no more ready task. */
  293. int starpu_task_wait_for_no_ready(void);
  294. int starpu_task_nready(void);
  295. int starpu_task_nsubmitted(void);
  296. void starpu_codelet_init(struct starpu_codelet *cl);
  297. void starpu_codelet_display_stats(struct starpu_codelet *cl);
  298. /* Return the task currently executed by the worker, or NULL if this is called
  299. * either from a thread that is not a task or simply because there is no task
  300. * being executed at the moment. */
  301. struct starpu_task *starpu_task_get_current(void);
  302. /* initialise the barrier for the parallel task, st all workers start it
  303. * at the same time */
  304. void starpu_parallel_task_barrier_init(struct starpu_task* task, int workerid);
  305. /* duplicate the given task */
  306. struct starpu_task *starpu_task_dup(struct starpu_task *task);
  307. void starpu_task_set_implementation(struct starpu_task *task, unsigned impl);
  308. unsigned starpu_task_get_implementation(struct starpu_task *task);
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. #endif /* __STARPU_TASK_H__ */