Ginkgo Generated from branch based on main. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
memory.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_BASE_MEMORY_HPP_
6#define GKO_PUBLIC_CORE_BASE_MEMORY_HPP_
7
8
9#include <ginkgo/core/base/fwd_decls.hpp>
10#include <ginkgo/core/base/types.hpp>
11
12
13namespace gko {
14
15
20class Allocator {
21public:
22 virtual ~Allocator() = default;
23
24 virtual void* allocate(size_type num_bytes) = 0;
25
26 virtual void deallocate(void* ptr) = 0;
27};
28
29
34class CpuAllocatorBase : public Allocator {};
35
36
41 friend class CudaExecutor;
42
43protected:
55 virtual bool check_environment(int device_id, CUstream_st* stream) const
56 {
57 return true;
58 }
59};
60
61
66 friend class HipExecutor;
67
68protected:
80 virtual bool check_environment(int device_id,
81 GKO_HIP_STREAM_STRUCT* stream) const
82 {
83 return true;
84 }
85};
86
87
92public:
93 void* allocate(size_type num_bytes) override;
94
95 void deallocate(void* ptr) override;
96};
97
98
103public:
104 void* allocate(size_type num_bytes) override;
105
106 void deallocate(void* ptr) override;
107};
108
109
110/*
111 * Allocator using cudaMallocAsync.
112 */
113class CudaAsyncAllocator : public CudaAllocatorBase {
114public:
115 void* allocate(size_type num_bytes) override;
116
117 void deallocate(void* ptr) override;
118
119 CudaAsyncAllocator(CUstream_st* stream);
120
121 bool check_environment(int device_id, CUstream_st* stream) const override;
122
123private:
124 CUstream_st* stream_;
125};
126
127
128/*
129 * Allocator using cudaMallocManaged
130 */
131class CudaUnifiedAllocator : public CudaAllocatorBase, public CpuAllocatorBase {
132public:
133 void* allocate(size_type num_bytes) override;
134
135 void deallocate(void* ptr) override;
136
137 CudaUnifiedAllocator(int device_id);
138
139 CudaUnifiedAllocator(int device_id, unsigned int flags);
140
141protected:
142 bool check_environment(int device_id, CUstream_st* stream) const override;
143
144private:
145 int device_id_;
146 unsigned int flags_;
147};
148
149
150/*
151 * Allocator using cudaHostMalloc.
152 */
153class CudaHostAllocator : public CudaAllocatorBase, public CpuAllocatorBase {
154public:
155 void* allocate(size_type num_bytes) override;
156
157 void deallocate(void* ptr) override;
158
159 CudaHostAllocator(int device_id);
160
161protected:
162 bool check_environment(int device_id, CUstream_st* stream) const override;
163
164private:
165 int device_id_;
166};
167
168
169/*
170 * Allocator using hipMalloc.
171 */
173public:
174 void* allocate(size_type num_bytes) override;
175
176 void deallocate(void* ptr) override;
177};
178
179
180/*
181 * Allocator using hipMallocAsync.
182 */
183class HipAsyncAllocator : public HipAllocatorBase {
184public:
185 void* allocate(size_type num_bytes) override;
186
187 void deallocate(void* ptr) override;
188
189 HipAsyncAllocator(GKO_HIP_STREAM_STRUCT* stream);
190
191protected:
192 bool check_environment(int device_id,
193 GKO_HIP_STREAM_STRUCT* stream) const override;
194
195private:
196 GKO_HIP_STREAM_STRUCT* stream_;
197};
198
199
200/*
201 * Allocator using hipMallocManaged
202 */
203class HipUnifiedAllocator : public HipAllocatorBase, public CpuAllocatorBase {
204public:
205 void* allocate(size_type num_bytes) override;
206
207 void deallocate(void* ptr) override;
208
209 HipUnifiedAllocator(int device_id);
210
211 HipUnifiedAllocator(int device_id, unsigned int flags);
212
213protected:
214 bool check_environment(int device_id,
215 GKO_HIP_STREAM_STRUCT* stream) const override;
216
217private:
218 int device_id_;
219 unsigned int flags_;
220};
221
222
223/*
224 * Allocator using hipHostAlloc.
225 */
226class HipHostAllocator : public HipAllocatorBase, public CpuAllocatorBase {
227public:
228 void* allocate(size_type num_bytes) override;
229
230 void deallocate(void* ptr) override;
231
232 HipHostAllocator(int device_id);
233
234protected:
235 bool check_environment(int device_id,
236 GKO_HIP_STREAM_STRUCT* stream) const override;
237
238private:
239 int device_id_;
240};
241
242
243} // namespace gko
244
245
246#endif // GKO_PUBLIC_CORE_BASE_MEMORY_HPP_
Provides generic allocation and deallocation functionality to be used by an Executor.
Definition memory.hpp:20
Implement this interface to provide an allocator for OmpExecutor or ReferenceExecutor.
Definition memory.hpp:34
Allocator using new/delete.
Definition memory.hpp:91
Implement this interface to provide an allocator for CudaExecutor.
Definition memory.hpp:40
Allocator using cudaMalloc.
Definition memory.hpp:102
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.
Implement this interface to provide an allocator for HipExecutor.
Definition memory.hpp:65
Definition memory.hpp:172
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90