Skip to content

[flang][cuda] Use async id for device stream allocation #118733

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 2 commits into from
Dec 5, 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: 4 additions & 3 deletions flang/include/flang/Runtime/CUDA/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef FORTRAN_RUNTIME_CUDA_ALLOCATOR_H_
#define FORTRAN_RUNTIME_CUDA_ALLOCATOR_H_

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

Expand All @@ -19,16 +20,16 @@ extern "C" {
void RTDECL(CUFRegisterAllocator)();
}

void *CUFAllocPinned(std::size_t, std::int64_t);
void *CUFAllocPinned(std::size_t, std::int64_t = kCudaNoStream);
void CUFFreePinned(void *);

void *CUFAllocDevice(std::size_t, std::int64_t);
void CUFFreeDevice(void *);

void *CUFAllocManaged(std::size_t, std::int64_t);
void *CUFAllocManaged(std::size_t, std::int64_t = kCudaNoStream);
void CUFFreeManaged(void *);

void *CUFAllocUnified(std::size_t, std::int64_t);
void *CUFAllocUnified(std::size_t, std::int64_t = kCudaNoStream);
void CUFFreeUnified(void *);

} // namespace Fortran::runtime::cuda
Expand Down
21 changes: 11 additions & 10 deletions flang/runtime/CUDA/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,28 @@ void RTDEF(CUFRegisterAllocator)() {
}
}

void *CUFAllocPinned(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
void *CUFAllocPinned(std::size_t sizeInBytes, std::int64_t) {
void *p;
CUDA_REPORT_IF_ERROR(cudaMallocHost((void **)&p, sizeInBytes));
return p;
}

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

void *CUFAllocDevice(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
void *CUFAllocDevice(std::size_t sizeInBytes, std::int64_t stream) {
void *p;
CUDA_REPORT_IF_ERROR(cudaMalloc(&p, sizeInBytes));
if (stream >= 0) {
CUDA_REPORT_IF_ERROR(
cudaMallocAsync(&p, sizeInBytes, (cudaStream_t)stream));
} else {
CUDA_REPORT_IF_ERROR(cudaMalloc(&p, sizeInBytes));
}
return p;
}

void CUFFreeDevice(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); }

void *CUFAllocManaged(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
void *CUFAllocManaged(std::size_t sizeInBytes, std::int64_t) {
void *p;
CUDA_REPORT_IF_ERROR(
cudaMallocManaged((void **)&p, sizeInBytes, cudaMemAttachGlobal));
Expand All @@ -61,10 +63,9 @@ void *CUFAllocManaged(

void CUFFreeManaged(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); }

void *CUFAllocUnified(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
void *CUFAllocUnified(std::size_t sizeInBytes, std::int64_t) {
// Call alloc managed for the time being.
return CUFAllocManaged(sizeInBytes, asyncId);
return CUFAllocManaged(sizeInBytes);
}

void CUFFreeUnified(void *p) {
Expand Down
17 changes: 17 additions & 0 deletions flang/unittests/Runtime/CUDA/AllocatorCUF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ TEST(AllocatableCUFTest, SimpleDeviceAllocate) {
EXPECT_FALSE(a->IsAllocated());
}

TEST(AllocatableCUFTest, SimpleStreamDeviceAllocate) {
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);
RTNAME(AllocatableAllocate)
(*a, 1, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
EXPECT_TRUE(a->IsAllocated());
RTNAME(AllocatableDeallocate)
(*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
EXPECT_FALSE(a->IsAllocated());
}

TEST(AllocatableCUFTest, SimplePinnedAllocate) {
using Fortran::common::TypeCategory;
RTNAME(CUFRegisterAllocator)();
Expand Down
Loading