handle_to_pointer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "../helper.h"
  21. static void cpu_task(void **buffers, void *args)
  22. {
  23. int *numbers;
  24. int i;
  25. size_t size;
  26. numbers = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  27. starpu_codelet_unpack_args (args, &size);
  28. for(i = 0; i < size; i++)
  29. {
  30. numbers[i] = i;
  31. }
  32. }
  33. #ifdef STARPU_USE_CUDA
  34. static void cuda_task(void **buffers, void *args)
  35. {
  36. int *numbers;
  37. int i;
  38. size_t size;
  39. numbers = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  40. starpu_codelet_unpack_args (args, &size);
  41. for(i = 0; i < size; i++)
  42. {
  43. cudaMemcpy(&numbers[i], &i, sizeof(int), cudaMemcpyHostToDevice);
  44. }
  45. }
  46. #endif
  47. static struct starpu_codelet cl =
  48. {
  49. .where = STARPU_CPU | STARPU_CUDA,
  50. .cpu_funcs = {cpu_task, NULL},
  51. #ifdef STARPU_USE_CUDA
  52. .cuda_funcs = {cuda_task, NULL},
  53. #endif
  54. .nbuffers = 1,
  55. .modes = {STARPU_W}
  56. };
  57. int main(int argc, char *argv[])
  58. {
  59. int err, ret;
  60. size_t i;
  61. int *pointer;
  62. starpu_data_handle_t handle;
  63. static const size_t count = 123;
  64. ret = starpu_init(NULL);
  65. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  66. err = starpu_malloc((void **)&pointer, count * sizeof(int));
  67. STARPU_ASSERT((err == 0) && (pointer != NULL));
  68. starpu_variable_data_register(&handle, 0, (uintptr_t)pointer,
  69. sizeof(int));
  70. STARPU_ASSERT(starpu_handle_to_pointer(handle, 0) == pointer);
  71. starpu_data_unregister(handle);
  72. starpu_vector_data_register(&handle, 0, (uintptr_t)pointer,
  73. count, sizeof(int));
  74. STARPU_ASSERT(starpu_handle_to_pointer(handle, 0) == pointer);
  75. starpu_data_unregister(handle);
  76. starpu_matrix_data_register(&handle, 0, (uintptr_t)pointer, 0,
  77. count, 1, sizeof(int));
  78. STARPU_ASSERT(starpu_handle_to_pointer(handle, 0) == pointer);
  79. starpu_data_unregister(handle);
  80. starpu_free(pointer);
  81. pointer = NULL;
  82. /* Lazy allocation. */
  83. starpu_vector_data_register(&handle, -1, 0 /* NULL */,
  84. count, sizeof(int));
  85. STARPU_ASSERT(starpu_handle_to_pointer(handle, 0) == NULL);
  86. /* Pass the handle to a task. */
  87. err = starpu_insert_task(&cl,
  88. STARPU_W, handle,
  89. STARPU_VALUE, &count, sizeof(count),
  90. 0);
  91. if (err == -ENODEV)
  92. return STARPU_TEST_SKIPPED;
  93. /* Acquire the handle, forcing a local allocation. */
  94. starpu_data_acquire(handle, STARPU_R);
  95. /* Make sure we have a local pointer to it. */
  96. pointer = (int *) starpu_handle_to_pointer(handle, 0);
  97. STARPU_ASSERT(pointer != NULL);
  98. for(i = 0; i < count; i++)
  99. {
  100. int *numbers = (int *)pointer;
  101. STARPU_ASSERT(numbers[i] == i);
  102. }
  103. starpu_data_release(handle);
  104. starpu_data_unregister(handle);
  105. starpu_shutdown();
  106. return EXIT_SUCCESS;
  107. }