Reference documentation for deal.II version GIT d77e5ebb0a 2023-01-27 22:35:02+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\}}\)
read_write_vector.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2015 - 2021 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 
22 #include <deal.II/base/index_set.h>
24 #include <deal.II/base/mpi_stub.h>
25 #include <deal.II/base/parallel.h>
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
49 template <typename>
50 class Vector;
51 
52 namespace 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
64 namespace PETScWrappers
65 {
66  namespace MPI
67  {
68  class Vector;
69  }
70 } // namespace PETScWrappers
71 # endif
72 
73 # ifdef DEAL_II_WITH_TRILINOS
74 namespace TrilinosWrappers
75 {
76  namespace MPI
77  {
78  class Vector;
79  }
80 } // namespace TrilinosWrappers
81 # endif
82 
83 # ifdef DEAL_II_WITH_CUDA
84 namespace LinearAlgebra
85 {
86  namespace CUDAWrappers
87  {
88  template <typename>
89  class Vector;
90  }
91 } // namespace LinearAlgebra
92 # endif
93 #endif
94 
95 namespace LinearAlgebra
96 {
135  template <typename Number>
137  {
138  public:
144  using value_type = Number;
145  using pointer = value_type *;
146  using const_pointer = const value_type *;
147  using iterator = value_type *;
148  using const_iterator = const value_type *;
150  using const_reference = const value_type &;
153 
162 
167 
172  explicit ReadWriteVector(const size_type size);
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
236  reinit(const TrilinosWrappers::MPI::Vector &trilinos_vec);
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
301  import(const ::Vector<Number> &vec,
302  VectorOperation::values operation,
303  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
304  &communication_pattern = {});
305 
315  void
316  import(const LinearAlgebra::Vector<Number> &vec,
317  VectorOperation::values operation,
318  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
319  &communication_pattern = {});
320 
329  template <typename MemorySpace>
330  void
332  VectorOperation::values operation,
333  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
334  &communication_pattern = {});
335 
336 #ifdef DEAL_II_WITH_PETSC
345  void
346  import(const PETScWrappers::MPI::Vector &petsc_vec,
347  VectorOperation::values operation,
348  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
349  &communication_pattern = {});
350 #endif
351 
352 #ifdef DEAL_II_WITH_TRILINOS
363  void
364  import(const TrilinosWrappers::MPI::Vector &trilinos_vec,
365  VectorOperation::values operation,
366  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
367  &communication_pattern = {});
368 
369 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
378  void
379  import(const TpetraWrappers::Vector<Number> &tpetra_vec,
380  VectorOperation::values operation,
381  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
382  &communication_pattern = {});
383 # endif
384 
393  void
394  import(const EpetraWrappers::Vector &epetra_vec,
395  VectorOperation::values operation,
396  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
397  &communication_pattern = {});
398 #endif
399 
400 #ifdef DEAL_II_WITH_CUDA
407  void
408  import(const CUDAWrappers::Vector<Number> &cuda_vec,
409  VectorOperation::values operation,
410  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
411  &communication_pattern = {});
412 #endif
413 
422  size_type
423  size() const;
424 
433  size_type
434  n_elements() const;
435 
440  size_type
442 
446  const IndexSet &
448 
454  iterator
455  begin();
456 
462  begin() const;
463 
468  iterator
469  end();
470 
476  end() const;
490  Number
492 
498  Number &
500 
508  Number
510 
518  Number &
520 
536  template <typename Number2>
537  void
538  extract_subvector_to(const std::vector<size_type> &indices,
539  std::vector<Number2> & values) const;
540 
568  template <typename ForwardIterator, typename OutputIterator>
569  void
570  extract_subvector_to(ForwardIterator indices_begin,
571  const ForwardIterator indices_end,
572  OutputIterator values_begin) const;
573 
584  Number
585  local_element(const size_type local_index) const;
586 
597  Number &
598  local_element(const size_type local_index);
611  template <typename Number2>
612  void
613  add(const std::vector<size_type> &indices,
614  const std::vector<Number2> & values);
615 
620  template <typename Number2>
621  void
622  add(const std::vector<size_type> & indices,
624 
630  template <typename Number2>
631  void
633  const size_type *indices,
634  const Number2 * values);
635 
639  void
640  print(std::ostream & out,
641  const unsigned int precision = 3,
642  const bool scientific = true) const;
643 
647  std::size_t
651  protected:
652 #ifdef DEAL_II_WITH_TRILINOS
653 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
659  void
660  import(
661  const Tpetra::Vector<Number, int, types::global_dof_index> &tpetra_vector,
662  const IndexSet & locally_owned_elements,
663  VectorOperation::values operation,
664  const MPI_Comm & mpi_comm,
665  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
666  &communication_pattern);
667 # endif
668 
674  void
675  import(const Epetra_MultiVector &multivector,
676  const IndexSet & locally_owned_elements,
677  VectorOperation::values operation,
678  const MPI_Comm & mpi_comm,
679  const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
680  &communication_pattern);
681 #endif
682 
686  unsigned int
688  {
689  // the following will throw an exception if the global_index is not
690  // in the remaining_elements
691  return static_cast<unsigned int>(
693  }
694 
698  void
699  resize_val(const size_type new_allocated_size);
700 
701 #ifdef DEAL_II_WITH_TRILINOS
702 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
708  create_tpetra_comm_pattern(const IndexSet &source_index_set,
709  const MPI_Comm &mpi_comm);
710 # endif
711 
717  create_epetra_comm_pattern(const IndexSet &source_index_set,
718  const MPI_Comm &mpi_comm);
719 #endif
720 
725 
730 
735  std::shared_ptr<Utilities::MPI::CommunicationPatternBase> comm_pattern;
736 
740  std::unique_ptr<Number[], decltype(std::free) *> values;
741 
746  mutable std::shared_ptr<::parallel::internal::TBBPartitioner>
748 
749  // Make all other ReadWriteVector types friends.
750  template <typename Number2>
751  friend class ReadWriteVector;
752 
753  private:
759  template <typename Functor>
761  {
762  public:
767 
771  virtual void
773 
774  private:
779 
783  const Functor &functor;
784  };
785  };
786 
790  /*---------------------------- Inline functions ---------------------------*/
791 
792 #ifndef DOXYGEN
793 
794  template <typename Number>
796  : Subscriptor()
797  , values(nullptr, free)
798  {
799  // virtual functions called in constructors and destructors never use the
800  // override in a derived class
801  // for clarity be explicit on which function is called
803  }
804 
805 
806 
807  template <typename Number>
809  const ReadWriteVector<Number> &v)
810  : Subscriptor()
811  , values(nullptr, free)
812  {
813  this->operator=(v);
814  }
815 
816 
817 
818  template <typename Number>
820  : Subscriptor()
821  , values(nullptr, free)
822  {
823  // virtual functions called in constructors and destructors never use the
824  // override in a derived class
825  // for clarity be explicit on which function is called
827  }
828 
829 
830 
831  template <typename Number>
833  const IndexSet &locally_stored_indices)
834  : Subscriptor()
835  , values(nullptr, free)
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
840  ReadWriteVector<Number>::reinit(locally_stored_indices);
841  }
842 
843 
844 
845  template <typename Number>
846  inline typename ReadWriteVector<Number>::size_type
848  {
849  return stored_elements.size();
850  }
851 
852 
853 
854  template <typename Number>
855  inline typename ReadWriteVector<Number>::size_type
857  {
858  return stored_elements.n_elements();
859  }
860 
861 
862 
863  template <typename Number>
864  inline typename ReadWriteVector<Number>::size_type
866  {
867  return stored_elements.n_elements();
868  }
869 
870 
871 
872  template <typename Number>
873  inline const IndexSet &
875  {
876  return stored_elements;
877  }
878 
879 
880 
881  template <typename Number>
882  inline typename ReadWriteVector<Number>::iterator
884  {
885  return values.get();
886  }
887 
888 
889 
890  template <typename Number>
893  {
894  return values.get();
895  }
896 
897 
898 
899  template <typename Number>
900  inline typename ReadWriteVector<Number>::iterator
902  {
903  return values.get() + this->locally_owned_size();
904  }
905 
906 
907 
908  template <typename Number>
911  {
912  return values.get() + this->locally_owned_size();
913  }
914 
915 
916 
917  template <typename Number>
918  inline Number
920  {
922  }
923 
924 
925 
926  template <typename Number>
927  inline Number &
929  {
931  }
932 
933 
934 
935  template <typename Number>
936  inline Number
938  {
939  return operator()(global_index);
940  }
941 
942 
943 
944  template <typename Number>
945  inline Number &
947  {
948  return operator()(global_index);
949  }
950 
951 
952 
953  template <typename Number>
954  template <typename Number2>
955  inline void
957  const std::vector<size_type> &indices,
958  std::vector<Number2> & extracted_values) const
959  {
960  for (size_type i = 0; i < indices.size(); ++i)
961  extracted_values[i] = operator()(indices[i]);
962  }
963 
964 
965 
966  template <typename Number>
967  template <typename ForwardIterator, typename OutputIterator>
968  inline void
970  ForwardIterator indices_begin,
971  const ForwardIterator indices_end,
972  OutputIterator values_begin) const
973  {
974  while (indices_begin != indices_end)
975  {
976  *values_begin = operator()(*indices_begin);
977  indices_begin++;
978  values_begin++;
979  }
980  }
981 
982 
983 
984  template <typename Number>
985  inline Number
986  ReadWriteVector<Number>::local_element(const size_type local_index) const
987  {
988  AssertIndexRange(local_index, this->locally_owned_size());
989 
990  return values[local_index];
991  }
992 
993 
994 
995  template <typename Number>
996  inline Number &
998  {
999  AssertIndexRange(local_index, this->locally_owned_size());
1000 
1001  return values[local_index];
1002  }
1003 
1004 
1005 
1006  template <typename Number>
1007  template <typename Number2>
1008  inline void
1009  ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1010  const std::vector<Number2> & values)
1011  {
1012  AssertDimension(indices.size(), values.size());
1013  add(indices.size(), indices.data(), values.data());
1014  }
1015 
1016 
1017 
1018  template <typename Number>
1019  template <typename Number2>
1020  inline void
1021  ReadWriteVector<Number>::add(const std::vector<size_type> & indices,
1022  const ReadWriteVector<Number2> &values)
1023  {
1024  const size_type size = indices.size();
1025  for (size_type i = 0; i < size; ++i)
1026  {
1027  Assert(
1029  ExcMessage(
1030  "The given value is not finite but either infinite or Not A Number (NaN)"));
1031  this->operator()(indices[i]) += values[indices[i]];
1032  }
1033  }
1034 
1035 
1036 
1037  template <typename Number>
1038  template <typename Number2>
1039  inline void
1040  ReadWriteVector<Number>::add(const size_type n_indices,
1041  const size_type *indices,
1042  const Number2 * values_to_add)
1043  {
1044  for (size_type i = 0; i < n_indices; ++i)
1045  {
1046  Assert(
1048  ExcMessage(
1049  "The given value is not finite but either infinite or Not A Number (NaN)"));
1050  this->operator()(indices[i]) += values_to_add[i];
1051  }
1052  }
1053 
1054 
1055 
1056  template <typename Number>
1057  template <typename Functor>
1059  ReadWriteVector<Number> &parent,
1060  const Functor & functor)
1061  : parent(parent)
1062  , functor(functor)
1063  {}
1064 
1065 
1066 
1067  template <typename Number>
1068  template <typename Functor>
1069  void
1071  const size_type begin,
1072  const size_type end)
1073  {
1074  for (size_type i = begin; i < end; ++i)
1075  functor(parent.values[i]);
1076  }
1077 
1078 #endif // ifndef DOXYGEN
1079 
1080 } // end of namespace LinearAlgebra
1081 
1082 
1083 
1091 template <typename Number>
1092 inline void
1095 {
1096  u.swap(v);
1097 }
1098 
1099 
1101 
1102 #endif
size_type size() const
Definition: index_set.h:1641
size_type index_within_set(const size_type global_index) const
Definition: index_set.h:1856
size_type n_elements() const
Definition: index_set.h:1789
FunctorTemplate(ReadWriteVector< Number > &parent, const Functor &functor)
virtual void operator()(const size_type begin, const size_type end)
ReadWriteVector< Number > & operator=(const ReadWriteVector< Number2 > &in_vector)
void reinit(const ReadWriteVector< Number2 > &in_vector, const bool omit_zeroing_entries=false)
ReadWriteVector(const ReadWriteVector< Number > &in_vector)
ReadWriteVector(const size_type size)
std::unique_ptr< Number[], decltype(std::free) * > values
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
TpetraWrappers::CommunicationPattern create_tpetra_comm_pattern(const IndexSet &source_index_set, const MPI_Comm &mpi_comm)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true) const
Number & operator()(const size_type global_index)
void add(const std::vector< size_type > &indices, const ReadWriteVector< Number2 > &values)
ReadWriteVector< Number > & operator=(const ReadWriteVector< Number > &in_vector)
ReadWriteVector< Number > & operator=(const Number s)
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)
EpetraWrappers::CommunicationPattern create_epetra_comm_pattern(const IndexSet &source_index_set, const MPI_Comm &mpi_comm)
Number & local_element(const size_type local_index)
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)
const IndexSet & get_stored_elements() const
size_type locally_owned_size() const
const_iterator end() const
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< Number2 > &values) const
const_iterator begin() const
~ReadWriteVector() override=default
ReadWriteVector(const IndexSet &locally_stored_indices)
types::global_dof_index size_type
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
Number & operator[](const size_type global_index)
Definition: vector.h:109
void swap(LinearAlgebra::ReadWriteVector< Number > &u, LinearAlgebra::ReadWriteVector< Number > &v)
#define DEAL_II_DEPRECATED
Definition: config.h:162
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:461
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:462
#define Assert(cond, exc)
Definition: exceptions.h:1509
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1703
#define AssertIndexRange(index, range)
Definition: exceptions.h:1768
static ::ExceptionBase & ExcMessage(std::string arg1)
TrilinosWrappers::types::int_type global_index(const Epetra_BlockMap &map, const ::types::global_dof_index i)
void free(T *&pointer)
Definition: cuda.h:97
bool is_finite(const double x)
Definition: numbers.h:553
unsigned int global_dof_index
Definition: types.h:81