handle_to_pointer.c 4.4 KB

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