complex_interface.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2012-2015,2017 CNRS
  5. * Copyright (C) 2013-2015 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.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, STARPU_MAIN_RAM);
  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, STARPU_MAIN_RAM);
  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, STARPU_MAIN_RAM);
  36. return complex_interface->nx;
  37. }
  38. static void complex_register_data_handle(starpu_data_handle_t handle, unsigned 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->nx = complex_interface->nx;
  47. if (node == home_node)
  48. {
  49. local_interface->real = complex_interface->real;
  50. local_interface->imaginary = complex_interface->imaginary;
  51. }
  52. else
  53. {
  54. local_interface->real = 0;
  55. local_interface->imaginary = 0;
  56. }
  57. }
  58. }
  59. static starpu_ssize_t complex_allocate_data_on_node(void *data_interface, unsigned node)
  60. {
  61. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  62. double *addr_real = 0;
  63. double *addr_imaginary = 0;
  64. starpu_ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  65. addr_real = (double*) starpu_malloc_on_node(node, requested_memory);
  66. if (!addr_real)
  67. goto fail_real;
  68. addr_imaginary = (double*) starpu_malloc_on_node(node, requested_memory);
  69. if (!addr_imaginary)
  70. goto fail_imaginary;
  71. /* update the data properly in consequence */
  72. complex_interface->real = addr_real;
  73. complex_interface->imaginary = addr_imaginary;
  74. return 2*requested_memory;
  75. fail_imaginary:
  76. starpu_free_on_node(node, (uintptr_t) addr_real, requested_memory);
  77. fail_real:
  78. return -ENOMEM;
  79. }
  80. static void complex_free_data_on_node(void *data_interface, unsigned node)
  81. {
  82. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  83. starpu_ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  84. starpu_free_on_node(node, (uintptr_t) complex_interface->real, requested_memory);
  85. starpu_free_on_node(node, (uintptr_t) complex_interface->imaginary, requested_memory);
  86. }
  87. static size_t complex_get_size(starpu_data_handle_t handle)
  88. {
  89. size_t size;
  90. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  91. size = complex_interface->nx * 2 * sizeof(double);
  92. return size;
  93. }
  94. static uint32_t complex_footprint(starpu_data_handle_t handle)
  95. {
  96. return starpu_hash_crc32c_be(starpu_complex_get_nx(handle), 0);
  97. }
  98. static int complex_pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  99. {
  100. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  101. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *)
  102. starpu_data_get_interface_on_node(handle, node);
  103. *count = complex_get_size(handle);
  104. if (ptr != NULL)
  105. {
  106. char *data;
  107. starpu_malloc_flags((void**) &data, *count, 0);
  108. *ptr = data;
  109. memcpy(data, complex_interface->real, complex_interface->nx*sizeof(double));
  110. memcpy(data+complex_interface->nx*sizeof(double), complex_interface->imaginary, complex_interface->nx*sizeof(double));
  111. }
  112. return 0;
  113. }
  114. static int complex_unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  115. {
  116. char *data = ptr;
  117. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  118. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *)
  119. starpu_data_get_interface_on_node(handle, node);
  120. STARPU_ASSERT(count == 2 * complex_interface->nx * sizeof(double));
  121. memcpy(complex_interface->real, data, complex_interface->nx*sizeof(double));
  122. memcpy(complex_interface->imaginary, data+complex_interface->nx*sizeof(double), complex_interface->nx*sizeof(double));
  123. return 0;
  124. }
  125. static starpu_ssize_t complex_describe(void *data_interface, char *buf, size_t size)
  126. {
  127. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  128. return snprintf(buf, size, "Complex%d", complex_interface->nx);
  129. }
  130. static int copy_any_to_any(void *src_interface, unsigned src_node,
  131. void *dst_interface, unsigned dst_node,
  132. void *async_data)
  133. {
  134. struct starpu_complex_interface *src_complex = src_interface;
  135. struct starpu_complex_interface *dst_complex = dst_interface;
  136. int ret = 0;
  137. if (starpu_interface_copy((uintptr_t) src_complex->real, 0, src_node,
  138. (uintptr_t) dst_complex->real, 0, dst_node,
  139. src_complex->nx*sizeof(src_complex->real[0]),
  140. async_data))
  141. ret = -EAGAIN;
  142. if (starpu_interface_copy((uintptr_t) src_complex->imaginary, 0, src_node,
  143. (uintptr_t) dst_complex->imaginary, 0, dst_node,
  144. src_complex->nx*sizeof(src_complex->imaginary[0]),
  145. async_data))
  146. ret = -EAGAIN;
  147. return ret;
  148. }
  149. static const struct starpu_data_copy_methods complex_copy_methods =
  150. {
  151. .any_to_any = copy_any_to_any
  152. };
  153. static struct starpu_data_interface_ops interface_complex_ops =
  154. {
  155. .register_data_handle = complex_register_data_handle,
  156. .allocate_data_on_node = complex_allocate_data_on_node,
  157. .free_data_on_node = complex_free_data_on_node,
  158. .copy_methods = &complex_copy_methods,
  159. .get_size = complex_get_size,
  160. .footprint = complex_footprint,
  161. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  162. .interface_size = sizeof(struct starpu_complex_interface),
  163. .handle_to_pointer = NULL,
  164. .pack_data = complex_pack_data,
  165. .unpack_data = complex_unpack_data,
  166. .describe = complex_describe
  167. };
  168. void starpu_complex_data_register(starpu_data_handle_t *handleptr, unsigned home_node, double *real, double *imaginary, int nx)
  169. {
  170. struct starpu_complex_interface complex =
  171. {
  172. .real = real,
  173. .imaginary = imaginary,
  174. .nx = nx
  175. };
  176. if (interface_complex_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  177. {
  178. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  179. }
  180. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  181. }