Skip to content

[flang][cuda] Add function to allocate and deallocate device module variable #109213

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 6 commits into from
Sep 19, 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
34 changes: 34 additions & 0 deletions flang/include/flang/Runtime/CUDA/allocatable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===-- include/flang/Runtime/CUDA/allocatable.h ----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef FORTRAN_RUNTIME_CUDA_ALLOCATABLE_H_
#define FORTRAN_RUNTIME_CUDA_ALLOCATABLE_H_

#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/entry-names.h"

namespace Fortran::runtime::cuda {

extern "C" {

/// Perform allocation of the descriptor with synchronization of it when
/// necessary.
int RTDECL(CUFAllocatableAllocate)(Descriptor &, bool hasStat = false,
const Descriptor *errMsg = nullptr, const char *sourceFile = nullptr,
int sourceLine = 0);

/// Perform deallocation of the descriptor with synchronization of it when
/// necessary.
int RTDECL(CUFAllocatableDeallocate)(Descriptor &, bool hasStat = false,
const Descriptor *errMsg = nullptr, const char *sourceFile = nullptr,
int sourceLine = 0);

} // extern "C"

} // namespace Fortran::runtime::cuda
#endif // FORTRAN_RUNTIME_CUDA_ALLOCATABLE_H_
11 changes: 0 additions & 11 deletions flang/include/flang/Runtime/CUDA/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/entry-names.h"

