// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /** @ingroup la_group * \class ROL::MultiVectorDefault * \brief Default implementation of the ROL::MultiVector container class */ #ifndef ROL_MULTIVECTOR_DEFAULT_HPP #define ROL_MULTIVECTOR_DEFAULT_HPP #include "ROL_MultiVector.hpp" namespace ROL { template class MultiVectorDefault : public MultiVector { typedef Vector V; // Single vector typedef ROL::Ptr PV; // Pointer to a vector typedef Teuchos::ArrayRCP APV; // Array of pointers to vectors typedef MultiVector MV; // Instance of this class typedef ROL::Ptr PMV; // Pointer to an instance of this class private: APV mvec_; // Array of pointers to vectors int numVectors_; // number of vectors (elements in the array) int length_; // number of elements in a vector virtual bool dimensionMismatch(const MV &A) const { bool equalWidth = ( A.getNumberOfVectors() != numVectors_ ); bool equalLength = ( A.getLength() != length_ ); return ( equalWidth | equalLength ); } public: // Create a MultiVector from an array of pointers to vectors MultiVectorDefault(APV mvec) : mvec_(mvec), numVectors_(mvec.size()), length_(mvec[0]->dimension()) {} // Create a MultiVector from a pointer to a single vector MultiVectorDefault(PV vec) : mvec_(APV(1,vec)), numVectors_(1), length_(vec->dimension()) {} // Create a MultiVector from a pointer to a constant vector ~MultiVectorDefault() {} // Make a new MultiVector of the same dimensions PMV clone() const { APV x(numVectors_); for(int i=0;iclone(); } return ROL::makePtr>(x); } // Make a new MultiVector of specified dimension PMV clone( const int numvecs ) const { APV x(numvecs); for(int i=0;iclone(); } return ROL::makePtr>(x); } // Make a deep copy of this MultiVector PMV deepCopy() const { APV x(numVectors_); for(int i=0;iclone(); x[i]->set(*mvec_[i]); } return ROL::makePtr>(x); } // Make a deep copy specified vectors in the MultiVector PMV deepCopy(const std::vector &index) const { int n = index.size(); APV x(n); for(int i=0;iclone(); x[i]->set(*mvec_[j]); } return ROL::makePtr>(x); } // Make a shallow copy specified vectors in the MultiVector PMV shallowCopy(const std::vector &index) { int n = index.size(); APV x(n); for(int i=0;i>(x); } // Make a const shallow copy specified vectors in the MultiVector const PMV shallowCopyConst(const std::vector &index) const { int n = index.size(); APV x(n); for(int i=0;i>(x); } // Get the number of elements of a vector in the MultiVector ptrdiff_t getLength() const { return length_; } // Get the number of vectors in the MultiVector int getNumberOfVectors() const { return numVectors_; } void axpy(const Real alpha, const MV& x) { for(int i=0;iaxpy(alpha,*(x.getVector(i))); } } // Generic BLAS level 3 matrix multiplication // \f$\text{this}\leftarrow \alpha A B+\beta\text{this}\f$ void gemm(const Real alpha, const MV& A, const Teuchos::SerialDenseMatrix &B, const Real beta) { // Scale this by beta this->scale(beta); for(int i=0;iaxpy(alpha*B(i,j),*A.getVector(i)); } } } // Scale the MultiVector by a single scalar alpha // \f$\text{this}\leftarrow\alpha\text{this}\f$ void scale(const Real alpha) { for(int i=0;iscale(alpha); } } // Scale each vector in the MultiVector by a different alpha // \f$\text{this}[i]\leftarrow\alpha[i]\text{this}[i]\f$ void scale(const std::vector &alpha) { ROL_TEST_FOR_EXCEPTION( static_cast(alpha.size()) != numVectors_, std::invalid_argument, "Error: alpha must have the same length as the number of vectors."); for(int i=0;iscale(alpha[i]); } } // Set the MultiVector equal to another MultiVector void set(const MV &A) { // ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A), // std::invalid_argument, // "Error: MultiVectors must have the same dimensions."); for(int i=0;iset(*(A.getVector(i))); } } // Set some of the vectors in this MultiVector equal to corresponding // vectors in another MultiVector void set(const MV &A, const std::vector &index) { // ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A), // std::invalid_argument, // "Error: MultiVectors must have the same dimensions."); int n = index.size(); for(int i=0;iset(*A.getVector(i)); } } } // Compute \f$\alpha A^\top \text{this}\f$ void innerProducts(const Real alpha, const MV &A, Teuchos::SerialDenseMatrix &B) const { // ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A), // std::invalid_argument, // "Error: MultiVectors must have the same dimensions."); for(int i=0;idot(*A.getVector(i)); } } } // Compute dot products of pairs of vectors void dots(const MV &A, std::vector &b) const { ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A), std::invalid_argument, "Error: MultiVectors must have the same dimensions."); for(int i=0;idot(*A.getVector(i)); } } // Compute the norm of each vector in the MultiVector void norms(std::vector &normvec) const { int min = numVectors_ < static_cast(normvec.size()) ? numVectors_ : normvec.size(); for(int i=0;inorm(); } } // Zero each of the vectors in the MultiVector void zero() { for(int i=0;izero(); } } // Return a pointer to the ith vector PV getVector(int i) const { ROL_TEST_FOR_EXCEPTION( i>=numVectors_, std::invalid_argument, "Error: index out of bounds"); return mvec_[i]; } }; } #endif