data_invalidation.c 3.4 KB

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