data_invalidation.c 5.0 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_FALSE,
  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. clFinish(queue);
  71. }
  72. #endif /* !STARPU_USE_OPENCL */
  73. static void cpu_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  74. {
  75. STARPU_SKIP_IF_VALGRIND;
  76. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  77. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  78. memset(buf, 42, length * sizeof(*buf));
  79. }
  80. static struct starpu_codelet memset_cl =
  81. {
  82. .cpu_funcs = {cpu_memset_codelet, NULL},
  83. #ifdef STARPU_USE_CUDA
  84. .cuda_funcs = {cuda_memset_codelet, NULL},
  85. #endif
  86. #ifdef STARPU_USE_OPENCL
  87. .opencl_funcs = {opencl_memset_codelet, NULL},
  88. #endif
  89. .nbuffers = 1,
  90. .modes = {STARPU_W}
  91. };
  92. /*
  93. * Check content
  94. */
  95. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  96. {
  97. STARPU_SKIP_IF_VALGRIND;
  98. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  99. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  100. unsigned i;
  101. for (i = 0; i < length; i++)
  102. {
  103. if (buf[i] != 42)
  104. {
  105. FPRINTF(stderr, "buf[%u] is '%c' while it should be '%c'\n", i, buf[i], 42);
  106. exit(-1);
  107. }
  108. }
  109. }
  110. static struct starpu_codelet check_content_cl =
  111. {
  112. .where = STARPU_CPU,
  113. .cpu_funcs = {cpu_check_content_codelet, NULL},
  114. .nbuffers = 1,
  115. .modes = {STARPU_R}
  116. };
  117. int main(int argc, char **argv)
  118. {
  119. int ret;
  120. ret = starpu_init(NULL);
  121. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  123. /* The buffer should never be explicitely allocated */
  124. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  125. unsigned loop;
  126. for (loop = 0; loop < NLOOPS; loop++)
  127. {
  128. struct starpu_task *memset_task;
  129. struct starpu_task *check_content_task;
  130. memset_task = starpu_task_create();
  131. memset_task->cl = &memset_cl;
  132. memset_task->handles[0] = v_handle;
  133. memset_task->detach = 0;
  134. ret = starpu_task_submit(memset_task);
  135. if (ret == -ENODEV) goto enodev;
  136. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  137. ret = starpu_task_wait(memset_task);
  138. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  139. check_content_task = starpu_task_create();
  140. check_content_task->cl = &check_content_cl;
  141. check_content_task->handles[0] = v_handle;
  142. check_content_task->detach = 0;
  143. ret = starpu_task_submit(check_content_task);
  144. if (ret == -ENODEV) goto enodev;
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  146. ret = starpu_task_wait(check_content_task);
  147. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  148. starpu_data_invalidate(v_handle);
  149. }
  150. /* this should get rid of automatically allocated buffers */
  151. starpu_data_unregister(v_handle);
  152. starpu_shutdown();
  153. return EXIT_SUCCESS;
  154. enodev:
  155. starpu_data_unregister(v_handle);
  156. fprintf(stderr, "WARNING: No one can execute this task\n");
  157. /* yes, we do not perform the computation but we did detect that no one
  158. * could perform the kernel, so this is not an error from StarPU */
  159. starpu_shutdown();
  160. return STARPU_TEST_SKIPPED;
  161. }