DDC 0.5.2
Loading...
Searching...
No Matches
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{
18 // Kokkos natively supports alignment for any scalar type and `Kokkos::complex<T>`
19 static_assert(
20 alignof(T)
21 <= std::max(alignof(std::max_align_t), alignof(Kokkos::complex<long double>)),
22 "Alignment not supported");
23
24public:
25 using value_type = T;
26
27 using memory_space = MemorySpace;
28
29 template <class U>
30 struct rebind
31 {
32 using other = KokkosAllocator<U, MemorySpace>;
33 };
34
35 constexpr KokkosAllocator() = default;
36
37 constexpr KokkosAllocator(KokkosAllocator const& x) = default;
38
39 constexpr KokkosAllocator(KokkosAllocator&& x) noexcept = default;
40
41 template <class U>
42 constexpr explicit KokkosAllocator(KokkosAllocator<U, MemorySpace> const&) noexcept
43 {
44 }
45
46 ~KokkosAllocator() = default;
47
48 constexpr KokkosAllocator& operator=(KokkosAllocator const& x) = default;
49
50 constexpr KokkosAllocator& operator=(KokkosAllocator&& x) noexcept = default;
51
52 template <class U>
53 constexpr KokkosAllocator& operator=(KokkosAllocator<U, MemorySpace> const&) noexcept
54 {
55 }
56
57 [[nodiscard]] T* allocate(std::size_t n) const
58 {
59 return static_cast<T*>(Kokkos::kokkos_malloc<MemorySpace>(sizeof(T) * n));
60 }
61
62 [[nodiscard]] T* allocate(std::string const& label, std::size_t n) const
63 {
64 return static_cast<T*>(Kokkos::kokkos_malloc<MemorySpace>(label, sizeof(T) * n));
65 }
66
67 void deallocate(T* p, std::size_t) const
68 {
69 Kokkos::kokkos_free(p);
70 }
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
79#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
80// In C++20, `a!=b` shall be automatically translated by the compiler to `!(a==b)`
81template <class T, class MST, class U, class MSU>
82constexpr bool operator!=(KokkosAllocator<T, MST> const&, KokkosAllocator<U, MSU> const&) noexcept
83{
85}
86#endif
87
88template <class T>
89using DeviceAllocator = KokkosAllocator<T, Kokkos::DefaultExecutionSpace::memory_space>;
90
91template <class T>
92using HostAllocator = KokkosAllocator<T, Kokkos::HostSpace>;
93
94} // namespace ddc
constexpr KokkosAllocator(KokkosAllocator< U, MemorySpace > const &) noexcept
void deallocate(T *p, std::size_t) const
T * allocate(std::size_t n) const
constexpr KokkosAllocator(KokkosAllocator &&x) noexcept=default
constexpr KokkosAllocator & operator=(KokkosAllocator const &x)=default
~KokkosAllocator()=default
constexpr KokkosAllocator & operator=(KokkosAllocator< U, MemorySpace > const &) noexcept
constexpr KokkosAllocator()=default
constexpr KokkosAllocator & operator=(KokkosAllocator &&x) noexcept=default
constexpr KokkosAllocator(KokkosAllocator const &x)=default
T * allocate(std::string const &label, std::size_t n) const
The top-level namespace of DDC.
constexpr bool operator==(KokkosAllocator< T, MST > const &, KokkosAllocator< U, MSU > const &) noexcept