data_lookup.c 5.1 KB

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