19#ifndef OPM_GPUVIEW_HEADER_HPP
20#define OPM_GPUVIEW_HEADER_HPP
22#include <dune/common/fvector.hh>
24#include <opm/common/ErrorMacros.hpp>
26#include <opm/common/utility/gpuDecorators.hpp>
27#include <opm/simulators/linalg/gpuistl/detail/safe_conversion.hpp>
75 return m_dataPtr[idx];
87 return m_dataPtr[idx];
100 GpuView(T* dataOnHost,
size_t numberOfElements)
101 : m_dataPtr(dataOnHost), m_numberOfElements(numberOfElements)
120 __host__ __device__
const T*
data()
const{
127 __host__ __device__
bool empty()
const {
128 return m_numberOfElements == 0;
150 return m_dataPtr[m_numberOfElements-1];
167 __host__ __device__ T
back()
const
172 return m_dataPtr[m_numberOfElements-1];
182 void copyFromHost(
const T* dataPointer,
size_t numberOfElements);
191 void copyToHost(T* dataPointer,
size_t numberOfElements)
const;
215 __host__ __device__
size_t size()
const{
216 return m_numberOfElements;
228 using iterator_category = std::forward_iterator_tag;
229 using difference_type = std::ptrdiff_t;
230 using value_type = T;
232 using reference = T&;
237 __host__ __device__
iterator(T* ptr) : m_ptr(ptr) {}
280 return !(m_ptr == other.m_ptr);
286 return m_ptr == other.m_ptr;
293 return std::distance(other.m_ptr, m_ptr);
314 return m_ptr < other.m_ptr;
321 return m_ptr > other.m_ptr;
350 return iterator(m_dataPtr + m_numberOfElements);
358 return iterator(m_dataPtr + m_numberOfElements);
363 size_t m_numberOfElements;
367 __host__ __device__
void assertSameSize(
const GpuView<T>& other)
const
369 assertSameSize(other.m_numberOfElements);
373 __host__ __device__
void assertSameSize(
size_t size)
const
375#if OPM_IS_INSIDE_DEVICE_FUNCTION
377 assert(
size == m_numberOfElements &&
"Views did not have the same size");
379 if (
size != m_numberOfElements) {
380 OPM_THROW(std::invalid_argument,
381 fmt::format(fmt::runtime(
"Given view has {}, while this View has {}."),
size, m_numberOfElements));
387 __host__ __device__
void assertHasElements()
const
389#if OPM_IS_INSIDE_DEVICE_FUNCTION
391 assert(m_numberOfElements > 0 &&
"View has 0 elements");
393 if (m_numberOfElements <= 0) {
394 OPM_THROW(std::invalid_argument,
"View has 0 elements");
400 __host__ __device__
void assertInRange(
size_t idx)
const
402#if OPM_IS_INSIDE_DEVICE_FUNCTION
404 assert(idx < m_numberOfElements &&
"The index provided was not in the range [0, buffersize-1]");
406 if (idx >= m_numberOfElements) {
407 OPM_THROW(std::invalid_argument,
408 fmt::format(fmt::runtime(
"The index provided was not in the range [0, buffersize-1]")));
Iterator class to make GpuViews more similar to std containers.
Definition GpuView.hpp:225
__host__ __device__ bool operator>(const iterator &other) const
Greater than comparison.
Definition GpuView.hpp:320
__host__ __device__ iterator operator--(int)
Post-decrement operator.
Definition GpuView.hpp:271
__host__ __device__ bool operator==(const iterator &other) const
Inequality comparison operator.
Definition GpuView.hpp:285
__host__ __device__ reference operator*() const
Dereference operator.
Definition GpuView.hpp:241
__host__ __device__ difference_type operator-(const iterator &other) const
subtraction operator
Definition GpuView.hpp:292
__host__ __device__ iterator & operator++()
Pre-increment operator.
Definition GpuView.hpp:247
__host__ __device__ iterator operator+(difference_type n) const
Addition operator with diffptr.
Definition GpuView.hpp:306
__host__ __device__ iterator operator-(difference_type n) const
Subtraction of given number of elements from iterator.
Definition GpuView.hpp:299
__host__ __device__ iterator operator++(int)
Post-increment operator.
Definition GpuView.hpp:255
__host__ __device__ iterator & operator--()
Pre-decrement operator.
Definition GpuView.hpp:263
__host__ __device__ bool operator!=(const iterator &other) const
Inequality comparison operator.
Definition GpuView.hpp:279
__host__ __device__ iterator(T *ptr)
Create iterator from a pointer.
Definition GpuView.hpp:237
__host__ __device__ bool operator<(const iterator &other) const
Less than comparison.
Definition GpuView.hpp:313
The GpuView class is provides a view of some data allocated on the GPU Essenstially is only stores a ...
Definition GpuView.hpp:53
__host__ __device__ iterator begin()
Get an iterator pointing to the first element of the buffer.
Definition GpuView.hpp:333
GpuView()=default
Default constructor that will initialize cublas and allocate 0 bytes of memory.
GpuView(T *dataOnHost, size_t numberOfElements)
GpuView allocates new GPU memory of size numberOfElements * sizeof(T) and copies numberOfElements fro...
Definition GpuView.hpp:100
__host__ __device__ bool empty() const
Definition GpuView.hpp:127
__host__ __device__ size_t size() const
size returns the size (number of T elements) in the vector
Definition GpuView.hpp:215
__host__ __device__ T * data()
Definition GpuView.hpp:113
void copyFromHost(const T *dataPointer, size_t numberOfElements)
copyFromHost copies numberOfElements from the CPU memory dataPointer
Definition GpuView.cpp:46
__host__ __device__ iterator end() const
Get a const iterator pointing to the address after the last element of the buffer.
Definition GpuView.hpp:357
__host__ __device__ T & front()
Definition GpuView.hpp:134
__host__ __device__ T operator[](size_t idx) const
operator[] to retrieve a copy of an item in the buffer
Definition GpuView.hpp:83
void copyToHost(T *dataPointer, size_t numberOfElements) const
copyFromHost copies numberOfElements to the CPU memory dataPointer
Definition GpuView.cpp:59
__host__ __device__ T & back()
Definition GpuView.hpp:145
__host__ __device__ T back() const
Definition GpuView.hpp:167
__host__ __device__ T front() const
Definition GpuView.hpp:156
__host__ __device__ iterator end()
Get an iterator pointing to the address after the last element of the buffer.
Definition GpuView.hpp:349
__host__ __device__ const T * data() const
Definition GpuView.hpp:120
~GpuView()=default
~GpuView calls cudaFree
__host__ __device__ T & operator[](size_t idx)
operator[] to retrieve a reference to an item in the buffer
Definition GpuView.hpp:71
std::vector< T > asStdVector() const
creates an std::vector of the same size and copies the GPU data to this std::vector
Definition GpuView.cpp:37
__host__ __device__ iterator begin() const
Get a const iterator pointing to the first element of the buffer.
Definition GpuView.hpp:341
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUDA...
Definition AmgxInterface.hpp:38