-
Notifications
You must be signed in to change notification settings - Fork 299
/
SparseMatrixProduct.inl
316 lines (265 loc) · 10.1 KB
/
SparseMatrixProduct.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once
#include <sofa/linearalgebra/SparseMatrixProduct.h>
#include <Eigen/Sparse>
#include <sofa/type/vector.h>
namespace sofa::linearalgebra::sparsematrixproduct
{
/**
* Represent a scalar and its index in an array of scalars
*/
template<class Scalar>
struct IndexedValue
{
Eigen::Index index {};
Scalar value;
IndexedValue() = default;
template<class AnyScalar, typename = std::enable_if_t<std::is_scalar_v<AnyScalar> > >
IndexedValue(AnyScalar s) : value(s) {}
IndexedValue(const IndexedValue& other) = default;
operator Scalar() const
{
return value;
}
};
template<class Scalar>
std::ostream& operator<<(std::ostream& o, IndexedValue<Scalar>& p)
{
o << "(" << p.value << ", " << p.index << ")";
return o;
}
/**
* Represent a sum of scalar products. It stores:
* - a value for the result
* - a list of pairs of indices to know what scalars were used for the computation
*/
template<class Scalar>
class IndexValueProduct
{
private:
using IndexLHS = Eigen::Index;
using IndexRHS = Eigen::Index;
using ScalarProduct = std::pair<IndexLHS, IndexRHS>;
sofa::type::vector<ScalarProduct> m_indices {};
Scalar value {};
public:
[[nodiscard]] const sofa::type::vector<ScalarProduct>& getIndices() const
{
return m_indices;
}
IndexValueProduct() = default;
template<class AnyScalar, typename = std::enable_if_t<std::is_scalar_v<AnyScalar> > >
IndexValueProduct(AnyScalar s) : value(s) {}
operator Scalar() const
{
return value;
}
template<class AnyScalar>
IndexValueProduct(const IndexValueProduct<AnyScalar>& other)
: m_indices(other.indices)
, value(static_cast<Scalar>(other.value))
{}
template<class AnyScalar>
void operator+=(const IndexValueProduct<AnyScalar>& other)
{
m_indices.insert(m_indices.end(), other.m_indices.begin(), other.m_indices.end());
value += static_cast<Scalar>(other.value);
}
template<class ScalarLhs, class ScalarRhs>
friend IndexValueProduct<decltype(ScalarLhs{} * ScalarRhs{})>
operator*(const IndexedValue<ScalarLhs>& lhs, const IndexedValue<ScalarRhs>& rhs);
};
template<class Scalar>
std::ostream& operator<<(std::ostream& o, IndexValueProduct<Scalar>& p)
{
o << "(" << p.value << ", [" << p.indices << "])";
return o;
}
template<class ScalarLhs, class ScalarRhs>
IndexValueProduct<decltype(ScalarLhs{} * ScalarRhs{})>
operator*(const IndexedValue<ScalarLhs>& lhs, const IndexedValue<ScalarRhs>& rhs)
{
IndexValueProduct<decltype(ScalarLhs{} * ScalarRhs{})> product;
product.m_indices.resize(1, {lhs.index, rhs.index});
product.value = lhs.value * rhs.value;
return product;
}
}
//this is to inform Eigen that the product of two IndexedValue is a IndexValueProduct
#define DEFINE_PRODUCT_OP_FOR_TYPES(lhs, rhs) \
template<> \
struct Eigen::ScalarBinaryOpTraits< \
sofa::linearalgebra::sparsematrixproduct::IndexedValue<lhs>, \
sofa::linearalgebra::sparsematrixproduct::IndexedValue<rhs>, \
Eigen::internal::scalar_product_op< \
sofa::linearalgebra::sparsematrixproduct::IndexedValue<lhs>, \
sofa::linearalgebra::sparsematrixproduct::IndexedValue<rhs> \
> \
> \
{ \
typedef sofa::linearalgebra::sparsematrixproduct::IndexValueProduct<decltype(lhs{} * rhs{})> ReturnType; \
};
DEFINE_PRODUCT_OP_FOR_TYPES(float, float)
DEFINE_PRODUCT_OP_FOR_TYPES(double, float)
DEFINE_PRODUCT_OP_FOR_TYPES(float, double)
DEFINE_PRODUCT_OP_FOR_TYPES(double, double)
namespace sofa::linearalgebra
{
template<class Lhs, class Rhs, class ResultType>
void SparseMatrixProduct<Lhs, Rhs, ResultType>::computeProduct(bool forceComputeIntersection)
{
if (forceComputeIntersection)
{
m_hasComputedIntersection = false;
}
if (m_hasComputedIntersection == false)
{
computeIntersection();
m_hasComputedIntersection = true;
}
else
{
computeProductFromIntersection();
}
}
template <class Lhs, class Rhs, class ResultType>
void SparseMatrixProduct<Lhs, Rhs, ResultType>::computeRegularProduct()
{
m_productResult = *m_lhs * *m_rhs;
}
template <typename _Scalar, int _Options, typename _StorageIndex>
void flagValueIndices(Eigen::SparseMatrix<sparsematrixproduct::IndexedValue<_Scalar>, _Options, _StorageIndex>& matrix)
{
for (Eigen::Index i = 0; i < matrix.nonZeros(); ++i)
{
matrix.valuePtr()[i].index = i;
}
}
template<class T>
struct EigenOptions
{
static constexpr auto value = T::Options;
};
template<class T>
static constexpr auto EigenOptions_v = EigenOptions<T>::value;
template<class T, int Options, typename StrideType>
struct EigenOptions<Eigen::Map<T, Options, StrideType>>
{
static constexpr auto value = EigenOptions_v<T>;
};
template<class T, int Options, typename StrideType>
struct EigenOptions<const Eigen::Map<T, Options, StrideType>>
{
static constexpr auto value = EigenOptions_v<T>;
};
template<class T>
struct EigenOptions<Eigen::Transpose<T>>
{
static constexpr auto value = (EigenOptions_v<T> == Eigen::RowMajor) ? Eigen::ColMajor : Eigen::RowMajor;
};
template<class T>
struct EigenOptions<const Eigen::Transpose<T>>
{
static constexpr auto value = (EigenOptions_v<T> == Eigen::RowMajor) ? Eigen::ColMajor : Eigen::RowMajor;
};
template<class Lhs, class Rhs, class ResultType>
void SparseMatrixProduct<Lhs, Rhs, ResultType>::computeIntersection()
{
using LocalLhs = Eigen::SparseMatrix<
sparsematrixproduct::IndexedValue<LhsScalar>,
EigenOptions_v<Lhs>,
typename Lhs::StorageIndex
>;
using LocalRhs = Eigen::SparseMatrix<
sparsematrixproduct::IndexedValue<RhsScalar>,
EigenOptions_v<Rhs>,
typename Rhs::StorageIndex
>;
//copy the input matrices in an intermediate matrix with the same properties
//except that the type of values is IndexedValue
LocalLhs lhs = m_lhs->template cast<sparsematrixproduct::IndexedValue<LhsScalar>>();
LocalRhs rhs = m_rhs->template cast<sparsematrixproduct::IndexedValue<RhsScalar>>();
flagValueIndices(lhs);
flagValueIndices(rhs);
using LocalResult = Eigen::SparseMatrix<
sparsematrixproduct::IndexValueProduct<decltype(LhsScalar{} * RhsScalar{})>,
ResultType::Options,
typename ResultType::StorageIndex
>;
const LocalResult product = lhs * rhs;
const auto productNonZeros = product.nonZeros();
m_intersectionAB.intersection.clear();
m_intersectionAB.intersection.reserve(productNonZeros);
for (Eigen::Index i = 0; i < productNonZeros; ++i)
{
m_intersectionAB.intersection.push_back(product.valuePtr()[i].getIndices());
//depending on the storage scheme, Eigen can change the order of the lhs and rhs
//Note: the condition has been determined empirically, using unit tests
//testing all possible combinations = 2^3 = 8
if constexpr ((Lhs::IsRowMajor && Rhs::IsRowMajor && ResultType::IsRowMajor)
|| ((Lhs::IsRowMajor || Rhs::IsRowMajor) && !ResultType::IsRowMajor))
{
for (auto& [lhsIndex, rhsIndex] : m_intersectionAB.intersection.back())
{
std::swap(lhsIndex, rhsIndex);
}
}
#if !defined(NDEBUG)
const auto lhsNonZeros = m_lhs->nonZeros();
const auto rhsNonZeros = m_rhs->nonZeros();
for (const auto& [lhsIndex, rhsIndex] : m_intersectionAB.intersection.back())
{
assert(lhsIndex < lhsNonZeros);
assert(rhsIndex < rhsNonZeros);
}
#endif
}
m_productResult = product.template cast<ResultScalar>();
}
template<class Lhs, class Rhs, class ResultType>
void SparseMatrixProduct<Lhs, Rhs, ResultType>::computeProductFromIntersection()
{
assert(m_intersectionAB.intersection.size() == m_productResult.nonZeros());
auto* lhs_ptr = m_lhs->valuePtr();
auto* rhs_ptr = m_rhs->valuePtr();
auto* product_ptr = m_productResult.valuePtr();
const auto lhsNonZeros = m_lhs->nonZeros();
const auto rhsNonZeros = m_rhs->nonZeros();
for (const auto& pairs : m_intersectionAB.intersection)
{
auto& value = *product_ptr++;
value = 0;
for (const auto& [lhsIndex, rhsIndex] : pairs)
{
assert(lhsIndex < lhsNonZeros);
assert(rhsIndex < rhsNonZeros);
value += lhs_ptr[lhsIndex] * rhs_ptr[rhsIndex];
}
}
}
template<class Lhs, class Rhs, class ResultType>
void SparseMatrixProduct<Lhs, Rhs, ResultType>::invalidateIntersection()
{
m_hasComputedIntersection = false;
}
}