data_invalidation.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012, 2014, 2016 Université de Bordeaux
  4. * Copyright (C) 2012, 2013, 2015, 2016 CNRS
  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. /*
  25. * Try to mix starpu_data_invalidate and starpu_data_invalidate_submit
  26. * calls with task insertions
  27. */
  28. #ifdef STARPU_QUICK_CHECK
  29. # define NLOOPS 100
  30. #else
  31. # define NLOOPS 1000
  32. #endif
  33. #define VECTORSIZE 1024
  34. static starpu_data_handle_t v_handle;
  35. /*
  36. * Memset
  37. */
  38. #ifdef STARPU_USE_CUDA
  39. static void cuda_memset_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  40. {
  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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  72. {
  73. STARPU_SKIP_IF_VALGRIND;
  74. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  75. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  76. memset(buf, 42, length * sizeof(*buf));
  77. }
  78. static struct starpu_codelet memset_cl =
  79. {
  80. .cpu_funcs = {cpu_memset_codelet},
  81. #ifdef STARPU_USE_CUDA
  82. .cuda_funcs = {cuda_memset_codelet},
  83. .cuda_flags = {STARPU_CUDA_ASYNC},
  84. #endif
  85. #ifdef STARPU_USE_OPENCL
  86. .opencl_funcs = {opencl_memset_codelet},
  87. .opencl_flags = {STARPU_OPENCL_ASYNC},
  88. #endif
  89. .cpu_funcs_name = {"cpu_memset_codelet"},
  90. .nbuffers = 1,
  91. .modes = {STARPU_W}
  92. };
  93. /*
  94. * Check content
  95. */
  96. void cpu_check_content_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  97. {
  98. STARPU_SKIP_IF_VALGRIND;
  99. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  100. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  101. unsigned i;
  102. for (i = 0; i < length; i++)
  103. {
  104. if (buf[i] != 42)
  105. {
  106. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, buf[i], 42);
  107. exit(-1);
  108. }
  109. }
  110. }
  111. static struct starpu_codelet check_content_cl =
  112. {
  113. .cpu_funcs = {cpu_check_content_codelet},
  114. .cpu_funcs_name = {"cpu_check_content_codelet"},
  115. .nbuffers = 1,
  116. .modes = {STARPU_R}
  117. };
  118. int main(int argc, char **argv)
  119. {
  120. int ret;
  121. ret = starpu_initialize(NULL, &argc, &argv);
  122. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  124. if(starpu_cpu_worker_get_count() == 0) return STARPU_TEST_SKIPPED;
  125. /* The buffer should never be explicitely allocated */
  126. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  127. unsigned loop;
  128. for (loop = 0; loop < NLOOPS; loop++)
  129. {
  130. struct starpu_task *memset_task;
  131. struct starpu_task *check_content_task;
  132. memset_task = starpu_task_create();
  133. memset_task->cl = &memset_cl;
  134. memset_task->handles[0] = v_handle;
  135. memset_task->detach = 0;
  136. ret = starpu_task_submit(memset_task);
  137. if (ret == -ENODEV) goto enodev;
  138. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  139. ret = starpu_task_wait(memset_task);
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  141. check_content_task = starpu_task_create();
  142. check_content_task->cl = &check_content_cl;
  143. check_content_task->handles[0] = v_handle;
  144. check_content_task->detach = 0;
  145. ret = starpu_task_submit(check_content_task);
  146. if (ret == -ENODEV) goto enodev;
  147. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  148. ret = starpu_task_wait(check_content_task);
  149. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  150. starpu_data_invalidate(v_handle);
  151. }
  152. for (loop = 0; loop < NLOOPS; loop++)
  153. {
  154. struct starpu_task *memset_task;
  155. struct starpu_task *check_content_task;
  156. memset_task = starpu_task_create();
  157. memset_task->cl = &memset_cl;
  158. memset_task->handles[0] = v_handle;
  159. ret = starpu_task_submit(memset_task);
  160. if (ret == -ENODEV) goto enodev;
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  162. check_content_task = starpu_task_create();
  163. check_content_task->cl = &check_content_cl;
  164. check_content_task->handles[0] = v_handle;
  165. ret = starpu_task_submit(check_content_task);
  166. if (ret == -ENODEV) goto enodev;
  167. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  168. starpu_data_invalidate_submit(v_handle);
  169. }
  170. /* this should get rid of automatically allocated buffers */
  171. starpu_data_unregister(v_handle);
  172. starpu_shutdown();
  173. return EXIT_SUCCESS;
  174. enodev:
  175. starpu_data_unregister(v_handle);
  176. fprintf(stderr, "WARNING: No one can execute this task\n");
  177. /* yes, we do not perform the computation but we did detect that no one
  178. * could perform the kernel, so this is not an error from StarPU */
  179. starpu_shutdown();
  180. return STARPU_TEST_SKIPPED;
  181. }