1 //===-- runtime/CUDA/allocator.cpp ----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "flang/Runtime/CUDA/allocator.h"
10 #include "../derived.h"
12 #include "../terminator.h"
13 #include "../type-info.h"
14 #include "flang/Common/Fortran.h"
15 #include "flang/ISO_Fortran_binding_wrapper.h"
16 #include "flang/Runtime/CUDA/common.h"
17 #include "flang/Runtime/allocator-registry.h"
19 #include "cuda_runtime.h"
21 namespace Fortran::runtime::cuda
{
24 void RTDEF(CUFRegisterAllocator
)() {
25 allocatorRegistry
.Register(
26 kPinnedAllocatorPos
, {&CUFAllocPinned
, CUFFreePinned
});
27 allocatorRegistry
.Register(
28 kDeviceAllocatorPos
, {&CUFAllocDevice
, CUFFreeDevice
});
29 allocatorRegistry
.Register(
30 kManagedAllocatorPos
, {&CUFAllocManaged
, CUFFreeManaged
});
31 allocatorRegistry
.Register(
32 kUnifiedAllocatorPos
, {&CUFAllocUnified
, CUFFreeUnified
});
36 void *CUFAllocPinned(std::size_t sizeInBytes
) {
38 CUDA_REPORT_IF_ERROR(cudaMallocHost((void **)&p
, sizeInBytes
));
42 void CUFFreePinned(void *p
) { CUDA_REPORT_IF_ERROR(cudaFreeHost(p
)); }
44 void *CUFAllocDevice(std::size_t sizeInBytes
) {
46 CUDA_REPORT_IF_ERROR(cudaMalloc(&p
, sizeInBytes
));
50 void CUFFreeDevice(void *p
) { CUDA_REPORT_IF_ERROR(cudaFree(p
)); }
52 void *CUFAllocManaged(std::size_t sizeInBytes
) {
55 cudaMallocManaged((void **)&p
, sizeInBytes
, cudaMemAttachGlobal
));
56 return reinterpret_cast<void *>(p
);
59 void CUFFreeManaged(void *p
) { CUDA_REPORT_IF_ERROR(cudaFree(p
)); }
61 void *CUFAllocUnified(std::size_t sizeInBytes
) {
62 // Call alloc managed for the time being.
63 return CUFAllocManaged(sizeInBytes
);
66 void CUFFreeUnified(void *p
) {
67 // Call free managed for the time being.
71 } // namespace Fortran::runtime::cuda