lazy_allocation.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. *
  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. #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[], STARPU_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. }
  41. #endif
  42. #ifdef STARPU_USE_OPENCL
  43. static void opencl_memset_codelet(void *buffers[], void *args)
  44. {
  45. (void) args;
  46. cl_command_queue queue;
  47. int id = starpu_worker_get_id();
  48. int devid = starpu_worker_get_devid(id);
  49. starpu_opencl_get_queue(devid, &queue);
  50. cl_mem buffer = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  51. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  52. char *v = malloc(length);
  53. STARPU_ASSERT(v != NULL);
  54. memset(v, 42, length);
  55. clEnqueueWriteBuffer(queue,
  56. buffer,
  57. CL_FALSE,
  58. 0, /* offset */
  59. length, /* sizeof (char) */
  60. v,
  61. 0, /* num_events_in_wait_list */
  62. NULL, /* event_wait_list */
  63. NULL /* event */);
  64. }
  65. #endif
  66. void cpu_memset_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  67. {
  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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  92. {
  93. STARPU_SKIP_IF_VALGRIND;
  94. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  95. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  96. unsigned i;
  97. for (i = 0; i < length; i++)
  98. {
  99. if (buf[i] != 42)
  100. {
  101. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  102. exit(-1);
  103. }
  104. }
  105. }
  106. #ifdef STARPU_USE_CUDA
  107. static void cuda_check_content_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  108. {
  109. STARPU_SKIP_IF_VALGRIND;
  110. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  111. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  112. unsigned i;
  113. for (i = 0; i < length; i++)
  114. {
  115. char dst;
  116. cudaMemcpyAsync(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  117. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  118. if (dst != 42)
  119. {
  120. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  121. exit(-1);
  122. }
  123. }
  124. }
  125. #endif
  126. #ifdef STARPU_USE_OPENCL
  127. static void opencl_check_content_codelet(void *buffers[], void *args)
  128. {
  129. STARPU_SKIP_IF_VALGRIND;
  130. cl_command_queue queue;
  131. int id = starpu_worker_get_id();
  132. int devid = starpu_worker_get_devid(id);
  133. starpu_opencl_get_queue(devid, &queue);
  134. cl_mem buf = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  135. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  136. unsigned i;
  137. for (i = 0; i < length; i++)
  138. {
  139. char dst;
  140. clEnqueueReadBuffer(queue,
  141. buf,
  142. CL_FALSE,
  143. i * sizeof(dst),
  144. sizeof(dst),
  145. &dst,
  146. 0, /* num_events_in_wait_list */
  147. NULL, /* event_wait_list */
  148. NULL /* event */);
  149. clFinish(queue);
  150. if (dst != 42)
  151. {
  152. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, dst, 42);
  153. exit(-1);
  154. }
  155. }
  156. }
  157. #endif
  158. static struct starpu_codelet check_content_cl =
  159. {
  160. .cpu_funcs = {cpu_check_content_codelet},
  161. #ifdef STARPU_USE_CUDA
  162. .cuda_funcs = {cuda_check_content_codelet},
  163. #endif
  164. #ifdef STARPU_USE_OPENCL
  165. .opencl_funcs = {opencl_check_content_codelet},
  166. #endif
  167. .cpu_funcs_name = {"cpu_check_content_codelet"},
  168. .nbuffers = 1,
  169. .modes = {STARPU_R}
  170. };
  171. int main(int argc, char **argv)
  172. {
  173. int ret;
  174. ret = starpu_initialize(NULL, &argc, &argv);
  175. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  176. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  177. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  178. ret = starpu_task_insert(&memset_cl, STARPU_W, v_handle, 0);
  179. if (ret == -ENODEV)
  180. return STARPU_TEST_SKIPPED;
  181. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  182. ret = starpu_task_wait_for_all();
  183. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  184. ret = starpu_task_insert(&check_content_cl, STARPU_R, v_handle, 0);
  185. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  186. ret = starpu_task_wait_for_all();
  187. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  188. starpu_data_unregister(v_handle);
  189. starpu_shutdown();
  190. return EXIT_SUCCESS;
  191. }