handle_to_pointer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. static void task(void **buffers, void *args)
  21. {
  22. float *numbers;
  23. size_t size, i;
  24. numbers = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  25. starpu_unpack_cl_args (args, &size);
  26. for(i = 0; i < size; i++)
  27. {
  28. numbers[i] = i;
  29. }
  30. }
  31. static starpu_codelet cl = {
  32. .where = STARPU_CPU | STARPU_CUDA,
  33. .cpu_func = task,
  34. .cuda_func = task,
  35. .nbuffers = 1
  36. };
  37. int main(int argc, char *argv[])
  38. {
  39. int err;
  40. size_t i;
  41. void *pointer;
  42. starpu_data_handle handle;
  43. static const size_t count = 123;
  44. starpu_init(NULL);
  45. err = starpu_malloc(&pointer, count * sizeof(float));
  46. assert((err == 0) && (pointer != NULL));
  47. starpu_variable_data_register(&handle, 0, (uintptr_t)pointer,
  48. sizeof(float));
  49. assert(starpu_handle_to_pointer(handle) == pointer);
  50. starpu_data_unregister(handle);
  51. starpu_vector_data_register(&handle, 0, (uintptr_t)pointer,
  52. count, sizeof(float));
  53. assert(starpu_handle_to_pointer(handle) == pointer);
  54. starpu_data_unregister(handle);
  55. starpu_matrix_data_register(&handle, 0, (uintptr_t)pointer, 0,
  56. count, 1, sizeof(float));
  57. assert(starpu_handle_to_pointer(handle) == pointer);
  58. starpu_data_unregister(handle);
  59. starpu_free(pointer);
  60. pointer = NULL;
  61. /* Lazy allocation. */
  62. starpu_vector_data_register(&handle, -1, 0 /* NULL */,
  63. count, sizeof(float));
  64. assert(starpu_handle_to_pointer(handle) == NULL);
  65. /* Pass the handle to a task. */
  66. starpu_insert_task(&cl,
  67. STARPU_W, handle,
  68. STARPU_VALUE, &count, sizeof(count),
  69. 0);
  70. /* Acquire the handle, forcing a local allocation. */
  71. starpu_data_acquire(handle, STARPU_R);
  72. /* Make sure we have a local pointer to it. */
  73. pointer = starpu_handle_to_pointer(handle);
  74. assert(pointer != NULL);
  75. for(i = 0; i < count; i++)
  76. {
  77. float *numbers = (float *)pointer;
  78. assert(numbers[i] == i);
  79. }
  80. starpu_data_release(handle);
  81. starpu_data_unregister(handle);
  82. starpu_shutdown();
  83. return EXIT_SUCCESS;
  84. }