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;
287 template <
typename T>
292 _unclosed_state (DO_NOTHING),
295 _first_local_index(0),
296 _last_local_index (0)
303 template <
typename T>
310 this->
init(n, n,
false, ptype);
315 template <
typename T>
323 this->
init(n, n_local,
false, ptype);
328 template <
typename T>
333 const std::vector<numeric_index_type> & ghost,
337 this->
init(n, n_local, ghost,
false, ptype);
342 template <
typename T>
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;
420 this->_is_closed =
true;
428 template <
typename T>
432 const std::vector<numeric_index_type> & ,
437 this->
init(n, n_local, fast, ptype);
453 template <
typename T>
459 this->
init(n,n,fast,ptype);
464 template <
typename T>
469 _remote_values.clear();
471 _unclosed_state = DO_NOTHING;
476 _last_local_index = 0;
483 template <
typename T>
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(),
503 template <
typename T>
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;
515 template <
typename T>
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;
528 template <
typename T>
533 libmesh_assert_equal_to (_values.size(), _local_size);
534 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
541 template <
typename T>
546 libmesh_assert_equal_to (_values.size(), _local_size);
547 libmesh_assert_equal_to ((_last_local_index - _first_local_index), _local_size);
554 template <
typename T>
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;
567 template <
typename T>
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;
580 template <
typename T>
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];
595 template <
typename T>
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;
623 template <
typename T>
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;
649 template <
typename T>
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);
672 template <
typename T>
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);
694 template <
typename T>
714 template <
typename T>
719 return std::numeric_limits<typename std::vector<T>::size_type>::max();
725 #endif // LIBMESH_DISTRIBUTED_VECTOR_H
bool closed()
Checks that the library has been closed.
UnclosedState
Whether we are adding or setting remote values or neither - this determines behavior at the next clos...
virtual Real max() const override
virtual T dot(const NumericVector< T > &V) const override
numeric_index_type _last_local_index
The last component (+1) stored locally.
virtual void scale(const T factor) override
Scale each element of the vector by the given factor.
virtual void add_vector(const NumericVector< T > &, const SparseMatrix< T > &) override
Computes , i.e.
virtual numeric_index_type last_local_index() const override
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
virtual numeric_index_type size() const =0
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 & operator=(const DistributedVector &)
Copy assignment operator.
std::vector< std::pair< numeric_index_type, T > > _remote_values
Entries to add or set on remote processors during the next close()
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
const Parallel::Communicator & comm() const
virtual NumericVector< T > & operator/=(const NumericVector< T > &v) override
Computes the component-wise division of this vector's entries by another's, .
virtual void add(const numeric_index_type i, const T value) override
Adds value to the vector entry specified by i.
bool _is_initialized
true once init() has been called.
virtual Real linfty_norm() const override
The libMesh namespace provides an interface to certain functionality in the library.
numeric_index_type _global_size
The global vector size.
virtual std::unique_ptr< NumericVector< T > > zero_clone() const override
virtual numeric_index_type local_size() const override
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
Adds v to *this, .
uint8_t processor_id_type
virtual void reciprocal() override
Computes the component-wise reciprocal, .
virtual void zero() override
Set all entries to zero.
virtual Real l1_norm() const override
This class provides a simple parallel, distributed vector datatype which is specific to libmesh...
dof_id_type numeric_index_type
bool _is_initialized
Flag that tells if init() has been called.
virtual ~DistributedVector()=default
virtual numeric_index_type size() const override
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
virtual std::size_t max_allowed_id() const override
virtual void pointwise_divide(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
ParallelType _type
Type of vector.
virtual std::unique_ptr< NumericVector< T > > clone() const override
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
virtual void conjugate() override
Negates the imaginary component of each entry in the vector.
ParallelType type() const
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.
numeric_index_type _local_size
The local vector size.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual numeric_index_type first_local_index() const override
virtual NumericVector< T > & operator*=(const NumericVector< T > &v) override
Computes the component-wise multiplication of this vector's entries by another's, ...
virtual void localize(std::vector< T > &v_local) const override
Creates a copy of the global vector in the local vector v_local.
virtual Real l2_norm() const override
virtual numeric_index_type local_size() const =0
virtual void abs() override
Sets for each entry in the vector.
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...
bool initialized()
Checks that library initialization has been done.
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 clear() override
Restores the NumericVector<T> to a pristine state.
std::vector< T > _values
Actual vector datatype to hold vector entries.
virtual Real min() const override
UnclosedState _unclosed_state
The current state of this vector, if it is unclosed.
virtual T sum() const override
virtual void swap(NumericVector< T > &v) override
Swaps the contents of this with v.
virtual void add_vector_transpose(const NumericVector< T > &, const SparseMatrix< T > &) override
Computes , i.e.
DistributedVector(const Parallel::Communicator &comm, const ParallelType=AUTOMATIC)
Dummy-Constructor.
numeric_index_type _first_local_index
The first component stored locally.
bool _is_closed
Flag which tracks whether the vector's values are consistent on all processors after insertion or add...
ParallelType
Defines an enum for parallel data structure types.
virtual T operator()(const numeric_index_type i) const override