matrix_interface.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-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 matrix_copy_data_methods_s =
  22. {
  23. .any_to_any = copy_any_to_any,
  24. };
  25. static void matrix_init(void *data_interface);
  26. static void register_matrix_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  27. static void *matrix_to_pointer(void *data_interface, unsigned node);
  28. static int matrix_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  29. static starpu_ssize_t allocate_matrix_buffer_on_node(void *data_interface_, unsigned dst_node);
  30. static void free_matrix_buffer_on_node(void *data_interface, unsigned node);
  31. static size_t matrix_interface_get_size(starpu_data_handle_t handle);
  32. static size_t matrix_interface_get_alloc_size(starpu_data_handle_t handle);
  33. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle);
  34. static uint32_t alloc_footprint_matrix_interface_crc32(starpu_data_handle_t handle);
  35. static int matrix_compare(void *data_interface_a, void *data_interface_b);
  36. static int matrix_alloc_compare(void *data_interface_a, void *data_interface_b);
  37. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f);
  38. static int pack_matrix_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  39. static int unpack_matrix_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  40. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  41. struct starpu_data_interface_ops starpu_interface_matrix_ops =
  42. {
  43. .init = matrix_init,
  44. .register_data_handle = register_matrix_handle,
  45. .allocate_data_on_node = allocate_matrix_buffer_on_node,
  46. .to_pointer = matrix_to_pointer,
  47. .pointer_is_inside = matrix_pointer_is_inside,
  48. .free_data_on_node = free_matrix_buffer_on_node,
  49. .copy_methods = &matrix_copy_data_methods_s,
  50. .get_size = matrix_interface_get_size,
  51. .get_alloc_size = matrix_interface_get_alloc_size,
  52. .footprint = footprint_matrix_interface_crc32,
  53. .alloc_footprint = alloc_footprint_matrix_interface_crc32,
  54. .compare = matrix_compare,
  55. .alloc_compare = matrix_alloc_compare,
  56. .interfaceid = STARPU_MATRIX_INTERFACE_ID,
  57. .interface_size = sizeof(struct starpu_matrix_interface),
  58. .display = display_matrix_interface,
  59. .pack_data = pack_matrix_handle,
  60. .unpack_data = unpack_matrix_handle,
  61. .describe = describe,
  62. .name = "STARPU_MATRIX_INTERFACE"
  63. };
  64. static void matrix_init(void *data_interface)
  65. {
  66. struct starpu_matrix_interface *matrix_interface = data_interface;
  67. matrix_interface->allocsize = -1;
  68. }
  69. static void register_matrix_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  70. {
  71. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  72. unsigned node;
  73. for (node = 0; node < STARPU_MAXNODES; node++)
  74. {
  75. struct starpu_matrix_interface *local_interface = (struct starpu_matrix_interface *)
  76. starpu_data_get_interface_on_node(handle, node);
  77. if (node == home_node)
  78. {
  79. local_interface->ptr = matrix_interface->ptr;
  80. local_interface->dev_handle = matrix_interface->dev_handle;
  81. local_interface->offset = matrix_interface->offset;
  82. local_interface->ld = matrix_interface->ld;
  83. }
  84. else
  85. {
  86. local_interface->ptr = 0;
  87. local_interface->dev_handle = 0;
  88. local_interface->offset = 0;
  89. local_interface->ld = 0;
  90. }
  91. local_interface->id = matrix_interface->id;
  92. local_interface->nx = matrix_interface->nx;
  93. local_interface->ny = matrix_interface->ny;
  94. local_interface->elemsize = matrix_interface->elemsize;
  95. local_interface->allocsize = matrix_interface->allocsize;
  96. }
  97. }
  98. static void *matrix_to_pointer(void *data_interface, unsigned node)
  99. {
  100. (void) node;
  101. struct starpu_matrix_interface *matrix_interface = data_interface;
  102. return (void*) matrix_interface->ptr;
  103. }
  104. static int matrix_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  105. {
  106. (void) node;
  107. struct starpu_matrix_interface *matrix_interface = data_interface;
  108. uint32_t ld = matrix_interface->ld;
  109. uint32_t nx = matrix_interface->nx;
  110. uint32_t ny = matrix_interface->ny;
  111. size_t elemsize = matrix_interface->elemsize;
  112. return (char*) ptr >= (char*) matrix_interface->ptr &&
  113. (char*) ptr < (char*) matrix_interface->ptr + (ny-1)*ld*elemsize + nx*elemsize;
  114. }
  115. /* declare a new data with the matrix interface */
  116. void starpu_matrix_data_register_allocsize(starpu_data_handle_t *handleptr, int home_node,
  117. uintptr_t ptr, uint32_t ld, uint32_t nx,
  118. uint32_t ny, size_t elemsize, size_t allocsize)
  119. {
  120. struct starpu_matrix_interface matrix_interface =
  121. {
  122. .id = STARPU_MATRIX_INTERFACE_ID,
  123. .ptr = ptr,
  124. .ld = ld,
  125. .nx = nx,
  126. .ny = ny,
  127. .elemsize = elemsize,
  128. .dev_handle = ptr,
  129. .offset = 0,
  130. .allocsize = allocsize,
  131. };
  132. #ifndef STARPU_SIMGRID
  133. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  134. {
  135. if (nx && ny && elemsize)
  136. {
  137. STARPU_ASSERT_ACCESSIBLE(ptr);
  138. STARPU_ASSERT_ACCESSIBLE(ptr + (ny-1)*ld*elemsize + nx*elemsize - 1);
  139. }
  140. }
  141. #endif
  142. starpu_data_register(handleptr, home_node, &matrix_interface, &starpu_interface_matrix_ops);
  143. }
  144. void starpu_matrix_data_register(starpu_data_handle_t *handleptr, int home_node,
  145. uintptr_t ptr, uint32_t ld, uint32_t nx,
  146. uint32_t ny, size_t elemsize)
  147. {
  148. starpu_matrix_data_register_allocsize(handleptr, home_node, ptr, ld, nx, ny, elemsize, nx * ny * elemsize);
  149. }
  150. void starpu_matrix_ptr_register(starpu_data_handle_t handle, unsigned node,
  151. uintptr_t ptr, uintptr_t dev_handle, size_t offset, uint32_t ld)
  152. {
  153. struct starpu_matrix_interface *matrix_interface = starpu_data_get_interface_on_node(handle, node);
  154. starpu_data_ptr_register(handle, node);
  155. matrix_interface->ptr = ptr;
  156. matrix_interface->dev_handle = dev_handle;
  157. matrix_interface->offset = offset;
  158. matrix_interface->ld = ld;
  159. }
  160. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle)
  161. {
  162. return starpu_hash_crc32c_be(starpu_matrix_get_nx(handle), starpu_matrix_get_ny(handle));
  163. }
  164. static uint32_t alloc_footprint_matrix_interface_crc32(starpu_data_handle_t handle)
  165. {
  166. return starpu_hash_crc32c_be(starpu_matrix_get_allocsize(handle), 0);
  167. }
  168. static int matrix_compare(void *data_interface_a, void *data_interface_b)
  169. {
  170. struct starpu_matrix_interface *matrix_a = (struct starpu_matrix_interface *) data_interface_a;
  171. struct starpu_matrix_interface *matrix_b = (struct starpu_matrix_interface *) data_interface_b;
  172. /* Two matricess are considered compatible if they have the same size */
  173. return (matrix_a->nx == matrix_b->nx)
  174. && (matrix_a->ny == matrix_b->ny)
  175. && (matrix_a->elemsize == matrix_b->elemsize);
  176. }
  177. static int matrix_alloc_compare(void *data_interface_a, void *data_interface_b)
  178. {
  179. struct starpu_matrix_interface *matrix_a = (struct starpu_matrix_interface *) data_interface_a;
  180. struct starpu_matrix_interface *matrix_b = (struct starpu_matrix_interface *) data_interface_b;
  181. /* Two matricess are considered allocation-compatible if they have the same size */
  182. return (matrix_a->allocsize == matrix_b->allocsize);
  183. }
  184. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f)
  185. {
  186. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  187. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  188. fprintf(f, "%u\t%u\t", matrix_interface->nx, matrix_interface->ny);
  189. }
  190. #define IS_CONTIGUOUS_MATRIX(nx, ny, ld) ((nx) == (ld))
  191. //#define DYNAMIC_MATRICES
  192. struct pack_matrix_header
  193. {
  194. #ifdef DYNAMIC_MATRICES
  195. /* Receiving matrices with different sizes from MPI */
  196. /* FIXME: that would break alignment for O_DIRECT disk access...
  197. * while in the disk case, we do know the matrix size anyway */
  198. uint32_t nx;
  199. uint32_t ny;
  200. size_t elemsize;
  201. #endif
  202. };
  203. static int pack_matrix_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  204. {
  205. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  206. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  207. starpu_data_get_interface_on_node(handle, node);
  208. uint32_t ld = matrix_interface->ld;
  209. uint32_t nx = matrix_interface->nx;
  210. uint32_t ny = matrix_interface->ny;
  211. size_t elemsize = matrix_interface->elemsize;
  212. *count = nx*ny*elemsize + sizeof(struct pack_matrix_header);
  213. if (ptr != NULL)
  214. {
  215. char *matrix = (void *)matrix_interface->ptr;
  216. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  217. struct pack_matrix_header *header = *ptr;
  218. #ifdef DYNAMIC_MATRICES
  219. header->nx = nx;
  220. header->ny = ny;
  221. header->elemsize = elemsize;
  222. #endif
  223. char *cur = (char*) *ptr + sizeof(*header);
  224. if (IS_CONTIGUOUS_MATRIX(nx, ny, ld))
  225. memcpy(cur, matrix, nx*ny*elemsize);
  226. else
  227. {
  228. uint32_t y;
  229. for(y=0 ; y<ny ; y++)
  230. {
  231. memcpy(cur, matrix, nx*elemsize);
  232. cur += nx*elemsize;
  233. matrix += ld * elemsize;
  234. }
  235. }
  236. }
  237. return 0;
  238. }
  239. static int unpack_matrix_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  240. {
  241. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  242. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  243. starpu_data_get_interface_on_node(handle, node);
  244. uint32_t ld = matrix_interface->ld;
  245. uint32_t nx = matrix_interface->nx;
  246. uint32_t ny = matrix_interface->ny;
  247. size_t elemsize = matrix_interface->elemsize;
  248. struct pack_matrix_header *header = ptr;
  249. #ifdef DYNAMIC_MATRICES
  250. STARPU_ASSERT(count >= sizeof(*header));
  251. if (IS_CONTIGUOUS_MATRIX(nx, ny, ld))
  252. {
  253. /* We can store whatever can fit */
  254. STARPU_ASSERT_MSG(header->elemsize == elemsize,
  255. "Data element size %u needs to be same as the received data element size %u",
  256. (unsigned) elemsize, (unsigned) header->elemsize);
  257. STARPU_ASSERT_MSG(header->nx * header->ny * header->elemsize <= matrix_interface->allocsize,
  258. "Initial size of data %lu needs to be big enough for received data %ux%ux%u",
  259. (unsigned long) matrix_interface->allocsize,
  260. (unsigned) header->nx, (unsigned) header->ny,
  261. (unsigned) header->elemsize);
  262. /* Better keep it contiguous */
  263. matrix_interface->ld = ld = header->nx;
  264. }
  265. else
  266. {
  267. STARPU_ASSERT_MSG(header->nx <= nx,
  268. "Initial nx %u of data needs to be big enough for received data nx %u\n",
  269. nx, header->nx);
  270. STARPU_ASSERT_MSG(header->ny <= ny,
  271. "Initial ny %u of data needs to be big enough for received data ny %u\n",
  272. ny, header->ny);
  273. }
  274. matrix_interface->nx = nx = header->nx;
  275. matrix_interface->ny = ny = header->ny;
  276. #endif
  277. char *cur = (char*) ptr + sizeof(*header);
  278. STARPU_ASSERT(count == sizeof(*header) + elemsize * nx * ny);
  279. char *matrix = (void *)matrix_interface->ptr;
  280. if (IS_CONTIGUOUS_MATRIX(nx, ny, ld))
  281. memcpy(matrix, ptr, nx*ny*elemsize);
  282. else
  283. {
  284. uint32_t y;
  285. for(y=0 ; y<ny ; y++)
  286. {
  287. memcpy(matrix, cur, nx*elemsize);
  288. cur += nx*elemsize;
  289. matrix += ld * elemsize;
  290. }
  291. }
  292. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  293. return 0;
  294. }
  295. static size_t matrix_interface_get_size(starpu_data_handle_t handle)
  296. {
  297. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  298. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  299. #ifdef STARPU_DEBUG
  300. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  301. #endif
  302. return matrix_interface->nx * matrix_interface->ny * matrix_interface->elemsize;
  303. }
  304. static size_t matrix_interface_get_alloc_size(starpu_data_handle_t handle)
  305. {
  306. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  307. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  308. #ifdef STARPU_DEBUG
  309. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  310. #endif
  311. STARPU_ASSERT_MSG(matrix_interface->allocsize != (size_t)-1, "The matrix allocation size needs to be defined");
  312. return matrix_interface->allocsize;
  313. }
  314. /* offer an access to the data parameters */
  315. uint32_t starpu_matrix_get_nx(starpu_data_handle_t handle)
  316. {
  317. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  318. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  319. #ifdef STARPU_DEBUG
  320. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  321. #endif
  322. return matrix_interface->nx;
  323. }
  324. uint32_t starpu_matrix_get_ny(starpu_data_handle_t handle)
  325. {
  326. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  327. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  328. #ifdef STARPU_DEBUG
  329. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  330. #endif
  331. return matrix_interface->ny;
  332. }
  333. uint32_t starpu_matrix_get_local_ld(starpu_data_handle_t handle)
  334. {
  335. unsigned node;
  336. node = starpu_worker_get_local_memory_node();
  337. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  338. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  339. starpu_data_get_interface_on_node(handle, node);
  340. #ifdef STARPU_DEBUG
  341. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  342. #endif
  343. return matrix_interface->ld;
  344. }
  345. uintptr_t starpu_matrix_get_local_ptr(starpu_data_handle_t handle)
  346. {
  347. unsigned node;
  348. node = starpu_worker_get_local_memory_node();
  349. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  350. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  351. starpu_data_get_interface_on_node(handle, node);
  352. #ifdef STARPU_DEBUG
  353. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  354. #endif
  355. return matrix_interface->ptr;
  356. }
  357. size_t starpu_matrix_get_elemsize(starpu_data_handle_t handle)
  358. {
  359. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  360. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  361. #ifdef STARPU_DEBUG
  362. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  363. #endif
  364. return matrix_interface->elemsize;
  365. }
  366. size_t starpu_matrix_get_allocsize(starpu_data_handle_t handle)
  367. {
  368. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  369. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  370. #ifdef STARPU_DEBUG
  371. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  372. #endif
  373. return matrix_interface->allocsize;
  374. }
  375. /* memory allocation/deallocation primitives for the matrix interface */
  376. /* returns the size of the allocated area */
  377. static starpu_ssize_t allocate_matrix_buffer_on_node(void *data_interface_, unsigned dst_node)
  378. {
  379. uintptr_t addr = 0, handle;
  380. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface_;
  381. uint32_t ld = matrix_interface->nx; // by default
  382. starpu_ssize_t allocated_memory = matrix_interface->allocsize;
  383. handle = starpu_malloc_on_node(dst_node, allocated_memory);
  384. if (!handle)
  385. return -ENOMEM;
  386. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  387. addr = handle;
  388. /* update the data properly in consequence */
  389. matrix_interface->ptr = addr;
  390. matrix_interface->dev_handle = handle;
  391. matrix_interface->offset = 0;
  392. matrix_interface->ld = ld;
  393. return allocated_memory;
  394. }
  395. static void free_matrix_buffer_on_node(void *data_interface, unsigned node)
  396. {
  397. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  398. starpu_free_on_node(node, matrix_interface->dev_handle, matrix_interface->allocsize);
  399. }
  400. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  401. {
  402. struct starpu_matrix_interface *src_matrix = (struct starpu_matrix_interface *) src_interface;
  403. struct starpu_matrix_interface *dst_matrix = (struct starpu_matrix_interface *) dst_interface;
  404. int ret = 0;
  405. uint32_t nx = dst_matrix->nx;
  406. uint32_t ny = dst_matrix->ny;
  407. size_t elemsize = dst_matrix->elemsize;
  408. uint32_t ld_src = src_matrix->ld;
  409. uint32_t ld_dst = dst_matrix->ld;
  410. if (starpu_interface_copy2d(src_matrix->dev_handle, src_matrix->offset, src_node,
  411. dst_matrix->dev_handle, dst_matrix->offset, dst_node,
  412. nx * elemsize,
  413. ny, ld_src * elemsize, ld_dst * elemsize,
  414. async_data))
  415. ret = -EAGAIN;
  416. starpu_interface_data_copy(src_node, dst_node, (size_t)nx*ny*elemsize);
  417. return ret;
  418. }
  419. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  420. {
  421. struct starpu_matrix_interface *matrix = (struct starpu_matrix_interface *) data_interface;
  422. return snprintf(buf, size, "M%ux%ux%u",
  423. (unsigned) matrix->nx,
  424. (unsigned) matrix->ny,
  425. (unsigned) matrix->elemsize);
  426. }