matrix_interface.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <datawizard/coherency.h>
  20. #include <datawizard/copy_driver.h>
  21. #include <datawizard/filters.h>
  22. #include <starpu_hash.h>
  23. #include <starpu_cuda.h>
  24. #include <starpu_opencl.h>
  25. #include <drivers/opencl/driver_opencl.h>
  26. #include <drivers/scc/driver_scc_source.h>
  27. #include <drivers/mic/driver_mic_source.h>
  28. /* If you can promise that there is no stride in your matrices, you can define this */
  29. // #define NO_STRIDE
  30. #ifdef STARPU_USE_CUDA
  31. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  32. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  33. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  34. 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);
  35. 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);
  36. #ifdef NO_STRIDE
  37. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  38. #endif
  39. #endif
  40. #ifdef STARPU_USE_OPENCL
  41. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  42. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  43. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  44. 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);
  45. 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);
  46. 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);
  47. #endif
  48. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  49. static const struct starpu_data_copy_methods matrix_copy_data_methods_s =
  50. {
  51. #ifdef STARPU_USE_CUDA
  52. .ram_to_cuda = copy_ram_to_cuda,
  53. .cuda_to_ram = copy_cuda_to_ram,
  54. .ram_to_cuda_async = copy_ram_to_cuda_async,
  55. .cuda_to_ram_async = copy_cuda_to_ram_async,
  56. .cuda_to_cuda = copy_cuda_to_cuda,
  57. #ifdef NO_STRIDE
  58. .cuda_to_cuda_async = copy_cuda_to_cuda_async,
  59. #endif
  60. #else
  61. #ifdef STARPU_SIMGRID
  62. #ifdef NO_STRIDE
  63. /* Enable GPU-GPU transfers in simgrid */
  64. .cuda_to_cuda_async = 1,
  65. #endif
  66. #endif
  67. #endif
  68. #ifdef STARPU_USE_OPENCL
  69. .ram_to_opencl = copy_ram_to_opencl,
  70. .opencl_to_ram = copy_opencl_to_ram,
  71. .opencl_to_opencl = copy_opencl_to_opencl,
  72. .ram_to_opencl_async = copy_ram_to_opencl_async,
  73. .opencl_to_ram_async = copy_opencl_to_ram_async,
  74. .opencl_to_opencl_async = copy_opencl_to_opencl_async,
  75. #endif
  76. .any_to_any = copy_any_to_any,
  77. };
  78. static void register_matrix_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  79. static void *matrix_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  80. static starpu_ssize_t allocate_matrix_buffer_on_node(void *data_interface_, unsigned dst_node);
  81. static void free_matrix_buffer_on_node(void *data_interface, unsigned node);
  82. static size_t matrix_interface_get_size(starpu_data_handle_t handle);
  83. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle);
  84. static int matrix_compare(void *data_interface_a, void *data_interface_b);
  85. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f);
  86. static int pack_matrix_handle(starpu_data_handle_t handle, unsigned node, void **ptr, ssize_t *count);
  87. static int unpack_matrix_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  88. struct starpu_data_interface_ops starpu_interface_matrix_ops =
  89. {
  90. .register_data_handle = register_matrix_handle,
  91. .allocate_data_on_node = allocate_matrix_buffer_on_node,
  92. .handle_to_pointer = matrix_handle_to_pointer,
  93. .free_data_on_node = free_matrix_buffer_on_node,
  94. .copy_methods = &matrix_copy_data_methods_s,
  95. .get_size = matrix_interface_get_size,
  96. .footprint = footprint_matrix_interface_crc32,
  97. .compare = matrix_compare,
  98. .interfaceid = STARPU_MATRIX_INTERFACE_ID,
  99. .interface_size = sizeof(struct starpu_matrix_interface),
  100. .display = display_matrix_interface,
  101. .pack_data = pack_matrix_handle,
  102. .unpack_data = unpack_matrix_handle
  103. };
  104. static void register_matrix_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  105. {
  106. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  107. unsigned node;
  108. for (node = 0; node < STARPU_MAXNODES; node++)
  109. {
  110. struct starpu_matrix_interface *local_interface = (struct starpu_matrix_interface *)
  111. starpu_data_get_interface_on_node(handle, node);
  112. if (node == home_node)
  113. {
  114. local_interface->ptr = matrix_interface->ptr;
  115. local_interface->dev_handle = matrix_interface->dev_handle;
  116. local_interface->offset = matrix_interface->offset;
  117. local_interface->ld = matrix_interface->ld;
  118. }
  119. else
  120. {
  121. local_interface->ptr = 0;
  122. local_interface->dev_handle = 0;
  123. local_interface->offset = 0;
  124. local_interface->ld = 0;
  125. }
  126. local_interface->id = matrix_interface->id;
  127. local_interface->nx = matrix_interface->nx;
  128. local_interface->ny = matrix_interface->ny;
  129. local_interface->elemsize = matrix_interface->elemsize;
  130. }
  131. }
  132. static void *matrix_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  133. {
  134. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  135. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  136. starpu_data_get_interface_on_node(handle, node);
  137. return (void*) matrix_interface->ptr;
  138. }
  139. /* declare a new data with the matrix interface */
  140. void starpu_matrix_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  141. uintptr_t ptr, uint32_t ld, uint32_t nx,
  142. uint32_t ny, size_t elemsize)
  143. {
  144. struct starpu_matrix_interface matrix_interface =
  145. {
  146. .id = STARPU_MATRIX_INTERFACE_ID,
  147. .ptr = ptr,
  148. .ld = ld,
  149. .nx = nx,
  150. .ny = ny,
  151. .elemsize = elemsize,
  152. .dev_handle = ptr,
  153. .offset = 0
  154. };
  155. #ifdef STARPU_USE_SCC
  156. _starpu_scc_set_offset_in_shared_memory((void*)matrix_interface.ptr,
  157. (void**)&(matrix_interface.dev_handle), &(matrix_interface.offset));
  158. #endif
  159. starpu_data_register(handleptr, home_node, &matrix_interface, &starpu_interface_matrix_ops);
  160. }
  161. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle)
  162. {
  163. return starpu_hash_crc32c_be(starpu_matrix_get_nx(handle), starpu_matrix_get_ny(handle));
  164. }
  165. static int matrix_compare(void *data_interface_a, void *data_interface_b)
  166. {
  167. struct starpu_matrix_interface *matrix_a = (struct starpu_matrix_interface *) data_interface_a;
  168. struct starpu_matrix_interface *matrix_b = (struct starpu_matrix_interface *) data_interface_b;
  169. /* Two matricess are considered compatible if they have the same size */
  170. return ((matrix_a->nx == matrix_b->nx)
  171. && (matrix_a->ny == matrix_b->ny)
  172. && (matrix_a->elemsize == matrix_b->elemsize));
  173. }
  174. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f)
  175. {
  176. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  177. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  178. fprintf(f, "%u\t%u\t", matrix_interface->nx, matrix_interface->ny);
  179. }
  180. static int pack_matrix_handle(starpu_data_handle_t handle, unsigned node, void **ptr, ssize_t *count)
  181. {
  182. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  183. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  184. starpu_data_get_interface_on_node(handle, node);
  185. *count = matrix_interface->nx*matrix_interface->ny*matrix_interface->elemsize;
  186. if (ptr != NULL)
  187. {
  188. uint32_t y;
  189. void *matrix = (void *)matrix_interface->ptr;
  190. *ptr = malloc(*count);
  191. void *cur = *ptr;
  192. for(y=0 ; y<matrix_interface->ny ; y++)
  193. {
  194. memcpy(cur, matrix, matrix_interface->nx*matrix_interface->elemsize);
  195. cur += matrix_interface->nx*matrix_interface->elemsize;
  196. matrix += matrix_interface->ld * matrix_interface->elemsize;
  197. }
  198. }
  199. return 0;
  200. }
  201. static int unpack_matrix_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  202. {
  203. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  204. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  205. starpu_data_get_interface_on_node(handle, node);
  206. STARPU_ASSERT(count == matrix_interface->elemsize * matrix_interface->nx * matrix_interface->ny);
  207. uint32_t y;
  208. void *cur = ptr;
  209. void *matrix = (void *)matrix_interface->ptr;
  210. for(y=0 ; y<matrix_interface->ny ; y++)
  211. {
  212. memcpy(matrix, cur, matrix_interface->nx*matrix_interface->elemsize);
  213. cur += matrix_interface->nx*matrix_interface->elemsize;
  214. matrix += matrix_interface->ld * matrix_interface->elemsize;
  215. }
  216. return 0;
  217. }
  218. static size_t matrix_interface_get_size(starpu_data_handle_t handle)
  219. {
  220. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  221. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  222. size_t size;
  223. size = (size_t)matrix_interface->nx*matrix_interface->ny*matrix_interface->elemsize;
  224. return size;
  225. }
  226. /* offer an access to the data parameters */
  227. uint32_t starpu_matrix_get_nx(starpu_data_handle_t handle)
  228. {
  229. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  230. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  231. return matrix_interface->nx;
  232. }
  233. uint32_t starpu_matrix_get_ny(starpu_data_handle_t handle)
  234. {
  235. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  236. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  237. return matrix_interface->ny;
  238. }
  239. uint32_t starpu_matrix_get_local_ld(starpu_data_handle_t handle)
  240. {
  241. unsigned node;
  242. node = _starpu_memory_node_get_local_key();
  243. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  244. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  245. starpu_data_get_interface_on_node(handle, node);
  246. return matrix_interface->ld;
  247. }
  248. uintptr_t starpu_matrix_get_local_ptr(starpu_data_handle_t handle)
  249. {
  250. unsigned node;
  251. node = _starpu_memory_node_get_local_key();
  252. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  253. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  254. starpu_data_get_interface_on_node(handle, node);
  255. return matrix_interface->ptr;
  256. }
  257. size_t starpu_matrix_get_elemsize(starpu_data_handle_t handle)
  258. {
  259. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  260. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  261. return matrix_interface->elemsize;
  262. }
  263. /* memory allocation/deallocation primitives for the matrix interface */
  264. /* returns the size of the allocated area */
  265. static starpu_ssize_t allocate_matrix_buffer_on_node(void *data_interface_, unsigned dst_node)
  266. {
  267. uintptr_t addr = 0, handle;
  268. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface_;
  269. uint32_t nx = matrix_interface->nx;
  270. uint32_t ny = matrix_interface->ny;
  271. uint32_t ld = nx; // by default
  272. size_t elemsize = matrix_interface->elemsize;
  273. starpu_ssize_t allocated_memory;
  274. handle = starpu_malloc_on_node(dst_node, nx*ny*elemsize);
  275. if (!handle)
  276. return -ENOMEM;
  277. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  278. addr = handle;
  279. allocated_memory = (size_t)nx*ny*elemsize;
  280. /* update the data properly in consequence */
  281. matrix_interface->ptr = addr;
  282. matrix_interface->dev_handle = handle;
  283. matrix_interface->offset = 0;
  284. matrix_interface->ld = ld;
  285. return allocated_memory;
  286. }
  287. static void free_matrix_buffer_on_node(void *data_interface, unsigned node)
  288. {
  289. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  290. uint32_t nx = matrix_interface->nx;
  291. uint32_t ny = matrix_interface->ny;
  292. size_t elemsize = matrix_interface->elemsize;
  293. starpu_free_on_node(node, matrix_interface->dev_handle, nx*ny*elemsize);
  294. }
  295. #ifdef STARPU_USE_CUDA
  296. 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, int is_async, cudaStream_t stream)
  297. {
  298. struct starpu_matrix_interface *src_matrix = src_interface;
  299. struct starpu_matrix_interface *dst_matrix = dst_interface;
  300. size_t elemsize = src_matrix->elemsize;
  301. cudaError_t cures;
  302. #if 0
  303. struct cudaMemcpy3DParms p;
  304. memset(&p, 0, sizeof(p));
  305. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->ld * elemsize, src_matrix->ny);
  306. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->ld * elemsize, dst_matrix->ny);
  307. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  308. p.kind = kind;
  309. if (is_async)
  310. {
  311. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  312. cures = cudaMemcpy3DAsync(&p, stream);
  313. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  314. if (!cures)
  315. return -EAGAIN;
  316. }
  317. cures = cudaMemcpy3D(&p);
  318. if (STARPU_UNLIKELY(cures))
  319. STARPU_CUDA_REPORT_ERROR(cures);
  320. #else
  321. if (is_async)
  322. {
  323. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  324. cures = cudaMemcpy2DAsync((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  325. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  326. src_matrix->nx*elemsize, src_matrix->ny, kind, stream);
  327. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  328. if (!cures)
  329. return -EAGAIN;
  330. }
  331. cures = cudaMemcpy2D((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  332. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  333. src_matrix->nx*elemsize, src_matrix->ny, kind);
  334. if (STARPU_UNLIKELY(cures))
  335. {
  336. int ret = copy_any_to_any(src_interface, src_node, dst_interface, dst_node, (void*)(uintptr_t)is_async);
  337. if (ret == -EAGAIN) return ret;
  338. if (ret) STARPU_CUDA_REPORT_ERROR(cures);
  339. }
  340. #endif
  341. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  342. return 0;
  343. }
  344. /* XXX this is broken : We need to properly call cudaDeviceEnablePeerAccess(), and avoid crossing NUMA nodes... */
  345. #ifdef NO_STRIDE
  346. static int copy_cuda_peer(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, int is_async, cudaStream_t stream)
  347. {
  348. struct starpu_matrix_interface *src_matrix = src_interface;
  349. struct starpu_matrix_interface *dst_matrix = dst_interface;
  350. size_t elemsize = src_matrix->elemsize;
  351. cudaError_t cures;
  352. int src_dev = _starpu_memory_node_get_devid(src_node);
  353. int dst_dev = _starpu_memory_node_get_devid(dst_node);
  354. #if 0
  355. /* That code is not even working!! */
  356. struct cudaExtent extent = make_cudaExtent(128, 128, 128);
  357. starpu_cuda_set_device(src_dev);
  358. struct cudaPitchedPtr mem_device1;
  359. cures = cudaMalloc3D(&mem_device1, extent);
  360. if (STARPU_UNLIKELY(cures))
  361. STARPU_CUDA_REPORT_ERROR(cures);
  362. starpu_cuda_set_device(dst_dev);
  363. struct cudaPitchedPtr mem_device2;
  364. cures = cudaMalloc3D(&mem_device2, extent);
  365. if (STARPU_UNLIKELY(cures))
  366. STARPU_CUDA_REPORT_ERROR(cures);
  367. struct cudaMemcpy3DPeerParms p;
  368. memset(&p, 0, sizeof(p));
  369. p.srcDevice = src_dev;
  370. p.dstDevice = dst_dev;
  371. p.srcPtr = mem_device1;
  372. p.dstPtr = mem_device2;
  373. p.extent = extent;
  374. fprintf(stderr,"%u %u\n", p.srcDevice, p.dstDevice);
  375. fprintf(stderr,"%p %p\n", p.srcArray, p.dstArray);
  376. fprintf(stderr,"%p %lu %lu %lu\n", p.srcPtr.ptr, p.srcPtr.pitch, p.srcPtr.xsize, p.srcPtr.ysize);
  377. fprintf(stderr,"%p %lu %lu %lu\n", p.dstPtr.ptr, p.dstPtr.pitch, p.dstPtr.xsize, p.dstPtr.ysize);
  378. fprintf(stderr,"%lu %lu %lu\n", p.srcPos.x, p.srcPos.y, p.srcPos.z);
  379. fprintf(stderr,"%lu %lu %lu\n", p.dstPos.x, p.dstPos.y, p.dstPos.z);
  380. fprintf(stderr,"%lu %lu %lu\n", p.extent.width, p.extent.height, p.extent.depth);
  381. cures = cudaMemcpy3DPeer(&p);
  382. if (STARPU_UNLIKELY(cures))
  383. STARPU_CUDA_REPORT_ERROR(cures);
  384. #endif
  385. #if 0
  386. struct cudaMemcpy3DPeerParms p;
  387. memset(&p, 0, sizeof(p));
  388. p.srcDevice = src_dev;
  389. p.dstDevice = dst_dev;
  390. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->nx * elemsize, src_matrix->ny);
  391. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->nx * elemsize, dst_matrix->ny);
  392. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  393. #if 1
  394. fprintf(stderr,"%u %u\n", p.srcDevice, p.dstDevice);
  395. fprintf(stderr,"%p %p\n", p.srcArray, p.dstArray);
  396. fprintf(stderr,"%p %lu %lu %lu\n", p.srcPtr.ptr, p.srcPtr.pitch, p.srcPtr.xsize, p.srcPtr.ysize);
  397. fprintf(stderr,"%p %lu %lu %lu\n", p.dstPtr.ptr, p.dstPtr.pitch, p.dstPtr.xsize, p.dstPtr.ysize);
  398. fprintf(stderr,"%lu %lu %lu\n", p.srcPos.x, p.srcPos.y, p.srcPos.z);
  399. fprintf(stderr,"%lu %lu %lu\n", p.dstPos.x, p.dstPos.y, p.dstPos.z);
  400. fprintf(stderr,"%lu %lu %lu\n", p.extent.width, p.extent.height, p.extent.depth);
  401. #endif
  402. cures = cudaMemcpy3DPeerAsync(&p, stream);
  403. if (STARPU_UNLIKELY(cures))
  404. STARPU_CUDA_REPORT_ERROR(cures);
  405. cudaStreamSynchronize(stream);
  406. if (is_async)
  407. {
  408. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  409. cures = cudaMemcpy3DPeerAsync(&p, stream);
  410. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  411. if (!cures)
  412. return -EAGAIN;
  413. }
  414. cures = cudaMemcpy3DPeer(&p);
  415. if (STARPU_UNLIKELY(cures))
  416. STARPU_CUDA_REPORT_ERROR(cures);
  417. #else
  418. /* XXX FIXME !!*/
  419. STARPU_ASSERT(src_matrix->nx == src_matrix->ld);
  420. STARPU_ASSERT(dst_matrix->nx == dst_matrix->ld);
  421. if (is_async)
  422. {
  423. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  424. cures = cudaMemcpyPeerAsync((char *)dst_matrix->ptr, dst_dev, (char *)src_matrix->ptr, src_dev, dst_matrix->nx*dst_matrix->ny*elemsize, stream);
  425. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  426. if (!cures)
  427. return -EAGAIN;
  428. }
  429. cures = cudaMemcpyPeer((char *)dst_matrix->ptr, dst_dev, (char *)src_matrix->ptr, src_dev, dst_matrix->nx*dst_matrix->ny*elemsize);
  430. if (STARPU_UNLIKELY(cures))
  431. STARPU_CUDA_REPORT_ERROR(cures);
  432. #endif
  433. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  434. return 0;
  435. }
  436. #endif
  437. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  438. {
  439. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 0, 0);
  440. }
  441. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  442. {
  443. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 0, 0);
  444. }
  445. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  446. {
  447. if (src_node == dst_node)
  448. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 0, 0);
  449. else
  450. {
  451. /* XXX not implemented */
  452. STARPU_ABORT();
  453. return 0;
  454. }
  455. }
  456. 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)
  457. {
  458. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 1, stream);
  459. }
  460. 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)
  461. {
  462. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 1, stream);
  463. }
  464. #ifdef NO_STRIDE
  465. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream)
  466. {
  467. if (src_node == dst_node)
  468. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 1, stream);
  469. else
  470. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node, 1, stream);
  471. }
  472. #endif
  473. #endif // STARPU_USE_CUDA
  474. #ifdef STARPU_USE_OPENCL
  475. static int copy_opencl_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  476. {
  477. struct starpu_matrix_interface *src_matrix = src_interface;
  478. struct starpu_matrix_interface *dst_matrix = dst_interface;
  479. int ret;
  480. STARPU_ASSERT_MSG((src_matrix->ld == src_matrix->nx) && (dst_matrix->ld == dst_matrix->nx), "XXX non contiguous buffers are not properly supported in OpenCL yet. (TODO)");
  481. ret = starpu_opencl_copy_async_sync(src_matrix->dev_handle, src_matrix->offset, src_node,
  482. dst_matrix->dev_handle, dst_matrix->offset, dst_node,
  483. src_matrix->nx*src_matrix->ny*src_matrix->elemsize,
  484. event);
  485. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  486. return ret;
  487. }
  488. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  489. {
  490. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  491. }
  492. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  493. {
  494. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  495. }
  496. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  497. {
  498. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  499. }
  500. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  501. {
  502. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  503. }
  504. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  505. {
  506. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  507. }
  508. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  509. {
  510. return copy_opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  511. }
  512. #endif
  513. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  514. {
  515. struct starpu_matrix_interface *src_matrix = (struct starpu_matrix_interface *) src_interface;
  516. struct starpu_matrix_interface *dst_matrix = (struct starpu_matrix_interface *) dst_interface;
  517. int ret = 0;
  518. unsigned y;
  519. uint32_t nx = dst_matrix->nx;
  520. uint32_t ny = dst_matrix->ny;
  521. size_t elemsize = dst_matrix->elemsize;
  522. uint32_t ld_src = src_matrix->ld;
  523. uint32_t ld_dst = dst_matrix->ld;
  524. if (ld_src == nx && ld_dst == nx)
  525. {
  526. /* Optimize unpartitioned and y-partitioned cases */
  527. if (starpu_interface_copy(src_matrix->dev_handle, src_matrix->offset, src_node,
  528. dst_matrix->dev_handle, dst_matrix->offset, dst_node,
  529. nx*ny*elemsize, async_data))
  530. ret = -EAGAIN;
  531. }
  532. else
  533. {
  534. for (y = 0; y < ny; y++)
  535. {
  536. uint32_t src_offset = y*ld_src*elemsize;
  537. uint32_t dst_offset = y*ld_dst*elemsize;
  538. if (starpu_interface_copy(src_matrix->dev_handle, src_matrix->offset + src_offset, src_node,
  539. dst_matrix->dev_handle, dst_matrix->offset + dst_offset, dst_node,
  540. nx*elemsize, async_data))
  541. ret = -EAGAIN;
  542. }
  543. }
  544. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)nx*ny*elemsize);
  545. return ret;
  546. }