deal.II version GIT relicensing-1838-g97284be5cd 2024-09-11 15:30: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
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#endif
85
86namespace LinearAlgebra
87{
125 template <typename Number>
126 class ReadWriteVector : public Subscriptor, public ReadVector<Number>
127 {
128 public:
134 using value_type = Number;
136 using const_pointer = const value_type *;
138 using const_iterator = const value_type *;
143
152
157
163
168 explicit ReadWriteVector(const IndexSet &locally_stored_indices);
169
173 ~ReadWriteVector() override = default;
174
183 virtual void
184 reinit(const size_type size, const bool omit_zeroing_entries = false);
185
194 template <typename Number2>
195 void
197 const bool omit_zeroing_entries = false);
198
208 virtual void
209 reinit(const IndexSet &locally_stored_indices,
210 const bool omit_zeroing_entries = false);
211
212
213#ifdef DEAL_II_WITH_TRILINOS
225 void
227#endif
228
242 template <typename Functor>
243 void
244 apply(const Functor &func);
245
258 void
260
266
270 template <typename Number2>
273
279 operator=(const Number s);
280
290 void
292 const ::Vector<Number> &vec,
293 VectorOperation::values operation,
294 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
295 &communication_pattern = {});
296
301 void
302 import(const ::Vector<Number> &V,
303 VectorOperation::values operation,
304 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
305 &communication_pattern = {})
306 {
307 import_elements(V, operation, communication_pattern);
308 }
309
318 template <typename MemorySpace>
319 void
322 VectorOperation::values operation,
323 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
324 &communication_pattern = {});
325
329 template <typename MemorySpace>
332 VectorOperation::values operation,
333 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
334 &communication_pattern = {})
335 {
336 import_elements(V, operation, communication_pattern);
337 }
338
339
340#ifdef DEAL_II_WITH_PETSC
349 void
351 const PETScWrappers::MPI::Vector &petsc_vec,
352 VectorOperation::values operation,
353 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
354 &communication_pattern = {});
355
360 void
362 VectorOperation::values operation,
363 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
364 &communication_pattern = {})
365 {
366 import_elements(V, operation, communication_pattern);
367 }
368#endif
369
370#ifdef DEAL_II_WITH_TRILINOS
381 void
383 const TrilinosWrappers::MPI::Vector &trilinos_vec,
384 VectorOperation::values operation,
385 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
386 &communication_pattern = {});
387
392 void
394 VectorOperation::values operation,
395 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
396 &communication_pattern = {})
397 {
398 import_elements(V, operation, communication_pattern);
399 }
400
401# ifdef DEAL_II_TRILINOS_WITH_TPETRA
410 template <typename MemorySpace, typename Dummy = Number>
411 std::enable_if_t<std::is_same_v<Dummy, Number> &&
415 VectorOperation::values operation,
416 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
417 &communication_pattern = {});
418
422 template <typename MemorySpace, typename Dummy = Number>
423 DEAL_II_DEPRECATED std::enable_if_t<std::is_same_v<Dummy, Number> &&
426 VectorOperation::values operation,
427 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
428 &communication_pattern = {})
429 {
430 import_elements(V, operation, communication_pattern);
431 }
432# endif
433
442 void
444 const EpetraWrappers::Vector &epetra_vec,
445 VectorOperation::values operation,
446 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
447 &communication_pattern = {});
448
453 void
454 import(const EpetraWrappers::Vector &V,
455 VectorOperation::values operation,
456 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
457 &communication_pattern = {})
458 {
459 import_elements(V, operation, communication_pattern);
460 }
461#endif
462
472 size() const override;
473
480
484 const IndexSet &
486
494
500 begin() const;
501
508
514 end() const;
528 Number
529 operator()(const size_type global_index) const;
530
536 Number &
537 operator()(const size_type global_index);
538
546 Number
547 operator[](const size_type global_index) const;
548
556 Number &
557 operator[](const size_type global_index);
558
574 template <typename Number2>
575 void
576 extract_subvector_to(const std::vector<size_type> &indices,
577 std::vector<Number2> &values) const;
578
582 virtual void
585 ArrayView<Number> &entries) const override;
586
614 template <typename ForwardIterator, typename OutputIterator>
615 void
616 extract_subvector_to(ForwardIterator indices_begin,
617 const ForwardIterator indices_end,
618 OutputIterator values_begin) const;
619
630 Number
631 local_element(const size_type local_index) const;
632
643 Number &
644 local_element(const size_type local_index);
657 template <typename Number2>
658 void
659 add(const std::vector<size_type> &indices,
660 const std::vector<Number2> &values);
661
666 template <typename Number2>
667 void
668 add(const std::vector<size_type> &indices,
670
676 template <typename Number2>
677 void
678 add(const size_type n_elements,
679 const size_type *indices,
680 const Number2 *values);
681
685 void
686 print(std::ostream &out,
687 const unsigned int precision = 3,
688 const bool scientific = true) const;
689
693 std::size_t
697 protected:
698#ifdef DEAL_II_WITH_TRILINOS
699# ifdef DEAL_II_TRILINOS_WITH_TPETRA
705 template <typename MemorySpace, typename Dummy = Number>
706 std::enable_if_t<std::is_same_v<Dummy, Number> &&
709 const Tpetra::
710 Vector<Number, int, types::signed_global_dof_index, MemorySpace>
711 &tpetra_vector,
712 const IndexSet &locally_owned_elements,
713 VectorOperation::values operation,
714 const MPI_Comm mpi_comm,
715 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
716 &communication_pattern);
717# endif
718
724 void
726 const Epetra_MultiVector &multivector,
727 const IndexSet &locally_owned_elements,
728 VectorOperation::values operation,
729 const MPI_Comm mpi_comm,
730 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
731 &communication_pattern);
732#endif
733
737 unsigned int
738 global_to_local(const types::global_dof_index global_index) const;
739
740#ifdef DEAL_II_WITH_TRILINOS
741# ifdef DEAL_II_TRILINOS_WITH_TPETRA
746 template <typename MemorySpace = ::MemorySpace::Host>
748 create_tpetra_comm_pattern(const IndexSet &source_index_set,
749 const MPI_Comm mpi_comm);
750# endif
751
757 create_epetra_comm_pattern(const IndexSet &source_index_set,
758 const MPI_Comm mpi_comm);
759#endif
760
765
770
775 std::shared_ptr<Utilities::MPI::CommunicationPatternBase> comm_pattern;
776
781
786 mutable std::shared_ptr<::parallel::internal::TBBPartitioner>
788
789 // Make all other ReadWriteVector types friends.
790 template <typename Number2>
791 friend class ReadWriteVector;
792
793 private:
799 template <typename Functor>
801 {
802 public:
807
811 virtual void
813
814 private:
819
823 const Functor &functor;
824 };
825 };
826
830 /*---------------------------- Inline functions ---------------------------*/
831
832#ifndef DOXYGEN
833
834
835 template <typename Number>
837 {
838 // virtual functions called in constructors and destructors never use the
839 // override in a derived class
840 // for clarity be explicit on which function is called
842 }
843
844
845
846 template <typename Number>
848 const ReadWriteVector<Number> &v)
849 : Subscriptor()
850 {
851 this->operator=(v);
852 }
853
854
855
856 template <typename Number>
858 {
859 // virtual functions called in constructors and destructors never use the
860 // override in a derived class
861 // for clarity be explicit on which function is called
863 }
864
865
866
867 template <typename Number>
869 const IndexSet &locally_stored_indices)
870 {
871 // virtual functions called in constructors and destructors never use the
872 // override in a derived class
873 // for clarity be explicit on which function is called
874 ReadWriteVector<Number>::reinit(locally_stored_indices);
875 }
876
877
878
879 template <typename Number>
882 {
883 return stored_elements.size();
884 }
885
886
887
888 template <typename Number>
891 {
893 }
894
895
896
897 template <typename Number>
898 inline const IndexSet &
900 {
901 return stored_elements;
902 }
903
904
905
906 template <typename Number>
909 {
910 return values.begin();
911 }
912
913
914
915 template <typename Number>
918 {
919 return values.begin();
920 }
921
922
923
924 template <typename Number>
927 {
928 return values.end();
929 }
930
931
932
933 template <typename Number>
936 {
937 return values.end();
938 }
939
940
941
942 template <typename Number>
943 inline Number
944 ReadWriteVector<Number>::operator()(const size_type global_index) const
945 {
946 return values[global_to_local(global_index)];
947 }
948
949
950
951 template <typename Number>
952 inline Number &
954 {
955 return values[global_to_local(global_index)];
956 }
957
958
959
960 template <typename Number>
961 inline Number
962 ReadWriteVector<Number>::operator[](const size_type global_index) const
963 {
964 return operator()(global_index);
965 }
966
967
968
969 template <typename Number>
970 inline Number &
972 {
973 return operator()(global_index);
974 }
975
976
977
978 template <typename Number>
979 template <typename Number2>
980 inline void
982 const std::vector<size_type> &indices,
983 std::vector<Number2> &extracted_values) const
984 {
985 for (size_type i = 0; i < indices.size(); ++i)
986 extracted_values[i] = operator()(indices[i]);
987 }
988
989
990
991 template <typename Number>
992 void
995 ArrayView<Number> &entries) const
996 {
997 AssertDimension(indices.size(), entries.size());
998 for (unsigned int i = 0; i < indices.size(); ++i)
999 entries[i] = (*this)[indices[i]];
1000 }
1001
1002
1003
1004 template <typename Number>
1005 template <typename ForwardIterator, typename OutputIterator>
1006 inline void
1008 ForwardIterator indices_begin,
1009 const ForwardIterator indices_end,
1010 OutputIterator values_begin) const
1011 {
1012 while (indices_begin != indices_end)
1013 {
1014 *values_begin = operator()(*indices_begin);
1015 ++indices_begin;
1016 ++values_begin;
1017 }
1018 }
1019
1020
1021
1022 template <typename Number>
1023 inline Number
1024 ReadWriteVector<Number>::local_element(const size_type local_index) const
1025 {
1026 AssertIndexRange(local_index, this->locally_owned_size());
1027
1028 return values[local_index];
1029 }
1030
1031
1032
1033 template <typename Number>
1034 inline Number &
1036 {
1037 AssertIndexRange(local_index, this->locally_owned_size());
1038
1039 return values[local_index];
1040 }
1041
1042
1043
1044 template <typename Number>
1045 template <typename Number2>
1046 inline void
1047 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1048 const std::vector<Number2> &values)
1049 {
1050 AssertDimension(indices.size(), values.size());
1051 add(indices.size(), indices.data(), values.data());
1052 }
1053
1054
1055
1056 template <typename Number>
1057 template <typename Number2>
1058 inline void
1059 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1060 const ReadWriteVector<Number2> &values)
1061 {
1062 const size_type size = indices.size();
1063 for (size_type i = 0; i < size; ++i)
1064 {
1065 Assert(
1067 ExcMessage(
1068 "The given value is not finite but either infinite or Not A Number (NaN)"));
1069 this->operator()(indices[i]) += values[indices[i]];
1070 }
1071 }
1072
1073
1074
1075 template <typename Number>
1076 template <typename Number2>
1077 inline void
1079 const size_type *indices,
1080 const Number2 *values_to_add)
1081 {
1082 for (size_type i = 0; i < n_indices; ++i)
1083 {
1084 Assert(
1086 ExcMessage(
1087 "The given value is not finite but either infinite or Not A Number (NaN)"));
1088 this->operator()(indices[i]) += values_to_add[i];
1089 }
1090 }
1091
1092
1093
1094 template <typename Number>
1095 inline unsigned int
1097 const types::global_dof_index global_index) const
1098 {
1099 // the following will throw an exception if the global_index is not
1100 // in the remaining_elements
1101 return static_cast<unsigned int>(
1102 stored_elements.index_within_set(global_index));
1103 }
1104
1105
1106
1107 template <typename Number>
1108 template <typename Functor>
1110 ReadWriteVector<Number> &parent,
1111 const Functor &functor)
1112 : parent(parent)
1113 , functor(functor)
1114 {}
1115
1116
1117
1118 template <typename Number>
1119 template <typename Functor>
1120 void
1122 const size_type begin,
1123 const size_type end)
1124 {
1125 for (size_type i = begin; i < end; ++i)
1126 functor(parent.values[i]);
1127 }
1128
1129#endif // ifndef DOXYGEN
1130
1131} // end of namespace LinearAlgebra
1132
1133
1134
1142template <typename Number>
1143inline void
1149
1150
1152
1153#endif
iterator end()
iterator begin()
pointer data()
size_type size() const
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
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:500
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:501
#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:533
STL namespace.
Definition types.h:32
unsigned int global_dof_index
Definition types.h:81