📄 distributed_vector.h
字号:
// $Id: distributed_vector.h 2789 2008-04-13 02:24:40Z roystgnr $// The libMesh Finite Element Library.// Copyright (C) 2002-2007 Benjamin S. Kirk, John W. Peterson // This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version. // This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details. // You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#include "libmesh_common.h"#ifndef __distributed_vector_h__#define __distributed_vector_h__// C++ includes#include <vector>#include <algorithm>#include <complex>#include <limits>// Local includes#include "numeric_vector.h"#include "parallel.h"/** * Distributed vector. Provides an interface for simple * parallel, distributed vectors. Offers some collective * communication capabilities. Note that the class will * sill function without MPI, but only on one processor. * This lets us keep the parallel details behind the scenes. * * @author Benjamin S. Kirk, 2003 */template <typename T>class DistributedVector : public NumericVector<T>{public: /** * Dummy-Constructor. Dimension=0 */ DistributedVector (); /** * Constructor. Set dimension to \p n and initialize all elements with zero. */ DistributedVector (const unsigned int n); /** * Constructor. Set local dimension to \p n_local, the global dimension * to \p n, and initialize all elements with zero. */ DistributedVector (const unsigned int n, const unsigned int n_local); /** * Destructor, deallocates memory. Made virtual to allow * for derived classes to behave properly. */ ~DistributedVector (); /** * Call the assemble functions */ void close (); /** * @returns the \p DistributedVector to a pristine state. */ void clear (); /** * Set all entries to zero. Equivalent to \p v = 0, but more obvious and * faster. */ void zero (); /** * Creates a copy of this vector and returns it in an \p AutoPtr. */ AutoPtr<NumericVector<T> > clone () const; /** * Change the dimension of the vector to \p N. The reserved memory for * this vector remains unchanged if possible, to make things faster, but * this may waste some memory, so take this in the back of your head. * However, if \p N==0 all memory is freed, i.e. if you want to resize * the vector and release the memory not needed, you have to first call * \p init(0) and then \p init(N). This cited behaviour is analogous * to that of the STL containers. * * On \p fast==false, the vector is filled by * zeros. */ void init (const unsigned int N, const unsigned int n_local, const bool fast=false); /** * call init with n_local = N, */ void init (const unsigned int N, const bool fast=false); /** * \f$U(0-N) = s\f$: fill all components. */ NumericVector<T> & operator= (const T s); /** * \f$U = V\f$: copy all components. */ NumericVector<T> & operator= (const NumericVector<T> &V); /** * \f$U = V\f$: copy all components. */ DistributedVector<T> & operator= (const DistributedVector<T> &V); /** * \f$U = V\f$: copy all components. */ NumericVector<T> & operator= (const std::vector<T> &v); /** * @returns the minimum element in the vector. * In case of complex numbers, this returns the minimum * Real part. */ Real min () const; /** * @returns the maximum element in the vector. * In case of complex numbers, this returns the maximum * Real part. */ Real max () const; /** * @returns the sum of all values in the vector */ T sum() const; /** * @returns the \f$l_1\f$-norm of the vector, i.e. * the sum of the absolute values. */ Real l1_norm () const; /** * @returns the \f$l_2\f$-norm of the vector, i.e. * the square root of the sum of the * squares of the elements. */ Real l2_norm () const; /** * @returns the maximum absolute value of the * elements of this vector, which is the * \f$l_\infty\f$-norm of a vector. */ Real linfty_norm () const; /** * @returns dimension of the vector. This * function was formerly called \p n(), but * was renamed to get the \p DistributedVector class * closer to the C++ standard library's * \p std::vector container. */ unsigned int size () const; /** * @returns the local size of the vector * (index_stop-index_start) */ unsigned int local_size() const; /** * @returns the index of the first vector element * actually stored on this processor */ unsigned int first_local_index() const; /** * @returns the index of the last vector element * actually stored on this processor */ unsigned int last_local_index() const; /** * Access components, returns \p U(i). */ T operator() (const unsigned int i) const; /** * Addition operator. * Fast equivalent to \p U.add(1, V). */ NumericVector<T> & operator += (const NumericVector<T> &V); /** * Subtraction operator. * Fast equivalent to \p U.add(-1, V). */ NumericVector<T> & operator -= (const NumericVector<T> &V); /** * v(i) = value */ void set (const unsigned int i, const T value); /** * v(i) += value */ void add (const unsigned int i, const T value); /** * \f$U(0-DIM)+=s\f$. * Addition of \p s to all components. Note * that \p s is a scalar and not a vector. */ void add (const T s); /** * \f$U+=V\f$. * Simple vector addition, equal to the * \p operator +=. */ void add (const NumericVector<T>& V); /** * \f$U+=a*V\f$. * Simple vector addition, equal to the * \p operator +=. */ void add (const T a, const NumericVector<T>& v); /** * \f$U+=v\f$ where v is a \p std::vector<T> * and you * want to specify WHERE to add it */ void add_vector (const std::vector<T>& v, const std::vector<unsigned int>& dof_indices); /** * \f$U+=V\f$ where U and V are type * \p NumericVector<T> and you * want to specify WHERE to add * the \p NumericVector<T> V */ void add_vector (const NumericVector<T>& V, const std::vector<unsigned int>& dof_indices); /** * \f$U+=A*V\f$. * Add the product of a Sparse matrix \p A * and a Numeric vector \p V to this Numeric vector. * @e Not @e implemented. */ void add_vector (const NumericVector<T>&, const SparseMatrix<T>&) { libmesh_error(); } /** * \f$U+=V\f$ where U and V are type * \p DenseVector<T> and you * want to specify WHERE to add * the \p DenseVector<T> V */ void add_vector (const DenseVector<T>& V, const std::vector<unsigned int>& dof_indices); /** * \f$ U=v \f$ where v is a DenseVector<T> * and you want to specify WHERE to insert it */ virtual void insert (const std::vector<T>& v, const std::vector<unsigned int>& dof_indices); /** * \f$U=V\f$, where U and V are type * NumericVector<T> and you * want to specify WHERE to insert * the NumericVector<T> V */ virtual void insert (const NumericVector<T>& V, const std::vector<unsigned int>& dof_indices); /** * \f$ U+=V \f$ where U and V are type * DenseVector<T> and you * want to specify WHERE to insert * the DenseVector<T> V */ virtual void insert (const DenseVector<T>& V, const std::vector<unsigned int>& dof_indices); /** * Scale each element of the * vector by the given factor. */ void scale (const T factor); /** * Computes the dot product, p = U.V */ virtual T dot(const NumericVector<T>& V) const; /** * Creates a copy of the global vector in the * local vector \p v_local. */ void localize (std::vector<T>& v_local) const; /** * Same, but fills a \p NumericVector<T> instead of * a \p std::vector. */ void localize (NumericVector<T>& v_local) const; /** * Creates a local vector \p v_local containing * only information relevant to this processor, as * defined by the \p send_list. */ void localize (NumericVector<T>& v_local, const std::vector<unsigned int>& send_list) const; /** * Updates a local vector with selected values from neighboring * processors, as defined by \p send_list. */ void localize (const unsigned int first_local_idx, const unsigned int last_local_idx, const std::vector<unsigned int>& send_list); /** * Creates a local copy of the global vector in * \p v_local only on processor \p proc_id. By * default the data is sent to processor 0. This method * is useful for outputting data from one processor.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -