malloc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <errno.h>
  17. #include <core/workers.h>
  18. #include <common/config.h>
  19. #include <starpu.h>
  20. #ifdef USE_CUDA
  21. #include <cuda.h>
  22. #endif
  23. #ifdef USE_CUDA
  24. struct malloc_pinned_codelet_struct {
  25. void **ptr;
  26. size_t dim;
  27. };
  28. static void malloc_pinned_codelet(void *buffers[] __attribute__((unused)), void *arg)
  29. {
  30. struct malloc_pinned_codelet_struct *s = arg;
  31. cudaError_t cures;
  32. cures = cudaHostAlloc((void **)(s->ptr), s->dim, cudaHostAllocPortable);
  33. if (STARPU_UNLIKELY(cures))
  34. CUDA_REPORT_ERROR(cures);
  35. }
  36. static starpu_codelet malloc_pinned_cl = {
  37. .where = CUDA,
  38. .cuda_func = malloc_pinned_codelet,
  39. .model = NULL,
  40. .nbuffers = 0
  41. };
  42. #endif
  43. int starpu_malloc_pinned_if_possible(void **A, size_t dim)
  44. {
  45. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  46. return -EDEADLK;
  47. STARPU_ASSERT(A);
  48. if (may_submit_cuda_task())
  49. {
  50. #ifdef USE_CUDA
  51. int push_res;
  52. struct malloc_pinned_codelet_struct s = {
  53. .ptr = A,
  54. .dim = dim
  55. };
  56. struct starpu_task *task = starpu_task_create();
  57. task->callback_func = NULL;
  58. task->cl = &malloc_pinned_cl;
  59. task->cl_arg = &s;
  60. task->synchronous = 1;
  61. push_res = starpu_submit_task(task);
  62. STARPU_ASSERT(push_res != -ENODEV);
  63. #endif
  64. }
  65. else {
  66. *A = malloc(dim);
  67. }
  68. STARPU_ASSERT(*A);
  69. return 0;
  70. }
  71. #ifdef USE_CUDA
  72. static void free_pinned_codelet(void *buffers[] __attribute__((unused)), void *arg)
  73. {
  74. cudaError_t cures;
  75. cures = cudaFreeHost(arg);
  76. if (STARPU_UNLIKELY(cures))
  77. CUDA_REPORT_ERROR(cures);
  78. }
  79. static starpu_codelet free_pinned_cl = {
  80. .where = CUDA,
  81. .cuda_func = free_pinned_codelet,
  82. .model = NULL,
  83. .nbuffers = 0
  84. };
  85. #endif
  86. int starpu_free_pinned_if_possible(void *A)
  87. {
  88. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  89. return -EDEADLK;
  90. if (may_submit_cuda_task())
  91. {
  92. #ifdef USE_CUDA
  93. int push_res;
  94. struct starpu_task *task = starpu_task_create();
  95. task->callback_func = NULL;
  96. task->cl = &free_pinned_cl;
  97. task->cl_arg = A;
  98. task->synchronous = 1;
  99. push_res = starpu_submit_task(task);
  100. STARPU_ASSERT(push_res != -ENODEV);
  101. #endif
  102. }
  103. else {
  104. free(A);
  105. }
  106. return 0;
  107. }