complex_interface.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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, 2018 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. static int complex_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  21. {
  22. struct starpu_complex_interface *complex_interface = data_interface;
  23. return ((char*) ptr >= (char*) &complex_interface->real &&
  24. (char*) ptr < (char*) (&complex_interface->real + 1))
  25. || ((char*) ptr >= (char*) &complex_interface->imaginary &&
  26. (char*) ptr < (char*) (&complex_interface->imaginary + 1));
  27. }
  28. double *starpu_complex_get_real(starpu_data_handle_t handle)
  29. {
  30. struct starpu_complex_interface *complex_interface =
  31. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  32. return complex_interface->real;
  33. }
  34. double *starpu_complex_get_imaginary(starpu_data_handle_t handle)
  35. {
  36. struct starpu_complex_interface *complex_interface =
  37. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  38. return complex_interface->imaginary;
  39. }
  40. int starpu_complex_get_nx(starpu_data_handle_t handle)
  41. {
  42. struct starpu_complex_interface *complex_interface =
  43. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  44. return complex_interface->nx;
  45. }
  46. static void complex_register_data_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  47. {
  48. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  49. unsigned node;
  50. for (node = 0; node < STARPU_MAXNODES; node++)
  51. {
  52. struct starpu_complex_interface *local_interface = (struct starpu_complex_interface *)
  53. starpu_data_get_interface_on_node(handle, node);
  54. local_interface->nx = complex_interface->nx;
  55. if (node == home_node)
  56. {
  57. local_interface->real = complex_interface->real;
  58. local_interface->imaginary = complex_interface->imaginary;
  59. }
  60. else
  61. {
  62. local_interface->real = 0;
  63. local_interface->imaginary = 0;
  64. }
  65. }
  66. }
  67. static starpu_ssize_t complex_allocate_data_on_node(void *data_interface, unsigned node)
  68. {
  69. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  70. double *addr_real = 0;
  71. double *addr_imaginary = 0;
  72. starpu_ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  73. addr_real = (double*) starpu_malloc_on_node(node, requested_memory);
  74. if (!addr_real)
  75. goto fail_real;
  76. addr_imaginary = (double*) starpu_malloc_on_node(node, requested_memory);
  77. if (!addr_imaginary)
  78. goto fail_imaginary;
  79. /* update the data properly in consequence */
  80. complex_interface->real = addr_real;
  81. complex_interface->imaginary = addr_imaginary;
  82. return 2*requested_memory;
  83. fail_imaginary:
  84. starpu_free_on_node(node, (uintptr_t) addr_real, requested_memory);
  85. fail_real:
  86. return -ENOMEM;
  87. }
  88. static void complex_free_data_on_node(void *data_interface, unsigned node)
  89. {
  90. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) data_interface;
  91. starpu_ssize_t requested_memory = complex_interface->nx * sizeof(complex_interface->real[0]);
  92. starpu_free_on_node(node, (uintptr_t) complex_interface->real, requested_memory);
  93. starpu_free_on_node(node, (uintptr_t) complex_interface->imaginary, requested_memory);
  94. }
  95. static size_t complex_get_size(starpu_data_handle_t handle)
  96. {
  97. size_t size;
  98. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  99. size = complex_interface->nx * 2 * sizeof(double);
  100. return size;
  101. }
  102. static uint32_t complex_footprint(starpu_data_handle_t handle)
  103. {
  104. return starpu_hash_crc32c_be(starpu_complex_get_nx(handle), 0);
  105. }
  106. static int complex_pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_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. *count = complex_get_size(handle);
  112. if (ptr != NULL)
  113. {
  114. char *data;
  115. starpu_malloc_flags((void**) &data, *count, 0);
  116. *ptr = data;
  117. memcpy(data, complex_interface->real, complex_interface->nx*sizeof(double));
  118. memcpy(data+complex_interface->nx*sizeof(double), complex_interface->imaginary, complex_interface->nx*sizeof(double));
  119. }
  120. return 0;
  121. }
  122. static int complex_unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  123. {
  124. char *data = ptr;
  125. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  126. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *)
  127. starpu_data_get_interface_on_node(handle, node);
  128. STARPU_ASSERT(count == 2 * complex_interface->nx * sizeof(double));
  129. memcpy(complex_interface->real, data, complex_interface->nx*sizeof(double));
  130. memcpy(complex_interface->imaginary, data+complex_interface->nx*sizeof(double), complex_interface->nx*sizeof(double));
  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. }