lazy_allocation.c 5.5 KB

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