vector_interface.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <common/config.h>
  17. #include <datawizard/data_parameters.h>
  18. #include <datawizard/coherency.h>
  19. #include <datawizard/copy-driver.h>
  20. #include <datawizard/hierarchy.h>
  21. #include <common/hash.h>
  22. #include <starpu.h>
  23. #ifdef USE_CUDA
  24. #include <cuda.h>
  25. #endif
  26. static int dummy_copy_ram_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node);
  27. #ifdef USE_CUDA
  28. static int copy_ram_to_cublas(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node);
  29. static int copy_cublas_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node);
  30. static int copy_ram_to_cublas_async(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node, cudaStream_t *stream);
  31. static int copy_cublas_to_ram_async(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node, cudaStream_t *stream);
  32. #endif
  33. static const struct copy_data_methods_s vector_copy_data_methods_s = {
  34. .ram_to_ram = dummy_copy_ram_to_ram,
  35. .ram_to_spu = NULL,
  36. #ifdef USE_CUDA
  37. .ram_to_cuda = copy_ram_to_cublas,
  38. .cuda_to_ram = copy_cublas_to_ram,
  39. .ram_to_cuda_async = copy_ram_to_cublas_async,
  40. .cuda_to_ram_async = copy_cublas_to_ram_async,
  41. #endif
  42. .cuda_to_cuda = NULL,
  43. .cuda_to_spu = NULL,
  44. .spu_to_ram = NULL,
  45. .spu_to_cuda = NULL,
  46. .spu_to_spu = NULL
  47. };
  48. static size_t allocate_vector_buffer_on_node(starpu_data_handle handle, uint32_t dst_node);
  49. static void liberate_vector_buffer_on_node(starpu_data_interface_t *interface, uint32_t node);
  50. static size_t vector_interface_get_size(starpu_data_handle handle);
  51. static uint32_t footprint_vector_interface_crc32(starpu_data_handle handle, uint32_t hstate);
  52. static void display_vector_interface(starpu_data_handle handle, FILE *f);
  53. #ifdef USE_GORDON
  54. static int convert_vector_to_gordon(starpu_data_interface_t *interface, uint64_t *ptr, gordon_strideSize_t *ss);
  55. #endif
  56. struct data_interface_ops_t interface_vector_ops = {
  57. .allocate_data_on_node = allocate_vector_buffer_on_node,
  58. .liberate_data_on_node = liberate_vector_buffer_on_node,
  59. .copy_methods = &vector_copy_data_methods_s,
  60. .get_size = vector_interface_get_size,
  61. .footprint = footprint_vector_interface_crc32,
  62. #ifdef USE_GORDON
  63. .convert_to_gordon = convert_vector_to_gordon,
  64. #endif
  65. .interfaceid = STARPU_VECTOR_INTERFACE_ID,
  66. .interface_size = sizeof(starpu_vector_interface_t),
  67. .display = display_vector_interface
  68. };
  69. #ifdef USE_GORDON
  70. int convert_vector_to_gordon(starpu_data_interface_t *interface, uint64_t *ptr, gordon_strideSize_t *ss)
  71. {
  72. *ptr = (*interface).vector.ptr;
  73. (*ss).size = (*interface).vector.nx * (*interface).vector.elemsize;
  74. return 0;
  75. }
  76. #endif
  77. /* declare a new data with the vector interface */
  78. void starpu_register_vector_data(starpu_data_handle *handleptr, uint32_t home_node,
  79. uintptr_t ptr, uint32_t nx, size_t elemsize)
  80. {
  81. starpu_data_handle handle =
  82. starpu_data_state_create(&interface_vector_ops);
  83. STARPU_ASSERT(handleptr);
  84. *handleptr = handle;
  85. unsigned node;
  86. for (node = 0; node < MAXNODES; node++)
  87. {
  88. starpu_vector_interface_t *local_interface =
  89. starpu_data_get_interface_on_node(handle, node);
  90. if (node == home_node) {
  91. local_interface->ptr = ptr;
  92. }
  93. else {
  94. local_interface->ptr = 0;
  95. }
  96. local_interface->nx = nx;
  97. local_interface->elemsize = elemsize;
  98. }
  99. handle->ops = &interface_vector_ops;
  100. register_new_data(handle, home_node, 0);
  101. }
  102. static inline uint32_t footprint_vector_interface_generic(uint32_t (*hash_func)(uint32_t input, uint32_t hstate), starpu_data_handle handle, uint32_t hstate)
  103. {
  104. uint32_t hash;
  105. hash = hstate;
  106. hash = hash_func(starpu_get_vector_nx(handle), hash);
  107. return hash;
  108. }
  109. uint32_t footprint_vector_interface_crc32(starpu_data_handle handle, uint32_t hstate)
  110. {
  111. return footprint_vector_interface_generic(crc32_be, handle, hstate);
  112. }
  113. struct dumped_vector_interface_s {
  114. uintptr_t ptr;
  115. uint32_t nx;
  116. uint32_t elemsize;
  117. } __attribute__ ((packed));
  118. static void display_vector_interface(starpu_data_handle handle, FILE *f)
  119. {
  120. starpu_vector_interface_t *interface =
  121. starpu_data_get_interface_on_node(handle, 0);
  122. fprintf(f, "%u\t", interface->nx);
  123. }
  124. static size_t vector_interface_get_size(starpu_data_handle handle)
  125. {
  126. size_t size;
  127. starpu_vector_interface_t *interface =
  128. starpu_data_get_interface_on_node(handle, 0);
  129. size = interface->nx*interface->elemsize;
  130. return size;
  131. }
  132. /* offer an access to the data parameters */
  133. uint32_t starpu_get_vector_nx(starpu_data_handle handle)
  134. {
  135. starpu_vector_interface_t *interface =
  136. starpu_data_get_interface_on_node(handle, 0);
  137. return interface->nx;
  138. }
  139. uintptr_t starpu_get_vector_local_ptr(starpu_data_handle handle)
  140. {
  141. unsigned node;
  142. node = get_local_memory_node();
  143. STARPU_ASSERT(handle->per_node[node].allocated);
  144. starpu_vector_interface_t *interface =
  145. starpu_data_get_interface_on_node(handle, node);
  146. return interface->ptr;
  147. }
  148. size_t starpu_get_vector_elemsize(starpu_data_handle handle)
  149. {
  150. starpu_vector_interface_t *interface =
  151. starpu_data_get_interface_on_node(handle, 0);
  152. return interface->elemsize;
  153. }
  154. /* memory allocation/deallocation primitives for the vector interface */
  155. /* returns the size of the allocated area */
  156. static size_t allocate_vector_buffer_on_node(starpu_data_handle handle, uint32_t dst_node)
  157. {
  158. starpu_vector_interface_t *interface =
  159. starpu_data_get_interface_on_node(handle, dst_node);
  160. uintptr_t addr = 0;
  161. size_t allocated_memory;
  162. uint32_t nx = interface->nx;
  163. size_t elemsize = interface->elemsize;
  164. node_kind kind = get_node_kind(dst_node);
  165. switch(kind) {
  166. case RAM:
  167. addr = (uintptr_t)malloc(nx*elemsize);
  168. break;
  169. #ifdef USE_CUDA
  170. case CUDA_RAM:
  171. cublasAlloc(nx, elemsize, (void **)&addr);
  172. break;
  173. #endif
  174. default:
  175. assert(0);
  176. }
  177. if (addr) {
  178. /* allocation succeeded */
  179. allocated_memory = nx*elemsize;
  180. /* update the data properly in consequence */
  181. interface->ptr = addr;
  182. } else {
  183. /* allocation failed */
  184. allocated_memory = 0;
  185. }
  186. return allocated_memory;
  187. }
  188. void liberate_vector_buffer_on_node(starpu_data_interface_t *interface, uint32_t node)
  189. {
  190. node_kind kind = get_node_kind(node);
  191. switch(kind) {
  192. case RAM:
  193. free((void*)interface->vector.ptr);
  194. break;
  195. #ifdef USE_CUDA
  196. case CUDA_RAM:
  197. cublasFree((void*)interface->vector.ptr);
  198. break;
  199. #endif
  200. default:
  201. assert(0);
  202. }
  203. }
  204. #ifdef USE_CUDA
  205. static int copy_cublas_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node)
  206. {
  207. starpu_vector_interface_t *src_vector;
  208. starpu_vector_interface_t *dst_vector;
  209. src_vector = starpu_data_get_interface_on_node(handle, src_node);
  210. dst_vector = starpu_data_get_interface_on_node(handle, dst_node);
  211. cublasGetVector(src_vector->nx, src_vector->elemsize,
  212. (uint8_t *)src_vector->ptr, 1,
  213. (uint8_t *)dst_vector->ptr, 1);
  214. TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  215. return 0;
  216. }
  217. static int copy_ram_to_cublas(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node)
  218. {
  219. starpu_vector_interface_t *src_vector;
  220. starpu_vector_interface_t *dst_vector;
  221. src_vector = starpu_data_get_interface_on_node(handle, src_node);
  222. dst_vector = starpu_data_get_interface_on_node(handle, dst_node);
  223. cublasSetVector(src_vector->nx, src_vector->elemsize,
  224. (uint8_t *)src_vector->ptr, 1,
  225. (uint8_t *)dst_vector->ptr, 1);
  226. TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  227. return 0;
  228. }
  229. static int copy_cublas_to_ram_async(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node, cudaStream_t *stream)
  230. {
  231. starpu_vector_interface_t *src_vector;
  232. starpu_vector_interface_t *dst_vector;
  233. src_vector = starpu_data_get_interface_on_node(handle, src_node);
  234. dst_vector = starpu_data_get_interface_on_node(handle, dst_node);
  235. cudaError_t cures;
  236. cures = cudaMemcpyAsync((char *)dst_vector->ptr, (char *)src_vector->ptr, src_vector->nx*src_vector->elemsize, cudaMemcpyDeviceToHost, *stream);
  237. if (cures)
  238. {
  239. /* do it in a synchronous fashion */
  240. cures = cudaMemcpy((char *)dst_vector->ptr, (char *)src_vector->ptr, src_vector->nx*src_vector->elemsize, cudaMemcpyDeviceToHost);
  241. cudaThreadSynchronize();
  242. if (STARPU_UNLIKELY(cures))
  243. CUDA_REPORT_ERROR(cures);
  244. return 0;
  245. }
  246. TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  247. return EAGAIN;
  248. }
  249. static int copy_ram_to_cublas_async(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node, cudaStream_t *stream)
  250. {
  251. starpu_vector_interface_t *src_vector;
  252. starpu_vector_interface_t *dst_vector;
  253. src_vector = starpu_data_get_interface_on_node(handle, src_node);
  254. dst_vector = starpu_data_get_interface_on_node(handle, dst_node);
  255. cudaError_t cures;
  256. cures = cudaMemcpyAsync((char *)dst_vector->ptr, (char *)src_vector->ptr, src_vector->nx*src_vector->elemsize, cudaMemcpyHostToDevice, *stream);
  257. if (cures)
  258. {
  259. /* do it in a synchronous fashion */
  260. cures = cudaMemcpy((char *)dst_vector->ptr, (char *)src_vector->ptr, src_vector->nx*src_vector->elemsize, cudaMemcpyHostToDevice);
  261. cudaThreadSynchronize();
  262. if (STARPU_UNLIKELY(cures))
  263. CUDA_REPORT_ERROR(cures);
  264. return 0;
  265. }
  266. TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  267. return EAGAIN;
  268. }
  269. #endif // USE_CUDA
  270. static int dummy_copy_ram_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node)
  271. {
  272. starpu_vector_interface_t *src_vector;
  273. starpu_vector_interface_t *dst_vector;
  274. src_vector = starpu_data_get_interface_on_node(handle, src_node);
  275. dst_vector = starpu_data_get_interface_on_node(handle, dst_node);
  276. uint32_t nx = dst_vector->nx;
  277. size_t elemsize = dst_vector->elemsize;
  278. uintptr_t ptr_src = src_vector->ptr;
  279. uintptr_t ptr_dst = dst_vector->ptr;
  280. memcpy((void *)ptr_dst, (void *)ptr_src, nx*elemsize);
  281. TRACE_DATA_COPY(src_node, dst_node, nx*elemsize);
  282. return 0;
  283. }