19#ifndef OPM_GPUVECTOR_HEADER_HPP
20#define OPM_GPUVECTOR_HEADER_HPP
26#include <opm/common/ErrorMacros.hpp>
28#include <dune/common/fvector.hh>
29#include <dune/istl/bvector.hh>
30#include <fmt/format.h>
80 using size_type = size_t;
91 GpuVector(
const GpuVector<T>& other);
104 explicit GpuVector(
const std::vector<T>& data);
114 GpuVector& operator=(
const GpuVector<T>& other);
126 template<
int BlockDimension>
127 explicit GpuVector(
const Dune::BlockVector<Dune::FieldVector<T, BlockDimension>>& bvector)
128 : GpuVector(bvector.
dim())
130 copyFromHost(bvector);
140 GpuVector& operator=(T scalar);
145 GpuVector() =
default;
152 explicit GpuVector(
const size_t numberOfElements);
164 GpuVector(
const T* dataOnHost,
const size_t numberOfElements);
169 virtual ~GpuVector() =
default;
179 const T* data()
const;
188 template <
int BlockDimension>
189 void copyFromHost(
const Dune::BlockVector<Dune::FieldVector<T, BlockDimension>>& bvector)
191 m_buffer.copyFromHost(bvector);
202 template <
int BlockDimension>
203 void copyFromHostAsync(
const Dune::BlockVector<Dune::FieldVector<T, BlockDimension>>& bvector, cudaStream_t stream =
detail::DEFAULT_STREAM)
206 if (
dim() != bvector.dim()) {
207 OPM_THROW(std::runtime_error,
208 fmt::format(
"Given incompatible vector size. GpuVector has size {}, \n"
209 "however, BlockVector has N() = {}, and dim = {}.",
214 const auto dataPointer =
static_cast<const T*
>(&(bvector[0][0]));
215 copyFromHostAsync(dataPointer,
dim(), stream);
225 template <
int BlockDimension>
226 void copyToHost(Dune::BlockVector<Dune::FieldVector<T, BlockDimension>>& bvector)
const
228 m_buffer.copyToHost(bvector);
239 template <
int BlockDimension>
240 void copyToHostAsync(Dune::BlockVector<Dune::FieldVector<T, BlockDimension>>& bvector, cudaStream_t stream =
detail::DEFAULT_STREAM)
const
243 if (
dim() != bvector.dim()) {
244 OPM_THROW(std::runtime_error,
245 fmt::format(
"Given incompatible vector size. GpuVector has size {},\n however, the BlockVector "
246 "has has N() = {}, and dim() = {}.",
251 const auto dataPointer =
static_cast<T*
>(&(bvector[0][0]));
252 copyToHostAsync(dataPointer,
dim(), stream);
262 void copyFromHost(
const T* dataPointer,
size_t numberOfElements);
274 void copyFromHostAsync(
const T* dataPointer,
size_t numberOfElements, cudaStream_t stream =
detail::DEFAULT_STREAM);
285 void copyToHost(T* dataPointer,
size_t numberOfElements)
const;
297 void copyToHostAsync(T* dataPointer,
size_t numberOfElements, cudaStream_t stream =
detail::DEFAULT_STREAM)
const;
308 void copyFromHost(
const std::vector<T>& data);
327 void copyToHost(std::vector<T>& data)
const;
346 void copyFromDeviceToDevice(
const GpuVector<T>& other);
348 void prepareSendBuf(GpuVector<T>& buffer,
const GpuVector<int>& indexSet)
const;
349 void syncFromRecvBuf(GpuVector<T>& buffer,
const GpuVector<int>& indexSet);
359 GpuVector<T>& operator*=(
const T& scalar);
369 GpuVector<T>& axpy(T alpha,
const GpuVector<T>& y);
377 GpuVector<T>& operator+=(
const GpuVector<T>& other);
385 GpuVector<T>& operator-=(
const GpuVector<T>& other);
395 T dot(
const GpuVector<T>& other)
const;
411 T dot(
const GpuVector<T>& other,
const GpuVector<int>& indexSet, GpuVector<T>& buffer)
const;
418 T two_norm(
const GpuVector<int>& indexSet, GpuVector<T>& buffer)
const;
426 T dot(
const GpuVector<T>& other,
const GpuVector<int>& indexSet)
const;
433 T two_norm(
const GpuVector<int>& indexSet)
const;
440 size_type
dim()
const;
450 void resize(
size_t new_size);
456 std::vector<T> asStdVector()
const;
462 template <
int blockSize>
463 Dune::BlockVector<Dune::FieldVector<T, blockSize>> asDuneBlockVector()
const
465 OPM_ERROR_IF(
dim() % blockSize != 0,
466 fmt::format(
"blockSize is not a multiple of dim(). Given blockSize = {}, and dim() = {}",
470 Dune::BlockVector<Dune::FieldVector<T, blockSize>> returnValue(
dim() / blockSize);
471 copyToHost(returnValue);
494 std::vector<T> v = asStdVector();
495 std::string res =
"";
504 GpuBuffer<T> m_buffer;
507 void assertSameSize(
const GpuVector<T>& other)
const;
508 void assertSameSize(
size_t size)
const;
510 void assertHasElements()
const;
515 static size_t checkedSize(
size_t size);
526 const auto hostBlockVector = vectorOnDevice.template asDuneBlockVector<1>();
static CuBlasHandle & getInstance()
getInstance creates (if necessary) and returns the single unique instance of CuBlasHandle (singleton)
static constexpr int dim
Definition: structuredgridvanguard.hh:68
void syncFromRecvBuf(T *deviceA, T *buffer, size_t numberOfElements, const int *indices)
constexpr cudaStream_t DEFAULT_STREAM
The default GPU stream (stream 0)
Definition: gpu_constants.hpp:31
void prepareSendBuf(const T *deviceA, T *buffer, size_t numberOfElements, const int *indices)
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUDA...
Definition: GpuFlowProblem.hpp:48
std::string toDebugString()
Definition: GpuVector.hpp:492
void setZeroAtIndexSet(const GpuVector< int > &indexSet)
The GpuVector class is a simple (arithmetic) vector class for the GPU that wraps a GpuBuffer.
void writeMatrixMarket(const GpuVector< T > &vectorOnDevice, std::ostream &ostr)
Definition: GpuVector.hpp:524
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)