data_invalidation.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #define NLOOPS 1000
  23. #define VECTORSIZE 1024
  24. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  25. static starpu_data_handle 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 starpu_codelet memset_cl = {
  45. .where = STARPU_CPU|STARPU_CUDA,
  46. .cpu_func = cpu_memset_codelet,
  47. #ifdef STARPU_USE_CUDA
  48. .cuda_func = cuda_memset_codelet,
  49. #endif
  50. .nbuffers = 1
  51. };
  52. /*
  53. * Check content
  54. */
  55. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  56. {
  57. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  58. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  59. unsigned i;
  60. for (i = 0; i < length; i++)
  61. {
  62. if (buf[i] != 42)
  63. {
  64. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  65. exit(-1);
  66. }
  67. }
  68. }
  69. static starpu_codelet check_content_cl = {
  70. .where = STARPU_CPU,
  71. .cpu_func = cpu_check_content_codelet,
  72. .nbuffers = 1
  73. };
  74. int main(int argc, char **argv)
  75. {
  76. int ret;
  77. starpu_init(NULL);
  78. /* The buffer should never be explicitely allocated */
  79. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  80. unsigned loop;
  81. for (loop = 0; loop < NLOOPS; loop++)
  82. {
  83. struct starpu_task *memset_task;
  84. struct starpu_task *check_content_task;
  85. memset_task = starpu_task_create();
  86. memset_task->cl = &memset_cl;
  87. memset_task->buffers[0].handle = v_handle;
  88. memset_task->buffers[0].mode = STARPU_W;
  89. memset_task->detach = 0;
  90. ret = starpu_task_submit(memset_task);
  91. if (ret == -ENODEV)
  92. goto enodev;
  93. ret = starpu_task_wait(memset_task);
  94. if (ret)
  95. exit(-1);
  96. check_content_task = starpu_task_create();
  97. check_content_task->cl = &check_content_cl;
  98. check_content_task->buffers[0].handle = v_handle;
  99. check_content_task->buffers[0].mode = STARPU_R;
  100. check_content_task->detach = 0;
  101. ret = starpu_task_submit(check_content_task);
  102. if (ret == -ENODEV)
  103. goto enodev;
  104. ret = starpu_task_wait(check_content_task);
  105. if (ret)
  106. exit(-1);
  107. starpu_data_invalidate(v_handle);
  108. }
  109. /* this should get rid of automatically allocated buffers */
  110. starpu_data_unregister(v_handle);
  111. starpu_shutdown();
  112. return 0;
  113. enodev:
  114. fprintf(stderr, "WARNING: No one can execute this task\n");
  115. /* yes, we do not perform the computation but we did detect that no one
  116. * could perform the kernel, so this is not an error from StarPU */
  117. return 77;
  118. }