Skip to content

Commit

Permalink
* reverted deletion of reserve pool policies
Browse files Browse the repository at this point in the history
* merged SimpleMalloc and CudaMalloc policies into AlpakaBuf policy
* since the AlpakaBuf policy is stateful now, Allocator now contains an instance of the reserve pool policy
  • Loading branch information
bernhardmgruber committed Jun 2, 2020
1 parent 739236e commit f8a9d92
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 24 deletions.
2 changes: 2 additions & 0 deletions examples/mallocMC_example01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <mallocMC/CreationPolicies.hpp>
#include <mallocMC/DistributionPolicies.hpp>
#include <mallocMC/OOMPolicies.hpp>
#include <mallocMC/ReservePoolPolicies.hpp>
#include <mallocMC/mallocMC_hostclass.hpp>
#include <numeric>

Expand Down Expand Up @@ -73,6 +74,7 @@ using ScatterAllocator = mallocMC::Allocator<
mallocMC::CreationPolicies::Scatter<ScatterHeapConfig, ScatterHashConfig>,
mallocMC::DistributionPolicies::XMallocSIMD<XMallocConfig>,
mallocMC::OOMPolicies::ReturnNull,
mallocMC::ReservePoolPolicies::AlpakaBuf<Acc>,
mallocMC::AlignmentPolicies::Shrink<ShrinkConfig>>;

ALPAKA_STATIC_ACC_MEM_GLOBAL int ** arA;
Expand Down
2 changes: 2 additions & 0 deletions examples/mallocMC_example03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <mallocMC/CreationPolicies.hpp>
#include <mallocMC/DistributionPolicies.hpp>
#include <mallocMC/OOMPolicies.hpp>
#include <mallocMC/ReservePoolPolicies.hpp>
#include <mallocMC/mallocMC_hostclass.hpp>
#include <numeric>
#include <vector>
Expand Down Expand Up @@ -70,6 +71,7 @@ using ScatterAllocator = mallocMC::Allocator<
mallocMC::CreationPolicies::Scatter<ScatterConfig, ScatterHashParams>,
mallocMC::DistributionPolicies::Noop,
mallocMC::OOMPolicies::ReturnNull,
mallocMC::ReservePoolPolicies::AlpakaBuf<Acc>,
mallocMC::AlignmentPolicies::Shrink<AlignmentConfig>>;

ALPAKA_STATIC_ACC_MEM_GLOBAL int * arA;
Expand Down
33 changes: 33 additions & 0 deletions src/include/mallocMC/ReservePoolPolicies.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
mallocMC: Memory Allocator for Many Core Architectures.
https://www.hzdr.de/crp
Copyright 2014 Institute of Radiation Physics,
Helmholtz-Zentrum Dresden - Rossendorf
Author(s): Carlchristian Eckert - c.eckert ( at ) hzdr.de
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once

