-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9fb92e2
[flang][cuda] Add function to allocate and deallocate device module v…
clementval 3547be8
Make GetDeviceAddress more generic
clementval a56f0e9
Fix call with RTDECL -> RTNAME
clementval 852ee90
clang-format
clementval b390c4e
More clang-format
clementval 5d91339
Fix comment
clementval File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the comment. Looks good otherwise.