CPPuddle
Loading...
Searching...
No Matches
recycling_kokkos_view.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2024 Gregor Daiß
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef RECYCLING_KOKKOS_VIEW_HPP
7#define RECYCLING_KOKKOS_VIEW_HPP
8#include <Kokkos_Core.hpp>
9#include <memory>
10#include <type_traits>
11
13
18
19namespace cppuddle {
20namespace memory_recycling {
21
24template<typename element_type, typename alloc_type>
26 alloc_type allocator;
28 view_deleter(alloc_type alloc, size_t total_elements) : allocator(alloc),
30 void operator()(element_type* p) {
31 allocator.deallocate(p, total_elements);
32 }
33};
34
38
40template <typename kokkos_type, typename alloc_type, typename element_type>
41class aggregated_recycling_view : public kokkos_type {
42private:
43 alloc_type allocator;
44 size_t total_elements{0};
45 std::shared_ptr<element_type> data_ref_counter;
46 static_assert(std::is_same_v<element_type, typename alloc_type::value_type>);
47
48public:
49 using view_type = kokkos_type;
50 template <class... Args>
51 explicit aggregated_recycling_view(alloc_type &alloc, Args... args)
52 : kokkos_type(
53 alloc.allocate(kokkos_type::required_allocation_size(args...) /
54 sizeof(element_type)),
55 args...),
56 total_elements(kokkos_type::required_allocation_size(args...) /
57 sizeof(element_type)),
58 allocator(alloc),
59 data_ref_counter(this->data(), view_deleter<element_type, alloc_type>(
60 alloc, total_elements)) {}
61
64 : kokkos_type(other), allocator(other.allocator) {
65 data_ref_counter = other.data_ref_counter;
66 total_elements = other.total_elements;
67 }
68
71 data_ref_counter = other.data_ref_counter;
72 allocator = other.allocator;
73 kokkos_type::operator=(other);
74 total_elements = other.total_elements;
75 return *this;
76 }
77
80 : kokkos_type(other), allocator(other.allocator) {
81 data_ref_counter = other.data_ref_counter;
82 total_elements = other.total_elements;
83 }
84
87 data_ref_counter = other.data_ref_counter;
88 allocator = other.allocator;
89 kokkos_type::operator=(other);
90 total_elements = other.total_elements;
91 return *this;
92 }
93
95};
96
97
100
102template <typename kokkos_type, typename alloc_type, typename element_type>
103class recycling_view : public kokkos_type {
104private:
105 size_t total_elements{0};
106 std::shared_ptr<element_type> data_ref_counter;
107
108public:
109 using view_type = kokkos_type;
110 static_assert(std::is_same_v<element_type, typename alloc_type::value_type>);
111 template <typename... Args,
112 std::enable_if_t<sizeof...(Args) == kokkos_type::rank, bool> = true>
113 recycling_view(Args... args)
114 : kokkos_type(
115 alloc_type{}.allocate(kokkos_type::required_allocation_size(args...) /
116 sizeof(element_type)),
117 args...),
118 total_elements(kokkos_type::required_allocation_size(args...) /
119 sizeof(element_type)),
120 data_ref_counter(this->data(), view_deleter<element_type, alloc_type>(
121 alloc_type{}, total_elements)) {}
122
123 template <typename... Args,
124 std::enable_if_t<sizeof...(Args) == kokkos_type::rank, bool> = true>
125 recycling_view(const size_t device_id, Args... args)
126 : kokkos_type(
127 alloc_type{device_id}.allocate(kokkos_type::required_allocation_size(args...) /
128 sizeof(element_type)),
129 args...),
130 total_elements(kokkos_type::required_allocation_size(args...) /
131 sizeof(element_type)),
132 data_ref_counter(this->data(), view_deleter<element_type, alloc_type>(
133 alloc_type{device_id}, total_elements)) {}
134
135 template <
136 typename layout_t,
137 std::enable_if_t<Kokkos::is_array_layout<layout_t>::value, bool> = true>
138 recycling_view(std::size_t device_id, layout_t layout)
139 : kokkos_type(
140 alloc_type{device_id}.allocate(kokkos_type::required_allocation_size(layout) /
141 sizeof(element_type)),
142 layout),
143 total_elements(kokkos_type::required_allocation_size(layout) /
144 sizeof(element_type)),
145 data_ref_counter(this->data(), view_deleter<element_type, alloc_type>(
146 alloc_type{device_id}, total_elements)) {}
147
150 : kokkos_type(other) {
151 total_elements = other.total_elements;
152 data_ref_counter = other.data_ref_counter;
153
154 }
155
158 data_ref_counter = other.data_ref_counter;
159 kokkos_type::operator=(other);
160 total_elements = other.total_elements;
161 return *this;
162 }
163
166 : kokkos_type(other) {
167 data_ref_counter = other.data_ref_counter;
168 total_elements = other.total_elements;
169 }
170
173 data_ref_counter = other.data_ref_counter;
174 kokkos_type::operator=(other);
175 total_elements = other.total_elements;
176 return *this;
177 }
178
180};
181
182} // namespace memory_recycling
183} // end namespace cppuddle
184
185#endif
Definition recycling_kokkos_view.hpp:41
aggregated_recycling_view(aggregated_recycling_view< kokkos_type, alloc_type, element_type > &&other) noexcept
Definition recycling_kokkos_view.hpp:78
aggregated_recycling_view< kokkos_type, alloc_type, element_type > & operator=(aggregated_recycling_view< kokkos_type, alloc_type, element_type > &&other) noexcept
Definition recycling_kokkos_view.hpp:85
~aggregated_recycling_view()
Definition recycling_kokkos_view.hpp:94
aggregated_recycling_view< kokkos_type, alloc_type, element_type > & operator=(const aggregated_recycling_view< kokkos_type, alloc_type, element_type > &other)
Definition recycling_kokkos_view.hpp:70
aggregated_recycling_view(const aggregated_recycling_view< kokkos_type, alloc_type, element_type > &other)
Definition recycling_kokkos_view.hpp:62
aggregated_recycling_view(alloc_type &alloc, Args... args)
Definition recycling_kokkos_view.hpp:51
kokkos_type view_type
Definition recycling_kokkos_view.hpp:49
Definition recycling_kokkos_view.hpp:103
kokkos_type view_type
Definition recycling_kokkos_view.hpp:109
recycling_view(recycling_view< kokkos_type, alloc_type, element_type > &&other) noexcept
Definition recycling_kokkos_view.hpp:164
~recycling_view()
Definition recycling_kokkos_view.hpp:179
recycling_view< kokkos_type, alloc_type, element_type > & operator=(recycling_view< kokkos_type, alloc_type, element_type > &&other) noexcept
Definition recycling_kokkos_view.hpp:171
recycling_view(const recycling_view< kokkos_type, alloc_type, element_type > &other)
Definition recycling_kokkos_view.hpp:148
recycling_view(const size_t device_id, Args... args)
Definition recycling_kokkos_view.hpp:125
recycling_view(Args... args)
Definition recycling_kokkos_view.hpp:113
recycling_view< kokkos_type, alloc_type, element_type > & operator=(const recycling_view< kokkos_type, alloc_type, element_type > &other)
Definition recycling_kokkos_view.hpp:157
recycling_view(std::size_t device_id, layout_t layout)
Definition recycling_kokkos_view.hpp:138
Definition config.hpp:31
Definition recycling_kokkos_view.hpp:25
size_t total_elements
Definition recycling_kokkos_view.hpp:27
view_deleter(alloc_type alloc, size_t total_elements)
Definition recycling_kokkos_view.hpp:28
void operator()(element_type *p)
Definition recycling_kokkos_view.hpp:30
alloc_type allocator
Definition recycling_kokkos_view.hpp:26