lazy_allocation.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  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. #ifdef STARPU_USE_OPENCL
  23. #include <starpu_opencl.h>
  24. #endif
  25. #include <starpu_cuda.h>
  26. #include <stdlib.h>
  27. #include "../helper.h"
  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[], __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. cudaStreamSynchronize(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();
  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_TRUE,
  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. static void cpu_memset_codelet(void *descr[], __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, NULL},
  77. #ifdef STARPU_USE_CUDA
  78. .cuda_funcs = {cuda_memset_codelet, NULL},
  79. #endif
  80. #ifdef STARPU_USE_OPENCL
  81. .opencl_funcs = {opencl_memset_codelet, NULL},
  82. #endif
  83. .nbuffers = 1,
  84. .modes = {STARPU_W}
  85. };
  86. /*
  87. * Check content
  88. */
  89. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  90. {
  91. STARPU_SKIP_IF_VALGRIND;
  92. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  93. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  94. unsigned i;
  95. for (i = 0; i < length; i++)
  96. {
  97. if (buf[i] != 42)
  98. {
  99. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  100. exit(-1);
  101. }
  102. }
  103. }
  104. #ifdef STARPU_USE_CUDA
  105. static void cuda_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  106. {
  107. STARPU_SKIP_IF_VALGRIND;
  108. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  109. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  110. unsigned i;
  111. for (i = 0; i < length; i++)
  112. {
  113. char dst;
  114. cudaMemcpy(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost);
  115. if (dst != 42)
  116. {
  117. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  118. exit(-1);
  119. }
  120. }
  121. }
  122. #endif
  123. #ifdef STARPU_USE_OPENCL
  124. static void opencl_check_content_codelet(void *buffers[], void *args)
  125. {
  126. STARPU_SKIP_IF_VALGRIND;
  127. cl_command_queue queue;
  128. int id = starpu_worker_get_id();
  129. int devid = starpu_worker_get_devid(id);
  130. starpu_opencl_get_queue(devid, &queue);
  131. cl_mem buf = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  132. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  133. unsigned i;
  134. for (i = 0; i < length; i++)
  135. {
  136. char dst;
  137. clEnqueueReadBuffer(
  138. queue,
  139. buf,
  140. CL_TRUE,
  141. i * sizeof(dst),
  142. sizeof(dst),
  143. &dst,
  144. 0, /* num_events_in_wait_list */
  145. NULL, /* event_wait_list */
  146. NULL /* event */);
  147. if (dst != 42)
  148. {
  149. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  150. exit(-1);
  151. }
  152. }
  153. }
  154. #endif
  155. static struct starpu_codelet check_content_cl =
  156. {
  157. .cpu_funcs = {cpu_check_content_codelet, NULL},
  158. #ifdef STARPU_USE_CUDA
  159. .cuda_funcs = {cuda_check_content_codelet, NULL},
  160. #endif
  161. #ifdef STARPU_USE_OPENCL
  162. .opencl_funcs = {opencl_check_content_codelet, NULL},
  163. #endif
  164. .nbuffers = 1,
  165. .modes = {STARPU_R}
  166. };
  167. int main(int argc, char **argv)
  168. {
  169. int ret;
  170. ret = starpu_init(NULL);
  171. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  172. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  173. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  174. ret = starpu_insert_task(&memset_cl, STARPU_W, v_handle, 0);
  175. if (ret == -ENODEV)
  176. return STARPU_TEST_SKIPPED;
  177. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  178. ret = starpu_task_wait_for_all();
  179. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  180. ret = starpu_insert_task(&check_content_cl, STARPU_R, v_handle, 0);
  181. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  182. ret = starpu_task_wait_for_all();
  183. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  184. starpu_data_unregister(v_handle);
  185. starpu_shutdown();
  186. return EXIT_SUCCESS;
  187. }