Reference documentation for deal.II version 8.5.1
read_write_vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2015 - 2017 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 at
12 // the top level of the deal.II distribution.
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 #include <deal.II/base/index_set.h>
21 #include <deal.II/base/mpi.h>
22 #include <deal.II/base/template_constraints.h>
23 #include <deal.II/base/types.h>
24 #include <deal.II/base/utilities.h>
25 #include <deal.II/base/memory_consumption.h>
26 #include <deal.II/base/thread_management.h>
27 #include <deal.II/lac/vector_view.h>
28 
29 #include <cstring>
30 #include <iomanip>
31 
32 #ifdef DEAL_II_WITH_TRILINOS
33 #include <deal.II/lac/trilinos_epetra_communication_pattern.h>
34 #include "Epetra_MultiVector.h"
35 #endif
36 
37 DEAL_II_NAMESPACE_OPEN
38 
39 namespace LinearAlgebra
40 {
41  class CommunicationPatternBase;
42  namespace distributed
43  {
44  template <typename> class Vector;
45  }
46 }
47 
48 #ifdef DEAL_II_WITH_PETSC
49 namespace PETScWrappers
50 {
51  namespace MPI
52  {
53  class Vector;
54  }
55 }
56 #endif
57 
58 #ifdef DEAL_II_WITH_TRILINOS
59 namespace TrilinosWrappers
60 {
61  namespace MPI
62  {
63  class Vector;
64  }
65 }
66 
67 namespace LinearAlgebra
68 {
69  namespace EpetraWrappers
70  {
71  class Vector;
72  }
73 }
74 #endif
75 
76 #ifdef DEAL_II_WITH_CUDA
77 namespace LinearAlgebra
78 {
79  namespace CUDAWrappers
80  {
81  template <typename> class Vector;
82  }
83 }
84 #endif
85 
86 namespace LinearAlgebra
87 {
127  template <typename Number>
128  class ReadWriteVector : public Subscriptor
129  {
130  public:
136  typedef Number value_type;
137  typedef value_type *pointer;
138  typedef const value_type *const_pointer;
139  typedef value_type *iterator;
140  typedef const value_type *const_iterator;
141  typedef value_type &reference;
142  typedef const value_type &const_reference;
143  typedef types::global_dof_index size_type;
144  typedef typename numbers::NumberTraits<Number>::real_type real_type;
145 
153  ReadWriteVector ();
154 
158  ReadWriteVector (const ReadWriteVector<Number> &in_vector);
159 
164  explicit ReadWriteVector (const size_type size);
165 
170  explicit ReadWriteVector (const IndexSet &locally_stored_indices);
171 
175  ~ReadWriteVector ();
176 
185  void reinit (const size_type size,
186  const bool omit_zeroing_entries = false);
187 
196  template <typename Number2>
197  void reinit(const ReadWriteVector<Number2> &in_vector,
198  const bool omit_zeroing_entries = false);
199 
209  void reinit (const IndexSet &locally_stored_indices,
210  const bool omit_zeroing_entries = false);
211 
212 #ifdef DEAL_II_WITH_CXX11
213 
226  template <typename Functor>
227  void apply(const Functor &func);
228 #endif
229 
242  void swap (ReadWriteVector<Number> &v);
243 
248  operator= (const ReadWriteVector<Number> &in_vector);
249 
253  template <typename Number2>
255  operator= (const ReadWriteVector<Number2> &in_vector);
256 
261  ReadWriteVector<Number> &operator = (const Number s);
262 
271  void import(const distributed::Vector<Number> &vec,
272  VectorOperation::values operation,
273  std_cxx11::shared_ptr<const CommunicationPatternBase> communication_pattern =
274  std_cxx11::shared_ptr<const CommunicationPatternBase> ());
275 
276 #ifdef DEAL_II_WITH_PETSC
277 
285  void import(const PETScWrappers::MPI::Vector &petsc_vec,
286  VectorOperation::values operation,
287  std_cxx11::shared_ptr<const CommunicationPatternBase> communication_pattern =
288  std_cxx11::shared_ptr<const CommunicationPatternBase> ());
289 #endif
290 
291 #ifdef DEAL_II_WITH_TRILINOS
292 
300  void import(const TrilinosWrappers::MPI::Vector &trilinos_vec,
301  VectorOperation::values operation,
302  std_cxx11::shared_ptr<const CommunicationPatternBase> communication_pattern =
303  std_cxx11::shared_ptr<const CommunicationPatternBase> ());
304 
313  void import(const EpetraWrappers::Vector &epetra_vec,
314  VectorOperation::values operation,
315  std_cxx11::shared_ptr<const CommunicationPatternBase> communication_pattern =
316  std_cxx11::shared_ptr<const CommunicationPatternBase> ());
317 #endif
318 
319 #ifdef DEAL_II_WITH_CUDA
320 
326  void import(const CUDAWrappers::Vector<Number> &cuda_vec,
327  VectorOperation::values operation,
328  std_cxx11::shared_ptr<const CommunicationPatternBase> communication_pattern =
329  std_cxx11::shared_ptr<const CommunicationPatternBase> ());
330 #endif
331 
340  size_type size() const;
341 
347  size_type n_elements() const;
348 
352  const IndexSet &get_stored_elements () const;
353 
359  iterator begin ();
360 
365  const_iterator begin () const;
366 
371  iterator end ();
372 
377  const_iterator end () const;
379 
380 
385 
391  Number operator () (const size_type global_index) const;
392 
398  Number &operator () (const size_type global_index);
399 
407  Number operator [] (const size_type global_index) const;
408 
416  Number &operator [] (const size_type global_index);
417 
424  template <typename Number2>
425  void extract_subvector_to (const std::vector<size_type> &indices,
426  std::vector<Number2> &values) const;
427 
432  template <typename ForwardIterator, typename OutputIterator>
433  void extract_subvector_to (ForwardIterator indices_begin,
434  const ForwardIterator indices_end,
435  OutputIterator values_begin) const;
436 
447  Number local_element (const size_type local_index) const;
448 
459  Number &local_element (const size_type local_index);
461 
462 
467 
472  template <typename Number2>
473  void add (const std::vector<size_type> &indices,
474  const std::vector<Number2> &values);
475 
480  template <typename Number2>
481  void add (const std::vector<size_type> &indices,
482  const ReadWriteVector<Number2> &values);
483 
489  template <typename Number2>
490  void add (const size_type n_elements,
491  const size_type *indices,
492  const Number2 *values);
493 
497  void print (std::ostream &out,
498  const unsigned int precision = 3,
499  const bool scientific = true) const;
500 
504  std::size_t memory_consumption () const;
506 
507  protected:
508 #ifdef DEAL_II_WITH_TRILINOS
509 
514  void import(const Epetra_MultiVector &multivector,
515  const IndexSet &locally_owned_elements,
516  VectorOperation::values operation,
517  const MPI_Comm &mpi_comm,
518  std_cxx11::shared_ptr<const CommunicationPatternBase> communication_pattern);
519 #endif
520 
524  unsigned int
525  global_to_local (const types::global_dof_index global_index) const
526  {
527  // the following will throw an exception if the global_index is not
528  // in the remaining_elements
529  return static_cast<unsigned int>(stored_elements.index_within_set(global_index));
530  }
531 
535  void resize_val (const size_type new_allocated_size);
536 
537 #if defined(DEAL_II_WITH_TRILINOS) && defined(DEAL_II_WITH_MPI)
538 
543  create_epetra_comm_pattern(const IndexSet &source_index_set,
544  const MPI_Comm &mpi_comm);
545 #endif
546 
551 
556 
561  std_cxx11::shared_ptr<CommunicationPatternBase> comm_pattern;
562 
566  Number *val;
567 
572  mutable std_cxx11::shared_ptr< ::parallel::internal::TBBPartitioner> thread_loop_partitioner;
573 
577  template <typename Number2> friend class ReadWriteVector;
578 
579 #ifdef DEAL_II_WITH_CXX11
580  private:
586  template <typename Functor>
588  {
589  public:
594  const Functor &functor);
595 
599  virtual void operator() (const size_type begin,
600  const size_type end);
601 
602  private:
607 
611  const Functor &functor;
612  };
613 #endif
614  };
615 
619  /*----------------------- Inline functions ----------------------------------*/
620 
621 #ifndef DOXYGEN
622 
623  template <typename Number>
624  inline
626  :
627  val(NULL)
628  {
629  reinit(0, true);
630  }
631 
632 
633 
634  template <typename Number>
635  inline
636  ReadWriteVector<Number>::ReadWriteVector (const ReadWriteVector<Number> &v)
637  :
638  Subscriptor(),
639  val(NULL)
640  {
641  this->operator=(v);
642  }
643 
644 
645 
646  template <typename Number>
647  inline
649  :
650  val(NULL)
651  {
652  reinit (size, false);
653  }
654 
655 
656 
657  template <typename Number>
658  inline
659  ReadWriteVector<Number>::ReadWriteVector (const IndexSet &locally_stored_indices)
660  :
661  val(NULL)
662  {
663  reinit (locally_stored_indices);
664  }
665 
666 
667 
668  template <typename Number>
669  inline
671  {
672  resize_val(0);
673  }
674 
675 
676 
677  template <typename Number>
678  inline
679  typename ReadWriteVector<Number>::size_type
681  {
682  return stored_elements.size();
683  }
684 
685 
686 
687  template <typename Number>
688  inline
689  typename ReadWriteVector<Number>::size_type
691  {
692  return stored_elements.n_elements();
693  }
694 
695 
696 
697  template <typename Number>
698  inline
699  const IndexSet &
701  {
702  return stored_elements;
703  }
704 
705 
706 
707  template <typename Number>
708  inline
709  typename ReadWriteVector<Number>::iterator
711  {
712  return &val[0];
713  }
714 
715 
716 
717  template <typename Number>
718  inline
719  typename ReadWriteVector<Number>::const_iterator
721  {
722  return &val[0];
723  }
724 
725 
726 
727  template <typename Number>
728  inline
729  typename ReadWriteVector<Number>::iterator
731  {
732  return &val[this->n_elements()];
733  }
734 
735 
736 
737  template <typename Number>
738  inline
739  typename ReadWriteVector<Number>::const_iterator
741  {
742  return &val[this->n_elements()];
743  }
744 
745 
746 
747  template <typename Number>
748  inline
749  Number
750  ReadWriteVector<Number>::operator() (const size_type global_index) const
751  {
752  return val[global_to_local(global_index)];
753  }
754 
755 
756 
757  template <typename Number>
758  inline
759  Number &
760  ReadWriteVector<Number>::operator() (const size_type global_index)
761  {
762  return val[global_to_local (global_index)];
763  }
764 
765 
766 
767  template <typename Number>
768  inline
769  Number
770  ReadWriteVector<Number>::operator[] (const size_type global_index) const
771  {
772  return operator()(global_index);
773  }
774 
775 
776 
777  template <typename Number>
778  inline
779  Number &
780  ReadWriteVector<Number>::operator[] (const size_type global_index)
781  {
782  return operator()(global_index);
783  }
784 
785 
786 
787  template <typename Number>
788  template <typename Number2>
789  inline
790  void ReadWriteVector<Number>::extract_subvector_to (const std::vector<size_type> &indices,
791  std::vector<Number2> &values) const
792  {
793  for (size_type i = 0; i < indices.size(); ++i)
794  values[i] = operator()(indices[i]);
795  }
796 
797 
798 
799  template <typename Number>
800  template <typename ForwardIterator, typename OutputIterator>
801  inline
802  void ReadWriteVector<Number>::extract_subvector_to (ForwardIterator indices_begin,
803  const ForwardIterator indices_end,
804  OutputIterator values_begin) const
805  {
806  while (indices_begin != indices_end)
807  {
808  *values_begin = operator()(*indices_begin);
809  indices_begin++;
810  values_begin++;
811  }
812  }
813 
814 
815 
816  template <typename Number>
817  inline
818  Number
819  ReadWriteVector<Number>::local_element (const size_type local_index) const
820  {
821  AssertIndexRange (local_index, this->n_elements());
822 
823  return val[local_index];
824  }
825 
826 
827 
828  template <typename Number>
829  inline
830  Number &
831  ReadWriteVector<Number>::local_element (const size_type local_index)
832  {
833  AssertIndexRange (local_index, this->n_elements());
834 
835  return val[local_index];
836  }
837 
838 
839 
840  template <typename Number>
841  template <typename Number2>
842  inline
843  void
844  ReadWriteVector<Number>::add (const std::vector<size_type> &indices,
845  const std::vector<Number2> &values)
846  {
847  AssertDimension (indices.size(), values.size());
848  add (indices.size(), &indices[0], &values[0]);
849  }
850 
851 
852 
853  template <typename Number>
854  template <typename Number2>
855  inline
856  void
857  ReadWriteVector<Number>::add (const std::vector<size_type> &indices,
858  const ReadWriteVector<Number2> &values)
859  {
860  const size_type size = indices.size();
861  for (size_type i=0; i<size; ++i)
862  {
863  Assert (numbers::is_finite(values[i]),
864  ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
865  this->operator()(indices[i]) += values[indices[i]];
866  }
867  }
868 
869 
870 
871  template <typename Number>
872  template <typename Number2>
873  inline
874  void
875  ReadWriteVector<Number>::add (const size_type n_indices,
876  const size_type *indices,
877  const Number2 *values)
878  {
879  for (size_type i=0; i<n_indices; ++i)
880  {
881  Assert (numbers::is_finite(values[i]),
882  ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
883  this->operator()(indices[i]) += values[i];
884  }
885  }
886 
887 
888 
889 #ifdef DEAL_II_WITH_CXX11
890  template <typename Number>
891  template <typename Functor>
892  inline
894  ReadWriteVector<Number> &parent,
895  const Functor &functor)
896  :
897  parent(parent),
898  functor(functor)
899  {}
900 
901 
902 
903  template <typename Number>
904  template <typename Functor>
905  void
907  const size_type end)
908  {
909  for (size_type i=begin; i<end; ++i)
910  functor(parent.val[i]);
911  }
912 #endif
913 
914 #endif // ifndef DOXYGEN
915 
916 } // end of namespace LinearAlgebra
917 
918 
919 
920 
928 template <typename Number>
929 inline
932 {
933  u.swap (v);
934 }
935 
936 
937 DEAL_II_NAMESPACE_CLOSE
938 
939 #endif
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1146
Number operator[](const size_type global_index) const
void swap(ReadWriteVector< Number > &v)
Number local_element(const size_type local_index) const
FunctorTemplate(ReadWriteVector< Number > &parent, const Functor &functor)
void resize_val(const size_type new_allocated_size)
#define AssertIndexRange(index, range)
Definition: exceptions.h:1170
std::size_t memory_consumption() const
void swap(LinearAlgebra::ReadWriteVector< Number > &u, LinearAlgebra::ReadWriteVector< Number > &v)
bool is_finite(const double x)
Definition: numbers.h:275
size_type size() const
Definition: index_set.h:1419
static ::ExceptionBase & ExcMessage(std::string arg1)
void reinit(const size_type size, const bool omit_zeroing_entries=false)
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:313
size_type index_within_set(const size_type global_index) const
Definition: index_set.h:1653
Number operator()(const size_type global_index) const
ReadWriteVector< Number > & operator=(const ReadWriteVector< Number > &in_vector)
EpetraWrappers::CommunicationPattern create_epetra_comm_pattern(const IndexSet &source_index_set, const MPI_Comm &mpi_comm)
std_cxx11::shared_ptr< ::parallel::internal::TBBPartitioner > thread_loop_partitioner
void add(const std::vector< size_type > &indices, const std::vector< Number2 > &values)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true) const
virtual void operator()(const size_type begin, const size_type end)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< Number2 > &values) const
void apply(const Functor &func)
unsigned int global_to_local(const types::global_dof_index global_index) const
size_type n_elements() const
size_type n_elements() const
Definition: index_set.h:1560
const IndexSet & get_stored_elements() const
std_cxx11::shared_ptr< CommunicationPatternBase > comm_pattern