matrix_interface.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <common/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 const 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;
  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_get_node_kind(dst_node);
  243. switch(kind)
  244. {
  245. case STARPU_CPU_RAM:
  246. 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. ld = nx;
  260. break;
  261. #endif
  262. #ifdef STARPU_USE_OPENCL
  263. case STARPU_OPENCL_RAM:
  264. {
  265. int ret;
  266. void *ptr;
  267. ret = _starpu_opencl_allocate_memory(&ptr, nx*ny*elemsize, CL_MEM_READ_WRITE);
  268. addr = (uintptr_t)ptr;
  269. if (ret)
  270. {
  271. fail = 1;
  272. }
  273. break;
  274. }
  275. #endif
  276. default:
  277. STARPU_ASSERT(0);
  278. }
  279. if (!fail)
  280. {
  281. /* allocation succeeded */
  282. allocated_memory = (size_t)nx*ny*elemsize;
  283. /* update the data properly in consequence */
  284. matrix_interface->ptr = addr;
  285. matrix_interface->dev_handle = addr;
  286. matrix_interface->offset = 0;
  287. matrix_interface->ld = ld;
  288. }
  289. else
  290. {
  291. /* allocation failed */
  292. allocated_memory = -ENOMEM;
  293. }
  294. return allocated_memory;
  295. }
  296. static void free_matrix_buffer_on_node(void *data_interface, uint32_t node)
  297. {
  298. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  299. #ifdef STARPU_USE_CUDA
  300. cudaError_t status;
  301. #endif
  302. enum _starpu_node_kind kind = _starpu_get_node_kind(node);
  303. switch(kind)
  304. {
  305. case STARPU_CPU_RAM:
  306. free((void*)matrix_interface->ptr);
  307. break;
  308. #ifdef STARPU_USE_CUDA
  309. case STARPU_CUDA_RAM:
  310. status = cudaFree((void*)matrix_interface->ptr);
  311. if (STARPU_UNLIKELY(status))
  312. STARPU_CUDA_REPORT_ERROR(status);
  313. break;
  314. #endif
  315. #ifdef STARPU_USE_OPENCL
  316. case STARPU_OPENCL_RAM:
  317. clReleaseMemObject((void *)matrix_interface->ptr);
  318. break;
  319. #endif
  320. default:
  321. STARPU_ASSERT(0);
  322. }
  323. }
  324. #ifdef STARPU_USE_CUDA
  325. 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)
  326. {
  327. struct starpu_matrix_interface *src_matrix = src_interface;
  328. struct starpu_matrix_interface *dst_matrix = dst_interface;
  329. size_t elemsize = src_matrix->elemsize;
  330. cudaError_t cures;
  331. #if 0
  332. struct cudaMemcpy3DParms p;
  333. memset(&p, 0, sizeof(p));
  334. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->ld * elemsize, src_matrix->ny);
  335. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->ld * elemsize, dst_matrix->ny);
  336. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  337. p.kind = kind;
  338. if (is_async)
  339. {
  340. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  341. cures = cudaMemcpy3DAsync(&p, stream);
  342. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  343. if (!cures)
  344. return -EAGAIN;
  345. }
  346. cures = cudaMemcpy3D(&p);
  347. if (STARPU_UNLIKELY(cures))
  348. STARPU_CUDA_REPORT_ERROR(cures);
  349. #else
  350. if (is_async)
  351. {
  352. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  353. cures = cudaMemcpy2DAsync((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  354. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  355. src_matrix->nx*elemsize, src_matrix->ny, kind, stream);
  356. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  357. if (!cures)
  358. return -EAGAIN;
  359. }
  360. cures = cudaMemcpy2D((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  361. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  362. src_matrix->nx*elemsize, src_matrix->ny, kind);
  363. if (STARPU_UNLIKELY(cures))
  364. STARPU_CUDA_REPORT_ERROR(cures);
  365. #endif
  366. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  367. return 0;
  368. }
  369. /* XXX this is broken : We need to properly call cudaDeviceEnablePeerAccess(), and avoid crossing NUMA nodes... */
  370. #ifdef NO_STRIDE
  371. 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)
  372. {
  373. struct starpu_matrix_interface *src_matrix = src_interface;
  374. struct starpu_matrix_interface *dst_matrix = dst_interface;
  375. size_t elemsize = src_matrix->elemsize;
  376. cudaError_t cures;
  377. int src_dev = _starpu_memory_node_to_devid(src_node);
  378. int dst_dev = _starpu_memory_node_to_devid(dst_node);
  379. #if 0
  380. /* That code is not even working!! */
  381. struct cudaExtent extent = make_cudaExtent(128, 128, 128);
  382. cures = cudaSetDevice(src_dev);
  383. STARPU_ASSERT(cures == cudaSuccess);
  384. struct cudaPitchedPtr mem_device1;
  385. cures = cudaMalloc3D(&mem_device1, extent);
  386. if (STARPU_UNLIKELY(cures))
  387. STARPU_CUDA_REPORT_ERROR(cures);
  388. cures = cudaSetDevice(dst_dev);
  389. STARPU_ASSERT(cures == cudaSuccess);
  390. struct cudaPitchedPtr mem_device2;
  391. cures = cudaMalloc3D(&mem_device2, extent);
  392. if (STARPU_UNLIKELY(cures))
  393. STARPU_CUDA_REPORT_ERROR(cures);
  394. struct cudaMemcpy3DPeerParms p;
  395. memset(&p, 0, sizeof(p));
  396. p.srcDevice = src_dev;
  397. p.dstDevice = dst_dev;
  398. p.srcPtr = mem_device1;
  399. p.dstPtr = mem_device2;
  400. p.extent = extent;
  401. fprintf(stderr,"%u %u\n", p.srcDevice, p.dstDevice);
  402. fprintf(stderr,"%p %p\n", p.srcArray, p.dstArray);
  403. fprintf(stderr,"%p %lu %lu %lu\n", p.srcPtr.ptr, p.srcPtr.pitch, p.srcPtr.xsize, p.srcPtr.ysize);
  404. fprintf(stderr,"%p %lu %lu %lu\n", p.dstPtr.ptr, p.dstPtr.pitch, p.dstPtr.xsize, p.dstPtr.ysize);
  405. fprintf(stderr,"%lu %lu %lu\n", p.srcPos.x, p.srcPos.y, p.srcPos.z);
  406. fprintf(stderr,"%lu %lu %lu\n", p.dstPos.x, p.dstPos.y, p.dstPos.z);
  407. fprintf(stderr,"%lu %lu %lu\n", p.extent.width, p.extent.height, p.extent.depth);
  408. cures = cudaMemcpy3DPeer(&p);
  409. if (STARPU_UNLIKELY(cures))
  410. STARPU_CUDA_REPORT_ERROR(cures);
  411. #endif
  412. #if 0
  413. struct cudaMemcpy3DPeerParms p;
  414. memset(&p, 0, sizeof(p));
  415. p.srcDevice = src_dev;
  416. p.dstDevice = dst_dev;
  417. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->nx * elemsize, src_matrix->ny);
  418. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->nx * elemsize, dst_matrix->ny);
  419. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  420. #if 1
  421. fprintf(stderr,"%u %u\n", p.srcDevice, p.dstDevice);
  422. fprintf(stderr,"%p %p\n", p.srcArray, p.dstArray);
  423. fprintf(stderr,"%p %lu %lu %lu\n", p.srcPtr.ptr, p.srcPtr.pitch, p.srcPtr.xsize, p.srcPtr.ysize);
  424. fprintf(stderr,"%p %lu %lu %lu\n", p.dstPtr.ptr, p.dstPtr.pitch, p.dstPtr.xsize, p.dstPtr.ysize);
  425. fprintf(stderr,"%lu %lu %lu\n", p.srcPos.x, p.srcPos.y, p.srcPos.z);
  426. fprintf(stderr,"%lu %lu %lu\n", p.dstPos.x, p.dstPos.y, p.dstPos.z);
  427. fprintf(stderr,"%lu %lu %lu\n", p.extent.width, p.extent.height, p.extent.depth);
  428. #endif
  429. cures = cudaMemcpy3DPeerAsync(&p, stream);
  430. if (STARPU_UNLIKELY(cures))
  431. STARPU_CUDA_REPORT_ERROR(cures);
  432. cudaThreadSynchronize();
  433. if (is_async)
  434. {
  435. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  436. cures = cudaMemcpy3DPeerAsync(&p, stream);
  437. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  438. if (!cures)
  439. return -EAGAIN;
  440. }
  441. cures = cudaMemcpy3DPeer(&p);
  442. if (STARPU_UNLIKELY(cures))
  443. STARPU_CUDA_REPORT_ERROR(cures);
  444. #else
  445. /* XXX FIXME !!*/
  446. STARPU_ASSERT(src_matrix->nx == src_matrix->ld);
  447. STARPU_ASSERT(dst_matrix->nx == dst_matrix->ld);
  448. if (is_async)
  449. {
  450. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  451. cures = cudaMemcpyPeerAsync((char *)dst_matrix->ptr, dst_dev, (char *)src_matrix->ptr, src_dev, dst_matrix->nx*dst_matrix->ny*elemsize, stream);
  452. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  453. if (!cures)
  454. return -EAGAIN;
  455. }
  456. cures = cudaMemcpyPeer((char *)dst_matrix->ptr, dst_dev, (char *)src_matrix->ptr, src_dev, dst_matrix->nx*dst_matrix->ny*elemsize);
  457. if (STARPU_UNLIKELY(cures))
  458. STARPU_CUDA_REPORT_ERROR(cures);
  459. #endif
  460. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  461. return 0;
  462. }
  463. #endif
  464. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  465. {
  466. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 0, 0);
  467. }
  468. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  469. {
  470. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 0, 0);
  471. }
  472. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  473. {
  474. if (src_node == dst_node)
  475. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 0, 0);
  476. else
  477. {
  478. /* XXX not implemented */
  479. STARPU_ABORT();
  480. return 0;
  481. }
  482. }
  483. 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)
  484. {
  485. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 1, stream);
  486. }
  487. 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)
  488. {
  489. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 1, stream);
  490. }
  491. #ifdef NO_STRIDE
  492. 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)
  493. {
  494. if (src_node == dst_node)
  495. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 1, stream);
  496. else
  497. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node, 1, stream);
  498. }
  499. #endif
  500. #endif // STARPU_USE_CUDA
  501. #ifdef STARPU_USE_OPENCL
  502. 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)
  503. {
  504. struct starpu_matrix_interface *src_matrix = src_interface;
  505. struct starpu_matrix_interface *dst_matrix = dst_interface;
  506. int err,ret;
  507. /* XXX non contiguous matrices are not supported with OpenCL yet ! (TODO) */
  508. STARPU_ASSERT((src_matrix->ld == src_matrix->nx) && (dst_matrix->ld == dst_matrix->nx));
  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. /* XXX non contiguous matrices are not supported with OpenCL yet ! (TODO) */
  523. STARPU_ASSERT((src_matrix->ld == src_matrix->nx) && (dst_matrix->ld == dst_matrix->nx));
  524. err = _starpu_opencl_copy_opencl_to_ram_async_sync((cl_mem)src_matrix->dev_handle, src_node, (void*)dst_matrix->ptr, dst_node,
  525. src_matrix->nx*src_matrix->ny*src_matrix->elemsize,
  526. src_matrix->offset, (cl_event*)_event, &ret);
  527. if (STARPU_UNLIKELY(err))
  528. STARPU_OPENCL_REPORT_ERROR(err);
  529. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  530. return ret;
  531. }
  532. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  533. {
  534. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  535. }
  536. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  537. {
  538. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  539. }
  540. #endif
  541. /* as not all platform easily have a lib installed ... */
  542. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  543. {
  544. struct starpu_matrix_interface *src_matrix = (struct starpu_matrix_interface *) src_interface;
  545. struct starpu_matrix_interface *dst_matrix = (struct starpu_matrix_interface *) dst_interface;
  546. unsigned y;
  547. uint32_t nx = dst_matrix->nx;
  548. uint32_t ny = dst_matrix->ny;
  549. size_t elemsize = dst_matrix->elemsize;
  550. uint32_t ld_src = src_matrix->ld;
  551. uint32_t ld_dst = dst_matrix->ld;
  552. uintptr_t ptr_src = src_matrix->ptr;
  553. uintptr_t ptr_dst = dst_matrix->ptr;
  554. for (y = 0; y < ny; y++)
  555. {
  556. uint32_t src_offset = y*ld_src*elemsize;
  557. uint32_t dst_offset = y*ld_dst*elemsize;
  558. memcpy((void *)(ptr_dst + dst_offset),
  559. (void *)(ptr_src + src_offset), nx*elemsize);
  560. }
  561. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)nx*ny*elemsize);
  562. return 0;
  563. }