data_lookup.c 6.0 KB

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