Reference documentation for deal.II version 9.6.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
read_write_vector.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2015 - 2024 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_read_write_vector_h
16#define dealii_read_write_vector_h
17
18#include <deal.II/base/config.h>
19
28#include <deal.II/base/types.h>
29
32
33#include <cstdlib>
34#include <cstring>
35#include <iomanip>
36
37#ifdef DEAL_II_WITH_TRILINOS
41
42# include <Epetra_MultiVector.h>
43
44#endif
45
47
48// Forward declarations
49#ifndef DOXYGEN
50template <typename>
51class Vector;
52
53namespace LinearAlgebra
54{
55 template <typename>
56 class Vector;
57 namespace distributed
58 {
59 template <typename, typename>
60 class Vector;
61 } // namespace distributed
62} // namespace LinearAlgebra
63
64# ifdef DEAL_II_WITH_PETSC
65namespace PETScWrappers
66{
67 namespace MPI
68 {
69 class Vector;
70 }
71} // namespace PETScWrappers
72# endif
73
74# ifdef DEAL_II_WITH_TRILINOS
75namespace TrilinosWrappers
76{
77 namespace MPI
78 {
79 class Vector;
80 }
81} // namespace TrilinosWrappers
82# endif
83
84# ifdef DEAL_II_WITH_CUDA
85namespace LinearAlgebra
86{
87 namespace CUDAWrappers
88 {
89 template <typename>
90 class Vector;
91 }
92} // namespace LinearAlgebra
93# endif
94#endif
95
96namespace LinearAlgebra
97{
135 template <typename Number>
136 class ReadWriteVector : public Subscriptor, public ReadVector<Number>
137 {
138 public:
144 using value_type = Number;
146 using const_pointer = const value_type *;
148 using const_iterator = const value_type *;
153
162
167
173
178 explicit ReadWriteVector(const IndexSet &locally_stored_indices);
179
183 ~ReadWriteVector() override = default;
184
193 virtual void
194 reinit(const size_type size, const bool omit_zeroing_entries = false);
195
204 template <typename Number2>
205 void
207 const bool omit_zeroing_entries = false);
208
218 virtual void
219 reinit(const IndexSet &locally_stored_indices,
220 const bool omit_zeroing_entries = false);
221
222
223#ifdef DEAL_II_WITH_TRILINOS
235 void
237#endif
238
252 template <typename Functor>
253 void
254 apply(const Functor &func);
255
268 void
270
276
280 template <typename Number2>
283
289 operator=(const Number s);
290
300 void
302 const ::Vector<Number> &vec,
303 VectorOperation::values operation,
304 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
305 &communication_pattern = {});
306
311 void
312 import(const ::Vector<Number> &V,
313 VectorOperation::values operation,
314 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
315 &communication_pattern = {})
316 {
317 import_elements(V, operation, communication_pattern);
318 }
319
328 template <typename MemorySpace>
329 void
332 VectorOperation::values operation,
333 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
334 &communication_pattern = {});
335
339 template <typename MemorySpace>
342 VectorOperation::values operation,
343 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
344 &communication_pattern = {})
345 {
346 import_elements(V, operation, communication_pattern);
347 }
348
349
350#ifdef DEAL_II_WITH_PETSC
359 void
361 const PETScWrappers::MPI::Vector &petsc_vec,
362 VectorOperation::values operation,
363 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
364 &communication_pattern = {});
365
370 void
372 VectorOperation::values operation,
373 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
374 &communication_pattern = {})
375 {
376 import_elements(V, operation, communication_pattern);
377 }
378#endif
379
380#ifdef DEAL_II_WITH_TRILINOS
391 void
393 const TrilinosWrappers::MPI::Vector &trilinos_vec,
394 VectorOperation::values operation,
395 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
396 &communication_pattern = {});
397
402 void
404 VectorOperation::values operation,
405 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
406 &communication_pattern = {})
407 {
408 import_elements(V, operation, communication_pattern);
409 }
410
411# ifdef DEAL_II_TRILINOS_WITH_TPETRA
420 template <typename MemorySpace, typename Dummy = Number>
421 std::enable_if_t<std::is_same_v<Dummy, Number> &&
425 VectorOperation::values operation,
426 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
427 &communication_pattern = {});
428
432 template <typename MemorySpace, typename Dummy = Number>
433 DEAL_II_DEPRECATED std::enable_if_t<std::is_same_v<Dummy, Number> &&
436 VectorOperation::values operation,
437 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
438 &communication_pattern = {})
439 {
440 import_elements(V, operation, communication_pattern);
441 }
442# endif
443
452 void
454 const EpetraWrappers::Vector &epetra_vec,
455 VectorOperation::values operation,
456 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
457 &communication_pattern = {});
458
463 void
464 import(const EpetraWrappers::Vector &V,
465 VectorOperation::values operation,
466 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
467 &communication_pattern = {})
468 {
469 import_elements(V, operation, communication_pattern);
470 }
471#endif
472
473#ifdef DEAL_II_WITH_CUDA
480 void
482 const CUDAWrappers::Vector<Number> &cuda_vec,
483 VectorOperation::values operation,
484 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
485 &communication_pattern = {});
486
487
492 void
494 VectorOperation::values operation,
495 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
496 &communication_pattern = {})
497 {
498 import_elements(V, operation, communication_pattern);
499 }
500#endif
501
510 size_type
511 size() const override;
512
519
523 const IndexSet &
525
533
539 begin() const;
540
547
553 end() const;
567 Number
568 operator()(const size_type global_index) const;
569
575 Number &
576 operator()(const size_type global_index);
577
585 Number
586 operator[](const size_type global_index) const;
587
595 Number &
596 operator[](const size_type global_index);
597
613 template <typename Number2>
614 void
615 extract_subvector_to(const std::vector<size_type> &indices,
616 std::vector<Number2> &values) const;
617
621 virtual void
624 ArrayView<Number> &entries) const override;
625
653 template <typename ForwardIterator, typename OutputIterator>
654 void
655 extract_subvector_to(ForwardIterator indices_begin,
656 const ForwardIterator indices_end,
657 OutputIterator values_begin) const;
658
669 Number
670 local_element(const size_type local_index) const;
671
682 Number &
683 local_element(const size_type local_index);
696 template <typename Number2>
697 void
698 add(const std::vector<size_type> &indices,
699 const std::vector<Number2> &values);
700
705 template <typename Number2>
706 void
707 add(const std::vector<size_type> &indices,
708 const ReadWriteVector<Number2> &values);
709
715 template <typename Number2>
716 void
717 add(const size_type n_elements,
718 const size_type *indices,
719 const Number2 *values);
720
724 void
725 print(std::ostream &out,
726 const unsigned int precision = 3,
727 const bool scientific = true) const;
728
732 std::size_t
736 protected:
737#ifdef DEAL_II_WITH_TRILINOS
738# ifdef DEAL_II_TRILINOS_WITH_TPETRA
744 template <typename MemorySpace, typename Dummy = Number>
745 std::enable_if_t<std::is_same_v<Dummy, Number> &&
748 const Tpetra::
749 Vector<Number, int, types::signed_global_dof_index, MemorySpace>
750 &tpetra_vector,
751 const IndexSet &locally_owned_elements,
752 VectorOperation::values operation,
753 const MPI_Comm mpi_comm,
754 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
755 &communication_pattern);
756# endif
757
763 void
765 const Epetra_MultiVector &multivector,
766 const IndexSet &locally_owned_elements,
767 VectorOperation::values operation,
768 const MPI_Comm mpi_comm,
769 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
770 &communication_pattern);
771#endif
772
776 unsigned int
777 global_to_local(const types::global_dof_index global_index) const;
778
779#ifdef DEAL_II_WITH_TRILINOS
780# ifdef DEAL_II_TRILINOS_WITH_TPETRA
785 template <typename MemorySpace = ::MemorySpace::Host>
787 create_tpetra_comm_pattern(const IndexSet &source_index_set,
788 const MPI_Comm mpi_comm);
789# endif
790
796 create_epetra_comm_pattern(const IndexSet &source_index_set,
797 const MPI_Comm mpi_comm);
798#endif
799
804
809
814 std::shared_ptr<Utilities::MPI::CommunicationPatternBase> comm_pattern;
815
820
825 mutable std::shared_ptr<::parallel::internal::TBBPartitioner>
827
828 // Make all other ReadWriteVector types friends.
829 template <typename Number2>
830 friend class ReadWriteVector;
831
832 private:
838 template <typename Functor>
840 {
841 public:
846
850 virtual void
852
853 private:
858
862 const Functor &functor;
863 };
864 };
865
869 /*---------------------------- Inline functions ---------------------------*/
870
871#ifndef DOXYGEN
872
873
874 template <typename Number>
876 {
877 // virtual functions called in constructors and destructors never use the
878 // override in a derived class
879 // for clarity be explicit on which function is called
881 }
882
883
884
885 template <typename Number>
887 const ReadWriteVector<Number> &v)
888 : Subscriptor()
889 {
890 this->operator=(v);
891 }
892
893
894
895 template <typename Number>
896 inline ReadWriteVector<Number>::ReadWriteVector(const size_type size)
897 {
898 // virtual functions called in constructors and destructors never use the
899 // override in a derived class
900 // for clarity be explicit on which function is called
902 }
903
904
905
906 template <typename Number>
908 const IndexSet &locally_stored_indices)
909 {
910 // virtual functions called in constructors and destructors never use the
911 // override in a derived class
912 // for clarity be explicit on which function is called
913 ReadWriteVector<Number>::reinit(locally_stored_indices);
914 }
915
916
917
918 template <typename Number>
921 {
922 return stored_elements.size();
923 }
924
925
926
927 template <typename Number>
930 {
932 }
933
934
935
936 template <typename Number>
937 inline const IndexSet &
939 {
940 return stored_elements;
941 }
942
943
944
945 template <typename Number>
948 {
949 return values.begin();
950 }
951
952
953
954 template <typename Number>
957 {
958 return values.begin();
959 }
960
961
962
963 template <typename Number>
966 {
967 return values.end();
968 }
969
970
971
972 template <typename Number>
975 {
976 return values.end();
977 }
978
979
980
981 template <typename Number>
982 inline Number
983 ReadWriteVector<Number>::operator()(const size_type global_index) const
984 {
985 return values[global_to_local(global_index)];
986 }
987
988
989
990 template <typename Number>
991 inline Number &
992 ReadWriteVector<Number>::operator()(const size_type global_index)
993 {
994 return values[global_to_local(global_index)];
995 }
996
997
998
999 template <typename Number>
1000 inline Number
1001 ReadWriteVector<Number>::operator[](const size_type global_index) const
1002 {
1003 return operator()(global_index);
1004 }
1005
1006
1007
1008 template <typename Number>
1009 inline Number &
1010 ReadWriteVector<Number>::operator[](const size_type global_index)
1011 {
1012 return operator()(global_index);
1013 }
1014
1015
1016
1017 template <typename Number>
1018 template <typename Number2>
1019 inline void
1021 const std::vector<size_type> &indices,
1022 std::vector<Number2> &extracted_values) const
1023 {
1024 for (size_type i = 0; i < indices.size(); ++i)
1025 extracted_values[i] = operator()(indices[i]);
1026 }
1027
1028
1029
1030 template <typename Number>
1031 void
1034 ArrayView<Number> &entries) const
1035 {
1036 AssertDimension(indices.size(), entries.size());
1037 for (unsigned int i = 0; i < indices.size(); ++i)
1038 entries[i] = (*this)[indices[i]];
1039 }
1040
1041
1042
1043 template <typename Number>
1044 template <typename ForwardIterator, typename OutputIterator>
1045 inline void
1047 ForwardIterator indices_begin,
1048 const ForwardIterator indices_end,
1049 OutputIterator values_begin) const
1050 {
1051 while (indices_begin != indices_end)
1052 {
1053 *values_begin = operator()(*indices_begin);
1054 ++indices_begin;
1055 ++values_begin;
1056 }
1057 }
1058
1059
1060
1061 template <typename Number>
1062 inline Number
1063 ReadWriteVector<Number>::local_element(const size_type local_index) const
1064 {
1065 AssertIndexRange(local_index, this->locally_owned_size());
1066
1067 return values[local_index];
1068 }
1069
1070
1071
1072 template <typename Number>
1073 inline Number &
1074 ReadWriteVector<Number>::local_element(const size_type local_index)
1075 {
1076 AssertIndexRange(local_index, this->locally_owned_size());
1077
1078 return values[local_index];
1079 }
1080
1081
1082
1083 template <typename Number>
1084 template <typename Number2>
1085 inline void
1086 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1087 const std::vector<Number2> &values)
1088 {
1089 AssertDimension(indices.size(), values.size());
1090 add(indices.size(), indices.data(), values.data());
1091 }
1092
1093
1094
1095 template <typename Number>
1096 template <typename Number2>
1097 inline void
1098 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1099 const ReadWriteVector<Number2> &values)
1100 {
1101 const size_type size = indices.size();
1102 for (size_type i = 0; i < size; ++i)
1103 {
1104 Assert(
1105 numbers::is_finite(values[i]),
1106 ExcMessage(
1107 "The given value is not finite but either infinite or Not A Number (NaN)"));
1108 this->operator()(indices[i]) += values[indices[i]];
1109 }
1110 }
1111
1112
1113
1114 template <typename Number>
1115 template <typename Number2>
1116 inline void
1117 ReadWriteVector<Number>::add(const size_type n_indices,
1118 const size_type *indices,
1119 const Number2 *values_to_add)
1120 {
1121 for (size_type i = 0; i < n_indices; ++i)
1122 {
1123 Assert(
1124 numbers::is_finite(values[i]),
1125 ExcMessage(
1126 "The given value is not finite but either infinite or Not A Number (NaN)"));
1127 this->operator()(indices[i]) += values_to_add[i];
1128 }
1129 }
1130
1131
1132
1133 template <typename Number>
1134 inline unsigned int
1136 const types::global_dof_index global_index) const
1137 {
1138 // the following will throw an exception if the global_index is not
1139 // in the remaining_elements
1140 return static_cast<unsigned int>(
1141 stored_elements.index_within_set(global_index));
1142 }
1143
1144
1145
1146 template <typename Number>
1147 template <typename Functor>
1150 const Functor &functor)
1151 : parent(parent)
1152 , functor(functor)
1153 {}
1154
1155
1156
1157 template <typename Number>
1158 template <typename Functor>
1159 void
1161 const size_type begin,
1162 const size_type end)
1163 {
1164 for (size_type i = begin; i < end; ++i)
1165 functor(parent.values[i]);
1166 }
1167
1168#endif // ifndef DOXYGEN
1169
1170} // end of namespace LinearAlgebra
1171
1172
1173
1181template <typename Number>
1182inline void
1188
1189
1191
1192#endif
std::size_t size() const
Definition array_view.h:684
size_type size() const
Definition index_set.h:1776
size_type index_within_set(const size_type global_index) const
Definition index_set.h:2001
size_type n_elements() const
Definition index_set.h:1934
FunctorTemplate(ReadWriteVector< Number > &parent, const Functor &functor)
virtual void operator()(const size_type begin, const size_type end)
void import_elements(const ::Vector< Number > &vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
void reinit(const ReadWriteVector< Number2 > &in_vector, const bool omit_zeroing_entries=false)
Number & operator()(const size_type global_index)
ReadWriteVector(const ReadWriteVector< Number > &in_vector)
ReadWriteVector(const size_type size)
void import_elements(const TrilinosWrappers::MPI::Vector &trilinos_vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
size_type size() const override
void add(const std::vector< size_type > &indices, const std::vector< Number2 > &values)
Number operator()(const size_type global_index) const
void apply(const Functor &func)
std::size_t memory_consumption() const
void import_elements(const CUDAWrappers::Vector< Number > &cuda_vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
Number & operator[](const size_type global_index)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true) const
void add(const std::vector< size_type > &indices, const ReadWriteVector< Number2 > &values)
Number & local_element(const size_type local_index)
void import_elements(const EpetraWrappers::Vector &epetra_vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
AlignedVector< Number > values
unsigned int global_to_local(const types::global_dof_index global_index) const
Number operator[](const size_type global_index) const
Number local_element(const size_type local_index) const
void add(const size_type n_elements, const size_type *indices, const Number2 *values)
TpetraWrappers::CommunicationPattern< MemorySpace > create_tpetra_comm_pattern(const IndexSet &source_index_set, const MPI_Comm mpi_comm)
virtual void extract_subvector_to(const ArrayView< const types::global_dof_index > &indices, ArrayView< Number > &entries) const override
void import_elements(const PETScWrappers::MPI::Vector &petsc_vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
ReadWriteVector< Number > & operator=(const Number s)
void reinit(const TrilinosWrappers::MPI::Vector &trilinos_vec)
virtual void reinit(const IndexSet &locally_stored_indices, const bool omit_zeroing_entries=false)
void extract_subvector_to(ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
virtual void reinit(const size_type size, const bool omit_zeroing_entries=false)
size_type locally_owned_size() const
const_iterator end() const
ReadWriteVector< Number > & operator=(const ReadWriteVector< Number > &in_vector)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< Number2 > &values) const
void swap(ReadWriteVector< Number > &v) noexcept
const_iterator begin() const
std::enable_if_t< std::is_same_v< Dummy, Number > &&::is_tpetra_type< Number >::value > import_elements(const Tpetra::Vector< Number, int, types::signed_global_dof_index, MemorySpace > &tpetra_vector, const IndexSet &locally_owned_elements, VectorOperation::values operation, const MPI_Comm mpi_comm, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern)
~ReadWriteVector() override=default
ReadWriteVector(const IndexSet &locally_stored_indices)
EpetraWrappers::CommunicationPattern create_epetra_comm_pattern(const IndexSet &source_index_set, const MPI_Comm mpi_comm)
types::global_dof_index size_type
std::enable_if_t< std::is_same_v< Dummy, Number > &&::is_tpetra_type< Number >::value > import_elements(const TpetraWrappers::Vector< Number, MemorySpace > &tpetra_vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
const IndexSet & get_stored_elements() const
ReadWriteVector< Number > & operator=(const ReadWriteVector< Number2 > &in_vector)
void import_elements(const distributed::Vector< Number, MemorySpace > &vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
std::shared_ptr< Utilities::MPI::CommunicationPatternBase > comm_pattern
std::shared_ptr<::parallel::internal::TBBPartitioner > thread_loop_partitioner
typename numbers::NumberTraits< Number >::real_type real_type
void import_elements(const Epetra_MultiVector &multivector, const IndexSet &locally_owned_elements, VectorOperation::values operation, const MPI_Comm mpi_comm, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern)
void swap(LinearAlgebra::ReadWriteVector< Number > &u, LinearAlgebra::ReadWriteVector< Number > &v) noexcept
#define DEAL_II_DEPRECATED
Definition config.h:207
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
bool is_finite(const double x)
Definition numbers.h:538
STL namespace.
Definition types.h:32
unsigned int global_dof_index
Definition types.h:81