complex_interface.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, enum cudaMemcpyKind kind)
  143. {
  144. struct starpu_complex_interface *src_complex = src_interface;
  145. struct starpu_complex_interface *dst_complex = dst_interface;
  146. cudaError_t cures;
  147. cures = cudaMemcpy((void *)dst_complex->real, (void *)src_complex->real, src_complex->nx*sizeof(src_complex->real[0]), kind);
  148. if (STARPU_UNLIKELY(cures))
  149. STARPU_CUDA_REPORT_ERROR(cures);
  150. cures = cudaMemcpy((char *)dst_complex->imaginary, (char *)src_complex->imaginary, src_complex->nx*sizeof(src_complex->imaginary[0]), kind);
  151. if (STARPU_UNLIKELY(cures))
  152. STARPU_CUDA_REPORT_ERROR(cures);
  153. return 0;
  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_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  158. }
  159. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  160. {
  161. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  162. }
  163. #endif
  164. #ifdef STARPU_USE_OPENCL
  165. static int copy_ram_to_opencl(void *src_interface, unsigned src_node,
  166. void *dst_interface, unsigned dst_node)
  167. {
  168. struct starpu_complex_interface *src_complex = src_interface;
  169. struct starpu_complex_interface *dst_complex = dst_interface;
  170. cl_int err;
  171. err = starpu_opencl_copy_ram_to_opencl(
  172. src_complex->real,
  173. src_node,
  174. (cl_mem) dst_complex->real,
  175. dst_node,
  176. src_complex->nx * sizeof(src_complex->real[0]),
  177. 0,
  178. NULL);
  179. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  180. STARPU_OPENCL_REPORT_ERROR(err);
  181. err = starpu_opencl_copy_ram_to_opencl(
  182. src_complex->imaginary,
  183. src_node,
  184. (cl_mem) dst_complex->imaginary,
  185. dst_node,
  186. src_complex->nx * sizeof(src_complex->imaginary[0]),
  187. 0,
  188. NULL);
  189. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  190. STARPU_OPENCL_REPORT_ERROR(err);
  191. return 0;
  192. }
  193. static int copy_opencl_to_ram(void *src_interface, unsigned src_node,
  194. void *dst_interface, unsigned dst_node)
  195. {
  196. struct starpu_complex_interface *src_complex = src_interface;
  197. struct starpu_complex_interface *dst_complex = dst_interface;
  198. cl_int err;
  199. err = starpu_opencl_copy_opencl_to_ram(
  200. (cl_mem) src_complex->real,
  201. src_node,
  202. dst_complex->real,
  203. dst_node,
  204. src_complex->nx * sizeof(src_complex->real[0]),
  205. 0,
  206. NULL);
  207. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  208. STARPU_OPENCL_REPORT_ERROR(err);
  209. err = starpu_opencl_copy_opencl_to_ram(
  210. (cl_mem) src_complex->imaginary,
  211. src_node,
  212. dst_complex->imaginary,
  213. dst_node,
  214. src_complex->nx * sizeof(src_complex->imaginary[0]),
  215. 0,
  216. NULL);
  217. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  218. STARPU_OPENCL_REPORT_ERROR(err);
  219. return 0;
  220. }
  221. #endif
  222. static const struct starpu_data_copy_methods complex_copy_methods =
  223. {
  224. #ifdef STARPU_USE_CUDA
  225. .ram_to_cuda = copy_ram_to_cuda,
  226. .cuda_to_ram = copy_cuda_to_ram,
  227. #endif
  228. #ifdef STARPU_USE_OPENCL
  229. .ram_to_opencl = copy_ram_to_opencl,
  230. .opencl_to_ram = copy_opencl_to_ram,
  231. #endif
  232. };
  233. static struct starpu_data_interface_ops interface_complex_ops =
  234. {
  235. .register_data_handle = complex_register_data_handle,
  236. .allocate_data_on_node = complex_allocate_data_on_node,
  237. .copy_methods = &complex_copy_methods,
  238. .get_size = complex_get_size,
  239. .footprint = complex_footprint,
  240. .interfaceid = -1,
  241. .interface_size = sizeof(struct starpu_complex_interface),
  242. };
  243. void starpu_complex_data_register(starpu_data_handle_t *handleptr, uint32_t home_node, double *real, double *imaginary, int nx)
  244. {
  245. struct starpu_complex_interface complex =
  246. {
  247. .real = real,
  248. .imaginary = imaginary,
  249. .nx = nx
  250. };
  251. if (interface_complex_ops.interfaceid == -1)
  252. {
  253. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  254. }
  255. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  256. }