lazy_allocation.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. void cpu_memset_codelet(void *descr[], STARPU_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. .cpu_funcs_name = {"cpu_memset_codelet", NULL},
  81. .nbuffers = 1,
  82. .modes = {STARPU_W}
  83. };
  84. /*
  85. * Check content
  86. */
  87. void cpu_check_content_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  88. {
  89. STARPU_SKIP_IF_VALGRIND;
  90. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  91. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  92. unsigned i;
  93. for (i = 0; i < length; i++)
  94. {
  95. if (buf[i] != 42)
  96. {
  97. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  98. exit(-1);
  99. }
  100. }
  101. }
  102. #ifdef STARPU_USE_CUDA
  103. static void cuda_check_content_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  104. {
  105. STARPU_SKIP_IF_VALGRIND;
  106. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  107. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  108. unsigned i;
  109. for (i = 0; i < length; i++)
  110. {
  111. char dst;
  112. cudaMemcpyAsync(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  113. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  114. if (dst != 42)
  115. {
  116. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  117. exit(-1);
  118. }
  119. }
  120. }
  121. #endif
  122. #ifdef STARPU_USE_OPENCL
  123. static void opencl_check_content_codelet(void *buffers[], void *args)
  124. {
  125. STARPU_SKIP_IF_VALGRIND;
  126. cl_command_queue queue;
  127. int id = starpu_worker_get_id();
  128. int devid = starpu_worker_get_devid(id);
  129. starpu_opencl_get_queue(devid, &queue);
  130. cl_mem buf = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  131. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  132. unsigned i;
  133. for (i = 0; i < length; i++)
  134. {
  135. char dst;
  136. clEnqueueReadBuffer(queue,
  137. buf,
  138. CL_FALSE,
  139. i * sizeof(dst),
  140. sizeof(dst),
  141. &dst,
  142. 0, /* num_events_in_wait_list */
  143. NULL, /* event_wait_list */
  144. NULL /* event */);
  145. clFinish(queue);
  146. if (dst != 42)
  147. {
  148. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, dst, 42);
  149. exit(-1);
  150. }
  151. }
  152. }
  153. #endif
  154. static struct starpu_codelet check_content_cl =
  155. {
  156. .cpu_funcs = {cpu_check_content_codelet, NULL},
  157. #ifdef STARPU_USE_CUDA
  158. .cuda_funcs = {cuda_check_content_codelet, NULL},
  159. #endif
  160. #ifdef STARPU_USE_OPENCL
  161. .opencl_funcs = {opencl_check_content_codelet, NULL},
  162. #endif
  163. .cpu_funcs_name = {"cpu_check_content_codelet", NULL},
  164. .nbuffers = 1,
  165. .modes = {STARPU_R}
  166. };
  167. int main(int argc, char **argv)
  168. {
  169. int ret;
  170. ret = starpu_initialize(NULL, &argc, &argv);
  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. }