data_invalidation.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2010-2014,2016 Université de Bordeaux
  5. * Copyright (C) 2011-2013,2015-2017 CNRS
  6. * Copyright (C) 2013 Thibaut Lambert
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <errno.h>
  22. #include <starpu.h>
  23. #include <stdlib.h>
  24. #include "../helper.h"
  25. /*
  26. * Try to mix starpu_data_invalidate and starpu_data_invalidate_submit
  27. * calls with task insertions
  28. */
  29. #ifdef STARPU_QUICK_CHECK
  30. # define NLOOPS 100
  31. #else
  32. # define NLOOPS 1000
  33. #endif
  34. #define VECTORSIZE 1024
  35. static starpu_data_handle_t v_handle;
  36. /*
  37. * Memset
  38. */
  39. #ifdef STARPU_USE_CUDA
  40. static void cuda_memset_codelet(void *descr[], void *arg)
  41. {
  42. (void)arg;
  43. STARPU_SKIP_IF_VALGRIND;
  44. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  45. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  46. cudaMemsetAsync(buf, 42, length, starpu_cuda_get_local_stream());
  47. }
  48. #endif
  49. #ifdef STARPU_USE_OPENCL
  50. static void opencl_memset_codelet(void *buffers[], void *args)
  51. {
  52. (void) args;
  53. cl_command_queue queue;
  54. int id = starpu_worker_get_id_check();
  55. int devid = starpu_worker_get_devid(id);
  56. starpu_opencl_get_queue(devid, &queue);
  57. cl_mem buffer = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  58. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  59. char *v = malloc(length);
  60. STARPU_ASSERT(v != NULL);
  61. memset(v, 42, length);
  62. clEnqueueWriteBuffer(queue,
  63. buffer,
  64. CL_FALSE,
  65. 0, /* offset */
  66. length, /* sizeof (char) */
  67. v,
  68. 0, /* num_events_in_wait_list */
  69. NULL, /* event_wait_list */
  70. NULL /* event */);
  71. }
  72. #endif /* !STARPU_USE_OPENCL */
  73. void cpu_memset_codelet(void *descr[], void *arg)
  74. {
  75. (void)arg;
  76. STARPU_SKIP_IF_VALGRIND;
  77. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  78. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  79. memset(buf, 42, length * sizeof(*buf));
  80. }
  81. static struct starpu_codelet memset_cl =
  82. {
  83. .cpu_funcs = {cpu_memset_codelet},
  84. #ifdef STARPU_USE_CUDA
  85. .cuda_funcs = {cuda_memset_codelet},
  86. .cuda_flags = {STARPU_CUDA_ASYNC},
  87. #endif
  88. #ifdef STARPU_USE_OPENCL
  89. .opencl_funcs = {opencl_memset_codelet},
  90. .opencl_flags = {STARPU_OPENCL_ASYNC},
  91. #endif
  92. .cpu_funcs_name = {"cpu_memset_codelet"},
  93. .nbuffers = 1,
  94. .modes = {STARPU_W}
  95. };
  96. /*
  97. * Check content
  98. */
  99. void cpu_check_content_codelet(void *descr[], void *arg)
  100. {
  101. (void)arg;
  102. STARPU_SKIP_IF_VALGRIND;
  103. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  104. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  105. unsigned i;
  106. for (i = 0; i < length; i++)
  107. {
  108. if (buf[i] != 42)
  109. {
  110. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, buf[i], 42);
  111. exit(-1);
  112. }
  113. }
  114. }
  115. static struct starpu_codelet check_content_cl =
  116. {
  117. .cpu_funcs = {cpu_check_content_codelet},
  118. .cpu_funcs_name = {"cpu_check_content_codelet"},
  119. .nbuffers = 1,
  120. .modes = {STARPU_R}
  121. };
  122. int main(int argc, char **argv)
  123. {
  124. int ret;
  125. ret = starpu_initialize(NULL, &argc, &argv);
  126. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  128. if(starpu_cpu_worker_get_count() == 0) return STARPU_TEST_SKIPPED;
  129. /* The buffer should never be explicitely allocated */
  130. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  131. unsigned loop;
  132. for (loop = 0; loop < NLOOPS; loop++)
  133. {
  134. struct starpu_task *memset_task;
  135. struct starpu_task *check_content_task;
  136. memset_task = starpu_task_create();
  137. memset_task->cl = &memset_cl;
  138. memset_task->handles[0] = v_handle;
  139. memset_task->detach = 0;
  140. ret = starpu_task_submit(memset_task);
  141. if (ret == -ENODEV) goto enodev;
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  143. ret = starpu_task_wait(memset_task);
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  145. check_content_task = starpu_task_create();
  146. check_content_task->cl = &check_content_cl;
  147. check_content_task->handles[0] = v_handle;
  148. check_content_task->detach = 0;
  149. ret = starpu_task_submit(check_content_task);
  150. if (ret == -ENODEV) goto enodev;
  151. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  152. ret = starpu_task_wait(check_content_task);
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  154. starpu_data_invalidate(v_handle);
  155. }
  156. for (loop = 0; loop < NLOOPS; loop++)
  157. {
  158. struct starpu_task *memset_task;
  159. struct starpu_task *check_content_task;
  160. memset_task = starpu_task_create();
  161. memset_task->cl = &memset_cl;
  162. memset_task->handles[0] = v_handle;
  163. ret = starpu_task_submit(memset_task);
  164. if (ret == -ENODEV) goto enodev;
  165. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  166. check_content_task = starpu_task_create();
  167. check_content_task->cl = &check_content_cl;
  168. check_content_task->handles[0] = v_handle;
  169. ret = starpu_task_submit(check_content_task);
  170. if (ret == -ENODEV) goto enodev;
  171. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  172. starpu_data_invalidate_submit(v_handle);
  173. }
  174. /* this should get rid of automatically allocated buffers */
  175. starpu_data_unregister(v_handle);
  176. starpu_shutdown();
  177. return EXIT_SUCCESS;
  178. enodev:
  179. starpu_data_unregister(v_handle);
  180. fprintf(stderr, "WARNING: No one can execute this task\n");
  181. /* yes, we do not perform the computation but we did detect that no one
  182. * could perform the kernel, so this is not an error from StarPU */
  183. starpu_shutdown();
  184. return STARPU_TEST_SKIPPED;
  185. }