lazy_allocation.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2012 inria
  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. #include <config.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #ifdef STARPU_USE_OPENCL
  23. #include <starpu_opencl.h>
  24. #endif
  25. #include <starpu_cuda.h>
  26. #include <stdlib.h>
  27. #include "../helper.h"
  28. #define VECTORSIZE 1024
  29. static starpu_data_handle_t v_handle;
  30. /*
  31. * Memset
  32. */
  33. #ifdef STARPU_USE_CUDA
  34. static void cuda_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  35. {
  36. STARPU_SKIP_IF_VALGRIND;
  37. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  38. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  39. cudaMemsetAsync(buf, 42, length, starpu_cuda_get_local_stream());
  40. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  41. }
  42. #endif
  43. #ifdef STARPU_USE_OPENCL
  44. static void opencl_memset_codelet(void *buffers[], void *args)
  45. {
  46. (void) args;
  47. cl_command_queue queue;
  48. int id = starpu_worker_get_id();
  49. int devid = starpu_worker_get_devid(id);
  50. starpu_opencl_get_queue(devid, &queue);
  51. cl_mem buffer = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  52. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  53. char *v = malloc(length);
  54. STARPU_ASSERT(v != NULL);
  55. memset(v, 42, length);
  56. clEnqueueWriteBuffer(queue,
  57. buffer,
  58. CL_FALSE,
  59. 0, /* offset */
  60. length, /* sizeof (char) */
  61. v,
  62. 0, /* num_events_in_wait_list */
  63. NULL, /* event_wait_list */
  64. NULL /* event */);
  65. clFinish(queue);
  66. }
  67. #endif
  68. static void cpu_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  69. {
  70. STARPU_SKIP_IF_VALGRIND;
  71. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  72. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  73. memset(buf, 42, length * sizeof(*buf));
  74. }
  75. static struct starpu_codelet memset_cl =
  76. {
  77. .cpu_funcs = {cpu_memset_codelet, NULL},
  78. #ifdef STARPU_USE_CUDA
  79. .cuda_funcs = {cuda_memset_codelet, NULL},
  80. #endif
  81. #ifdef STARPU_USE_OPENCL
  82. .opencl_funcs = {opencl_memset_codelet, NULL},
  83. #endif
  84. .nbuffers = 1,
  85. .modes = {STARPU_W}
  86. };
  87. /*
  88. * Check content
  89. */
  90. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  91. {
  92. STARPU_SKIP_IF_VALGRIND;
  93. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  94. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  95. unsigned i;
  96. for (i = 0; i < length; i++)
  97. {
  98. if (buf[i] != 42)
  99. {
  100. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  101. exit(-1);
  102. }
  103. }
  104. }
  105. #ifdef STARPU_USE_CUDA
  106. static void cuda_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  107. {
  108. STARPU_SKIP_IF_VALGRIND;
  109. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  110. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  111. unsigned i;
  112. for (i = 0; i < length; i++)
  113. {
  114. char dst;
  115. cudaMemcpy(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost);
  116. if (dst != 42)
  117. {
  118. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  119. exit(-1);
  120. }
  121. }
  122. }
  123. #endif
  124. #ifdef STARPU_USE_OPENCL
  125. static void opencl_check_content_codelet(void *buffers[], void *args)
  126. {
  127. STARPU_SKIP_IF_VALGRIND;
  128. cl_command_queue queue;
  129. int id = starpu_worker_get_id();
  130. int devid = starpu_worker_get_devid(id);
  131. starpu_opencl_get_queue(devid, &queue);
  132. cl_mem buf = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  133. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  134. unsigned i;
  135. for (i = 0; i < length; i++)
  136. {
  137. char dst;
  138. clEnqueueReadBuffer(queue,
  139. buf,
  140. CL_FALSE,
  141. i * sizeof(dst),
  142. sizeof(dst),
  143. &dst,
  144. 0, /* num_events_in_wait_list */
  145. NULL, /* event_wait_list */
  146. NULL /* event */);
  147. clFinish(queue);
  148. if (dst != 42)
  149. {
  150. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, dst, 42);
  151. exit(-1);
  152. }
  153. }
  154. }
  155. #endif
  156. static struct starpu_codelet check_content_cl =
  157. {
  158. .cpu_funcs = {cpu_check_content_codelet, NULL},
  159. #ifdef STARPU_USE_CUDA
  160. .cuda_funcs = {cuda_check_content_codelet, NULL},
  161. #endif
  162. #ifdef STARPU_USE_OPENCL
  163. .opencl_funcs = {opencl_check_content_codelet, NULL},
  164. #endif
  165. .nbuffers = 1,
  166. .modes = {STARPU_R}
  167. };
  168. int main(int argc, char **argv)
  169. {
  170. int ret;
  171. ret = starpu_init(NULL);
  172. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  173. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  174. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  175. ret = starpu_insert_task(&memset_cl, STARPU_W, v_handle, 0);
  176. if (ret == -ENODEV)
  177. return STARPU_TEST_SKIPPED;
  178. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  179. ret = starpu_task_wait_for_all();
  180. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  181. ret = starpu_insert_task(&check_content_cl, STARPU_R, v_handle, 0);
  182. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  183. ret = starpu_task_wait_for_all();
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  185. starpu_data_unregister(v_handle);
  186. starpu_shutdown();
  187. return EXIT_SUCCESS;
  188. }