Skip to content

[flang][cuda] Add async id to allocators #134724

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
Apr 8, 2025
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
10 changes: 6 additions & 4 deletions flang-rt/include/flang-rt/runtime/allocator-registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@

#include "flang/Common/api-attrs.h"
#include "flang/Runtime/allocator-registry-consts.h"
#include <cstdint>
#include <cstdlib>
#include <vector>

#define MAX_ALLOCATOR 7 // 3 bits are reserved in the descriptor.

namespace Fortran::runtime {

using AllocFct = void *(*)(std::size_t);
using AllocFct = void *(*)(std::size_t, std::int64_t);
using FreeFct = void (*)(void *);

typedef struct Allocator_t {
AllocFct alloc{nullptr};
FreeFct free{nullptr};
} Allocator_t;

#ifdef RT_DEVICE_COMPILATION
static RT_API_ATTRS void *MallocWrapper(std::size_t size) {
static RT_API_ATTRS void *MallocWrapper(
std::size_t size, [[maybe_unused]] std::int64_t) {
return std::malloc(size);
}
#ifdef RT_DEVICE_COMPILATION
static RT_API_ATTRS void FreeWrapper(void *p) { return std::free(p); }
#endif

Expand All @@ -39,7 +41,7 @@ struct AllocatorRegistry {
: allocators{{&MallocWrapper, &FreeWrapper}} {}
#else
constexpr AllocatorRegistry() {
allocators[kDefaultAllocator] = {&std::malloc, &std::free};
allocators[kDefaultAllocator] = {&MallocWrapper, &std::free};
};
#endif
RT_API_ATTRS void Register(int, Allocator_t);
Expand Down
14 changes: 9 additions & 5 deletions flang-rt/lib/cuda/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ void RTDEF(CUFRegisterAllocator)() {
}
}

void *CUFAllocPinned(std::size_t sizeInBytes) {
void *CUFAllocPinned(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
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) {
void *CUFAllocDevice(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
void *p;
if (Fortran::runtime::executionEnvironment.cudaDeviceIsManaged) {
CUDA_REPORT_IF_ERROR(
Expand All @@ -55,7 +57,8 @@ void *CUFAllocDevice(std::size_t sizeInBytes) {

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

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

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

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

void CUFFreeUnified(void *p) {
Expand Down
3 changes: 2 additions & 1 deletion flang-rt/lib/cuda/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ RT_EXT_API_GROUP_BEGIN

Descriptor *RTDEF(CUFAllocDescriptor)(
std::size_t sizeInBytes, const char *sourceFile, int sourceLine) {
return reinterpret_cast<Descriptor *>(CUFAllocManaged(sizeInBytes));
return reinterpret_cast<Descriptor *>(
CUFAllocManaged(sizeInBytes, /*asyncId*/ -1));
}

void RTDEF(CUFFreeDescriptor)(
Expand Down
2 changes: 1 addition & 1 deletion flang-rt/lib/runtime/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ RT_API_ATTRS int Descriptor::Allocate() {
// Zero size allocation is possible in Fortran and the resulting
// descriptor must be allocated/associated. Since std::malloc(0)
// result is implementation defined, always allocate at least one byte.
void *p{alloc(byteSize ? byteSize : 1)};
void *p{alloc(byteSize ? byteSize : 1, /*asyncId=*/-1)};
if (!p) {
return CFI_ERROR_MEM_ALLOCATION;
}
Expand Down
2 changes: 1 addition & 1 deletion flang-rt/lib/runtime/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ RT_API_ATTRS void *AllocateValidatedPointerPayload(
byteSize = ((byteSize + align - 1) / align) * align;
std::size_t total{byteSize + sizeof(std::uintptr_t)};
AllocFct alloc{allocatorRegistry.GetAllocator(allocatorIdx)};
void *p{alloc(total)};
void *p{alloc(total, /*asyncId=*/-1)};
if (p && allocatorIdx == 0) {
// Fill the footer word with the XOR of the ones' complement of
// the base address, which is a value that would be highly unlikely
Expand Down
8 changes: 4 additions & 4 deletions flang/include/flang/Runtime/CUDA/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ extern "C" {
void RTDECL(CUFRegisterAllocator)();
}

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

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

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

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

} // namespace Fortran::runtime::cuda
Expand Down