data_invalidation.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <starpu_cuda.h>
  21. #include <stdlib.h>
  22. #include "../helper.h"
  23. #define NLOOPS 1000
  24. #define VECTORSIZE 1024
  25. static starpu_data_handle_t v_handle;
  26. /*
  27. * Memset
  28. */
  29. #ifdef STARPU_USE_CUDA
  30. static void cuda_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  31. {
  32. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  33. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  34. cudaMemsetAsync(buf, 42, length, starpu_cuda_get_local_stream());
  35. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  36. }
  37. #endif
  38. static void cpu_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  39. {
  40. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  41. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  42. memset(buf, 42, length);
  43. }
  44. static struct starpu_codelet memset_cl =
  45. {
  46. .where = STARPU_CPU|STARPU_CUDA,
  47. .cpu_funcs = {cpu_memset_codelet, NULL},
  48. #ifdef STARPU_USE_CUDA
  49. .cuda_funcs = {cuda_memset_codelet, NULL},
  50. #endif
  51. .nbuffers = 1
  52. };
  53. /*
  54. * Check content
  55. */
  56. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  57. {
  58. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  59. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  60. unsigned i;
  61. for (i = 0; i < length; i++)
  62. {
  63. if (buf[i] != 42)
  64. {
  65. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  66. exit(-1);
  67. }
  68. }
  69. }
  70. static struct starpu_codelet check_content_cl =
  71. {
  72. .where = STARPU_CPU,
  73. .cpu_funcs = {cpu_check_content_codelet, NULL},
  74. .nbuffers = 1
  75. };
  76. int main(int argc, char **argv)
  77. {
  78. int ret;
  79. ret = starpu_init(NULL);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  81. /* The buffer should never be explicitely allocated */
  82. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  83. unsigned loop;
  84. for (loop = 0; loop < NLOOPS; loop++)
  85. {
  86. struct starpu_task *memset_task;
  87. struct starpu_task *check_content_task;
  88. memset_task = starpu_task_create();
  89. memset_task->cl = &memset_cl;
  90. memset_task->buffers[0].handle = v_handle;
  91. memset_task->buffers[0].mode = STARPU_W;
  92. memset_task->detach = 0;
  93. ret = starpu_task_submit(memset_task);
  94. if (ret == -ENODEV) goto enodev;
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  96. ret = starpu_task_wait(memset_task);
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  98. check_content_task = starpu_task_create();
  99. check_content_task->cl = &check_content_cl;
  100. check_content_task->buffers[0].handle = v_handle;
  101. check_content_task->buffers[0].mode = STARPU_R;
  102. check_content_task->detach = 0;
  103. ret = starpu_task_submit(check_content_task);
  104. if (ret == -ENODEV) goto enodev;
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  106. ret = starpu_task_wait(check_content_task);
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  108. starpu_data_invalidate(v_handle);
  109. }
  110. /* this should get rid of automatically allocated buffers */
  111. starpu_data_unregister(v_handle);
  112. starpu_shutdown();
  113. return EXIT_SUCCESS;
  114. enodev:
  115. starpu_data_unregister(v_handle);
  116. fprintf(stderr, "WARNING: No one can execute this task\n");
  117. /* yes, we do not perform the computation but we did detect that no one
  118. * could perform the kernel, so this is not an error from StarPU */
  119. starpu_shutdown();
  120. return STARPU_TEST_SKIPPED;
  121. }