Ginkgo  Generated from pipelines/1589998975 branch based on develop. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
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 
13 namespace gko {
14 
15 
20 class Allocator {
21 public:
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 
34 class CpuAllocatorBase : public Allocator {};
35 
36 
40 class CudaAllocatorBase : public Allocator {
41  friend class CudaExecutor;
42 
43 protected:
55  virtual bool check_environment(int device_id, CUstream_st* stream) const
56  {
57  return true;
58  }
59 };
60 
61 
65 class HipAllocatorBase : public Allocator {
66  friend class HipExecutor;
67 
68 protected:
80  virtual bool check_environment(int device_id,
81  GKO_HIP_STREAM_STRUCT* stream) const
82  {
83  return true;
84  }
85 };
86 
87 
92 public:
93  void* allocate(size_type num_bytes) override;
94 
95  void deallocate(void* ptr) override;
96 };
97 
98 
103 public:
104  void* allocate(size_type num_bytes) override;
105 
106  void deallocate(void* ptr) override;
107 };
108 
109 
110 /*
111  * Allocator using cudaMallocAsync.
112  */
114 public:
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 
123 private:
124  CUstream_st* stream_;
125 };
126 
127 
128 /*
129  * Allocator using cudaMallocManaged
130  */
132 public:
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 
141 protected:
142  bool check_environment(int device_id, CUstream_st* stream) const override;
143 
144 private:
145  int device_id_;
146  unsigned int flags_;
147 };
148 
149 
150 /*
151  * Allocator using cudaHostMalloc.
152  */
154 public:
155  void* allocate(size_type num_bytes) override;
156 
157  void deallocate(void* ptr) override;
158 
159  CudaHostAllocator(int device_id);
160 
161 protected:
162  bool check_environment(int device_id, CUstream_st* stream) const override;
163 
164 private:
165  int device_id_;
166 };
167 
168 
169 /*
170  * Allocator using hipMalloc.
171  */
173 public:
174  void* allocate(size_type num_bytes) override;
175 
176  void deallocate(void* ptr) override;
177 };
178 
179 
180 /*
181  * Allocator using hipMallocAsync.
182  */
184 public:
185  void* allocate(size_type num_bytes) override;
186 
187  void deallocate(void* ptr) override;
188 
189  HipAsyncAllocator(GKO_HIP_STREAM_STRUCT* stream);
190 
191 protected:
192  bool check_environment(int device_id,
193  GKO_HIP_STREAM_STRUCT* stream) const override;
194 
195 private:
196  GKO_HIP_STREAM_STRUCT* stream_;
197 };
198 
199 
200 /*
201  * Allocator using hipMallocManaged
202  */
204 public:
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 
213 protected:
214  bool check_environment(int device_id,
215  GKO_HIP_STREAM_STRUCT* stream) const override;
216 
217 private:
218  int device_id_;
219  unsigned int flags_;
220 };
221 
222 
223 /*
224  * Allocator using hipHostAlloc.
225  */
227 public:
228  void* allocate(size_type num_bytes) override;
229 
230  void deallocate(void* ptr) override;
231 
232  HipHostAllocator(int device_id);
233 
234 protected:
235  bool check_environment(int device_id,
236  GKO_HIP_STREAM_STRUCT* stream) const override;
237 
238 private:
239  int device_id_;
240 };
241 
242 
243 } // namespace gko
244 
245 
246 #endif // GKO_PUBLIC_CORE_BASE_MEMORY_HPP_
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:89
gko::CudaAsyncAllocator::check_environment
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.
gko::HipExecutor
This is the Executor subclass which represents the HIP enhanced device.
Definition: executor.hpp:1777
gko::HipAllocator
Definition: memory.hpp:172
gko::CudaExecutor
This is the Executor subclass which represents the CUDA device.
Definition: executor.hpp:1540
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::CpuAllocatorBase
Implement this interface to provide an allocator for OmpExecutor or ReferenceExecutor.
Definition: memory.hpp:34
gko::CudaUnifiedAllocator
Definition: memory.hpp:131
gko::HipUnifiedAllocator
Definition: memory.hpp:203
gko::CudaHostAllocator
Definition: memory.hpp:153
gko::HipAllocatorBase
Implement this interface to provide an allocator for HipExecutor.
Definition: memory.hpp:65
gko::CudaAllocator
Allocator using cudaMalloc.
Definition: memory.hpp:102
gko::CpuAllocator
Allocator using new/delete.
Definition: memory.hpp:91
gko::Allocator
Provides generic allocation and deallocation functionality to be used by an Executor.
Definition: memory.hpp:20
gko::HipAsyncAllocator
Definition: memory.hpp:183
gko::CudaAllocatorBase
Implement this interface to provide an allocator for CudaExecutor.
Definition: memory.hpp:40
gko::HipHostAllocator
Definition: memory.hpp:226
gko::CudaAsyncAllocator
Definition: memory.hpp:113