data_lookup.c 5.5 KB

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