handle_to_pointer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2014,2016,2018 Université de Bordeaux
  4. * Copyright (C) 2011-2013 Inria
  5. * Copyright (C) 2011-2013,2015-2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #undef NDEBUG
  19. #include <assert.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include "../helper.h"
  23. /*
  24. * Test the value returned by starpu_handle_to_pointer
  25. */
  26. void cpu_task(void **buffers, void *args)
  27. {
  28. int *numbers;
  29. int i;
  30. int size;
  31. numbers = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  32. starpu_codelet_unpack_args (args, &size);
  33. for(i = 0; i < (int)size; i++)
  34. {
  35. numbers[i] = i;
  36. }
  37. }
  38. #ifdef STARPU_USE_CUDA
  39. static void cuda_task(void **buffers, void *args)
  40. {
  41. int *numbers;
  42. int i;
  43. int size;
  44. numbers = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  45. starpu_codelet_unpack_args (args, &size);
  46. for(i = 0; i < (int)size; i++)
  47. {
  48. cudaMemcpyAsync(&numbers[i], &i, sizeof(int), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  49. }
  50. }
  51. #endif
  52. #ifdef STARPU_USE_OPENCL
  53. static void opencl_task(void *buffers[], void *args)
  54. {
  55. (void)args;
  56. cl_command_queue queue;
  57. int id = starpu_worker_get_id_check();
  58. int devid = starpu_worker_get_devid(id);
  59. starpu_opencl_get_queue(devid, &queue);
  60. cl_mem numbers = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  61. unsigned size = STARPU_VECTOR_GET_NX(buffers[0]);
  62. unsigned i;
  63. for (i = 0; i < size; i++)
  64. {
  65. clEnqueueWriteBuffer(queue,
  66. numbers,
  67. CL_TRUE,
  68. i*sizeof(int), /* offset */
  69. sizeof(int),
  70. &i,
  71. 0, /* num_events_in_wait_list */
  72. NULL, /* event_wait_list */
  73. NULL /* event */);
  74. }
  75. }
  76. #endif
  77. static struct starpu_codelet cl =
  78. {
  79. .cpu_funcs = {cpu_task},
  80. #ifdef STARPU_USE_CUDA
  81. .cuda_funcs = {cuda_task},
  82. .cuda_flags = {STARPU_CUDA_ASYNC},
  83. #endif
  84. #ifdef STARPU_USE_OPENCL
  85. .opencl_funcs = {opencl_task},
  86. .opencl_flags = {STARPU_OPENCL_ASYNC},
  87. #endif
  88. .cpu_funcs_name = {"cpu_task"},
  89. .nbuffers = 1,
  90. .modes = {STARPU_W}
  91. };
  92. int main(int argc, char *argv[])
  93. {
  94. int err, ret;
  95. int *pointer;
  96. starpu_data_handle_t handle;
  97. static const int count = 123;
  98. ret = starpu_initialize(NULL, &argc, &argv);
  99. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  100. err = starpu_malloc((void **)&pointer, count * sizeof(int));
  101. STARPU_ASSERT((err == 0) && (pointer != NULL));
  102. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer,
  103. sizeof(int));
  104. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == pointer);
  105. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, pointer));
  106. starpu_data_unregister(handle);
  107. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer,
  108. count, sizeof(int));
  109. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == pointer);
  110. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, pointer));
  111. starpu_data_unregister(handle);
  112. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer, 0,
  113. count, 1, sizeof(int));
  114. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == pointer);
  115. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, pointer));
  116. starpu_data_unregister(handle);
  117. starpu_free(pointer);
  118. pointer = NULL;
  119. /* Lazy allocation. */
  120. starpu_vector_data_register(&handle, -1, 0 /* NULL */,
  121. count, sizeof(int));
  122. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == NULL);
  123. /* Pass the handle to a task. */
  124. err = starpu_task_insert(&cl,
  125. STARPU_W, handle,
  126. STARPU_VALUE, &count, sizeof(count),
  127. 0);
  128. if (err == -ENODEV)
  129. return STARPU_TEST_SKIPPED;
  130. /* Acquire the handle, forcing a local allocation. */
  131. starpu_data_acquire(handle, STARPU_R);
  132. /* Make sure we have a local pointer to it. */
  133. ret = EXIT_SUCCESS;
  134. pointer = (int *) starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  135. if (pointer == NULL)
  136. {
  137. FPRINTF(stderr, "pointer should be non NULL\n");
  138. ret = EXIT_FAILURE;
  139. }
  140. else
  141. {
  142. int i;
  143. for(i = 0; i < count; i++)
  144. {
  145. int *numbers = (int *)pointer;
  146. STARPU_ASSERT(starpu_data_pointer_is_inside(handle, STARPU_MAIN_RAM, numbers));
  147. if (numbers[i] != i)
  148. {
  149. FPRINTF(stderr, "Incorrect value numbers[%d] == %d should be %d\n", (int)i, numbers[i], (int)i);
  150. ret = EXIT_FAILURE;
  151. }
  152. }
  153. }
  154. starpu_data_release(handle);
  155. starpu_data_unregister(handle);
  156. starpu_shutdown();
  157. return ret;
  158. }