1 #ifndef HeterogeneousCore_AlpakaInterface_interface_CachingAllocator_h 2 #define HeterogeneousCore_AlpakaInterface_interface_CachingAllocator_h 14 #include <type_traits> 16 #include <alpaka/alpaka.hpp> 29 unsigned int power = 1;
44 }
else if (
value >= (1ul << 40) and
value % (1ul << 40) == 0) {
46 }
else if (
value >= (1ul << 30) and
value % (1ul << 30) == 0) {
48 }
else if (
value >= (1ul << 20) and
value % (1ul << 20) == 0) {
50 }
else if (
value >= (1ul << 10) and
value % (1ul << 10) == 0) {
88 template <
typename TDev,
typename TQueue>
91 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED 92 friend class alpaka_cuda_async::AlpakaService;
94 #ifdef ALPAKA_ACC_GPU_HIP_ENABLED 95 friend class alpaka_rocm_async::AlpakaService;
97 #ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED 98 friend class alpaka_serial_sync::AlpakaService;
100 #ifdef ALPAKA_ACC_CPU_B_TBB_T_SEQ_ENABLED 101 friend class alpaka_tbb_async::AlpakaService;
107 using Buffer = alpaka::Buf<Device, std::byte, alpaka::DimInt<1u>,
size_t>;
110 static_assert(alpaka::isDevice<Device>,
"TDev should be an alpaka Device type.");
111 static_assert(alpaka::isQueue<Queue>,
"TQueue should be an alpaka Queue type.");
112 static_assert(std::is_same_v<
Device, alpaka::Dev<Queue>>
or std::is_same_v<Device, alpaka::DevCpu>,
113 "The \"memory device\" type can either be the same as the \"synchronisation device\" type, or be the " 125 bool reuseSameQueueAllocations,
146 std::ostringstream
out;
147 out <<
"CachingAllocator settings\n" 149 <<
" min bin " <<
minBin_ <<
"\n" 150 <<
" max bin " <<
maxBin_ <<
"\n" 151 <<
" resulting bins:\n";
183 block.requested = bytes;
202 return block.buffer->data();
211 std::stringstream
ss;
212 ss <<
"Trying to free a non-live block at " << ptr;
213 throw std::runtime_error(
ss.str());
238 alpaka::enqueue(*(
block.queue), *(
block.event));
241 std::ostringstream
out;
242 out <<
"CachingAllocator::free() caught an alpaka error: " <<
e.what() <<
"\n";
244 << ptr <<
" from associated queue " <<
block.queue->m_spQueueImpl.get() <<
", event " 247 <<
" live blocks (" <<
cachedBytes_.
live <<
" bytes) outstanding." << std::endl;
258 std::ostringstream
out;
260 << ptr <<
" from associated queue " <<
block.queue->m_spQueueImpl.get() <<
" , event " 261 <<
block.event->m_spEventImpl.get() <<
" .\n\t\t " <<
cachedBlocks_.size() <<
" available blocks cached (" 263 <<
" bytes) outstanding." << std::endl;
280 std::ostringstream
out;
281 out <<
"CachingAllocator::free() caught an alpaka error: " <<
e.what() <<
"\n";
283 << ptr <<
" from associated queue " <<
block.queue->m_spQueueImpl.get() <<
", event " 286 <<
" live blocks (" <<
cachedBytes_.
live <<
" bytes) outstanding." << std::endl;
293 std::ostringstream
out;
295 << ptr <<
" from associated queue " <<
block.queue->m_spQueueImpl.get() <<
", event " 296 <<
block.event->m_spEventImpl.get() <<
" .\n\t\t " <<
cachedBlocks_.size() <<
" available blocks cached (" 298 <<
" bytes) outstanding." << std::endl;
321 size_t totalMemory = alpaka::getMemBytes(
device_);
327 if (memoryFraction > 0 and memoryFraction <
size) {
328 size = memoryFraction;
334 std::tuple<unsigned int, size_t>
findBin(
size_t bytes)
const {
339 throw std::runtime_error(
"Requested allocation size " +
std::to_string(bytes) +
340 " bytes is too large for the caching detail with maximum bin " +
342 " bytes. You might want to increase the maximum bin size");
346 while (binBytes < bytes) {
350 return std::make_tuple(
bin, binBytes);
358 for (
auto iBlock = begin; iBlock != end; ++iBlock) {
360 alpaka::isComplete(*(iBlock->second.event))) {
364 block = iBlock->second;
368 if (
block.device() != alpaka::getDev(*(
block.event))) {
382 std::ostringstream
out;
384 <<
block.buffer->data() <<
" (" <<
block.bytes <<
" bytes) for queue " 385 <<
block.queue->m_spQueueImpl.get() <<
", event " <<
block.event->m_spEventImpl.get()
386 <<
" (previously associated with queue " << iBlock->second.queue->m_spQueueImpl.get() <<
" , event " 387 << iBlock->second.event->m_spEventImpl.get() <<
")." << std::endl;
403 return alpaka::allocBuf<std::byte, size_t>(
device_, bytes);
404 }
else if constexpr (std::is_same_v<Device, alpaka::DevCpu>) {
406 using Platform = alpaka::Platform<alpaka::Dev<Queue>>;
407 return alpaka::allocMappedBuf<Platform, std::byte, size_t>(
device_, platform<Platform>(), bytes);
410 static_assert(std::is_same_v<
Device, alpaka::Dev<Queue>>
or std::is_same_v<Device, alpaka::DevCpu>,
411 "The \"memory device\" type can either be the same as the \"synchronisation device\" type, or be " 419 }
catch (std::runtime_error
const&
e) {
422 std::ostringstream
out;
424 <<
" bytes for queue " <<
block.queue->m_spQueueImpl.get()
425 <<
", retrying after freeing cached allocations" << std::endl;
447 std::ostringstream
out;
449 <<
block.buffer->data() <<
" (" <<
block.bytes <<
" bytes associated with queue " 450 <<
block.queue->m_spQueueImpl.get() <<
", event " <<
block.event->m_spEventImpl.get() <<
"." << std::endl;
463 std::ostringstream
out;
512 #endif // HeterogeneousCore_AlpakaInterface_interface_CachingAllocator_h
constexpr unsigned int maxBin
constexpr size_t maxCachedBytes
static std::string to_string(const XMLCh *ch)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
std::string getName(const G4String &)
constexpr unsigned int binGrowth
constexpr unsigned int minBin
constexpr double maxCachedFraction