lazy_allocation.c 3.1 KB

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