data_invalidation.c 5.7 KB

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