vector_interface.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <datawizard/coherency.h>
  20. #include <datawizard/copy_driver.h>
  21. #include <datawizard/filters.h>
  22. #include <starpu_hash.h>
  23. #include <starpu_cuda.h>
  24. #include <starpu_opencl.h>
  25. #include <drivers/opencl/driver_opencl.h>
  26. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  27. static struct starpu_data_copy_methods vector_copy_data_methods_s =
  28. {
  29. .any_to_any = copy_any_to_any,
  30. };
  31. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  32. static ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node);
  33. static void *vector_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  34. static void free_vector_buffer_on_node(void *data_interface, unsigned node);
  35. static size_t vector_interface_get_size(starpu_data_handle_t handle);
  36. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle);
  37. static int vector_compare(void *data_interface_a, void *data_interface_b);
  38. static void display_vector_interface(starpu_data_handle_t handle, FILE *f);
  39. static struct starpu_data_interface_ops interface_vector_ops =
  40. {
  41. .register_data_handle = register_vector_handle,
  42. .allocate_data_on_node = allocate_vector_buffer_on_node,
  43. .handle_to_pointer = vector_handle_to_pointer,
  44. .free_data_on_node = free_vector_buffer_on_node,
  45. .copy_methods = &vector_copy_data_methods_s,
  46. .get_size = vector_interface_get_size,
  47. .footprint = footprint_vector_interface_crc32,
  48. .compare = vector_compare,
  49. .interfaceid = STARPU_VECTOR_INTERFACE_ID,
  50. .interface_size = sizeof(struct starpu_vector_interface),
  51. .display = display_vector_interface,
  52. };
  53. static void *vector_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  54. {
  55. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  56. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  57. starpu_data_get_interface_on_node(handle, node);
  58. return (void*) vector_interface->ptr;
  59. }
  60. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  61. {
  62. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  63. unsigned node;
  64. for (node = 0; node < STARPU_MAXNODES; node++)
  65. {
  66. struct starpu_vector_interface *local_interface = (struct starpu_vector_interface *)
  67. starpu_data_get_interface_on_node(handle, node);
  68. if (node == home_node)
  69. {
  70. local_interface->ptr = vector_interface->ptr;
  71. local_interface->dev_handle = vector_interface->dev_handle;
  72. local_interface->offset = vector_interface->offset;
  73. }
  74. else
  75. {
  76. local_interface->ptr = 0;
  77. local_interface->dev_handle = 0;
  78. local_interface->offset = 0;
  79. }
  80. local_interface->nx = vector_interface->nx;
  81. local_interface->elemsize = vector_interface->elemsize;
  82. }
  83. }
  84. /* declare a new data with the vector interface */
  85. void starpu_vector_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  86. uintptr_t ptr, uint32_t nx, size_t elemsize)
  87. {
  88. struct starpu_vector_interface vector =
  89. {
  90. .ptr = ptr,
  91. .nx = nx,
  92. .elemsize = elemsize,
  93. .dev_handle = ptr,
  94. .offset = 0
  95. };
  96. starpu_data_register(handleptr, home_node, &vector, &interface_vector_ops);
  97. }
  98. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle)
  99. {
  100. return starpu_crc32_be(starpu_vector_get_nx(handle), 0);
  101. }
  102. static int vector_compare(void *data_interface_a, void *data_interface_b)
  103. {
  104. struct starpu_vector_interface *vector_a = (struct starpu_vector_interface *) data_interface_a;
  105. struct starpu_vector_interface *vector_b = (struct starpu_vector_interface *) data_interface_b;
  106. /* Two vectors are considered compatible if they have the same size */
  107. return ((vector_a->nx == vector_b->nx)
  108. && (vector_a->elemsize == vector_b->elemsize));
  109. }
  110. static void display_vector_interface(starpu_data_handle_t handle, FILE *f)
  111. {
  112. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  113. starpu_data_get_interface_on_node(handle, 0);
  114. fprintf(f, "%u\t", vector_interface->nx);
  115. }
  116. static size_t vector_interface_get_size(starpu_data_handle_t handle)
  117. {
  118. size_t size;
  119. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  120. starpu_data_get_interface_on_node(handle, 0);
  121. size = vector_interface->nx*vector_interface->elemsize;
  122. return size;
  123. }
  124. /* offer an access to the data parameters */
  125. uint32_t starpu_vector_get_nx(starpu_data_handle_t handle)
  126. {
  127. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  128. starpu_data_get_interface_on_node(handle, 0);
  129. return vector_interface->nx;
  130. }
  131. uintptr_t starpu_vector_get_local_ptr(starpu_data_handle_t handle)
  132. {
  133. unsigned node;
  134. node = _starpu_memory_node_get_local_key();
  135. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  136. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  137. starpu_data_get_interface_on_node(handle, node);
  138. return vector_interface->ptr;
  139. }
  140. size_t starpu_vector_get_elemsize(starpu_data_handle_t handle)
  141. {
  142. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  143. starpu_data_get_interface_on_node(handle, 0);
  144. return vector_interface->elemsize;
  145. }
  146. /* memory allocation/deallocation primitives for the vector interface */
  147. /* returns the size of the allocated area */
  148. static ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node)
  149. {
  150. uintptr_t addr = 0, handle;
  151. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface_;
  152. uint32_t nx = vector_interface->nx;
  153. size_t elemsize = vector_interface->elemsize;
  154. ssize_t allocated_memory;
  155. handle = starpu_allocate_buffer_on_node(dst_node, nx*elemsize);
  156. if (!handle)
  157. return -ENOMEM;
  158. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  159. addr = handle;
  160. allocated_memory = nx*elemsize;
  161. /* update the data properly in consequence */
  162. vector_interface->ptr = addr;
  163. vector_interface->dev_handle = handle;
  164. vector_interface->offset = 0;
  165. return allocated_memory;
  166. }
  167. static void free_vector_buffer_on_node(void *data_interface, unsigned node)
  168. {
  169. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  170. uint32_t nx = vector_interface->nx;
  171. size_t elemsize = vector_interface->elemsize;
  172. starpu_free_buffer_on_node(node, vector_interface->ptr, nx*elemsize);
  173. }
  174. static int copy_any_to_any(void *src_interface, unsigned src_node,
  175. void *dst_interface, unsigned dst_node, void *async_data)
  176. {
  177. struct starpu_vector_interface *src_vector = src_interface;
  178. struct starpu_vector_interface *dst_vector = dst_interface;
  179. int ret;
  180. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  181. dst_vector->dev_handle, dst_vector->offset, dst_node,
  182. src_vector->nx*src_vector->elemsize, async_data);
  183. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  184. return ret;
  185. }