lazy_allocation.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 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 <config.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #include <starpu_cuda.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  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. STARPU_SKIP_IF_VALGRIND;
  33. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  34. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  35. cudaMemsetAsync(buf, 42, length, starpu_cuda_get_local_stream());
  36. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  37. }
  38. #endif
  39. static void cpu_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. memset(buf, 42, length);
  45. }
  46. static struct starpu_codelet memset_cl =
  47. {
  48. .where = STARPU_CPU|STARPU_CUDA,
  49. .cpu_funcs = {cpu_memset_codelet, NULL},
  50. #ifdef STARPU_USE_CUDA
  51. .cuda_funcs = {cuda_memset_codelet, NULL},
  52. #endif
  53. .nbuffers = 1,
  54. .modes = {STARPU_W}
  55. };
  56. /*
  57. * Check content
  58. */
  59. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  60. {
  61. STARPU_SKIP_IF_VALGRIND;
  62. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  63. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  64. unsigned i;
  65. for (i = 0; i < length; i++)
  66. {
  67. if (buf[i] != 42)
  68. {
  69. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  70. exit(-1);
  71. }
  72. }
  73. }
  74. #ifdef STARPU_USE_CUDA
  75. static void cuda_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  76. {
  77. STARPU_SKIP_IF_VALGRIND;
  78. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  79. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  80. unsigned i;
  81. for (i = 0; i < length; i++)
  82. {
  83. char dst;
  84. cudaMemcpy(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost);
  85. if (dst != 42)
  86. {
  87. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  88. exit(-1);
  89. }
  90. }
  91. }
  92. #endif
  93. static struct starpu_codelet check_content_cl =
  94. {
  95. .where = STARPU_CPU|STARPU_CUDA,
  96. .cpu_funcs = {cpu_check_content_codelet, NULL},
  97. #ifdef STARPU_USE_CUDA
  98. .cuda_funcs = {cuda_check_content_codelet, NULL},
  99. #endif
  100. .nbuffers = 1,
  101. .modes = {STARPU_R}
  102. };
  103. int main(int argc, char **argv)
  104. {
  105. int ret;
  106. ret = starpu_init(NULL);
  107. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  109. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  110. ret = starpu_insert_task(&memset_cl, STARPU_W, v_handle, 0);
  111. if (ret == -ENODEV)
  112. return STARPU_TEST_SKIPPED;
  113. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  114. ret = starpu_task_wait_for_all();
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  116. ret = starpu_insert_task(&check_content_cl, STARPU_R, v_handle, 0);
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  118. ret = starpu_task_wait_for_all();
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  120. starpu_data_unregister(v_handle);
  121. starpu_shutdown();
  122. return EXIT_SUCCESS;
  123. }