starpu_task.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  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_TASK_H__
  19. #define __STARPU_TASK_H__
  20. #include <starpu.h>
  21. #include <starpu_data.h>
  22. #include <starpu_task_bundle.h>
  23. #include <errno.h>
  24. #if defined STARPU_USE_CUDA && !defined STARPU_DONT_INCLUDE_CUDA_HEADERS
  25. # include <cuda.h>
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. #define STARPU_CPU ((1ULL)<<1)
  32. #define STARPU_CUDA ((1ULL)<<3)
  33. #define STARPU_SPU ((1ULL)<<4),
  34. #define STARPU_GORDON ((1ULL)<<5)
  35. #define STARPU_OPENCL ((1ULL)<<6)
  36. /* Codelet types */
  37. enum starpu_codelet_type
  38. {
  39. STARPU_SEQ,
  40. STARPU_SPMD,
  41. STARPU_FORKJOIN
  42. };
  43. /* task status */
  44. enum starpu_task_status
  45. {
  46. STARPU_TASK_INVALID,
  47. #define STARPU_TASK_INVALID 0
  48. STARPU_TASK_BLOCKED,
  49. STARPU_TASK_READY,
  50. STARPU_TASK_RUNNING,
  51. STARPU_TASK_FINISHED,
  52. STARPU_TASK_BLOCKED_ON_TAG,
  53. STARPU_TASK_BLOCKED_ON_TASK,
  54. STARPU_TASK_BLOCKED_ON_DATA
  55. };
  56. typedef uint64_t starpu_tag_t;
  57. typedef void (*starpu_cpu_func_t)(void **, void*); /* CPU core */
  58. typedef void (*starpu_cuda_func_t)(void **, void*); /* NVIDIA CUDA device */
  59. typedef void (*starpu_opencl_func_t)(void **, void*); /* OpenCL CUDA device */
  60. typedef uint8_t starpu_gordon_func_t; /* Cell SPU */
  61. #define STARPU_MULTIPLE_CPU_IMPLEMENTATIONS ((starpu_cpu_func_t) -1)
  62. #define STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS ((starpu_cuda_func_t) -1)
  63. #define STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS ((starpu_opencl_func_t) -1)
  64. #define STARPU_MULTIPLE_GORDON_IMPLEMENTATIONS 255
  65. /*
  66. * A codelet describes the various function
  67. * that may be called from a worker
  68. */
  69. struct starpu_task;
  70. struct starpu_codelet
  71. {
  72. /* where can it be performed ? */
  73. uint32_t where;
  74. int (*can_execute)(unsigned workerid, struct starpu_task *task, unsigned nimpl);
  75. enum starpu_codelet_type type;
  76. int max_parallelism;
  77. /* the different implementations of the codelet */
  78. starpu_cuda_func_t cuda_func STARPU_DEPRECATED;
  79. starpu_cpu_func_t cpu_func STARPU_DEPRECATED;
  80. starpu_opencl_func_t opencl_func STARPU_DEPRECATED;
  81. uint8_t gordon_func STARPU_DEPRECATED;
  82. starpu_cpu_func_t cpu_funcs[STARPU_MAXIMPLEMENTATIONS];
  83. starpu_cuda_func_t cuda_funcs[STARPU_MAXIMPLEMENTATIONS];
  84. starpu_opencl_func_t opencl_funcs[STARPU_MAXIMPLEMENTATIONS];
  85. starpu_gordon_func_t gordon_funcs[STARPU_MAXIMPLEMENTATIONS];
  86. /* how many buffers do the codelet takes as argument ? */
  87. unsigned nbuffers;
  88. /* which are the access modes for these buffers */
  89. enum starpu_access_mode modes[STARPU_NMAXBUFS];
  90. /* performance model of the codelet */
  91. struct starpu_perfmodel *model;
  92. /* consumption model of the codelet.
  93. * In the case of parallel codelets, accounts for all units. */
  94. struct starpu_perfmodel *power_model;
  95. /* statistics collected at runtime: this is filled by StarPU and should
  96. * not be accessed directly (use the starpu_display_codelet_stats
  97. * function instead for instance). */
  98. unsigned long per_worker_stats[STARPU_NMAXWORKERS];
  99. const char *name;
  100. };
  101. struct starpu_task
  102. {
  103. struct starpu_codelet *cl;
  104. /* arguments managed by the DSM */
  105. struct starpu_buffer_descr buffers[STARPU_NMAXBUFS] STARPU_DEPRECATED;
  106. starpu_data_handle_t handles[STARPU_NMAXBUFS];
  107. void *interfaces[STARPU_NMAXBUFS];
  108. /* arguments not managed by the DSM are given as a buffer */
  109. void *cl_arg;
  110. /* when the task is done, callback_func(callback_arg) is called */
  111. void (*callback_func)(void *);
  112. void *callback_arg;
  113. unsigned use_tag;
  114. starpu_tag_t tag_id;
  115. /* options for the task execution */
  116. unsigned synchronous; /* if set, a call to push is blocking */
  117. int priority; /* STARPU_MAX_PRIO = most important
  118. : STARPU_MIN_PRIO = least important */
  119. /* in case the task has to be executed on a specific worker */
  120. unsigned execute_on_a_specific_worker;
  121. unsigned workerid;
  122. /* Bundle including the task */
  123. starpu_task_bundle_t bundle;
  124. /* If this flag is set, it is not possible to synchronize with the task
  125. * by the means of starpu_task_wait later on. Internal data structures
  126. * are only garanteed to be freed once starpu_task_wait is called if
  127. * that flag is not set. */
  128. int detach;
  129. /* If that flag is set, the task structure will automatically be freed,
  130. * either after the execution of the callback if the task is detached,
  131. * or during starpu_task_wait otherwise. If this flag is not set,
  132. * dynamically allocated data structures will not be freed until
  133. * starpu_task_destroy is called explicitely. Setting this flag for a
  134. * statically allocated task structure will result in undefined
  135. * behaviour. */
  136. int destroy;
  137. /* If this flag is set, the task will be re-submitted to StarPU once it
  138. * has been executed. This flag must not be set if the destroy flag is
  139. * set too. */
  140. int regenerate;
  141. enum starpu_task_status status;
  142. /* This gets filled when profiling is enabled by using
  143. * starpu_profiling_status_set */
  144. struct starpu_task_profiling_info *profiling_info;
  145. /* Predicted duration of the task in µs. This field is only valid if the
  146. * scheduling strategy uses performance models. */
  147. double predicted;
  148. /* Predicted data transfer duration for the task in µs. This field is
  149. * only valid if the scheduling strategy uses performance models. */
  150. double predicted_transfer;
  151. /* This field are provided for the convenience of the scheduler. */
  152. struct starpu_task *prev;
  153. struct starpu_task *next;
  154. unsigned int mf_skip;
  155. /* this is private to StarPU, do not modify. If the task is allocated
  156. * by hand (without starpu_task_create), this field should be set to
  157. * NULL. */
  158. void *starpu_private;
  159. /* the magic field is set when initialising the task.
  160. * starpu_task_submit will fail if the field does not have the
  161. * right value. This will hence avoid submitting tasks which
  162. * have not been properly initialised.
  163. */
  164. int magic;
  165. };
  166. /* It is possible to initialize statically allocated tasks with this value.
  167. * This is equivalent to initializing a starpu_task structure with the
  168. * starpu_task_init function. */
  169. #define STARPU_TASK_INITIALIZER \
  170. { \
  171. .cl = NULL, \
  172. .cl_arg = NULL, \
  173. .callback_func = NULL, \
  174. .callback_arg = NULL, \
  175. .priority = STARPU_DEFAULT_PRIO, \
  176. .use_tag = 0, \
  177. .synchronous = 0, \
  178. .execute_on_a_specific_worker = 0, \
  179. .bundle = NULL, \
  180. .detach = 1, \
  181. .destroy = 0, \
  182. .regenerate = 0, \
  183. .status = STARPU_TASK_INVALID, \
  184. .profiling_info = NULL, \
  185. .predicted = -1.0, \
  186. .predicted_transfer = -1.0, \
  187. .starpu_private = NULL, \
  188. .magic = 42 \
  189. }
  190. /*
  191. * handle task dependencies: it is possible to associate a task with a unique
  192. * "tag" and to express dependencies between tasks by the means of those tags
  193. *
  194. * To do so, fill the tag_id field with a tag number (can be arbitrary) and set
  195. * use_tag to 1.
  196. *
  197. * If starpu_tag_declare_deps is called with that tag number, the task will not
  198. * be started until the task which wears the declared dependency tags are
  199. * complete.
  200. */
  201. /*
  202. * WARNING ! use with caution ...
  203. * In case starpu_tag_declare_deps is passed constant arguments, the caller
  204. * must make sure that the constants are casted to starpu_tag_t. Otherwise,
  205. * due to integer sizes and argument passing on the stack, the C compiler
  206. * might consider the tag * 0x200000003 instead of 0x2 and 0x3 when calling:
  207. * "starpu_tag_declare_deps(0x1, 2, 0x2, 0x3)"
  208. * Using starpu_tag_declare_deps_array is a way to avoid this problem.
  209. */
  210. /* make id depend on the list of ids */
  211. void starpu_tag_declare_deps(starpu_tag_t id, unsigned ndeps, ...);
  212. void starpu_tag_declare_deps_array(starpu_tag_t id, unsigned ndeps, starpu_tag_t *array);
  213. /* task depends on the tasks in task array */
  214. void starpu_task_declare_deps_array(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[]);
  215. int starpu_tag_wait(starpu_tag_t id);
  216. int starpu_tag_wait_array(unsigned ntags, starpu_tag_t *id);
  217. /* The application can feed a tag explicitely */
  218. void starpu_tag_notify_from_apps(starpu_tag_t id);
  219. /* To reuse a tag not associated with a task */
  220. void starpu_tag_restart(starpu_tag_t id);
  221. /* To release resources, tags should be freed after use */
  222. void starpu_tag_remove(starpu_tag_t id);
  223. /* Initialize a task structure with default values. */
  224. void starpu_task_init(struct starpu_task *task);
  225. /* Release all the structures automatically allocated to execute the task. This
  226. * is called implicitely by starpu_task_destroy, but the task structure itself
  227. * is not freed. Values previously set by the user remain unchanged.
  228. * This should be used for statically allocated tasks for instance.
  229. * It should also be used for submitting the same task several times.
  230. */
  231. void starpu_task_clean(struct starpu_task *task);
  232. /* Allocate a task structure and initialize it with default values. Tasks
  233. * allocated dynamically with starpu_task_create are automatically freed when
  234. * the task is terminated. If the destroy flag is explicitely unset, the
  235. * ressources used by the task are freed by calling starpu_task_destroy.
  236. * */
  237. struct starpu_task *starpu_task_create(void);
  238. /* Free the ressource allocated during the execution of the task and deallocate
  239. * the task structure itself. This function can be called automatically after
  240. * the execution of a task by setting the "destroy" flag of the starpu_task
  241. * structure (default behaviour). Calling this function on a statically
  242. * allocated task results in an undefined behaviour. */
  243. void starpu_task_destroy(struct starpu_task *task);
  244. int starpu_task_submit(struct starpu_task *task) STARPU_WARN_UNUSED_RESULT;
  245. /* This function blocks until the task was executed. It is not possible to
  246. * synchronize with a task more than once. It is not possible to wait
  247. * synchronous or detached tasks.
  248. * Upon successful completion, this function returns 0. Otherwise, -EINVAL
  249. * indicates that the waited task was either synchronous or detached. */
  250. int starpu_task_wait(struct starpu_task *task) STARPU_WARN_UNUSED_RESULT;
  251. /* This function waits until all the tasks that were already submitted have
  252. * been executed. */
  253. int starpu_task_wait_for_all(void);
  254. /* This function waits until there is no more ready task. */
  255. int starpu_task_wait_for_no_ready(void);
  256. void starpu_codelet_init(struct starpu_codelet *cl);
  257. void starpu_display_codelet_stats(struct starpu_codelet *cl);
  258. /* Return the task currently executed by the worker, or NULL if this is called
  259. * either from a thread that is not a task or simply because there is no task
  260. * being executed at the moment. */
  261. struct starpu_task *starpu_task_get_current(void);
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265. #endif /* __STARPU_TASK_H__ */