starpu_data_interfaces.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 Institut National de Recherche en Informatique et Automatique
  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_DATA_INTERFACES_H__
  20. #define __STARPU_DATA_INTERFACES_H__
  21. #include <starpu_data.h>
  22. #include <starpu_util.h>
  23. #ifdef STARPU_USE_GORDON
  24. /* to get the gordon_strideSize_t data structure from gordon */
  25. #include <gordon.h>
  26. #endif
  27. #ifdef STARPU_USE_CUDA
  28. /* to use CUDA streams */
  29. # ifdef STARPU_DONT_INCLUDE_CUDA_HEADERS
  30. typedef void *cudaStream_t;
  31. # else
  32. # include <cuda_runtime.h>
  33. # endif
  34. #endif
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* The following structures are used to describe data interfaces */
  39. /* This structure contains the different methods to transfer data between the
  40. * different types of memory nodes */
  41. struct starpu_data_copy_methods {
  42. /* src type is ram */
  43. int (*ram_to_ram)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  44. int (*ram_to_cuda)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  45. int (*ram_to_opencl)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  46. int (*ram_to_spu)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  47. /* src type is cuda */
  48. int (*cuda_to_ram)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  49. int (*cuda_to_cuda)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  50. int (*cuda_to_opencl)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  51. int (*cuda_to_spu)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  52. /* src type is spu */
  53. int (*spu_to_ram)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  54. int (*spu_to_cuda)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  55. int (*spu_to_opencl)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  56. int (*spu_to_spu)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  57. /* src type is opencl */
  58. int (*opencl_to_ram)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  59. int (*opencl_to_cuda)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  60. int (*opencl_to_opencl)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  61. int (*opencl_to_spu)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  62. #ifdef STARPU_USE_CUDA
  63. /* for asynchronous CUDA transfers */
  64. int (*ram_to_cuda_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  65. int (*cuda_to_ram_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  66. int (*cuda_to_cuda_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  67. #endif
  68. #ifdef STARPU_USE_OPENCL
  69. /* for asynchronous OpenCL transfers */
  70. /* XXX we do not use a cl_event *event type for the last argument
  71. * because nvcc does not like when we have to include OpenCL headers */
  72. int (*ram_to_opencl_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, /* cl_event * */ void *event);
  73. int (*opencl_to_ram_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, /* cl_event * */ void *event);
  74. int (*opencl_to_opencl_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, /* cl_event * */ void *event);
  75. #endif
  76. };
  77. enum starpu_data_interface_id {
  78. STARPU_MATRIX_INTERFACE_ID=0,
  79. STARPU_BLOCK_INTERFACE_ID=1,
  80. STARPU_VECTOR_INTERFACE_ID=2,
  81. STARPU_CSR_INTERFACE_ID=3,
  82. STARPU_BCSR_INTERFACE_ID=4,
  83. STARPU_VARIABLE_INTERFACE_ID=5,
  84. STARPU_VOID_INTERFACE_ID=6,
  85. STARPU_MULTIFORMAT_INTERFACE_ID=7,
  86. STARPU_NINTERFACES_ID=8 /* number of data interfaces */
  87. };
  88. struct starpu_data_interface_ops {
  89. /* Register an existing interface into a data handle. */
  90. void (*register_data_handle)(starpu_data_handle handle,
  91. uint32_t home_node, void *data_interface);
  92. /* Allocate data for the interface on a given node. */
  93. starpu_ssize_t (*allocate_data_on_node)(void *data_interface, uint32_t node);
  94. /* Free data of the interface on a given node. */
  95. void (*free_data_on_node)(void *data_interface, uint32_t node);
  96. /* ram/cuda/spu/opencl synchronous and asynchronous transfer methods */
  97. const struct starpu_data_copy_methods *copy_methods;
  98. /* Return the current pointer (if any) for the handle on the given node. */
  99. void * (*handle_to_pointer)(starpu_data_handle handle, uint32_t node);
  100. /* Return an estimation of the size of data, for performance models */
  101. size_t (*get_size)(starpu_data_handle handle);
  102. /* Return a 32bit footprint which characterizes the data size */
  103. uint32_t (*footprint)(starpu_data_handle handle);
  104. /* Compare the data size of two interfaces */
  105. int (*compare)(void *data_interface_a, void *data_interface_b);
  106. /* Dump the sizes of a handle to a file */
  107. void (*display)(starpu_data_handle handle, FILE *f);
  108. #ifdef STARPU_USE_GORDON
  109. /* Convert the data size to the spu size format */
  110. int (*convert_to_gordon)(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss);
  111. #endif
  112. /* an identifier that is unique to each interface */
  113. enum starpu_data_interface_id interfaceid;
  114. /* The size of the interface data descriptor */
  115. size_t interface_size;
  116. };
  117. void starpu_data_register(starpu_data_handle *handleptr, uint32_t home_node,
  118. void *data_interface,
  119. struct starpu_data_interface_ops *ops);
  120. /* Return the pointer associated with HANDLE on node NODE or NULL if HANDLE's
  121. * interface does not support this operation or data for this handle is not
  122. * allocated on that node. */
  123. void *starpu_handle_to_pointer(starpu_data_handle handle, uint32_t node);
  124. /* Return the local pointer associated with HANDLE or NULL if HANDLE's
  125. * interface does not have data allocated locally */
  126. void *starpu_handle_get_local_ptr(starpu_data_handle handle);
  127. /* "node" means memory node: 0 for main RAM, then 1, 2, etc. for various GPUs,
  128. * etc.
  129. *
  130. * On registration, the source of data is usually a pointer in RAM, in which
  131. * case 0 should be passed.
  132. */
  133. void *starpu_data_get_interface_on_node(starpu_data_handle handle, unsigned memory_node);
  134. /* Matrix interface for dense matrices */
  135. struct starpu_matrix_interface {
  136. uintptr_t ptr;
  137. uintptr_t dev_handle;
  138. size_t offset;
  139. uint32_t nx;
  140. uint32_t ny;
  141. uint32_t ld;
  142. size_t elemsize;
  143. };
  144. void starpu_matrix_data_register(starpu_data_handle *handle, uint32_t home_node,
  145. uintptr_t ptr, uint32_t ld, uint32_t nx,
  146. uint32_t ny, size_t elemsize);
  147. uint32_t starpu_matrix_get_nx(starpu_data_handle handle);
  148. uint32_t starpu_matrix_get_ny(starpu_data_handle handle);
  149. uint32_t starpu_matrix_get_local_ld(starpu_data_handle handle);
  150. uintptr_t starpu_matrix_get_local_ptr(starpu_data_handle handle);
  151. size_t starpu_matrix_get_elemsize(starpu_data_handle handle);
  152. /* helper methods */
  153. #define STARPU_MATRIX_GET_PTR(interface) (((struct starpu_matrix_interface *)(interface))->ptr)
  154. #define STARPU_MATRIX_GET_NX(interface) (((struct starpu_matrix_interface *)(interface))->nx)
  155. #define STARPU_MATRIX_GET_NY(interface) (((struct starpu_matrix_interface *)(interface))->ny)
  156. #define STARPU_MATRIX_GET_LD(interface) (((struct starpu_matrix_interface *)(interface))->ld)
  157. #define STARPU_MATRIX_GET_ELEMSIZE(interface) (((struct starpu_matrix_interface *)(interface))->elemsize)
  158. /* BLOCK interface for 3D dense blocks */
  159. /* TODO: rename to 3dmatrix? */
  160. struct starpu_block_interface {
  161. uintptr_t ptr;
  162. uintptr_t dev_handle;
  163. size_t offset;
  164. uint32_t nx;
  165. uint32_t ny;
  166. uint32_t nz;
  167. uint32_t ldy; /* number of elements between two lines */
  168. uint32_t ldz; /* number of elements between two planes */
  169. size_t elemsize;
  170. };
  171. void starpu_block_data_register(starpu_data_handle *handle, uint32_t home_node,
  172. uintptr_t ptr, uint32_t ldy, uint32_t ldz, uint32_t nx,
  173. uint32_t ny, uint32_t nz, size_t elemsize);
  174. uint32_t starpu_block_get_nx(starpu_data_handle handle);
  175. uint32_t starpu_block_get_ny(starpu_data_handle handle);
  176. uint32_t starpu_block_get_nz(starpu_data_handle handle);
  177. uint32_t starpu_block_get_local_ldy(starpu_data_handle handle);
  178. uint32_t starpu_block_get_local_ldz(starpu_data_handle handle);
  179. uintptr_t starpu_block_get_local_ptr(starpu_data_handle handle);
  180. size_t starpu_block_get_elemsize(starpu_data_handle handle);
  181. /* helper methods */
  182. #define STARPU_BLOCK_GET_PTR(interface) (((struct starpu_block_interface *)(interface))->ptr)
  183. #define STARPU_BLOCK_GET_NX(interface) (((struct starpu_block_interface *)(interface))->nx)
  184. #define STARPU_BLOCK_GET_NY(interface) (((struct starpu_block_interface *)(interface))->ny)
  185. #define STARPU_BLOCK_GET_NZ(interface) (((struct starpu_block_interface *)(interface))->nz)
  186. #define STARPU_BLOCK_GET_LDY(interface) (((struct starpu_block_interface *)(interface))->ldy)
  187. #define STARPU_BLOCK_GET_LDZ(interface) (((struct starpu_block_interface *)(interface))->ldz)
  188. #define STARPU_BLOCK_GET_ELEMSIZE(interface) (((struct starpu_block_interface *)(interface))->elemsize)
  189. /* vector interface for contiguous (non-strided) buffers */
  190. struct starpu_vector_interface {
  191. uintptr_t ptr;
  192. uintptr_t dev_handle;
  193. size_t offset;
  194. uint32_t nx;
  195. size_t elemsize;
  196. };
  197. void starpu_vector_data_register(starpu_data_handle *handle, uint32_t home_node,
  198. uintptr_t ptr, uint32_t nx, size_t elemsize);
  199. uint32_t starpu_vector_get_nx(starpu_data_handle handle);
  200. size_t starpu_vector_get_elemsize(starpu_data_handle handle);
  201. uintptr_t starpu_vector_get_local_ptr(starpu_data_handle handle);
  202. /* helper methods */
  203. #define STARPU_VECTOR_GET_PTR(interface) (((struct starpu_vector_interface *)(interface))->ptr)
  204. #define STARPU_VECTOR_GET_NX(interface) (((struct starpu_vector_interface *)(interface))->nx)
  205. #define STARPU_VECTOR_GET_ELEMSIZE(interface) (((struct starpu_vector_interface *)(interface))->elemsize)
  206. /* variable interface for a single data (not a vector, a matrix, a list, ...) */
  207. struct starpu_variable_interface {
  208. uintptr_t ptr;
  209. size_t elemsize;
  210. };
  211. void starpu_variable_data_register(starpu_data_handle *handle, uint32_t home_node,
  212. uintptr_t ptr, size_t elemsize);
  213. size_t starpu_variable_get_elemsize(starpu_data_handle handle);
  214. uintptr_t starpu_variable_get_local_ptr(starpu_data_handle handle);
  215. /* helper methods */
  216. #define STARPU_VARIABLE_GET_PTR(interface) (((struct starpu_variable_interface *)(interface))->ptr)
  217. #define STARPU_VARIABLE_GET_ELEMSIZE(interface) (((struct starpu_variable_interface *)(interface))->elemsize)
  218. /* void interface. There is no data really associated to that interface, but it
  219. * may be used as a synchronization mechanism. It also permits to express an
  220. * abstract piece of data that is managed by the application internally: this
  221. * makes it possible to forbid the concurrent execution of different tasks
  222. * accessing the same "void" data in read-write concurrently. */
  223. void starpu_void_data_register(starpu_data_handle *handleptr);
  224. /* CSR interface for sparse matrices (compressed sparse row representation) */
  225. struct starpu_csr_interface {
  226. uint32_t nnz; /* number of non-zero entries */
  227. uint32_t nrow; /* number of rows */
  228. uintptr_t nzval; /* non-zero values */
  229. uint32_t *colind; /* position of non-zero entried on the row */
  230. uint32_t *rowptr; /* index (in nzval) of the first entry of the row */
  231. /* k for k-based indexing (0 or 1 usually) */
  232. /* also useful when partitionning the matrix ... */
  233. uint32_t firstentry;
  234. size_t elemsize;
  235. };
  236. void starpu_csr_data_register(starpu_data_handle *handle, uint32_t home_node, uint32_t nnz, uint32_t nrow,
  237. uintptr_t nzval, uint32_t *colind, uint32_t *rowptr, uint32_t firstentry, size_t elemsize);
  238. uint32_t starpu_csr_get_nnz(starpu_data_handle handle);
  239. uint32_t starpu_csr_get_nrow(starpu_data_handle handle);
  240. uint32_t starpu_csr_get_firstentry(starpu_data_handle handle);
  241. uintptr_t starpu_csr_get_local_nzval(starpu_data_handle handle);
  242. uint32_t *starpu_csr_get_local_colind(starpu_data_handle handle);
  243. uint32_t *starpu_csr_get_local_rowptr(starpu_data_handle handle);
  244. size_t starpu_csr_get_elemsize(starpu_data_handle handle);
  245. #define STARPU_CSR_GET_NNZ(interface) (((struct starpu_csr_interface *)(interface))->nnz)
  246. #define STARPU_CSR_GET_NROW(interface) (((struct starpu_csr_interface *)(interface))->nrow)
  247. #define STARPU_CSR_GET_NZVAL(interface) (((struct starpu_csr_interface *)(interface))->nzval)
  248. #define STARPU_CSR_GET_COLIND(interface) (((struct starpu_csr_interface *)(interface))->colind)
  249. #define STARPU_CSR_GET_ROWPTR(interface) (((struct starpu_csr_interface *)(interface))->rowptr)
  250. #define STARPU_CSR_GET_FIRSTENTRY(interface) (((struct starpu_csr_interface *)(interface))->firstentry)
  251. #define STARPU_CSR_GET_ELEMSIZE(interface) (((struct starpu_csr_interface *)(interface))->elemsize)
  252. /* BCSR interface for sparse matrices (blocked compressed sparse row
  253. * representation) */
  254. struct starpu_bcsr_interface {
  255. uint32_t nnz; /* number of non-zero BLOCKS */
  256. uint32_t nrow; /* number of rows (in terms of BLOCKS) */
  257. uintptr_t nzval; /* non-zero values */
  258. uint32_t *colind; /* position of non-zero entried on the row */
  259. /* uint32_t *rowind; */ /* position of non-zero entried on the col */
  260. uint32_t *rowptr; /* index (in nzval) of the first entry of the row */
  261. /* k for k-based indexing (0 or 1 usually) */
  262. /* also useful when partitionning the matrix ... */
  263. uint32_t firstentry;
  264. /* size of the blocks */
  265. uint32_t r;
  266. uint32_t c;
  267. size_t elemsize;
  268. };
  269. void starpu_bcsr_data_register(starpu_data_handle *handle, uint32_t home_node, uint32_t nnz, uint32_t nrow,
  270. uintptr_t nzval, uint32_t *colind, uint32_t *rowptr, uint32_t firstentry, uint32_t r, uint32_t c, size_t elemsize);
  271. uint32_t starpu_bcsr_get_nnz(starpu_data_handle handle);
  272. uint32_t starpu_bcsr_get_nrow(starpu_data_handle handle);
  273. uint32_t starpu_bcsr_get_firstentry(starpu_data_handle handle);
  274. uintptr_t starpu_bcsr_get_local_nzval(starpu_data_handle handle);
  275. uint32_t *starpu_bcsr_get_local_colind(starpu_data_handle handle);
  276. uint32_t *starpu_bcsr_get_local_rowptr(starpu_data_handle handle);
  277. uint32_t starpu_bcsr_get_r(starpu_data_handle handle);
  278. uint32_t starpu_bcsr_get_c(starpu_data_handle handle);
  279. size_t starpu_bcsr_get_elemsize(starpu_data_handle handle);
  280. /*
  281. * Multiformat interface
  282. */
  283. struct starpu_multiformat_data_interface_ops {
  284. size_t cpu_elemsize;
  285. #ifdef STARPU_USE_OPENCL
  286. size_t opencl_elemsize;
  287. struct starpu_codelet_t *cpu_to_opencl_cl;
  288. struct starpu_codelet_t *opencl_to_cpu_cl;
  289. #endif
  290. #ifdef STARPU_USE_CUDA
  291. size_t cuda_elemsize;
  292. struct starpu_codelet_t *cpu_to_cuda_cl;
  293. struct starpu_codelet_t *cuda_to_cpu_cl;
  294. #endif
  295. };
  296. struct starpu_multiformat_interface {
  297. void *cpu_ptr;
  298. #ifdef STARPU_USE_CUDA
  299. void *cuda_ptr;
  300. #endif
  301. #ifdef STARPU_USE_OPENCL
  302. void *opencl_ptr;
  303. #endif
  304. uintptr_t dev_handle;
  305. size_t offset;
  306. uint32_t nx;
  307. struct starpu_multiformat_data_interface_ops *ops;
  308. double conversion_time;
  309. };
  310. void starpu_multiformat_data_register(starpu_data_handle *handle,
  311. uint32_t home_node,
  312. void *ptr,
  313. uint32_t nobjects,
  314. struct starpu_multiformat_data_interface_ops *format_ops);
  315. #define STARPU_MULTIFORMAT_GET_PTR(interface) (((struct starpu_multiformat_interface *)(interface))->cpu_ptr)
  316. #ifdef STARPU_USE_CUDA
  317. #define STARPU_MULTIFORMAT_GET_CUDA_PTR(interface) (((struct starpu_multiformat_interface *)(interface))->cuda_ptr)
  318. #endif
  319. #ifdef STARPU_USE_OPENCL
  320. #define STARPU_MULTIFORMAT_GET_OPENCL_PTR(interface) (((struct starpu_multiformat_interface *)(interface))->opencl_ptr)
  321. #endif
  322. #define STARPU_MULTIFORMAT_GET_NX(interface) (((struct starpu_multiformat_interface *)(interface))->nx)
  323. enum starpu_data_interface_id starpu_get_handle_interface_id(starpu_data_handle handle);
  324. /* Lookup a ram pointer into a StarPU handle */
  325. extern starpu_data_handle starpu_data_lookup(const void *ptr);
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #endif /* __STARPU_DATA_INTERFACES_H__ */