variable_interface.c 17 KB

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