Debian Codegraph @

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

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2024 - 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_trilinos_tpetra_sparse_matrix_templates_h
16 #define dealii_trilinos_tpetra_sparse_matrix_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/types.h>
21
22 #include <deal.II/lac/trilinos_tpetra_types.h>
23
24 #ifdef DEAL_II_TRILINOS_WITH_TPETRA
25
26 # include <deal.II/lac/dynamic_sparsity_pattern.h>
27 # include <deal.II/lac/full_matrix.h>
28 # include <deal.II/lac/trilinos_tpetra_sparse_matrix.h>
29 # include <deal.II/lac/trilinos_tpetra_sparsity_pattern.h>
30
31 DEAL_II_NAMESPACE_OPEN
32
33 namespace LinearAlgebra
34 {
35
36 namespace TpetraWrappers
37 {
38 namespace internal
39 {
40 template <typename Number, typename MemorySpace>
41 void
42 apply(const SparseMatrix<Number, MemorySpace> &M,
43 const Vector<Number, MemorySpace> &src,
44 Vector<Number, MemorySpace> &dst,
45 Teuchos::ETransp mode = Teuchos::NO_TRANS,
46 Number alpha = Teuchos::ScalarTraits<Number>::one(),
47 Number beta = Teuchos::ScalarTraits<Number>::zero())
48 {
49 Assert(
50 &src != &dst,
51 (typename SparseMatrix<double,
52 MemorySpace>::ExcSourceEqualsDestination()));
53 Assert(M.trilinos_matrix().isFillComplete(),
54 (typename SparseMatrix<double,
55 MemorySpace>::ExcMatrixNotCompressed()));
56
57 if (mode == Teuchos::NO_TRANS)
58 {
59 Assert(src.trilinos_vector().getMap()->isSameAs(
60 *M.trilinos_matrix().getDomainMap()),
61 (typename SparseMatrix<double,
62 MemorySpace>::ExcColMapMismatch()));
63 Assert(
64 dst.trilinos_vector().getMap()->isSameAs(
65 *M.trilinos_matrix().getRangeMap()),
66 (typename SparseMatrix<double,
67 MemorySpace>::ExcDomainMapMismatch()));
68 }
69 else
70 {
71 Assert(dst.trilinos_vector().getMap()->isSameAs(
72 *M.trilinos_matrix().getDomainMap()),
73 (typename SparseMatrix<double,
74 MemorySpace>::ExcColMapMismatch()));
75 Assert(
76 src.trilinos_vector().getMap()->isSameAs(
77 *M.trilinos_matrix().getRangeMap()),
78 (typename SparseMatrix<double,
79 MemorySpace>::ExcDomainMapMismatch()));
80 }
81
82 M.trilinos_matrix().apply(
83 src.trilinos_vector(), dst.trilinos_vector(), mode, alpha, beta);
84 }
85
86
87
88 template <typename Number, typename MemorySpace>
89 void
90 apply(const SparseMatrix<Number, MemorySpace> &M,
91 const dealii::Vector<Number> &src,
92 dealii::Vector<Number> &dst,
93 Teuchos::ETransp mode = Teuchos::NO_TRANS,
94 Number alpha = Teuchos::ScalarTraits<Number>::one(),
95 Number beta = Teuchos::ScalarTraits<Number>::zero())
96 {
97 Assert(
98 &src != &dst,
99 (typename SparseMatrix<double,
100 MemorySpace>::ExcSourceEqualsDestination()));
101 Assert(M.trilinos_matrix().isFillComplete(),
102 (typename SparseMatrix<double,
103 MemorySpace>::ExcMatrixNotCompressed()));
104
105 // get the size of the input vectors:
106 const size_type dst_local_size = dst.end() - dst.begin();
107 const size_type src_local_size = src.end() - src.begin();
108
109 // For the dst vector:
110 Kokkos::View<Number **, Kokkos::LayoutLeft, Kokkos::HostSpace>
111 kokkos_view_dst(dst.begin(), dst_local_size, 1);
112
113 // get a Kokkos::DualView
114 auto mirror_view_dst = Kokkos::create_mirror_view_and_copy(
115 typename MemorySpace::kokkos_space{}, kokkos_view_dst);
116 typename TpetraTypes::VectorType<Number, MemorySpace>::dual_view_type
117 kokkos_dual_view_dst(mirror_view_dst, kokkos_view_dst);
118
119 // create the Tpetra::Vector
120 typename TpetraTypes::VectorType<Number, MemorySpace> tpetra_dst(
121 M.trilinos_matrix().getRangeMap(), kokkos_dual_view_dst);
122
123 // For the src vector:
124 // create a Kokkos::View from the src vector
125 Kokkos::View<Number **, Kokkos::LayoutLeft, Kokkos::HostSpace>
126 kokkos_view_src(const_cast<Number *>(src.begin()), src_local_size, 1);
127
128 // get a Kokkos::DualView
129 auto mirror_view_src = Kokkos::create_mirror_view_and_copy(
130 typename MemorySpace::kokkos_space{}, kokkos_view_src);
131 typename TpetraTypes::VectorType<Number, MemorySpace>::dual_view_type
132 kokkos_dual_view_src(mirror_view_src, kokkos_view_src);
133
134 // create the Tpetra::Vector
135 typename TpetraTypes::VectorType<Number, MemorySpace> tpetra_src(
136 M.trilinos_matrix().getDomainMap(), kokkos_dual_view_src);
137
138 M.trilinos_matrix().apply(tpetra_src, tpetra_dst, mode, alpha, beta);
139 }
140
141
142
143 template <typename Number, typename MemorySpace, typename VectorType>
144 void
145 apply(SparseMatrix<Number, MemorySpace> &,
146 const VectorType &,
147 VectorType &,
148 Teuchos::ETransp,
149 Number,
150 Number)
151 {
152 DEAL_II_NOT_IMPLEMENTED();
153 }
154 } // namespace internal
155
156
157
158 // reinit_matrix():
159 namespace SparseMatrixImpl
160 {
161 using size_type = dealii::types::signed_global_dof_index;
162
163 template <typename Number,
164 typename MemorySpace,
165 typename SparsityPatternType>
166 void
167 reinit_matrix(
168 const IndexSet &row_parallel_partitioning,
169 const IndexSet &column_parallel_partitioning,
170 const SparsityPatternType &sparsity_pattern,
171 const bool exchange_data,
172 const MPI_Comm communicator,
173 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> &column_space_map,
174 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>> &matrix)
175 {
176 // release memory before reallocation
177 matrix.reset();
178
179 // Get the Tpetra::Maps
180 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> row_space_map =
181 row_parallel_partitioning
182 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
183 communicator, false);
184
185 column_space_map =
186 column_parallel_partitioning
187 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
188 communicator, false);
189
190 if (column_space_map->getComm()->getRank() == 0)
191 {
192 AssertDimension(sparsity_pattern.n_rows(),
193 row_parallel_partitioning.size());
194 AssertDimension(sparsity_pattern.n_cols(),
195 column_parallel_partitioning.size());
196 }
197
198 // if we want to exchange data, build a usual Trilinos sparsity pattern
199 // and let that handle the exchange. otherwise, manually create a
200 // CrsGraph, which consumes considerably less memory because it can set
201 // correct number of indices right from the start
202 if (exchange_data)
203 {
204 SparsityPattern<MemorySpace> trilinos_sparsity;
205 trilinos_sparsity.reinit(row_parallel_partitioning,
206 column_parallel_partitioning,
207 sparsity_pattern,
208 communicator,
209 exchange_data);
210 matrix = Utilities::Trilinos::internal::make_rcp<
211 TpetraTypes::MatrixType<Number, MemorySpace>>(
212 trilinos_sparsity.trilinos_sparsity_pattern());
213
214 return;
215 }
216
217 // compute the number of entries per row
218 const size_type first_row = row_space_map->getMinGlobalIndex();
219 const size_type last_row = row_space_map->getMaxGlobalIndex() + 1;
220
221 Teuchos::Array<size_t> n_entries_per_row(last_row - first_row);
222 for (size_type row = first_row; row < last_row; ++row)
223 n_entries_per_row[row - first_row] = sparsity_pattern.row_length(row);
224
225 // The deal.II notion of a 'sparsity pattern' corresponds to the
226 // Tpetra concept of a 'graph'. Hence, we generate a graph by copying
227 // the sparsity pattern into it, and then build up the matrix from the
228 // graph. This is considerable faster than directly filling elements
229 // into the matrix. Moreover, it consumes less memory, since the
230 // internal reordering is done on ints only, and we can leave the
231 // doubles aside.
232 Teuchos::RCP<TpetraTypes::GraphType<MemorySpace>> graph =
233 Utilities::Trilinos::internal::make_rcp<
234 TpetraTypes::GraphType<MemorySpace>>(row_space_map,
235 n_entries_per_row);
236
237 // This functions assumes that the sparsity pattern sits on all
238 // processors (completely). The parallel version uses a Tpetra graph
239 // that is already distributed.
240
241 // now insert the indices
242 Teuchos::Array<TrilinosWrappers::types::int_type> row_indices;
243
244 for (size_type global_row = first_row; global_row < last_row;
245 ++global_row)
246 {
247 const int row_length = sparsity_pattern.row_length(global_row);
248 if (row_length == 0)
249 continue;
250
251 row_indices.resize(row_length, -1);
252 for (size_type col = 0; col < row_length; ++col)
253 row_indices[col] =
254 sparsity_pattern.column_number(global_row, col);
255
256 AssertIndexRange(global_row, row_space_map->getGlobalNumElements());
257 graph->insertGlobalIndices(global_row, row_indices);
258 }
259
260 // Eventually, optimize the graph structure (sort indices, make memory
261 // contiguous, etc.). note that the documentation of the function indeed
262 // states that we first need to provide the column (domain) map and then
263 // the row (range) map
264 graph->fillComplete(column_space_map, row_space_map);
265
266 // check whether we got the number of columns right.
267 AssertDimension(sparsity_pattern.n_cols(), graph->getGlobalNumCols());
268
269 // And now finally generate the matrix.
270 matrix = Utilities::Trilinos::internal::make_rcp<
271 TpetraTypes::MatrixType<Number, MemorySpace>>(graph);
272 }
273
274
275
276 template <typename Number, typename MemorySpace>
277 void
278 reinit_matrix(
279 const IndexSet &row_parallel_partitioning,
280 const IndexSet &column_parallel_partitioning,
281 const DynamicSparsityPattern &sparsity_pattern,
282 const bool exchange_data,
283 const MPI_Comm communicator,
284 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> &column_space_map,
285 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>> &matrix)
286 {
287 // release memory before reallocation
288 matrix.reset();
289
290 // Get the Tpetra::Maps
291 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> row_space_map =
292 row_parallel_partitioning
293 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
294 communicator, false);
295
296 column_space_map =
297 column_parallel_partitioning
298 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
299 communicator, false);
300
301 if (column_space_map->getComm()->getRank() == 0)
302 {
303 AssertDimension(sparsity_pattern.n_rows(),
304 row_parallel_partitioning.size());
305 AssertDimension(sparsity_pattern.n_cols(),
306 column_parallel_partitioning.size());
307 }
308
309 // if we want to exchange data, build a usual Trilinos sparsity pattern
310 // and let that handle the exchange. otherwise, manually create a
311 // CrsGraph, which consumes considerably less memory because it can set
312 // correct number of indices right from the start
313 if (exchange_data)
314 {
315 SparsityPattern<MemorySpace> trilinos_sparsity;
316 trilinos_sparsity.reinit(row_parallel_partitioning,
317 column_parallel_partitioning,
318 sparsity_pattern,
319 communicator,
320 exchange_data);
321 matrix = Utilities::Trilinos::internal::make_rcp<
322 TpetraTypes::MatrixType<Number, MemorySpace>>(
323 trilinos_sparsity.trilinos_sparsity_pattern());
324
325 return;
326 }
327
328 IndexSet relevant_rows(sparsity_pattern.row_index_set());
329 // serial case
330 if (relevant_rows.size() == 0)
331 {
332 relevant_rows.set_size(row_space_map->getGlobalNumElements());
333 relevant_rows.add_range(0, row_space_map->getGlobalNumElements());
334 }
335 relevant_rows.compress();
336
337
338 std::vector<TrilinosWrappers::types::int_type> ghost_rows;
339 Teuchos::Array<size_t> n_entries_per_row(
340 # if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
341 row_space_map->getLocalNumElements());
342 # else
343 row_space_map->getNodeNumElements());
344 # endif
345 {
346 size_type own = 0;
347 for (const auto global_row : relevant_rows)
348 {
349 if (row_space_map->isNodeGlobalElement(global_row))
350 n_entries_per_row[own++] =
351 sparsity_pattern.row_length(global_row);
352 }
353 }
354
355 // The deal.II notation of a Sparsity pattern corresponds to the Tpetra
356 // concept of a Graph. Hence, we generate a graph by copying the
357 // sparsity pattern into it, and then build up the matrix from the
358 // graph. This is considerable faster than directly filling elements
359 // into the matrix. Moreover, it consumes less memory, since the
360 // internal reordering is done on ints only, and we can leave the
361 // doubles aside.
362 Teuchos::RCP<TpetraTypes::GraphType<MemorySpace>> graph =
363 Utilities::Trilinos::internal::make_rcp<
364 TpetraTypes::GraphType<MemorySpace>>(row_space_map,
365 n_entries_per_row);
366
367 // This functions assumes that the sparsity pattern sits on all
368 // processors (completely). The parallel version uses a Tpetra graph
369 // that is already distributed.
370
371 // now insert the indices
372 Teuchos::Array<TrilinosWrappers::types::int_type> row_indices;
373
374 for (const auto global_row : relevant_rows)
375 {
376 const int row_length = sparsity_pattern.row_length(global_row);
377 if (row_length == 0)
378 continue;
379
380 row_indices.resize(row_length, -1);
381 for (size_type col = 0; col < row_length; ++col)
382 row_indices[col] =
383 sparsity_pattern.column_number(global_row, col);
384
385 AssertIndexRange(global_row, row_space_map->getGlobalNumElements());
386 graph->insertGlobalIndices(global_row, row_indices);
387 }
388
389 // Eventually, optimize the graph structure (sort indices, make memory
390 // contiguous, etc.). note that the documentation of the function indeed
391 // states that we first need to provide the column (domain) map and then
392 // the row (range) map
393 graph->fillComplete(column_space_map, row_space_map);
394
395 // check whether we got the number of columns right.
396 AssertDimension(sparsity_pattern.n_cols(), graph->getGlobalNumCols());
397
398 // And now finally generate the matrix.
399 matrix = Utilities::Trilinos::internal::make_rcp<
400 TpetraTypes::MatrixType<Number, MemorySpace>>(graph);
401 }
402 } // namespace SparseMatrixImpl
403
404
405
406 // Constructors and initialization:
407
408 // The constructor is actually the only point where we have to check
409 // whether we build a serial or a parallel Trilinos matrix.
410 // Actually, it does not even matter how many threads there are, but
411 // only if we use an MPI compiler or a standard compiler. So, even one
412 // thread on a configuration with MPI will still get a parallel interface.
413 template <typename Number, typename MemorySpace>
414 SparseMatrix<Number, MemorySpace>::SparseMatrix()
415 : column_space_map(Utilities::Trilinos::internal::make_rcp<
416 TpetraTypes::MapType<MemorySpace>>(
417 0,
418 0,
419 Utilities::Trilinos::tpetra_comm_self()))
420 {
421 // Prepare the graph
422 Teuchos::RCP<TpetraTypes::GraphType<MemorySpace>> graph =
423 Utilities::Trilinos::internal::make_rcp<
424 TpetraTypes::GraphType<MemorySpace>>(column_space_map,
425 column_space_map,
426 0);
427 graph->fillComplete();
428
429 // Create the matrix from the graph
430 matrix = Utilities::Trilinos::internal::make_rcp<
431 TpetraTypes::MatrixType<Number, MemorySpace>>(graph);
432
433 compressed = false;
434 }
435
436
437
438 template <typename Number, typename MemorySpace>
439 SparseMatrix<Number, MemorySpace>::SparseMatrix(
440 const SparsityPattern<MemorySpace> &sparsity_pattern)
441 : matrix(Utilities::Trilinos::internal::make_rcp<
442 TpetraTypes::MatrixType<Number, MemorySpace>>(
443 sparsity_pattern.trilinos_sparsity_pattern()))
444 {
445 column_space_map =
446 Teuchos::rcp_const_cast<TpetraTypes::MapType<MemorySpace>>(
447 sparsity_pattern.domain_partitioner());
448 compressed = false;
449 compress(VectorOperation::add);
450 }
451
452
453
454 template <typename Number, typename MemorySpace>
455 SparseMatrix<Number, MemorySpace>::SparseMatrix(
456 const size_type m,
457 const size_type n,
458 const unsigned int n_max_entries_per_row)
459 : column_space_map(Utilities::Trilinos::internal::make_rcp<
460 TpetraTypes::MapType<MemorySpace>>(
461 static_cast<dealii::types::signed_global_dof_index>(n),
462 0,
463 Utilities::Trilinos::tpetra_comm_self()))
464 , matrix(Utilities::Trilinos::internal::make_rcp<
465 TpetraTypes::MatrixType<Number, MemorySpace>>(
466 Utilities::Trilinos::internal::make_rcp<
467 TpetraTypes::MapType<MemorySpace>>(
468 static_cast<dealii::types::signed_global_dof_index>(m),
469 0,
470 Utilities::Trilinos::tpetra_comm_self()),
471 column_space_map,
472 n_max_entries_per_row))
473 , compressed(false)
474 {}
475
476
477
478 template <typename Number, typename MemorySpace>
479 SparseMatrix<Number, MemorySpace>::SparseMatrix(
480 const size_type m,
481 const size_type n,
482 const std::vector<unsigned int> &n_entries_per_row)
483 : column_space_map(Utilities::Trilinos::internal::make_rcp<
484 TpetraTypes::MapType<MemorySpace>>(
485 n,
486 0,
487 Utilities::Trilinos::tpetra_comm_self()))
488 , compressed(false)
489 {
490 std::vector<size_t> entries_per_row_size_type(n_entries_per_row.begin(),
491 n_entries_per_row.end());
492 matrix = Utilities::Trilinos::internal::make_rcp<
493 TpetraTypes::MatrixType<Number, MemorySpace>>(
494 Utilities::Trilinos::internal::make_rcp<
495 TpetraTypes::MapType<MemorySpace>>(
496 m, 0, Utilities::Trilinos::tpetra_comm_self()),
497 column_space_map,
498 Teuchos::ArrayView<size_t>{entries_per_row_size_type});
499 }
500
501
502
503 template <typename Number, typename MemorySpace>
504 SparseMatrix<Number, MemorySpace>::SparseMatrix(
505 SparseMatrix<Number, MemorySpace> &&other) noexcept
506 : column_space_map(std::move(other.column_space_map))
507 , matrix(std::move(other.matrix))
508 , compressed(std::move(other.compressed))
509 {
510 other.compressed = false;
511 }
512
513
514
515 template <typename Number, typename MemorySpace>
516 SparseMatrix<Number, MemorySpace> &
517 SparseMatrix<Number, MemorySpace>::operator=(
518 SparseMatrix<Number, MemorySpace> &&other) noexcept
519 {
520 column_space_map = std::move(other.column_space_map);
521 matrix = std::move(other.matrix);
522 compressed = std::move(other.compressed);
523
524 return *this;
525 }
526
527
528
529 template <typename Number, typename MemorySpace>
530 template <typename SparsityPatternType>
531 void
532 SparseMatrix<Number, MemorySpace>::reinit(
533 const SparsityPatternType &sparsity_pattern)
534 {
535 SparseMatrixImpl::reinit_matrix<Number, MemorySpace, SparsityPatternType>(
536 complete_index_set(sparsity_pattern.n_rows()),
537 complete_index_set(sparsity_pattern.n_cols()),
538 sparsity_pattern,
539 false,
540 MPI_COMM_SELF,
541 column_space_map,
542 matrix);
543
544 compressed = false;
545 compress(VectorOperation::add);
546 }
547
548
549
550 template <typename Number, typename MemorySpace>
551 void
552 SparseMatrix<Number, MemorySpace>::reinit(
553 const SparsityPattern<MemorySpace> &sparsity_pattern)
554 {
555 column_space_map.reset();
556 matrix.reset();
557
558 // reinit with a (distributed) Trilinos sparsity pattern.
559 column_space_map =
560 Teuchos::rcp_const_cast<TpetraTypes::MapType<MemorySpace>>(
561 sparsity_pattern.domain_partitioner());
562 matrix = Utilities::Trilinos::internal::make_rcp<
563 TpetraTypes::MatrixType<Number, MemorySpace>>(
564 sparsity_pattern.trilinos_sparsity_pattern());
565
566 compressed = false;
567 compress(VectorOperation::add);
568 }
569
570
571
572 // Constructors and initialization using an IndexSet description:
573
574 template <typename Number, typename MemorySpace>
575 SparseMatrix<Number, MemorySpace>::SparseMatrix(
576 const IndexSet &parallel_partitioning,
577 const MPI_Comm communicator,
578 const unsigned int n_max_entries_per_row)
579 : column_space_map(
580 parallel_partitioning.template make_tpetra_map_rcp<
581 TpetraTypes::NodeType<MemorySpace>>(communicator, false))
582 , matrix(Utilities::Trilinos::internal::make_rcp<
583 TpetraTypes::MatrixType<Number, MemorySpace>>(
584 column_space_map,
585 n_max_entries_per_row))
586 , compressed(false)
587 {}
588
589
590
591 template <typename Number, typename MemorySpace>
592 SparseMatrix<Number, MemorySpace>::SparseMatrix(
593 const IndexSet &parallel_partitioning,
594 const MPI_Comm communicator,
595 const std::vector<unsigned int> &n_entries_per_row)
596 : column_space_map(
597 parallel_partitioning.template make_tpetra_map_rcp<
598 TpetraTypes::NodeType<MemorySpace>>(communicator, false))
599 , compressed(false)
600 {
601 Teuchos::Array<size_t> n_entries_per_row_array(n_entries_per_row.begin(),
602 n_entries_per_row.end());
603 matrix = Utilities::Trilinos::internal::make_rcp<
604 TpetraTypes::MatrixType<Number, MemorySpace>>(column_space_map,
605 n_entries_per_row_array);
606 }
607
608
609
610 template <typename Number, typename MemorySpace>
611 SparseMatrix<Number, MemorySpace>::SparseMatrix(
612 const IndexSet &row_parallel_partitioning,
613 const IndexSet &col_parallel_partitioning,
614 const MPI_Comm communicator,
615 const size_type n_max_entries_per_row)
616 : column_space_map(
617 col_parallel_partitioning.template make_tpetra_map_rcp<
618 TpetraTypes::NodeType<MemorySpace>>(communicator, false))
619 , matrix(Utilities::Trilinos::internal::make_rcp<
620 TpetraTypes::MatrixType<Number, MemorySpace>>(
621 row_parallel_partitioning.template make_tpetra_map_rcp<
622 TpetraTypes::NodeType<MemorySpace>>(communicator, false),
623 n_max_entries_per_row))
624 , compressed(false)
625 {}
626
627
628
629 template <typename Number, typename MemorySpace>
630 SparseMatrix<Number, MemorySpace>::SparseMatrix(
631 const IndexSet &row_parallel_partitioning,
632 const IndexSet &col_parallel_partitioning,
633 const MPI_Comm communicator,
634 const std::vector<unsigned int> &n_entries_per_row)
635 : column_space_map(
636 col_parallel_partitioning.template make_tpetra_map_rcp<
637 TpetraTypes::NodeType<MemorySpace>>(communicator, false))
638 , compressed(false)
639 {
640 Teuchos::Array<size_t> n_entries_per_row_array(n_entries_per_row.begin(),
641 n_entries_per_row.end());
642
643 matrix = Utilities::Trilinos::internal::make_rcp<
644 TpetraTypes::MatrixType<Number, MemorySpace>>(
645 row_parallel_partitioning
646 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
647 communicator, false),
648 n_entries_per_row_array);
649 }
650
651
652
653 template <typename Number, typename MemorySpace>
654 template <typename SparsityPatternType>
655 std::enable_if_t<
656 !std::is_same_v<SparsityPatternType, dealii::SparseMatrix<double>>>
657 SparseMatrix<Number, MemorySpace>::reinit(
658 const IndexSet &parallel_partitioning,
659 const SparsityPatternType &sparsity_pattern,
660 const MPI_Comm communicator,
661 const bool exchange_data)
662 {
663 reinit(parallel_partitioning,
664 parallel_partitioning,
665 sparsity_pattern,
666 communicator,
667 exchange_data);
668 }
669
670
671
672 template <typename Number, typename MemorySpace>
673 template <typename SparsityPatternType>
674 std::enable_if_t<
675 !std::is_same_v<SparsityPatternType, dealii::SparseMatrix<double>>>
676 SparseMatrix<Number, MemorySpace>::reinit(
677 const IndexSet &row_parallel_partitioning,
678 const IndexSet &col_parallel_partitioning,
679 const SparsityPatternType &sparsity_pattern,
680 const MPI_Comm communicator,
681 const bool exchange_data)
682 {
683 SparseMatrixImpl::reinit_matrix<Number, MemorySpace, SparsityPatternType>(
684 row_parallel_partitioning,
685 col_parallel_partitioning,
686 sparsity_pattern,
687 exchange_data,
688 communicator,
689 column_space_map,
690 matrix);
691
692 compressed = false;
693 compress(VectorOperation::add);
694 }
695
696
697
698 template <typename Number, typename MemorySpace>
699 void
700 SparseMatrix<Number, MemorySpace>::reinit(
701 const IndexSet &row_parallel_partitioning,
702 const IndexSet &col_parallel_partitioning,
703 const dealii::SparseMatrix<Number> &dealii_sparse_matrix,
704 const MPI_Comm communicator,
705 const double drop_tolerance,
706 const bool copy_values,
707 const dealii::SparsityPattern *use_this_sparsity)
708 {
709 const size_type n_rows = dealii_sparse_matrix.m();
710 AssertDimension(row_parallel_partitioning.size(), n_rows);
711 AssertDimension(col_parallel_partitioning.size(),
712 dealii_sparse_matrix.n());
713
714 const dealii::SparsityPattern &sparsity_pattern =
715 (use_this_sparsity != nullptr) ?
716 *use_this_sparsity :
717 dealii_sparse_matrix.get_sparsity_pattern();
718
719 if (matrix.is_null() || m() != n_rows ||
720 n_nonzero_elements() != sparsity_pattern.n_nonzero_elements() ||
721 copy_values)
722 if (use_this_sparsity == nullptr)
723 reinit(row_parallel_partitioning,
724 col_parallel_partitioning,
725 sparsity_pattern,
726 communicator,
727 false);
728
729 // in case we do not copy values, we are done
730 if (copy_values == false)
731 return;
732
733 // fill the values: go through all rows of the
734 // matrix, and then all columns. since the sparsity patterns of the
735 // input matrix and the specified sparsity pattern might be different,
736 // need to go through the row for both these sparsity structures
737 // simultaneously in order to really set the correct values.
738 # if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
739 size_type maximum_row_length = matrix->getLocalMaxNumRowEntries();
740 # else
741 size_type maximum_row_length = matrix->getNodeMaxNumRowEntries();
742 # endif
743 std::vector<size_type> row_indices(maximum_row_length);
744 std::vector<Number> values(maximum_row_length);
745
746 for (size_type row = 0; row < n_rows; ++row)
747 // see if the row is locally stored on this processor
748 if (row_parallel_partitioning.is_element(row) == true)
749 {
750 dealii::SparsityPattern::iterator select_index =
751 sparsity_pattern.begin(row);
752 typename dealii::SparseMatrix<Number>::const_iterator it =
753 dealii_sparse_matrix.begin(row);
754 size_type col = 0;
755 if (sparsity_pattern.n_rows() == sparsity_pattern.n_cols())
756 {
757 // optimized diagonal
758 AssertDimension(it->column(), row);
759 if (std::fabs(it->value()) > drop_tolerance)
760 {
761 values[col] = it->value();
762 row_indices[col++] = it->column();
763 }
764 ++select_index;
765 ++it;
766 }
767
768 while (it != dealii_sparse_matrix.end(row) &&
769 select_index != sparsity_pattern.end(row))
770 {
771 while (select_index->column() < it->column() &&
772 select_index != sparsity_pattern.end(row))
773 ++select_index;
774 while (it->column() < select_index->column() &&
775 it != dealii_sparse_matrix.end(row))
776 ++it;
777
778 if (it == dealii_sparse_matrix.end(row))
779 break;
780 if (std::fabs(it->value()) > drop_tolerance)
781 {
782 values[col] = it->value();
783 row_indices[col++] = it->column();
784 }
785 ++select_index;
786 ++it;
787 }
788 set(row,
789 col,
790 reinterpret_cast<size_type *>(row_indices.data()),
791 values.data(),
792 false);
793 }
794
795 compress(VectorOperation::insert);
796 }
797
798
799
800 // Information on the matrix
801
802 template <typename Number, typename MemorySpace>
803 inline unsigned int
804 SparseMatrix<Number, MemorySpace>::local_size() const
805 {
806 # if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
807 return matrix->getLocalNumRows();
808 # else
809 return matrix->getNodeNumRows();
810 # endif
811 }
812
813
814
815 template <typename Number, typename MemorySpace>
816 inline std::pair<typename SparseMatrix<Number, MemorySpace>::size_type,
817 typename SparseMatrix<Number, MemorySpace>::size_type>
818 SparseMatrix<Number, MemorySpace>::local_range() const
819 {
820 size_type begin, end;
821 begin = matrix->getRowMap()->getMinLocalIndex();
822 end = matrix->getRowMap()->getMaxLocalIndex() + 1;
823
824 return std::make_pair(begin, end);
825 }
826
827
828
829 template <typename Number, typename MemorySpace>
830 inline size_t
831 SparseMatrix<Number, MemorySpace>::n_nonzero_elements() const
832 {
833 return matrix->getGlobalNumEntries();
834 }
835
836
837
838 template <typename Number, typename MemorySpace>
839 MPI_Comm
840 SparseMatrix<Number, MemorySpace>::get_mpi_communicator() const
841 {
842 return Utilities::Trilinos::teuchos_comm_to_mpi_comm(matrix->getComm());
843 }
844
845
846
847 // Modifying entries
848
849 template <typename Number, typename MemorySpace>
850 SparseMatrix<Number, MemorySpace> &
851 SparseMatrix<Number, MemorySpace>::operator=(const double d)
852 {
853 Assert(d == 0, ExcScalarAssignmentOnlyForZeroValue());
854
855 if (compressed)
856 {
857 matrix->resumeFill();
858 compressed = false;
859 }
860
861 // As checked above, we are only allowed to use d==0.0, so pass
862 // a constant zero (instead of a run-time value 'd' that *happens* to
863 // have a zero value) to the underlying class in hopes that the compiler
864 // can optimize this somehow.
865 matrix->setAllToScalar(/*d=*/0.0);
866
867 return *this;
868 }
869
870
871
872 template <typename Number, typename MemorySpace>
873 SparseMatrix<Number, MemorySpace> &
874 SparseMatrix<Number, MemorySpace>::operator*=(const Number a)
875 {
876 if (compressed)
877 {
878 matrix->resumeFill();
879 compressed = false;
880 }
881
882 matrix->scale(a);
883
884 matrix->fillComplete();
885 compressed = true;
886
887 return *this;
888 }
889
890
891
892 template <typename Number, typename MemorySpace>
893 SparseMatrix<Number, MemorySpace> &
894 SparseMatrix<Number, MemorySpace>::operator/=(const Number a)
895 {
896 Assert(a != 0, ExcDivideByZero());
897
898 if (compressed)
899 {
900 matrix->resumeFill();
901 compressed = false;
902 }
903
904 const Number factor = 1.0 / a;
905 matrix->scale(factor);
906
907 matrix->fillComplete();
908 compressed = true;
909
910 return *this;
911 }
912
913
914
915 template <typename Number, typename MemorySpace>
916 Number
917 SparseMatrix<Number, MemorySpace>::matrix_norm_square(
918 const Vector<Number, MemorySpace> &v) const
919 {
920 AssertDimension(m(), v.size());
921 Assert(matrix->isFillComplete(), ExcMatrixNotCompressed());
922 Assert(matrix->getRowMap()->isSameAs(*matrix->getDomainMap()),
923 ExcNotQuadratic());
924
925 Vector<Number, MemorySpace> temp_vector;
926 temp_vector.reinit(v, true);
927
928 vmult(temp_vector, v);
929 return temp_vector * v;
930 }
931
932
933
934 template <typename Number, typename MemorySpace>
935 Number
936 SparseMatrix<Number, MemorySpace>::matrix_scalar_product(
937 const Vector<Number, MemorySpace> &u,
938 const Vector<Number, MemorySpace> &v) const
939 {
940 AssertDimension(m(), u.size());
941 AssertDimension(m(), v.size());
942 Assert(matrix->isFillComplete(), ExcMatrixNotCompressed());
943 Assert(matrix->getRowMap()->isSameAs(*matrix->getDomainMap()),
944 ExcNotQuadratic());
945
946 Vector<Number, MemorySpace> temp_vector;
947 temp_vector.reinit(v, true);
948
949 vmult(temp_vector, v);
950 return u * temp_vector;
951 }
952
953
954
955 template <typename Number, typename MemorySpace>
956 Number
957 SparseMatrix<Number, MemorySpace>::frobenius_norm() const
958 {
959 Assert(matrix->isFillComplete(), ExcMatrixNotCompressed());
960 return matrix->getFrobeniusNorm();
961 }
962
963
964
965 template <typename Number, typename MemorySpace>
966 void
967 SparseMatrix<Number, MemorySpace>::set(const size_type row,
968 const size_type n_cols,
969 const size_type *col_indices,
970 const Number *values,
971 const bool elide_zero_values)
972 {
973 AssertIndexRange(row, this->m());
974 const types::signed_global_dof_index *col_index_ptr;
975 const Number *col_value_ptr;
976 const types::signed_global_dof_index trilinos_row = row;
977 types::signed_global_dof_index n_columns;
978
979 boost::container::small_vector<Number, 200> local_value_array(
980 elide_zero_values ? n_cols : 0);
981 boost::container::small_vector<types::signed_global_dof_index, 200>
982 local_index_array(elide_zero_values ? n_cols : 0);
983
984 // If we don't elide zeros, the pointers are already available... need to
985 // cast to non-const pointers as that is the format taken by Trilinos (but
986 // we will not modify const data)
987 if (elide_zero_values == false)
988 {
989 col_index_ptr =
990 reinterpret_cast<const dealii::types::signed_global_dof_index *>(
991 col_indices);
992 col_value_ptr = values;
993 n_columns = n_cols;
994 }
995 else
996 {
997 // Otherwise, extract nonzero values in each row and get the
998 // respective indices.
999 col_index_ptr = local_index_array.data();
1000 col_value_ptr = local_value_array.data();
1001
1002 n_columns = 0;
1003 for (size_type j = 0; j < n_cols; ++j)
1004 {
1005 const double value = values[j];
1006 AssertIsFinite(value);
1007 if (value != 0)
1008 {
1009 local_index_array[n_columns] = col_indices[j];
1010 local_value_array[n_columns] = value;
1011 ++n_columns;
1012 }
1013 }
1014
1015 AssertIndexRange(n_columns, n_cols + 1);
1016 }
1017
1018 // We distinguish between two cases: the first one is when the matrix is
1019 // not filled (i.e., it is possible to add new elements to the sparsity
1020 // pattern), and the second one is when the pattern is already fixed. In
1021 // the former case, we add the possibility to insert new values, and in
1022 // the second we just replace data.
1023
1024 // If the matrix is marked as compressed, we need to
1025 // call resumeFill() first.
1026 if (compressed || matrix->isFillComplete())
1027 {
1028 matrix->resumeFill();
1029 compressed = false;
1030 }
1031
1032 if (!matrix->isStaticGraph())
1033 matrix->insertGlobalValues(trilinos_row,
1034 n_columns,
1035 col_value_ptr,
1036 col_index_ptr);
1037 else
1038 matrix->replaceGlobalValues(trilinos_row,
1039 n_columns,
1040 col_value_ptr,
1041 col_index_ptr);
1042 }
1043
1044
1045
1046 template <typename Number, typename MemorySpace>
1047 void
1048 SparseMatrix<Number, MemorySpace>::set(
1049 const std::vector<size_type> &indices,
1050 const FullMatrix<Number> &values,
1051 const bool elide_zero_values)
1052 {
1053 Assert(indices.size() == values.m(),
1054 ExcDimensionMismatch(indices.size(), values.m()));
1055 Assert(values.m() == values.n(), ExcNotQuadratic());
1056
1057 for (size_type i = 0; i < indices.size(); ++i)
1058 set(indices[i],
1059 indices.size(),
1060 indices.data(),
1061 &values(i, 0),
1062 elide_zero_values);
1063 }
1064
1065
1066
1067 template <typename Number, typename MemorySpace>
1068 void
1069 SparseMatrix<Number, MemorySpace>::set(
1070 const std::vector<size_type> &row_indices,
1071 const std::vector<size_type> &col_indices,
1072 const FullMatrix<Number> &full_matrix,
1073 const bool elide_zero_values)
1074 {
1075 Assert(row_indices.size() == full_matrix.m(),
1076 ExcDimensionMismatch(row_indices.size(), full_matrix.m()));
1077 Assert(col_indices.size() == full_matrix.n(),
1078 ExcDimensionMismatch(col_indices.size(), full_matrix.n()));
1079
1080 for (size_type i = 0; i < row_indices.size(); ++i)
1081 set(row_indices[i],
1082 col_indices.size(),
1083 col_indices.data(),
1084 &full_matrix(i, 0),
1085 elide_zero_values);
1086 }
1087
1088
1089
1090 template <typename Number, typename MemorySpace>
1091 void
1092 SparseMatrix<Number, MemorySpace>::set(
1093 const size_type row,
1094 const std::vector<size_type> &col_indices,
1095 const std::vector<Number> &values,
1096 const bool elide_zero_values)
1097 {
1098 Assert(col_indices.size() == values.size(),
1099 ExcDimensionMismatch(col_indices.size(), values.size()));
1100
1101 set(row,
1102 col_indices.size(),
1103 col_indices.data(),
1104 &values[0],
1105 elide_zero_values);
1106 }
1107
1108
1109
1110 template <typename Number, typename MemorySpace>
1111 void
1112 SparseMatrix<Number, MemorySpace>::add(
1113 const size_type row,
1114 const size_type n_cols,
1115 const size_type *col_indices,
1116 const Number *values,
1117 const bool elide_zero_values,
1118 const bool /*col_indices_are_sorted*/)
1119 {
1120 AssertIndexRange(row, this->m());
1121 for (size_t n = 0; n < n_cols; ++n)
1122 AssertIndexRange(col_indices[n], this->n());
1123
1124 // If the matrix is marked as compressed, we need to
1125 // call resumeFill() first.
1126 if (compressed || matrix->isFillComplete())
1127 {
1128 matrix->resumeFill();
1129 compressed = false;
1130 }
1131
1132 // count zero entries;
1133 const size_t n_zero_entries =
1134 (elide_zero_values ? std::count(values, values + n_cols, Number(0)) :
1135 0);
1136
1137 // Exit early if there is nothing to do
1138 if (n_zero_entries == n_cols)
1139 return;
1140
1141 // Convert the input into Teuchos::Array
1142 Teuchos::Array<types::signed_global_dof_index> col_indices_array(
1143 n_cols - n_zero_entries);
1144 Teuchos::Array<Number> values_array(n_cols - n_zero_entries);
1145 if (elide_zero_values)
1146 {
1147 size_t n_columns = 0;
1148 for (size_t i = 0; i < n_cols; ++i)
1149 {
1150 // skip all zero entries, while filling the
1151 if (values[i] != 0)
1152 {
1153 AssertIsFinite(values[i]);
1154 AssertIndexRange(n_columns, n_zero_entries);
1155 col_indices_array[n_columns] = col_indices[i];
1156 values_array[n_columns] = values[i];
1157 ++n_columns;
1158 }
1159 }
1160 }
1161 else
1162 for (size_t i = 0; i < n_cols; ++i)
1163 {
1164 AssertIsFinite(values[i]);
1165 col_indices_array[i] = col_indices[i];
1166 values_array[i] = values[i];
1167 }
1168
1169 // Sum the values into the global matrix.
1170 matrix->sumIntoGlobalValues(row, col_indices_array, values_array);
1171 }
1172
1173
1174
1175 template <typename Number, typename MemorySpace>
1176 void
1177 SparseMatrix<Number, MemorySpace>::add(
1178 const Number factor,
1179 const SparseMatrix<Number, MemorySpace> &source)
1180 {
1181 AssertDimension(source.m(), m());
1182 AssertDimension(source.n(), n());
1183 AssertDimension(source.local_range().first, local_range().first);
1184 AssertDimension(source.local_range().second, local_range().second);
1185 Assert(matrix->getRowMap()->isSameAs(*source.matrix->getRowMap()),
1186 ExcMessage(
1187 "Can only add matrices with same distribution of rows."));
1188 Assert(matrix->isFillComplete() && source.matrix->isFillComplete(),
1189 ExcMessage("Addition of matrices is only allowed if the matrices "
1190 "are filled, i.e., if compress() has been called."));
1191
1192 // Tpetra does not have a function that adds a matrix in-place to the
1193 // we're currently storing. But it (inefficiently) has one that returns a
1194 // new matrix with the result. As a consequence, replace the current one
1195 // by the one that is returned.
1196 //
1197 // Inconveniently, however, the Tpetra::CrsMatrix::add() function returns
1198 // a RCP<Tpetra::RowMatrix>, though it guarantees that the stored object
1199 // is actually a Tpetra::CrsMatrix for our combination of arguments. So,
1200 // we have to do some casting around to assign things back to 'matrix':
1201 auto result = matrix->add(factor,
1202 *source.matrix,
1203 1.0,
1204 matrix->getDomainMap(),
1205 matrix->getRangeMap(),
1206 Teuchos::null);
1207 // needs a local typedef or Assert thinks it gets more arguments than it
1208 // does...
1209 using MatrixType = TpetraTypes::MatrixType<Number, MemorySpace>;
1210 Assert(dynamic_cast<MatrixType *>(result.get()), ExcInternalError());
1211 matrix.reset(dynamic_cast<TpetraTypes::MatrixType<Number, MemorySpace> *>(
1212 result.release().get()));
1213 }
1214
1215
1216
1217 template <typename Number, typename MemorySpace>
1218 void
1219 SparseMatrix<Number, MemorySpace>::clear_row(const size_type row,
1220 const Number new_diag_value)
1221 {
1222 clear_rows(ArrayView<const size_type>(row), new_diag_value);
1223 }
1224
1225
1226
1227 template <typename Number, typename MemorySpace>
1228 void
1229 SparseMatrix<Number, MemorySpace>::clear_rows(
1230 const ArrayView<const size_type> &rows,
1231 const Number new_diag_value)
1232 {
1233 // If the matrix is marked as compressed, we need to
1234 // call resumeFill() first.
1235 if (compressed || matrix->isFillComplete())
1236 {
1237 matrix->resumeFill();
1238 compressed = false;
1239 }
1240
1241 std::vector<int> col_indices_vector;
1242 std::vector<Number> values_vector;
1243
1244 for (size_type row : rows)
1245 {
1246 // Only do this on the rows owned locally on this processor.
1247 int local_row = matrix->getRowMap()->getLocalElement(row);
1248 if (local_row != Teuchos::OrdinalTraits<int>::invalid())
1249 {
1250 size_t nnz = matrix->getNumEntriesInLocalRow(local_row);
1251 col_indices_vector.resize(nnz);
1252 values_vector.resize(nnz);
1253
1254 typename TpetraTypes::MatrixType<Number, MemorySpace>::
1255 nonconst_local_inds_host_view_type col_indices(
1256 col_indices_vector.data(), nnz);
1257 typename TpetraTypes::MatrixType<Number, MemorySpace>::
1258 nonconst_values_host_view_type values(values_vector.data(),
1259 nnz);
1260
1261 matrix->getLocalRowCopy(local_row, col_indices, values, nnz);
1262
1263 const size_t diag_index = std::find(col_indices_vector.begin(),
1264 col_indices_vector.end(),
1265 local_row) -
1266 col_indices_vector.begin();
1267
1268 for (size_t j = 0; j < nnz; ++j)
1269 if (diag_index != j || new_diag_value == 0)
1270 values[j] = 0.;
1271
1272 if (diag_index != nnz)
1273 values[diag_index] = new_diag_value;
1274
1275 [[maybe_unused]] int n_replacements =
1276 matrix->replaceLocalValues(local_row, col_indices, values);
1277 AssertDimension(n_replacements, nnz);
1278 }
1279 }
1280 compress(VectorOperation::insert);
1281 }
1282
1283
1284
1285 template <typename Number, typename MemorySpace>
1286 void
1287 SparseMatrix<Number, MemorySpace>::copy_from(
1288 const SparseMatrix<Number, MemorySpace> &source)
1289 {
1290 if (this == &source)
1291 return;
1292
1293 // release memory before reallocation
1294 matrix.reset();
1295 column_space_map.reset();
1296
1297 // TODO:
1298 // If the source and the target matrix have the same structure, we do
1299 // not need to perform a deep copy.
1300
1301 // Perform a deep copy
1302 matrix = Utilities::Trilinos::internal::make_rcp<
1303 TpetraTypes::MatrixType<Number, MemorySpace>>(*source.matrix,
1304 Teuchos::Copy);
1305
1306 column_space_map =
1307 Teuchos::rcp_const_cast<TpetraTypes::MapType<MemorySpace>>(
1308 matrix->getColMap());
1309 compressed = source.compressed;
1310 }
1311
1312
1313
1314 template <typename Number, typename MemorySpace>
1315 void
1316 SparseMatrix<Number, MemorySpace>::clear()
1317 {
1318 // When we clear the matrix, reset
1319 // the pointer and generate an
1320 // empty matrix.
1321 column_space_map = Utilities::Trilinos::internal::make_rcp<
1322 TpetraTypes::MapType<MemorySpace>>(
1323 0, 0, Utilities::Trilinos::tpetra_comm_self());
1324
1325 // Prepare the graph
1326 Teuchos::RCP<TpetraTypes::GraphType<MemorySpace>> graph =
1327 Utilities::Trilinos::internal::make_rcp<
1328 TpetraTypes::GraphType<MemorySpace>>(column_space_map,
1329 column_space_map,
1330 0);
1331 graph->fillComplete();
1332
1333 // Create the matrix from the graph
1334 matrix = Utilities::Trilinos::internal::make_rcp<
1335 TpetraTypes::MatrixType<Number, MemorySpace>>(graph);
1336
1337 compressed = true;
1338 }
1339
1340
1341
1342 // Multiplications
1343 template <typename Number, typename MemorySpace>
1344 template <typename InputVectorType>
1345 void
1346 SparseMatrix<Number, MemorySpace>::vmult(InputVectorType &dst,
1347 const InputVectorType &src) const
1348 {
1349 internal::apply(*this, src, dst);
1350 }
1351
1352
1353
1354 template <typename Number, typename MemorySpace>
1355 template <typename InputVectorType>
1356 void
1357 SparseMatrix<Number, MemorySpace>::Tvmult(InputVectorType &dst,
1358 const InputVectorType &src) const
1359 {
1360 internal::apply(*this, src, dst, Teuchos::TRANS);
1361 }
1362
1363
1364
1365 template <typename Number, typename MemorySpace>
1366 template <typename InputVectorType>
1367 void
1368 SparseMatrix<Number, MemorySpace>::vmult_add(
1369 InputVectorType &dst,
1370 const InputVectorType &src) const
1371 {
1372 internal::apply(*this,
1373 src,
1374 dst,
1375 Teuchos::NO_TRANS,
1376 Teuchos::ScalarTraits<Number>::one(),
1377 Teuchos::ScalarTraits<Number>::one());
1378 }
1379
1380
1381
1382 template <typename Number, typename MemorySpace>
1383 template <typename InputVectorType>
1384 void
1385 SparseMatrix<Number, MemorySpace>::Tvmult_add(
1386 InputVectorType &dst,
1387 const InputVectorType &src) const
1388 {
1389 internal::apply(*this,
1390 src,
1391 dst,
1392 Teuchos::TRANS,
1393 Teuchos::ScalarTraits<Number>::one(),
1394 Teuchos::ScalarTraits<Number>::one());
1395 }
1396
1397
1398
1399 template <typename Number, typename MemorySpace>
1400 void
1401 SparseMatrix<Number, MemorySpace>::print(
1402 std::ostream &out,
1403 const bool print_detailed_trilinos_information) const
1404 {
1405 if (print_detailed_trilinos_information)
1406 {
1407 auto teuchos_out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(out));
1408 matrix->describe(*teuchos_out, Teuchos::VERB_EXTREME);
1409 }
1410 else
1411 {
1412 typename TpetraTypes::MatrixType<Number,
1413 MemorySpace>::values_host_view_type
1414 values;
1415 typename TpetraTypes::MatrixType<Number, MemorySpace>::
1416 local_inds_host_view_type indices;
1417
1418 # if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
1419 for (size_t i = 0; i < matrix->getLocalNumRows(); ++i)
1420 # else
1421 for (size_t i = 0; i < matrix->getNodeNumRows(); ++i)
1422 # endif
1423 {
1424 matrix->getLocalRowView(i, indices, values);
1425
1426 for (size_type j = 0; j < static_cast<size_type>(indices.size());
1427 ++j)
1428 out << "(" << matrix->getRowMap()->getGlobalElement(i) << ","
1429 << matrix->getColMap()->getGlobalElement(indices[j]) << ") "
1430 << values[j] << std::endl;
1431 }
1432 }
1433
1434 AssertThrow(out.fail() == false, ExcIO());
1435 }
1436
1437
1438
1439 template <typename Number, typename MemorySpace>
1440 void
1441 SparseMatrix<Number, MemorySpace>::compress(VectorOperation::values)
1442 {
1443 if (!compressed)
1444 {
1445 matrix->fillComplete(column_space_map, matrix->getRowMap());
1446 compressed = true;
1447 }
1448 }
1449
1450
1451
1452 template <typename Number, typename MemorySpace>
1453 void
1454 SparseMatrix<Number, MemorySpace>::resume_fill()
1455 {
1456 if (compressed)
1457 {
1458 matrix->resumeFill();
1459 compressed = false;
1460 }
1461 }
1462
1463
1464 template <typename Number, typename MemorySpace>
1465 Number
1466 SparseMatrix<Number, MemorySpace>::element(const size_type i,
1467 const size_type j,
1468 const bool no_error) const
1469 {
1470 // Extract local indices in the matrix.
1471 const int trilinos_i = matrix->getRowMap()->getLocalElement(i);
1472 const int trilinos_j = matrix->getColMap()->getLocalElement(j);
1473 Number value = 0.;
1474
1475 if (trilinos_i == Teuchos::OrdinalTraits<int>::invalid() ||
1476 trilinos_j == Teuchos::OrdinalTraits<int>::invalid())
1477 {
1478 if (no_error)
1479 return {};
1480 Assert(false,
1481 ExcAccessToNonLocalElement(
1482 i, j, local_range().first, local_range().second - 1));
1483 }
1484 else
1485 {
1486 Assert(matrix->isFillComplete(), ExcMatrixNotCompressed());
1487
1488 // Prepare pointers for extraction of a view of the row.
1489 size_t nnz_present = matrix->getNumEntriesInLocalRow(trilinos_i);
1490
1491 typename TpetraTypes::MatrixType<Number, MemorySpace>::
1492 nonconst_local_inds_host_view_type col_indices("indices",
1493 nnz_present);
1494 typename TpetraTypes::MatrixType<Number, MemorySpace>::
1495 nonconst_values_host_view_type values("values", nnz_present);
1496
1497 matrix->getLocalRowCopy(trilinos_i, col_indices, values, nnz_present);
1498
1499 // Search the index where we look for the value, and then finally get
1500 // it.
1501 int local_col_index = 0;
1502 for (; local_col_index < static_cast<int>(nnz_present);
1503 ++local_col_index)
1504 {
1505 if (col_indices[local_col_index] == trilinos_j)
1506 break;
1507 }
1508
1509 if (local_col_index == static_cast<int>(nnz_present))
1510 {
1511 if (no_error)
1512 return {};
1513 Assert(false, ExcInvalidIndex(i, j));
1514 }
1515 else
1516 value = values[local_col_index];
1517 }
1518
1519 return value;
1520 }
1521
1522
1523
1524 template <typename Number, typename MemorySpace>
1525 Number
1526 SparseMatrix<Number, MemorySpace>::operator()(const size_type i,
1527 const size_type j) const
1528 {
1529 return element(i, j, /* no_error */ false);
1530 }
1531
1532
1533
1534 template <typename Number, typename MemorySpace>
1535 Number
1536 SparseMatrix<Number, MemorySpace>::el(const size_type i,
1537 const size_type j) const
1538 {
1539 return element(i, j, /* no_error */ true);
1540 }
1541
1542
1543
1544 template <typename Number, typename MemorySpace>
1545 Number
1546 SparseMatrix<Number, MemorySpace>::diag_element(const size_type i) const
1547 {
1548 Assert(m() == n(), ExcNotQuadratic());
1549
1550 if constexpr (running_in_debug_mode())
1551 {
1552 // use operator() in debug mode because it checks if this is a valid
1553 // element (in parallel)
1554 return operator()(i, i);
1555 }
1556 else
1557 {
1558 // Trilinos doesn't seem to have a more efficient way to access the
1559 // diagonal than by just using the standard el(i,j) function.
1560 return el(i, i);
1561 }
1562 }
1563 } // namespace TpetraWrappers
1564
1565 } // namespace LinearAlgebra
1566
1567 DEAL_II_NAMESPACE_CLOSE
1568
1569 #else
1570
1571 // Make sure the scripts that create the C++20 module input files have
1572 // something to latch on if the preprocessor #ifdef above would
1573 // otherwise lead to an empty content of the file.
1574 DEAL_II_NAMESPACE_OPEN
1575 DEAL_II_NAMESPACE_CLOSE
1576
1577 #endif // DEAL_II_TRILINOS_WITH_TPETRA
1578
1579 #endif // dealii_trilinos_tpetra_sparse_matrix_templates_h