handle_to_pointer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifdef STARPU_USE_OPENCL
  20. #include <starpu_opencl.h>
  21. #endif
  22. #ifdef STARPU_USE_CUDA
  23. #include <starpu_cuda.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include "../helper.h"
  27. static void cpu_task(void **buffers, void *args)
  28. {
  29. int *numbers;
  30. int i;
  31. size_t size;
  32. numbers = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  33. starpu_codelet_unpack_args (args, &size);
  34. for(i = 0; i < size; i++)
  35. {
  36. numbers[i] = i;
  37. }
  38. }
  39. #ifdef STARPU_USE_CUDA
  40. static void cuda_task(void **buffers, void *args)
  41. {
  42. int *numbers;
  43. int i;
  44. size_t size;
  45. numbers = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  46. starpu_codelet_unpack_args (args, &size);
  47. for(i = 0; i < size; i++)
  48. {
  49. cudaMemcpyAsync(&numbers[i], &i, sizeof(int), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  50. }
  51. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  52. }
  53. #endif
  54. #ifdef STARPU_USE_OPENCL
  55. static void opencl_task(void *buffers[], void *args)
  56. {
  57. cl_command_queue queue;
  58. int id = starpu_worker_get_id();
  59. int devid = starpu_worker_get_devid(id);
  60. starpu_opencl_get_queue(devid, &queue);
  61. cl_mem numbers = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  62. unsigned size = STARPU_VECTOR_GET_NX(buffers[0]);
  63. unsigned i;
  64. for (i = 0; i < size; i++)
  65. {
  66. clEnqueueWriteBuffer(queue,
  67. numbers,
  68. CL_TRUE,
  69. i*sizeof(int), /* offset */
  70. sizeof(int),
  71. &i,
  72. 0, /* num_events_in_wait_list */
  73. NULL, /* event_wait_list */
  74. NULL /* event */);
  75. }
  76. clFinish(queue);
  77. }
  78. #endif
  79. static struct starpu_codelet cl =
  80. {
  81. .cpu_funcs = {cpu_task, NULL},
  82. #ifdef STARPU_USE_CUDA
  83. .cuda_funcs = {cuda_task, NULL},
  84. #endif
  85. #ifdef STARPU_USE_OPENCL
  86. .opencl_funcs = {opencl_task, NULL},
  87. #endif
  88. .nbuffers = 1,
  89. .modes = {STARPU_W}
  90. };
  91. int main(int argc, char *argv[])
  92. {
  93. int err, ret;
  94. size_t i;
  95. int *pointer;
  96. starpu_data_handle_t handle;
  97. static const size_t count = 123;
  98. ret = starpu_init(NULL);
  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, 0, (uintptr_t)pointer,
  103. sizeof(int));
  104. STARPU_ASSERT(starpu_handle_to_pointer(handle, 0) == pointer);
  105. starpu_data_unregister(handle);
  106. starpu_vector_data_register(&handle, 0, (uintptr_t)pointer,
  107. count, sizeof(int));
  108. STARPU_ASSERT(starpu_handle_to_pointer(handle, 0) == pointer);
  109. starpu_data_unregister(handle);
  110. starpu_matrix_data_register(&handle, 0, (uintptr_t)pointer, 0,
  111. count, 1, sizeof(int));
  112. STARPU_ASSERT(starpu_handle_to_pointer(handle, 0) == 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_handle_to_pointer(handle, 0) == NULL);
  120. /* Pass the handle to a task. */
  121. err = starpu_insert_task(&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_handle_to_pointer(handle, 0);
  132. if (pointer == NULL)
  133. {
  134. FPRINTF(stderr, "pointer should be non NULL\n");
  135. ret = EXIT_FAILURE;
  136. }
  137. else
  138. {
  139. for(i = 0; i < count; i++)
  140. {
  141. int *numbers = (int *)pointer;
  142. if (numbers[i] != i)
  143. {
  144. FPRINTF(stderr, "Incorrect value numbers[%d] == %d should be %d\n", (int)i, numbers[i], (int)i);
  145. ret = EXIT_FAILURE;
  146. }
  147. }
  148. }
  149. starpu_data_release(handle);
  150. starpu_data_unregister(handle);
  151. starpu_shutdown();
  152. return ret;
  153. }