complex_interface.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. static int complex_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  19. {
  20. (void)node;
  21. struct starpu_complex_interface *complex_interface = data_interface;
  22. return ((char*) ptr >= (char*) &complex_interface->real &&
  23. (char*) ptr < (char*) (&complex_interface->real + 1))
  24. || ((char*) ptr >= (char*) &complex_interface->imaginary &&
  25. (char*) ptr < (char*) (&complex_interface->imaginary + 1));
  26. }
  27. double *starpu_complex_get_real(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, STARPU_MAIN_RAM);
  31. return complex_interface->real;
  32. }
  33. double *starpu_complex_get_imaginary(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, STARPU_MAIN_RAM);
  37. return complex_interface->imaginary;
  38. }
  39. int starpu_complex_get_nx(starpu_data_handle_t handle)
  40. {
  41. struct starpu_complex_interface *complex_interface =
  42. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  43. return complex_interface->nx;
  44. }
  45. static void complex_register_data_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  46. {
  47. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  48. unsigned node;
  49. for (node = 0; node < STARPU_MAXNODES; node++)
  50. {
  51. struct starpu_complex_interface *local_interface = (struct starpu_complex_interface *)
  52. starpu_data_get_interface_on_node(handle, node);
  53. local_interface->nx = complex_interface->nx;
  54. if (node == home_node)
  55. {
  56. local_interface->real = complex_interface->real;
  57. local_interface->imaginary = complex_interface->imaginary;
  58. }
  59. else
  60. {
  61. local_interface->real = NULL;
  62. local_interface->imaginary = NULL;
  63. }
  64. }
  65. }
  66. static starpu_ssize_t complex_allocate_data_on_node(void *data_interface, unsigned node)
  67. {
  68. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  69. double *addr_real = NULL;
  70. double *addr_imaginary = NULL;
  71. starpu_ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  72. addr_real = (double*) starpu_malloc_on_node(node, requested_memory);
  73. if (!addr_real)
  74. goto fail_real;
  75. addr_imaginary = (double*) starpu_malloc_on_node(node, requested_memory);
  76. if (!addr_imaginary)
  77. goto fail_imaginary;
  78. /* update the data properly in consequence */
  79. complex_interface->real = addr_real;
  80. complex_interface->imaginary = addr_imaginary;
  81. return 2*requested_memory;
  82. fail_imaginary:
  83. starpu_free_on_node(node, (uintptr_t) addr_real, requested_memory);
  84. fail_real:
  85. return -ENOMEM;
  86. }
  87. static void complex_free_data_on_node(void *data_interface, unsigned node)
  88. {
  89. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  90. starpu_ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  91. starpu_free_on_node(node, (uintptr_t) complex_interface->real, requested_memory);
  92. starpu_free_on_node(node, (uintptr_t) complex_interface->imaginary, requested_memory);
  93. }
  94. static size_t complex_get_size(starpu_data_handle_t handle)
  95. {
  96. size_t size;
  97. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  98. size = complex_interface->nx * 2 * sizeof(double);
  99. return size;
  100. }
  101. static uint32_t complex_footprint(starpu_data_handle_t handle)
  102. {
  103. return starpu_hash_crc32c_be(starpu_complex_get_nx(handle), 0);
  104. }
  105. static int complex_pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  106. {
  107. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  108. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *)
  109. starpu_data_get_interface_on_node(handle, node);
  110. *count = complex_get_size(handle);
  111. if (ptr != NULL)
  112. {
  113. char *data;
  114. data = (void*) starpu_malloc_on_node_flags(node, *count, 0);
  115. *ptr = data;
  116. memcpy(data, complex_interface->real, complex_interface->nx*sizeof(double));
  117. memcpy(data+complex_interface->nx*sizeof(double), complex_interface->imaginary, complex_interface->nx*sizeof(double));
  118. }
  119. return 0;
  120. }
  121. static int complex_unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  122. {
  123. char *data = ptr;
  124. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  125. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *)
  126. starpu_data_get_interface_on_node(handle, node);
  127. STARPU_ASSERT(count == 2 * complex_interface->nx * sizeof(double));
  128. memcpy(complex_interface->real, data, complex_interface->nx*sizeof(double));
  129. memcpy(complex_interface->imaginary, data+complex_interface->nx*sizeof(double), complex_interface->nx*sizeof(double));
  130. starpu_free_on_node_flags(node, (uintptr_t) ptr, count, 0);
  131. return 0;
  132. }
  133. static starpu_ssize_t complex_describe(void *data_interface, char *buf, size_t size)
  134. {
  135. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  136. return snprintf(buf, size, "Complex%d", complex_interface->nx);
  137. }
  138. static int copy_any_to_any(void *src_interface, unsigned src_node,
  139. void *dst_interface, unsigned dst_node,
  140. void *async_data)
  141. {
  142. struct starpu_complex_interface *src_complex = src_interface;
  143. struct starpu_complex_interface *dst_complex = dst_interface;
  144. int ret = 0;
  145. if (starpu_interface_copy((uintptr_t) src_complex->real, 0, src_node,
  146. (uintptr_t) dst_complex->real, 0, dst_node,
  147. src_complex->nx*sizeof(src_complex->real[0]),
  148. async_data))
  149. ret = -EAGAIN;
  150. if (starpu_interface_copy((uintptr_t) src_complex->imaginary, 0, src_node,
  151. (uintptr_t) dst_complex->imaginary, 0, dst_node,
  152. src_complex->nx*sizeof(src_complex->imaginary[0]),
  153. async_data))
  154. ret = -EAGAIN;
  155. return ret;
  156. }
  157. static const struct starpu_data_copy_methods complex_copy_methods =
  158. {
  159. .any_to_any = copy_any_to_any
  160. };
  161. static struct starpu_data_interface_ops interface_complex_ops =
  162. {
  163. .register_data_handle = complex_register_data_handle,
  164. .allocate_data_on_node = complex_allocate_data_on_node,
  165. .free_data_on_node = complex_free_data_on_node,
  166. .copy_methods = &complex_copy_methods,
  167. .get_size = complex_get_size,
  168. .footprint = complex_footprint,
  169. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  170. .interface_size = sizeof(struct starpu_complex_interface),
  171. .to_pointer = NULL,
  172. .pointer_is_inside = complex_pointer_is_inside,
  173. .pack_data = complex_pack_data,
  174. .unpack_data = complex_unpack_data,
  175. .describe = complex_describe
  176. };
  177. void starpu_complex_data_register(starpu_data_handle_t *handleptr, unsigned home_node, double *real, double *imaginary, int nx)
  178. {
  179. struct starpu_complex_interface complex =
  180. {
  181. .real = real,
  182. .imaginary = imaginary,
  183. .nx = nx
  184. };
  185. if (interface_complex_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  186. {
  187. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  188. }
  189. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  190. }