[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / flang / runtime / CUDA / allocator.cpp
blob85b3daf65a8ba4fc20dc2e2e6a4d936428da2f58
1 //===-- runtime/CUDA/allocator.cpp ----------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "flang/Runtime/CUDA/allocator.h"
10 #include "../derived.h"
11 #include "../stat.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 {
22 extern "C" {
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) {
37 void *p;
38 CUDA_REPORT_IF_ERROR(cudaMallocHost((void **)&p, sizeInBytes));
39 return p;
42 void CUFFreePinned(void *p) { CUDA_REPORT_IF_ERROR(cudaFreeHost(p)); }
44 void *CUFAllocDevice(std::size_t sizeInBytes) {
45 void *p;
46 CUDA_REPORT_IF_ERROR(cudaMalloc(&p, sizeInBytes));
47 return p;
50 void CUFFreeDevice(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); }
52 void *CUFAllocManaged(std::size_t sizeInBytes) {
53 void *p;
54 CUDA_REPORT_IF_ERROR(
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.
68 CUFFreeManaged(p);
71 } // namespace Fortran::runtime::cuda