From be928becab92cae58e4fc4305b2df902c97b0169 Mon Sep 17 00:00:00 2001 From: Julian Lenz Date: Tue, 24 Nov 2026 13:17:22 +0100 Subject: [PATCH 1/4] fix XMallocSIMD popcount arity alpaka::popcount takes (intrinsic, value) and only overloads on std::uint32_t/std::uint64_t; ballot() already returns the accelerator's mask_type, so pass the accelerator as the intrinsic. Co-authored-by: chillenzer-agents <326908604+chillenzer-agents@users.noreply.github.com> --- include/mallocMC/distributionPolicies/XMallocSIMD.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mallocMC/distributionPolicies/XMallocSIMD.hpp b/include/mallocMC/distributionPolicies/XMallocSIMD.hpp index b25cc3d2..7b6f1705 100644 --- a/include/mallocMC/distributionPolicies/XMallocSIMD.hpp +++ b/include/mallocMC/distributionPolicies/XMallocSIMD.hpp @@ -135,7 +135,7 @@ namespace mallocMC bool const coalescible = bytes > 0 && bytes < (pagesize / 32); #if (MALLOCMC_DEVICE_COMPILE) - threadcount = alpaka::popcount(alpaka::warp::ballot(acc, coalescible)); + threadcount = alpaka::popcount(acc, alpaka::warp::ballot(acc, coalescible)); #else threadcount = 1; // TODO #endif From ae60748ca0b3013b98ba2e390634a7448e6af403 Mon Sep 17 00:00:00 2001 From: Julian Lenz Date: Tue, 24 Nov 2026 13:17:30 +0100 Subject: [PATCH 2/4] replace undefined threadfenceBlock call in XMallocSIMD The helper was dropped from mallocMC_utils.hpp, leaving the call dangling. It implemented __threadfence_block(), so use the block-scope fence. Co-authored-by: chillenzer-agents <326908604+chillenzer-agents@users.noreply.github.com> --- include/mallocMC/distributionPolicies/XMallocSIMD.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mallocMC/distributionPolicies/XMallocSIMD.hpp b/include/mallocMC/distributionPolicies/XMallocSIMD.hpp index 7b6f1705..7be6e6d1 100644 --- a/include/mallocMC/distributionPolicies/XMallocSIMD.hpp +++ b/include/mallocMC/distributionPolicies/XMallocSIMD.hpp @@ -167,7 +167,7 @@ namespace mallocMC *(uint32*) myalloc = threadcount; } - threadfenceBlock(acc); + alpaka::mem_fence(acc, alpaka::memory_scope::Block{}); void* myres = myalloc; if(can_use_coalescing) From a268cbdeb4952dd30d1beca2090759c5422cbec5 Mon Sep 17 00:00:00 2001 From: Julian Lenz Date: Tue, 24 Nov 2026 13:28:19 +0100 Subject: [PATCH 3/4] cover the XMallocSIMD distribution policy in the allocator unit test The allocator-level test only instantiated the Noop distribution policy. Add a parameterised test that runs the same allocation through Noop and XMallocSIMD so that the latter is compiled and executed by default. XMallocSIMD is combined with the Scatter creation policy: FlatterScatter cannot free the coalesced sub-chunks that XMallocSIMD hands out and does not treat zero-byte requests as a no-op. The test uses the CPU serial accelerator because alpaka reports a warp size of zero for GPU accelerators during host compilation, which makes XMallocSIMD's shared-memory arrays ill-formed. Co-authored-by: chillenzer-agents <326908604+chillenzer-agents@users.noreply.github.com> --- test/unit/source/Allocator.cpp | 116 +++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/test/unit/source/Allocator.cpp b/test/unit/source/Allocator.cpp index 59aba169..89b331a9 100644 --- a/test/unit/source/Allocator.cpp +++ b/test/unit/source/Allocator.cpp @@ -28,14 +28,21 @@ #include "mallocMC/alignmentPolicies/Shrink.hpp" #include "mallocMC/creationPolicies/FlatterScatter.hpp" +#include "mallocMC/creationPolicies/Scatter.hpp" #include "mallocMC/distributionPolicies/Noop.hpp" +#include "mallocMC/distributionPolicies/XMallocSIMD.hpp" #include "mallocMC/oOMPolicies/ReturnNull.hpp" #include "mallocMC/reservePoolPolicies/AlpakaBuf.hpp" +#include #include +#include #include #include + +#include +#include using Dim = alpaka::DimInt<1>; using Idx = std::size_t; @@ -60,3 +67,112 @@ TEST_CASE("Allocator") allocator{dev, queue, 0}; } } + +// XMallocSIMD sizes its shared-memory arrays with the warp size. For GPU accelerators alpaka reports a warp size of +// zero during host compilation, which makes these arrays ill-formed. CPU accelerators report one, so this test uses +// the CPU serial accelerator even if a GPU backend is enabled. XMallocSIMD only coalesces in device code, so on the +// host this exercises the policy's instantiation and its pass-through behaviour. +using XMallocAcc = alpaka::AccCpuSerial; + +// XMallocSIMD is only compatible with CreationPolicies::Scatter: Scatter treats a zero-byte request as no allocation +// and implements the deallocation of coalesced sub-chunks, both of which XMallocSIMD relies on. FlatterScatter does +// neither. Both policies have to agree on the page size (see mallocMC_constraints.hpp). +struct XMallocScatterConfig +{ + static constexpr std::uint32_t pagesize = 4096U; + static constexpr std::uint32_t accessblocksize = 1024U * 1024U; + static constexpr std::uint32_t regionsize = 16U; + static constexpr std::uint32_t wastefactor = 2U; + static constexpr bool resetfreedpages = false; +}; + +using XMallocScatter = mallocMC::CreationPolicies::Scatter; + +// The distinct names are what makes the two runs distinguishable in Catch2's output. +struct ScatterNoop +{ + using DistributionPolicy = mallocMC::DistributionPolicies::Noop; +}; + +struct ScatterXMallocSIMD +{ + using DistributionPolicy = mallocMC::DistributionPolicies::XMallocSIMD<>; +}; + +struct AllocateWriteCheckAndFree +{ + template + ALPAKA_FN_ACC auto operator()(TAcc const& acc, TAllocatorHandle allocHandle, std::uint32_t* results) const -> void + { + constexpr std::uint32_t numBytes = 32U; + // XMallocSIMD only coalesces requests smaller than pagesize / 32. + static_assert(numBytes < mallocMC::DistributionPolicies::XMallocSIMD<>::Properties::pagesize / 32U); + + auto const idx = alpaka::getIdx(acc)[0]; + void* const pointer = allocHandle.malloc(acc, numBytes); + results[idx] = pointer != nullptr ? 1U : 0U; + if(pointer != nullptr) + { + auto* const data = static_cast(pointer); + std::fill(data, data + numBytes / sizeof(std::uint32_t), static_cast(idx + 1U)); + } + + // The barrier makes writes to overlapping sub-chunks (i.e. broken coalescing) visible before any thread reads + // back its own data. + alpaka::syncBlockThreads(acc); + if(pointer != nullptr) + { + auto const* const data = static_cast(pointer); + bool const intact = std::all_of( + data, + data + numBytes / sizeof(std::uint32_t), + [idx](std::uint32_t const value) { return value == idx + 1U; }); + results[idx] = intact ? results[idx] : 0U; + } + + allocHandle.free(acc, pointer); + } +}; + +TEMPLATE_TEST_CASE("Allocator allocates and frees", "", ScatterNoop, ScatterXMallocSIMD) +{ + using DistributionPolicy = typename TestType::DistributionPolicy; + using Allocator = mallocMC::Allocator< + alpaka::AccToTag, + XMallocScatter, + DistributionPolicy, + mallocMC::OOMPolicies::ReturnNull, + mallocMC::ReservePoolPolicies::AlpakaBuf, + mallocMC::AlignmentPolicies::Shrink<>>; + + auto const platform = alpaka::Platform{}; + auto const dev = alpaka::getDevByIdx(platform, 0); + auto queue = alpaka::Queue{dev}; + + // Scatter needs a heap that holds at least one full access block to initialise. + constexpr std::size_t heapSize = 4U * 1024U * 1024U; + Allocator allocator{dev, queue, heapSize}; + + // Use up to a warp so XMallocSIMD has something to coalesce. CPU accelerators may only allow a single thread. + auto const devProps = alpaka::getAccDevProps(dev); + auto const numThreads = std::min(32U, static_cast(devProps.m_blockThreadCountMax)); + + auto results = alpaka::allocBuf(dev, Idx{numThreads}); + auto const workDiv = alpaka::WorkDivMembers{Idx{1}, Idx{numThreads}, Idx{1}}; + alpaka::exec( + queue, + workDiv, + AllocateWriteCheckAndFree{}, + allocator.getAllocatorHandle(), + alpaka::getPtrNative(results)); + alpaka::wait(queue); + + auto const platformHost = alpaka::Platform{}; + auto const devHost = alpaka::getDevByIdx(platformHost, 0); + auto resultsHost = alpaka::allocBuf(devHost, Idx{numThreads}); + alpaka::memcpy(queue, resultsHost, results); + alpaka::wait(queue); + + auto const* const resultsPtr = alpaka::getPtrNative(resultsHost); + CHECK(std::all_of(resultsPtr, resultsPtr + numThreads, [](std::uint32_t const result) { return result == 1U; })); +} From 0eb96ff2e30a56c8c0f9ba217572051ec644ae0b Mon Sep 17 00:00:00 2001 From: Julian Lenz Date: Thu, 26 Nov 2026 16:29:35 +0100 Subject: [PATCH 4/4] fix XMallocSIMD compilation without a compile-time warp size alpaka reports a warp size of zero during the host pass of a GPU compilation and for SYCL. XMallocSIMD used that value as a shared-array extent and as a divisor, so those configurations were ill-formed. Fall back to a single shared slot and warp id zero there. Coalescing is already compiled out when MALLOCMC_DEVICE_COMPILE is undefined, so the policy remains a pass-through. Skip the warp-0 shared-counter initialisation to avoid a same-slot write race on SYCL. For accelerators with a known compile-time warp size the generated code is unchanged. Co-authored-by: chillenzer-agents <326908604+chillenzer-agents@users.noreply.github.com> --- .../distributionPolicies/XMallocSIMD.hpp | 30 +++++++++++++++---- include/mallocMC/mallocMC_utils.hpp | 20 +++++++++---- test/unit/source/Allocator.cpp | 7 ++--- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/include/mallocMC/distributionPolicies/XMallocSIMD.hpp b/include/mallocMC/distributionPolicies/XMallocSIMD.hpp index 7be6e6d1..3fb0cd54 100644 --- a/include/mallocMC/distributionPolicies/XMallocSIMD.hpp +++ b/include/mallocMC/distributionPolicies/XMallocSIMD.hpp @@ -81,6 +81,22 @@ namespace mallocMC { private: using uint32 = std::uint32_t; + + /** Number of warp slots in the per-block shared arrays. + * + * Falls back to a single slot if alpaka does not know the warp size at compile time, which is the case + * during the host compilation of CUDA/HIP code and for SYCL. Coalescing is disabled in those cases, so the + * arrays are never accessed beyond that slot. + */ + template + static consteval uint32 warpSlots() + { + if constexpr(getWarpSize() == 0) + return 1U; + else + return maxThreadsPerBlock / getWarpSize(); + } + bool can_use_coalescing; uint32 warpid; uint32 myoffset; @@ -126,9 +142,13 @@ namespace mallocMC threadcount = 0; // init with initial counter - auto& warp_sizecounter = alpaka:: - declareSharedVar()], __COUNTER__>(acc); - warp_sizecounter[warpid] = 16; + auto& warp_sizecounter + = alpaka::declareSharedVar()], __COUNTER__>(acc); + // Without a compile-time warp size every thread maps to slot zero, so all work-items would store to + // the same slot without a barrier. Coalescing is compiled out in that case and the slot is never read, + // hence the store is skipped. + if constexpr(getWarpSize() != 0) + warp_sizecounter[warpid] = 16; // second half: make sure that all coalesced allocations can fit // within one page necessary for offset calculation @@ -155,9 +175,7 @@ namespace mallocMC template ALPAKA_FN_ACC auto distribute(AlpakaAcc const& acc, void* allocatedMem) -> void* { - auto& warp_res - = alpaka::declareSharedVar()], __COUNTER__>( - acc); + auto& warp_res = alpaka::declareSharedVar()], __COUNTER__>(acc); char* myalloc = (char*) allocatedMem; if(req_size && can_use_coalescing) diff --git a/include/mallocMC/mallocMC_utils.hpp b/include/mallocMC/mallocMC_utils.hpp index e2e6e53b..e9a85164 100644 --- a/include/mallocMC/mallocMC_utils.hpp +++ b/include/mallocMC/mallocMC_utils.hpp @@ -177,15 +177,25 @@ namespace mallocMC * The id is constant over the lifetime of the thread. * The id is not equal to warpid(). * - * @return warp id within the block + * @return warp id within the block, or 0 if the accelerator has no compile-time warp size, i.e. during the host + * compilation of CUDA/HIP code and for SYCL. */ template ALPAKA_FN_ACC inline auto warpid_withinblock(AlpakaAcc const& acc) -> std::uint32_t { - auto const localId = alpaka::mapIdx<1>( - alpaka::getIdx(acc), - alpaka::getWorkDiv(acc))[0]; - return localId / getWarpSize(); + if constexpr(getWarpSize() == 0) + { + // Alpaka can provide the warp size at runtime, so it is not a compile-time constant here. XMallocSIMD + // does not coalesce in these configurations, so zero is a safe placeholder rather than a meaningful id. + return 0U; + } + else + { + auto const localId = alpaka::mapIdx<1>( + alpaka::getIdx(acc), + alpaka::getWorkDiv(acc))[0]; + return localId / getWarpSize(); + } } template && std::is_integral_v>> diff --git a/test/unit/source/Allocator.cpp b/test/unit/source/Allocator.cpp index 89b331a9..c40628c5 100644 --- a/test/unit/source/Allocator.cpp +++ b/test/unit/source/Allocator.cpp @@ -68,10 +68,9 @@ TEST_CASE("Allocator") } } -// XMallocSIMD sizes its shared-memory arrays with the warp size. For GPU accelerators alpaka reports a warp size of -// zero during host compilation, which makes these arrays ill-formed. CPU accelerators report one, so this test uses -// the CPU serial accelerator even if a GPU backend is enabled. XMallocSIMD only coalesces in device code, so on the -// host this exercises the policy's instantiation and its pass-through behaviour. +// XMallocSIMD sizes its shared-memory arrays with the warp size and falls back to a single slot when alpaka has no +// compile-time warp size (GPU host compilation and SYCL); CPU accelerators report one. The test still uses +// AccCpuSerial because coalescing is device-only and this suite is CPU-only. using XMallocAcc = alpaka::AccCpuSerial; // XMallocSIMD is only compatible with CreationPolicies::Scatter: Scatter treats a zero-byte request as no allocation