data_lookup.c 6.1 KB

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