Reference documentation for deal.II version 9.5.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// Copyright (C) 2015 - 2023 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16#ifndef dealii_read_write_vector_h
17#define dealii_read_write_vector_h
18
19#include <deal.II/base/config.h>
20
28#include <deal.II/base/types.h>
29
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# ifdef DEAL_II_WITH_CUDA
84namespace LinearAlgebra
85{
86 namespace CUDAWrappers
87 {
88 template <typename>
89 class Vector;
90 }
91} // namespace LinearAlgebra
92# endif
93#endif
94
95namespace LinearAlgebra
96{
135 template <typename 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
310 DEAL_II_DEPRECATED_EARLY
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
329 void
332 VectorOperation::values operation,
333 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
334 &communication_pattern = {});
335
339 DEAL_II_DEPRECATED_EARLY
340 void
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
357 template <typename MemorySpace>
358 void
361 VectorOperation::values operation,
362 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
363 &communication_pattern = {});
364
368 template <typename MemorySpace>
369 DEAL_II_DEPRECATED_EARLY void
371 VectorOperation::values operation,
372 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
373 &communication_pattern = {})
374 {
375 import_elements(V, operation, communication_pattern);
376 }
377
378
379#ifdef DEAL_II_WITH_PETSC
388 void
390 const PETScWrappers::MPI::Vector &petsc_vec,
391 VectorOperation::values operation,
392 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
393 &communication_pattern = {});
394
398 DEAL_II_DEPRECATED_EARLY
399 void
401 VectorOperation::values operation,
402 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
403 &communication_pattern = {})
404 {
405 import_elements(V, operation, communication_pattern);
406 }
407#endif
408
409#ifdef DEAL_II_WITH_TRILINOS
420 void
422 const TrilinosWrappers::MPI::Vector &trilinos_vec,
423 VectorOperation::values operation,
424 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
425 &communication_pattern = {});
426
430 DEAL_II_DEPRECATED_EARLY
431 void
433 VectorOperation::values operation,
434 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
435 &communication_pattern = {})
436 {
437 import_elements(V, operation, communication_pattern);
438 }
439
440# ifdef DEAL_II_TRILINOS_WITH_TPETRA
449 template <typename Dummy = Number>
450 std::enable_if_t<std::is_same<Dummy, Number>::value &&
453 const TpetraWrappers::Vector<Number> &tpetra_vec,
454 VectorOperation::values operation,
455 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
456 &communication_pattern = {});
457
461 template <typename Dummy = Number>
462 DEAL_II_DEPRECATED_EARLY
463 std::enable_if_t<std::is_same<Dummy, Number>::value &&
465 import(
467 VectorOperation::values operation,
468 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
469 &communication_pattern = {})
470 {
471 import_elements(V, operation, communication_pattern);
472 }
473# endif
474
483 void
485 const EpetraWrappers::Vector &epetra_vec,
486 VectorOperation::values operation,
487 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
488 &communication_pattern = {});
489
493 DEAL_II_DEPRECATED_EARLY
494 void
495 import(const EpetraWrappers::Vector &V,
496 VectorOperation::values operation,
497 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
498 &communication_pattern = {})
499 {
500 import_elements(V, operation, communication_pattern);
501 }
502#endif
503
504#ifdef DEAL_II_WITH_CUDA
511 void
513 const CUDAWrappers::Vector<Number> &cuda_vec,
514 VectorOperation::values operation,
515 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
516 &communication_pattern = {});
517
518
522 DEAL_II_DEPRECATED_EARLY
523 void
525 VectorOperation::values operation,
526 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
527 &communication_pattern = {})
528 {
529 import_elements(V, operation, communication_pattern);
530 }
531#endif
532
542 size() const;
543
553 n_elements() const;
554
561
565 const IndexSet &
567
575
581 begin() const;
582
589
595 end() const;
609 Number
610 operator()(const size_type global_index) const;
611
617 Number &
618 operator()(const size_type global_index);
619
627 Number
628 operator[](const size_type global_index) const;
629
637 Number &
638 operator[](const size_type global_index);
639
655 template <typename Number2>
656 void
657 extract_subvector_to(const std::vector<size_type> &indices,
658 std::vector<Number2> & values) const;
659
687 template <typename ForwardIterator, typename OutputIterator>
688 void
689 extract_subvector_to(ForwardIterator indices_begin,
690 const ForwardIterator indices_end,
691 OutputIterator values_begin) const;
692
703 Number
704 local_element(const size_type local_index) const;
705
716 Number &
717 local_element(const size_type local_index);
730 template <typename Number2>
731 void
732 add(const std::vector<size_type> &indices,
733 const std::vector<Number2> & values);
734
739 template <typename Number2>
740 void
741 add(const std::vector<size_type> & indices,
743
749 template <typename Number2>
750 void
752 const size_type *indices,
753 const Number2 * values);
754
758 void
759 print(std::ostream & out,
760 const unsigned int precision = 3,
761 const bool scientific = true) const;
762
766 std::size_t
770 protected:
771#ifdef DEAL_II_WITH_TRILINOS
772# ifdef DEAL_II_TRILINOS_WITH_TPETRA
778 template <typename Dummy = Number>
779 std::enable_if_t<std::is_same<Dummy, Number>::value &&
782 const Tpetra::Vector<Number, int, types::signed_global_dof_index>
783 & tpetra_vector,
784 const IndexSet & locally_owned_elements,
785 VectorOperation::values operation,
786 const MPI_Comm mpi_comm,
787 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
788 &communication_pattern);
789# endif
790
796 void
798 const Epetra_MultiVector &multivector,
799 const IndexSet & locally_owned_elements,
800 VectorOperation::values operation,
801 const MPI_Comm mpi_comm,
802 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
803 &communication_pattern);
804#endif
805
809 unsigned int
810 global_to_local(const types::global_dof_index global_index) const;
811
815 void
816 resize_val(const size_type new_allocated_size);
817
818#ifdef DEAL_II_WITH_TRILINOS
819# ifdef DEAL_II_TRILINOS_WITH_TPETRA
825 create_tpetra_comm_pattern(const IndexSet &source_index_set,
826 const MPI_Comm mpi_comm);
827# endif
828
834 create_epetra_comm_pattern(const IndexSet &source_index_set,
835 const MPI_Comm mpi_comm);
836#endif
837
842
847
852 std::shared_ptr<Utilities::MPI::CommunicationPatternBase> comm_pattern;
853
857 std::unique_ptr<Number[], /*decltype(std::free) * */ void (*)(void *)>
859
864 mutable std::shared_ptr<::parallel::internal::TBBPartitioner>
866
867 // Make all other ReadWriteVector types friends.
868 template <typename Number2>
869 friend class ReadWriteVector;
870
871 private:
877 template <typename Functor>
879 {
880 public:
885
889 virtual void
891
892 private:
897
901 const Functor &functor;
902 };
903 };
904
908 /*---------------------------- Inline functions ---------------------------*/
909
910#ifndef DOXYGEN
911
912
913 template <typename Number>
915 : Subscriptor()
916 , values(nullptr, free)
917 {
918 // virtual functions called in constructors and destructors never use the
919 // override in a derived class
920 // for clarity be explicit on which function is called
922 }
923
924
925
926 template <typename Number>
928 const ReadWriteVector<Number> &v)
929 : Subscriptor()
930 , values(nullptr, free)
931 {
932 this->operator=(v);
933 }
934
935
936
937 template <typename Number>
939 : Subscriptor()
940 , values(nullptr, free)
941 {
942 // virtual functions called in constructors and destructors never use the
943 // override in a derived class
944 // for clarity be explicit on which function is called
946 }
947
948
949
950 template <typename Number>
952 const IndexSet &locally_stored_indices)
953 : Subscriptor()
954 , values(nullptr, free)
955 {
956 // virtual functions called in constructors and destructors never use the
957 // override in a derived class
958 // for clarity be explicit on which function is called
959 ReadWriteVector<Number>::reinit(locally_stored_indices);
960 }
961
962
963
964 template <typename Number>
967 {
968 return stored_elements.size();
969 }
970
971
972
973 template <typename Number>
976 {
978 }
979
980
981
982 template <typename Number>
985 {
987 }
988
989
990
991 template <typename Number>
992 inline const IndexSet &
994 {
995 return stored_elements;
996 }
997
998
999
1000 template <typename Number>
1001 inline typename ReadWriteVector<Number>::iterator
1003 {
1004 return values.get();
1005 }
1006
1007
1008
1009 template <typename Number>
1012 {
1013 return values.get();
1014 }
1015
1016
1017
1018 template <typename Number>
1019 inline typename ReadWriteVector<Number>::iterator
1021 {
1022 return values.get() + this->locally_owned_size();
1023 }
1024
1025
1026
1027 template <typename Number>
1030 {
1031 return values.get() + this->locally_owned_size();
1032 }
1033
1034
1035
1036 template <typename Number>
1037 inline Number
1038 ReadWriteVector<Number>::operator()(const size_type global_index) const
1039 {
1040 return values[global_to_local(global_index)];
1041 }
1042
1043
1044
1045 template <typename Number>
1046 inline Number &
1048 {
1049 return values[global_to_local(global_index)];
1050 }
1051
1052
1053
1054 template <typename Number>
1055 inline Number
1056 ReadWriteVector<Number>::operator[](const size_type global_index) const
1057 {
1058 return operator()(global_index);
1059 }
1060
1061
1062
1063 template <typename Number>
1064 inline Number &
1066 {
1067 return operator()(global_index);
1068 }
1069
1070
1071
1072 template <typename Number>
1073 template <typename Number2>
1074 inline void
1076 const std::vector<size_type> &indices,
1077 std::vector<Number2> & extracted_values) const
1078 {
1079 for (size_type i = 0; i < indices.size(); ++i)
1080 extracted_values[i] = operator()(indices[i]);
1081 }
1082
1083
1084
1085 template <typename Number>
1086 template <typename ForwardIterator, typename OutputIterator>
1087 inline void
1089 ForwardIterator indices_begin,
1090 const ForwardIterator indices_end,
1091 OutputIterator values_begin) const
1092 {
1093 while (indices_begin != indices_end)
1094 {
1095 *values_begin = operator()(*indices_begin);
1096 indices_begin++;
1097 values_begin++;
1098 }
1099 }
1100
1101
1102
1103 template <typename Number>
1104 inline Number
1105 ReadWriteVector<Number>::local_element(const size_type local_index) const
1106 {
1107 AssertIndexRange(local_index, this->locally_owned_size());
1108
1109 return values[local_index];
1110 }
1111
1112
1113
1114 template <typename Number>
1115 inline Number &
1117 {
1118 AssertIndexRange(local_index, this->locally_owned_size());
1119
1120 return values[local_index];
1121 }
1122
1123
1124
1125 template <typename Number>
1126 template <typename Number2>
1127 inline void
1128 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1129 const std::vector<Number2> & values)
1130 {
1131 AssertDimension(indices.size(), values.size());
1132 add(indices.size(), indices.data(), values.data());
1133 }
1134
1135
1136
1137 template <typename Number>
1138 template <typename Number2>
1139 inline void
1140 ReadWriteVector<Number>::add(const std::vector<size_type> & indices,
1141 const ReadWriteVector<Number2> &values)
1142 {
1143 const size_type size = indices.size();
1144 for (size_type i = 0; i < size; ++i)
1145 {
1146 Assert(
1148 ExcMessage(
1149 "The given value is not finite but either infinite or Not A Number (NaN)"));
1150 this->operator()(indices[i]) += values[indices[i]];
1151 }
1152 }
1153
1154
1155
1156 template <typename Number>
1157 template <typename Number2>
1158 inline void
1160 const size_type *indices,
1161 const Number2 * values_to_add)
1162 {
1163 for (size_type i = 0; i < n_indices; ++i)
1164 {
1165 Assert(
1167 ExcMessage(
1168 "The given value is not finite but either infinite or Not A Number (NaN)"));
1169 this->operator()(indices[i]) += values_to_add[i];
1170 }
1171 }
1172
1173
1174
1175 template <typename Number>
1176 inline unsigned int
1178 const types::global_dof_index global_index) const
1179 {
1180 // the following will throw an exception if the global_index is not
1181 // in the remaining_elements
1182 return static_cast<unsigned int>(
1183 stored_elements.index_within_set(global_index));
1184 }
1185
1186
1187
1188 template <typename Number>
1189 template <typename Functor>
1191 ReadWriteVector<Number> &parent,
1192 const Functor & functor)
1193 : parent(parent)
1194 , functor(functor)
1195 {}
1196
1197
1198
1199 template <typename Number>
1200 template <typename Functor>
1201 void
1203 const size_type begin,
1204 const size_type end)
1205 {
1206 for (size_type i = begin; i < end; ++i)
1207 functor(parent.values[i]);
1208 }
1209
1210#endif // ifndef DOXYGEN
1211
1212} // end of namespace LinearAlgebra
1213
1214
1215
1223template <typename Number>
1224inline void
1227{
1228 u.swap(v);
1229}
1230
1231
1233
1234#endif
size_type size() const
Definition index_set.h:1661
size_type index_within_set(const size_type global_index) const
Definition index_set.h:1883
size_type n_elements() const
Definition index_set.h:1816
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)
std::enable_if_t< std::is_same< Dummy, Number >::value &&::is_tpetra_type< Number >::value > import_elements(const TpetraWrappers::Vector< Number > &tpetra_vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
void import_elements(const TrilinosWrappers::MPI::Vector &trilinos_vec, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern={})
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={})
unsigned int global_to_local(const types::global_dof_index global_index) const
void resize_val(const size_type new_allocated_size)
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)
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)
TpetraWrappers::CommunicationPattern create_tpetra_comm_pattern(const IndexSet &source_index_set, const MPI_Comm mpi_comm)
std::enable_if_t< std::is_same< Dummy, Number >::value &&::is_tpetra_type< Number >::value > import_elements(const Tpetra::Vector< Number, int, types::signed_global_dof_index > &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)
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
const_iterator begin() const
void import_elements(const LinearAlgebra::Vector< Number > &vec, VectorOperation::values operation, 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)
std::unique_ptr< Number[], void(*)(void *)> values
types::global_dof_index size_type
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={})
void swap(ReadWriteVector< Number > &v)
size_type n_elements() const
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)
#define DEAL_II_DEPRECATED
Definition config.h:172
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:472
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:473
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
void free(T *&pointer)
Definition cuda.h:97
bool is_finite(const double x)
Definition numbers.h:538
unsigned int global_dof_index
Definition types.h:82