handle_to_pointer.c 4.4 KB

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