data_lookup.c 6.1 KB

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