data_invalidation.c 3.9 KB

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