lazy_allocation.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 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 "../common/helper.h"
  23. #define VECTORSIZE 1024
  24. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  25. static starpu_data_handle 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. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  33. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  34. cudaMemsetAsync(buf, 42, length, starpu_cuda_get_local_stream());
  35. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  36. }
  37. #endif
  38. static void cpu_memset_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  39. {
  40. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  41. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  42. memset(buf, 42, length);
  43. }
  44. static starpu_codelet memset_cl = {
  45. .where = STARPU_CPU|STARPU_CUDA,
  46. .cpu_func = cpu_memset_codelet,
  47. #ifdef STARPU_USE_CUDA
  48. .cuda_func = cuda_memset_codelet,
  49. #endif
  50. .nbuffers = 1
  51. };
  52. /*
  53. * Check content
  54. */
  55. static void cpu_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  56. {
  57. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  58. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  59. unsigned i;
  60. for (i = 0; i < length; i++)
  61. {
  62. if (buf[i] != 42)
  63. {
  64. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, buf[i], 42);
  65. exit(-1);
  66. }
  67. }
  68. }
  69. #ifdef STARPU_USE_CUDA
  70. static void cuda_check_content_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  71. {
  72. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  73. unsigned length = STARPU_VECTOR_GET_NX(descr[0]);
  74. unsigned i;
  75. for (i = 0; i < length; i++)
  76. {
  77. char dst;
  78. cudaMemcpy(&dst, &buf[i], sizeof(char), cudaMemcpyDeviceToHost);
  79. if (dst != 42)
  80. {
  81. FPRINTF(stderr, "buf[%u] is %c while it should be %c\n", i, dst, 42);
  82. exit(-1);
  83. }
  84. }
  85. }
  86. #endif
  87. static starpu_codelet check_content_cl = {
  88. .where = STARPU_CPU|STARPU_CUDA,
  89. .cpu_func = cpu_check_content_codelet,
  90. #ifdef STARPU_USE_CUDA
  91. .cuda_func = cuda_check_content_codelet,
  92. #endif
  93. .nbuffers = 1
  94. };
  95. int main(int argc, char **argv)
  96. {
  97. int ret;
  98. ret = starpu_init(NULL);
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  100. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  101. ret = starpu_insert_task(&memset_cl, STARPU_W, v_handle, 0);
  102. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  103. ret = starpu_task_wait_for_all();
  104. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  105. ret = starpu_insert_task(&check_content_cl, STARPU_R, v_handle, 0);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  107. ret = starpu_task_wait_for_all();
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  109. starpu_data_unregister(v_handle);
  110. starpu_shutdown();
  111. return 0;
  112. }