lazy_allocation.c 3.6 KB

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