complex_interface.c 7.5 KB

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