complex_interface.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU 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. * StarPU 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 <starpu.h>
  17. #include <starpu_cuda.h>
  18. #include <starpu_opencl.h>
  19. #include <starpu_hash.h>
  20. #include "complex_interface.h"
  21. double *starpu_complex_get_real(starpu_data_handle_t handle)
  22. {
  23. struct starpu_complex_interface *complex_interface =
  24. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  25. return complex_interface->real;
  26. }
  27. double *starpu_complex_get_imaginary(starpu_data_handle_t handle)
  28. {
  29. struct starpu_complex_interface *complex_interface =
  30. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  31. return complex_interface->imaginary;
  32. }
  33. int starpu_complex_get_nx(starpu_data_handle_t handle)
  34. {
  35. struct starpu_complex_interface *complex_interface =
  36. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  37. return complex_interface->nx;
  38. }
  39. static void complex_register_data_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface)
  40. {
  41. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  42. unsigned node;
  43. for (node = 0; node < STARPU_MAXNODES; node++)
  44. {
  45. struct starpu_complex_interface *local_interface = (struct starpu_complex_interface *)
  46. starpu_data_get_interface_on_node(handle, node);
  47. local_interface->real = complex_interface->real;
  48. local_interface->imaginary = complex_interface->imaginary;
  49. local_interface->nx = complex_interface->nx;
  50. }
  51. }
  52. static starpu_ssize_t complex_allocate_data_on_node(void *data_interface, uint32_t node)
  53. {
  54. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  55. unsigned fail = 0;
  56. double *addr_real = 0;
  57. double *addr_imaginary = 0;
  58. ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  59. enum starpu_node_kind kind = starpu_node_get_kind(node);
  60. switch(kind)
  61. {
  62. case STARPU_CPU_RAM:
  63. addr_real = malloc(requested_memory);
  64. addr_imaginary = malloc(requested_memory);
  65. if (!addr_real || !addr_imaginary)
  66. fail = 1;
  67. break;
  68. #ifdef STARPU_USE_CUDA
  69. case STARPU_CUDA_RAM:
  70. {
  71. cudaError_t status;
  72. status = cudaMalloc((void **)&addr_real, requested_memory);
  73. if (!addr_real || (status != cudaSuccess))
  74. {
  75. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  76. STARPU_CUDA_REPORT_ERROR(status);
  77. fail = 1;
  78. }
  79. else
  80. {
  81. status = cudaMalloc((void **)&addr_imaginary, requested_memory);
  82. if (!addr_imaginary || (status != cudaSuccess))
  83. {
  84. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  85. STARPU_CUDA_REPORT_ERROR(status);
  86. fail = 1;
  87. }
  88. }
  89. break;
  90. }
  91. #endif
  92. #ifdef STARPU_USE_OPENCL
  93. case STARPU_OPENCL_RAM:
  94. {
  95. int ret;
  96. cl_mem real, imaginary;
  97. ret = starpu_opencl_allocate_memory(&real, requested_memory, CL_MEM_READ_WRITE);
  98. if (ret != CL_SUCCESS)
  99. {
  100. fail = 1;
  101. break;
  102. }
  103. else
  104. {
  105. addr_real = (double *) real;
  106. }
  107. ret = starpu_opencl_allocate_memory(&imaginary, requested_memory, CL_MEM_READ_WRITE);
  108. if (ret != CL_SUCCESS)
  109. {
  110. fail = 1;
  111. break;
  112. }
  113. else
  114. {
  115. addr_imaginary = (double *) imaginary;
  116. }
  117. break;
  118. }
  119. #endif
  120. default:
  121. STARPU_ASSERT(0);
  122. }
  123. if (fail)
  124. return -ENOMEM;
  125. /* update the data properly in consequence */
  126. complex_interface->real = addr_real;
  127. complex_interface->imaginary = addr_imaginary;
  128. return 2*requested_memory;
  129. }
  130. static size_t complex_get_size(starpu_data_handle_t handle)
  131. {
  132. size_t size;
  133. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  134. size = complex_interface->nx * 2 * sizeof(double);
  135. return size;
  136. }
  137. static uint32_t complex_footprint(starpu_data_handle_t handle)
  138. {
  139. return starpu_crc32_be(starpu_complex_get_nx(handle), 0);
  140. }
  141. #ifdef STARPU_USE_CUDA
  142. static int copy_cuda_async_sync(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, enum cudaMemcpyKind kind, cudaStream_t stream)
  143. {
  144. struct starpu_complex_interface *src_complex = src_interface;
  145. struct starpu_complex_interface *dst_complex = dst_interface;
  146. cudaStream_t sstream = stream;
  147. int ret;
  148. ret = starpu_cuda_copy_async_sync((void *)src_complex->real, src_node, (void *)dst_complex->real, dst_node,
  149. src_complex->nx*sizeof(src_complex->real[0]), sstream, kind);
  150. if (ret == 0) sstream = NULL;
  151. ret = starpu_cuda_copy_async_sync((char *)src_complex->imaginary, src_node, (char *)dst_complex->imaginary, dst_node,
  152. src_complex->nx*sizeof(src_complex->imaginary[0]), sstream, kind);
  153. return ret;
  154. }
  155. static int copy_ram_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  156. {
  157. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, NULL);
  158. }
  159. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  160. {
  161. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, stream);
  162. }
  163. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  164. {
  165. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, NULL);
  166. }
  167. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  168. {
  169. return copy_cuda_async_sync(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, stream);
  170. }
  171. #endif
  172. #ifdef STARPU_USE_OPENCL
  173. static int copy_ram_to_opencl(void *src_interface, unsigned src_node,
  174. void *dst_interface, unsigned dst_node)
  175. {
  176. struct starpu_complex_interface *src_complex = src_interface;
  177. struct starpu_complex_interface *dst_complex = dst_interface;
  178. cl_int err;
  179. err = starpu_opencl_copy_ram_to_opencl(src_complex->real,
  180. src_node,
  181. (cl_mem) dst_complex->real,
  182. dst_node,
  183. src_complex->nx * sizeof(src_complex->real[0]),
  184. 0,
  185. NULL,
  186. NULL);
  187. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  188. STARPU_OPENCL_REPORT_ERROR(err);
  189. err = starpu_opencl_copy_ram_to_opencl(src_complex->imaginary,
  190. src_node,
  191. (cl_mem) dst_complex->imaginary,
  192. dst_node,
  193. src_complex->nx * sizeof(src_complex->imaginary[0]),
  194. 0,
  195. NULL,
  196. NULL);
  197. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  198. STARPU_OPENCL_REPORT_ERROR(err);
  199. return 0;
  200. }
  201. static int copy_opencl_to_ram(void *src_interface, unsigned src_node,
  202. void *dst_interface, unsigned dst_node)
  203. {
  204. struct starpu_complex_interface *src_complex = src_interface;
  205. struct starpu_complex_interface *dst_complex = dst_interface;
  206. cl_int err;
  207. err = starpu_opencl_copy_opencl_to_ram((cl_mem) src_complex->real,
  208. src_node,
  209. dst_complex->real,
  210. dst_node,
  211. src_complex->nx * sizeof(src_complex->real[0]),
  212. 0,
  213. NULL,
  214. NULL);
  215. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  216. STARPU_OPENCL_REPORT_ERROR(err);
  217. err = starpu_opencl_copy_opencl_to_ram((cl_mem) src_complex->imaginary,
  218. src_node,
  219. dst_complex->imaginary,
  220. dst_node,
  221. src_complex->nx * sizeof(src_complex->imaginary[0]),
  222. 0,
  223. NULL,
  224. NULL);
  225. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  226. STARPU_OPENCL_REPORT_ERROR(err);
  227. return 0;
  228. }
  229. #endif
  230. static struct starpu_data_copy_methods complex_copy_methods =
  231. {
  232. #ifdef STARPU_USE_CUDA
  233. .ram_to_cuda = copy_ram_to_cuda,
  234. .cuda_to_ram = copy_cuda_to_ram,
  235. .ram_to_cuda_async = copy_ram_to_cuda_async,
  236. .cuda_to_ram_async = copy_cuda_to_ram_async,
  237. #endif
  238. #ifdef STARPU_USE_OPENCL
  239. .ram_to_opencl = copy_ram_to_opencl,
  240. .opencl_to_ram = copy_opencl_to_ram,
  241. #warning implement the asynchronous functions
  242. #endif
  243. };
  244. static struct starpu_data_interface_ops interface_complex_ops =
  245. {
  246. .register_data_handle = complex_register_data_handle,
  247. .allocate_data_on_node = complex_allocate_data_on_node,
  248. .copy_methods = &complex_copy_methods,
  249. .get_size = complex_get_size,
  250. .footprint = complex_footprint,
  251. .interfaceid = -1,
  252. .interface_size = sizeof(struct starpu_complex_interface),
  253. };
  254. void starpu_complex_data_register(starpu_data_handle_t *handleptr, uint32_t home_node, double *real, double *imaginary, int nx)
  255. {
  256. struct starpu_complex_interface complex =
  257. {
  258. .real = real,
  259. .imaginary = imaginary,
  260. .nx = nx
  261. };
  262. if (interface_complex_ops.interfaceid == -1)
  263. {
  264. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  265. }
  266. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  267. }