matrix_interface.c 17 KB

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