data_invalidation.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012 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. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <starpu_cuda.h>
  23. #ifdef STARPU_USE_OPENCL
  24. #include <starpu_opencl.h>
  25. #endif
  26. #include <stdlib.h>
  27. #include "../helper.h"
  28. #ifdef STARPU_SLOW_MACHINE
  29. #define NLOOPS 100
  30. #else
  31. #define NLOOPS 1000
  32. #endif
  33. #define VECTORSIZE 1024
  34. static starpu_data_handle_t v_handle;
  35. /*
  36. * Memset
  37. */
  38. #ifdef STARPU_USE_CUDA
  39. static void cuda_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  40. {
  41. STARPU_SKIP_IF_VALGRIND;
  42. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  43. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  44. cudaMemsetAsync(buf, 42, length, starpu_cuda_get_local_stream());
  45. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  46. }
  47. #endif
  48. #ifdef STARPU_USE_OPENCL
  49. static void opencl_memset_codelet(void *buffers[], void *args)
  50. {
  51. (void) args;
  52. cl_command_queue queue;
  53. int id = starpu_worker_get_id();
  54. int devid = starpu_worker_get_devid(id);
  55. starpu_opencl_get_queue(devid, &queue);
  56. cl_mem buffer = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  57. unsigned length = STARPU_VECTOR_GET_NX(buffers[0]);
  58. char *v = malloc(length);
  59. STARPU_ASSERT(v != NULL);
  60. memset(v, 42, length);
  61. clEnqueueWriteBuffer(queue,
  62. buffer,
  63. CL_TRUE,
  64. 0, /* offset */
  65. length, /* sizeof (char) */
  66. v,
  67. 0, /* num_events_in_wait_list */
  68. NULL, /* event_wait_list */
  69. NULL /* event */);
  70. }
  71. #endif /* !STARPU_USE_OPENCL */
  72. static void cpu_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  73. {
  74. STARPU_SKIP_IF_VALGRIND;
  75. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  76. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  77. memset(buf, 42, length * sizeof(*buf));
  78. }
  79. static struct starpu_codelet memset_cl =
  80. {
  81. .cpu_funcs = {cpu_memset_codelet, NULL},
  82. #ifdef STARPU_USE_CUDA
  83. .cuda_funcs = {cuda_memset_codelet, NULL},
  84. #endif
  85. #ifdef STARPU_USE_OPENCL
  86. .opencl_funcs = {opencl_memset_codelet, NULL},
  87. #endif
  88. .nbuffers = 1,
  89. .modes = {STARPU_W}
  90. };
  91. /*
  92. * Check content
  93. */
  94. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  95. {
  96. STARPU_SKIP_IF_VALGRIND;
  97. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  98. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  99. unsigned i;
  100. for (i = 0; i < length; i++)
  101. {
  102. if (buf[i] != 42)
  103. {
  104. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  105. exit(-1);
  106. }
  107. }
  108. }
  109. static struct starpu_codelet check_content_cl =
  110. {
  111. .where = STARPU_CPU,
  112. .cpu_funcs = {cpu_check_content_codelet, NULL},
  113. .nbuffers = 1,
  114. .modes = {STARPU_R}
  115. };
  116. int main(int argc, char **argv)
  117. {
  118. int ret;
  119. ret = starpu_init(NULL);
  120. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  122. /* The buffer should never be explicitely allocated */
  123. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  124. unsigned loop;
  125. for (loop = 0; loop < NLOOPS; loop++)
  126. {
  127. struct starpu_task *memset_task;
  128. struct starpu_task *check_content_task;
  129. memset_task = starpu_task_create();
  130. memset_task->cl = &memset_cl;
  131. memset_task->handles[0] = v_handle;
  132. memset_task->detach = 0;
  133. ret = starpu_task_submit(memset_task);
  134. if (ret == -ENODEV) goto enodev;
  135. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  136. ret = starpu_task_wait(memset_task);
  137. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  138. check_content_task = starpu_task_create();
  139. check_content_task->cl = &check_content_cl;
  140. check_content_task->handles[0] = v_handle;
  141. check_content_task->detach = 0;
  142. ret = starpu_task_submit(check_content_task);
  143. if (ret == -ENODEV) goto enodev;
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  145. ret = starpu_task_wait(check_content_task);
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  147. starpu_data_invalidate(v_handle);
  148. }
  149. /* this should get rid of automatically allocated buffers */
  150. starpu_data_unregister(v_handle);
  151. starpu_shutdown();
  152. return EXIT_SUCCESS;
  153. enodev:
  154. starpu_data_unregister(v_handle);
  155. fprintf(stderr, "WARNING: No one can execute this task\n");
  156. /* yes, we do not perform the computation but we did detect that no one
  157. * could perform the kernel, so this is not an error from StarPU */
  158. starpu_shutdown();
  159. return STARPU_TEST_SKIPPED;
  160. }