lazy_allocation.c 5.4 KB

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