#define CUDA_REPORT_IF_ERROR(expr) \
[](cudaError_t err) { \
if (err == cudaSuccess) \
return; \
const char *name = cudaGetErrorName(err); \
if (!name) \
name = "<unknown>"; \
Terminator terminator{__FILE__, __LINE__}; \
terminator.Crash("'%s' failed with '%s'", #expr, name); \
}(expr)

namespace Fortran::runtime::cuda {

extern "C" {
Expand Down
30 changes: 30 additions & 0 deletions flang/include/flang/Runtime/CUDA/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- include/flang/Runtime/CUDA/common.h ------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef FORTRAN_RUNTIME_CUDA_COMMON_H_
#define FORTRAN_RUNTIME_CUDA_COMMON_H_

#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/entry-names.h"

static constexpr unsigned kHostToDevice = 0;
static constexpr unsigned kDeviceToHost = 1;
static constexpr unsigned kDeviceToDevice = 2;

#define CUDA_REPORT_IF_ERROR(expr) \
[](cudaError_t err) { \
if (err == cudaSuccess) \
return; \
const char *name = cudaGetErrorName(err); \
if (!name) \
name = "<unknown>"; \
Terminator terminator{__FILE__, __LINE__}; \
terminator.Crash("'%s' failed with '%s'", #expr, name); \
}(expr)

#endif // FORTRAN_RUNTIME_CUDA_COMMON_H_
13 changes: 11 additions & 2 deletions flang/include/flang/Runtime/CUDA/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ namespace Fortran::runtime::cuda {

extern "C" {

// Allocate a descriptor in managed.
/// Allocate a descriptor in managed.
Descriptor *RTDECL(CUFAllocDesciptor)(
std::size_t, const char *sourceFile = nullptr, int sourceLine = 0);

// Deallocate a descriptor allocated in managed or unified memory.
/// Deallocate a descriptor allocated in managed or unified memory.
void RTDECL(CUFFreeDesciptor)(
Descriptor *, const char *sourceFile = nullptr, int sourceLine = 0);

/// Retrieve the device pointer from the host one.
void *RTDECL(CUFGetDeviceAddress)(
void *hostPtr, const char *sourceFile = nullptr, int sourceLine = 0);

/// Sync the \p src descriptor to the \p dst descriptor.
void RTDECL(CUFDescriptorSync)(Descriptor *dst, const Descriptor *src,
const char *sourceFile = nullptr, int sourceLine = 0);

} // extern "C"

} // namespace Fortran::runtime::cuda
#endif // FORTRAN_RUNTIME_CUDA_DESCRIPTOR_H_
4 changes: 0 additions & 4 deletions flang/include/flang/Runtime/CUDA/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#include "flang/Runtime/entry-names.h"
#include <cstddef>

static constexpr unsigned kHostToDevice = 0;
static constexpr unsigned kDeviceToHost = 1;
static constexpr unsigned kDeviceToDevice = 2;

namespace Fortran::runtime::cuda {

extern "C" {
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/CufOpConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "flang/Optimizer/Dialect/FIROps.h"
#include "flang/Optimizer/HLFIR/HLFIROps.h"
#include "flang/Optimizer/Support/DataLayout.h"
#include "flang/Runtime/CUDA/common.h"
#include "flang/Runtime/CUDA/descriptor.h"
#include "flang/Runtime/CUDA/memory.h"
#include "flang/Runtime/allocatable.h"
Expand Down
4 changes: 4 additions & 0 deletions flang/runtime/CUDA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ set(CUFRT_LIBNAME CufRuntime_cuda_${CUDAToolkit_VERSION_MAJOR})

add_flang_library(${CUFRT_LIBNAME}
allocator.cpp
allocatable.cpp
descriptor.cpp
memory.cpp

LINK_COMPONENTS
Support
)

if (BUILD_SHARED_LIBS)
Expand Down
71 changes: 71 additions & 0 deletions flang/runtime/CUDA/allocatable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===-- runtime/CUDA/allocatable.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "flang/Runtime/CUDA/allocatable.h"
#include "../stat.h"
#include "../terminator.h"
#include "flang/Runtime/CUDA/common.h"
#include "flang/Runtime/CUDA/descriptor.h"
#include "flang/Runtime/allocatable.h"
#include "llvm/Support/ErrorHandling.h"

#include "cuda_runtime.h"

namespace Fortran::runtime::cuda {

extern "C" {
RT_EXT_API_GROUP_BEGIN

int RTDEF(CUFAllocatableAllocate)(Descriptor &desc, bool hasStat,
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
if (desc.HasAddendum()) {
Terminator terminator{sourceFile, sourceLine};
// TODO: This require a bit more work to set the correct type descriptor
// address
terminator.Crash(
"not yet implemented: CUDA descriptor allocation with addendum");
}
// Perform the standard allocation.
int stat{RTNAME(AllocatableAllocate)(
desc, hasStat, errMsg, sourceFile, sourceLine)};
#ifndef RT_DEVICE_COMPILATION
// Descriptor synchronization is only done when the allocation is done
// from the host.
if (stat == StatOk) {
void *deviceAddr{
RTNAME(CUFGetDeviceAddress)((void *)&desc, sourceFile, sourceLine)};
RTNAME(CUFDescriptorSync)
((Descriptor *)deviceAddr, &desc, sourceFile, sourceLine);
}
#endif
return stat;
}

int RTDEF(CUFAllocatableDeallocate)(Descriptor &desc, bool hasStat,
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
// Perform the standard allocation.
int stat{RTNAME(AllocatableDeallocate)(
desc, hasStat, errMsg, sourceFile, sourceLine)};
#ifndef RT_DEVICE_COMPILATION
// Descriptor synchronization is only done when the deallocation is done
// from the host.
if (stat == StatOk) {
void *deviceAddr{
RTNAME(CUFGetDeviceAddress)((void *)&desc, sourceFile, sourceLine)};
RTNAME(CUFDescriptorSync)
((Descriptor *)deviceAddr, &desc, sourceFile, sourceLine);
}
#endif
return stat;
}

RT_EXT_API_GROUP_END

} // extern "C"

} // namespace Fortran::runtime::cuda
1 change: 1 addition & 0 deletions flang/runtime/CUDA/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../type-info.h"
#include "flang/Common/Fortran.h"
#include "flang/ISO_Fortran_binding_wrapper.h"
#include "flang/Runtime/CUDA/common.h"
#include "flang/Runtime/allocator-registry.h"

#include "cuda_runtime.h"
Expand Down
22 changes: 22 additions & 0 deletions flang/runtime/CUDA/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
//===----------------------------------------------------------------------===//

#include "flang/Runtime/CUDA/descriptor.h"
#include "../terminator.h"
#include "flang/Runtime/CUDA/allocator.h"
#include "flang/Runtime/CUDA/common.h"

#include "cuda_runtime.h"

namespace Fortran::runtime::cuda {
extern "C" {
Expand All @@ -23,6 +27,24 @@ void RTDEF(CUFFreeDesciptor)(
CUFFreeManaged(reinterpret_cast<void *>(desc));
}

void *RTDEF(CUFGetDeviceAddress)(
void *hostPtr, const char *sourceFile, int sourceLine) {
Terminator terminator{sourceFile, sourceLine};
void *p;
CUDA_REPORT_IF_ERROR(cudaGetSymbolAddress((void **)&p, hostPtr));
if (!p) {
terminator.Crash("Could not retrieve symbol's address");
}
return p;
}

void RTDEF(CUFDescriptorSync)(Descriptor *dst, const Descriptor *src,
const char *sourceFile, int sourceLine) {
std::size_t count{src->SizeInBytes()};
CUDA_REPORT_IF_ERROR(cudaMemcpy(
(void *)dst, (const void *)src, count, cudaMemcpyHostToDevice));
}

RT_EXT_API_GROUP_END
}
} // namespace Fortran::runtime::cuda
60 changes: 60 additions & 0 deletions flang/unittests/Runtime/CUDA/Allocatable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===-- flang/unittests/Runtime/Allocatable.cpp ------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "flang/Runtime/allocatable.h"
#include "gtest/gtest.h"
#include "../../../runtime/terminator.h"
#include "flang/Common/Fortran.h"
#include "flang/Runtime/CUDA/allocator.h"
#include "flang/Runtime/CUDA/common.h"
#include "flang/Runtime/CUDA/descriptor.h"
#include "flang/Runtime/allocator-registry.h"

#include "cuda_runtime.h"

using namespace Fortran::runtime;
using namespace Fortran::runtime::cuda;

static OwningPtr<Descriptor> createAllocatable(
Fortran::common::TypeCategory tc, int kind, int rank = 1) {
return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr,
CFI_attribute_allocatable);
}

TEST(AllocatableCUFTest, SimpleDeviceAllocatable) {
using Fortran::common::TypeCategory;
RTNAME(CUFRegisterAllocator)();
// REAL(4), DEVICE, ALLOCATABLE :: a(:)
auto a{createAllocatable(TypeCategory::Real, 4)};
a->SetAllocIdx(kDeviceAllocatorPos);
EXPECT_EQ((int)kDeviceAllocatorPos, a->GetAllocIdx());
EXPECT_FALSE(a->HasAddendum());
RTNAME(AllocatableSetBounds)(*a, 0, 1, 10);

// Emulate a device descriptor for the purpose of unit testing part of the
// code.
Descriptor *device_desc;
CUDA_REPORT_IF_ERROR(cudaMalloc(&device_desc, a->SizeInBytes()));

RTNAME(AllocatableAllocate)
(*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
EXPECT_TRUE(a->IsAllocated());
RTNAME(CUFDescriptorSync)(device_desc, a.get(), __FILE__, __LINE__);
cudaDeviceSynchronize();

EXPECT_EQ(cudaSuccess, cudaGetLastError());

RTNAME(AllocatableDeallocate)
(*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
EXPECT_FALSE(a->IsAllocated());

RTNAME(CUFDescriptorSync)(device_desc, a.get(), __FILE__, __LINE__);
cudaDeviceSynchronize();

EXPECT_EQ(cudaSuccess, cudaGetLastError());
}
8 changes: 8 additions & 0 deletions flang/unittests/Runtime/CUDA/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
if (FLANG_CUF_RUNTIME)

add_flang_unittest(FlangCufRuntimeTests
Allocatable.cpp
AllocatorCUF.cpp
)

if (BUILD_SHARED_LIBS)
set(CUDA_RT_TARGET CUDA::cudart)
else()
set(CUDA_RT_TARGET CUDA::cudart_static)
endif()

target_link_libraries(FlangCufRuntimeTests
PRIVATE
${CUDA_RT_TARGET}
CufRuntime_cuda_${CUDAToolkit_VERSION_MAJOR}
FortranRuntime
)
Expand Down
Loading