block_interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #ifdef BUILDING_STARPU
  18. #include <datawizard/memory_nodes.h>
  19. #endif
  20. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  21. static const struct starpu_data_copy_methods block_copy_data_methods_s =
  22. {
  23. .any_to_any = copy_any_to_any,
  24. };
  25. static void register_block_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  26. static void *block_to_pointer(void *data_interface, unsigned node);
  27. static int block_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  28. static starpu_ssize_t allocate_block_buffer_on_node(void *data_interface_, unsigned dst_node);
  29. static void free_block_buffer_on_node(void *data_interface, unsigned node);
  30. static size_t block_interface_get_size(starpu_data_handle_t handle);
  31. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle);
  32. static int block_compare(void *data_interface_a, void *data_interface_b);
  33. static void display_block_interface(starpu_data_handle_t handle, FILE *f);
  34. static int pack_block_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  35. static int unpack_block_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  36. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  37. struct starpu_data_interface_ops starpu_interface_block_ops =
  38. {
  39. .register_data_handle = register_block_handle,
  40. .allocate_data_on_node = allocate_block_buffer_on_node,
  41. .to_pointer = block_to_pointer,
  42. .pointer_is_inside = block_pointer_is_inside,
  43. .free_data_on_node = free_block_buffer_on_node,
  44. .copy_methods = &block_copy_data_methods_s,
  45. .get_size = block_interface_get_size,
  46. .footprint = footprint_block_interface_crc32,
  47. .compare = block_compare,
  48. .interfaceid = STARPU_BLOCK_INTERFACE_ID,
  49. .interface_size = sizeof(struct starpu_block_interface),
  50. .display = display_block_interface,
  51. .pack_data = pack_block_handle,
  52. .unpack_data = unpack_block_handle,
  53. .describe = describe,
  54. .name = "STARPU_BLOCK_INTERFACE"
  55. };
  56. static void *block_to_pointer(void *data_interface, unsigned node)
  57. {
  58. (void) node;
  59. struct starpu_block_interface *block_interface = data_interface;
  60. return (void*) block_interface->ptr;
  61. }
  62. static int block_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  63. {
  64. (void) node;
  65. struct starpu_block_interface *block_interface = data_interface;
  66. uint32_t ldy = block_interface->ldy;
  67. uint32_t ldz = block_interface->ldz;
  68. uint32_t nx = block_interface->nx;
  69. uint32_t ny = block_interface->ny;
  70. uint32_t nz = block_interface->nz;
  71. size_t elemsize = block_interface->elemsize;
  72. return (char*) ptr >= (char*) block_interface->ptr &&
  73. (char*) ptr < (char*) block_interface->ptr + (nz-1)*ldz*elemsize + (ny-1)*ldy*elemsize + nx*elemsize;
  74. }
  75. static void register_block_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  76. {
  77. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  78. unsigned node;
  79. for (node = 0; node < STARPU_MAXNODES; node++)
  80. {
  81. struct starpu_block_interface *local_interface = (struct starpu_block_interface *)
  82. starpu_data_get_interface_on_node(handle, node);
  83. if (node == home_node)
  84. {
  85. local_interface->ptr = block_interface->ptr;
  86. local_interface->dev_handle = block_interface->dev_handle;
  87. local_interface->offset = block_interface->offset;
  88. local_interface->ldy = block_interface->ldy;
  89. local_interface->ldz = block_interface->ldz;
  90. }
  91. else
  92. {
  93. local_interface->ptr = 0;
  94. local_interface->dev_handle = 0;
  95. local_interface->offset = 0;
  96. local_interface->ldy = 0;
  97. local_interface->ldz = 0;
  98. }
  99. local_interface->id = block_interface->id;
  100. local_interface->nx = block_interface->nx;
  101. local_interface->ny = block_interface->ny;
  102. local_interface->nz = block_interface->nz;
  103. local_interface->elemsize = block_interface->elemsize;
  104. }
  105. }
  106. /* declare a new data with the BLAS interface */
  107. void starpu_block_data_register(starpu_data_handle_t *handleptr, int home_node,
  108. uintptr_t ptr, uint32_t ldy, uint32_t ldz, uint32_t nx,
  109. uint32_t ny, uint32_t nz, size_t elemsize)
  110. {
  111. struct starpu_block_interface block_interface =
  112. {
  113. .id = STARPU_BLOCK_INTERFACE_ID,
  114. .ptr = ptr,
  115. .dev_handle = ptr,
  116. .offset = 0,
  117. .ldy = ldy,
  118. .ldz = ldz,
  119. .nx = nx,
  120. .ny = ny,
  121. .nz = nz,
  122. .elemsize = elemsize
  123. };
  124. #ifndef STARPU_SIMGRID
  125. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  126. {
  127. if (nx && ny && nz && elemsize)
  128. {
  129. STARPU_ASSERT_ACCESSIBLE(ptr);
  130. STARPU_ASSERT_ACCESSIBLE(ptr + (nz-1)*ldz*elemsize + (ny-1)*ldy*elemsize + nx*elemsize - 1);
  131. }
  132. }
  133. #endif
  134. starpu_data_register(handleptr, home_node, &block_interface, &starpu_interface_block_ops);
  135. }
  136. void starpu_block_ptr_register(starpu_data_handle_t handle, unsigned node,
  137. uintptr_t ptr, uintptr_t dev_handle, size_t offset, uint32_t ldy, uint32_t ldz)
  138. {
  139. struct starpu_block_interface *block_interface = starpu_data_get_interface_on_node(handle, node);
  140. starpu_data_ptr_register(handle, node);
  141. block_interface->ptr = ptr;
  142. block_interface->dev_handle = dev_handle;
  143. block_interface->offset = offset;
  144. block_interface->ldy = ldy;
  145. block_interface->ldz = ldz;
  146. }
  147. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle)
  148. {
  149. uint32_t hash;
  150. hash = starpu_hash_crc32c_be(starpu_block_get_nx(handle), 0);
  151. hash = starpu_hash_crc32c_be(starpu_block_get_ny(handle), hash);
  152. hash = starpu_hash_crc32c_be(starpu_block_get_nz(handle), hash);
  153. return hash;
  154. }
  155. static int block_compare(void *data_interface_a, void *data_interface_b)
  156. {
  157. struct starpu_block_interface *block_a = (struct starpu_block_interface *) data_interface_a;
  158. struct starpu_block_interface *block_b = (struct starpu_block_interface *) data_interface_b;
  159. /* Two matricess are considered compatible if they have the same size */
  160. return (block_a->nx == block_b->nx)
  161. && (block_a->ny == block_b->ny)
  162. && (block_a->nz == block_b->nz)
  163. && (block_a->elemsize == block_b->elemsize);
  164. }
  165. static void display_block_interface(starpu_data_handle_t handle, FILE *f)
  166. {
  167. struct starpu_block_interface *block_interface;
  168. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  169. fprintf(f, "%u\t%u\t%u\t", block_interface->nx, block_interface->ny, block_interface->nz);
  170. }
  171. #define IS_CONTIGUOUS_MATRIX(nx, ny, ldy) ((nx) == (ldy))
  172. #define IS_CONTIGUOUS_BLOCK(nx, ny, nz, ldy, ldz) ((nx) * (ny) == (ldz))
  173. static int pack_block_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  174. {
  175. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  176. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  177. starpu_data_get_interface_on_node(handle, node);
  178. uint32_t ldy = block_interface->ldy;
  179. uint32_t ldz = block_interface->ldz;
  180. uint32_t nx = block_interface->nx;
  181. uint32_t ny = block_interface->ny;
  182. uint32_t nz = block_interface->nz;
  183. size_t elemsize = block_interface->elemsize;
  184. *count = nx*ny*nz*elemsize;
  185. if (ptr != NULL)
  186. {
  187. uint32_t z, y;
  188. char *block = (void *)block_interface->ptr;
  189. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  190. char *cur = *ptr;
  191. if (IS_CONTIGUOUS_BLOCK(nx, ny, nz, ldy, ldz))
  192. memcpy(cur, block, nx * ny * nz * elemsize);
  193. else
  194. {
  195. char *block_z = block;
  196. for(z=0 ; z<nz ; z++)
  197. {
  198. if (IS_CONTIGUOUS_MATRIX(nx, ny, ldy))
  199. {
  200. memcpy(cur, block_z, nx * ny * elemsize);
  201. cur += nx*ny*elemsize;
  202. }
  203. else
  204. {
  205. char *block_y = block_z;
  206. for(y=0 ; y<ny ; y++)
  207. {
  208. memcpy(cur, block_y, nx*elemsize);
  209. cur += nx*elemsize;
  210. block_y += ldy * elemsize;
  211. }
  212. }
  213. block_z += ldz * elemsize;
  214. }
  215. }
  216. }
  217. return 0;
  218. }
  219. static int unpack_block_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  220. {
  221. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  222. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  223. starpu_data_get_interface_on_node(handle, node);
  224. uint32_t ldy = block_interface->ldy;
  225. uint32_t ldz = block_interface->ldz;
  226. uint32_t nx = block_interface->nx;
  227. uint32_t ny = block_interface->ny;
  228. uint32_t nz = block_interface->nz;
  229. size_t elemsize = block_interface->elemsize;
  230. STARPU_ASSERT(count == elemsize * nx * ny * nz);
  231. uint32_t z, y;
  232. char *cur = ptr;
  233. char *block = (void *)block_interface->ptr;
  234. if (IS_CONTIGUOUS_BLOCK(nx, ny, nz, ldy, ldz))
  235. memcpy(block, cur, nx * ny * nz * elemsize);
  236. else
  237. {
  238. char *block_z = block;
  239. for(z=0 ; z<nz ; z++)
  240. {
  241. if (IS_CONTIGUOUS_MATRIX(nx, ny, ldy))
  242. {
  243. memcpy(block_z, cur, nx * ny * elemsize);
  244. cur += nx*ny*elemsize;
  245. }
  246. else
  247. {
  248. char *block_y = block_z;
  249. for(y=0 ; y<ny ; y++)
  250. {
  251. memcpy(block_y, cur, nx*elemsize);
  252. cur += nx*elemsize;
  253. block_y += ldy * elemsize;
  254. }
  255. }
  256. block_z += ldz * elemsize;
  257. }
  258. }
  259. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  260. return 0;
  261. }
  262. static size_t block_interface_get_size(starpu_data_handle_t handle)
  263. {
  264. size_t size;
  265. struct starpu_block_interface *block_interface;
  266. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  267. #ifdef STARPU_DEBUG
  268. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  269. #endif
  270. size = block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  271. return size;
  272. }
  273. /* offer an access to the data parameters */
  274. uint32_t starpu_block_get_nx(starpu_data_handle_t handle)
  275. {
  276. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  277. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  278. #ifdef STARPU_DEBUG
  279. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  280. #endif
  281. return block_interface->nx;
  282. }
  283. uint32_t starpu_block_get_ny(starpu_data_handle_t handle)
  284. {
  285. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  286. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  287. #ifdef STARPU_DEBUG
  288. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  289. #endif
  290. return block_interface->ny;
  291. }
  292. uint32_t starpu_block_get_nz(starpu_data_handle_t handle)
  293. {
  294. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  295. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  296. #ifdef STARPU_DEBUG
  297. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  298. #endif
  299. return block_interface->nz;
  300. }
  301. uint32_t starpu_block_get_local_ldy(starpu_data_handle_t handle)
  302. {
  303. unsigned node;
  304. node = starpu_worker_get_local_memory_node();
  305. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  306. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  307. starpu_data_get_interface_on_node(handle, node);
  308. #ifdef STARPU_DEBUG
  309. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  310. #endif
  311. return block_interface->ldy;
  312. }
  313. uint32_t starpu_block_get_local_ldz(starpu_data_handle_t handle)
  314. {
  315. unsigned node;
  316. node = starpu_worker_get_local_memory_node();
  317. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  318. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  319. starpu_data_get_interface_on_node(handle, node);
  320. #ifdef STARPU_DEBUG
  321. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  322. #endif
  323. return block_interface->ldz;
  324. }
  325. uintptr_t starpu_block_get_local_ptr(starpu_data_handle_t handle)
  326. {
  327. unsigned node;
  328. node = starpu_worker_get_local_memory_node();
  329. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  330. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  331. starpu_data_get_interface_on_node(handle, node);
  332. #ifdef STARPU_DEBUG
  333. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  334. #endif
  335. return block_interface->ptr;
  336. }
  337. size_t starpu_block_get_elemsize(starpu_data_handle_t handle)
  338. {
  339. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  340. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  341. #ifdef STARPU_DEBUG
  342. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  343. #endif
  344. return block_interface->elemsize;
  345. }
  346. /* memory allocation/deallocation primitives for the BLOCK interface */
  347. /* returns the size of the allocated area */
  348. static starpu_ssize_t allocate_block_buffer_on_node(void *data_interface_, unsigned dst_node)
  349. {
  350. uintptr_t addr = 0, handle;
  351. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) data_interface_;
  352. uint32_t nx = dst_block->nx;
  353. uint32_t ny = dst_block->ny;
  354. uint32_t nz = dst_block->nz;
  355. size_t elemsize = dst_block->elemsize;
  356. starpu_ssize_t allocated_memory;
  357. handle = starpu_malloc_on_node(dst_node, nx*ny*nz*elemsize);
  358. if (!handle)
  359. return -ENOMEM;
  360. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  361. addr = handle;
  362. allocated_memory = nx*ny*nz*elemsize;
  363. /* update the data properly in consequence */
  364. dst_block->ptr = addr;
  365. dst_block->dev_handle = handle;
  366. dst_block->offset = 0;
  367. dst_block->ldy = nx;
  368. dst_block->ldz = nx*ny;
  369. return allocated_memory;
  370. }
  371. static void free_block_buffer_on_node(void *data_interface, unsigned node)
  372. {
  373. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  374. uint32_t nx = block_interface->nx;
  375. uint32_t ny = block_interface->ny;
  376. uint32_t nz = block_interface->nz;
  377. size_t elemsize = block_interface->elemsize;
  378. starpu_free_on_node(node, block_interface->dev_handle, nx*ny*nz*elemsize);
  379. }
  380. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  381. {
  382. struct starpu_block_interface *src_block = (struct starpu_block_interface *) src_interface;
  383. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) dst_interface;
  384. int ret = 0;
  385. uint32_t nx = dst_block->nx;
  386. uint32_t ny = dst_block->ny;
  387. uint32_t nz = dst_block->nz;
  388. size_t elemsize = dst_block->elemsize;
  389. uint32_t ldy_src = src_block->ldy;
  390. uint32_t ldz_src = src_block->ldz;
  391. uint32_t ldy_dst = dst_block->ldy;
  392. uint32_t ldz_dst = dst_block->ldz;
  393. if (starpu_interface_copy3d(src_block->dev_handle, src_block->offset, src_node,
  394. dst_block->dev_handle, dst_block->offset, dst_node,
  395. nx * elemsize,
  396. ny, ldy_src * elemsize, ldy_dst * elemsize,
  397. nz, ldz_src * elemsize, ldz_dst * elemsize,
  398. async_data))
  399. ret = -EAGAIN;
  400. starpu_interface_data_copy(src_node, dst_node, nx*ny*nz*elemsize);
  401. return ret;
  402. }
  403. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  404. {
  405. struct starpu_block_interface *block = (struct starpu_block_interface *) data_interface;
  406. return snprintf(buf, size, "B%ux%ux%ux%u",
  407. (unsigned) block->nx,
  408. (unsigned) block->ny,
  409. (unsigned) block->nz,
  410. (unsigned) block->elemsize);
  411. }