16 #ifndef dealii__parallel_block_vector_h 17 #define dealii__parallel_block_vector_h 20 #include <deal.II/base/config.h> 21 #include <deal.II/base/exceptions.h> 22 #include <deal.II/lac/block_indices.h> 23 #include <deal.II/lac/block_vector_base.h> 24 #include <deal.II/lac/parallel_vector.h> 26 #include <deal.II/lac/petsc_parallel_block_vector.h> 27 #include <deal.II/lac/trilinos_parallel_block_vector.h> 33 DEAL_II_NAMESPACE_OPEN
62 template <
typename Number>
81 typedef typename BaseClass::pointer pointer;
82 typedef typename BaseClass::const_pointer const_pointer;
83 typedef typename BaseClass::reference reference;
84 typedef typename BaseClass::const_reference const_reference;
85 typedef typename BaseClass::size_type size_type;
99 explicit BlockVector (
const size_type num_blocks = 0,
100 const size_type block_size = 0);
109 #ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG 122 template <
typename OtherNumber>
131 BlockVector (
const std::vector<size_type> &block_sizes);
137 BlockVector (
const std::vector<IndexSet> &local_ranges,
138 const std::vector<IndexSet> &ghost_indices,
139 const MPI_Comm communicator);
144 BlockVector (
const std::vector<IndexSet> &local_ranges,
145 const MPI_Comm communicator);
169 template <
class Number2>
179 #ifdef DEAL_II_WITH_PETSC 191 #ifdef DEAL_II_WITH_TRILINOS 217 void reinit (
const size_type num_blocks,
218 const size_type block_size = 0,
219 const bool omit_zeroing_entries =
false);
240 void reinit (
const std::vector<size_type> &N,
241 const bool omit_zeroing_entries=
false);
257 template <
typename Number2>
259 const bool omit_zeroing_entries=
false);
285 void compress (::VectorOperation::values operation);
334 template <
typename Number2>
340 template <
typename Number2>
346 template <
typename Number2>
377 real_type
lp_norm (
const real_type p)
const;
409 template <
class BlockVector2>
410 void scale (
const BlockVector2 &v);
449 template <
typename Number>
452 const size_type block_size)
454 reinit (n_blocks, block_size);
459 template <
typename Number>
467 template <
typename Number>
470 const std::vector<IndexSet> &ghost_indices,
471 const MPI_Comm communicator)
473 std::vector<size_type> sizes(local_ranges.size());
474 for (
unsigned int i=0; i<local_ranges.size(); ++i)
475 sizes[i] = local_ranges[i].
size();
480 for (
unsigned int i=0; i<this->
n_blocks(); ++i)
481 this->
block(i).reinit(local_ranges[i], ghost_indices[i], communicator);
485 template <
typename Number>
488 const MPI_Comm communicator)
490 std::vector<size_type> sizes(local_ranges.size());
491 for (
unsigned int i=0; i<local_ranges.size(); ++i)
492 sizes[i] = local_ranges[i].
size();
497 for (
unsigned int i=0; i<this->
n_blocks(); ++i)
498 this->
block(i).reinit(local_ranges[i], communicator);
503 template <
typename Number>
512 for (size_type i=0; i<this->
n_blocks(); ++i)
517 #ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG 519 template <
typename Number>
520 template <
typename OtherNumber>
532 template <
typename Number>
535 const size_type bl_sz,
536 const bool omit_zeroing_entries)
538 std::vector<size_type> n(n_bl, bl_sz);
539 reinit(n, omit_zeroing_entries);
543 template <
typename Number>
546 const bool omit_zeroing_entries)
548 this->block_indices.reinit (n);
549 if (this->components.size() != this->n_blocks())
550 this->components.resize(this->n_blocks());
552 for (size_type i=0; i<this->n_blocks(); ++i)
553 this->components[i].reinit(n[i], omit_zeroing_entries);
558 template <
typename Number>
559 template <
typename Number2>
562 const bool omit_zeroing_entries)
565 if (this->components.size() != this->n_blocks())
566 this->components.resize(this->n_blocks());
568 for (
unsigned int i=0; i<this->n_blocks(); ++i)
569 this->block(i).
reinit(v.
block(i), omit_zeroing_entries);
574 template <
typename Number>
581 template <
typename Number>
589 BaseClass::operator = (s);
595 template <
typename Number>
602 Assert (this->n_blocks() == 0 || this->n_blocks() == v.
n_blocks(),
603 ExcDimensionMismatch(this->n_blocks(), v.
n_blocks()));
605 if (this->n_blocks() != v.
n_blocks())
608 for (size_type i=0; i<this->n_blocks(); ++i)
609 this->components[i] = v.
block(i);
611 this->collect_sizes();
617 template <
typename Number>
622 BaseClass::operator = (v);
628 template <
typename Number>
629 template <
typename Number2>
635 BaseClass::operator = (v);
641 #ifdef DEAL_II_WITH_PETSC 643 template <
typename Number>
649 for (
unsigned int i=0; i<this->n_blocks(); ++i)
650 this->block(i) = petsc_vec.
block(i);
659 #ifdef DEAL_II_WITH_TRILINOS 661 template <
typename Number>
667 for (
unsigned int i=0; i<this->n_blocks(); ++i)
668 this->block(i) = trilinos_vec.
block(i);
677 template <
typename Number>
684 for (
unsigned int block=0; block<this->n_blocks(); ++block)
685 this->block(block).compress_start(block*10 + 8273, operation);
686 for (
unsigned int block=0; block<this->n_blocks(); ++block)
687 this->block(block).compress_finish(operation);
692 template <
typename Number>
697 for (
unsigned int block=0; block<this->n_blocks(); ++block)
698 this->block(block).update_ghost_values_start(block*10 + 9923);
699 for (
unsigned int block=0; block<this->n_blocks(); ++block)
700 this->block(block).update_ghost_values_finish();
705 template <
typename Number>
710 for (
unsigned int block=0; block<this->n_blocks(); ++block)
711 this->block(block).zero_out_ghosts();
716 template <
typename Number>
721 bool has_ghost_elements =
false;
722 for (
unsigned int block=0; block<this->n_blocks(); ++block)
723 if (this->block(block).has_ghost_elements() ==
true)
724 has_ghost_elements =
true;
725 return has_ghost_elements;
730 template <
typename Number>
735 Assert (this->n_blocks() > 0, ExcEmptyObject());
742 int local_result = -1;
743 for (
unsigned int i=0; i<this->n_blocks(); ++i)
744 local_result = std::max(local_result,
745 -static_cast<int>(this->block(i).all_zero_local()));
747 if (this->block(0).partitioner->n_mpi_processes() > 1)
749 this->block(0).partitioner->get_communicator());
756 template <
typename Number>
761 Assert (this->n_blocks() > 0, ExcEmptyObject());
762 int local_result = -1;
763 for (
unsigned int i=0; i<this->n_blocks(); ++i)
764 local_result = std::max(local_result,
765 -static_cast<int>(this->block(i).is_non_negative_local()));
766 if (this->block(0).partitioner->n_mpi_processes() > 1)
768 this->block(0).partitioner->get_communicator());
775 template <
typename Number>
776 template <
typename Number2>
781 Assert (this->n_blocks() > 0, ExcEmptyObject());
786 unsigned int local_result = 0;
787 for (
unsigned int i=0; i<this->n_blocks(); ++i)
788 local_result = std::max(local_result,
789 static_cast<unsigned int>(!this->block(i).vectors_equal_local(v.
block(i))));
790 unsigned int result =
791 this->block(0).partitioner->n_mpi_processes() > 1
801 template <
typename Number>
802 template <
typename Number2>
807 return !(operator == (v));
812 template <
typename Number>
813 template <
typename Number2>
818 Assert (this->n_blocks() > 0, ExcEmptyObject());
821 Number local_result = Number();
822 for (
unsigned int i=0; i<this->n_blocks(); ++i)
823 local_result += this->block(i).inner_product_local(v.
block(i));
825 if (this->block(0).partitioner->n_mpi_processes() > 1)
827 this->block(0).partitioner->get_communicator());
834 template <
typename Number>
836 typename BlockVector<Number>::real_type
839 Assert (this->n_blocks() > 0, ExcEmptyObject());
841 real_type local_result = real_type();
842 for (
unsigned int i=0; i<this->n_blocks(); ++i)
843 local_result += this->block(i).norm_sqr_local();
845 if (this->block(0).partitioner->n_mpi_processes() > 1)
847 this->block(0).partitioner->get_communicator());
854 template <
typename Number>
859 Assert (this->n_blocks() > 0, ExcEmptyObject());
861 Number local_result = Number();
862 for (
unsigned int i=0; i<this->n_blocks(); ++i)
863 local_result += this->block(i).mean_value_local()*(real_type)this->block(i).partitioner->local_size();
865 if (this->block(0).partitioner->n_mpi_processes() > 1)
867 this->block(0).partitioner->get_communicator())/
868 (real_type)this->size();
870 return local_result/(real_type)this->size();
875 template <
typename Number>
877 typename BlockVector<Number>::real_type
880 Assert (this->n_blocks() > 0, ExcEmptyObject());
882 real_type local_result = real_type();
883 for (
unsigned int i=0; i<this->n_blocks(); ++i)
884 local_result += this->block(i).l1_norm_local();
886 if (this->block(0).partitioner->n_mpi_processes() > 1)
888 this->block(0).partitioner->get_communicator());
895 template <
typename Number>
897 typename BlockVector<Number>::real_type
900 return std::sqrt(norm_sqr());
905 template <
typename Number>
907 typename BlockVector<Number>::real_type
910 Assert (this->n_blocks() > 0, ExcEmptyObject());
912 real_type local_result = real_type();
913 for (
unsigned int i=0; i<this->n_blocks(); ++i)
914 local_result += std::pow(this->block(i).lp_norm_local(p), p);
916 if (this->block(0).partitioner->n_mpi_processes() > 1)
918 this->block(0).partitioner->get_communicator()),
919 static_cast<real_type>(1.0/p));
921 return std::pow (local_result, static_cast<real_type>(1.0/p));
926 template <
typename Number>
928 typename BlockVector<Number>::real_type
931 Assert (this->n_blocks() > 0, ExcEmptyObject());
933 real_type local_result = real_type();
934 for (
unsigned int i=0; i<this->n_blocks(); ++i)
935 local_result = std::max(local_result, this->block(i).linfty_norm_local());
937 if (this->block(0).partitioner->n_mpi_processes() > 1)
939 this->block(0).partitioner->get_communicator());
946 template <
typename Number>
953 Number local_result = Number();
954 for (
unsigned int i=0; i<this->n_blocks(); ++i)
955 local_result += this->block(i).add_and_dot_local(a, V.
block(i), W.
block(i));
957 if (this->block(0).partitioner->n_mpi_processes() > 1)
959 this->block(0).partitioner->get_communicator());
966 template <
typename Number>
972 ExcDimensionMismatch(this->n_blocks(), v.
n_blocks()));
974 for (size_type i=0; i<this->n_blocks(); ++i)
975 ::swap (this->components[i], v.
components[i]);
981 template <
typename Number>
982 template <
class BlockVector2>
985 BaseClass::scale (v);
1002 template <
typename Number>
1010 DEAL_II_NAMESPACE_CLOSE
bool has_ghost_elements() const
#define AssertDimension(dim1, dim2)
void scale(const BlockVector2 &v)
void swap(parallel::distributed::BlockVector< Number > &u, parallel::distributed::BlockVector< Number > &v)
BlockVector & operator=(const value_type s)
void compress(::VectorOperation::values operation)
Number mean_value() const
BaseClass::BlockType BlockType
DeclException0(ExcIteratorRangeDoesNotMatchVectorSize)
BlockVectorBase< Vector< Number > > BaseClass
real_type l2_norm() const
BlockIndices block_indices
real_type linfty_norm() const
T sum(const T &t, const MPI_Comm &mpi_communicator)
const BlockIndices & get_block_indices() const
#define Assert(cond, exc)
BlockType::real_type real_type
Vector< Number > BlockType
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
Number add_and_dot(const Number a, const BlockVector< Number > &V, const BlockVector< Number > &W)
bool operator!=(const BlockVector< Number2 > &v) const
void reinit(const size_type num_blocks, const size_type block_size=0, const bool omit_zeroing_entries=false)
void swap(BlockVector< Number > &v)
std::vector< Vector< Number > > components
real_type lp_norm(const real_type p) const
unsigned int n_blocks() const
bool is_non_negative() const
real_type l1_norm() const
bool operator==(const BlockVector< Number2 > &v) const
BaseClass::value_type value_type
void update_ghost_values() const
Number operator*(const BlockVector< Number2 > &V) const
BlockType & block(const unsigned int i)
#define AssertIsFinite(number)
T max(const T &t, const MPI_Comm &mpi_communicator)
BlockVector(const size_type num_blocks=0, const size_type block_size=0)
real_type norm_sqr() const