data_lookup.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. .get_nchildren = NULL,
  89. .get_child_ops = NULL
  90. };
  91. starpu_data_partition(handle, &f);
  92. assert(starpu_data_get_nb_children(handle) == CHILDREN_COUNT);
  93. for (i = 0; i < CHILDREN_COUNT; i++)
  94. {
  95. starpu_data_handle child;
  96. child = starpu_data_get_sub_data(handle, 1, i);
  97. children_pointers[i] = starpu_handle_get_local_ptr(child);
  98. assert(children_pointers[i] != NULL);
  99. /* Make sure we have a pointer -> handle mapping for CHILD. */
  100. assert(starpu_data_lookup(children_pointers[i]) == child);
  101. }
  102. starpu_data_unpartition(handle, 0);
  103. for (i = 0; i < CHILDREN_COUNT; i++)
  104. {
  105. if (children_pointers[i] != ptr)
  106. /* Make sure the pointer -> handle mapping is gone. */
  107. assert(starpu_data_lookup(children_pointers[i]) == NULL);
  108. }
  109. /* Make sure the parent's mapping is back. */
  110. assert(starpu_data_lookup(ptr) == handle);
  111. starpu_data_unregister(handle);
  112. starpu_free(ptr);
  113. #undef CHILDREN_COUNT
  114. }
  115. int main(int argc, char *argv[])
  116. {
  117. int err;
  118. size_t i;
  119. void *vectors[VECTOR_COUNT], *variables[VARIABLE_COUNT];
  120. starpu_data_handle vector_handles[VECTOR_COUNT];
  121. starpu_data_handle variable_handles[VARIABLE_COUNT];
  122. starpu_init(NULL);
  123. /* Register data regions. */
  124. for(i = 0; i < VARIABLE_COUNT; i++)
  125. {
  126. err = starpu_malloc(&variables[i], sizeof(float));
  127. assert(err == 0);
  128. starpu_variable_data_register(&variable_handles[i], 0,
  129. (uintptr_t)variables[i],
  130. sizeof(float));
  131. }
  132. for(i = 0; i < VECTOR_COUNT; i++)
  133. {
  134. err = starpu_malloc(&vectors[i], VECTOR_SIZE * sizeof(float));
  135. assert(err == 0);
  136. starpu_vector_data_register(&vector_handles[i], 0,
  137. (uintptr_t)vectors[i],
  138. VECTOR_SIZE, sizeof(float));
  139. }
  140. /* Look them up. */
  141. for(i = 0; i < VARIABLE_COUNT; i++)
  142. {
  143. starpu_data_handle handle;
  144. handle = starpu_data_lookup(variables[i]);
  145. assert(handle == variable_handles[i]);
  146. }
  147. for(i = 0; i < VECTOR_COUNT; i++)
  148. {
  149. starpu_data_handle handle;
  150. handle = starpu_data_lookup(vectors[i]);
  151. assert(handle == vector_handles[i]);
  152. }
  153. /* Unregister them. */
  154. for(i = 0; i < VARIABLE_COUNT; i++)
  155. {
  156. starpu_data_unregister(variable_handles[i]);
  157. }
  158. for(i = 0; i < VECTOR_COUNT; i++)
  159. {
  160. starpu_data_unregister(vector_handles[i]);
  161. }
  162. /* Make sure we can no longer find them. */
  163. for(i = 0; i < VARIABLE_COUNT; i++)
  164. {
  165. starpu_data_handle handle;
  166. handle = starpu_data_lookup(variables[i]);
  167. assert(handle == NULL);
  168. starpu_free(variables[i]);
  169. }
  170. for(i = 0; i < VECTOR_COUNT; i++)
  171. {
  172. starpu_data_handle handle;
  173. handle = starpu_data_lookup(vectors[i]);
  174. assert(handle == NULL);
  175. starpu_free(vectors[i]);
  176. }
  177. err = test_lazy_allocation();
  178. if (err == -ENODEV) goto enodev;
  179. test_filters();
  180. starpu_shutdown();
  181. return EXIT_SUCCESS;
  182. enodev:
  183. fprintf(stderr, "WARNING: No one can execute this task\n");
  184. /* yes, we do not perform the computation but we did detect that no one
  185. * could perform the kernel, so this is not an error from StarPU */
  186. return 77;
  187. }