lazy_allocation.c 5.5 KB

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