lazy_allocation.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  36. {
  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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  68. {
  69. STARPU_SKIP_IF_VALGRIND;
  70. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  71. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  72. memset(buf, 42, length * sizeof(*buf));
  73. }
  74. static struct starpu_codelet memset_cl =
  75. {
  76. .cpu_funcs = {cpu_memset_codelet},
  77. #ifdef STARPU_USE_CUDA
  78. .cuda_funcs = {cuda_memset_codelet},
  79. .cuda_flags = {STARPU_CUDA_ASYNC},
  80. #endif
  81. #ifdef STARPU_USE_OPENCL
  82. .opencl_funcs = {opencl_memset_codelet},
  83. .opencl_flags = {STARPU_OPENCL_ASYNC},
  84. #endif
  85. .cpu_funcs_name = {"cpu_memset_codelet"},
  86. .nbuffers = 1,
  87. .modes = {STARPU_W}
  88. };
  89. /*
  90. * Check content
  91. */
  92. void cpu_check_content_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  93. {
  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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  109. {
  110. STARPU_SKIP_IF_VALGRIND;
  111. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  112. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  113. unsigned i;
  114. for (i = 0; i < length; i++)
  115. {
  116. char dst;
  117. cudaMemcpyAsync(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  118. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  119. if (dst != 42)
  120. {
  121. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  122. exit(-1);
  123. }
  124. }
  125. }
  126. #endif
  127. #ifdef STARPU_USE_OPENCL
  128. static void opencl_check_content_codelet(void *buffers[], void *args)
  129. {
  130. STARPU_SKIP_IF_VALGRIND;
  131. cl_command_queue queue;
  132. int id = starpu_worker_get_id_check();
  133. int devid = starpu_worker_get_devid(id);
  134. starpu_opencl_get_queue(devid, &queue);
  135. cl_mem buf = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  136. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  137. unsigned i;
  138. for (i = 0; i < length; i++)
  139. {
  140. char dst;
  141. clEnqueueReadBuffer(queue,
  142. buf,
  143. CL_FALSE,
  144. i * sizeof(dst),
  145. sizeof(dst),
  146. &dst,
  147. 0, /* num_events_in_wait_list */
  148. NULL, /* event_wait_list */
  149. NULL /* event */);
  150. clFinish(queue);
  151. if (dst != 42)
  152. {
  153. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, dst, 42);
  154. exit(-1);
  155. }
  156. }
  157. }
  158. #endif
  159. static struct starpu_codelet check_content_cl =
  160. {
  161. .cpu_funcs = {cpu_check_content_codelet},
  162. #ifdef STARPU_USE_CUDA
  163. .cuda_funcs = {cuda_check_content_codelet},
  164. #endif
  165. #ifdef STARPU_USE_OPENCL
  166. .opencl_funcs = {opencl_check_content_codelet},
  167. #endif
  168. .cpu_funcs_name = {"cpu_check_content_codelet"},
  169. .nbuffers = 1,
  170. .modes = {STARPU_R}
  171. };
  172. int main(int argc, char **argv)
  173. {
  174. int ret;
  175. ret = starpu_initialize(NULL, &argc, &argv);
  176. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  177. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  178. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  179. ret = starpu_task_insert(&memset_cl, STARPU_W, v_handle, 0);
  180. if (ret == -ENODEV)
  181. return STARPU_TEST_SKIPPED;
  182. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  183. ret = starpu_task_wait_for_all();
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  185. ret = starpu_task_insert(&check_content_cl, STARPU_R, v_handle, 0);
  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. starpu_data_unregister(v_handle);
  190. starpu_shutdown();
  191. return EXIT_SUCCESS;
  192. }