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