handle_to_pointer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 INRIA
  4. * Copyright (C) 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #undef NDEBUG
  18. #include <assert.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #include "../helper.h"
  22. /*
  23. * Test the value returned by starpu_handle_to_pointer
  24. */
  25. void cpu_task(void **buffers, void *args)
  26. {
  27. int *numbers;
  28. int i;
  29. int size;
  30. numbers = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  31. starpu_codelet_unpack_args (args, &size);
  32. for(i = 0; i < (int)size; i++)
  33. {
  34. numbers[i] = i;
  35. }
  36. }
  37. #ifdef STARPU_USE_CUDA
  38. static void cuda_task(void **buffers, void *args)
  39. {
  40. int *numbers;
  41. int i;
  42. int size;
  43. numbers = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  44. starpu_codelet_unpack_args (args, &size);
  45. for(i = 0; i < (int)size; i++)
  46. {
  47. cudaMemcpyAsync(&numbers[i], &i, sizeof(int), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  48. }
  49. }
  50. #endif
  51. #ifdef STARPU_USE_OPENCL
  52. static void opencl_task(void *buffers[], void *args)
  53. {
  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 i;
  94. int *pointer;
  95. starpu_data_handle_t handle;
  96. static const int count = 123;
  97. ret = starpu_initialize(NULL, &argc, &argv);
  98. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  99. err = starpu_malloc((void **)&pointer, count * sizeof(int));
  100. STARPU_ASSERT((err == 0) && (pointer != NULL));
  101. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer,
  102. sizeof(int));
  103. STARPU_ASSERT(starpu_data_handle_to_pointer(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_data_unregister(handle);
  109. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)pointer, 0,
  110. count, 1, sizeof(int));
  111. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == pointer);
  112. starpu_data_unregister(handle);
  113. starpu_free(pointer);
  114. pointer = NULL;
  115. /* Lazy allocation. */
  116. starpu_vector_data_register(&handle, -1, 0 /* NULL */,
  117. count, sizeof(int));
  118. STARPU_ASSERT(starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM) == NULL);
  119. /* Pass the handle to a task. */
  120. err = starpu_task_insert(&cl,
  121. STARPU_W, handle,
  122. STARPU_VALUE, &count, sizeof(count),
  123. 0);
  124. if (err == -ENODEV)
  125. return STARPU_TEST_SKIPPED;
  126. /* Acquire the handle, forcing a local allocation. */
  127. starpu_data_acquire(handle, STARPU_R);
  128. /* Make sure we have a local pointer to it. */
  129. ret = EXIT_SUCCESS;
  130. pointer = (int *) starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  131. if (pointer == NULL)
  132. {
  133. FPRINTF(stderr, "pointer should be non NULL\n");
  134. ret = EXIT_FAILURE;
  135. }
  136. else
  137. {
  138. for(i = 0; i < count; i++)
  139. {
  140. int *numbers = (int *)pointer;
  141. if (numbers[i] != i)
  142. {
  143. FPRINTF(stderr, "Incorrect value numbers[%d] == %d should be %d\n", (int)i, numbers[i], (int)i);
  144. ret = EXIT_FAILURE;
  145. }
  146. }
  147. }
  148. starpu_data_release(handle);
  149. starpu_data_unregister(handle);
  150. starpu_shutdown();
  151. return ret;
  152. }