multiformat_interface.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 Institut National de Recherche en Informatique et Automatique
  4. * Copyright (C) 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 <core/task.h>
  27. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node);
  28. #ifdef STARPU_USE_CUDA
  29. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node);
  30. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node);
  31. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  32. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  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_cuda_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  35. #endif
  36. #ifdef STARPU_USE_OPENCL
  37. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node);
  38. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node);
  39. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node);
  40. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node, cl_event *event);
  41. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node, cl_event *event);
  42. #endif
  43. static const struct starpu_data_copy_methods multiformat_copy_data_methods_s =
  44. {
  45. .ram_to_ram = copy_ram_to_ram,
  46. #ifdef STARPU_USE_CUDA
  47. .ram_to_cuda = copy_ram_to_cuda,
  48. .cuda_to_ram = copy_cuda_to_ram,
  49. .ram_to_cuda_async = copy_ram_to_cuda_async,
  50. .cuda_to_ram_async = copy_cuda_to_ram_async,
  51. .cuda_to_cuda = copy_cuda_to_cuda,
  52. .cuda_to_cuda_async = copy_cuda_to_cuda_async,
  53. #else
  54. #ifdef STARPU_SIMGRID
  55. /* Enable GPU-GPU transfers in simgrid */
  56. .cuda_to_cuda_async = 1,
  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. .opencl_to_opencl = copy_opencl_to_opencl,
  63. .ram_to_opencl_async = copy_ram_to_opencl_async,
  64. .opencl_to_ram_async = copy_opencl_to_ram_async,
  65. #endif
  66. };
  67. static void register_multiformat_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  68. static ssize_t allocate_multiformat_buffer_on_node(void *data_interface_, unsigned dst_node);
  69. static void *multiformat_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  70. static void free_multiformat_buffer_on_node(void *data_interface, unsigned node);
  71. static size_t multiformat_interface_get_size(starpu_data_handle_t handle);
  72. static uint32_t footprint_multiformat_interface_crc32(starpu_data_handle_t handle);
  73. static int multiformat_compare(void *data_interface_a, void *data_interface_b);
  74. static void display_multiformat_interface(starpu_data_handle_t handle, FILE *f);
  75. static uint32_t starpu_multiformat_get_nx(starpu_data_handle_t handle);
  76. static struct starpu_multiformat_data_interface_ops*
  77. get_mf_ops(void *data_interface)
  78. {
  79. struct starpu_multiformat_interface *mf;
  80. mf = (struct starpu_multiformat_interface *) data_interface;
  81. return mf->ops;
  82. }
  83. static struct starpu_data_interface_ops interface_multiformat_ops =
  84. {
  85. .register_data_handle = register_multiformat_handle,
  86. .allocate_data_on_node = allocate_multiformat_buffer_on_node,
  87. .handle_to_pointer = multiformat_handle_to_pointer,
  88. .free_data_on_node = free_multiformat_buffer_on_node,
  89. .copy_methods = &multiformat_copy_data_methods_s,
  90. .get_size = multiformat_interface_get_size,
  91. .footprint = footprint_multiformat_interface_crc32,
  92. .compare = multiformat_compare,
  93. .interfaceid = STARPU_MULTIFORMAT_INTERFACE_ID,
  94. .interface_size = sizeof(struct starpu_multiformat_interface),
  95. .display = display_multiformat_interface,
  96. .is_multiformat = 1,
  97. .get_mf_ops = get_mf_ops
  98. };
  99. static void *multiformat_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  100. {
  101. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  102. struct starpu_multiformat_interface *multiformat_interface =
  103. (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, node);
  104. switch(starpu_node_get_kind(node))
  105. {
  106. case STARPU_CPU_RAM:
  107. return multiformat_interface->cpu_ptr;
  108. #ifdef STARPU_USE_CUDA
  109. case STARPU_CUDA_RAM:
  110. return multiformat_interface->cuda_ptr;
  111. #endif
  112. #ifdef STARPU_USE_OPENCL
  113. case STARPU_OPENCL_RAM:
  114. return multiformat_interface->opencl_ptr;
  115. #endif
  116. default:
  117. STARPU_ABORT();
  118. }
  119. return NULL;
  120. }
  121. static void register_multiformat_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  122. {
  123. struct starpu_multiformat_interface *multiformat_interface;
  124. multiformat_interface = (struct starpu_multiformat_interface *) data_interface;
  125. unsigned node;
  126. for (node = 0; node < STARPU_MAXNODES; node++)
  127. {
  128. struct starpu_multiformat_interface *local_interface =
  129. (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, node);
  130. if (node == home_node)
  131. {
  132. local_interface->cpu_ptr = multiformat_interface->cpu_ptr;
  133. #ifdef STARPU_USE_CUDA
  134. local_interface->cuda_ptr = multiformat_interface->cuda_ptr;
  135. #endif
  136. #ifdef STARPU_USE_OPENCL
  137. local_interface->opencl_ptr = multiformat_interface->opencl_ptr;
  138. #endif
  139. }
  140. else
  141. {
  142. local_interface->cpu_ptr = NULL;
  143. #ifdef STARPU_USE_CUDA
  144. local_interface->cuda_ptr = NULL;
  145. #endif
  146. #ifdef STARPU_USE_OPENCL
  147. local_interface->opencl_ptr = NULL;
  148. #endif
  149. }
  150. local_interface->nx = multiformat_interface->nx;
  151. local_interface->ops = multiformat_interface->ops;
  152. }
  153. }
  154. void starpu_multiformat_data_register(starpu_data_handle_t *handleptr,
  155. unsigned home_node,
  156. void *ptr,
  157. uint32_t nobjects,
  158. struct starpu_multiformat_data_interface_ops *format_ops)
  159. {
  160. _starpu_codelet_check_deprecated_fields(format_ops->cpu_to_opencl_cl);
  161. _starpu_codelet_check_deprecated_fields(format_ops->opencl_to_cpu_cl);
  162. _starpu_codelet_check_deprecated_fields(format_ops->cpu_to_cuda_cl);
  163. _starpu_codelet_check_deprecated_fields(format_ops->cuda_to_cpu_cl);
  164. struct starpu_multiformat_interface multiformat =
  165. {
  166. .cpu_ptr = ptr,
  167. .cuda_ptr = NULL,
  168. .opencl_ptr = NULL,
  169. .nx = nobjects,
  170. .ops = format_ops
  171. };
  172. starpu_data_register(handleptr, home_node, &multiformat, &interface_multiformat_ops);
  173. }
  174. static uint32_t footprint_multiformat_interface_crc32(starpu_data_handle_t handle)
  175. {
  176. return starpu_crc32_be(starpu_multiformat_get_nx(handle), 0);
  177. }
  178. static int multiformat_compare(void *data_interface_a, void *data_interface_b)
  179. {
  180. struct starpu_multiformat_interface *multiformat_a = (struct starpu_multiformat_interface *) data_interface_a;
  181. struct starpu_multiformat_interface *multiformat_b = (struct starpu_multiformat_interface *) data_interface_b;
  182. return ((multiformat_a->nx == multiformat_b->nx)
  183. && (multiformat_a->ops->cpu_elemsize == multiformat_b->ops->cpu_elemsize)
  184. #ifdef STARPU_USE_CUDA
  185. && (multiformat_a->ops->cuda_elemsize == multiformat_b->ops->cuda_elemsize)
  186. #endif
  187. #ifdef STARPU_USE_OPENCL
  188. && (multiformat_a->ops->opencl_elemsize == multiformat_b->ops->opencl_elemsize)
  189. #endif
  190. );
  191. }
  192. static void display_multiformat_interface(starpu_data_handle_t handle, FILE *f)
  193. {
  194. struct starpu_multiformat_interface *multiformat_interface;
  195. multiformat_interface = (struct starpu_multiformat_interface *)
  196. starpu_data_get_interface_on_node(handle, 0);
  197. fprintf(f, "%u\t", multiformat_interface->nx);
  198. }
  199. /* XXX : returns CPU size */
  200. static size_t multiformat_interface_get_size(starpu_data_handle_t handle)
  201. {
  202. size_t size;
  203. struct starpu_multiformat_interface *multiformat_interface;
  204. multiformat_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  205. size = multiformat_interface->nx * multiformat_interface->ops->cpu_elemsize;
  206. return size;
  207. }
  208. uint32_t starpu_multiformat_get_nx(starpu_data_handle_t handle)
  209. {
  210. struct starpu_multiformat_interface *multiformat_interface;
  211. multiformat_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  212. return multiformat_interface->nx;
  213. }
  214. static ssize_t allocate_multiformat_buffer_on_node(void *data_interface_, unsigned dst_node)
  215. {
  216. struct starpu_multiformat_interface *multiformat_interface;
  217. multiformat_interface = (struct starpu_multiformat_interface *) data_interface_;
  218. uintptr_t addr = 0;
  219. ssize_t allocated_memory = 0;
  220. size_t size;
  221. size = multiformat_interface->nx * multiformat_interface->ops->cpu_elemsize;
  222. allocated_memory += size;
  223. addr = starpu_malloc_on_node(dst_node, size);
  224. if (!addr)
  225. goto fail_cpu;
  226. multiformat_interface->cpu_ptr = (void *) addr;
  227. #ifdef STARPU_USE_CUDA
  228. size = multiformat_interface->nx * multiformat_interface->ops->cuda_elemsize;
  229. allocated_memory += size;
  230. addr = starpu_malloc_on_node(dst_node, size);
  231. if (!addr)
  232. goto fail_cuda;
  233. multiformat_interface->cuda_ptr = (void *) addr;
  234. #endif
  235. #ifdef STARPU_USE_OPENCL
  236. size = multiformat_interface->nx * multiformat_interface->ops->opencl_elemsize;
  237. allocated_memory += size;
  238. addr = starpu_malloc_on_node(dst_node, size);
  239. if (!addr)
  240. goto fail_opencl;
  241. multiformat_interface->opencl_ptr = (void *) addr;
  242. #endif
  243. return allocated_memory;
  244. #ifdef STARPU_USE_OPENCL
  245. fail_opencl:
  246. #ifdef STARPU_USE_CUDA
  247. starpu_free_on_node(dst_node, (uintptr_t) multiformat_interface->cuda_ptr, multiformat_interface->nx * multiformat_interface->ops->cuda_elemsize);
  248. #endif
  249. #endif
  250. #ifdef STARPU_USE_CUDA
  251. fail_cuda:
  252. #endif
  253. starpu_free_on_node(dst_node, (uintptr_t) multiformat_interface->cpu_ptr, multiformat_interface->nx * multiformat_interface->ops->cpu_elemsize);
  254. fail_cpu:
  255. return -ENOMEM;
  256. }
  257. static void free_multiformat_buffer_on_node(void *data_interface, unsigned node)
  258. {
  259. struct starpu_multiformat_interface *multiformat_interface;
  260. multiformat_interface = (struct starpu_multiformat_interface *) data_interface;
  261. starpu_free_on_node(node, (uintptr_t) multiformat_interface->cpu_ptr,
  262. multiformat_interface->nx * multiformat_interface->ops->cpu_elemsize);
  263. multiformat_interface->cpu_ptr = NULL;
  264. #ifdef STARPU_USE_CUDA
  265. starpu_free_on_node(node, (uintptr_t) multiformat_interface->cuda_ptr,
  266. multiformat_interface->nx * multiformat_interface->ops->cuda_elemsize);
  267. multiformat_interface->cuda_ptr = NULL;
  268. #endif
  269. #ifdef STARPU_USE_OPENCL
  270. starpu_free_on_node(node, (uintptr_t) multiformat_interface->opencl_ptr,
  271. multiformat_interface->nx * multiformat_interface->ops->opencl_elemsize);
  272. multiformat_interface->opencl_ptr = NULL;
  273. #endif
  274. }
  275. /*
  276. * Copy methods
  277. */
  278. static int copy_ram_to_ram(void *src_interface, unsigned src_node __attribute__ ((unused)),
  279. void *dst_interface, unsigned dst_node __attribute__ ((unused)))
  280. {
  281. struct starpu_multiformat_interface *src_multiformat;
  282. struct starpu_multiformat_interface *dst_multiformat;
  283. src_multiformat = (struct starpu_multiformat_interface *) src_interface;
  284. dst_multiformat = (struct starpu_multiformat_interface *) dst_interface;
  285. STARPU_ASSERT(src_multiformat != NULL);
  286. STARPU_ASSERT(dst_multiformat != NULL);
  287. STARPU_ASSERT(dst_multiformat->ops != NULL);
  288. size_t size = dst_multiformat->nx * dst_multiformat->ops->cpu_elemsize;
  289. memcpy(dst_multiformat->cpu_ptr, src_multiformat->cpu_ptr, size);
  290. return 0;
  291. }
  292. #ifdef STARPU_USE_CUDA
  293. static int copy_cuda_common(void *src_interface, unsigned src_node __attribute__ ((unused)),
  294. void *dst_interface, unsigned dst_node __attribute__ ((unused)),
  295. enum cudaMemcpyKind kind)
  296. {
  297. struct starpu_multiformat_interface *src_multiformat;
  298. struct starpu_multiformat_interface *dst_multiformat;
  299. src_multiformat = (struct starpu_multiformat_interface *) src_interface;
  300. dst_multiformat = (struct starpu_multiformat_interface *) dst_interface;
  301. size_t size;
  302. cudaError_t status;
  303. switch (kind)
  304. {
  305. case cudaMemcpyHostToDevice:
  306. {
  307. size = src_multiformat->nx * src_multiformat->ops->cuda_elemsize;
  308. if (src_multiformat->cuda_ptr == NULL)
  309. {
  310. src_multiformat->cuda_ptr = malloc(size);
  311. if (src_multiformat->cuda_ptr == NULL)
  312. return -ENOMEM;
  313. }
  314. status = cudaMemcpy(dst_multiformat->cpu_ptr, src_multiformat->cpu_ptr, size, kind);
  315. if (STARPU_UNLIKELY(status))
  316. {
  317. STARPU_CUDA_REPORT_ERROR(status);
  318. }
  319. break;
  320. }
  321. case cudaMemcpyDeviceToHost:
  322. {
  323. size = src_multiformat->nx * src_multiformat->ops->cuda_elemsize;
  324. status = cudaMemcpy(dst_multiformat->cuda_ptr, src_multiformat->cuda_ptr, size, kind);
  325. if (STARPU_UNLIKELY(status))
  326. STARPU_CUDA_REPORT_ERROR(status);
  327. break;
  328. }
  329. case cudaMemcpyDeviceToDevice:
  330. {
  331. size = src_multiformat->nx * src_multiformat->ops->cuda_elemsize;
  332. status = cudaMemcpy(dst_multiformat->cuda_ptr, src_multiformat->cuda_ptr, size, kind);
  333. if (STARPU_UNLIKELY(status))
  334. STARPU_CUDA_REPORT_ERROR(status);
  335. break;
  336. }
  337. default:
  338. STARPU_ABORT();
  339. }
  340. return 0;
  341. }
  342. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node)
  343. {
  344. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  345. }
  346. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node)
  347. {
  348. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  349. }
  350. static int copy_cuda_common_async(void *src_interface, unsigned src_node __attribute__ ((unused)),
  351. void *dst_interface, unsigned dst_node __attribute__ ((unused)),
  352. cudaStream_t stream, enum cudaMemcpyKind kind)
  353. {
  354. struct starpu_multiformat_interface *src_multiformat;
  355. struct starpu_multiformat_interface *dst_multiformat;
  356. src_multiformat = (struct starpu_multiformat_interface *) src_interface;
  357. dst_multiformat = (struct starpu_multiformat_interface *) dst_interface;
  358. size_t size;
  359. cudaError_t status;
  360. switch (kind)
  361. {
  362. case cudaMemcpyHostToDevice:
  363. {
  364. size = src_multiformat->nx * src_multiformat->ops->cuda_elemsize;
  365. if (src_multiformat->cuda_ptr == NULL)
  366. {
  367. src_multiformat->cuda_ptr = malloc(size);
  368. if (src_multiformat->cuda_ptr == NULL)
  369. return -ENOMEM;
  370. }
  371. status = cudaMemcpyAsync(dst_multiformat->cpu_ptr, src_multiformat->cpu_ptr, size, kind, stream);
  372. if (STARPU_UNLIKELY(status))
  373. {
  374. STARPU_CUDA_REPORT_ERROR(status);
  375. }
  376. break;
  377. }
  378. case cudaMemcpyDeviceToHost:
  379. {
  380. size = src_multiformat->nx * src_multiformat->ops->cuda_elemsize;
  381. status = cudaMemcpy(dst_multiformat->cuda_ptr, src_multiformat->cuda_ptr, size, kind);
  382. if (STARPU_UNLIKELY(status))
  383. STARPU_CUDA_REPORT_ERROR(status);
  384. break;
  385. }
  386. case cudaMemcpyDeviceToDevice:
  387. {
  388. size = src_multiformat->nx * src_multiformat->ops->cuda_elemsize;
  389. status = cudaMemcpyAsync(dst_multiformat->cuda_ptr, src_multiformat->cuda_ptr, size, kind, stream);
  390. if (STARPU_UNLIKELY(status))
  391. STARPU_CUDA_REPORT_ERROR(status);
  392. break;
  393. }
  394. default:
  395. STARPU_ABORT();
  396. }
  397. return 0;
  398. }
  399. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  400. {
  401. return copy_cuda_common_async(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyHostToDevice);
  402. }
  403. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  404. {
  405. return copy_cuda_common_async(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToHost);
  406. }
  407. #ifdef HAVE_CUDA_MEMCPY_PEER
  408. static int copy_cuda_peer_common(void *src_interface, unsigned src_node,
  409. void *dst_interface, unsigned dst_node,
  410. cudaStream_t stream)
  411. {
  412. struct starpu_multiformat_interface *src_multiformat;
  413. struct starpu_multiformat_interface *dst_multiformat;
  414. src_multiformat = (struct starpu_multiformat_interface *) src_interface;
  415. dst_multiformat = (struct starpu_multiformat_interface *) dst_interface;
  416. STARPU_ASSERT(src_multiformat != NULL);
  417. STARPU_ASSERT(dst_multiformat != NULL);
  418. STARPU_ASSERT(src_multiformat->ops != NULL);
  419. cudaError_t status;
  420. int size = src_multiformat->nx * src_multiformat->ops->cuda_elemsize;
  421. int src_dev = _starpu_memory_node_get_devid(src_node);
  422. int dst_dev = _starpu_memory_node_get_devid(dst_node);
  423. if (stream)
  424. {
  425. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  426. status = cudaMemcpyPeerAsync(dst_multiformat->cuda_ptr, dst_dev,
  427. src_multiformat->cuda_ptr, src_dev,
  428. size, stream);
  429. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  430. /* All good ! Still, returning -EAGAIN, because we will need to
  431. check the transfert completion later */
  432. if (status == cudaSuccess)
  433. return -EAGAIN;
  434. }
  435. /* Either a synchronous transfert was requested, or the asynchronous one
  436. failed. */
  437. status = cudaMemcpyPeer(dst_multiformat->cuda_ptr, dst_dev,
  438. src_multiformat->cuda_ptr, src_dev,
  439. size);
  440. if (STARPU_UNLIKELY(status != cudaSuccess))
  441. STARPU_CUDA_REPORT_ERROR(status);
  442. _STARPU_TRACE_DATA_COPY(src_node, dst_node, size);
  443. return 0;
  444. }
  445. #endif
  446. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  447. {
  448. if (src_node == dst_node)
  449. {
  450. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice);
  451. }
  452. else
  453. {
  454. #ifdef HAVE_CUDA_MEMCPY_PEER
  455. return copy_cuda_peer_common(src_interface, src_node,
  456. dst_interface, dst_node,
  457. NULL);
  458. #else
  459. STARPU_ABORT();
  460. #endif
  461. }
  462. }
  463. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node,
  464. void *dst_interface, unsigned dst_node,
  465. cudaStream_t stream)
  466. {
  467. if (src_node == dst_node)
  468. {
  469. return copy_cuda_common_async(src_interface, src_node,
  470. dst_interface, dst_node,
  471. stream, cudaMemcpyDeviceToDevice);
  472. }
  473. else
  474. {
  475. #ifdef HAVE_CUDA_MEMCPY_PEER
  476. return copy_cuda_peer_common(src_interface, src_node,
  477. dst_interface, dst_node,
  478. stream);
  479. #else
  480. STARPU_ABORT();
  481. #endif
  482. }
  483. }
  484. #endif /* STARPU_USE_CUDA */
  485. #ifdef STARPU_USE_OPENCL
  486. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node,
  487. void *dst_interface, unsigned dst_node,
  488. cl_event *event)
  489. {
  490. int err, ret;
  491. size_t size;
  492. struct starpu_multiformat_interface *src_multiformat;
  493. struct starpu_multiformat_interface *dst_multiformat;
  494. src_multiformat = (struct starpu_multiformat_interface *) src_interface;
  495. dst_multiformat = (struct starpu_multiformat_interface *) dst_interface;
  496. STARPU_ASSERT(src_multiformat != NULL);
  497. STARPU_ASSERT(dst_multiformat != NULL);
  498. STARPU_ASSERT(src_multiformat->ops != NULL);
  499. size = src_multiformat->nx * src_multiformat->ops->opencl_elemsize;
  500. err = starpu_opencl_copy_ram_to_opencl(src_multiformat->cpu_ptr,
  501. src_node,
  502. (cl_mem) dst_multiformat->cpu_ptr,
  503. dst_node,
  504. size,
  505. 0,
  506. event,
  507. &ret);
  508. if (STARPU_UNLIKELY(err))
  509. STARPU_OPENCL_REPORT_ERROR(err);
  510. _STARPU_TRACE_DATA_COPY(src_node, dst_node, size);
  511. return ret;
  512. }
  513. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node,
  514. void *dst_interface, unsigned dst_node,
  515. cl_event *event)
  516. {
  517. int err, ret;
  518. size_t size;
  519. struct starpu_multiformat_interface *src_multiformat;
  520. struct starpu_multiformat_interface *dst_multiformat;
  521. src_multiformat = (struct starpu_multiformat_interface *) src_interface;
  522. dst_multiformat = (struct starpu_multiformat_interface *) dst_interface;
  523. STARPU_ASSERT(src_multiformat != NULL);
  524. STARPU_ASSERT(dst_multiformat != NULL);
  525. STARPU_ASSERT(src_multiformat->ops != NULL);
  526. STARPU_ASSERT(dst_multiformat->ops != NULL);
  527. size = src_multiformat->nx * src_multiformat->ops->opencl_elemsize;
  528. if (dst_multiformat->opencl_ptr == NULL)
  529. {
  530. /* XXX : it is weird that we might have to allocate memory here... */
  531. dst_multiformat->opencl_ptr = malloc(dst_multiformat->nx * dst_multiformat->ops->opencl_elemsize);
  532. }
  533. err = starpu_opencl_copy_opencl_to_ram((cl_mem)src_multiformat->opencl_ptr,
  534. src_node,
  535. dst_multiformat->opencl_ptr,
  536. dst_node,
  537. size,
  538. 0,
  539. event,
  540. &ret);
  541. if (STARPU_UNLIKELY(err))
  542. STARPU_OPENCL_REPORT_ERROR(err);
  543. _STARPU_TRACE_DATA_COPY(src_node, dst_node, size);
  544. return ret;
  545. }
  546. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED,
  547. void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  548. {
  549. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  550. }
  551. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED,
  552. void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  553. {
  554. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  555. }
  556. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node,
  557. void *dst_interface, unsigned dst_node)
  558. {
  559. (void) src_interface;
  560. (void) dst_interface;
  561. (void) src_node;
  562. (void) dst_node;
  563. STARPU_ASSERT_MSG(0, "XXX multiformat copy OpenCL-OpenCL not supported yet (TODO)");
  564. return 0;
  565. }
  566. #endif