DDC 0.1.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 <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
21 using memory_space = MemorySpace;
22
23 template <class U>
24 struct rebind
25 {
26 using other = KokkosAllocator<U, MemorySpace>;
27 };
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>
47 constexpr KokkosAllocator& operator=(KokkosAllocator<U, MemorySpace> const&) noexcept
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
73#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
74// In C++20, `a!=b` shall be automatically translated by the compiler to `!(a==b)`
75template <class T, class MST, class U, class MSU>
76constexpr bool operator!=(KokkosAllocator<T, MST> const&, KokkosAllocator<U, MSU> const&) noexcept
77{
79}
80#endif
81
82template <class T>
83using DeviceAllocator = KokkosAllocator<T, Kokkos::DefaultExecutionSpace::memory_space>;
84
85template <class T>
86using HostAllocator = KokkosAllocator<T, Kokkos::HostSpace>;
87
88} // 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