handle_to_pointer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013 Inria
  4. * Copyright (C) 2012-2013,2015-2017 CNRS
  5. * Copyright (C) 2012-2014,2016 Université de Bordeaux
  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_data_unregister(handle);
  106. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer,
  107. count, sizeof(int));
  108. STARPU_ASSERT(starpu_data_handle_to_pointer(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_data_unregister(handle);
  114. starpu_free(pointer);
  115. pointer = NULL;
  116. /* Lazy allocation. */
  117. starpu_vector_data_register(&handle, -1, 0 /* NULL */,
  118. count, sizeof(int));
  119. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == NULL);
  120. /* Pass the handle to a task. */
  121. err = starpu_task_insert(&cl,
  122. STARPU_W, handle,
  123. STARPU_VALUE, &count, sizeof(count),
  124. 0);
  125. if (err == -ENODEV)
  126. return STARPU_TEST_SKIPPED;
  127. /* Acquire the handle, forcing a local allocation. */
  128. starpu_data_acquire(handle, STARPU_R);
  129. /* Make sure we have a local pointer to it. */
  130. ret = EXIT_SUCCESS;
  131. pointer = (int *) starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  132. if (pointer == NULL)
  133. {
  134. FPRINTF(stderr, "pointer should be non NULL\n");
  135. ret = EXIT_FAILURE;
  136. }
  137. else
  138. {
  139. int i;
  140. for(i = 0; i < count; i++)
  141. {
  142. int *numbers = (int *)pointer;
  143. if (numbers[i] != i)
  144. {
  145. FPRINTF(stderr, "Incorrect value numbers[%d] == %d should be %d\n", (int)i, numbers[i], (int)i);
  146. ret = EXIT_FAILURE;
  147. }
  148. }
  149. }
  150. starpu_data_release(handle);
  151. starpu_data_unregister(handle);
  152. starpu_shutdown();
  153. return ret;
  154. }