data_lookup.c 6.3 KB

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