20#include "libmesh/libmesh_common.h"
24#ifndef LIBMESH_DISTRIBUTED_VECTOR_H
25#define LIBMESH_DISTRIBUTED_VECTOR_H
28#include "libmesh/int_range.h"
29#include "libmesh/numeric_vector.h"
30#include "libmesh/parallel.h"
90 const std::vector<numeric_index_type> & ghost,
111 virtual void close ()
override;
113 virtual void clear ()
override;
115 virtual void zero ()
override;
117 virtual std::unique_ptr<NumericVector<T>>
zero_clone ()
const override;
119 virtual std::unique_ptr<NumericVector<T>>
clone ()
const override;
123 const bool fast=
false,
127 const bool fast=
false,
132 const std::vector<numeric_index_type> & ghost,
133 const bool fast =
false,
137 const bool fast =
false)
override;
145 virtual Real min ()
const override;
147 virtual Real max ()
const override;
149 virtual T
sum()
const override;
183 virtual void add (
const T s)
override;
197 { libmesh_not_implemented(); }
201 { libmesh_not_implemented(); }
203 virtual void scale (
const T factor)
override;
205 virtual void abs()
override;
209 virtual void localize (std::vector<T> & v_local)
const override;
214 const std::vector<numeric_index_type> & send_list)
const override;
216 virtual void localize (std::vector<T> & v_local,
217 const std::vector<numeric_index_type> & indices)
const override;
221 const std::vector<numeric_index_type> & send_list)
override;
292 _unclosed_state (DO_NOTHING),
295 _first_local_index(0),
296 _last_local_index (0)
310 this->
init(n, n,
false, ptype);
323 this->
init(n, n_local,
false, ptype);
333 const std::vector<numeric_index_type> & ghost,
337 this->
init(n, n_local, ghost,
false, ptype);
350 parallel_object_only();
352 libmesh_assert_less_equal (n_local, n);
375 _unclosed_state = DO_NOTHING;
376 _values.resize(n_local);
377 _remote_values.clear();
378 _local_size = n_local;
381 _first_local_index = 0;
383#ifdef LIBMESH_HAVE_MPI
385 std::vector<numeric_index_type> local_sizes (this->n_processors(), 0);
387 local_sizes[this->processor_id()] = n_local;
389 this->comm().sum(local_sizes);
393 for (
auto p :
make_range(this->processor_id()))
394 _first_local_index += local_sizes[p];
402 for (
auto p :
make_range(this->n_processors()))
403 dbg_sum += local_sizes[p];
405 libmesh_assert_equal_to (dbg_sum, n);
412 libmesh_error_msg_if(n != n_local,
"ERROR: MPI is required for n != n_local!");
416 _last_local_index = _first_local_index + n_local;
419 this->_is_initialized =
true;
420 this->_is_closed =
true;
432 const std::vector<numeric_index_type> & ,
437 this->init(n, n_local, fast, ptype);
459 this->init(n,n,fast,ptype);
469 _remote_values.clear();
471 _unclosed_state = DO_NOTHING;
476 _last_local_index = 0;
478 this->_is_closed = this->_is_initialized =
false;
488 libmesh_assert_equal_to (_values.size(), _local_size);
489 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
493 libmesh_assert_equal_to (_unclosed_state, DO_NOTHING);
496 std::fill (_values.begin(),
507 std::unique_ptr<NumericVector<T>> cloned_vector =
508 std::make_unique<DistributedVector<T>>(this->comm());
509 cloned_vector->init(*
this);
510 return cloned_vector;
519 std::unique_ptr<NumericVector<T>> cloned_vector =
520 std::make_unique<DistributedVector<T>>(this->comm());
521 cloned_vector->init(*
this,
true);
522 *cloned_vector = *
this;
523 return cloned_vector;
533 libmesh_assert_equal_to (_values.size(), _local_size);
534 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
546 libmesh_assert_equal_to (_values.size(), _local_size);
547 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
559 libmesh_assert_equal_to (_values.size(), _local_size);
560 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
562 return _first_local_index;
572 libmesh_assert_equal_to (_values.size(), _local_size);
573 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
575 return _last_local_index;
585 libmesh_assert_equal_to (_values.size(), _local_size);
586 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
588 (i < last_local_index())) );
590 return _values[i - _first_local_index];
600 libmesh_assert_equal_to (_values.size(), _local_size);
601 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
602 libmesh_assert_less (i, size());
604 _unclosed_state == SET_VALUES);
606 std::scoped_lock lock(this->_numeric_vector_mutex);
607 if (i >= first_local_index() && i < last_local_index())
609 _values[i - _first_local_index] =
value;
613 _remote_values.emplace_back(i,
value);
614 _unclosed_state = SET_VALUES;
617 this->_is_closed =
false;
628 libmesh_assert_equal_to (_values.size(), _local_size);
629 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
630 libmesh_assert_less (i, size());
632 std::scoped_lock lock(this->_numeric_vector_mutex);
634 if (i >= first_local_index() && i < last_local_index())
636 _values[i - _first_local_index] +=
value;
640 _remote_values.emplace_back(i,
value);
641 _unclosed_state = ADD_VALUES;
644 this->_is_closed =
false;
654 parallel_object_only();
658 libmesh_assert_equal_to (_values.size(), _local_size);
659 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
661 Real local_min = std::numeric_limits<Real>::max();
662 for (
auto v : _values)
665 this->comm().min(local_min);
677 parallel_object_only();
681 libmesh_assert_equal_to (_values.size(), _local_size);
682 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
684 Real local_max = -std::numeric_limits<Real>::max();
685 for (
auto v : _values)
688 this->comm().max(local_max);
719 return std::numeric_limits<typename std::vector<T>::size_type>::max();
This class provides a simple parallel, distributed vector datatype which is specific to libmesh.
virtual void localize(std::vector< T > &v_local) const override
Creates a copy of the global vector in the local vector v_local.
virtual numeric_index_type local_size() const override
virtual void pointwise_divide(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
DistributedVector & operator=(const DistributedVector &)
Copy assignment operator.
virtual NumericVector< T > & operator*=(const NumericVector< T > &v) override
Computes the component-wise multiplication of this vector's entries by another's, .
virtual Real max() const override
std::vector< T > _values
Actual vector datatype to hold vector entries.
numeric_index_type _first_local_index
The first component stored locally.
virtual NumericVector< T > & operator/=(const NumericVector< T > &v) override
Computes the component-wise division of this vector's entries by another's, .
virtual void set(const numeric_index_type i, const T value) override
Sets v(i) = value.
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
Subtracts v from *this, .
DistributedVector(const Parallel::Communicator &comm, const ParallelType=AUTOMATIC)
Dummy-Constructor.
UnclosedState _unclosed_state
The current state of this vector, if it is unclosed.
virtual void add_vector(const NumericVector< T > &, const SparseMatrix< T > &) override
Computes , i.e.
numeric_index_type _last_local_index
The last component (+1) stored locally.
virtual Real l1_norm() const override
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC) override
Change the dimension of the vector to n.
virtual void reciprocal() override
Computes the component-wise reciprocal, .
virtual void add_vector_transpose(const NumericVector< T > &, const SparseMatrix< T > &) override
Computes , i.e.
virtual void conjugate() override
Negates the imaginary component of each entry in the vector.
DistributedVector(const DistributedVector &)=default
virtual void localize_to_one(std::vector< T > &v_local, const processor_id_type proc_id=0) const override
Creates a local copy of the global vector in v_local only on processor proc_id.
virtual void zero() override
Set all entries to zero.
virtual Real l2_norm() const override
virtual std::unique_ptr< NumericVector< T > > clone() const override
virtual void clear() override
Restores the NumericVector<T> to a pristine state.
virtual void scale(const T factor) override
Scale each element of the vector by the given factor.
virtual void add(const numeric_index_type i, const T value) override
Adds value to the vector entry specified by i.
virtual Real linfty_norm() const override
UnclosedState
Whether we are adding or setting remote values or neither - this determines behavior at the next clos...
virtual std::unique_ptr< NumericVector< T > > zero_clone() const override
std::vector< std::pair< numeric_index_type, T > > _remote_values
Entries to add or set on remote processors during the next close()
virtual numeric_index_type first_local_index() const override
numeric_index_type _global_size
The global vector size.
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
Adds v to *this, .
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
virtual void swap(NumericVector< T > &v) override
Swaps the contents of this with v.
virtual T sum() const override
virtual T dot(const NumericVector< T > &V) const override
numeric_index_type _local_size
The local vector size.
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
DistributedVector(DistributedVector &&)=default
The 5 special functions can be defaulted for this class, as it does not manage any memory itself.
virtual ~DistributedVector()=default
virtual void abs() override
Sets for each entry in the vector.
virtual T operator()(const numeric_index_type i) const override
virtual Real min() const override
virtual numeric_index_type last_local_index() const override
virtual numeric_index_type size() const override
virtual std::size_t max_allowed_id() const override
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
ParallelType type() const
ParallelType _type
Type of vector.
bool _is_initialized
true once init() has been called.
virtual numeric_index_type size() const =0
bool _is_closed
Flag which tracks whether the vector's values are consistent on all processors after insertion or add...
virtual numeric_index_type local_size() const =0
const Parallel::Communicator & comm() const
The libMesh namespace provides an interface to certain functionality in the library.
ParallelType
Defines an enum for parallel data structure types.
bool closed()
Checks that the library has been closed.
dof_id_type numeric_index_type
bool initialized()
Checks that library initialization has been done.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...