lazy_allocation.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014,2016 Université de Bordeaux
  4. * Copyright (C) 2011,2012 Inria
  5. * Copyright (C) 2011-2013,2015-2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  24. /*
  25. * Trigger lazy allocation by registering NULL, then setting a value, and
  26. * then checking it
  27. */
  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[], void *arg)
  35. {
  36. (void)arg;
  37. STARPU_SKIP_IF_VALGRIND;
  38. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  39. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  40. cudaMemsetAsync(buf, 42, length, 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_check();
  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. }
  66. #endif
  67. void cpu_memset_codelet(void *descr[], void *arg)
  68. {
  69. (void)arg;
  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},
  78. #ifdef STARPU_USE_CUDA
  79. .cuda_funcs = {cuda_memset_codelet},
  80. .cuda_flags = {STARPU_CUDA_ASYNC},
  81. #endif
  82. #ifdef STARPU_USE_OPENCL
  83. .opencl_funcs = {opencl_memset_codelet},
  84. .opencl_flags = {STARPU_OPENCL_ASYNC},
  85. #endif
  86. .cpu_funcs_name = {"cpu_memset_codelet"},
  87. .nbuffers = 1,
  88. .modes = {STARPU_W}
  89. };
  90. /*
  91. * Check content
  92. */
  93. void cpu_check_content_codelet(void *descr[], void *arg)
  94. {
  95. (void)arg;
  96. STARPU_SKIP_IF_VALGRIND;
  97. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  98. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  99. unsigned i;
  100. for (i = 0; i < length; i++)
  101. {
  102. if (buf[i] != 42)
  103. {
  104. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  105. exit(-1);
  106. }
  107. }
  108. }
  109. #ifdef STARPU_USE_CUDA
  110. static void cuda_check_content_codelet(void *descr[], void *arg)
  111. {
  112. (void)arg;
  113. STARPU_SKIP_IF_VALGRIND;
  114. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  115. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  116. unsigned i;
  117. for (i = 0; i < length; i++)
  118. {
  119. char dst;
  120. cudaMemcpyAsync(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  121. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  122. if (dst != 42)
  123. {
  124. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  125. exit(-1);
  126. }
  127. }
  128. }
  129. #endif
  130. #ifdef STARPU_USE_OPENCL
  131. static void opencl_check_content_codelet(void *buffers[], void *args)
  132. {
  133. (void)args;
  134. STARPU_SKIP_IF_VALGRIND;
  135. cl_command_queue queue;
  136. int id = starpu_worker_get_id_check();
  137. int devid = starpu_worker_get_devid(id);
  138. starpu_opencl_get_queue(devid, &queue);
  139. cl_mem buf = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  140. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  141. unsigned i;
  142. for (i = 0; i < length; i++)
  143. {
  144. char dst;
  145. clEnqueueReadBuffer(queue,
  146. buf,
  147. CL_FALSE,
  148. i * sizeof(dst),
  149. sizeof(dst),
  150. &dst,
  151. 0, /* num_events_in_wait_list */
  152. NULL, /* event_wait_list */
  153. NULL /* event */);
  154. clFinish(queue);
  155. if (dst != 42)
  156. {
  157. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, dst, 42);
  158. exit(-1);
  159. }
  160. }
  161. }
  162. #endif
  163. static struct starpu_codelet check_content_cl =
  164. {
  165. .cpu_funcs = {cpu_check_content_codelet},
  166. #ifdef STARPU_USE_CUDA
  167. .cuda_funcs = {cuda_check_content_codelet},
  168. #endif
  169. #ifdef STARPU_USE_OPENCL
  170. .opencl_funcs = {opencl_check_content_codelet},
  171. #endif
  172. .cpu_funcs_name = {"cpu_check_content_codelet"},
  173. .nbuffers = 1,
  174. .modes = {STARPU_R}
  175. };
  176. int main(int argc, char **argv)
  177. {
  178. int ret;
  179. ret = starpu_initialize(NULL, &argc, &argv);
  180. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  181. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  182. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  183. ret = starpu_task_insert(&memset_cl, STARPU_W, v_handle, 0);
  184. if (ret == -ENODEV)
  185. return STARPU_TEST_SKIPPED;
  186. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  187. ret = starpu_task_wait_for_all();
  188. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  189. ret = starpu_task_insert(&check_content_cl, STARPU_R, v_handle, 0);
  190. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  191. ret = starpu_task_wait_for_all();
  192. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  193. starpu_data_unregister(v_handle);
  194. starpu_shutdown();
  195. return EXIT_SUCCESS;
  196. }