variable_interface.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. static int copy_ram_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  27. #ifdef STARPU_USE_CUDA
  28. static int copy_ram_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  29. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  30. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  31. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  32. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  33. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  34. #endif
  35. #ifdef STARPU_USE_OPENCL
  36. static int copy_ram_to_opencl(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  37. static int copy_opencl_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  38. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  39. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  40. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  41. #endif
  42. static struct starpu_data_copy_methods variable_copy_data_methods_s =
  43. {
  44. .ram_to_ram = copy_ram_to_ram,
  45. .ram_to_spu = NULL,
  46. #ifdef STARPU_USE_CUDA
  47. .ram_to_cuda = copy_ram_to_cuda,
  48. .cuda_to_ram = copy_cuda_to_ram,
  49. .cuda_to_cuda = copy_cuda_to_cuda,
  50. .ram_to_cuda_async = copy_ram_to_cuda_async,
  51. .cuda_to_ram_async = copy_cuda_to_ram_async,
  52. .cuda_to_cuda_async = copy_cuda_to_cuda_async,
  53. #endif
  54. #ifdef STARPU_USE_OPENCL
  55. .ram_to_opencl = copy_ram_to_opencl,
  56. .opencl_to_ram = copy_opencl_to_ram,
  57. .opencl_to_opencl = copy_opencl_to_opencl,
  58. .ram_to_opencl_async = copy_ram_to_opencl_async,
  59. .opencl_to_ram_async = copy_opencl_to_ram_async,
  60. #endif
  61. .cuda_to_spu = NULL,
  62. .spu_to_ram = NULL,
  63. .spu_to_cuda = NULL,
  64. .spu_to_spu = NULL
  65. };
  66. static void register_variable_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface);
  67. static ssize_t allocate_variable_buffer_on_node(void *data_interface_, uint32_t dst_node);
  68. static void *variable_handle_to_pointer(starpu_data_handle_t data_handle, uint32_t node);
  69. static void free_variable_buffer_on_node(void *data_interface, uint32_t node);
  70. static size_t variable_interface_get_size(starpu_data_handle_t handle);
  71. static uint32_t footprint_variable_interface_crc32(starpu_data_handle_t handle);
  72. static int variable_compare(void *data_interface_a, void *data_interface_b);
  73. static void display_variable_interface(starpu_data_handle_t handle, FILE *f);
  74. #ifdef STARPU_USE_GORDON
  75. static int convert_variable_to_gordon(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss);
  76. #endif
  77. static struct starpu_data_interface_ops interface_variable_ops =
  78. {
  79. .register_data_handle = register_variable_handle,
  80. .allocate_data_on_node = allocate_variable_buffer_on_node,
  81. .handle_to_pointer = variable_handle_to_pointer,
  82. .free_data_on_node = free_variable_buffer_on_node,
  83. .copy_methods = &variable_copy_data_methods_s,
  84. .get_size = variable_interface_get_size,
  85. .footprint = footprint_variable_interface_crc32,
  86. .compare = variable_compare,
  87. #ifdef STARPU_USE_GORDON
  88. .convert_to_gordon = convert_variable_to_gordon,
  89. #endif
  90. .interfaceid = STARPU_VARIABLE_INTERFACE_ID,
  91. .interface_size = sizeof(struct starpu_variable_interface),
  92. .display = display_variable_interface,
  93. };
  94. static void *variable_handle_to_pointer(starpu_data_handle_t handle, uint32_t node)
  95. {
  96. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  97. return (void*) STARPU_VARIABLE_GET_PTR(starpu_data_get_interface_on_node(handle, node));
  98. }
  99. static void register_variable_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface)
  100. {
  101. unsigned node;
  102. for (node = 0; node < STARPU_MAXNODES; node++)
  103. {
  104. struct starpu_variable_interface *local_interface = (struct starpu_variable_interface *)
  105. starpu_data_get_interface_on_node(handle, node);
  106. if (node == home_node)
  107. {
  108. local_interface->ptr = STARPU_VARIABLE_GET_PTR(data_interface);
  109. }
  110. else
  111. {
  112. local_interface->ptr = 0;
  113. }
  114. local_interface->elemsize = STARPU_VARIABLE_GET_ELEMSIZE(data_interface);
  115. }
  116. }
  117. #ifdef STARPU_USE_GORDON
  118. int convert_variable_to_gordon(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss)
  119. {
  120. *ptr = STARPU_VARIABLE_GET_PTR(interface);
  121. (*ss).size = STARPU_VARIABLE_GET_ELEMSIZE(interface);
  122. return 0;
  123. }
  124. #endif
  125. /* declare a new data with the variable interface */
  126. void starpu_variable_data_register(starpu_data_handle_t *handleptr, uint32_t home_node,
  127. uintptr_t ptr, size_t elemsize)
  128. {
  129. struct starpu_variable_interface variable =
  130. {
  131. .ptr = ptr,
  132. .elemsize = elemsize
  133. };
  134. starpu_data_register(handleptr, home_node, &variable, &interface_variable_ops);
  135. }
  136. static uint32_t footprint_variable_interface_crc32(starpu_data_handle_t handle)
  137. {
  138. return starpu_crc32_be(starpu_variable_get_elemsize(handle), 0);
  139. }
  140. static int variable_compare(void *data_interface_a, void *data_interface_b)
  141. {
  142. struct starpu_variable_interface *variable_a = (struct starpu_variable_interface *) data_interface_a;
  143. struct starpu_variable_interface *variable_b = (struct starpu_variable_interface *) data_interface_b;
  144. /* Two variables are considered compatible if they have the same size */
  145. return (variable_a->elemsize == variable_b->elemsize);
  146. }
  147. static void display_variable_interface(starpu_data_handle_t handle, FILE *f)
  148. {
  149. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  150. starpu_data_get_interface_on_node(handle, 0);
  151. fprintf(f, "%ld\t", (long)variable_interface->elemsize);
  152. }
  153. static size_t variable_interface_get_size(starpu_data_handle_t handle)
  154. {
  155. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  156. starpu_data_get_interface_on_node(handle, 0);
  157. return variable_interface->elemsize;
  158. }
  159. uintptr_t starpu_variable_get_local_ptr(starpu_data_handle_t handle)
  160. {
  161. unsigned node;
  162. node = _starpu_get_local_memory_node();
  163. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  164. return STARPU_VARIABLE_GET_PTR(starpu_data_get_interface_on_node(handle, node));
  165. }
  166. size_t starpu_variable_get_elemsize(starpu_data_handle_t handle)
  167. {
  168. return STARPU_VARIABLE_GET_ELEMSIZE(starpu_data_get_interface_on_node(handle, 0));
  169. }
  170. /* memory allocation/deallocation primitives for the variable interface */
  171. /* returns the size of the allocated area */
  172. static ssize_t allocate_variable_buffer_on_node(void *data_interface_, uint32_t dst_node)
  173. {
  174. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *) data_interface_;
  175. unsigned fail = 0;
  176. uintptr_t addr = 0;
  177. ssize_t allocated_memory;
  178. size_t elemsize = variable_interface->elemsize;
  179. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  180. #ifdef STARPU_USE_CUDA
  181. cudaError_t status;
  182. #endif
  183. switch(kind)
  184. {
  185. case STARPU_CPU_RAM:
  186. addr = (uintptr_t)malloc(elemsize);
  187. if (!addr)
  188. fail = 1;
  189. break;
  190. #ifdef STARPU_USE_CUDA
  191. case STARPU_CUDA_RAM:
  192. status = cudaMalloc((void **)&addr, elemsize);
  193. if (!addr || (status != cudaSuccess))
  194. {
  195. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  196. STARPU_CUDA_REPORT_ERROR(status);
  197. fail = 1;
  198. }
  199. break;
  200. #endif
  201. #ifdef STARPU_USE_OPENCL
  202. case STARPU_OPENCL_RAM:
  203. {
  204. int ret;
  205. cl_mem ptr;
  206. ret = starpu_opencl_allocate_memory(&ptr, elemsize, CL_MEM_READ_WRITE);
  207. addr = (uintptr_t)ptr;
  208. if (ret)
  209. {
  210. fail = 1;
  211. }
  212. break;
  213. }
  214. #endif
  215. default:
  216. STARPU_ABORT();
  217. }
  218. if (fail)
  219. return -ENOMEM;
  220. /* allocation succeeded */
  221. allocated_memory = elemsize;
  222. /* update the data properly in consequence */
  223. variable_interface->ptr = addr;
  224. return allocated_memory;
  225. }
  226. static void free_variable_buffer_on_node(void *data_interface, uint32_t node)
  227. {
  228. enum starpu_node_kind kind = starpu_node_get_kind(node);
  229. switch(kind)
  230. {
  231. case STARPU_CPU_RAM:
  232. free((void*)STARPU_VARIABLE_GET_PTR(data_interface));
  233. break;
  234. #ifdef STARPU_USE_CUDA
  235. case STARPU_CUDA_RAM:
  236. {
  237. cudaError_t err;
  238. err = cudaFree((void*)STARPU_VARIABLE_GET_PTR(data_interface));
  239. if (STARPU_UNLIKELY(err != cudaSuccess))
  240. STARPU_CUDA_REPORT_ERROR(err);
  241. break;
  242. }
  243. #endif
  244. #ifdef STARPU_USE_OPENCL
  245. case STARPU_OPENCL_RAM:
  246. {
  247. cl_int err;
  248. err = clReleaseMemObject((void*)STARPU_VARIABLE_GET_PTR(data_interface));
  249. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  250. STARPU_OPENCL_REPORT_ERROR(err);
  251. break;
  252. }
  253. #endif
  254. default:
  255. STARPU_ABORT();
  256. }
  257. }
  258. #ifdef STARPU_USE_CUDA
  259. static int copy_cuda_async_sync(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream, enum cudaMemcpyKind kind)
  260. {
  261. struct starpu_variable_interface *src_variable = src_interface;
  262. struct starpu_variable_interface *dst_variable = dst_interface;
  263. int ret;
  264. ret = starpu_cuda_copy_async_sync((void *)src_variable->ptr, src_node, (void *)dst_variable->ptr, dst_node, src_variable->elemsize, stream, kind);
  265. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  266. return ret;
  267. }
  268. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  269. {
  270. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, NULL, cudaMemcpyDeviceToHost);
  271. }
  272. static int copy_ram_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  273. {
  274. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, NULL, cudaMemcpyHostToDevice);
  275. }
  276. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  277. {
  278. if (src_node == dst_node)
  279. {
  280. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, NULL, cudaMemcpyDeviceToDevice);
  281. }
  282. else
  283. {
  284. #ifdef HAVE_CUDA_MEMCPY_PEER
  285. int src_dev = _starpu_memory_node_to_devid(src_node);
  286. int dst_dev = _starpu_memory_node_to_devid(dst_node);
  287. struct starpu_variable_interface *src_variable = src_interface;
  288. struct starpu_variable_interface *dst_variable = dst_interface;
  289. cudaError_t cures;
  290. cures = cudaMemcpyPeer((char *)dst_variable->ptr, dst_dev, (char *)src_variable->ptr, src_dev, src_variable->elemsize);
  291. if (STARPU_UNLIKELY(cures))
  292. STARPU_CUDA_REPORT_ERROR(cures);
  293. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  294. #else
  295. /* This is illegal without support for cudaMemcpyPeer */
  296. STARPU_ABORT();
  297. #endif
  298. return 0;
  299. }
  300. }
  301. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  302. {
  303. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToHost);
  304. }
  305. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  306. {
  307. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyHostToDevice);
  308. }
  309. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  310. {
  311. if (src_node == dst_node)
  312. {
  313. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToDevice);
  314. }
  315. else
  316. {
  317. #ifdef HAVE_CUDA_MEMCPY_PEER
  318. int src_dev = _starpu_memory_node_to_devid(src_node);
  319. int dst_dev = _starpu_memory_node_to_devid(dst_node);
  320. struct starpu_variable_interface *src_variable = src_interface;
  321. struct starpu_variable_interface *dst_variable = dst_interface;
  322. size_t length = src_variable->elemsize;
  323. cudaError_t cures;
  324. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  325. cures = cudaMemcpyPeerAsync((char *)dst_variable->ptr, dst_dev, (char *)src_variable->ptr, src_dev, length, stream);
  326. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  327. if (cures)
  328. {
  329. /* sychronous fallback */
  330. cures = cudaMemcpyPeer((char *)dst_variable->ptr, dst_dev, (char *)src_variable->ptr, src_dev, length);
  331. if (STARPU_UNLIKELY(cures))
  332. STARPU_CUDA_REPORT_ERROR(cures);
  333. return 0;
  334. }
  335. _STARPU_TRACE_DATA_COPY(src_node, dst_node, length);
  336. return -EAGAIN;
  337. #else
  338. /* This is illegal without cudaMemcpyPeer */
  339. STARPU_ABORT();
  340. return 0;
  341. #endif
  342. }
  343. }
  344. #endif // STARPU_USE_CUDA
  345. #ifdef STARPU_USE_OPENCL
  346. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface,
  347. unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event)
  348. {
  349. struct starpu_variable_interface *src_variable = src_interface;
  350. struct starpu_variable_interface *dst_variable = dst_interface;
  351. int err,ret;
  352. err = starpu_opencl_copy_ram_to_opencl((void*)src_variable->ptr, src_node, (cl_mem)dst_variable->ptr, dst_node, src_variable->elemsize,
  353. 0, event, &ret);
  354. if (STARPU_UNLIKELY(err))
  355. STARPU_OPENCL_REPORT_ERROR(err);
  356. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  357. return ret;
  358. }
  359. 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)
  360. {
  361. struct starpu_variable_interface *src_variable = src_interface;
  362. struct starpu_variable_interface *dst_variable = dst_interface;
  363. int err, ret;
  364. err = starpu_opencl_copy_opencl_to_ram((cl_mem)src_variable->ptr, src_node, (void*)dst_variable->ptr, dst_node, src_variable->elemsize,
  365. 0, event, &ret);
  366. if (STARPU_UNLIKELY(err))
  367. STARPU_OPENCL_REPORT_ERROR(err);
  368. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  369. return ret;
  370. }
  371. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  372. {
  373. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  374. }
  375. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  376. {
  377. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  378. }
  379. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  380. {
  381. cl_int err;
  382. struct starpu_variable_interface *src_variable = src_interface;
  383. struct starpu_variable_interface *dst_variable = dst_interface;
  384. cl_mem src_ptr = (cl_mem)src_variable->ptr;
  385. cl_mem dst_ptr = (cl_mem)dst_variable->ptr;
  386. cl_command_queue cq;
  387. starpu_opencl_get_current_queue(&cq);
  388. cl_event event;
  389. STARPU_ASSERT(src_variable->elemsize == dst_variable->elemsize);
  390. err= clEnqueueCopyBuffer(cq, src_ptr, dst_ptr, 0, 0, src_variable->elemsize, 0, NULL, &event);
  391. if (STARPU_UNLIKELY(err))
  392. STARPU_OPENCL_REPORT_ERROR(err);
  393. err = clWaitForEvents(1, &event);
  394. if (STARPU_UNLIKELY(err))
  395. STARPU_OPENCL_REPORT_ERROR(err);
  396. err = clReleaseEvent(event);
  397. if (STARPU_UNLIKELY(err))
  398. STARPU_OPENCL_REPORT_ERROR(err);
  399. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  400. return 0;
  401. }
  402. #endif
  403. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  404. {
  405. struct starpu_variable_interface *src_variable = (struct starpu_variable_interface *) src_interface;
  406. struct starpu_variable_interface *dst_variable = (struct starpu_variable_interface *) dst_interface;
  407. size_t elemsize = dst_variable->elemsize;
  408. uintptr_t ptr_src = src_variable->ptr;
  409. uintptr_t ptr_dst = dst_variable->ptr;
  410. memcpy((void *)ptr_dst, (void *)ptr_src, elemsize);
  411. _STARPU_TRACE_DATA_COPY(src_node, dst_node, elemsize);
  412. return 0;
  413. }