data_lookup.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 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. #undef NDEBUG
  17. #include <assert.h>
  18. #include <starpu.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include "../helper.h"
  22. /*
  23. * Check that starpu_data_lookup returns the proper data handle, given
  24. * the registered buffer address
  25. */
  26. void task(void **buffers, void *args)
  27. {
  28. float *numbers;
  29. size_t size, i;
  30. numbers = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  31. starpu_codelet_unpack_args (args, &size);
  32. for(i = 0; i < size; i++)
  33. {
  34. numbers[i] = i;
  35. }
  36. }
  37. static struct starpu_codelet cl =
  38. {
  39. .cpu_funcs = {task},
  40. .cpu_funcs_name = {"task"},
  41. .nbuffers = 1,
  42. .modes = {STARPU_W}
  43. };
  44. static int test_lazy_allocation(void)
  45. {
  46. static const size_t count = 123;
  47. size_t i;
  48. void *pointer;
  49. starpu_data_handle_t handle;
  50. int ret;
  51. /* Lazily-allocated vector. */
  52. starpu_vector_data_register(&handle, -1, 0 /* NULL */,
  53. count, sizeof(float));
  54. ret = starpu_task_insert(&cl,
  55. STARPU_W, handle,
  56. STARPU_VALUE, &count, sizeof(size_t),
  57. 0);
  58. if (ret == -ENODEV) return ret;
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  60. /* yes, we do not perform the computation but we did detect that no one
  61. * could perform the kernel, so this is not an error from StarPU */
  62. /* Acquire the handle, forcing a local allocation. */
  63. ret = starpu_data_acquire(handle, STARPU_R);
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  65. /* Make sure we have a local pointer to it. */
  66. pointer = starpu_data_get_local_ptr(handle);
  67. STARPU_ASSERT(pointer != NULL);
  68. for(i = 0; i < count; i++)
  69. {
  70. float *numbers = (float *)pointer;
  71. STARPU_ASSERT(numbers[i] == i);
  72. }
  73. /* Make sure the pointer/handle mapping is up-to-date. */
  74. STARPU_ASSERT(starpu_data_lookup(pointer) == handle);
  75. starpu_data_release(handle);
  76. starpu_data_unregister(handle);
  77. STARPU_ASSERT(starpu_data_lookup(pointer) == NULL);
  78. return 0;
  79. }
  80. #define VECTOR_COUNT 12
  81. #define VARIABLE_COUNT 42
  82. #define VECTOR_SIZE 123
  83. static void test_filters(void)
  84. {
  85. #define CHILDREN_COUNT 10
  86. int ret, i;
  87. int *ptr, *children_pointers[CHILDREN_COUNT];
  88. starpu_data_handle_t handle;
  89. ret = starpu_malloc((void**)&ptr, VECTOR_SIZE * sizeof(*ptr));
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  91. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)ptr,
  92. VECTOR_SIZE, sizeof(*ptr));
  93. struct starpu_data_filter f =
  94. {
  95. .filter_func = starpu_vector_filter_block,
  96. .nchildren = CHILDREN_COUNT
  97. };
  98. starpu_data_partition(handle, &f);
  99. STARPU_ASSERT(starpu_data_get_nb_children(handle) == CHILDREN_COUNT);
  100. for (i = 0; i < CHILDREN_COUNT; i++)
  101. {
  102. starpu_data_handle_t child;
  103. child = starpu_data_get_sub_data(handle, 1, i);
  104. children_pointers[i] = (int *) starpu_data_get_local_ptr(child);
  105. STARPU_ASSERT(children_pointers[i] != NULL);
  106. /* Make sure we have a pointer -> handle mapping for CHILD. */
  107. STARPU_ASSERT(starpu_data_lookup(children_pointers[i]) == child);
  108. }
  109. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  110. for (i = 0; i < CHILDREN_COUNT; i++)
  111. {
  112. if (children_pointers[i] != ptr)
  113. /* Make sure the pointer -> handle mapping is gone. */
  114. STARPU_ASSERT(starpu_data_lookup(children_pointers[i]) == NULL);
  115. }
  116. /* Make sure the parent's mapping is back. */
  117. STARPU_ASSERT(starpu_data_lookup(ptr) == handle);
  118. starpu_data_unregister(handle);
  119. starpu_free_noflag(ptr, VECTOR_SIZE * sizeof(*ptr));
  120. #undef CHILDREN_COUNT
  121. }
  122. int main(int argc, char *argv[])
  123. {
  124. int ret;
  125. size_t i;
  126. void *vectors[VECTOR_COUNT], *variables[VARIABLE_COUNT];
  127. starpu_data_handle_t vector_handles[VECTOR_COUNT];
  128. starpu_data_handle_t variable_handles[VARIABLE_COUNT];
  129. ret = starpu_initialize(NULL, &argc, &argv);
  130. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  131. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  132. /* Register data regions. */
  133. for(i = 0; i < VARIABLE_COUNT; i++)
  134. {
  135. ret = starpu_malloc(&variables[i], sizeof(float));
  136. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  137. starpu_variable_data_register(&variable_handles[i], STARPU_MAIN_RAM,
  138. (uintptr_t)variables[i],
  139. sizeof(float));
  140. }
  141. for(i = 0; i < VECTOR_COUNT; i++)
  142. {
  143. ret = starpu_malloc(&vectors[i], VECTOR_SIZE * sizeof(float));
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  145. starpu_vector_data_register(&vector_handles[i], STARPU_MAIN_RAM,
  146. (uintptr_t)vectors[i],
  147. VECTOR_SIZE, sizeof(float));
  148. }
  149. /* Look them up. */
  150. for(i = 0; i < VARIABLE_COUNT; i++)
  151. {
  152. starpu_data_handle_t handle;
  153. handle = starpu_data_lookup(variables[i]);
  154. STARPU_ASSERT(handle == variable_handles[i]);
  155. }
  156. for(i = 0; i < VECTOR_COUNT; i++)
  157. {
  158. starpu_data_handle_t handle;
  159. handle = starpu_data_lookup(vectors[i]);
  160. STARPU_ASSERT(handle == vector_handles[i]);
  161. }
  162. /* Unregister them. */
  163. for(i = 0; i < VARIABLE_COUNT; i++)
  164. {
  165. starpu_data_unregister(variable_handles[i]);
  166. }
  167. for(i = 0; i < VECTOR_COUNT; i++)
  168. {
  169. starpu_data_unregister(vector_handles[i]);
  170. }
  171. /* Make sure we can no longer find them. */
  172. for(i = 0; i < VARIABLE_COUNT; i++)
  173. {
  174. starpu_data_handle_t handle;
  175. handle = starpu_data_lookup(variables[i]);
  176. STARPU_ASSERT(handle == NULL);
  177. starpu_free_noflag(variables[i], sizeof(float));
  178. }
  179. for(i = 0; i < VECTOR_COUNT; i++)
  180. {
  181. starpu_data_handle_t handle;
  182. handle = starpu_data_lookup(vectors[i]);
  183. STARPU_ASSERT(handle == NULL);
  184. starpu_free_noflag(vectors[i], VECTOR_SIZE * sizeof(float));
  185. }
  186. ret = test_lazy_allocation();
  187. if (ret == -ENODEV) goto enodev;
  188. test_filters();
  189. starpu_shutdown();
  190. return EXIT_SUCCESS;
  191. enodev:
  192. fprintf(stderr, "WARNING: No one can execute this task\n");
  193. /* yes, we do not perform the computation but we did detect that no one
  194. * could perform the kernel, so this is not an error from StarPU */
  195. starpu_shutdown();
  196. return STARPU_TEST_SKIPPED;
  197. }