data_invalidation.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2012 Centre National de la Recherche Scientifique
  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. #if STARPU_HAVE_VALGRIND_H
  19. #include <valgrind/valgrind.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <starpu.h>
  25. #include <starpu_cuda.h>
  26. #include <stdlib.h>
  27. #include "../helper.h"
  28. #define NLOOPS 1000
  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[], __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. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  42. }
  43. #endif
  44. static void cpu_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  45. {
  46. STARPU_SKIP_IF_VALGRIND;
  47. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  48. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  49. memset(buf, 42, length);
  50. }
  51. static struct starpu_codelet memset_cl =
  52. {
  53. .where = STARPU_CPU|STARPU_CUDA,
  54. .cpu_funcs = {cpu_memset_codelet, NULL},
  55. #ifdef STARPU_USE_CUDA
  56. .cuda_funcs = {cuda_memset_codelet, NULL},
  57. #endif
  58. .nbuffers = 1,
  59. .modes = {STARPU_W}
  60. };
  61. /*
  62. * Check content
  63. */
  64. static void cpu_check_content_codelet(void *descr[], __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. unsigned i;
  70. for (i = 0; i < length; i++)
  71. {
  72. if (buf[i] != 42)
  73. {
  74. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  75. exit(-1);
  76. }
  77. }
  78. }
  79. static struct starpu_codelet check_content_cl =
  80. {
  81. .where = STARPU_CPU,
  82. .cpu_funcs = {cpu_check_content_codelet, NULL},
  83. .nbuffers = 1,
  84. .modes = {STARPU_R}
  85. };
  86. int main(int argc, char **argv)
  87. {
  88. int ret;
  89. ret = starpu_init(NULL);
  90. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  92. /* The buffer should never be explicitely allocated */
  93. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  94. unsigned loop;
  95. for (loop = 0; loop < NLOOPS; loop++)
  96. {
  97. struct starpu_task *memset_task;
  98. struct starpu_task *check_content_task;
  99. memset_task = starpu_task_create();
  100. memset_task->cl = &memset_cl;
  101. memset_task->handles[0] = v_handle;
  102. memset_task->detach = 0;
  103. ret = starpu_task_submit(memset_task);
  104. if (ret == -ENODEV) goto enodev;
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  106. ret = starpu_task_wait(memset_task);
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  108. check_content_task = starpu_task_create();
  109. check_content_task->cl = &check_content_cl;
  110. check_content_task->handles[0] = v_handle;
  111. check_content_task->detach = 0;
  112. ret = starpu_task_submit(check_content_task);
  113. if (ret == -ENODEV) goto enodev;
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  115. ret = starpu_task_wait(check_content_task);
  116. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  117. starpu_data_invalidate(v_handle);
  118. }
  119. /* this should get rid of automatically allocated buffers */
  120. starpu_data_unregister(v_handle);
  121. starpu_shutdown();
  122. return EXIT_SUCCESS;
  123. enodev:
  124. starpu_data_unregister(v_handle);
  125. fprintf(stderr, "WARNING: No one can execute this task\n");
  126. /* yes, we do not perform the computation but we did detect that no one
  127. * could perform the kernel, so this is not an error from StarPU */
  128. starpu_shutdown();
  129. return STARPU_TEST_SKIPPED;
  130. }