matrix_interface.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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. /* If you can promise that there is no stride in your matrices, you can define this */
  27. // #define NO_STRIDE
  28. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  29. #ifdef STARPU_USE_CUDA
  30. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  31. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  32. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  33. 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);
  34. 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);
  35. #ifdef NO_STRIDE
  36. 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);
  37. #endif
  38. #endif
  39. #ifdef STARPU_USE_OPENCL
  40. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  41. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  42. 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, void *_event);
  43. 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, void *_event);
  44. #endif
  45. static struct starpu_data_copy_methods matrix_copy_data_methods_s =
  46. {
  47. .ram_to_ram = copy_ram_to_ram,
  48. .ram_to_spu = NULL,
  49. #ifdef STARPU_USE_CUDA
  50. .ram_to_cuda = copy_ram_to_cuda,
  51. .cuda_to_ram = copy_cuda_to_ram,
  52. .ram_to_cuda_async = copy_ram_to_cuda_async,
  53. .cuda_to_ram_async = copy_cuda_to_ram_async,
  54. .cuda_to_cuda = copy_cuda_to_cuda,
  55. #ifdef NO_STRIDE
  56. .cuda_to_cuda_async = copy_cuda_to_cuda_async,
  57. #endif
  58. #endif
  59. #ifdef STARPU_USE_OPENCL
  60. .ram_to_opencl = copy_ram_to_opencl,
  61. .opencl_to_ram = copy_opencl_to_ram,
  62. .ram_to_opencl_async = copy_ram_to_opencl_async,
  63. .opencl_to_ram_async = copy_opencl_to_ram_async,
  64. #endif
  65. .cuda_to_spu = NULL,
  66. .spu_to_ram = NULL,
  67. .spu_to_cuda = NULL,
  68. .spu_to_spu = NULL
  69. };
  70. static void register_matrix_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface);
  71. static void *matrix_handle_to_pointer(starpu_data_handle_t data_handle, uint32_t node);
  72. static ssize_t allocate_matrix_buffer_on_node(void *data_interface_, uint32_t dst_node);
  73. static void free_matrix_buffer_on_node(void *data_interface, uint32_t node);
  74. static size_t matrix_interface_get_size(starpu_data_handle_t handle);
  75. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle);
  76. static int matrix_compare(void *data_interface_a, void *data_interface_b);
  77. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f);
  78. #ifdef STARPU_USE_GORDON
  79. static int convert_matrix_to_gordon(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss);
  80. #endif
  81. struct starpu_data_interface_ops _starpu_interface_matrix_ops =
  82. {
  83. .register_data_handle = register_matrix_handle,
  84. .allocate_data_on_node = allocate_matrix_buffer_on_node,
  85. .handle_to_pointer = matrix_handle_to_pointer,
  86. .free_data_on_node = free_matrix_buffer_on_node,
  87. .copy_methods = &matrix_copy_data_methods_s,
  88. .get_size = matrix_interface_get_size,
  89. .footprint = footprint_matrix_interface_crc32,
  90. .compare = matrix_compare,
  91. #ifdef STARPU_USE_GORDON
  92. .convert_to_gordon = convert_matrix_to_gordon,
  93. #endif
  94. .interfaceid = STARPU_MATRIX_INTERFACE_ID,
  95. .interface_size = sizeof(struct starpu_matrix_interface),
  96. .display = display_matrix_interface,
  97. };
  98. #ifdef STARPU_USE_GORDON
  99. static int convert_matrix_to_gordon(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss)
  100. {
  101. size_t elemsize = GET_MATRIX_ELEMSIZE(interface);
  102. uint32_t nx = STARPU_MATRIX_GET_NX(interface);
  103. uint32_t ny = STARPU_MATRIX_GET_NY(interface);
  104. uint32_t ld = STARPU_MATRIX_GET_LD(interface);
  105. *ptr = STARPU_MATRIX_GET_PTR(interface);
  106. /* The gordon_stride_init function may use a contiguous buffer
  107. * in case nx = ld (in that case, (*ss).size = elemsize*nx*ny */
  108. *ss = gordon_stride_init(ny, nx*elemsize, ld*elemsize);
  109. return 0;
  110. }
  111. #endif
  112. static void register_matrix_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface)
  113. {
  114. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  115. unsigned node;
  116. for (node = 0; node < STARPU_MAXNODES; node++)
  117. {
  118. struct starpu_matrix_interface *local_interface = (struct starpu_matrix_interface *)
  119. starpu_data_get_interface_on_node(handle, node);
  120. if (node == home_node)
  121. {
  122. local_interface->ptr = matrix_interface->ptr;
  123. local_interface->dev_handle = matrix_interface->dev_handle;
  124. local_interface->offset = matrix_interface->offset;
  125. local_interface->ld = matrix_interface->ld;
  126. }
  127. else
  128. {
  129. local_interface->ptr = 0;
  130. local_interface->dev_handle = 0;
  131. local_interface->offset = 0;
  132. local_interface->ld = 0;
  133. }
  134. local_interface->nx = matrix_interface->nx;
  135. local_interface->ny = matrix_interface->ny;
  136. local_interface->elemsize = matrix_interface->elemsize;
  137. }
  138. }
  139. static void *matrix_handle_to_pointer(starpu_data_handle_t handle, uint32_t node)
  140. {
  141. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  142. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  143. starpu_data_get_interface_on_node(handle, node);
  144. return (void*) matrix_interface->ptr;
  145. }
  146. /* declare a new data with the matrix interface */
  147. void starpu_matrix_data_register(starpu_data_handle_t *handleptr, uint32_t home_node,
  148. uintptr_t ptr, uint32_t ld, uint32_t nx,
  149. uint32_t ny, size_t elemsize)
  150. {
  151. struct starpu_matrix_interface matrix_interface =
  152. {
  153. .ptr = ptr,
  154. .ld = ld,
  155. .nx = nx,
  156. .ny = ny,
  157. .elemsize = elemsize,
  158. .dev_handle = ptr,
  159. .offset = 0
  160. };
  161. starpu_data_register(handleptr, home_node, &matrix_interface, &_starpu_interface_matrix_ops);
  162. }
  163. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle)
  164. {
  165. return starpu_crc32_be(starpu_matrix_get_nx(handle), starpu_matrix_get_ny(handle));
  166. }
  167. static int matrix_compare(void *data_interface_a, void *data_interface_b)
  168. {
  169. struct starpu_matrix_interface *matrix_a = (struct starpu_matrix_interface *) data_interface_a;
  170. struct starpu_matrix_interface *matrix_b = (struct starpu_matrix_interface *) data_interface_b;
  171. /* Two matricess are considered compatible if they have the same size */
  172. return ((matrix_a->nx == matrix_b->nx)
  173. && (matrix_a->ny == matrix_b->ny)
  174. && (matrix_a->elemsize == matrix_b->elemsize));
  175. }
  176. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f)
  177. {
  178. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  179. starpu_data_get_interface_on_node(handle, 0);
  180. fprintf(f, "%u\t%u\t", matrix_interface->nx, matrix_interface->ny);
  181. }
  182. static size_t matrix_interface_get_size(starpu_data_handle_t handle)
  183. {
  184. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  185. starpu_data_get_interface_on_node(handle, 0);
  186. size_t size;
  187. size = (size_t)matrix_interface->nx*matrix_interface->ny*matrix_interface->elemsize;
  188. return size;
  189. }
  190. /* offer an access to the data parameters */
  191. uint32_t starpu_matrix_get_nx(starpu_data_handle_t handle)
  192. {
  193. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  194. starpu_data_get_interface_on_node(handle, 0);
  195. return matrix_interface->nx;
  196. }
  197. uint32_t starpu_matrix_get_ny(starpu_data_handle_t handle)
  198. {
  199. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  200. starpu_data_get_interface_on_node(handle, 0);
  201. return matrix_interface->ny;
  202. }
  203. uint32_t starpu_matrix_get_local_ld(starpu_data_handle_t handle)
  204. {
  205. unsigned node;
  206. node = _starpu_get_local_memory_node();
  207. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  208. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  209. starpu_data_get_interface_on_node(handle, node);
  210. return matrix_interface->ld;
  211. }
  212. uintptr_t starpu_matrix_get_local_ptr(starpu_data_handle_t handle)
  213. {
  214. unsigned node;
  215. node = _starpu_get_local_memory_node();
  216. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  217. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  218. starpu_data_get_interface_on_node(handle, node);
  219. return matrix_interface->ptr;
  220. }
  221. size_t starpu_matrix_get_elemsize(starpu_data_handle_t handle)
  222. {
  223. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  224. starpu_data_get_interface_on_node(handle, 0);
  225. return matrix_interface->elemsize;
  226. }
  227. /* memory allocation/deallocation primitives for the matrix interface */
  228. /* returns the size of the allocated area */
  229. static ssize_t allocate_matrix_buffer_on_node(void *data_interface_, uint32_t dst_node)
  230. {
  231. uintptr_t addr = 0, handle = 0;
  232. unsigned fail = 0;
  233. ssize_t allocated_memory;
  234. #ifdef STARPU_USE_CUDA
  235. cudaError_t status;
  236. #endif
  237. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface_;
  238. uint32_t nx = matrix_interface->nx;
  239. uint32_t ny = matrix_interface->ny;
  240. uint32_t ld = nx; // by default
  241. size_t elemsize = matrix_interface->elemsize;
  242. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  243. switch(kind)
  244. {
  245. case STARPU_CPU_RAM:
  246. handle = addr = (uintptr_t)malloc((size_t)nx*ny*elemsize);
  247. if (!addr)
  248. fail = 1;
  249. break;
  250. #ifdef STARPU_USE_CUDA
  251. case STARPU_CUDA_RAM:
  252. status = cudaMalloc((void **)&addr, (size_t)nx*ny*elemsize);
  253. if (!addr || status != cudaSuccess)
  254. {
  255. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  256. STARPU_CUDA_REPORT_ERROR(status);
  257. fail = 1;
  258. }
  259. handle = addr;
  260. ld = nx;
  261. break;
  262. #endif
  263. #ifdef STARPU_USE_OPENCL
  264. case STARPU_OPENCL_RAM:
  265. {
  266. int ret;
  267. cl_mem mem;
  268. ret = starpu_opencl_allocate_memory(&mem, nx*ny*elemsize, CL_MEM_READ_WRITE);
  269. handle = (uintptr_t)mem;
  270. if (ret)
  271. {
  272. fail = 1;
  273. }
  274. break;
  275. }
  276. #endif
  277. default:
  278. STARPU_ABORT();
  279. }
  280. if (!fail)
  281. {
  282. /* allocation succeeded */
  283. allocated_memory = (size_t)nx*ny*elemsize;
  284. /* update the data properly in consequence */
  285. matrix_interface->ptr = addr;
  286. matrix_interface->dev_handle = handle;
  287. matrix_interface->offset = 0;
  288. matrix_interface->ld = ld;
  289. }
  290. else
  291. {
  292. /* allocation failed */
  293. allocated_memory = -ENOMEM;
  294. }
  295. return allocated_memory;
  296. }
  297. static void free_matrix_buffer_on_node(void *data_interface, uint32_t node)
  298. {
  299. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  300. #ifdef STARPU_USE_CUDA
  301. cudaError_t status;
  302. #endif
  303. enum starpu_node_kind kind = starpu_node_get_kind(node);
  304. switch(kind)
  305. {
  306. case STARPU_CPU_RAM:
  307. free((void*)matrix_interface->ptr);
  308. break;
  309. #ifdef STARPU_USE_CUDA
  310. case STARPU_CUDA_RAM:
  311. status = cudaFree((void*)matrix_interface->ptr);
  312. if (STARPU_UNLIKELY(status))
  313. STARPU_CUDA_REPORT_ERROR(status);
  314. break;
  315. #endif
  316. #ifdef STARPU_USE_OPENCL
  317. case STARPU_OPENCL_RAM:
  318. clReleaseMemObject((void *)matrix_interface->dev_handle);
  319. break;
  320. #endif
  321. default:
  322. STARPU_ABORT();
  323. }
  324. }
  325. #ifdef STARPU_USE_CUDA
  326. 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)
  327. {
  328. struct starpu_matrix_interface *src_matrix = src_interface;
  329. struct starpu_matrix_interface *dst_matrix = dst_interface;
  330. size_t elemsize = src_matrix->elemsize;
  331. cudaError_t cures;
  332. #if 0
  333. struct cudaMemcpy3DParms p;
  334. memset(&p, 0, sizeof(p));
  335. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->ld * elemsize, src_matrix->ny);
  336. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->ld * elemsize, dst_matrix->ny);
  337. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  338. p.kind = kind;
  339. if (is_async)
  340. {
  341. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  342. cures = cudaMemcpy3DAsync(&p, stream);
  343. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  344. if (!cures)
  345. return -EAGAIN;
  346. }
  347. cures = cudaMemcpy3D(&p);
  348. if (STARPU_UNLIKELY(cures))
  349. STARPU_CUDA_REPORT_ERROR(cures);
  350. #else
  351. if (is_async)
  352. {
  353. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  354. cures = cudaMemcpy2DAsync((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  355. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  356. src_matrix->nx*elemsize, src_matrix->ny, kind, stream);
  357. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  358. if (!cures)
  359. return -EAGAIN;
  360. }
  361. cures = cudaMemcpy2D((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  362. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  363. src_matrix->nx*elemsize, src_matrix->ny, kind);
  364. if (STARPU_UNLIKELY(cures))
  365. STARPU_CUDA_REPORT_ERROR(cures);
  366. #endif
  367. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  368. return 0;
  369. }
  370. /* XXX this is broken : We need to properly call cudaDeviceEnablePeerAccess(), and avoid crossing NUMA nodes... */
  371. #ifdef NO_STRIDE
  372. 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)
  373. {
  374. struct starpu_matrix_interface *src_matrix = src_interface;
  375. struct starpu_matrix_interface *dst_matrix = dst_interface;
  376. size_t elemsize = src_matrix->elemsize;
  377. cudaError_t cures;
  378. int src_dev = _starpu_memory_node_to_devid(src_node);
  379. int dst_dev = _starpu_memory_node_to_devid(dst_node);
  380. #if 0
  381. /* That code is not even working!! */
  382. struct cudaExtent extent = make_cudaExtent(128, 128, 128);
  383. cures = cudaSetDevice(src_dev);
  384. STARPU_ASSERT(cures == cudaSuccess);
  385. struct cudaPitchedPtr mem_device1;
  386. cures = cudaMalloc3D(&mem_device1, extent);
  387. if (STARPU_UNLIKELY(cures))
  388. STARPU_CUDA_REPORT_ERROR(cures);
  389. cures = cudaSetDevice(dst_dev);
  390. STARPU_ASSERT(cures == cudaSuccess);
  391. struct cudaPitchedPtr mem_device2;
  392. cures = cudaMalloc3D(&mem_device2, extent);
  393. if (STARPU_UNLIKELY(cures))
  394. STARPU_CUDA_REPORT_ERROR(cures);
  395. struct cudaMemcpy3DPeerParms p;
  396. memset(&p, 0, sizeof(p));
  397. p.srcDevice = src_dev;
  398. p.dstDevice = dst_dev;
  399. p.srcPtr = mem_device1;
  400. p.dstPtr = mem_device2;
  401. p.extent = extent;
  402. fprintf(stderr,"%u %u\n", p.srcDevice, p.dstDevice);
  403. fprintf(stderr,"%p %p\n", p.srcArray, p.dstArray);
  404. fprintf(stderr,"%p %lu %lu %lu\n", p.srcPtr.ptr, p.srcPtr.pitch, p.srcPtr.xsize, p.srcPtr.ysize);
  405. fprintf(stderr,"%p %lu %lu %lu\n", p.dstPtr.ptr, p.dstPtr.pitch, p.dstPtr.xsize, p.dstPtr.ysize);
  406. fprintf(stderr,"%lu %lu %lu\n", p.srcPos.x, p.srcPos.y, p.srcPos.z);
  407. fprintf(stderr,"%lu %lu %lu\n", p.dstPos.x, p.dstPos.y, p.dstPos.z);
  408. fprintf(stderr,"%lu %lu %lu\n", p.extent.width, p.extent.height, p.extent.depth);
  409. cures = cudaMemcpy3DPeer(&p);
  410. if (STARPU_UNLIKELY(cures))
  411. STARPU_CUDA_REPORT_ERROR(cures);
  412. #endif
  413. #if 0
  414. struct cudaMemcpy3DPeerParms p;
  415. memset(&p, 0, sizeof(p));
  416. p.srcDevice = src_dev;
  417. p.dstDevice = dst_dev;
  418. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->nx * elemsize, src_matrix->ny);
  419. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->nx * elemsize, dst_matrix->ny);
  420. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  421. #if 1
  422. fprintf(stderr,"%u %u\n", p.srcDevice, p.dstDevice);
  423. fprintf(stderr,"%p %p\n", p.srcArray, p.dstArray);
  424. fprintf(stderr,"%p %lu %lu %lu\n", p.srcPtr.ptr, p.srcPtr.pitch, p.srcPtr.xsize, p.srcPtr.ysize);
  425. fprintf(stderr,"%p %lu %lu %lu\n", p.dstPtr.ptr, p.dstPtr.pitch, p.dstPtr.xsize, p.dstPtr.ysize);
  426. fprintf(stderr,"%lu %lu %lu\n", p.srcPos.x, p.srcPos.y, p.srcPos.z);
  427. fprintf(stderr,"%lu %lu %lu\n", p.dstPos.x, p.dstPos.y, p.dstPos.z);
  428. fprintf(stderr,"%lu %lu %lu\n", p.extent.width, p.extent.height, p.extent.depth);
  429. #endif
  430. cures = cudaMemcpy3DPeerAsync(&p, stream);
  431. if (STARPU_UNLIKELY(cures))
  432. STARPU_CUDA_REPORT_ERROR(cures);
  433. cudaThreadSynchronize();
  434. if (is_async)
  435. {
  436. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  437. cures = cudaMemcpy3DPeerAsync(&p, stream);
  438. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  439. if (!cures)
  440. return -EAGAIN;
  441. }
  442. cures = cudaMemcpy3DPeer(&p);
  443. if (STARPU_UNLIKELY(cures))
  444. STARPU_CUDA_REPORT_ERROR(cures);
  445. #else
  446. /* XXX FIXME !!*/
  447. STARPU_ASSERT(src_matrix->nx == src_matrix->ld);
  448. STARPU_ASSERT(dst_matrix->nx == dst_matrix->ld);
  449. if (is_async)
  450. {
  451. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  452. cures = cudaMemcpyPeerAsync((char *)dst_matrix->ptr, dst_dev, (char *)src_matrix->ptr, src_dev, dst_matrix->nx*dst_matrix->ny*elemsize, stream);
  453. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  454. if (!cures)
  455. return -EAGAIN;
  456. }
  457. cures = cudaMemcpyPeer((char *)dst_matrix->ptr, dst_dev, (char *)src_matrix->ptr, src_dev, dst_matrix->nx*dst_matrix->ny*elemsize);
  458. if (STARPU_UNLIKELY(cures))
  459. STARPU_CUDA_REPORT_ERROR(cures);
  460. #endif
  461. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  462. return 0;
  463. }
  464. #endif
  465. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  466. {
  467. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 0, 0);
  468. }
  469. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  470. {
  471. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 0, 0);
  472. }
  473. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  474. {
  475. if (src_node == dst_node)
  476. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 0, 0);
  477. else
  478. {
  479. /* XXX not implemented */
  480. STARPU_ABORT();
  481. return 0;
  482. }
  483. }
  484. 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)
  485. {
  486. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 1, stream);
  487. }
  488. 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)
  489. {
  490. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 1, stream);
  491. }
  492. #ifdef NO_STRIDE
  493. 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)
  494. {
  495. if (src_node == dst_node)
  496. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 1, stream);
  497. else
  498. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node, 1, stream);
  499. }
  500. #endif
  501. #endif // STARPU_USE_CUDA
  502. #ifdef STARPU_USE_OPENCL
  503. 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, void *_event)
  504. {
  505. struct starpu_matrix_interface *src_matrix = src_interface;
  506. struct starpu_matrix_interface *dst_matrix = dst_interface;
  507. int err,ret;
  508. 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)");
  509. err = starpu_opencl_copy_ram_to_opencl_async_sync((void*)src_matrix->ptr, src_node, (cl_mem)dst_matrix->dev_handle, dst_node,
  510. src_matrix->nx*src_matrix->ny*src_matrix->elemsize,
  511. dst_matrix->offset, (cl_event*)_event, &ret);
  512. if (STARPU_UNLIKELY(err))
  513. STARPU_OPENCL_REPORT_ERROR(err);
  514. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  515. return ret;
  516. }
  517. 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, void *_event)
  518. {
  519. struct starpu_matrix_interface *src_matrix = src_interface;
  520. struct starpu_matrix_interface *dst_matrix = dst_interface;
  521. int err, ret;
  522. 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)");
  523. err = starpu_opencl_copy_opencl_to_ram_async_sync((cl_mem)src_matrix->dev_handle, src_node, (void*)dst_matrix->ptr, dst_node,
  524. src_matrix->nx*src_matrix->ny*src_matrix->elemsize,
  525. src_matrix->offset, (cl_event*)_event, &ret);
  526. if (STARPU_UNLIKELY(err))
  527. STARPU_OPENCL_REPORT_ERROR(err);
  528. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  529. return ret;
  530. }
  531. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  532. {
  533. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  534. }
  535. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  536. {
  537. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  538. }
  539. #endif
  540. /* as not all platform easily have a lib installed ... */
  541. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  542. {
  543. struct starpu_matrix_interface *src_matrix = (struct starpu_matrix_interface *) src_interface;
  544. struct starpu_matrix_interface *dst_matrix = (struct starpu_matrix_interface *) dst_interface;
  545. unsigned y;
  546. uint32_t nx = dst_matrix->nx;
  547. uint32_t ny = dst_matrix->ny;
  548. size_t elemsize = dst_matrix->elemsize;
  549. uint32_t ld_src = src_matrix->ld;
  550. uint32_t ld_dst = dst_matrix->ld;
  551. uintptr_t ptr_src = src_matrix->ptr;
  552. uintptr_t ptr_dst = dst_matrix->ptr;
  553. for (y = 0; y < ny; y++)
  554. {
  555. uint32_t src_offset = y*ld_src*elemsize;
  556. uint32_t dst_offset = y*ld_dst*elemsize;
  557. memcpy((void *)(ptr_dst + dst_offset),
  558. (void *)(ptr_src + src_offset), nx*elemsize);
  559. }
  560. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)nx*ny*elemsize);
  561. return 0;
  562. }