#include "reservePoolPolicies/CudaSetLimits.hpp"
#include "reservePoolPolicies/CudaSetLimits_impl.hpp"
#include "reservePoolPolicies/AlpakaBuf.hpp"
23 changes: 11 additions & 12 deletions src/include/mallocMC/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,22 @@ namespace mallocMC
* @tparam T_CreationPolicy The desired type of a CreationPolicy
* @tparam T_DistributionPolicy The desired type of a DistributionPolicy
* @tparam T_OOMPolicy The desired type of a OOMPolicy
* @tparam T_ReservePoolPolicy The desired type of a ReservePoolPolicy
* @tparam T_AlignmentPolicy The desired type of a AlignmentPolicy
*/
template<
typename AlpakaAcc,
typename T_CreationPolicy,
typename T_DistributionPolicy,
typename T_OOMPolicy,
typename T_ReservePoolPolicy,
typename T_AlignmentPolicy>
class Allocator :
public PolicyConstraints<
T_CreationPolicy,
T_DistributionPolicy,
T_OOMPolicy,
T_ReservePoolPolicy,
T_AlignmentPolicy>
{
using uint32 = std::uint32_t;
Expand All @@ -123,6 +126,7 @@ namespace mallocMC
using CreationPolicy = T_CreationPolicy;
using DistributionPolicy = T_DistributionPolicy;
using OOMPolicy = T_OOMPolicy;
using ReservePoolPolicy = T_ReservePoolPolicy;
using AlignmentPolicy = T_AlignmentPolicy;
using HeapInfoVector = std::vector<HeapInfo>;
using DevAllocator = DeviceAllocator<
Expand All @@ -133,13 +137,7 @@ namespace mallocMC
using AllocatorHandle = AllocatorHandleImpl<Allocator>;

private:
using PoolBufferType = alpaka::mem::buf::Buf<
alpaka::dev::Dev<AlpakaAcc>,
unsigned char,
alpaka::dim::DimInt<1>,
size_t>;
std::unique_ptr<PoolBufferType>
poolBuffer; // FIXME(bgruber): replace by std::optional<>
ReservePoolPolicy reservePolicy;
using DevAllocatorStorageBufferType = alpaka::mem::buf::Buf<
alpaka::dev::Dev<AlpakaAcc>,
DevAllocator,
Expand All @@ -157,9 +155,7 @@ namespace mallocMC
ALPAKA_FN_HOST void
alloc(AlpakaDevice & dev, AlpakaQueue & queue, size_t size)
{
poolBuffer = std::make_unique<PoolBufferType>(
alpaka::mem::buf::alloc<unsigned char, size_t>(dev, size));
void * pool = alpaka::mem::view::getPtrNative(*poolBuffer);
void * pool = reservePolicy.setMemPool(dev, size);
std::tie(pool, size) = AlignmentPolicy::alignPool(pool, size);

devAllocatorBuffer
Expand All @@ -184,8 +180,9 @@ namespace mallocMC
ALPAKA_FN_HOST void free()
{
devAllocatorBuffer = {};
poolBuffer = {};
heapInfos = {};
reservePolicy.resetMemPool(heapInfos.p);
heapInfos.size = 0;
heapInfos.p = nullptr;
}

/* forbid to copy the allocator */
Expand Down Expand Up @@ -243,6 +240,8 @@ namespace mallocMC
<< "" << linebreak;
ss << "OOMPolicy: " << OOMPolicy::classname()
<< " " << linebreak;
ss << "ReservePoolPolicy: " << ReservePoolPolicy::classname()
<< " " << linebreak;
ss << "AlignmentPolicy: " << AlignmentPolicy::classname()
<< " " << linebreak;
return ss.str();
Expand Down
27 changes: 18 additions & 9 deletions src/include/mallocMC/creationPolicies/OldMalloc_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ namespace mallocMC
public:
static constexpr auto providesAvailableSlots = false;

ALPAKA_FN_ACC auto create(uint32 bytes) const -> void *
template<typename AlpakaAcc>
ALPAKA_FN_ACC auto create(const AlpakaAcc & acc, uint32 bytes) const
-> void *
{
return ::malloc(static_cast<size_t>(bytes));
}

template <typename AlpakaAcc>
ALPAKA_FN_ACC void destroy(const AlpakaAcc& /*acc*/, void * mem) const
template<typename AlpakaAcc>
ALPAKA_FN_ACC void
destroy(const AlpakaAcc & /*acc*/, void * mem) const
{
::free(mem);
}
Expand All @@ -59,12 +62,18 @@ namespace mallocMC
return s != 0 && (p == nullptr);
}

template<typename AlpakaQueue, typename T>
static auto
initHeap(AlpakaQueue & queue, T * dAlloc, void *, size_t) -> void *
{
return dAlloc;
}
template<
typename AlpakaAcc,
typename AlpakaDevice,
typename AlpakaQueue,
typename T_DeviceAllocator>
static void initHeap(
AlpakaDevice & dev,
AlpakaQueue & queue,
T_DeviceAllocator * heap,
void * pool,
size_t memsize)
{}

static auto classname() -> std::string
{
Expand Down
5 changes: 2 additions & 3 deletions src/include/mallocMC/creationPolicies/Scatter_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,12 +992,12 @@ namespace mallocMC
typename AlpakaDevice,
typename AlpakaQueue,
typename T_DeviceAllocator>
static auto initHeap(
static void initHeap(
AlpakaDevice & dev,
AlpakaQueue & queue,
T_DeviceAllocator * heap,
void * pool,
size_t memsize) -> void *
size_t memsize)
{
if(pool == nullptr && memsize != 0)
{
Expand Down Expand Up @@ -1027,7 +1027,6 @@ namespace mallocMC
queue,
alpaka::kernel::createTaskKernel<AlpakaAcc>(
workDiv, initKernel, heap, pool, memsize));
return heap;
}

/** counts how many elements of a size fit inside a given page
Expand Down
2 changes: 2 additions & 0 deletions src/include/mallocMC/mallocMC_constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace mallocMC
typename T_CreationPolicy,
typename T_DistributionPolicy,
typename T_OOMPolicy,
typename T_GetHeapPolicy,
typename T_AlignmentPolicy>

class PolicyConstraints :
PolicyCheck2<T_CreationPolicy, T_DistributionPolicy>
{};
Expand Down
70 changes: 70 additions & 0 deletions src/include/mallocMC/reservePoolPolicies/AlpakaBuf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
mallocMC: Memory Allocator for Many Core Architectures.
Copyright 2020 Helmholtz-Zentrum Dresden - Rossendorf,
CERN
Author(s): Bernhard Manfred Gruber
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once

#include <alpaka/alpaka.hpp>
#include <memory>
#include <string>

namespace mallocMC
{
namespace ReservePoolPolicies
{
template<typename AlpakaAcc>
struct AlpakaBuf
{
template<typename AlpakaDev>
auto setMemPool(const AlpakaDev & dev, size_t memsize) -> void *
{
poolBuffer = std::make_unique<PoolBufferType>(
alpaka::mem::buf::alloc<unsigned char, size_t>(
dev, memsize));
return alpaka::mem::view::getPtrNative(*poolBuffer);
}

void resetMemPool(void * p)
{
poolBuffer = {};
}

static auto classname() -> std::string
{
return "AlpakaBuf";
}

private:
using PoolBufferType = alpaka::mem::buf::Buf<
alpaka::dev::Dev<AlpakaAcc>,
unsigned char,
alpaka::dim::DimInt<1>,
size_t>;
std::unique_ptr<PoolBufferType>
poolBuffer; // FIXME(bgruber): replace by std::optional<>
};
} // namespace ReservePoolPolicies
} // namespace mallocMC
45 changes: 45 additions & 0 deletions src/include/mallocMC/reservePoolPolicies/CudaSetLimits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
mallocMC: Memory Allocator for Many Core Architectures.
Copyright 2014 Institute of Radiation Physics,
Helmholtz-Zentrum Dresden - Rossendorf
Author(s): Carlchristian Eckert - c.eckert ( at ) hzdr.de
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once

namespace mallocMC
{
namespace ReservePoolPolicies
{
/**
* @brief set CUDA internal heap for device-side malloc calls
*
* This ReservePoolPolicy is intended for use with CUDA capable
* accelerators that support at least compute capability 2.0. It should
* be used in conjunction with a CreationPolicy that actually requires
* the CUDA-internal heap to be sized by calls to cudaDeviceSetLimit()
*/
struct CudaSetLimits;

} // namespace ReservePoolPolicies
} // namespace mallocMC
Loading

0 comments on commit f8a9d92

Please sign in to comment.