tensor_interface.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux
  4. * Copyright (C) 2011,2012,2017 Inria
  5. * Copyright (C) 2010-2017,2019 CNRS
  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. #ifdef STARPU_USE_CUDA
  20. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  21. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  22. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  23. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  24. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  25. #endif
  26. #ifdef STARPU_USE_OPENCL
  27. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  28. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  29. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  30. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  31. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  32. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  33. #endif
  34. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  35. static const struct starpu_data_copy_methods tensor_copy_data_methods_s =
  36. {
  37. #ifdef STARPU_USE_CUDA
  38. .ram_to_cuda = copy_ram_to_cuda,
  39. .cuda_to_ram = copy_cuda_to_ram,
  40. .ram_to_cuda_async = copy_ram_to_cuda_async,
  41. .cuda_to_ram_async = copy_cuda_to_ram_async,
  42. .cuda_to_cuda = copy_cuda_to_cuda,
  43. #endif
  44. #ifdef STARPU_USE_OPENCL
  45. .ram_to_opencl = copy_ram_to_opencl,
  46. .opencl_to_ram = copy_opencl_to_ram,
  47. .opencl_to_opencl = copy_opencl_to_opencl,
  48. .ram_to_opencl_async = copy_ram_to_opencl_async,
  49. .opencl_to_ram_async = copy_opencl_to_ram_async,
  50. .opencl_to_opencl_async = copy_opencl_to_opencl_async,
  51. #endif
  52. .any_to_any = copy_any_to_any,
  53. };
  54. static void register_tensor_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  55. static void *tensor_to_pointer(void *data_interface, unsigned node);
  56. static int tensor_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  57. static starpu_ssize_t allocate_tensor_buffer_on_node(void *data_interface_, unsigned dst_node);
  58. static void free_tensor_buffer_on_node(void *data_interface, unsigned node);
  59. static size_t tensor_interface_get_size(starpu_data_handle_t handle);
  60. static uint32_t footprint_tensor_interface_crc32(starpu_data_handle_t handle);
  61. static int tensor_compare(void *data_interface_a, void *data_interface_b);
  62. static void display_tensor_interface(starpu_data_handle_t handle, FILE *f);
  63. static int pack_tensor_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  64. static int unpack_tensor_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  65. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  66. struct starpu_data_interface_ops starpu_interface_tensor_ops =
  67. {
  68. .register_data_handle = register_tensor_handle,
  69. .allocate_data_on_node = allocate_tensor_buffer_on_node,
  70. .to_pointer = tensor_to_pointer,
  71. .pointer_is_inside = tensor_pointer_is_inside,
  72. .free_data_on_node = free_tensor_buffer_on_node,
  73. .copy_methods = &tensor_copy_data_methods_s,
  74. .get_size = tensor_interface_get_size,
  75. .footprint = footprint_tensor_interface_crc32,
  76. .compare = tensor_compare,
  77. .interfaceid = STARPU_TENSOR_INTERFACE_ID,
  78. .interface_size = sizeof(struct starpu_tensor_interface),
  79. .display = display_tensor_interface,
  80. .pack_data = pack_tensor_handle,
  81. .unpack_data = unpack_tensor_handle,
  82. .describe = describe,
  83. .name = "STARPU_TENSOR_INTERFACE"
  84. };
  85. static void *tensor_to_pointer(void *data_interface, unsigned node)
  86. {
  87. (void) node;
  88. struct starpu_tensor_interface *tensor_interface = data_interface;
  89. return (void*) tensor_interface->ptr;
  90. }
  91. static int tensor_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  92. {
  93. (void) node;
  94. struct starpu_tensor_interface *tensor_interface = data_interface;
  95. uint32_t ldy = tensor_interface->ldy;
  96. uint32_t ldz = tensor_interface->ldz;
  97. uint32_t ldt = tensor_interface->ldt;
  98. uint32_t nx = tensor_interface->nx;
  99. uint32_t ny = tensor_interface->ny;
  100. uint32_t nz = tensor_interface->nz;
  101. uint32_t nt = tensor_interface->nt;
  102. size_t elemsize = tensor_interface->elemsize;
  103. return (char*) ptr >= (char*) tensor_interface->ptr &&
  104. (char*) ptr < (char*) tensor_interface->ptr + (nt-1)*ldt*elemsize + (nz-1)*ldz*elemsize + (ny-1)*ldy*elemsize + nx*elemsize;
  105. }
  106. static void register_tensor_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  107. {
  108. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *) data_interface;
  109. unsigned node;
  110. for (node = 0; node < STARPU_MAXNODES; node++)
  111. {
  112. struct starpu_tensor_interface *local_interface = (struct starpu_tensor_interface *)
  113. starpu_data_get_interface_on_node(handle, node);
  114. if (node == home_node)
  115. {
  116. local_interface->ptr = tensor_interface->ptr;
  117. local_interface->dev_handle = tensor_interface->dev_handle;
  118. local_interface->offset = tensor_interface->offset;
  119. local_interface->ldy = tensor_interface->ldy;
  120. local_interface->ldz = tensor_interface->ldz;
  121. local_interface->ldt = tensor_interface->ldt;
  122. }
  123. else
  124. {
  125. local_interface->ptr = 0;
  126. local_interface->dev_handle = 0;
  127. local_interface->offset = 0;
  128. local_interface->ldy = 0;
  129. local_interface->ldz = 0;
  130. local_interface->ldt = 0;
  131. }
  132. local_interface->id = tensor_interface->id;
  133. local_interface->nx = tensor_interface->nx;
  134. local_interface->ny = tensor_interface->ny;
  135. local_interface->nz = tensor_interface->nz;
  136. local_interface->nt = tensor_interface->nt;
  137. local_interface->elemsize = tensor_interface->elemsize;
  138. }
  139. }
  140. /* declare a new data with the BLAS interface */
  141. void starpu_tensor_data_register(starpu_data_handle_t *handleptr, int home_node,
  142. uintptr_t ptr, uint32_t ldy, uint32_t ldz, uint32_t ldt, uint32_t nx,
  143. uint32_t ny, uint32_t nz, uint32_t nt, size_t elemsize)
  144. {
  145. struct starpu_tensor_interface tensor_interface =
  146. {
  147. .id = STARPU_TENSOR_INTERFACE_ID,
  148. .ptr = ptr,
  149. .dev_handle = ptr,
  150. .offset = 0,
  151. .ldy = ldy,
  152. .ldz = ldz,
  153. .ldt = ldt,
  154. .nx = nx,
  155. .ny = ny,
  156. .nz = nz,
  157. .nt = nt,
  158. .elemsize = elemsize
  159. };
  160. #ifndef STARPU_SIMGRID
  161. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  162. {
  163. STARPU_ASSERT_ACCESSIBLE(ptr);
  164. STARPU_ASSERT_ACCESSIBLE(ptr + (nt-1)*ldt*elemsize + (nz-1)*ldz*elemsize + (ny-1)*ldy*elemsize + nx*elemsize - 1);
  165. }
  166. #endif
  167. starpu_data_register(handleptr, home_node, &tensor_interface, &starpu_interface_tensor_ops);
  168. }
  169. void starpu_tensor_ptr_register(starpu_data_handle_t handle, unsigned node,
  170. uintptr_t ptr, uintptr_t dev_handle, size_t offset, uint32_t ldy, uint32_t ldz, uint32_t ldt)
  171. {
  172. struct starpu_tensor_interface *tensor_interface = starpu_data_get_interface_on_node(handle, node);
  173. starpu_data_ptr_register(handle, node);
  174. tensor_interface->ptr = ptr;
  175. tensor_interface->dev_handle = dev_handle;
  176. tensor_interface->offset = offset;
  177. tensor_interface->ldy = ldy;
  178. tensor_interface->ldz = ldz;
  179. tensor_interface->ldt = ldt;
  180. }
  181. static uint32_t footprint_tensor_interface_crc32(starpu_data_handle_t handle)
  182. {
  183. uint32_t hash;
  184. hash = starpu_hash_crc32c_be(starpu_tensor_get_nx(handle), 0);
  185. hash = starpu_hash_crc32c_be(starpu_tensor_get_ny(handle), hash);
  186. hash = starpu_hash_crc32c_be(starpu_tensor_get_nz(handle), hash);
  187. hash = starpu_hash_crc32c_be(starpu_tensor_get_nt(handle), hash);
  188. return hash;
  189. }
  190. static int tensor_compare(void *data_interface_a, void *data_interface_b)
  191. {
  192. struct starpu_tensor_interface *tensor_a = (struct starpu_tensor_interface *) data_interface_a;
  193. struct starpu_tensor_interface *tensor_b = (struct starpu_tensor_interface *) data_interface_b;
  194. /* Two matricess are considered compatible if they have the same size */
  195. return (tensor_a->nx == tensor_b->nx)
  196. && (tensor_a->ny == tensor_b->ny)
  197. && (tensor_a->nz == tensor_b->nz)
  198. && (tensor_a->nt == tensor_b->nt)
  199. && (tensor_a->elemsize == tensor_b->elemsize);
  200. }
  201. static void display_tensor_interface(starpu_data_handle_t handle, FILE *f)
  202. {
  203. struct starpu_tensor_interface *tensor_interface;
  204. tensor_interface = (struct starpu_tensor_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  205. fprintf(f, "%u\t%u\t%u\t%u\t", tensor_interface->nx, tensor_interface->ny, tensor_interface->nz, tensor_interface->nt);
  206. }
  207. #define IS_CONTIGUOUS_MATRIX(nx, ny, ldy) ((nx) == (ldy))
  208. #define IS_CONTIGUOUS_BLOCK(nx, ny, nz, ldy, ldz) ((nx) * (ny) == (ldz))
  209. #define IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, ldy, ldz, ldt) ((nx) * (ny) * (nz) == (ldt))
  210. static int pack_tensor_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  211. {
  212. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  213. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  214. starpu_data_get_interface_on_node(handle, node);
  215. uint32_t ldy = tensor_interface->ldy;
  216. uint32_t ldz = tensor_interface->ldz;
  217. uint32_t ldt = tensor_interface->ldt;
  218. uint32_t nx = tensor_interface->nx;
  219. uint32_t ny = tensor_interface->ny;
  220. uint32_t nz = tensor_interface->nz;
  221. uint32_t nt = tensor_interface->nt;
  222. size_t elemsize = tensor_interface->elemsize;
  223. *count = nx*ny*nz*nt*elemsize;
  224. if (ptr != NULL)
  225. {
  226. uint32_t t, z, y;
  227. char *block = (void *)tensor_interface->ptr;
  228. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  229. char *cur = *ptr;
  230. if (IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, ldy, ldz, ldt))
  231. memcpy(cur, block, nx * ny * nz * nt * elemsize);
  232. else
  233. {
  234. char *block_t = block;
  235. for(t=0 ; t<nt ; t++)
  236. {
  237. if (IS_CONTIGUOUS_BLOCK(nx, ny, nz, ldy, ldz))
  238. {
  239. memcpy(cur, block_t, nx * ny * nz * elemsize);
  240. cur += nx*ny*nz*elemsize;
  241. }
  242. else
  243. {
  244. char *block_z = block_t;
  245. for(z=0 ; z<nz ; z++)
  246. {
  247. if (IS_CONTIGUOUS_MATRIX(nx, ny, ldy))
  248. {
  249. memcpy(cur, block_z, nx * ny * elemsize);
  250. cur += nx*ny*elemsize;
  251. }
  252. else
  253. {
  254. char *block_y = block_z;
  255. for(y=0 ; y<ny ; y++)
  256. {
  257. memcpy(cur, block_y, nx*elemsize);
  258. cur += nx*elemsize;
  259. block_y += ldy * elemsize;
  260. }
  261. }
  262. block_z += ldz * elemsize;
  263. }
  264. }
  265. block_t += ldt * elemsize;
  266. }
  267. }
  268. }
  269. return 0;
  270. }
  271. static int unpack_tensor_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  272. {
  273. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  274. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  275. starpu_data_get_interface_on_node(handle, node);
  276. uint32_t ldy = tensor_interface->ldy;
  277. uint32_t ldz = tensor_interface->ldz;
  278. uint32_t ldt = tensor_interface->ldt;
  279. uint32_t nx = tensor_interface->nx;
  280. uint32_t ny = tensor_interface->ny;
  281. uint32_t nz = tensor_interface->nz;
  282. uint32_t nt = tensor_interface->nt;
  283. size_t elemsize = tensor_interface->elemsize;
  284. STARPU_ASSERT(count == elemsize * nx * ny * nz * nt);
  285. uint32_t t, z, y;
  286. char *cur = ptr;
  287. char *block = (void *)tensor_interface->ptr;
  288. if (IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, ldy, ldz, ldt))
  289. memcpy(block, cur, nx * ny * nz * nt * elemsize);
  290. else
  291. {
  292. char *block_t = block;
  293. for(t=0 ; t<nt ; t++)
  294. {
  295. if (IS_CONTIGUOUS_BLOCK(nx, ny, nz, ldy, ldz))
  296. {
  297. memcpy(block_t, cur, nx * ny * nz * elemsize);
  298. cur += nx*ny*nz*elemsize;
  299. }
  300. else
  301. {
  302. char *block_z = block_t;
  303. for(z=0 ; z<nz ; z++)
  304. {
  305. if (IS_CONTIGUOUS_MATRIX(nx, ny, ldy))
  306. {
  307. memcpy(block_z, cur, nx * ny * elemsize);
  308. cur += nx*ny*elemsize;
  309. }
  310. else
  311. {
  312. char *block_y = block_z;
  313. for(y=0 ; y<ny ; y++)
  314. {
  315. memcpy(block_y, cur, nx*elemsize);
  316. cur += nx*elemsize;
  317. block_y += ldy * elemsize;
  318. }
  319. }
  320. block_z += ldz * elemsize;
  321. }
  322. }
  323. block_t += ldt * elemsize;
  324. }
  325. }
  326. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  327. return 0;
  328. }
  329. static size_t tensor_interface_get_size(starpu_data_handle_t handle)
  330. {
  331. size_t size;
  332. struct starpu_tensor_interface *tensor_interface;
  333. tensor_interface = (struct starpu_tensor_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  334. #ifdef STARPU_DEBUG
  335. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  336. #endif
  337. size = tensor_interface->nx*tensor_interface->ny*tensor_interface->nz*tensor_interface->nt*tensor_interface->elemsize;
  338. return size;
  339. }
  340. /* offer an access to the data parameters */
  341. uint32_t starpu_tensor_get_nx(starpu_data_handle_t handle)
  342. {
  343. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  344. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  345. #ifdef STARPU_DEBUG
  346. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  347. #endif
  348. return tensor_interface->nx;
  349. }
  350. uint32_t starpu_tensor_get_ny(starpu_data_handle_t handle)
  351. {
  352. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  353. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  354. #ifdef STARPU_DEBUG
  355. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  356. #endif
  357. return tensor_interface->ny;
  358. }
  359. uint32_t starpu_tensor_get_nz(starpu_data_handle_t handle)
  360. {
  361. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  362. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  363. #ifdef STARPU_DEBUG
  364. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  365. #endif
  366. return tensor_interface->nz;
  367. }
  368. uint32_t starpu_tensor_get_nt(starpu_data_handle_t handle)
  369. {
  370. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  371. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  372. #ifdef STARPU_DEBUG
  373. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  374. #endif
  375. return tensor_interface->nt;
  376. }
  377. uint32_t starpu_tensor_get_local_ldy(starpu_data_handle_t handle)
  378. {
  379. unsigned node;
  380. node = starpu_worker_get_local_memory_node();
  381. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  382. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  383. starpu_data_get_interface_on_node(handle, node);
  384. #ifdef STARPU_DEBUG
  385. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  386. #endif
  387. return tensor_interface->ldy;
  388. }
  389. uint32_t starpu_tensor_get_local_ldz(starpu_data_handle_t handle)
  390. {
  391. unsigned node;
  392. node = starpu_worker_get_local_memory_node();
  393. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  394. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  395. starpu_data_get_interface_on_node(handle, node);
  396. #ifdef STARPU_DEBUG
  397. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  398. #endif
  399. return tensor_interface->ldz;
  400. }
  401. uint32_t starpu_tensor_get_local_ldt(starpu_data_handle_t handle)
  402. {
  403. unsigned node;
  404. node = starpu_worker_get_local_memory_node();
  405. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  406. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  407. starpu_data_get_interface_on_node(handle, node);
  408. #ifdef STARPU_DEBUG
  409. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  410. #endif
  411. return tensor_interface->ldt;
  412. }
  413. uintptr_t starpu_tensor_get_local_ptr(starpu_data_handle_t handle)
  414. {
  415. unsigned node;
  416. node = starpu_worker_get_local_memory_node();
  417. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  418. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  419. starpu_data_get_interface_on_node(handle, node);
  420. #ifdef STARPU_DEBUG
  421. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  422. #endif
  423. return tensor_interface->ptr;
  424. }
  425. size_t starpu_tensor_get_elemsize(starpu_data_handle_t handle)
  426. {
  427. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *)
  428. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  429. #ifdef STARPU_DEBUG
  430. STARPU_ASSERT_MSG(tensor_interface->id == STARPU_TENSOR_INTERFACE_ID, "Error. The given data is not a block.");
  431. #endif
  432. return tensor_interface->elemsize;
  433. }
  434. /* memory allocation/deallocation primitives for the BLOCK interface */
  435. /* returns the size of the allocated area */
  436. static starpu_ssize_t allocate_tensor_buffer_on_node(void *data_interface_, unsigned dst_node)
  437. {
  438. uintptr_t addr = 0, handle;
  439. struct starpu_tensor_interface *dst_block = (struct starpu_tensor_interface *) data_interface_;
  440. uint32_t nx = dst_block->nx;
  441. uint32_t ny = dst_block->ny;
  442. uint32_t nz = dst_block->nz;
  443. uint32_t nt = dst_block->nt;
  444. size_t elemsize = dst_block->elemsize;
  445. starpu_ssize_t allocated_memory;
  446. handle = starpu_malloc_on_node(dst_node, nx*ny*nz*nt*elemsize);
  447. if (!handle)
  448. return -ENOMEM;
  449. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  450. addr = handle;
  451. allocated_memory = nx*ny*nz*nt*elemsize;
  452. /* update the data properly in consequence */
  453. dst_block->ptr = addr;
  454. dst_block->dev_handle = handle;
  455. dst_block->offset = 0;
  456. dst_block->ldy = nx;
  457. dst_block->ldz = nx*ny;
  458. dst_block->ldt = nx*ny*nz;
  459. return allocated_memory;
  460. }
  461. static void free_tensor_buffer_on_node(void *data_interface, unsigned node)
  462. {
  463. struct starpu_tensor_interface *tensor_interface = (struct starpu_tensor_interface *) data_interface;
  464. uint32_t nx = tensor_interface->nx;
  465. uint32_t ny = tensor_interface->ny;
  466. uint32_t nz = tensor_interface->nz;
  467. uint32_t nt = tensor_interface->nt;
  468. size_t elemsize = tensor_interface->elemsize;
  469. starpu_free_on_node(node, tensor_interface->dev_handle, nx*ny*nz*nt*elemsize);
  470. }
  471. #ifdef STARPU_USE_CUDA
  472. static int copy_cuda_common(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, enum cudaMemcpyKind kind)
  473. {
  474. struct starpu_tensor_interface *src_block = src_interface;
  475. struct starpu_tensor_interface *dst_block = dst_interface;
  476. uint32_t nx = src_block->nx;
  477. uint32_t ny = src_block->ny;
  478. uint32_t nz = src_block->nz;
  479. uint32_t nt = src_block->nt;
  480. size_t elemsize = src_block->elemsize;
  481. cudaError_t cures;
  482. if (IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, src_block->ldy, src_block->ldz, src_block->ldt) &&
  483. IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, dst_block->ldy, dst_block->ldz, dst_block->ldt))
  484. {
  485. /* Same lds on both sides, and contiguous, simple */
  486. starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*nt*elemsize, NULL, kind);
  487. }
  488. else
  489. {
  490. /* TODO: use cudaMemcpy2D for whole 3D blocks etc. when they are contiguous */
  491. /* Default case: we transfer all blocks one by one: nz transfers */
  492. /* TODO: use cudaMemcpy3D now that it works (except on cuda 4.2) */
  493. unsigned t;
  494. for (t = 0; t < src_block->nt; t++)
  495. {
  496. unsigned z;
  497. for (z = 0; z < src_block->nz; z++)
  498. {
  499. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + t*src_block->ldt*src_block->elemsize + z*src_block->ldz*src_block->elemsize;
  500. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + t*dst_block->ldt*src_block->elemsize + z*dst_block->ldz*dst_block->elemsize;
  501. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  502. (char *)src_ptr, src_block->ldy*elemsize,
  503. nx*elemsize, ny, kind);
  504. if (!cures)
  505. cures = cudaDeviceSynchronize();
  506. if (STARPU_UNLIKELY(cures))
  507. STARPU_CUDA_REPORT_ERROR(cures);
  508. }
  509. }
  510. }
  511. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->nt*src_block->elemsize);
  512. return 0;
  513. }
  514. static int copy_cuda_async_common(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream, enum cudaMemcpyKind kind)
  515. {
  516. struct starpu_tensor_interface *src_block = src_interface;
  517. struct starpu_tensor_interface *dst_block = dst_interface;
  518. uint32_t nx = src_block->nx;
  519. uint32_t ny = src_block->ny;
  520. uint32_t nz = src_block->nz;
  521. uint32_t nt = src_block->nt;
  522. size_t elemsize = src_block->elemsize;
  523. cudaError_t cures;
  524. int ret;
  525. if (IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, src_block->ldy, src_block->ldz, src_block->ldt) &&
  526. IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, dst_block->ldy, dst_block->ldz, dst_block->ldt))
  527. {
  528. /* Same lds on both sides, and contiguous, simple */
  529. ret = starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*nt*elemsize, stream, kind);
  530. }
  531. else
  532. {
  533. /* TODO: use cudaMemcpy2D for whole 3D blocks etc. when they are contiguous */
  534. /* Default case: we transfer all blocks one by one: nz transfers */
  535. /* TODO: use cudaMemcpy3D now that it works (except on cuda 4.2) */
  536. unsigned t;
  537. for (t = 0; t < src_block->nt; t++)
  538. {
  539. unsigned z;
  540. for (z = 0; z < src_block->nz; z++)
  541. {
  542. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + t*src_block->ldt*src_block->elemsize + z*src_block->ldz*src_block->elemsize;
  543. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + t*dst_block->ldt*dst_block->elemsize + z*dst_block->ldz*dst_block->elemsize;
  544. double start;
  545. starpu_interface_start_driver_copy_async(src_node, dst_node, &start);
  546. cures = cudaMemcpy2DAsync((char *)dst_ptr, dst_block->ldy*elemsize,
  547. (char *)src_ptr, src_block->ldy*elemsize,
  548. nx*elemsize, ny, kind, stream);
  549. starpu_interface_end_driver_copy_async(src_node, dst_node, start);
  550. if (STARPU_UNLIKELY(cures))
  551. {
  552. /* I don't know how to do that "better" */
  553. goto no_async_default;
  554. }
  555. }
  556. }
  557. ret = -EAGAIN;
  558. }
  559. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->nt*src_block->elemsize);
  560. return ret;
  561. no_async_default:
  562. {
  563. unsigned t;
  564. for (t = 0; t < src_block->nt; t++)
  565. {
  566. unsigned z;
  567. for (z = 0; z < src_block->nz; z++)
  568. {
  569. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + t*src_block->ldt*src_block->elemsize + z*src_block->ldz*src_block->elemsize;
  570. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + t*dst_block->ldt*dst_block->elemsize + z*dst_block->ldz*dst_block->elemsize;
  571. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  572. (char *)src_ptr, src_block->ldy*elemsize,
  573. nx*elemsize, ny, kind);
  574. if (!cures)
  575. cures = cudaDeviceSynchronize();
  576. if (STARPU_UNLIKELY(cures))
  577. STARPU_CUDA_REPORT_ERROR(cures);
  578. }
  579. }
  580. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->nt*src_block->elemsize);
  581. return 0;
  582. }
  583. }
  584. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  585. {
  586. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  587. }
  588. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  589. {
  590. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  591. }
  592. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  593. {
  594. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice);
  595. }
  596. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream)
  597. {
  598. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToHost);
  599. }
  600. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream)
  601. {
  602. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyHostToDevice);
  603. }
  604. #endif // STARPU_USE_CUDA
  605. #ifdef STARPU_USE_OPENCL
  606. static int copy_opencl_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  607. {
  608. struct starpu_tensor_interface *src_block = src_interface;
  609. struct starpu_tensor_interface *dst_block = dst_interface;
  610. int ret = 0;
  611. uint32_t nx = src_block->nx;
  612. uint32_t ny = src_block->ny;
  613. uint32_t nz = src_block->nz;
  614. /* We may have a contiguous buffer for the entire block, or contiguous
  615. * plans within the block, we can avoid many small transfers that way */
  616. if (IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, src_block->ldy, src_block->ldz, src_block->ldt) &&
  617. IS_CONTIGUOUS_TENSOR(nx, ny, nz, nt, dst_block->ldy, dst_block->ldz, dst_block->ldt))
  618. {
  619. ret = starpu_opencl_copy_async_sync(src_block->dev_handle, src_block->offset, src_node,
  620. dst_block->dev_handle, dst_block->offset, dst_node,
  621. src_block->nx*src_block->ny*src_block->nz*src_block->nt*src_block->elemsize,
  622. event);
  623. }
  624. else
  625. {
  626. /* Default case: we transfer all lines one by one: ny*nz transfers */
  627. /* TODO: rect support */
  628. unsigned t;
  629. for (t = 0; t < src_block->nt; t++)
  630. {
  631. unsigned z;
  632. for (z = 0; z < src_block->nz; z++)
  633. {
  634. unsigned j;
  635. for(j=0 ; j<src_block->ny ; j++)
  636. {
  637. ret = starpu_opencl_copy_async_sync(src_block->dev_handle,
  638. src_block->offset + t*src_block->ldt*src_block->elemsize + z*src_block->ldz*src_block->elemsize + j*src_block->ldy*src_block->elemsize,
  639. src_node,
  640. dst_block->dev_handle,
  641. dst_block->offset + t*dst_block->ldt*dst_block->elemsize + z*dst_block->ldz*dst_block->elemsize + j*dst_block->ldy*dst_block->elemsize,
  642. dst_node,
  643. src_block->nx*src_block->elemsize,
  644. event);
  645. }
  646. }
  647. }
  648. }
  649. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->nt*src_block->elemsize);
  650. return ret;
  651. }
  652. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  653. {
  654. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  655. }
  656. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  657. {
  658. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  659. }
  660. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  661. {
  662. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  663. }
  664. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  665. {
  666. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  667. }
  668. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  669. {
  670. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  671. }
  672. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  673. {
  674. return copy_opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  675. }
  676. #endif
  677. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  678. {
  679. struct starpu_tensor_interface *src_block = (struct starpu_tensor_interface *) src_interface;
  680. struct starpu_tensor_interface *dst_block = (struct starpu_tensor_interface *) dst_interface;
  681. int ret = 0;
  682. uint32_t nx = dst_block->nx;
  683. uint32_t ny = dst_block->ny;
  684. uint32_t nz = dst_block->nz;
  685. uint32_t nt = dst_block->nt;
  686. size_t elemsize = dst_block->elemsize;
  687. uint32_t ldy_src = src_block->ldy;
  688. uint32_t ldz_src = src_block->ldz;
  689. uint32_t ldt_src = src_block->ldt;
  690. uint32_t ldy_dst = dst_block->ldy;
  691. uint32_t ldz_dst = dst_block->ldz;
  692. uint32_t ldt_dst = dst_block->ldt;
  693. if (starpu_interface_copy4d(src_block->dev_handle, src_block->offset, src_node,
  694. dst_block->dev_handle, dst_block->offset, dst_node,
  695. nx * elemsize,
  696. ny, ldy_src * elemsize, ldy_dst * elemsize,
  697. nz, ldz_src * elemsize, ldz_dst * elemsize,
  698. nt, ldt_src * elemsize, ldt_dst * elemsize,
  699. async_data))
  700. ret = -EAGAIN;
  701. starpu_interface_data_copy(src_node, dst_node, nx*ny*nz*nt*elemsize);
  702. return ret;
  703. }
  704. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  705. {
  706. struct starpu_tensor_interface *block = (struct starpu_tensor_interface *) data_interface;
  707. return snprintf(buf, size, "T%ux%ux%ux%ux%u",
  708. (unsigned) block->nx,
  709. (unsigned) block->ny,
  710. (unsigned) block->nz,
  711. (unsigned) block->nt,
  712. (unsigned) block->elemsize);
  713. }