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 //===----------------------------------------------------------------------===// 8 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" 18 19 #include "cuda_runtime.h" 20 21 namespace Fortran::runtime::cuda { 22 extern "C" { 23 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}); 33 } 34 } 35 36 void *CUFAllocPinned(std::size_t sizeInBytes) { 37 void *p; 38 CUDA_REPORT_IF_ERROR(cudaMallocHost((void **)&p, sizeInBytes)); 39 return p; 40 } 41 42 void CUFFreePinned(void *p) { CUDA_REPORT_IF_ERROR(cudaFreeHost(p)); } 43 44 void *CUFAllocDevice(std::size_t sizeInBytes) { 45 void *p; 46 CUDA_REPORT_IF_ERROR(cudaMalloc(&p, sizeInBytes)); 47 return p; 48 } 49 50 void CUFFreeDevice(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); } 51 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); 57 } 58 59 void CUFFreeManaged(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); } 60 61 void *CUFAllocUnified(std::size_t sizeInBytes) { 62 // Call alloc managed for the time being. 63 return CUFAllocManaged(sizeInBytes); 64 } 65 66 void CUFFreeUnified(void *p) { 67 // Call free managed for the time being. 68 CUFFreeManaged(p); 69 } 70 71 } // namespace Fortran::runtime::cuda 72