Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
memory.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_MEMORY_HPP_
34#define GKO_PUBLIC_CORE_BASE_MEMORY_HPP_
35
36
37#include <ginkgo/core/base/fwd_decls.hpp>
38#include <ginkgo/core/base/types.hpp>
39
40
41namespace gko {
42
43
48class Allocator {
49public:
50 virtual ~Allocator() = default;
51
52 virtual void* allocate(size_type num_bytes) = 0;
53
54 virtual void deallocate(void* ptr) = 0;
55};
56
57
62class CpuAllocatorBase : public Allocator {};
63
64
69 friend class CudaExecutor;
70
71protected:
83 virtual bool check_environment(int device_id, CUstream_st* stream) const
84 {
85 return true;
86 }
87};
88
89
94 friend class HipExecutor;
95
96protected:
108 virtual bool check_environment(int device_id,
109 GKO_HIP_STREAM_STRUCT* stream) const
110 {
111 return true;
112 }
113};
114
115
120public:
121 void* allocate(size_type num_bytes) override;
122
123 void deallocate(void* ptr) override;
124};
125
126
131public:
132 void* allocate(size_type num_bytes) override;
133
134 void deallocate(void* ptr) override;
135};
136
137
138/*
139 * Allocator using cudaMallocAsync.
140 */
142public:
143 void* allocate(size_type num_bytes) override;
144
145 void deallocate(void* ptr) override;
146
148
149 bool check_environment(int device_id, CUstream_st* stream) const override;
150
151private:
152 CUstream_st* stream_;
153};
154
155
156/*
157 * Allocator using cudaMallocManaged
158 */
160public:
161 void* allocate(size_type num_bytes) override;
162
163 void deallocate(void* ptr) override;
164
165 CudaUnifiedAllocator(int device_id);
166
167 CudaUnifiedAllocator(int device_id, unsigned int flags);
168
169protected:
170 bool check_environment(int device_id, CUstream_st* stream) const override;
171
172private:
173 int device_id_;
174 unsigned int flags_;
175};
176
177
178/*
179 * Allocator using cudaHostMalloc.
180 */
182public:
183 void* allocate(size_type num_bytes) override;
184
185 void deallocate(void* ptr) override;
186
187 CudaHostAllocator(int device_id);
188
189protected:
190 bool check_environment(int device_id, CUstream_st* stream) const override;
191
192private:
193 int device_id_;
194};
195
196
197/*
198 * Allocator using hipMalloc.
199 */
201public:
202 void* allocate(size_type num_bytes) override;
203
204 void deallocate(void* ptr) override;
205};
206
207
208/*
209 * Allocator using hipMallocAsync.
210 */
212public:
213 void* allocate(size_type num_bytes) override;
214
215 void deallocate(void* ptr) override;
216
217 HipAsyncAllocator(GKO_HIP_STREAM_STRUCT* stream);
218
219protected:
220 bool check_environment(int device_id,
221 GKO_HIP_STREAM_STRUCT* stream) const override;
222
223private:
224 GKO_HIP_STREAM_STRUCT* stream_;
225};
226
227
228/*
229 * Allocator using hipMallocManaged
230 */
232public:
233 void* allocate(size_type num_bytes) override;
234
235 void deallocate(void* ptr) override;
236
237 HipUnifiedAllocator(int device_id);
238
239 HipUnifiedAllocator(int device_id, unsigned int flags);
240
241protected:
242 bool check_environment(int device_id,
243 GKO_HIP_STREAM_STRUCT* stream) const override;
244
245private:
246 int device_id_;
247 unsigned int flags_;
248};
249
250
251/*
252 * Allocator using hipHostAlloc.
253 */
255public:
256 void* allocate(size_type num_bytes) override;
257
258 void deallocate(void* ptr) override;
259
260 HipHostAllocator(int device_id);
261
262protected:
263 bool check_environment(int device_id,
264 GKO_HIP_STREAM_STRUCT* stream) const override;
265
266private:
267 int device_id_;
268};
269
270
271} // namespace gko
272
273
274#endif // GKO_PUBLIC_CORE_BASE_MEMORY_HPP_
Provides generic allocation and deallocation functionality to be used by an Executor.
Definition memory.hpp:48
Implement this interface to provide an allocator for OmpExecutor or ReferenceExecutor.
Definition memory.hpp:62
Allocator using new/delete.
Definition memory.hpp:119
Implement this interface to provide an allocator for CudaExecutor.
Definition memory.hpp:68
Allocator using cudaMalloc.
Definition memory.hpp:130
Definition memory.hpp:141
bool check_environment(int device_id, CUstream_st *stream) const override
Checks if the allocator can be used safely with the provided device ID and stream.
This is the Executor subclass which represents the CUDA device.
Definition executor.hpp:1513
Definition memory.hpp:181
Definition memory.hpp:159
Implement this interface to provide an allocator for HipExecutor.
Definition memory.hpp:93
Definition memory.hpp:200
Definition memory.hpp:211
This is the Executor subclass which represents the HIP enhanced device.
Definition executor.hpp:1727
Definition memory.hpp:254
Definition memory.hpp:231
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120