deal.II version GIT relicensing-2206-gaa53ff9447 2024-12-02 09:10:00+00:00
\(\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
27#include <deal.II/base/types.h>
28
31
32#include <cstdlib>
33#include <cstring>
34#include <iomanip>
35
36#ifdef DEAL_II_WITH_TRILINOS
40
41# include <Epetra_MultiVector.h>
42
43#endif
44
46
47// Forward declarations
48#ifndef DOXYGEN
49template <typename>
50class Vector;
51
52namespace LinearAlgebra
53{
54 template <typename>
55 class Vector;
56 namespace distributed
57 {
58 template <typename, typename>
59 class Vector;
60 } // namespace distributed
61} // namespace LinearAlgebra
62
63# ifdef DEAL_II_WITH_PETSC
64namespace PETScWrappers
65{
66 namespace MPI
67 {
68 class Vector;
69 }
70} // namespace PETScWrappers
71# endif
72
73# ifdef DEAL_II_WITH_TRILINOS
74namespace TrilinosWrappers
75{
76 namespace MPI
77 {
78 class Vector;
79 }
80} // namespace TrilinosWrappers
81# endif
82
83#endif
84
85namespace LinearAlgebra
86{
124 template <typename Number>
125 class ReadWriteVector : public ReadVector<Number>
126 {
127 public:
133 using value_type = Number;
135 using const_pointer = const value_type *;
137 using const_iterator = const value_type *;
142
151
156
162
167 explicit ReadWriteVector(const IndexSet &locally_stored_indices);
168
172 ~ReadWriteVector() override = default;
173
182 virtual void
183 reinit(const size_type size, const bool omit_zeroing_entries = false);
184
193 template <typename Number2>
194 void
196 const bool omit_zeroing_entries = false);
197
207 virtual void
208 reinit(const IndexSet &locally_stored_indices,
209 const bool omit_zeroing_entries = false);
210
211
212#ifdef DEAL_II_WITH_TRILINOS
224 void
226#endif
227
241 template <typename Functor>
242 void
243 apply(const Functor &func);
244
257 void
259
265
269 template <typename Number2>
272
278 operator=(const Number s);
279
289 void
291 const ::Vector<Number> &vec,
292 VectorOperation::values operation,
293 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
294 &communication_pattern = {});
295
300 void
301 import(const ::Vector<Number> &V,
302 VectorOperation::values operation,
303 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
304 &communication_pattern = {})
305 {
306 import_elements(V, operation, communication_pattern);
307 }
308
317 template <typename MemorySpace>
318 void
321 VectorOperation::values operation,
322 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
323 &communication_pattern = {});
324
328 template <typename MemorySpace>
331 VectorOperation::values operation,
332 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
333 &communication_pattern = {})
334 {
335 import_elements(V, operation, communication_pattern);
336 }
337
338
339#ifdef DEAL_II_WITH_PETSC
348 void
350 const PETScWrappers::MPI::Vector &petsc_vec,
351 VectorOperation::values operation,
352 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
353 &communication_pattern = {});
354
359 void
361 VectorOperation::values operation,
362 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
363 &communication_pattern = {})
364 {
365 import_elements(V, operation, communication_pattern);
366 }
367#endif
368
369#ifdef DEAL_II_WITH_TRILINOS
380 void
382 const TrilinosWrappers::MPI::Vector &trilinos_vec,
383 VectorOperation::values operation,
384 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
385 &communication_pattern = {});
386
391 void
393 VectorOperation::values operation,
394 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
395 &communication_pattern = {})
396 {
397 import_elements(V, operation, communication_pattern);
398 }
399
400# ifdef DEAL_II_TRILINOS_WITH_TPETRA
409 template <typename MemorySpace, typename Dummy = Number>
410 std::enable_if_t<std::is_same_v<Dummy, Number> &&
414 VectorOperation::values operation,
415 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
416 &communication_pattern = {});
417
421 template <typename MemorySpace, typename Dummy = Number>
422 DEAL_II_DEPRECATED 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 {
429 import_elements(V, operation, communication_pattern);
430 }
431# endif
432
441 void
443 const EpetraWrappers::Vector &epetra_vec,
444 VectorOperation::values operation,
445 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
446 &communication_pattern = {});
447
452 void
453 import(const EpetraWrappers::Vector &V,
454 VectorOperation::values operation,
455 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
456 &communication_pattern = {})
457 {
458 import_elements(V, operation, communication_pattern);
459 }
460#endif
461
471 size() const override;
472
479
483 const IndexSet &
485
493
499 begin() const;
500
507
513 end() const;
527 Number
528 operator()(const size_type global_index) const;
529
535 Number &
536 operator()(const size_type global_index);
537
545 Number
546 operator[](const size_type global_index) const;
547
555 Number &
556 operator[](const size_type global_index);
557
573 template <typename Number2>
574 void
575 extract_subvector_to(const std::vector<size_type> &indices,
576 std::vector<Number2> &values) const;
577
581 virtual void
584 const ArrayView<Number> &entries) const override;
585
613 template <typename ForwardIterator, typename OutputIterator>
614 void
615 extract_subvector_to(ForwardIterator indices_begin,
616 const ForwardIterator indices_end,
617 OutputIterator values_begin) const;
618
629 Number
630 local_element(const size_type local_index) const;
631
642 Number &
643 local_element(const size_type local_index);
656 template <typename Number2>
657 void
658 add(const std::vector<size_type> &indices,
659 const std::vector<Number2> &values);
660
665 template <typename Number2>
666 void
667 add(const std::vector<size_type> &indices,
669
675 template <typename Number2>
676 void
677 add(const size_type n_elements,
678 const size_type *indices,
679 const Number2 *values);
680
684 void
685 print(std::ostream &out,
686 const unsigned int precision = 3,
687 const bool scientific = true) const;
688
692 std::size_t
696 protected:
697#ifdef DEAL_II_WITH_TRILINOS
698# ifdef DEAL_II_TRILINOS_WITH_TPETRA
704 template <typename MemorySpace, typename Dummy = Number>
705 std::enable_if_t<std::is_same_v<Dummy, Number> &&
708 const Tpetra::
709 Vector<Number, int, types::signed_global_dof_index, MemorySpace>
710 &tpetra_vector,
711 const IndexSet &locally_owned_elements,
712 VectorOperation::values operation,
713 const MPI_Comm mpi_comm,
714 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
715 &communication_pattern);
716# endif
717
723 void
725 const Epetra_MultiVector &multivector,
726 const IndexSet &locally_owned_elements,
727 VectorOperation::values operation,
728 const MPI_Comm mpi_comm,
729 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
730 &communication_pattern);
731#endif
732
736 unsigned int
737 global_to_local(const types::global_dof_index global_index) const;
738
739#ifdef DEAL_II_WITH_TRILINOS
740# ifdef DEAL_II_TRILINOS_WITH_TPETRA
745 template <typename MemorySpace = ::MemorySpace::Host>
747 create_tpetra_comm_pattern(const IndexSet &source_index_set,
748 const MPI_Comm mpi_comm);
749# endif
750
756 create_epetra_comm_pattern(const IndexSet &source_index_set,
757 const MPI_Comm mpi_comm);
758#endif
759
764
769
774 std::shared_ptr<Utilities::MPI::CommunicationPatternBase> comm_pattern;
775
780
785 mutable std::shared_ptr<::parallel::internal::TBBPartitioner>
787
788 // Make all other ReadWriteVector types friends.
789 template <typename Number2>
790 friend class ReadWriteVector;
791
792 private:
798 template <typename Functor>
800 {
801 public:
806
810 virtual void
812
813 private:
818
822 const Functor &functor;
823 };
824 };
825
829 /*---------------------------- Inline functions ---------------------------*/
830
831#ifndef DOXYGEN
832
833
834 template <typename Number>
836 {
837 // virtual functions called in constructors and destructors never use the
838 // override in a derived class
839 // for clarity be explicit on which function is called
841 }
842
843
844
845 template <typename Number>
847 const ReadWriteVector<Number> &v)
848 {
849 this->operator=(v);
850 }
851
852
853
854 template <typename Number>
855 inline ReadWriteVector<Number>::ReadWriteVector(const size_type size)
856 {
857 // virtual functions called in constructors and destructors never use the
858 // override in a derived class
859 // for clarity be explicit on which function is called
861 }
862
863
864
865 template <typename Number>
867 const IndexSet &locally_stored_indices)
868 {
869 // virtual functions called in constructors and destructors never use the
870 // override in a derived class
871 // for clarity be explicit on which function is called
872 ReadWriteVector<Number>::reinit(locally_stored_indices);
873 }
874
875
876
877 template <typename Number>
880 {
881 return stored_elements.size();
882 }
883
884
885
886 template <typename Number>
889 {
890 return stored_elements.n_elements();
891 }
892
893
894
895 template <typename Number>
896 inline const IndexSet &
898 {
899 return stored_elements;
900 }
901
902
903
904 template <typename Number>
907 {
908 return values.begin();
909 }
910
911
912
913 template <typename Number>
916 {
917 return values.begin();
918 }
919
920
921
922 template <typename Number>
925 {
926 return values.end();
927 }
928
929
930
931 template <typename Number>
934 {
935 return values.end();
936 }
937
938
939
940 template <typename Number>
941 inline Number
942 ReadWriteVector<Number>::operator()(const size_type global_index) const
943 {
944 return values[global_to_local(global_index)];
945 }
946
947
948
949 template <typename Number>
950 inline Number &
951 ReadWriteVector<Number>::operator()(const size_type global_index)
952 {
953 return values[global_to_local(global_index)];
954 }
955
956
957
958 template <typename Number>
959 inline Number
960 ReadWriteVector<Number>::operator[](const size_type global_index) const
961 {
962 return operator()(global_index);
963 }
964
965
966
967 template <typename Number>
968 inline Number &
969 ReadWriteVector<Number>::operator[](const size_type global_index)
970 {
971 return operator()(global_index);
972 }
973
974
975
976 template <typename Number>
977 template <typename Number2>
978 inline void
980 const std::vector<size_type> &indices,
981 std::vector<Number2> &extracted_values) const
982 {
983 for (size_type i = 0; i < indices.size(); ++i)
984 extracted_values[i] = operator()(indices[i]);
985 }
986
987
988
989 template <typename Number>
990 void
993 const ArrayView<Number> &entries) const
994 {
995 AssertDimension(indices.size(), entries.size());
996 for (unsigned int i = 0; i < indices.size(); ++i)
997 entries[i] = (*this)[indices[i]];
998 }
999
1000
1001
1002 template <typename Number>
1003 template <typename ForwardIterator, typename OutputIterator>
1004 inline void
1006 ForwardIterator indices_begin,
1007 const ForwardIterator indices_end,
1008 OutputIterator values_begin) const
1009 {
1010 while (indices_begin != indices_end)
1011 {
1012 *values_begin = operator()(*indices_begin);
1013 ++indices_begin;
1014 ++values_begin;
1015 }
1016 }
1017
1018
1019
1020 template <typename Number>
1021 inline Number
1022 ReadWriteVector<Number>::local_element(const size_type local_index) const
1023 {
1024 AssertIndexRange(local_index, this->locally_owned_size());
1025
1026 return values[local_index];
1027 }
1028
1029
1030
1031 template <typename Number>
1032 inline Number &
1033 ReadWriteVector<Number>::local_element(const size_type local_index)
1034 {
1035 AssertIndexRange(local_index, this->locally_owned_size());
1036
1037 return values[local_index];
1038 }
1039
1040
1041
1042 template <typename Number>
1043 template <typename Number2>
1044 inline void
1045 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1046 const std::vector<Number2> &values)
1047 {
1048 AssertDimension(indices.size(), values.size());
1049 add(indices.size(), indices.data(), values.data());
1050 }
1051
1052
1053
1054 template <typename Number>
1055 template <typename Number2>
1056 inline void
1057 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1058 const ReadWriteVector<Number2> &values)
1059 {
1060 const size_type size = indices.size();
1061 for (size_type i = 0; i < size; ++i)
1062 {
1063 Assert(
1064 numbers::is_finite(values[i]),
1065 ExcMessage(
1066 "The given value is not finite but either infinite or Not A Number (NaN)"));
1067 this->operator()(indices[i]) += values[indices[i]];
1068 }
1069 }
1070
1071
1072
1073 template <typename Number>
1074 template <typename Number2>
1075 inline void
1076 ReadWriteVector<Number>::add(const size_type n_indices,
1077 const size_type *indices,
1078 const Number2 *values_to_add)
1079 {
1080 for (size_type i = 0; i < n_indices; ++i)
1081 {
1082 Assert(
1083 numbers::is_finite(values[i]),
1084 ExcMessage(
1085 "The given value is not finite but either infinite or Not A Number (NaN)"));
1086 this->operator()(indices[i]) += values_to_add[i];
1087 }
1088 }
1089
1090
1091
1092 template <typename Number>
1093 inline unsigned int
1095 const types::global_dof_index global_index) const
1096 {
1097 // the following will throw an exception if the global_index is not
1098 // in the remaining_elements
1099 return static_cast<unsigned int>(
1100 stored_elements.index_within_set(global_index));
1101 }
1102
1103
1104
1105 template <typename Number>
1106 template <typename Functor>
1108 ReadWriteVector<Number> &parent,
1109 const Functor &functor)
1110 : parent(parent)
1111 , functor(functor)
1112 {}
1113
1114
1115
1116 template <typename Number>
1117 template <typename Functor>
1118 void
1120 const size_type begin,
1121 const size_type end)
1122 {
1123 for (size_type i = begin; i < end; ++i)
1124 functor(parent.values[i]);
1125 }
1126
1127#endif // ifndef DOXYGEN
1128
1129} // end of namespace LinearAlgebra
1130
1131
1132
1140template <typename Number>
1141inline void
1147
1148
1150
1151#endif
std::size_t size() const
Definition array_view.h:684
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
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
virtual void extract_subvector_to(const ArrayView< const types::global_dof_index > &indices, const ArrayView< Number > &entries) const override
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)
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:205
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::size_t size
Definition mpi.cc:734
types::global_dof_index locally_owned_size
Definition mpi.cc:822
bool is_finite(const double x)
Definition numbers.h:533
STL namespace.
Definition types.h:32
unsigned int global_dof_index
Definition types.h:81