handle_to_pointer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. /*
  22. * Test the value returned by starpu_handle_to_pointer
  23. */
  24. void cpu_task(void **buffers, void *args)
  25. {
  26. int *numbers;
  27. int i;
  28. int size;
  29. numbers = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  30. starpu_codelet_unpack_args (args, &size);
  31. for(i = 0; i < (int)size; i++)
  32. {
  33. numbers[i] = i;
  34. }
  35. }
  36. #ifdef STARPU_USE_CUDA
  37. static void cuda_task(void **buffers, void *args)
  38. {
  39. int *numbers;
  40. int i;
  41. int size;
  42. numbers = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  43. starpu_codelet_unpack_args (args, &size);
  44. for(i = 0; i < (int)size; i++)
  45. {
  46. cudaMemcpyAsync(&numbers[i], &i, sizeof(int), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  47. }
  48. }
  49. #endif
  50. #ifdef STARPU_USE_OPENCL
  51. static void opencl_task(void *buffers[], void *args)
  52. {
  53. (void)args;
  54. cl_command_queue queue;
  55. int id = starpu_worker_get_id_check();
  56. int devid = starpu_worker_get_devid(id);
  57. starpu_opencl_get_queue(devid, &queue);
  58. cl_mem numbers = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  59. unsigned size = STARPU_VECTOR_GET_NX(buffers[0]);
  60. unsigned i;
  61. for (i = 0; i < size; i++)
  62. {
  63. clEnqueueWriteBuffer(queue,
  64. numbers,
  65. CL_TRUE,
  66. i*sizeof(int), /* offset */
  67. sizeof(int),
  68. &i,
  69. 0, /* num_events_in_wait_list */
  70. NULL, /* event_wait_list */
  71. NULL /* event */);
  72. }
  73. }
  74. #endif
  75. static struct starpu_codelet cl =
  76. {
  77. .cpu_funcs = {cpu_task},
  78. #ifdef STARPU_USE_CUDA
  79. .cuda_funcs = {cuda_task},
  80. .cuda_flags = {STARPU_CUDA_ASYNC},
  81. #endif
  82. #ifdef STARPU_USE_OPENCL
  83. .opencl_funcs = {opencl_task},
  84. .opencl_flags = {STARPU_OPENCL_ASYNC},
  85. #endif
  86. .cpu_funcs_name = {"cpu_task"},
  87. .nbuffers = 1,
  88. .modes = {STARPU_W}
  89. };
  90. int main(int argc, char *argv[])
  91. {
  92. int err, ret;
  93. int *pointer;
  94. starpu_data_handle_t handle;
  95. static const int count = 123;
  96. ret = starpu_initialize(NULL, &argc, &argv);
  97. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  98. err = starpu_malloc((void **)&pointer, count * sizeof(int));
  99. STARPU_ASSERT((err == 0) && (pointer != NULL));
  100. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer,
  101. sizeof(int));
  102. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == pointer);
  103. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, pointer));
  104. starpu_data_unregister(handle);
  105. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer,
  106. count, sizeof(int));
  107. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == pointer);
  108. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, pointer));
  109. starpu_data_unregister(handle);
  110. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer, 0,
  111. count, 1, sizeof(int));
  112. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == pointer);
  113. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, pointer));
  114. starpu_data_unregister(handle);
  115. starpu_free(pointer);
  116. pointer = NULL;
  117. /* Lazy allocation. */
  118. starpu_vector_data_register(&handle, -1, 0 /* NULL */,
  119. count, sizeof(int));
  120. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == NULL);
  121. /* Pass the handle to a task. */
  122. err = starpu_task_insert(&cl,
  123. STARPU_W, handle,
  124. STARPU_VALUE, &count, sizeof(count),
  125. 0);
  126. if (err == -ENODEV)
  127. return STARPU_TEST_SKIPPED;
  128. /* Acquire the handle, forcing a local allocation. */
  129. starpu_data_acquire(handle, STARPU_R);
  130. /* Make sure we have a local pointer to it. */
  131. ret = EXIT_SUCCESS;
  132. pointer = (int *) starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  133. if (pointer == NULL)
  134. {
  135. FPRINTF(stderr, "pointer should be non NULL\n");
  136. ret = EXIT_FAILURE;
  137. }
  138. else
  139. {
  140. int i;
  141. for(i = 0; i < count; i++)
  142. {
  143. int *numbers = (int *)pointer;
  144. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, numbers));
  145. if (numbers[i] != i)
  146. {
  147. FPRINTF(stderr, "Incorrect value numbers[%d] == %d should be %d\n", (int)i, numbers[i], (int)i);
  148. ret = EXIT_FAILURE;
  149. }
  150. }
  151. }
  152. starpu_data_release(handle);
  153. starpu_data_unregister(handle);
  154. starpu_shutdown();
  155. return ret;
  156. }