complex_interface.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_hash.h>
  19. #include "complex_interface.h"
  20. double *starpu_complex_get_real(starpu_data_handle_t handle)
  21. {
  22. struct starpu_complex_interface *complex_interface =
  23. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  24. return complex_interface->real;
  25. }
  26. double *starpu_complex_get_imaginary(starpu_data_handle_t handle)
  27. {
  28. struct starpu_complex_interface *complex_interface =
  29. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  30. return complex_interface->imaginary;
  31. }
  32. int starpu_complex_get_nx(starpu_data_handle_t handle)
  33. {
  34. struct starpu_complex_interface *complex_interface =
  35. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  36. return complex_interface->nx;
  37. }
  38. static void complex_register_data_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface)
  39. {
  40. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  41. unsigned node;
  42. for (node = 0; node < STARPU_MAXNODES; node++)
  43. {
  44. struct starpu_complex_interface *local_interface = (struct starpu_complex_interface *)
  45. starpu_data_get_interface_on_node(handle, node);
  46. local_interface->real = complex_interface->real;
  47. local_interface->imaginary = complex_interface->imaginary;
  48. local_interface->nx = complex_interface->nx;
  49. }
  50. }
  51. static starpu_ssize_t complex_allocate_data_on_node(void *data_interface, uint32_t node)
  52. {
  53. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  54. unsigned fail = 0;
  55. double *addr_real = 0;
  56. double *addr_imaginary = 0;
  57. ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  58. enum starpu_node_kind kind = starpu_node_get_kind(node);
  59. switch(kind)
  60. {
  61. case STARPU_CPU_RAM:
  62. addr_real = malloc(requested_memory);
  63. addr_imaginary = malloc(requested_memory);
  64. if (!addr_real || !addr_imaginary)
  65. fail = 1;
  66. break;
  67. #ifdef STARPU_USE_CUDA
  68. case STARPU_CUDA_RAM:
  69. {
  70. cudaError_t status;
  71. status = cudaMalloc((void **)&addr_real, requested_memory);
  72. if (!addr_real || (status != cudaSuccess))
  73. {
  74. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  75. STARPU_CUDA_REPORT_ERROR(status);
  76. fail = 1;
  77. }
  78. else
  79. {
  80. status = cudaMalloc((void **)&addr_imaginary, requested_memory);
  81. if (!addr_imaginary || (status != cudaSuccess))
  82. {
  83. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  84. STARPU_CUDA_REPORT_ERROR(status);
  85. fail = 1;
  86. }
  87. }
  88. break;
  89. }
  90. #endif
  91. #ifdef STARPU_USE_OPENCL
  92. case STARPU_OPENCL_RAM:
  93. {
  94. STARPU_ASSERT(0);
  95. }
  96. #endif
  97. default:
  98. STARPU_ASSERT(0);
  99. }
  100. if (fail)
  101. return -ENOMEM;
  102. /* update the data properly in consequence */
  103. complex_interface->real = addr_real;
  104. complex_interface->imaginary = addr_imaginary;
  105. return 2*requested_memory;
  106. }
  107. static size_t complex_get_size(starpu_data_handle_t handle)
  108. {
  109. size_t size;
  110. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  111. size = complex_interface->nx * 2 * sizeof(double);
  112. return size;
  113. }
  114. static uint32_t complex_footprint(starpu_data_handle_t handle)
  115. {
  116. return starpu_crc32_be(starpu_complex_get_nx(handle), 0);
  117. }
  118. #ifdef STARPU_USE_CUDA
  119. static int copy_cuda_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, enum cudaMemcpyKind kind)
  120. {
  121. struct starpu_complex_interface *src_complex = src_interface;
  122. struct starpu_complex_interface *dst_complex = dst_interface;
  123. cudaError_t cures;
  124. cures = cudaMemcpy((void *)dst_complex->real, (void *)src_complex->real, src_complex->nx*sizeof(src_complex->real[0]), kind);
  125. if (STARPU_UNLIKELY(cures))
  126. STARPU_CUDA_REPORT_ERROR(cures);
  127. cures = cudaMemcpy((char *)dst_complex->imaginary, (char *)src_complex->imaginary, src_complex->nx*sizeof(src_complex->imaginary[0]), kind);
  128. if (STARPU_UNLIKELY(cures))
  129. STARPU_CUDA_REPORT_ERROR(cures);
  130. return 0;
  131. }
  132. static int copy_ram_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  133. {
  134. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  135. }
  136. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  137. {
  138. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  139. }
  140. #endif
  141. static const struct starpu_data_copy_methods complex_copy_methods =
  142. {
  143. #ifdef STARPU_USE_CUDA
  144. .ram_to_cuda = copy_ram_to_cuda,
  145. .cuda_to_ram = copy_cuda_to_ram,
  146. #endif
  147. };
  148. static struct starpu_data_interface_ops interface_complex_ops =
  149. {
  150. .register_data_handle = complex_register_data_handle,
  151. .allocate_data_on_node = complex_allocate_data_on_node,
  152. .copy_methods = &complex_copy_methods,
  153. .get_size = complex_get_size,
  154. .footprint = complex_footprint,
  155. .interfaceid = -1,
  156. .interface_size = sizeof(struct starpu_complex_interface),
  157. };
  158. void starpu_complex_data_register(starpu_data_handle_t *handleptr, uint32_t home_node, double *real, double *imaginary, int nx)
  159. {
  160. struct starpu_complex_interface complex =
  161. {
  162. .real = real,
  163. .imaginary = imaginary,
  164. .nx = nx
  165. };
  166. if (interface_complex_ops.interfaceid == -1)
  167. {
  168. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  169. }
  170. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  171. }