starpu_task.h 10 KB

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