Skip to content
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

Make the caching memory allocator lock-free #46658

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions HeterogeneousCore/AlpakaInterface/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<use name="alpaka"/>
<use name="boost_header"/>
<use name="fmt"/>
<use name="tbb"/>
<use name="FWCore/Utilities"/>
<flags ALPAKA_BACKENDS="1"/>
<export>
<lib name="1"/>
</export>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace cms::alpakatools {

// Which memory allocator to use
// - Synchronous: (device and host) cudaMalloc/hipMalloc and cudaMallocHost/hipMallocHost
// - Asynchronous: (device only) cudaMallocAsync (requires CUDA >= 11.2)
// - Asynchronous: (device only) cudaMallocAsync/hipMallocAsync (requires CUDA >= 11.2 or ROCm >= 5.6)
// - Caching: (device and host) caching allocator
enum class AllocatorPolicy { Synchronous = 0, Asynchronous = 1, Caching = 2 };

Expand Down
49 changes: 37 additions & 12 deletions HeterogeneousCore/AlpakaInterface/interface/CachedBufAlloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <alpaka/alpaka.hpp>

#include "HeterogeneousCore/AlpakaInterface/interface/CachingAllocator.h"
#include "HeterogeneousCore/AlpakaInterface/interface/getDeviceCachingAllocator.h"
#include "HeterogeneousCore/AlpakaInterface/interface/getHostCachingAllocator.h"

Expand Down Expand Up @@ -50,10 +51,14 @@ namespace cms::alpakatools {
// FIXME the BufCpu does not support a pitch ?
size_t size = alpaka::getExtentProduct(extent);
size_t sizeBytes = size * sizeof(TElem);
void* memPtr = allocator.allocate(sizeBytes, queue);
auto* block = allocator.allocate(sizeBytes, queue);
void* memPtr = block->buffer->data();

// use a custom deleter to return the buffer to the CachingAllocator
auto deleter = [alloc = &allocator](TElem* ptr) { alloc->free(ptr); };
auto deleter = [&allocator, block](TElem* ptr) mutable {
assert(ptr == reinterpret_cast<void*>(block->buffer->data()));
allocator.free(block);
};

return alpaka::BufCpu<TElem, TDim, TIdx>(dev, reinterpret_cast<TElem*>(memPtr), std::move(deleter), extent);
}
Expand All @@ -73,10 +78,14 @@ namespace cms::alpakatools {
// FIXME the BufCpu does not support a pitch ?
size_t size = alpaka::getExtentProduct(extent);
size_t sizeBytes = size * sizeof(TElem);
void* memPtr = allocator.allocate(sizeBytes, queue);
auto* block = allocator.allocate(sizeBytes, queue);
void* memPtr = block->buffer->data();

// use a custom deleter to return the buffer to the CachingAllocator
auto deleter = [alloc = &allocator](TElem* ptr) { alloc->free(ptr); };
auto deleter = [&allocator, block](TElem* ptr) mutable {
assert(ptr == reinterpret_cast<void*>(block->buffer->data()));
allocator.free(block);
};

return alpaka::BufCpu<TElem, TDim, TIdx>(dev, reinterpret_cast<TElem*>(memPtr), std::move(deleter), extent);
}
Expand All @@ -99,10 +108,14 @@ namespace cms::alpakatools {
size_t pitchBytes = widthBytes;
size_t size = alpaka::getExtentProduct(extent);
size_t sizeBytes = size * sizeof(TElem);
void* memPtr = allocator.allocate(sizeBytes, queue);
auto* block = allocator.allocate(sizeBytes, queue);
void* memPtr = block->buffer->data();

// use a custom deleter to return the buffer to the CachingAllocator
auto deleter = [alloc = &allocator](TElem* ptr) { alloc->free(ptr); };
auto deleter = [&allocator, block](TElem* ptr) mutable {
assert(ptr == reinterpret_cast<void*>(block->buffer->data()));
allocator.free(block);
};

return alpaka::BufCudaRt<TElem, TDim, TIdx>(
dev, reinterpret_cast<TElem*>(memPtr), std::move(deleter), extent, pitchBytes);
Expand All @@ -127,10 +140,14 @@ namespace cms::alpakatools {
// FIXME the BufCpu does not support a pitch ?
size_t size = alpaka::getExtentProduct(extent);
size_t sizeBytes = size * sizeof(TElem);
void* memPtr = allocator.allocate(sizeBytes, queue);
auto* block = allocator.allocate(sizeBytes, queue);
void* memPtr = block->buffer->data();

// use a custom deleter to return the buffer to the CachingAllocator
auto deleter = [alloc = &allocator](TElem* ptr) { alloc->free(ptr); };
auto deleter = [&allocator, block](TElem* ptr) mutable {
assert(ptr == reinterpret_cast<void*>(block->buffer->data()));
allocator.free(block);
};

return alpaka::BufCpu<TElem, TDim, TIdx>(dev, reinterpret_cast<TElem*>(memPtr), std::move(deleter), extent);
}
Expand All @@ -150,10 +167,14 @@ namespace cms::alpakatools {
// FIXME the BufCpu does not support a pitch ?
size_t size = alpaka::getExtentProduct(extent);
size_t sizeBytes = size * sizeof(TElem);
void* memPtr = allocator.allocate(sizeBytes, queue);
auto* block = allocator.allocate(sizeBytes, queue);
void* memPtr = block->buffer->data();

// use a custom deleter to return the buffer to the CachingAllocator
auto deleter = [alloc = &allocator](TElem* ptr) { alloc->free(ptr); };
auto deleter = [&allocator, block](TElem* ptr) mutable {
assert(ptr == reinterpret_cast<void*>(block->buffer->data()));
allocator.free(block);
};

return alpaka::BufCpu<TElem, TDim, TIdx>(dev, reinterpret_cast<TElem*>(memPtr), std::move(deleter), extent);
}
Expand All @@ -176,10 +197,14 @@ namespace cms::alpakatools {
size_t pitchBytes = widthBytes;
size_t size = alpaka::getExtentProduct(extent);
size_t sizeBytes = size * sizeof(TElem);
void* memPtr = allocator.allocate(sizeBytes, queue);
auto* block = allocator.allocate(sizeBytes, queue);
void* memPtr = block->buffer->data();

// use a custom deleter to return the buffer to the CachingAllocator
auto deleter = [alloc = &allocator](TElem* ptr) { alloc->free(ptr); };
auto deleter = [&allocator, block](TElem* ptr) mutable {
assert(ptr == reinterpret_cast<void*>(block->buffer->data()));
allocator.free(block);
};

return alpaka::BufHipRt<TElem, TDim, TIdx>(
dev, reinterpret_cast<TElem*>(memPtr), std::move(deleter), extent, pitchBytes);
Expand Down
Loading