Skip to content
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
34 changes: 26 additions & 8 deletions include/mallocMC/distributionPolicies/XMallocSIMD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename AlpakaAcc>
static consteval uint32 warpSlots()
{
if constexpr(getWarpSize<AlpakaAcc>() == 0)
return 1U;
else
return maxThreadsPerBlock / getWarpSize<AlpakaAcc>();
}

bool can_use_coalescing;
uint32 warpid;
uint32 myoffset;
Expand Down Expand Up @@ -126,16 +142,20 @@ namespace mallocMC
threadcount = 0;

// init with initial counter
auto& warp_sizecounter = alpaka::
declareSharedVar<std::uint32_t[maxThreadsPerBlock / getWarpSize<AlpakaAcc>()], __COUNTER__>(acc);
warp_sizecounter[warpid] = 16;
auto& warp_sizecounter
= alpaka::declareSharedVar<std::uint32_t[warpSlots<AlpakaAcc>()], __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<AlpakaAcc>() != 0)
warp_sizecounter[warpid] = 16;

// second half: make sure that all coalesced allocations can fit
// within one page necessary for offset calculation
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
Expand All @@ -155,9 +175,7 @@ namespace mallocMC
template<typename AlpakaAcc>
ALPAKA_FN_ACC auto distribute(AlpakaAcc const& acc, void* allocatedMem) -> void*
{
auto& warp_res
= alpaka::declareSharedVar<char * [maxThreadsPerBlock / getWarpSize<AlpakaAcc>()], __COUNTER__>(
acc);
auto& warp_res = alpaka::declareSharedVar<char * [warpSlots<AlpakaAcc>()], __COUNTER__>(acc);

char* myalloc = (char*) allocatedMem;
if(req_size && can_use_coalescing)
Expand All @@ -167,7 +185,7 @@ namespace mallocMC
*(uint32*) myalloc = threadcount;
}

threadfenceBlock(acc);
alpaka::mem_fence(acc, alpaka::memory_scope::Block{});

void* myres = myalloc;
if(can_use_coalescing)
Expand Down
20 changes: 15 additions & 5 deletions include/mallocMC/mallocMC_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename AlpakaAcc>
ALPAKA_FN_ACC inline auto warpid_withinblock(AlpakaAcc const& acc) -> std::uint32_t
{
auto const localId = alpaka::mapIdx<1>(
alpaka::getIdx<alpaka::Block, alpaka::Threads>(acc),
alpaka::getWorkDiv<alpaka::Block, alpaka::Threads>(acc))[0];
return localId / getWarpSize<AlpakaAcc>();
if constexpr(getWarpSize<AlpakaAcc>() == 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<alpaka::Block, alpaka::Threads>(acc),
alpaka::getWorkDiv<alpaka::Block, alpaka::Threads>(acc))[0];
return localId / getWarpSize<AlpakaAcc>();
}
}

template<typename T, typename U, typename = std::enable_if_t<std::is_integral_v<T> && std::is_integral_v<U>>>
Expand Down
115 changes: 115 additions & 0 deletions test/unit/source/Allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <alpaka/acc/AccCpuSerial.hpp>
#include <alpaka/example/ExampleDefaultAcc.hpp>

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <mallocMC/mallocMC.hpp>

#include <algorithm>
#include <cstdint>
using Dim = alpaka::DimInt<1>;
using Idx = std::size_t;

Expand All @@ -60,3 +67,111 @@ TEST_CASE("Allocator")
allocator{dev, queue, 0};
}
}

// 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<Dim, Idx>;

// 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<XMallocScatterConfig>;

// 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<typename TAcc, typename TAllocatorHandle>
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<alpaka::Grid, alpaka::Threads>(acc)[0];
void* const pointer = allocHandle.malloc(acc, numBytes);
results[idx] = pointer != nullptr ? 1U : 0U;
if(pointer != nullptr)
{
auto* const data = static_cast<std::uint32_t*>(pointer);
std::fill(data, data + numBytes / sizeof(std::uint32_t), static_cast<std::uint32_t>(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<std::uint32_t const*>(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<XMallocAcc>,
XMallocScatter,
DistributionPolicy,
mallocMC::OOMPolicies::ReturnNull,
mallocMC::ReservePoolPolicies::AlpakaBuf<XMallocAcc>,
mallocMC::AlignmentPolicies::Shrink<>>;

auto const platform = alpaka::Platform<XMallocAcc>{};
auto const dev = alpaka::getDevByIdx(platform, 0);
auto queue = alpaka::Queue<XMallocAcc, alpaka::Blocking>{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<XMallocAcc>(dev);
auto const numThreads = std::min<std::size_t>(32U, static_cast<std::size_t>(devProps.m_blockThreadCountMax));

auto results = alpaka::allocBuf<std::uint32_t, Idx>(dev, Idx{numThreads});
auto const workDiv = alpaka::WorkDivMembers<Dim, Idx>{Idx{1}, Idx{numThreads}, Idx{1}};
alpaka::exec<XMallocAcc>(
queue,
workDiv,
AllocateWriteCheckAndFree{},
allocator.getAllocatorHandle(),
alpaka::getPtrNative(results));
alpaka::wait(queue);

auto const platformHost = alpaka::Platform<alpaka::DevCpu>{};
auto const devHost = alpaka::getDevByIdx(platformHost, 0);
auto resultsHost = alpaka::allocBuf<std::uint32_t, Idx>(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; }));
}