PolyFEM
Loading...
Searching...
No Matches
MatrixCache.cpp
Go to the documentation of this file.
1#include "MatrixCache.hpp"
2
5
6#include <algorithm>
7
8namespace polyfem::utils
9{
11 {
12 init(size);
13 }
14
15 SparseMatrixCache::SparseMatrixCache(const size_t rows, const size_t cols)
16 {
17 init(rows, cols);
18 }
19
21 {
22 init(other);
23 }
24
26 const SparseMatrixCache &other, const bool copy_main_cache_ptr)
27 {
28 init(other, copy_main_cache_ptr);
29 }
30
31 void SparseMatrixCache::init(const size_t size)
32 {
33 assert(mapping().empty() || size_ == size);
34
35 size_ = size;
36 tmp_.resize(size_, size_);
37 mat_.resize(size_, size_);
38 mat_.setZero();
39 }
40
41 void SparseMatrixCache::init(const size_t rows, const size_t cols)
42 {
43 assert(mapping().empty());
44
45 size_ = rows == cols ? rows : 0;
46 tmp_.resize(rows, cols);
47 mat_.resize(rows, cols);
48 mat_.setZero();
49 }
50
52 {
53 assert(this != &other);
54 assert(&other == &dynamic_cast<const SparseMatrixCache &>(other));
55 init(dynamic_cast<const SparseMatrixCache &>(other));
56 }
57
59 const SparseMatrixCache &other, const bool copy_main_cache_ptr)
60 {
61 assert(this != &other);
62 if (copy_main_cache_ptr)
63 {
65 }
66 else if (main_cache_ == nullptr)
67 {
68 main_cache_ = other.main_cache();
69 // Only one level of cache
70 assert(main_cache_ != this && main_cache_ != nullptr && main_cache_->main_cache_ == nullptr);
71 }
72 size_ = other.size_;
73
74 values_.resize(other.values_.size());
75
76 tmp_.resize(other.mat_.rows(), other.mat_.cols());
77 mat_.resize(other.mat_.rows(), other.mat_.cols());
78 mat_.setZero();
79 std::fill(values_.begin(), values_.end(), 0);
80 }
81
83 {
84 tmp_.setZero();
85 mat_.setZero();
86
87 std::fill(values_.begin(), values_.end(), 0);
88 }
89
90 void SparseMatrixCache::add_value(const int e, const int i, const int j, const double value)
91 {
92 // caches have yet to be constructed (likely because the matrix has yet to be fully assembled)
93 if (mapping().empty())
94 {
95 // save entry so it can be added to the matrix later
96 entries_.emplace_back(i, j, value);
97
98 // save the index information so the cache can be built later
99 if (second_cache_entries_.size() <= e)
100 second_cache_entries_.resize(e + 1);
101 second_cache_entries_[e].emplace_back(i, j);
102 }
103 else
104 {
105 if (e != current_e_)
106 {
107 current_e_ = e;
109 }
110
111 // save entry directly to value buffer at the proper index
112 values_[second_cache()[e][current_e_index_]] += value;
114 }
115 }
116
118 {
119 // caches have yet to be constructed (likely because the matrix has yet to be fully assembled)
120 if (mapping().empty())
121 {
122 tmp_.setFromTriplets(entries_.begin(), entries_.end());
123 tmp_.makeCompressed();
124 mat_ += tmp_;
125
126 tmp_.setZero();
127 tmp_.data().squeeze();
128 mat_.makeCompressed();
129
130 entries_.clear();
131
132 mat_.makeCompressed();
133 }
134 }
135
137 {
138 prune();
139
140 // caches have yet to be constructed (likely because the matrix has yet to be fully assembled)
141 if (mapping().empty())
142 {
143 if (compute_mapping && size_ > 0)
144 {
145 assert(main_cache_ == nullptr);
146
147 values_.resize(mat_.nonZeros());
148 inner_index_.resize(mat_.nonZeros());
149 outer_index_.resize(mat_.rows() + 1);
150 mapping_.resize(mat_.rows());
151
152 // note: mat_ is column major
153 const auto inn_ptr = mat_.innerIndexPtr();
154 const auto out_ptr = mat_.outerIndexPtr();
155 inner_index_.assign(inn_ptr, inn_ptr + inner_index_.size());
156 outer_index_.assign(out_ptr, out_ptr + outer_index_.size());
157
158 size_t index = 0;
159 // loop over columns of the matrix
160 for (size_t i = 0; i < mat_.cols(); ++i)
161 {
162 const auto start = outer_index_[i];
163 const auto end = outer_index_[i + 1];
164
165 // loop over the nonzero elements of the given column
166 for (size_t ii = start; ii < end; ++ii)
167 {
168 // pick out current row
169 const auto j = inner_index_[ii];
170 auto &map = mapping_[j];
171 map.emplace_back(i, index);
172 ++index;
173 }
174 }
175
176 logger().trace("Cache computed");
177
178 second_cache_.clear();
180 // loop over each element
181 for (int e = 0; e < second_cache_entries_.size(); ++e)
182 {
183 // loop over each global index affected by the given element
184 for (const auto &p : second_cache_entries_[e])
185 {
186 const int i = p.first;
187 const int j = p.second;
188
189 // pick out column/sparse matrix index pairs for the given column
190 const auto &map = mapping()[i];
191 int index = -1;
192
193 // loop over column/sparse matrix index pairs
194 for (const auto &p : map)
195 {
196 // match columns
197 if (p.first == j)
198 {
199 assert(p.second < values_.size());
200 index = p.second;
201 break;
202 }
203 }
204 assert(index >= 0);
205
206 // save the sparse matrix index used by this element
207 second_cache_[e].emplace_back(index);
208 }
209 }
210
211 second_cache_entries_.resize(0);
212
213 logger().trace("Second cache computed");
214 }
215 }
216 else
217 {
218 assert(size_ > 0);
219 const auto &outer_index = main_cache()->outer_index_;
220 const auto &inner_index = main_cache()->inner_index_;
221 // The cached arrays already describe the compressed matrix. Copy them
222 // directly instead of rebuilding the sparse structure through an evaluator.
223 assert(mat_.isCompressed());
224 assert(mat_.rows() == size_ && mat_.cols() == size_);
225 assert(outer_index.size() == size_ + 1);
226 assert(inner_index.size() == values_.size());
227 mat_.resizeNonZeros(values_.size());
228 std::copy(outer_index.begin(), outer_index.end(), mat_.outerIndexPtr());
229 if (!values_.empty())
230 {
231 std::copy(inner_index.begin(), inner_index.end(), mat_.innerIndexPtr());
232 std::copy(values_.begin(), values_.end(), mat_.valuePtr());
233 }
234
235 current_e_ = -1;
236 current_e_index_ = -1;
237 }
238 std::fill(values_.begin(), values_.end(), 0);
239 return mat_;
240 }
241
242 std::shared_ptr<MatrixCache> SparseMatrixCache::operator+(const MatrixCache &a) const
243 {
244 assert(&a == &dynamic_cast<const SparseMatrixCache &>(a));
245 return *this + dynamic_cast<const SparseMatrixCache &>(a);
246 }
247
248 std::shared_ptr<MatrixCache> SparseMatrixCache::operator+(const SparseMatrixCache &a) const
249 {
250 std::shared_ptr<SparseMatrixCache> out = std::make_shared<SparseMatrixCache>(a);
251
252 if (a.mapping().empty() || mapping().empty())
253 {
254 out->mat_ = a.mat_ + mat_;
255 const size_t this_e_size = second_cache_entries_.size();
256 const size_t a_e_size = a.second_cache_entries_.size();
257
258 out->second_cache_entries_.resize(std::max(this_e_size, a_e_size));
259 for (int e = 0; e < std::min(this_e_size, a_e_size); ++e)
260 {
261 assert(second_cache_entries_[e].size() == 0 || a.second_cache_entries_[e].size() == 0);
262 out->second_cache_entries_[e].insert(out->second_cache_entries_[e].end(), second_cache_entries_[e].begin(), second_cache_entries_[e].end());
263 out->second_cache_entries_[e].insert(out->second_cache_entries_[e].end(), a.second_cache_entries_[e].begin(), a.second_cache_entries_[e].end());
264 }
265
266 for (int e = std::min(this_e_size, a_e_size); e < std::max(this_e_size, a_e_size); ++e)
267 {
268 if (second_cache_entries_.size() < e)
269 out->second_cache_entries_[e].insert(out->second_cache_entries_[e].end(), second_cache_entries_[e].begin(), second_cache_entries_[e].end());
270 else
271 out->second_cache_entries_[e].insert(out->second_cache_entries_[e].end(), a.second_cache_entries_[e].begin(), a.second_cache_entries_[e].end());
272 }
273 }
274 else
275 {
276 const auto &outer_index = main_cache()->outer_index_;
277 const auto &inner_index = main_cache()->inner_index_;
278 const auto &aouter_index = a.main_cache()->outer_index_;
279 const auto &ainner_index = a.main_cache()->inner_index_;
280 assert(ainner_index.size() == inner_index.size());
281 assert(aouter_index.size() == outer_index.size());
282 assert(a.values_.size() == values_.size());
283
284 maybe_parallel_for(a.values_.size(), [&](int start, int end, int thread_id) {
285 for (int i = start; i < end; ++i)
286 {
287 out->values_[i] = a.values_[i] + values_[i];
288 }
289 });
290 }
291
292 return out;
293 }
294
296 {
297 assert(&o == &dynamic_cast<const SparseMatrixCache &>(o));
298 *this += dynamic_cast<const SparseMatrixCache &>(o);
299 }
300
302 {
303 if (mapping().empty() || o.mapping().empty())
304 {
305 mat_ += o.mat_;
306
307 const size_t this_e_size = second_cache_entries_.size();
308 const size_t o_e_size = o.second_cache_entries_.size();
309
310 second_cache_entries_.resize(std::max(this_e_size, o_e_size));
311 for (int e = 0; e < o_e_size; ++e)
312 {
313 assert(second_cache_entries_[e].size() == 0 || o.second_cache_entries_[e].size() == 0);
315 }
316 }
317 else
318 {
319 const auto &outer_index = main_cache()->outer_index_;
320 const auto &inner_index = main_cache()->inner_index_;
321 const auto &oouter_index = o.main_cache()->outer_index_;
322 const auto &oinner_index = o.main_cache()->inner_index_;
323 assert(inner_index.size() == oinner_index.size());
324 assert(outer_index.size() == oouter_index.size());
325 assert(values_.size() == o.values_.size());
326
327 maybe_parallel_for(o.values_.size(), [&](int start, int end, int thread_id) {
328 for (int i = start; i < end; ++i)
329 {
330 values_[i] += o.values_[i];
331 }
332 });
333 }
334 }
335
336 // ========================================================================
337
339 {
340 mat_.setZero(size, size);
341 }
342
343 DenseMatrixCache::DenseMatrixCache(const size_t rows, const size_t cols)
344 {
345 mat_.setZero(rows, cols);
346 }
347
349 {
350 init(other);
351 }
352
354 {
355 init(other);
356 }
357
358 void DenseMatrixCache::init(const size_t size)
359 {
360 mat_.setZero(size, size);
361 }
362
363 void DenseMatrixCache::init(const size_t rows, const size_t cols)
364 {
365 mat_.setZero(rows, cols);
366 }
367
369 {
370 init(dynamic_cast<const DenseMatrixCache &>(other));
371 }
372
374 {
375 mat_.setZero(other.mat_.rows(), other.mat_.cols());
376 }
377
379 {
380 mat_.setZero();
381 }
382
383 void DenseMatrixCache::add_value(const int e, const int i, const int j, const double value)
384 {
385 mat_(i, j) += value;
386 }
387
389
391 {
392 return mat_.sparseView();
393 }
394
395 std::shared_ptr<MatrixCache> DenseMatrixCache::operator+(const MatrixCache &a) const
396 {
397 return *this + dynamic_cast<const DenseMatrixCache &>(a);
398 }
399
400 std::shared_ptr<MatrixCache> DenseMatrixCache::operator+(const DenseMatrixCache &a) const
401 {
402 std::shared_ptr<DenseMatrixCache> out = std::make_shared<DenseMatrixCache>(a);
403 out->mat_ += mat_;
404 return out;
405 }
406
408 {
409 *this += dynamic_cast<const DenseMatrixCache &>(o);
410 }
411
413 {
414 mat_ += o.mat_;
415 }
416
417} // namespace polyfem::utils
std::shared_ptr< MatrixCache > operator+(const MatrixCache &a) const override
void init(const size_t size) override
StiffnessMatrix get_matrix(const bool compute_mapping=true) override
void add_value(const int e, const int i, const int j, const double value) override
void operator+=(const MatrixCache &o) override
abstract class used for caching
void operator+=(const MatrixCache &o) override
void add_value(const int e, const int i, const int j, const double value) override
e = element_index, i = global row_index, j = global column_index, value = value to add to matrix if t...
void set_zero() override
set matrix values to zero modifies tmp_, mat_, and values (setting all to zero)
std::vector< int > outer_index_
saves inner/outer indices for sparse matrix
void prune() override
if caches have yet to be constructed, add the saved triplets to mat_ modifies tmp_ and mat_,...
const SparseMatrixCache * main_cache_
std::shared_ptr< MatrixCache > operator+(const MatrixCache &a) const override
const std::vector< std::vector< std::pair< int, size_t > > > & mapping() const
std::vector< std::vector< std::pair< int, int > > > second_cache_entries_
maps element indices to global matrix indices
const SparseMatrixCache * main_cache() const
std::vector< double > values_
buffer for values (corresponds to inner/outer_index_ structure for sparse matrix)
const std::vector< std::vector< int > > & second_cache() const
std::vector< Eigen::Triplet< double > > entries_
contains global matrix indices and corresponding value
StiffnessMatrix get_matrix(const bool compute_mapping=true) override
if the cache is yet to be constructed, save the cached (ordered) indices in inner_index_ and outer_in...
void init(const size_t size) override
set matrix to be size x size
std::vector< std::vector< int > > second_cache_
maps element index to local index
std::vector< std::vector< std::pair< int, size_t > > > mapping_
maps row indices to column index/local index pairs
void maybe_parallel_for(int size, const std::function< void(int, int, int)> &partial_for)
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:24