data_lookup.c 5.8 KB

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