GpuBuffer.hpp
Go to the documentation of this file.
1/*
2 Copyright 2024 SINTEF AS
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef OPM_GPUBUFFER_HEADER_HPP
20#define OPM_GPUBUFFER_HEADER_HPP
21#include <dune/common/fvector.hh>
22#include <dune/istl/bvector.hh>
23#include <exception>
24#include <fmt/core.h>
25#include <opm/common/ErrorMacros.hpp>
28#include <vector>
29#include <string>
30
31
32namespace Opm::gpuistl
33{
34
54template <typename T>
55class GpuBuffer
56{
57public:
58 using field_type = T;
59 using size_type = size_t;
60 using value_type = T;
61
70 GpuBuffer(const GpuBuffer<T>& other);
71
81 explicit GpuBuffer(const std::vector<T>& data);
82
86 explicit GpuBuffer();
87
93 explicit GpuBuffer(const size_t numberOfElements);
94
95
105 GpuBuffer(const T* dataOnHost, const size_t numberOfElements);
106
110 virtual ~GpuBuffer();
111
115 T* data();
116
120 const T* data() const;
121
125 __host__ __device__ T& front()
126 {
127#ifndef NDEBUG
128 assertHasElements();
129#endif
130 return m_dataOnDevice[0];
131 }
132
136 __host__ __device__ T& back()
137 {
138#ifndef NDEBUG
139 assertHasElements();
140#endif
141 return m_dataOnDevice[m_numberOfElements-1];
142 }
143
147 __host__ __device__ T front() const
148 {
149#ifndef NDEBUG
150 assertHasElements();
151#endif
152 return m_dataOnDevice[0];
153 }
154
158 __host__ __device__ T back() const
159 {
160#ifndef NDEBUG
161 assertHasElements();
162#endif
163 return m_dataOnDevice[m_numberOfElements-1];
164 }
165
173 template <int BlockDimension>
174 void copyFromHost(const Dune::BlockVector<Dune::FieldVector<T, BlockDimension>>& bvector)
175 {
176 // TODO: [perf] vector.size() can be replaced by bvector.N() * BlockDimension
177 if (m_numberOfElements != bvector.size()) {
178 OPM_THROW(std::runtime_error,
179 fmt::format("Given incompatible vector size. GpuBuffer has size {}, \n"
180 "however, BlockVector has N() = {}, and size = {}.",
181 m_numberOfElements,
182 bvector.N(),
183 bvector.size()));
184 }
185 const auto dataPointer = static_cast<const T*>(&(bvector[0][0]));
186 copyFromHost(dataPointer, m_numberOfElements);
187 }
188
196 template <int BlockDimension>
197 void copyToHost(Dune::BlockVector<Dune::FieldVector<T, BlockDimension>>& bvector) const
198 {
199 // TODO: [perf] vector.size() can be replaced by bvector.N() * BlockDimension
200 if (m_numberOfElements != bvector.size()) {
201 OPM_THROW(std::runtime_error,
202 fmt::format("Given incompatible vector size. GpuBuffer has size {},\n however, the BlockVector "
203 "has has N() = {}, and size() = {}.",
204 m_numberOfElements,
205 bvector.N(),
206 bvector.size()));
207 }
208 const auto dataPointer = static_cast<T*>(&(bvector[0][0]));
209 copyToHost(dataPointer, m_numberOfElements);
210 }
211
219 void copyFromHost(const T* dataPointer, size_t numberOfElements);
220
228 void copyToHost(T* dataPointer, size_t numberOfElements) const;
229
237 void copyFromHost(const std::vector<T>& data);
238
246 void copyToHost(std::vector<T>& data) const;
247
252 size_type size() const;
253
258 void resize(size_t);
259
264 std::vector<T> asStdVector() const;
265
266private:
267 T* m_dataOnDevice = nullptr;
268 size_t m_numberOfElements;
269
270 void assertSameSize(const GpuBuffer<T>& other) const;
271 void assertSameSize(size_t size) const;
272
273 void assertHasElements() const;
274};
275
276template <class T>
277GpuView<const T> make_view(const GpuBuffer<T>&);
278
279} // namespace Opm::gpuistl
280#endif
Definition: autotuner.hpp:29