Skip to content

Commit

Permalink
Add an allocator parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadioleau committed Mar 10, 2022
1 parent 39ebd8e commit aa5f35c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
29 changes: 18 additions & 11 deletions include/ddc/chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

#include "ddc/chunk_common.hpp"
#include "ddc/chunk_span.hpp"
#include "ddc/memory.hpp"

template <class, class>
template <class, class, class>
class Chunk;

template <class ElementType, class SupportType>
inline constexpr bool enable_chunk<Chunk<ElementType, SupportType>> = true;
template <class ElementType, class SupportType, class Allocator>
inline constexpr bool enable_chunk<Chunk<ElementType, SupportType, Allocator>> = true;

template <class ElementType, class... DDims>
class Chunk<ElementType, DiscreteDomain<DDims...>>
template <class ElementType, class... DDims, class Allocator>
class Chunk<ElementType, DiscreteDomain<DDims...>, Allocator>
: public ChunkCommon<ElementType, DiscreteDomain<DDims...>, std::experimental::layout_right>
{
protected:
Expand Down Expand Up @@ -60,23 +61,29 @@ class Chunk<ElementType, DiscreteDomain<DDims...>>

using reference = typename base_type::reference;

template <class, class>
template <class, class, class>
friend class Chunk;

private:
Allocator m_allocator;

public:
/// Empty Chunk
Chunk() = default;

/// Construct a Chunk on a domain with uninitialized values
explicit Chunk(mdomain_type const& domain)
: base_type(new (std::align_val_t(64)) value_type[domain.size()], domain)
explicit Chunk(mdomain_type const& domain, Allocator const& allocator = Allocator())
: m_allocator(allocator)
, base_type(std::allocator_traits<Allocator>::allocate(m_allocator, domain.size()), domain)
{
}

/// Construct a Chunk from a deepcopy of a ChunkSpan
template <class OElementType, class... ODDims, class LayoutType>
explicit Chunk(ChunkSpan<OElementType, DiscreteDomain<ODDims...>, LayoutType> chunk_span)
: Chunk(chunk_span.domain())
explicit Chunk(
ChunkSpan<OElementType, DiscreteDomain<ODDims...>, LayoutType> chunk_span,
Allocator const& allocator = Allocator())
: Chunk(chunk_span.domain(), allocator)
{
deepcopy(span_view(), chunk_span);
}
Expand All @@ -95,7 +102,7 @@ class Chunk<ElementType, DiscreteDomain<DDims...>>
~Chunk()
{
if (this->m_internal_mdspan.data()) {
operator delete[](this->data(), std::align_val_t(64));
std::allocator_traits<Allocator>::deallocate(m_allocator, this->data(), this->size());
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/ddc/chunk_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ChunkCommon<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy>
template <class, class, class>
friend class ChunkSpan;

template <class, class>
template <class, class, class>
friend class Chunk;

static_assert(mapping_type::is_always_strided());
Expand Down
12 changes: 7 additions & 5 deletions include/ddc/chunk_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

#include "ddc/chunk_common.hpp"
#include "ddc/discrete_domain.hpp"
#include "ddc/memory.hpp"

template <class, class>
template <class ElementType, class, class Allocator = HostAllocator<ElementType>>
class Chunk;

template <
Expand Down Expand Up @@ -119,8 +120,8 @@ class ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy>
/** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
* @param other the Chunk to view
*/
template <class OElementType>
constexpr ChunkSpan(Chunk<OElementType, mdomain_type>& other) noexcept
template <class OElementType, class Allocator>
constexpr ChunkSpan(Chunk<OElementType, mdomain_type, Allocator>& other) noexcept
: base_type(other.m_internal_mdspan, other.m_domain)
{
}
Expand All @@ -132,8 +133,9 @@ class ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy>
template <
class OElementType,
class SFINAEElementType = ElementType,
class = std::enable_if_t<std::is_const_v<SFINAEElementType>>>
constexpr ChunkSpan(Chunk<OElementType, mdomain_type> const& other) noexcept
class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
class Allocator>
constexpr ChunkSpan(Chunk<OElementType, mdomain_type, Allocator> const& other) noexcept
: base_type(other.m_internal_mdspan, other.m_domain)
{
}
Expand Down
53 changes: 53 additions & 0 deletions include/ddc/memory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <memory>
#include <type_traits>

template <class T>
struct HostAllocator;

struct MemorySpace
{
};

template <class T>
constexpr inline bool is_memory_space = std::is_base_of_v<MemorySpace, T>;

struct HostMemorySpace : MemorySpace
{
template <class T>
using default_allocator_type = HostAllocator<T>;
};

template <class Memory, class T>
using DefaultAllocator_t = typename Memory::template default_allocator_type<T>;

template <class T>
class HostAllocator
{
public:
using value_type = T;
using memory_space_type = HostMemorySpace;

constexpr HostAllocator() = default;

constexpr HostAllocator(HostAllocator const& x) = default;

constexpr HostAllocator(HostAllocator&& x) noexcept = default;

~HostAllocator() = default;

constexpr HostAllocator& operator=(HostAllocator const& x) = default;

constexpr HostAllocator& operator=(HostAllocator&& x) noexcept = default;

[[nodiscard]] T* allocate(std::size_t n)
{
return new (std::align_val_t(64)) value_type[n];
}

void deallocate(T* p, std::size_t)
{
operator delete[](p, std::align_val_t(64));
}
};

0 comments on commit aa5f35c

Please sign in to comment.