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