Debian Codegraph @

include/deal.II/lac/chunk_sparse_matrix.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2008 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_chunk_sparse_matrix_templates_h
16 #define dealii_chunk_sparse_matrix_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/parallel.h>
22 #include <deal.II/base/template_constraints.h>
23
24 #include <deal.II/lac/chunk_sparse_matrix.h>
25 #include <deal.II/lac/full_matrix.h>
26 #include <deal.II/lac/vector.h>
27
28 #include <algorithm>
29 #include <cmath>
30 #include <functional>
31 #include <iomanip>
32 #include <numeric>
33 #include <ostream>
34 #include <vector>
35
36 DEAL_II_NAMESPACE_OPEN
37
38
39 namespace internal
40 {
41 // TODO: the goal of the ChunkSparseMatrix class is to stream data and use
42 // the vectorization features of modern processors. to make this happen,
43 // we will have to vectorize the functions in the following namespace, either
44 // by hand or by using, for example, optimized BLAS versions for them.
45 namespace ChunkSparseMatrixImplementation
46 {
47 /**
48 * Declare type for container size.
49 */
50 using size_type = types::global_dof_index;
51
52 /**
53 * Add the result of multiplying a chunk of size chunk_size times
54 * chunk_size by a source vector fragment of size chunk_size to the
55 * destination vector fragment.
56 */
57 template <typename MatrixIterator,
58 typename SrcIterator,
59 typename DstIterator>
60 inline void
61 chunk_vmult_add(const size_type chunk_size,
62 const MatrixIterator matrix,
63 const SrcIterator src,
64 DstIterator dst)
65 {
66 MatrixIterator matrix_row = matrix;
67
68 for (size_type i = 0; i < chunk_size; ++i, matrix_row += chunk_size)
69 {
70 typename std::iterator_traits<DstIterator>::value_type sum = 0;
71
72 for (size_type j = 0; j < chunk_size; ++j)
73 sum += matrix_row[j] * src[j];
74
75 dst[i] += sum;
76 }
77 }
78
79
80
81 /**
82 * Like the previous function, but subtract. We need this for computing
83 * the residual.
84 */
85 template <typename MatrixIterator,
86 typename SrcIterator,
87 typename DstIterator>
88 inline void
89 chunk_vmult_subtract(const size_type chunk_size,
90 const MatrixIterator matrix,
91 const SrcIterator src,
92 DstIterator dst)
93 {
94 MatrixIterator matrix_row = matrix;
95
96 for (size_type i = 0; i < chunk_size; ++i, matrix_row += chunk_size)
97 {
98 typename std::iterator_traits<DstIterator>::value_type sum = 0;
99
100 for (size_type j = 0; j < chunk_size; ++j)
101 sum += matrix_row[j] * src[j];
102
103 dst[i] -= sum;
104 }
105 }
106
107
108 /**
109 * Add the result of multiplying the transpose of a chunk of size
110 * chunk_size times chunk_size by a source vector fragment of size
111 * chunk_size to the destination vector fragment.
112 */
113 template <typename MatrixIterator,
114 typename SrcIterator,
115 typename DstIterator>
116 inline void
117 chunk_Tvmult_add(const size_type chunk_size,
118 const MatrixIterator matrix,
119 const SrcIterator src,
120 DstIterator dst)
121 {
122 for (size_type i = 0; i < chunk_size; ++i)
123 {
124 typename std::iterator_traits<DstIterator>::value_type sum = 0;
125
126 for (size_type j = 0; j < chunk_size; ++j)
127 sum += matrix[j * chunk_size + i] * src[j];
128
129 dst[i] += sum;
130 }
131 }
132
133
134 /**
135 * Produce the result of the matrix scalar product $u^TMv$ for an
136 * individual chunk.
137 */
138 template <typename result_type,
139 typename MatrixIterator,
140 typename SrcIterator1,
141 typename SrcIterator2>
142 inline result_type
143 chunk_matrix_scalar_product(const size_type chunk_size,
144 const MatrixIterator matrix,
145 const SrcIterator1 u,
146 const SrcIterator2 v)
147 {
148 result_type result = 0;
149
150 MatrixIterator matrix_row = matrix;
151
152 for (size_type i = 0; i < chunk_size; ++i, matrix_row += chunk_size)
153 {
154 typename std::iterator_traits<SrcIterator2>::value_type sum = 0;
155
156 for (size_type j = 0; j < chunk_size; ++j)
157 sum += matrix_row[j] * v[j];
158
159 result += u[i] * sum;
160 }
161
162 return result;
163 }
164
165
166
167 /**
168 * Perform a vmult_add using the ChunkSparseMatrix data structures, but
169 * only using a subinterval of the matrix rows.
170 *
171 * In the sequential case, this function is called on all rows, in the
172 * parallel case it may be called on a subrange, at the discretion of the
173 * task scheduler.
174 */
175 template <typename number, typename InVector, typename OutVector>
176 void
177 vmult_add_on_subrange(const ChunkSparsityPattern &cols,
178 const unsigned int begin_row,
179 const unsigned int end_row,
180 const number *values,
181 const std::size_t *rowstart,
182 const size_type *colnums,
183 const InVector &src,
184 OutVector &dst)
185 {
186 const size_type m = cols.n_rows();
187 const size_type n = cols.n_cols();
188 const size_type chunk_size = cols.get_chunk_size();
189
190 // loop over all chunks. note that we need to treat the last chunk row
191 // and column differently if they have padding elements
192 const size_type n_filled_last_rows = m % chunk_size;
193 const size_type n_filled_last_cols = n % chunk_size;
194
195 const size_type last_regular_row =
196 n_filled_last_rows > 0 ?
197 std::min(m / chunk_size, static_cast<size_type>(end_row)) :
198 end_row;
199 const size_type irregular_col = n / chunk_size;
200
201 typename OutVector::iterator dst_ptr =
202 dst.begin() + chunk_size * begin_row;
203 const number *val_ptr =
204 &values[rowstart[begin_row] * chunk_size * chunk_size];
205 const size_type *colnum_ptr = &colnums[rowstart[begin_row]];
206 for (unsigned int chunk_row = begin_row; chunk_row < last_regular_row;
207 ++chunk_row)
208 {
209 const number *const val_end_of_row =
210 &values[rowstart[chunk_row + 1] * chunk_size * chunk_size];
211 while (val_ptr != val_end_of_row)
212 {
213 if (*colnum_ptr != irregular_col)
214 chunk_vmult_add(chunk_size,
215 val_ptr,
216 src.begin() + *colnum_ptr * chunk_size,
217 dst_ptr);
218 else
219 // we're at a chunk column that has padding
220 for (size_type r = 0; r < chunk_size; ++r)
221 for (size_type c = 0; c < n_filled_last_cols; ++c)
222 dst_ptr[r] += (val_ptr[r * chunk_size + c] *
223 src(*colnum_ptr * chunk_size + c));
224
225 ++colnum_ptr;
226 val_ptr += chunk_size * chunk_size;
227 }
228
229 dst_ptr += chunk_size;
230 }
231
232 // now deal with last chunk row if necessary
233 if (n_filled_last_rows > 0 && end_row == (m / chunk_size + 1))
234 {
235 const size_type chunk_row = last_regular_row;
236
237 const number *const val_end_of_row =
238 &values[rowstart[chunk_row + 1] * chunk_size * chunk_size];
239 while (val_ptr != val_end_of_row)
240 {
241 if (*colnum_ptr != irregular_col)
242 {
243 // we're at a chunk row but not column that has padding
244 for (size_type r = 0; r < n_filled_last_rows; ++r)
245 for (size_type c = 0; c < chunk_size; ++c)
246 dst_ptr[r] += (val_ptr[r * chunk_size + c] *
247 src(*colnum_ptr * chunk_size + c));
248 }
249 else
250 // we're at a chunk row and column that has padding
251 for (size_type r = 0; r < n_filled_last_rows; ++r)
252 for (size_type c = 0; c < n_filled_last_cols; ++c)
253 dst_ptr[r] += (val_ptr[r * chunk_size + c] *
254 src(*colnum_ptr * chunk_size + c));
255
256 ++colnum_ptr;
257 val_ptr += chunk_size * chunk_size;
258 }
259 }
260 Assert(std::size_t(colnum_ptr - colnums) == rowstart[end_row],
261 ExcInternalError());
262 Assert(std::size_t(val_ptr - values) ==
263 rowstart[end_row] * chunk_size * chunk_size,
264 ExcInternalError());
265 }
266 } // namespace ChunkSparseMatrixImplementation
267 } // namespace internal
268
269
270
271 template <typename number>
272 ChunkSparseMatrix<number>::ChunkSparseMatrix()
273 : cols(nullptr, "ChunkSparseMatrix")
274 , val(nullptr)
275 , max_len(0)
276 {}
277
278
279
280 template <typename number>
281 ChunkSparseMatrix<number>::ChunkSparseMatrix(const ChunkSparseMatrix &m)
282 : EnableObserverPointer(m)
283 , cols(nullptr, "ChunkSparseMatrix")
284 , val(nullptr)
285 , max_len(0)
286 {
287 Assert(m.cols == nullptr && m.val == nullptr && m.max_len == 0,
288 ExcMessage(
289 "This constructor can only be called if the provided argument "
290 "is an empty matrix. This constructor can not be used to "
291 "copy-construct a non-empty matrix. Use the "
292 "ChunkSparseMatrix::copy_from() function for that purpose."));
293 }
294
295
296
297 template <typename number>
298 ChunkSparseMatrix<number> &
299 ChunkSparseMatrix<number>::operator=(const ChunkSparseMatrix<number> &m)
300 {
301 Assert(m.cols == nullptr && m.val == nullptr && m.max_len == 0,
302 ExcMessage(
303 "This operator can only be called if the provided right "
304 "hand side is an empty matrix. This operator can not be "
305 "used to copy a non-empty matrix. Use the "
306 "ChunkSparseMatrix::copy_from() function for that purpose."));
307
308 return *this;
309 }
310
311
312
313 template <typename number>
314 ChunkSparseMatrix<number>::ChunkSparseMatrix(const ChunkSparsityPattern &c)
315 : cols(nullptr, "ChunkSparseMatrix")
316 , val(nullptr)
317 , max_len(0)
318 {
319 // virtual functions called in constructors and destructors never use the
320 // override in a derived class
321 // for clarity be explicit on which function is called
322 ChunkSparseMatrix<number>::reinit(c);
323 }
324
325
326
327 template <typename number>
328 ChunkSparseMatrix<number>::ChunkSparseMatrix(const ChunkSparsityPattern &c,
329 const IdentityMatrix &id)
330 : cols(nullptr, "ChunkSparseMatrix")
331 , val(nullptr)
332 , max_len(0)
333 {
334 Assert(c.n_rows() == id.m(), ExcDimensionMismatch(c.n_rows(), id.m()));
335 Assert(c.n_cols() == id.n(), ExcDimensionMismatch(c.n_cols(), id.n()));
336
337 // virtual functions called in constructors and destructors never use the
338 // override in a derived class
339 // for clarity be explicit on which function is called
340 ChunkSparseMatrix<number>::reinit(c);
341 for (size_type i = 0; i < n(); ++i)
342 this->set(i, i, 1.);
343 }
344
345
346
347 template <typename number>
348 ChunkSparseMatrix<number>::~ChunkSparseMatrix()
349 {
350 cols = nullptr;
351 }
352
353
354
355 namespace internal
356 {
357 namespace ChunkSparseMatrixImplementation
358 {
359 template <typename T>
360 void
361 zero_subrange(const unsigned int begin, const unsigned int end, T *dst)
362 {
363 std::memset(dst + begin, 0, (end - begin) * sizeof(T));
364 }
365 } // namespace ChunkSparseMatrixImplementation
366 } // namespace internal
367
368
369
370 template <typename number>
371 ChunkSparseMatrix<number> &
372 ChunkSparseMatrix<number>::operator=(const double d)
373 {
374 Assert(d == 0, ExcScalarAssignmentOnlyForZeroValue());
375
376 Assert(cols != nullptr, ExcNeedsSparsityPattern());
377 Assert(cols->sparsity_pattern.compressed || cols->empty(),
378 ChunkSparsityPattern::ExcNotCompressed());
379
380 // do initial zeroing of elements in parallel. Try to achieve a similar
381 // layout as when doing matrix-vector products, as on some NUMA systems, a
382 // memory block is assigned to memory banks where the first access is
383 // generated. For sparse matrices, the first operations is usually the
384 // operator=. The grain size is chosen to reflect the number of rows in
385 // minimum_parallel_grain_size, weighted by the number of nonzero entries
386 // per row on average.
387 const unsigned int matrix_size = cols->sparsity_pattern.n_nonzero_elements() *
388 cols->chunk_size * cols->chunk_size;
389 const unsigned int grain_size =
390 internal::SparseMatrixImplementation::minimum_parallel_grain_size *
391 (matrix_size + m()) / m();
392 if (matrix_size > grain_size)
393 parallel::apply_to_subranges(
394 0U,
395 matrix_size,
396 [this](const unsigned int begin, const unsigned int end) {
397 internal::ChunkSparseMatrixImplementation::zero_subrange(begin,
398 end,
399 val.get());
400 },
401 grain_size);
402 else if (matrix_size > 0)
403 std::memset(val.get(), 0, matrix_size * sizeof(number));
404
405 return *this;
406 }
407
408
409
410 template <typename number>
411 ChunkSparseMatrix<number> &
412 ChunkSparseMatrix<number>::operator=(const IdentityMatrix &id)
413 {
414 Assert(cols->n_rows() == id.m(),
415 ExcDimensionMismatch(cols->n_rows(), id.m()));
416 Assert(cols->n_cols() == id.n(),
417 ExcDimensionMismatch(cols->n_cols(), id.n()));
418
419 *this = 0;
420 for (size_type i = 0; i < n(); ++i)
421 this->set(i, i, 1.);
422
423 return *this;
424 }
425
426
427
428 template <typename number>
429 void
430 ChunkSparseMatrix<number>::reinit(const ChunkSparsityPattern &sparsity)
431 {
432 cols = &sparsity;
433
434 if (cols->empty())
435 {
436 val.reset();
437 max_len = 0;
438 return;
439 }
440
441 // allocate not just m() * n() elements but enough so that we can store full
442 // chunks. this entails some padding elements
443 const size_type chunk_size = cols->get_chunk_size();
444 const size_type N =
445 cols->sparsity_pattern.n_nonzero_elements() * chunk_size * chunk_size;
446 if (N > max_len || max_len == 0)
447 {
448 val = std::make_unique<number[]>(N);
449 max_len = N;
450 }
451
452 // fill with zeros. do not just fill N elements but all that we allocated to
453 // ensure that also the padding elements are zero and not left at previous
454 // values
455 this->operator=(0.);
456 }
457
458
459
460 template <typename number>
461 void
462 ChunkSparseMatrix<number>::clear()
463 {
464 cols = nullptr;
465 val.reset();
466 max_len = 0;
467 }
468
469
470
471 template <typename number>
472 bool
473 ChunkSparseMatrix<number>::empty() const
474 {
475 if (cols == nullptr)
476 return true;
477 else
478 return cols->empty();
479 }
480
481
482
483 template <typename number>
484 typename ChunkSparseMatrix<number>::size_type
485 ChunkSparseMatrix<number>::n_nonzero_elements() const
486 {
487 Assert(cols != nullptr, ExcNeedsSparsityPattern());
488 return cols->n_nonzero_elements();
489 }
490
491
492
493 template <typename number>
494 typename ChunkSparseMatrix<number>::size_type
495 ChunkSparseMatrix<number>::n_actually_nonzero_elements() const
496 {
497 Assert(cols != nullptr, ExcNeedsSparsityPattern());
498
499 // count those elements that are nonzero, even if they lie in the padding
500 // around the matrix. since we have the invariant that padding elements are
501 // zero, nothing bad can happen here
502 const size_type chunk_size = cols->get_chunk_size();
503 return std::count_if(val.get(),
504 val.get() + cols->sparsity_pattern.n_nonzero_elements() *
505 chunk_size * chunk_size,
506 [](const double element) { return element != 0.; });
507 }
508
509
510
511 template <typename number>
512 void
513 ChunkSparseMatrix<number>::symmetrize()
514 {
515 Assert(cols != nullptr, ExcNeedsSparsityPattern());
516 Assert(cols->rows == cols->cols, ExcNotQuadratic());
517
518 DEAL_II_NOT_IMPLEMENTED();
519 }
520
521
522
523 template <typename number>
524 template <typename somenumber>
525 ChunkSparseMatrix<number> &
526 ChunkSparseMatrix<number>::copy_from(
527 const ChunkSparseMatrix<somenumber> &matrix)
528 {
529 Assert(cols != nullptr, ExcNeedsSparsityPattern());
530 Assert(val != nullptr, ExcNotInitialized());
531 Assert(cols == matrix.cols, ExcDifferentChunkSparsityPatterns());
532
533 // copy everything, including padding elements
534 const size_type chunk_size = cols->get_chunk_size();
535 std::copy(matrix.val.get(),
536 matrix.val.get() + cols->sparsity_pattern.n_nonzero_elements() *
537 chunk_size * chunk_size,
538 val.get());
539
540 return *this;
541 }
542
543
544
545 template <typename number>
546 template <typename somenumber>
547 void
548 ChunkSparseMatrix<number>::copy_from(const FullMatrix<somenumber> &matrix)
549 {
550 // first delete previous content
551 *this = 0;
552
553 // then copy old matrix
554 for (size_type row = 0; row < matrix.m(); ++row)
555 for (size_type col = 0; col < matrix.n(); ++col)
556 if (matrix(row, col) != 0)
557 set(row, col, matrix(row, col));
558 }
559
560
561
562 template <typename number>
563 template <typename somenumber>
564 void
565 ChunkSparseMatrix<number>::add(const number factor,
566 const ChunkSparseMatrix<somenumber> &matrix)
567 {
568 Assert(cols != nullptr, ExcNeedsSparsityPattern());
569 Assert(val != nullptr, ExcNotInitialized());
570 Assert(cols == matrix.cols, ExcDifferentChunkSparsityPatterns());
571
572 // add everything, including padding elements
573 const size_type chunk_size = cols->get_chunk_size();
574 number *val_ptr = val.get();
575 const somenumber *matrix_ptr = matrix.val.get();
576 const number *const end_ptr =
577 val.get() +
578 cols->sparsity_pattern.n_nonzero_elements() * chunk_size * chunk_size;
579
580 while (val_ptr != end_ptr)
581 *val_ptr++ += factor * *matrix_ptr++;
582 }
583
584
585
586 template <typename number>
587 void
588 ChunkSparseMatrix<number>::extract_row_copy(const size_type row,
589 const size_type array_length,
590 size_type &row_length,
591 size_type *column_indices,
592 number *values) const
593 {
594 AssertIndexRange(cols->row_length(row), array_length + 1);
595 AssertIndexRange(row, m());
596 const unsigned int chunk_size = cols->get_chunk_size();
597 const size_type reduced_row = row / chunk_size;
598
599 SparsityPattern::iterator it = cols->sparsity_pattern.begin(reduced_row),
600 itend = cols->sparsity_pattern.end(reduced_row);
601 const number *val_ptr =
602 &val[(it - cols->sparsity_pattern.begin(0)) * chunk_size * chunk_size +
603 (row % chunk_size) * chunk_size];
604
605 // find out if we did padding and if this row is affected by it
606 if (cols->n_cols() % chunk_size == 0)
607 {
608 for (; it < itend; ++it)
609 {
610 for (unsigned int c = 0; c < chunk_size; ++c)
611 {
612 *values++ = val_ptr[c];
613 *column_indices++ = it->column() * chunk_size + c;
614 }
615 val_ptr += chunk_size * chunk_size;
616 }
617 row_length =
618 chunk_size * (cols->sparsity_pattern.row_length(reduced_row));
619 }
620 else
621 {
622 const unsigned int last_chunk_size = cols->n_cols() % chunk_size;
623 row_length = 0;
624 for (; it < itend; ++it)
625 {
626 const unsigned int next_chunk_size =
627 (it->column() == cols->sparsity_pattern.n_cols() - 1) ?
628 last_chunk_size :
629 chunk_size;
630 for (unsigned int c = 0; c < next_chunk_size; ++c, ++row_length)
631 {
632 *values++ = val_ptr[c];
633 *column_indices++ = it->column() * chunk_size + c;
634 }
635 val_ptr += chunk_size * chunk_size;
636 }
637 }
638 }
639
640
641
642 template <typename number>
643 template <class OutVector, class InVector>
644 void
645 ChunkSparseMatrix<number>::vmult(OutVector &dst, const InVector &src) const
646 {
647 Assert(cols != nullptr, ExcNeedsSparsityPattern());
648 Assert(val != nullptr, ExcNotInitialized());
649 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
650 Assert(n() == src.size(), ExcDimensionMismatch(n(), src.size()));
651
652 Assert(!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
653
654 // set the output vector to zero and then add to it the contributions of
655 // vmults from individual chunks. this is what vmult_add does
656 dst = 0;
657 vmult_add(dst, src);
658 }
659
660
661
662 template <typename number>
663 template <class OutVector, class InVector>
664 void
665 ChunkSparseMatrix<number>::Tvmult(OutVector &dst, const InVector &src) const
666 {
667 Assert(val != nullptr, ExcNotInitialized());
668 Assert(cols != nullptr, ExcNeedsSparsityPattern());
669 Assert(n() == dst.size(), ExcDimensionMismatch(n(), dst.size()));
670 Assert(m() == src.size(), ExcDimensionMismatch(m(), src.size()));
671
672 Assert(!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
673
674 Assert(cols != nullptr, ExcNeedsSparsityPattern());
675 Assert(val != nullptr, ExcNotInitialized());
676 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
677 Assert(n() == src.size(), ExcDimensionMismatch(n(), src.size()));
678
679 Assert(!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
680
681 // set the output vector to zero and then add to it the contributions of
682 // vmults from individual chunks. this is what vmult_add does
683 dst = 0;
684 Tvmult_add(dst, src);
685 }
686
687
688
689 template <typename number>
690 template <class OutVector, class InVector>
691 void
692 ChunkSparseMatrix<number>::vmult_add(OutVector &dst, const InVector &src) const
693 {
694 Assert(cols != nullptr, ExcNeedsSparsityPattern());
695 Assert(val != nullptr, ExcNotInitialized());
696 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
697 Assert(n() == src.size(), ExcDimensionMismatch(n(), src.size()));
698
699 Assert(!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
700 parallel::apply_to_subranges(
701 0U,
702 cols->sparsity_pattern.n_rows(),
703 [this, &src, &dst](const unsigned int begin_row,
704 const unsigned int end_row) {
705 internal::ChunkSparseMatrixImplementation::vmult_add_on_subrange(
706 *cols,
707 begin_row,
708 end_row,
709 val.get(),
710 cols->sparsity_pattern.rowstart.get(),
711 cols->sparsity_pattern.colnums.get(),
712 src,
713 dst);
714 },
715 internal::SparseMatrixImplementation::minimum_parallel_grain_size /
716 cols->chunk_size +
717 1);
718 }
719
720
721 template <typename number>
722 template <class OutVector, class InVector>
723 void
724 ChunkSparseMatrix<number>::Tvmult_add(OutVector &dst, const InVector &src) const
725 {
726 Assert(cols != nullptr, ExcNeedsSparsityPattern());
727 Assert(val != nullptr, ExcNotInitialized());
728 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
729 Assert(n() == src.size(), ExcDimensionMismatch(n(), src.size()));
730
731 Assert(!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
732
733 const size_type n_chunk_rows = cols->sparsity_pattern.n_rows();
734
735 // loop over all chunks. note that we need to treat the last chunk row and
736 // column differently if they have padding elements
737 const bool rows_have_padding = (m() % cols->chunk_size != 0),
738 cols_have_padding = (n() % cols->chunk_size != 0);
739
740 const size_type n_regular_chunk_rows =
741 (rows_have_padding ? n_chunk_rows - 1 : n_chunk_rows);
742
743 // like in vmult_add, but don't keep an iterator into dst around since we're
744 // not traversing it sequentially this time
745 const number *val_ptr = val.get();
746 const size_type *colnum_ptr = cols->sparsity_pattern.colnums.get();
747
748 for (size_type chunk_row = 0; chunk_row < n_regular_chunk_rows; ++chunk_row)
749 {
750 const number *const val_end_of_row =
751 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
752 cols->chunk_size];
753 while (val_ptr != val_end_of_row)
754 {
755 if ((cols_have_padding == false) ||
756 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
757 internal::ChunkSparseMatrixImplementation::chunk_Tvmult_add(
758 cols->chunk_size,
759 val_ptr,
760 src.begin() + chunk_row * cols->chunk_size,
761 dst.begin() + *colnum_ptr * cols->chunk_size);
762 else
763 // we're at a chunk column that has padding
764 for (size_type r = 0; r < cols->chunk_size; ++r)
765 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
766 dst(*colnum_ptr * cols->chunk_size + c) +=
767 (val_ptr[r * cols->chunk_size + c] *
768 src(chunk_row * cols->chunk_size + r));
769
770 ++colnum_ptr;
771 val_ptr += cols->chunk_size * cols->chunk_size;
772 }
773 }
774
775 // now deal with last chunk row if necessary
776 if (rows_have_padding)
777 {
778 const size_type chunk_row = n_chunk_rows - 1;
779
780 const number *const val_end_of_row =
781 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
782 cols->chunk_size];
783 while (val_ptr != val_end_of_row)
784 {
785 if ((cols_have_padding == false) ||
786 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
787 {
788 // we're at a chunk row but not column that has padding
789 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
790 for (size_type c = 0; c < cols->chunk_size; ++c)
791 dst(*colnum_ptr * cols->chunk_size + c) +=
792 (val_ptr[r * cols->chunk_size + c] *
793 src(chunk_row * cols->chunk_size + r));
794 }
795 else
796 // we're at a chunk row and column that has padding
797 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
798 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
799 dst(*colnum_ptr * cols->chunk_size + c) +=
800 (val_ptr[r * cols->chunk_size + c] *
801 src(chunk_row * cols->chunk_size + r));
802
803 ++colnum_ptr;
804 val_ptr += cols->chunk_size * cols->chunk_size;
805 }
806 }
807 }
808
809
810 template <typename number>
811 template <typename somenumber>
812 somenumber
813 ChunkSparseMatrix<number>::matrix_norm_square(const Vector<somenumber> &v) const
814 {
815 Assert(cols != nullptr, ExcNeedsSparsityPattern());
816 Assert(val != nullptr, ExcNotInitialized());
817 Assert(m() == v.size(), ExcDimensionMismatch(m(), v.size()));
818 Assert(n() == v.size(), ExcDimensionMismatch(n(), v.size()));
819
820 somenumber result = 0;
821
822 ////////////////
823 // like matrix_scalar_product, except that the two vectors are now the same
824
825 const size_type n_chunk_rows = cols->sparsity_pattern.n_rows();
826
827 // loop over all chunks. note that we need to treat the last chunk row and
828 // column differently if they have padding elements
829 const bool rows_have_padding = (m() % cols->chunk_size != 0),
830 cols_have_padding = (n() % cols->chunk_size != 0);
831
832 const size_type n_regular_chunk_rows =
833 (rows_have_padding ? n_chunk_rows - 1 : n_chunk_rows);
834
835 const number *val_ptr = val.get();
836 const size_type *colnum_ptr = cols->sparsity_pattern.colnums.get();
837 typename Vector<somenumber>::const_iterator v_ptr = v.begin();
838
839 for (size_type chunk_row = 0; chunk_row < n_regular_chunk_rows; ++chunk_row)
840 {
841 const number *const val_end_of_row =
842 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
843 cols->chunk_size];
844 while (val_ptr != val_end_of_row)
845 {
846 if ((cols_have_padding == false) ||
847 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
848 result += internal::ChunkSparseMatrixImplementation::
849 chunk_matrix_scalar_product<somenumber>(cols->chunk_size,
850 val_ptr,
851 v_ptr,
852 v.begin() +
853 *colnum_ptr *
854 cols->chunk_size);
855 else
856 // we're at a chunk column that has padding
857 for (size_type r = 0; r < cols->chunk_size; ++r)
858 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
859 result += v(chunk_row * cols->chunk_size + r) *
860 (val_ptr[r * cols->chunk_size + c] *
861 v(*colnum_ptr * cols->chunk_size + c));
862
863 ++colnum_ptr;
864 val_ptr += cols->chunk_size * cols->chunk_size;
865 }
866
867
868 v_ptr += cols->chunk_size;
869 }
870
871 // now deal with last chunk row if necessary
872 if (rows_have_padding)
873 {
874 const size_type chunk_row = n_chunk_rows - 1;
875
876 const number *const val_end_of_row =
877 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
878 cols->chunk_size];
879 while (val_ptr != val_end_of_row)
880 {
881 if ((cols_have_padding == false) ||
882 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
883 {
884 // we're at a chunk row but not column that has padding
885 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
886 for (size_type c = 0; c < cols->chunk_size; ++c)
887 result += v(chunk_row * cols->chunk_size + r) *
888 (val_ptr[r * cols->chunk_size + c] *
889 v(*colnum_ptr * cols->chunk_size + c));
890 }
891 else
892 // we're at a chunk row and column that has padding
893 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
894 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
895 result += v(chunk_row * cols->chunk_size + r) *
896 (val_ptr[r * cols->chunk_size + c] *
897 v(*colnum_ptr * cols->chunk_size + c));
898
899 ++colnum_ptr;
900 val_ptr += cols->chunk_size * cols->chunk_size;
901 }
902 }
903
904 return result;
905 }
906
907
908
909 template <typename number>
910 template <typename somenumber>
911 somenumber
912 ChunkSparseMatrix<number>::matrix_scalar_product(
913 const Vector<somenumber> &u,
914 const Vector<somenumber> &v) const
915 {
916 Assert(cols != nullptr, ExcNeedsSparsityPattern());
917 Assert(val != nullptr, ExcNotInitialized());
918 Assert(m() == u.size(), ExcDimensionMismatch(m(), u.size()));
919 Assert(n() == v.size(), ExcDimensionMismatch(n(), v.size()));
920
921 // the following works like the vmult_add function
922 somenumber result = 0;
923
924 const size_type n_chunk_rows = cols->sparsity_pattern.n_rows();
925
926 // loop over all chunks. note that we need to treat the last chunk row and
927 // column differently if they have padding elements
928 const bool rows_have_padding = (m() % cols->chunk_size != 0),
929 cols_have_padding = (n() % cols->chunk_size != 0);
930
931 const size_type n_regular_chunk_rows =
932 (rows_have_padding ? n_chunk_rows - 1 : n_chunk_rows);
933
934 const number *val_ptr = val.get();
935 const size_type *colnum_ptr = cols->sparsity_pattern.colnums.get();
936 typename Vector<somenumber>::const_iterator u_ptr = u.begin();
937
938 for (size_type chunk_row = 0; chunk_row < n_regular_chunk_rows; ++chunk_row)
939 {
940 const number *const val_end_of_row =
941 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
942 cols->chunk_size];
943 while (val_ptr != val_end_of_row)
944 {
945 if ((cols_have_padding == false) ||
946 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
947 result += internal::ChunkSparseMatrixImplementation::
948 chunk_matrix_scalar_product<somenumber>(cols->chunk_size,
949 val_ptr,
950 u_ptr,
951 v.begin() +
952 *colnum_ptr *
953 cols->chunk_size);
954 else
955 // we're at a chunk column that has padding
956 for (size_type r = 0; r < cols->chunk_size; ++r)
957 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
958 result += u(chunk_row * cols->chunk_size + r) *
959 (val_ptr[r * cols->chunk_size + c] *
960 v(*colnum_ptr * cols->chunk_size + c));
961
962 ++colnum_ptr;
963 val_ptr += cols->chunk_size * cols->chunk_size;
964 }
965
966
967 u_ptr += cols->chunk_size;
968 }
969
970 // now deal with last chunk row if necessary
971 if (rows_have_padding)
972 {
973 const size_type chunk_row = n_chunk_rows - 1;
974
975 const number *const val_end_of_row =
976 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
977 cols->chunk_size];
978 while (val_ptr != val_end_of_row)
979 {
980 if ((cols_have_padding == false) ||
981 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
982 {
983 // we're at a chunk row but not column that has padding
984 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
985 for (size_type c = 0; c < cols->chunk_size; ++c)
986 result += u(chunk_row * cols->chunk_size + r) *
987 (val_ptr[r * cols->chunk_size + c] *
988 v(*colnum_ptr * cols->chunk_size + c));
989 }
990 else
991 // we're at a chunk row and column that has padding
992 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
993 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
994 result += u(chunk_row * cols->chunk_size + r) *
995 (val_ptr[r * cols->chunk_size + c] *
996 v(*colnum_ptr * cols->chunk_size + c));
997
998 ++colnum_ptr;
999 val_ptr += cols->chunk_size * cols->chunk_size;
1000 }
1001 }
1002
1003 return result;
1004 }
1005
1006
1007
1008 template <typename number>
1009 typename ChunkSparseMatrix<number>::real_type
1010 ChunkSparseMatrix<number>::l1_norm() const
1011 {
1012 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1013 Assert(val != nullptr, ExcNotInitialized());
1014
1015 const size_type n_chunk_rows = cols->sparsity_pattern.n_rows();
1016
1017 // loop over all rows and columns; it is safe to also loop over the padding
1018 // elements (they are zero) if we make sure that the vector into which we
1019 // sum column sums is large enough
1020 Vector<real_type> column_sums(cols->sparsity_pattern.n_cols() *
1021 cols->chunk_size);
1022
1023 for (size_type chunk_row = 0; chunk_row < n_chunk_rows; ++chunk_row)
1024 for (size_type j = cols->sparsity_pattern.rowstart[chunk_row];
1025 j < cols->sparsity_pattern.rowstart[chunk_row + 1];
1026 ++j)
1027 for (size_type r = 0; r < cols->chunk_size; ++r)
1028 for (size_type s = 0; s < cols->chunk_size; ++s)
1029 column_sums(cols->sparsity_pattern.colnums[j] * cols->chunk_size +
1030 s) +=
1031 numbers::NumberTraits<number>::abs(
1032 val[j * cols->chunk_size * cols->chunk_size +
1033 r * cols->chunk_size + s]);
1034
1035 return column_sums.linfty_norm();
1036 }
1037
1038
1039
1040 template <typename number>
1041 typename ChunkSparseMatrix<number>::real_type
1042 ChunkSparseMatrix<number>::linfty_norm() const
1043 {
1044 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1045 Assert(val != nullptr, ExcNotInitialized());
1046
1047 // this function works like l1_norm(). it can be made more efficient
1048 // (without allocating a temporary vector) as is done in the SparseMatrix
1049 // class but since it is rarely called in time critical places it is
1050 // probably not worth it
1051 const size_type n_chunk_rows = cols->sparsity_pattern.n_rows();
1052
1053 // loop over all rows and columns; it is safe to also loop over the padding
1054 // elements (they are zero) if we make sure that the vector into which we
1055 // sum column sums is large enough
1056 Vector<real_type> row_sums(cols->sparsity_pattern.n_rows() *
1057 cols->chunk_size);
1058
1059 for (size_type chunk_row = 0; chunk_row < n_chunk_rows; ++chunk_row)
1060 for (size_type j = cols->sparsity_pattern.rowstart[chunk_row];
1061 j < cols->sparsity_pattern.rowstart[chunk_row + 1];
1062 ++j)
1063 for (size_type r = 0; r < cols->chunk_size; ++r)
1064 for (size_type s = 0; s < cols->chunk_size; ++s)
1065 row_sums(chunk_row * cols->chunk_size + r) +=
1066 numbers::NumberTraits<number>::abs(
1067 val[j * cols->chunk_size * cols->chunk_size +
1068 r * cols->chunk_size + s]);
1069
1070 return row_sums.linfty_norm();
1071 }
1072
1073
1074
1075 template <typename number>
1076 typename ChunkSparseMatrix<number>::real_type
1077 ChunkSparseMatrix<number>::frobenius_norm() const
1078 {
1079 // simply add up all entries in the sparsity pattern, without taking any
1080 // reference to rows or columns
1081 //
1082 // padding elements are zero, so we can add them up as well
1083 real_type norm_sqr = 0;
1084 for (const number *ptr = val.get(); ptr != val.get() + max_len; ++ptr)
1085 norm_sqr += numbers::NumberTraits<number>::abs_square(*ptr);
1086
1087 return std::sqrt(norm_sqr);
1088 }
1089
1090
1091
1092 template <typename number>
1093 template <typename somenumber>
1094 somenumber
1095 ChunkSparseMatrix<number>::residual(Vector<somenumber> &dst,
1096 const Vector<somenumber> &u,
1097 const Vector<somenumber> &b) const
1098 {
1099 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1100 Assert(val != nullptr, ExcNotInitialized());
1101 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
1102 Assert(m() == b.size(), ExcDimensionMismatch(m(), b.size()));
1103 Assert(n() == u.size(), ExcDimensionMismatch(n(), u.size()));
1104
1105 Assert(&u != &dst, ExcSourceEqualsDestination());
1106
1107 // set dst=b, then subtract the result of A*u from it. since the purpose of
1108 // the current class is to promote streaming of data rather than more random
1109 // access patterns, breaking things up into two loops may be reasonable
1110 dst = b;
1111
1112 /////////
1113 // the rest of this function is like vmult_add, except that we subtract
1114 // rather than add A*u
1115 /////////
1116 const size_type n_chunk_rows = cols->sparsity_pattern.n_rows();
1117
1118 // loop over all chunks. note that we need to treat the last chunk row and
1119 // column differently if they have padding elements
1120 const bool rows_have_padding = (m() % cols->chunk_size != 0),
1121 cols_have_padding = (n() % cols->chunk_size != 0);
1122
1123 const size_type n_regular_chunk_rows =
1124 (rows_have_padding ? n_chunk_rows - 1 : n_chunk_rows);
1125
1126 const number *val_ptr = val.get();
1127 const size_type *colnum_ptr = cols->sparsity_pattern.colnums.get();
1128 typename Vector<somenumber>::iterator dst_ptr = dst.begin();
1129
1130 for (size_type chunk_row = 0; chunk_row < n_regular_chunk_rows; ++chunk_row)
1131 {
1132 const number *const val_end_of_row =
1133 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
1134 cols->chunk_size];
1135 while (val_ptr != val_end_of_row)
1136 {
1137 if ((cols_have_padding == false) ||
1138 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
1139 internal::ChunkSparseMatrixImplementation::chunk_vmult_subtract(
1140 cols->chunk_size,
1141 val_ptr,
1142 u.begin() + *colnum_ptr * cols->chunk_size,
1143 dst_ptr);
1144 else
1145 // we're at a chunk column that has padding
1146 for (size_type r = 0; r < cols->chunk_size; ++r)
1147 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
1148 dst(chunk_row * cols->chunk_size + r) -=
1149 (val_ptr[r * cols->chunk_size + c] *
1150 u(*colnum_ptr * cols->chunk_size + c));
1151
1152 ++colnum_ptr;
1153 val_ptr += cols->chunk_size * cols->chunk_size;
1154 }
1155
1156
1157 dst_ptr += cols->chunk_size;
1158 }
1159
1160 // now deal with last chunk row if necessary
1161 if (rows_have_padding)
1162 {
1163 const size_type chunk_row = n_chunk_rows - 1;
1164
1165 const number *const val_end_of_row =
1166 &val[cols->sparsity_pattern.rowstart[chunk_row + 1] * cols->chunk_size *
1167 cols->chunk_size];
1168 while (val_ptr != val_end_of_row)
1169 {
1170 if ((cols_have_padding == false) ||
1171 (*colnum_ptr != cols->sparsity_pattern.n_cols() - 1))
1172 {
1173 // we're at a chunk row but not column that has padding
1174 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
1175 for (size_type c = 0; c < cols->chunk_size; ++c)
1176 dst(chunk_row * cols->chunk_size + r) -=
1177 (val_ptr[r * cols->chunk_size + c] *
1178 u(*colnum_ptr * cols->chunk_size + c));
1179 }
1180 else
1181 // we're at a chunk row and column that has padding
1182 for (size_type r = 0; r < m() % cols->chunk_size; ++r)
1183 for (size_type c = 0; c < n() % cols->chunk_size; ++c)
1184 dst(chunk_row * cols->chunk_size + r) -=
1185 (val_ptr[r * cols->chunk_size + c] *
1186 u(*colnum_ptr * cols->chunk_size + c));
1187
1188 ++colnum_ptr;
1189 val_ptr += cols->chunk_size * cols->chunk_size;
1190 }
1191
1192
1193 dst_ptr += cols->chunk_size;
1194 }
1195
1196 // finally compute the norm
1197 return dst.l2_norm();
1198 }
1199
1200
1201
1202 template <typename number>
1203 template <typename somenumber>
1204 void
1205 ChunkSparseMatrix<number>::precondition_Jacobi(Vector<somenumber> &dst,
1206 const Vector<somenumber> &src,
1207 const number /*om*/) const
1208 {
1209 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1210 Assert(val != nullptr, ExcNotInitialized());
1211 Assert(m() == n(),
1212 ExcMessage("This operation is only valid on square matrices."));
1213
1214 Assert(dst.size() == n(), ExcDimensionMismatch(dst.size(), n()));
1215 Assert(src.size() == n(), ExcDimensionMismatch(src.size(), n()));
1216
1217 DEAL_II_NOT_IMPLEMENTED();
1218 }
1219
1220
1221
1222 template <typename number>
1223 template <typename somenumber>
1224 void
1225 ChunkSparseMatrix<number>::precondition_SSOR(Vector<somenumber> &dst,
1226 const Vector<somenumber> &src,
1227 const number /*om*/) const
1228 {
1229 // to understand how this function works you may want to take a look at the
1230 // CVS archives to see the original version which is much clearer...
1231 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1232 Assert(val != nullptr, ExcNotInitialized());
1233 Assert(m() == n(),
1234 ExcMessage("This operation is only valid on square matrices."));
1235
1236 Assert(dst.size() == n(), ExcDimensionMismatch(dst.size(), n()));
1237 Assert(src.size() == n(), ExcDimensionMismatch(src.size(), n()));
1238
1239 DEAL_II_NOT_IMPLEMENTED();
1240 }
1241
1242
1243 template <typename number>
1244 template <typename somenumber>
1245 void
1246 ChunkSparseMatrix<number>::precondition_SOR(Vector<somenumber> &dst,
1247 const Vector<somenumber> &src,
1248 const number om) const
1249 {
1250 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1251 Assert(val != nullptr, ExcNotInitialized());
1252 Assert(m() == n(),
1253 ExcMessage("This operation is only valid on square matrices."));
1254
1255
1256 dst = src;
1257 SOR(dst, om);
1258 }
1259
1260
1261 template <typename number>
1262 template <typename somenumber>
1263 void
1264 ChunkSparseMatrix<number>::precondition_TSOR(Vector<somenumber> &dst,
1265 const Vector<somenumber> &src,
1266 const number om) const
1267 {
1268 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1269 Assert(val != nullptr, ExcNotInitialized());
1270 Assert(m() == n(),
1271 ExcMessage("This operation is only valid on square matrices."));
1272
1273
1274 dst = src;
1275 TSOR(dst, om);
1276 }
1277
1278
1279 template <typename number>
1280 template <typename somenumber>
1281 void
1282 ChunkSparseMatrix<number>::SOR(Vector<somenumber> &dst,
1283 const number /*om*/) const
1284 {
1285 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1286 Assert(val != nullptr, ExcNotInitialized());
1287 Assert(m() == n(),
1288 ExcMessage("This operation is only valid on square matrices."));
1289 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
1290
1291 DEAL_II_NOT_IMPLEMENTED();
1292 }
1293
1294
1295 template <typename number>
1296 template <typename somenumber>
1297 void
1298 ChunkSparseMatrix<number>::TSOR(Vector<somenumber> &dst,
1299 const number /*om*/) const
1300 {
1301 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1302 Assert(val != nullptr, ExcNotInitialized());
1303 Assert(m() == n(),
1304 ExcMessage("This operation is only valid on square matrices."));
1305 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
1306
1307 DEAL_II_NOT_IMPLEMENTED();
1308 }
1309
1310
1311 template <typename number>
1312 template <typename somenumber>
1313 void
1314 ChunkSparseMatrix<number>::PSOR(
1315 Vector<somenumber> &dst,
1316 const std::vector<size_type> &permutation,
1317 const std::vector<size_type> &inverse_permutation,
1318 const number /*om*/) const
1319 {
1320 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1321 Assert(val != nullptr, ExcNotInitialized());
1322 Assert(m() == n(),
1323 ExcMessage("This operation is only valid on square matrices."));
1324
1325 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
1326 Assert(m() == permutation.size(),
1327 ExcDimensionMismatch(m(), permutation.size()));
1328 Assert(m() == inverse_permutation.size(),
1329 ExcDimensionMismatch(m(), inverse_permutation.size()));
1330
1331 DEAL_II_NOT_IMPLEMENTED();
1332 }
1333
1334
1335 template <typename number>
1336 template <typename somenumber>
1337 void
1338 ChunkSparseMatrix<number>::TPSOR(
1339 Vector<somenumber> &dst,
1340 const std::vector<size_type> &permutation,
1341 const std::vector<size_type> &inverse_permutation,
1342 const number /*om*/) const
1343 {
1344 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1345 Assert(val != nullptr, ExcNotInitialized());
1346 Assert(m() == n(),
1347 ExcMessage("This operation is only valid on square matrices."));
1348
1349 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
1350 Assert(m() == permutation.size(),
1351 ExcDimensionMismatch(m(), permutation.size()));
1352 Assert(m() == inverse_permutation.size(),
1353 ExcDimensionMismatch(m(), inverse_permutation.size()));
1354
1355 DEAL_II_NOT_IMPLEMENTED();
1356 }
1357
1358
1359
1360 template <typename number>
1361 template <typename somenumber>
1362 void
1363 ChunkSparseMatrix<number>::SOR_step(Vector<somenumber> &v,
1364 const Vector<somenumber> &b,
1365 const number /*om*/) const
1366 {
1367 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1368 Assert(val != nullptr, ExcNotInitialized());
1369 Assert(m() == n(),
1370 ExcMessage("This operation is only valid on square matrices."));
1371
1372 Assert(m() == v.size(), ExcDimensionMismatch(m(), v.size()));
1373 Assert(m() == b.size(), ExcDimensionMismatch(m(), b.size()));
1374
1375 DEAL_II_NOT_IMPLEMENTED();
1376 }
1377
1378
1379
1380 template <typename number>
1381 template <typename somenumber>
1382 void
1383 ChunkSparseMatrix<number>::TSOR_step(Vector<somenumber> &v,
1384 const Vector<somenumber> &b,
1385 const number /*om*/) const
1386 {
1387 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1388 Assert(val != nullptr, ExcNotInitialized());
1389 Assert(m() == n(),
1390 ExcMessage("This operation is only valid on square matrices."));
1391
1392 Assert(m() == v.size(), ExcDimensionMismatch(m(), v.size()));
1393 Assert(m() == b.size(), ExcDimensionMismatch(m(), b.size()));
1394
1395 DEAL_II_NOT_IMPLEMENTED();
1396 }
1397
1398
1399
1400 template <typename number>
1401 template <typename somenumber>
1402 void
1403 ChunkSparseMatrix<number>::SSOR_step(Vector<somenumber> &v,
1404 const Vector<somenumber> &b,
1405 const number om) const
1406 {
1407 SOR_step(v, b, om);
1408 TSOR_step(v, b, om);
1409 }
1410
1411
1412
1413 template <typename number>
1414 template <typename somenumber>
1415 void
1416 ChunkSparseMatrix<number>::SSOR(Vector<somenumber> &dst,
1417 const number /*om*/) const
1418 {
1419 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1420 Assert(val != nullptr, ExcNotInitialized());
1421 Assert(m() == n(),
1422 ExcMessage("This operation is only valid on square matrices."));
1423
1424 Assert(m() == dst.size(), ExcDimensionMismatch(m(), dst.size()));
1425
1426 DEAL_II_NOT_IMPLEMENTED();
1427 }
1428
1429
1430
1431 template <typename number>
1432 void
1433 ChunkSparseMatrix<number>::print(std::ostream &out) const
1434 {
1435 AssertThrow(out.fail() == false, ExcIO());
1436
1437 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1438 Assert(val != nullptr, ExcNotInitialized());
1439
1440 DEAL_II_NOT_IMPLEMENTED();
1441
1442 AssertThrow(out.fail() == false, ExcIO());
1443 }
1444
1445
1446 template <typename number>
1447 void
1448 ChunkSparseMatrix<number>::print_formatted(std::ostream &out,
1449 const unsigned int precision,
1450 const bool scientific,
1451 const unsigned int width_,
1452 const char *zero_string,
1453 const double denominator,
1454 const char *separator) const
1455 {
1456 AssertThrow(out.fail() == false, ExcIO());
1457
1458 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1459 Assert(val != nullptr, ExcNotInitialized());
1460
1461 unsigned int width = width_;
1462
1463 DEAL_II_NOT_IMPLEMENTED();
1464
1465 std::ios::fmtflags old_flags = out.flags();
1466 unsigned int old_precision = out.precision(precision);
1467
1468 if (scientific)
1469 {
1470 out.setf(std::ios::scientific, std::ios::floatfield);
1471 if (width == 0u)
1472 width = precision + 7;
1473 }
1474 else
1475 {
1476 out.setf(std::ios::fixed, std::ios::floatfield);
1477 if (width == 0u)
1478 width = precision + 2;
1479 }
1480
1481 for (size_type i = 0; i < m(); ++i)
1482 {
1483 for (size_type j = 0; j < n(); ++j)
1484 if (cols->sparsity_pattern(i, j) != SparsityPattern::invalid_entry)
1485 out << std::setw(width)
1486 << val[cols->sparsity_pattern(i, j)] * denominator << separator;
1487 else
1488 out << std::setw(width) << zero_string << separator;
1489 out << std::endl;
1490 };
1491 AssertThrow(out.fail() == false, ExcIO());
1492
1493 // reset output format
1494 out.precision(old_precision);
1495 out.flags(old_flags);
1496 }
1497
1498
1499
1500 template <typename number>
1501 void
1502 ChunkSparseMatrix<number>::print_pattern(std::ostream &out,
1503 const double threshold) const
1504 {
1505 AssertThrow(out.fail() == false, ExcIO());
1506
1507 Assert(cols != nullptr, ExcNeedsSparsityPattern());
1508 Assert(val != nullptr, ExcNotInitialized());
1509
1510 const size_type chunk_size = cols->get_chunk_size();
1511
1512 // loop over all chunk rows and columns, and each time we find something
1513 // repeat it chunk_size times in both directions
1514 for (size_type i = 0; i < cols->sparsity_pattern.n_rows(); ++i)
1515 {
1516 for (size_type d = 0; d < chunk_size; ++d)
1517 for (size_type j = 0; j < cols->sparsity_pattern.n_cols(); ++j)
1518 if (cols->sparsity_pattern(i, j) == SparsityPattern::invalid_entry)
1519 {
1520 for (size_type e = 0; e < chunk_size; ++e)
1521 out << '.';
1522 }
1523 else if (std::fabs(val[cols->sparsity_pattern(i, j)]) > threshold)
1524 {
1525 for (size_type e = 0; e < chunk_size; ++e)
1526 out << '*';
1527 }
1528 else
1529 {
1530 for (size_type e = 0; e < chunk_size; ++e)
1531 out << ':';
1532 }
1533 out << std::endl;
1534 }
1535
1536 AssertThrow(out.fail() == false, ExcIO());
1537 }
1538
1539
1540
1541 template <typename number>
1542 void
1543 ChunkSparseMatrix<number>::block_write(std::ostream &out) const
1544 {
1545 AssertThrow(out.fail() == false, ExcIO());
1546
1547 // first the simple objects, bracketed in [...]
1548 out << '[' << max_len << "][";
1549 // then write out real data
1550 out.write(reinterpret_cast<const char *>(val.get()),
1551 reinterpret_cast<const char *>(val.get() + max_len) -
1552 reinterpret_cast<const char *>(val.get()));
1553 out << ']';
1554
1555 AssertThrow(out.fail() == false, ExcIO());
1556 }
1557
1558
1559
1560 template <typename number>
1561 void
1562 ChunkSparseMatrix<number>::block_read(std::istream &in)
1563 {
1564 AssertThrow(in.fail() == false, ExcIO());
1565
1566 char c;
1567
1568 // first read in simple data
1569 in >> c;
1570 AssertThrow(c == '[', ExcIO());
1571 in >> max_len;
1572
1573 in >> c;
1574 AssertThrow(c == ']', ExcIO());
1575 in >> c;
1576 AssertThrow(c == '[', ExcIO());
1577
1578 // reallocate space
1579 val = std::make_unique<number[]>(max_len);
1580
1581 // then read data
1582 in.read(reinterpret_cast<char *>(val.get()),
1583 reinterpret_cast<char *>(val.get() + max_len) -
1584 reinterpret_cast<char *>(val.get()));
1585 in >> c;
1586 AssertThrow(c == ']', ExcIO());
1587 }
1588
1589
1590
1591 template <typename number>
1592 std::size_t
1593 ChunkSparseMatrix<number>::memory_consumption() const
1594 {
1595 return sizeof(*this) + max_len * sizeof(number);
1596 }
1597
1598
1599 DEAL_II_NAMESPACE_CLOSE
1600
1601 #endif