Skip to content

[flang][cuda] Use cuda runtime API #103488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions flang/include/flang/Runtime/CUDA/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
#include "flang/Runtime/entry-names.h"

#define CUDA_REPORT_IF_ERROR(expr) \
[](CUresult result) { \
if (!result) \
[](cudaError_t err) { \
if (err == cudaSuccess) \
return; \
const char *name = nullptr; \
cuGetErrorName(result, &name); \
const char *name = cudaGetErrorName(err); \
if (!name) \
name = "<unknown>"; \
Terminator terminator{__FILE__, __LINE__}; \
Expand Down
10 changes: 8 additions & 2 deletions flang/runtime/CUDA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
#===------------------------------------------------------------------------===#

include_directories(${CUDAToolkit_INCLUDE_DIRS})
find_library(CUDA_RUNTIME_LIBRARY cuda HINTS ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES} REQUIRED)

add_flang_library(CufRuntime
allocator.cpp
descriptor.cpp
)

if (BUILD_SHARED_LIBS)
set(CUF_LIBRARY ${CUDA_LIBRARIES})
else()
set(CUF_LIBRARY ${CUDA_cudart_static_LIBRARY})
endif()

target_link_libraries(CufRuntime
PRIVATE
FortranRuntime
${CUDA_RUNTIME_LIBRARY}
${CUF_LIBRARY}
)
24 changes: 10 additions & 14 deletions flang/runtime/CUDA/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "flang/ISO_Fortran_binding_wrapper.h"
#include "flang/Runtime/allocator-registry.h"

#include "cuda.h"
#include "cuda_runtime.h"

namespace Fortran::runtime::cuda {
extern "C" {
Expand All @@ -34,32 +34,28 @@ void RTDEF(CUFRegisterAllocator)() {

void *CUFAllocPinned(std::size_t sizeInBytes) {
void *p;
CUDA_REPORT_IF_ERROR(cuMemAllocHost(&p, sizeInBytes));
CUDA_REPORT_IF_ERROR(cudaMallocHost((void **)&p, sizeInBytes));
return p;
}

void CUFFreePinned(void *p) { CUDA_REPORT_IF_ERROR(cuMemFreeHost(p)); }
void CUFFreePinned(void *p) { CUDA_REPORT_IF_ERROR(cudaFreeHost(p)); }

void *CUFAllocDevice(std::size_t sizeInBytes) {
CUdeviceptr p = 0;
CUDA_REPORT_IF_ERROR(cuMemAlloc(&p, sizeInBytes));
return reinterpret_cast<void *>(p);
void *p;
CUDA_REPORT_IF_ERROR(cudaMalloc(&p, sizeInBytes));
return p;
}

void CUFFreeDevice(void *p) {
CUDA_REPORT_IF_ERROR(cuMemFree(reinterpret_cast<CUdeviceptr>(p)));
}
void CUFFreeDevice(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); }

void *CUFAllocManaged(std::size_t sizeInBytes) {
CUdeviceptr p = 0;
void *p;
CUDA_REPORT_IF_ERROR(
cuMemAllocManaged(&p, sizeInBytes, CU_MEM_ATTACH_GLOBAL));
cudaMallocManaged((void **)&p, sizeInBytes, cudaMemAttachGlobal));
return reinterpret_cast<void *>(p);
}

void CUFFreeManaged(void *p) {
CUDA_REPORT_IF_ERROR(cuMemFree(reinterpret_cast<CUdeviceptr>(p)));
}
void CUFFreeManaged(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); }

void *CUFAllocUnified(std::size_t sizeInBytes) {
// Call alloc managed for the time being.
Expand Down
33 changes: 1 addition & 32 deletions flang/unittests/Runtime/CUDA/AllocatorCUF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "flang/Runtime/allocatable.h"
#include "flang/Runtime/allocator-registry.h"

#include "cuda.h"
#include "cuda_runtime.h"

using namespace Fortran::runtime;
using namespace Fortran::runtime::cuda;
Expand All @@ -25,38 +25,9 @@ static OwningPtr<Descriptor> createAllocatable(
CFI_attribute_allocatable);
}

thread_local static int32_t defaultDevice = 0;

CUdevice getDefaultCuDevice() {
CUdevice device;
CUDA_REPORT_IF_ERROR(cuDeviceGet(&device, /*ordinal=*/defaultDevice));
return device;
}

class ScopedContext {
public:
ScopedContext() {
// Static reference to CUDA primary context for device ordinal
// defaultDevice.
static CUcontext context = [] {
CUDA_REPORT_IF_ERROR(cuInit(/*flags=*/0));
CUcontext ctx;
// Note: this does not affect the current context.
CUDA_REPORT_IF_ERROR(
cuDevicePrimaryCtxRetain(&ctx, getDefaultCuDevice()));
return ctx;
}();

CUDA_REPORT_IF_ERROR(cuCtxPushCurrent(context));
}

~ScopedContext() { CUDA_REPORT_IF_ERROR(cuCtxPopCurrent(nullptr)); }
};

TEST(AllocatableCUFTest, SimpleDeviceAllocate) {
using Fortran::common::TypeCategory;
RTNAME(CUFRegisterAllocator)();
ScopedContext ctx;
// REAL(4), DEVICE, ALLOCATABLE :: a(:)
auto a{createAllocatable(TypeCategory::Real, 4)};
a->SetAllocIdx(kDeviceAllocatorPos);
Expand All @@ -74,7 +45,6 @@ TEST(AllocatableCUFTest, SimpleDeviceAllocate) {
TEST(AllocatableCUFTest, SimplePinnedAllocate) {
using Fortran::common::TypeCategory;
RTNAME(CUFRegisterAllocator)();
ScopedContext ctx;
// INTEGER(4), PINNED, ALLOCATABLE :: a(:)
auto a{createAllocatable(TypeCategory::Integer, 4)};
EXPECT_FALSE(a->HasAddendum());
Expand All @@ -93,7 +63,6 @@ TEST(AllocatableCUFTest, SimplePinnedAllocate) {
TEST(AllocatableCUFTest, DescriptorAllocationTest) {
using Fortran::common::TypeCategory;
RTNAME(CUFRegisterAllocator)();
ScopedContext ctx;
// REAL(4), DEVICE, ALLOCATABLE :: a(:)
auto a{createAllocatable(TypeCategory::Real, 4)};
Descriptor *desc = nullptr;
Expand Down
Loading