Skip to content

Commit 388b632

Browse files
authored
[flang][cuda] Defined allocator for unified data (llvm#102189)
CUDA unified variable where set to use the same allocator than managed variable. This patch adds a specific allocator for the unified variables. Currently it will call the managed allocator underneath but we want to have the flexibility to change that in the future.
1 parent 10d7805 commit 388b632

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

flang/include/flang/Runtime/CUDA/allocator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@ void CUFFreeDevice(void *);
3636
void *CUFAllocManaged(std::size_t);
3737
void CUFFreeManaged(void *);
3838

39+
void *CUFAllocUnified(std::size_t);
40+
void CUFFreeUnified(void *);
41+
3942
} // namespace Fortran::runtime::cuda
4043
#endif // FORTRAN_RUNTIME_CUDA_ALLOCATOR_H_

flang/include/flang/Runtime/allocator-registry.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ static constexpr unsigned kDefaultAllocator = 0;
1919
static constexpr unsigned kPinnedAllocatorPos = 1;
2020
static constexpr unsigned kDeviceAllocatorPos = 2;
2121
static constexpr unsigned kManagedAllocatorPos = 3;
22+
static constexpr unsigned kUnifiedAllocatorPos = 4;
2223

23-
#define MAX_ALLOCATOR 5
24+
#define MAX_ALLOCATOR 7 // 3 bits are reserved in the descriptor.
2425

2526
namespace Fortran::runtime {
2627

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,9 +1860,10 @@ static unsigned getAllocatorIdx(const Fortran::semantics::Symbol &sym) {
18601860
return kPinnedAllocatorPos;
18611861
if (*cudaAttr == Fortran::common::CUDADataAttr::Device)
18621862
return kDeviceAllocatorPos;
1863-
if (*cudaAttr == Fortran::common::CUDADataAttr::Managed ||
1864-
*cudaAttr == Fortran::common::CUDADataAttr::Unified)
1863+
if (*cudaAttr == Fortran::common::CUDADataAttr::Managed)
18651864
return kManagedAllocatorPos;
1865+
if (*cudaAttr == Fortran::common::CUDADataAttr::Unified)
1866+
return kUnifiedAllocatorPos;
18661867
}
18671868
return kDefaultAllocator;
18681869
}

flang/runtime/CUDA/allocator.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void CUFRegisterAllocator() {
2626
kDeviceAllocatorPos, {&CUFAllocDevice, CUFFreeDevice});
2727
allocatorRegistry.Register(
2828
kManagedAllocatorPos, {&CUFAllocManaged, CUFFreeManaged});
29+
allocatorRegistry.Register(
30+
kUnifiedAllocatorPos, {&CUFAllocUnified, CUFFreeUnified});
2931
}
3032

3133
void *CUFAllocPinned(std::size_t sizeInBytes) {
@@ -57,4 +59,14 @@ void CUFFreeManaged(void *p) {
5759
CUDA_REPORT_IF_ERROR(cuMemFree(reinterpret_cast<CUdeviceptr>(p)));
5860
}
5961

62+
void *CUFAllocUnified(std::size_t sizeInBytes) {
63+
// Call alloc managed for the time being.
64+
return CUFAllocManaged(sizeInBytes);
65+
}
66+
67+
void CUFFreeUnified(void *p) {
68+
// Call free managed for the time being.
69+
CUFFreeManaged(p);
70+
}
71+
6072
} // namespace Fortran::runtime::cuda

0 commit comments

Comments
 (0)