DDC 0.0.0

a discrete domain computation library

kokkos_allocator.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <cstddef>
8#include <string>
9#include <type_traits>
10
11#include <Kokkos_Core.hpp>
12
13namespace ddc {
14
15template <class T, class MemorySpace>
17{
18public:
19 using value_type = T;
20
22
23 template <class U>
28
29 constexpr KokkosAllocator() = default;
30
31 constexpr KokkosAllocator(KokkosAllocator const& x) = default;
32
33 constexpr KokkosAllocator(KokkosAllocator&& x) noexcept = default;
34
35 template <class U>
36 constexpr explicit KokkosAllocator(KokkosAllocator<U, MemorySpace> const&) noexcept
37 {
38 }
39
40 ~KokkosAllocator() = default;
41
42 constexpr KokkosAllocator& operator=(KokkosAllocator const& x) = default;
43
44 constexpr KokkosAllocator& operator=(KokkosAllocator&& x) noexcept = default;
45
46 template <class U>
48 {
49 }
50
51 [[nodiscard]] T* allocate(std::size_t n) const
52 {
53 return static_cast<T*>(Kokkos::kokkos_malloc<MemorySpace>(sizeof(T) * n));
54 }
55
56 [[nodiscard]] T* allocate(std::string const& label, std::size_t n) const
57 {
58 return static_cast<T*>(Kokkos::kokkos_malloc<MemorySpace>(label, sizeof(T) * n));
59 }
60
61 void deallocate(T* p, std::size_t) const
62 {
63 Kokkos::kokkos_free(p);
64 }
65};
66
67template <class T, class MST, class U, class MSU>
68constexpr bool operator==(KokkosAllocator<T, MST> const&, KokkosAllocator<U, MSU> const&) noexcept
69{
70 return std::is_same_v<KokkosAllocator<T, MST>, KokkosAllocator<U, MSU>>;
71}
72
73template <class T, class MST, class U, class MSU>
74constexpr bool operator!=(KokkosAllocator<T, MST> const&, KokkosAllocator<U, MSU> const&) noexcept
75{
76 return !std::is_same_v<KokkosAllocator<T, MST>, KokkosAllocator<U, MSU>>;
77}
78
79template <class T>
81
82template <class T>
84
85} // namespace ddc
Definition kokkos_allocator.hpp:17
constexpr KokkosAllocator(KokkosAllocator< U, MemorySpace > const &) noexcept
Definition kokkos_allocator.hpp:36
void deallocate(T *p, std::size_t) const
Definition kokkos_allocator.hpp:61
T * allocate(std::size_t n) const
Definition kokkos_allocator.hpp:51
constexpr KokkosAllocator(KokkosAllocator &&x) noexcept=default
constexpr KokkosAllocator & operator=(KokkosAllocator const &x)=default
~KokkosAllocator()=default
constexpr KokkosAllocator & operator=(KokkosAllocator< U, MemorySpace > const &) noexcept
Definition kokkos_allocator.hpp:47
constexpr KokkosAllocator()=default
T value_type
Definition kokkos_allocator.hpp:19
MemorySpace memory_space
Definition kokkos_allocator.hpp:21
constexpr KokkosAllocator & operator=(KokkosAllocator &&x) noexcept=default
constexpr KokkosAllocator(KokkosAllocator const &x)=default
T * allocate(std::string const &label, std::size_t n) const
Definition kokkos_allocator.hpp:56
Definition kokkos_allocator.hpp:25
The top-level namespace of DDC.
Definition aligned_allocator.hpp:11
constexpr bool enable_chunk
Definition chunk_traits.hpp:16
constexpr bool operator!=(AlignedAllocator< T, NT > const &, AlignedAllocator< U, NU > const &) noexcept
Definition aligned_allocator.hpp:65
constexpr bool operator==(AlignedAllocator< T, NT > const &, AlignedAllocator< U, NU > const &) noexcept
Definition aligned_allocator.hpp:59