data_invalidation.c 5.7 KB

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