data_lookup.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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(float),
  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_to_pointer(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. int main(int argc, char *argv[])
  70. {
  71. int err;
  72. size_t i;
  73. void *vectors[VECTOR_COUNT], *variables[VARIABLE_COUNT];
  74. starpu_data_handle vector_handles[VECTOR_COUNT];
  75. starpu_data_handle variable_handles[VARIABLE_COUNT];
  76. starpu_init(NULL);
  77. /* Register data regions. */
  78. for(i = 0; i < VARIABLE_COUNT; i++)
  79. {
  80. err = starpu_malloc(&variables[i], sizeof(float));
  81. assert(err == 0);
  82. starpu_variable_data_register(&variable_handles[i], 0,
  83. (uintptr_t)variables[i],
  84. sizeof(float));
  85. }
  86. for(i = 0; i < VECTOR_COUNT; i++)
  87. {
  88. err = starpu_malloc(&vectors[i], VECTOR_SIZE * sizeof(float));
  89. assert(err == 0);
  90. starpu_vector_data_register(&vector_handles[i], 0,
  91. (uintptr_t)vectors[i],
  92. VECTOR_SIZE, sizeof(float));
  93. }
  94. /* Look them up. */
  95. for(i = 0; i < VARIABLE_COUNT; i++)
  96. {
  97. starpu_data_handle handle;
  98. handle = starpu_data_lookup(variables[i]);
  99. assert(handle == variable_handles[i]);
  100. }
  101. for(i = 0; i < VECTOR_COUNT; i++)
  102. {
  103. starpu_data_handle handle;
  104. handle = starpu_data_lookup(vectors[i]);
  105. assert(handle == vector_handles[i]);
  106. }
  107. /* Unregister them. */
  108. for(i = 0; i < VARIABLE_COUNT; i++)
  109. {
  110. starpu_data_unregister(variable_handles[i]);
  111. }
  112. for(i = 0; i < VECTOR_COUNT; i++)
  113. {
  114. starpu_data_unregister(vector_handles[i]);
  115. }
  116. /* Make sure we can no longer find them. */
  117. for(i = 0; i < VARIABLE_COUNT; i++)
  118. {
  119. starpu_data_handle handle;
  120. handle = starpu_data_lookup(variables[i]);
  121. assert(handle == NULL);
  122. starpu_free(variables[i]);
  123. }
  124. for(i = 0; i < VECTOR_COUNT; i++)
  125. {
  126. starpu_data_handle handle;
  127. handle = starpu_data_lookup(vectors[i]);
  128. assert(handle == NULL);
  129. starpu_free(vectors[i]);
  130. }
  131. test_lazy_allocation();
  132. starpu_shutdown();
  133. return EXIT_SUCCESS;
  134. }