18#ifndef OPM_MINIMATRIX_HPP
19#define OPM_MINIMATRIX_HPP
22#include <initializer_list>
24#include <opm/common/utility/gpuDecorators.hpp>
25#include <opm/simulators/linalg/gpuistl/MiniVector.hpp>
26#include <opm/simulators/linalg/matrixblock.hh>
36template<
class T,
int dimension>
40 using size_type = std::size_t;
41 using array_type = std::array<T, dimension * dimension>;
52 OPM_HOST_DEVICE
MiniMatrix(std::initializer_list<T> init) {
54 for (
auto it = init.begin(); it != init.end() && i < data_.size(); ++it, ++i)
62 template <
class Type,
int n,
int m>
64 static_assert(n == m,
"MiniMatrix can only be constructed from square MatrixBlock");
65 static_assert(n == dimension,
"MiniMatrix dimension must match MatrixBlock dimension");
66 for (size_type i = 0; i < dimension; ++i)
68 for (size_type j = 0; j < dimension; ++j)
70 (*this)[i][j] = mb[i][j];
85 return &data_[row * dimension];
93 OPM_HOST_DEVICE
const T*
operator[](size_type row)
const {
94 return &data_[row * dimension];
100 OPM_HOST_DEVICE
auto begin() {
return data_.begin(); }
105 OPM_HOST_DEVICE
auto end() {
return data_.end(); }
110 OPM_HOST_DEVICE
auto begin()
const {
return data_.begin(); }
115 OPM_HOST_DEVICE
auto end()
const {
return data_.end(); }
120 OPM_HOST_DEVICE T*
data() {
return data_.data(); }
125 OPM_HOST_DEVICE
const T*
data()
const {
return data_.data(); }
131 OPM_HOST_DEVICE
void fill(
const T& value) {
132 for (
auto& x : data_) x = value;
138 OPM_HOST_DEVICE
static constexpr size_type
size() {
return dimension; }
146 for (size_type i = 0; i < data_.size(); ++i)
147 data_[i] += other.data_[i];
168 for (size_type i = 0; i < data_.size(); ++i)
169 data_[i] -= other.data_[i];
184 OPM_HOST_DEVICE
MiniMatrix& operator=(
const T& scalar) {
185 for (
auto& x : data_)
195 OPM_HOST_DEVICE std::array<T, dimension>
operator*(
const std::array<T, dimension>& x)
const {
196 std::array<T, dimension> result{};
197 for (size_type row = 0; row < dimension; ++row) {
199 for (size_type col = 0; col < dimension; ++col)
200 sum += (*
this)[row][col] * x[col];
213 for (size_type row = 0; row < dimension; ++row) {
214 for (size_type col = 0; col < dimension; ++col) {
216 for (size_type k = 0; k < dimension; ++k)
217 sum += temp[row][k] * B[k][col];
218 (*this)[row][col] = sum;
243 for (size_type row = 0; row < dimension; ++row) {
245 for (size_type col = 0; col < dimension; ++col)
246 sum += (*
this)[row][col] * x[col];
253 for (
auto& x : data_)
259 for (
auto& x : data_)
Definition matrixblock.hh:229
A small fixed-size square matrix class for use in CUDA kernels.
Definition MiniMatrix.hpp:37
OPM_HOST_DEVICE MiniMatrix & operator-=(const MiniMatrix &other)
Subtract another matrix from this one (element-wise).
Definition MiniMatrix.hpp:167
OPM_HOST_DEVICE MiniMatrix & operator*=(const MiniMatrix &B)
Matrix-matrix multiplication: C = A * B.
Definition MiniMatrix.hpp:211
OPM_HOST_DEVICE auto begin()
Get iterator to beginning of data.
Definition MiniMatrix.hpp:100
OPM_HOST_DEVICE auto end()
Get iterator to end of data.
Definition MiniMatrix.hpp:105
OPM_HOST_DEVICE MiniMatrix operator+(const MiniMatrix &other) const
Add two matrices (element-wise).
Definition MiniMatrix.hpp:156
static OPM_HOST_DEVICE constexpr size_type size()
Get matrix dimension.
Definition MiniMatrix.hpp:138
OPM_HOST_DEVICE MiniMatrix operator*(const MiniMatrix &B) const
Matrix-matrix multiplication: C = A * B.
Definition MiniMatrix.hpp:229
OPM_HOST_DEVICE MiniMatrix()=default
Default constructor.
OPM_HOST_DEVICE auto begin() const
Get const iterator to beginning of data.
Definition MiniMatrix.hpp:110
OPM_HOST_DEVICE const T * operator[](size_type row) const
Access row for further column indexing (const version).
Definition MiniMatrix.hpp:93
OPM_HOST_DEVICE std::array< T, dimension > operator*(const std::array< T, dimension > &x) const
Matrix-vector multiplication: y = A * x.
Definition MiniMatrix.hpp:195
OPM_HOST_DEVICE T * operator[](size_type row)
Access row for further column indexing.
Definition MiniMatrix.hpp:84
OPM_HOST_DEVICE MiniMatrix & operator+=(const MiniMatrix &other)
Add another matrix to this one (element-wise).
Definition MiniMatrix.hpp:145
OPM_HOST_DEVICE MiniMatrix(std::initializer_list< T > init)
Constructor from initializer list.
Definition MiniMatrix.hpp:52
OPM_HOST_DEVICE void fill(const T &value)
Fill all elements with a value.
Definition MiniMatrix.hpp:131
OPM_HOST_DEVICE const T * data() const
Get const pointer to raw data.
Definition MiniMatrix.hpp:125
OPM_HOST_DEVICE T * data()
Get pointer to raw data.
Definition MiniMatrix.hpp:120
OPM_HOST_DEVICE Opm::gpuistl::MiniVector< T, dimension > operator*(const Opm::gpuistl::MiniVector< T, dimension > &x) const
Matrix-vector multiplication: y = A * x, with MiniVector.
Definition MiniMatrix.hpp:241
OPM_HOST_DEVICE MiniMatrix operator-(const MiniMatrix &other) const
Subtract two matrices (element-wise).
Definition MiniMatrix.hpp:178
OPM_HOST_DEVICE auto end() const
Get const iterator to end of data.
Definition MiniMatrix.hpp:115
Definition MiniVector.hpp:50
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUDA...
Definition AmgxInterface.hpp:38