variable_interface.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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 const struct starpu_data_copy_methods variable_copy_data_methods_s =
  28. {
  29. .any_to_any = copy_any_to_any,
  30. };
  31. static void register_variable_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  32. static starpu_ssize_t allocate_variable_buffer_on_node(void *data_interface_, unsigned dst_node);
  33. static void *variable_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  34. static void free_variable_buffer_on_node(void *data_interface, unsigned node);
  35. static size_t variable_interface_get_size(starpu_data_handle_t handle);
  36. static uint32_t footprint_variable_interface_crc32(starpu_data_handle_t handle);
  37. static int variable_compare(void *data_interface_a, void *data_interface_b);
  38. static void display_variable_interface(starpu_data_handle_t handle, FILE *f);
  39. static struct starpu_data_interface_ops interface_variable_ops =
  40. {
  41. .register_data_handle = register_variable_handle,
  42. .allocate_data_on_node = allocate_variable_buffer_on_node,
  43. .handle_to_pointer = variable_handle_to_pointer,
  44. .free_data_on_node = free_variable_buffer_on_node,
  45. .copy_methods = &variable_copy_data_methods_s,
  46. .get_size = variable_interface_get_size,
  47. .footprint = footprint_variable_interface_crc32,
  48. .compare = variable_compare,
  49. .interfaceid = STARPU_VARIABLE_INTERFACE_ID,
  50. .interface_size = sizeof(struct starpu_variable_interface),
  51. .display = display_variable_interface,
  52. };
  53. static void *variable_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  54. {
  55. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  56. return (void*) STARPU_VARIABLE_GET_PTR(starpu_data_get_interface_on_node(handle, node));
  57. }
  58. static void register_variable_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  59. {
  60. unsigned node;
  61. for (node = 0; node < STARPU_MAXNODES; node++)
  62. {
  63. struct starpu_variable_interface *local_interface = (struct starpu_variable_interface *)
  64. starpu_data_get_interface_on_node(handle, node);
  65. if (node == home_node)
  66. {
  67. local_interface->ptr = STARPU_VARIABLE_GET_PTR(data_interface);
  68. }
  69. else
  70. {
  71. local_interface->ptr = 0;
  72. }
  73. local_interface->elemsize = STARPU_VARIABLE_GET_ELEMSIZE(data_interface);
  74. }
  75. }
  76. /* declare a new data with the variable interface */
  77. void starpu_variable_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  78. uintptr_t ptr, size_t elemsize)
  79. {
  80. struct starpu_variable_interface variable =
  81. {
  82. .ptr = ptr,
  83. .elemsize = elemsize
  84. };
  85. starpu_data_register(handleptr, home_node, &variable, &interface_variable_ops);
  86. }
  87. static uint32_t footprint_variable_interface_crc32(starpu_data_handle_t handle)
  88. {
  89. return starpu_crc32_be(starpu_variable_get_elemsize(handle), 0);
  90. }
  91. static int variable_compare(void *data_interface_a, void *data_interface_b)
  92. {
  93. struct starpu_variable_interface *variable_a = (struct starpu_variable_interface *) data_interface_a;
  94. struct starpu_variable_interface *variable_b = (struct starpu_variable_interface *) data_interface_b;
  95. /* Two variables are considered compatible if they have the same size */
  96. return (variable_a->elemsize == variable_b->elemsize);
  97. }
  98. static void display_variable_interface(starpu_data_handle_t handle, FILE *f)
  99. {
  100. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  101. starpu_data_get_interface_on_node(handle, 0);
  102. fprintf(f, "%ld\t", (long)variable_interface->elemsize);
  103. }
  104. static size_t variable_interface_get_size(starpu_data_handle_t handle)
  105. {
  106. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  107. starpu_data_get_interface_on_node(handle, 0);
  108. return variable_interface->elemsize;
  109. }
  110. uintptr_t starpu_variable_get_local_ptr(starpu_data_handle_t handle)
  111. {
  112. unsigned node;
  113. node = _starpu_memory_node_get_local_key();
  114. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  115. return STARPU_VARIABLE_GET_PTR(starpu_data_get_interface_on_node(handle, node));
  116. }
  117. size_t starpu_variable_get_elemsize(starpu_data_handle_t handle)
  118. {
  119. return STARPU_VARIABLE_GET_ELEMSIZE(starpu_data_get_interface_on_node(handle, 0));
  120. }
  121. /* memory allocation/deallocation primitives for the variable interface */
  122. /* returns the size of the allocated area */
  123. static starpu_ssize_t allocate_variable_buffer_on_node(void *data_interface_, unsigned dst_node)
  124. {
  125. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *) data_interface_;
  126. size_t elemsize = variable_interface->elemsize;
  127. uintptr_t addr = starpu_malloc_on_node(dst_node, elemsize);
  128. if (!addr)
  129. return -ENOMEM;
  130. /* update the data properly in consequence */
  131. variable_interface->ptr = addr;
  132. return elemsize;
  133. }
  134. static void free_variable_buffer_on_node(void *data_interface, unsigned node)
  135. {
  136. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *) data_interface;
  137. starpu_free_on_node(node, variable_interface->ptr, variable_interface->elemsize);
  138. }
  139. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  140. {
  141. struct starpu_variable_interface *src_variable = (struct starpu_variable_interface *) src_interface;
  142. struct starpu_variable_interface *dst_variable = (struct starpu_variable_interface *) dst_interface;
  143. size_t elemsize = dst_variable->elemsize;
  144. uintptr_t ptr_src = src_variable->ptr;
  145. uintptr_t ptr_dst = dst_variable->ptr;
  146. int ret;
  147. ret = starpu_interface_copy(ptr_src, 0, src_node, ptr_dst, 0, dst_node, elemsize, async_data);
  148. _STARPU_TRACE_DATA_COPY(src_node, dst_node, elemsize);
  149. return ret;
  150. }