lazy_allocation.c 5.6 KB

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