PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Matrix.cuh
Go to the documentation of this file.
1
10
11#ifndef PBAT_GPU_IMPL_MATH_MATRIX_CUH
12#define PBAT_GPU_IMPL_MATH_MATRIX_CUH
13
14#include "pbat/gpu/impl/common/Buffer.cuh"
15
16#include <concepts>
17#include <cublas_v2.h>
18#include <exception>
19#include <type_traits>
20
21namespace pbat::gpu::impl::math {
22
23template <class TMatrix>
24concept CMatrix = requires(TMatrix a)
25{
26 requires std::is_same_v<typename TMatrix::ValueType, float> or
27 std::is_same_v<typename TMatrix::ValueType, double>;
28 {a.Raw()}->std::same_as<typename TMatrix::ValueType*>;
29 {a.Rows()}->std::convertible_to<int>;
30 {a.Cols()}->std::convertible_to<int>;
31 {a.LeadingDimensions()}->std::convertible_to<int>;
32 {a.Operation()}->std::convertible_to<cublasOperation_t>;
33 {a.Transposed()};
34};
35
36template <class TVector>
37concept CVector = requires(TVector a)
38{
39 requires std::is_same_v<typename TVector::ValueType, float> or
40 std::is_same_v<typename TVector::ValueType, double>;
41 {a.Raw()}->std::same_as<typename TVector::ValueType*>;
42 {a.Rows()}->std::convertible_to<int>;
43 {a.Increment()}->std::convertible_to<int>;
44};
45
46template <class T>
48{
49 using ValueType = std::remove_cvref_t<T>;
50
51 ValueType* data;
52 int n;
53 int inc;
54
55 // Storage information
56 ValueType* Raw() { return data; }
57 ValueType const* Raw() const { return data; }
58 auto Rows() const { return n; }
59 constexpr auto Cols() const { return 1; }
60 auto Increment() const { return inc; }
61
62 // Accessors
63 VectorView<ValueType> Slice(auto row, auto rows, auto inc) const
64 {
65 return VectorView<ValueType>{const_cast<ValueType*>(data) + row, rows, inc};
66 }
67 VectorView<ValueType> Segment(auto row, auto rows) const { return Slice(row, rows, 1); }
68 VectorView<ValueType> Head(auto rows) const { return Slice(0, rows, 1); }
69 VectorView<ValueType> Tail(auto rows) const { return Slice(n - rows, rows, 1); }
70};
71
72template <class T>
73struct MatrixView
74{
75 using ValueType = std::remove_cvref_t<T>;
76
77 ValueType* data;
78 int m;
79 int n;
80 int ld;
81 cublasOperation_t op;
82
83 MatrixView(
84 ValueType* dataIn,
85 int mIn,
86 int nIn,
87 int ldIn,
88 cublasOperation_t opIn = cublasOperation_t::CUBLAS_OP_N)
89 : data(dataIn), m(mIn), n(nIn), ld(ldIn), op(opIn)
90 {
91 if (ld < m)
92 {
93 throw std::invalid_argument(
94 "MatrixView::MatrixView(ValueType* data, int m, int n, int ld) -> ld < m");
95 }
96 }
97
98 template <CVector TVector>
99 MatrixView(TVector& v) : data(v.Raw()), m(v.Rows()), n(1), ld(v.Rows()), op(CUBLAS_OP_N)
100 {
101 if (v.Increment() != 1)
102 {
103 throw std::invalid_argument(
104 "MatrixView::MatrixView(TVector const& v) -> v.Increment() must be 1");
105 }
106 }
107
108 // Storage information
109 ValueType* Raw() { return data; }
110 ValueType const* Raw() const { return data; }
111 auto Rows() const { return m; }
112 auto Cols() const { return n; }
113 auto LeadingDimensions() const { return ld; }
114 auto Operation() const { return op; }
115
116 // Accessors
117 MatrixView<ValueType> Transposed() const
118 {
119 return MatrixView<ValueType>{
120 const_cast<ValueType*>(data),
121 m,
122 n,
123 ld,
124 op == CUBLAS_OP_N ? CUBLAS_OP_T : CUBLAS_OP_N};
125 }
126 MatrixView<ValueType> SubMatrix(auto row, auto col, auto rows, auto cols) const
127 {
128 return MatrixView<ValueType>{const_cast<ValueType*>(data) + ld * col + row, rows, cols, ld};
129 }
130 MatrixView<ValueType> LeftCols(auto cols) const { return SubMatrix(0, 0, m, cols); }
131 MatrixView<ValueType> RightCols(auto cols) const
132 {
133 return SubMatrix(0, Cols() - cols, m, cols);
134 }
135 MatrixView<ValueType> TopRows(auto rows) const { return SubMatrix(0, 0, rows, Cols()); }
136 MatrixView<ValueType> BottomRows(auto rows) const
137 {
138 return SubMatrix(Rows() - rows, 0, rows, Cols());
139 }
140 MatrixView<ValueType> Col(auto col) const { return SubMatrix(0, col, Rows(), Cols()); }
141 MatrixView<ValueType> Row(auto row) const { return SubMatrix(row, 0, 1, Cols()); }
142 VectorView<ValueType> Flattened() const
143 {
144 return VectorView<ValueType>{const_cast<ValueType*>(data), Rows() * Cols(), 1};
145 }
146};
147
148template <class T>
149struct Matrix
150{
151 using ValueType = std::remove_cvref_t<T>;
152
153 Matrix() = default;
154 Matrix(auto rows, auto cols) : data(rows * cols), m(static_cast<int>(rows)) {}
155
157 int m;
158
159 // Storage information
160 ValueType* Raw() { return data.Raw(); }
161 ValueType const* Raw() const { return data.Raw(); }
162 auto Rows() const { return m; }
163 auto Cols() const { return static_cast<int>(data.Size()) / m; }
164 auto LeadingDimensions() const { return m; }
165 auto Operation() const { return CUBLAS_OP_N; }
166
167 // Accessors
168 MatrixView<ValueType> View() const
169 {
170 return MatrixView<ValueType>{const_cast<ValueType*>(data.Raw()), m, Cols(), m};
171 }
172 MatrixView<ValueType> SubMatrix(auto row, auto col, auto rows, auto cols) const
173 {
174 ValueType* a = const_cast<ValueType*>(data.Raw());
175 return MatrixView<ValueType>{a + m * col + row, rows, cols, m};
176 }
177 MatrixView<ValueType> LeftCols(auto cols) const { return SubMatrix(0, 0, m, cols); }
178 MatrixView<ValueType> RightCols(auto cols) const
179 {
180 return SubMatrix(0, Cols() - cols, m, cols);
181 }
182 MatrixView<ValueType> TopRows(auto rows) const { return SubMatrix(0, 0, rows, Cols()); }
183 MatrixView<ValueType> BottomRows(auto rows) const
184 {
185 return SubMatrix(Rows() - rows, 0, rows, Cols());
186 }
187 MatrixView<ValueType> Col(auto col) const { return SubMatrix(0, col, Rows(), Cols()); }
188 MatrixView<ValueType> Row(auto row) const { return SubMatrix(row, 0, 1, Cols()); }
189 VectorView<ValueType> Flattened() const
190 {
191 return VectorView<ValueType>{const_cast<ValueType*>(data.Raw()), Rows() * Cols(), 1};
192 }
193 MatrixView<ValueType> Transposed() const
194 {
195 return MatrixView<ValueType>{const_cast<ValueType*>(data.Raw()), m, Cols(), m, CUBLAS_OP_T};
196 }
197};
198
199template <class T>
200struct Vector
201{
202 using ValueType = std::remove_cvref_t<T>;
203
204 Vector() = default;
205 Vector(auto rows) : data(rows), n(static_cast<int>(rows)) {}
206
208 int n;
209
210 // Storage information
211 ValueType* Raw() { return data.Raw(); }
212 ValueType const* Raw() const { return data.Raw(); }
213 auto Rows() const { return n; }
214 constexpr auto Cols() const { return 1; }
215 constexpr auto Increment() const { return 1; }
216
217 // Accessors
218 VectorView<ValueType> Slice(auto row, auto rows, auto inc) const
219 {
220 ValueType* a = const_cast<ValueType*>(data.Raw());
221 return VectorView<ValueType>{a + row, rows, inc};
222 }
223 VectorView<ValueType> Segment(auto row, auto rows) const { return Slice(row, rows, 1); }
224 VectorView<ValueType> Head(auto rows) const { return Slice(0, rows, 1); }
225 VectorView<ValueType> Tail(auto rows) const { return Slice(n - rows, rows, 1); }
226};
227
228}; // namespace pbat::gpu::impl::math
229
230#endif // PBAT_GPU_IMPL_MATH_MATRIX_CUH
Definition Buffer.cuh:21
Definition Matrix.cuh:24
Definition Matrix.cuh:37
auto Slice(R &&r)
Slice view over a range for Eigen advanced indexing.
Definition Eigen.h:86
GPU implementations of math functions.
Definition Blas.cu:7
common::Buffer< ValueType > data
m x n dense matrix coefficients in column-major order
Definition Matrix.cuh:156
int m
Number of rows.
Definition Matrix.cuh:157
Definition Matrix.cuh:74
int ld
Leading dimension.
Definition Matrix.cuh:80
ValueType * data
Pointer to the matrix coefficients.
Definition Matrix.cuh:77
int n
Number of columns.
Definition Matrix.cuh:79
int m
Number of rows.
Definition Matrix.cuh:78
cublasOperation_t op
CUBLAS operation type.
Definition Matrix.cuh:81
int n
Number of rows.
Definition Matrix.cuh:208
common::Buffer< ValueType > data
n x 1 dense vector coefficients
Definition Matrix.cuh:207
Definition Matrix.cuh:48
int inc
Increment.
Definition Matrix.cuh:53
int n
Number of rows.
Definition Matrix.cuh:52
ValueType * data
Pointer to the vector coefficients.
Definition Matrix.cuh:51