Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
Tpetra_Vector_def.hpp
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2008) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 
00042 #ifndef TPETRA_VECTOR_DEF_HPP
00043 #define TPETRA_VECTOR_DEF_HPP
00044 
00045 #include <Kokkos_DefaultArithmetic.hpp>
00046 #include <Kokkos_NodeTrace.hpp>
00047 #include <Tpetra_MultiVector.hpp>
00048 
00049 #ifdef DOXYGEN_USE_ONLY
00050   #include "Tpetra_Vector_decl.hpp"
00051 #endif
00052 
00053 namespace Tpetra {
00054 
00055   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00056   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00057   Vector (const Teuchos::RCP<const map_type>& map, const bool zeroOut)
00058     : base_type (map, 1, zeroOut)
00059   {}
00060 
00061   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00062   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00063   Vector (const Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& source)
00064     : base_type (source)
00065   {}
00066 
00067   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00068   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00069   Vector (const Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& source,
00070           const Teuchos::DataAccess copyOrView)
00071     : base_type (source, copyOrView)
00072   {}
00073 
00074   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00075   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00076   Vector (const Teuchos::RCP<const map_type>& map,
00077           const Teuchos::ArrayRCP<Scalar>& view,
00078           EPrivateHostViewConstructor /* dummy */)
00079     : base_type (map, view, view.size (), 1, HOST_VIEW_CONSTRUCTOR)
00080   {}
00081 
00082   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00083   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00084   Vector (const Teuchos::RCP<const map_type>& map,
00085           const Teuchos::ArrayRCP<Scalar>& view,
00086           EPrivateComputeViewConstructor /* dummy */)
00087     : base_type (map, view, view.size (), 1, COMPUTE_VIEW_CONSTRUCTOR)
00088   {}
00089 
00090   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00091   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00092   Vector (const Teuchos::RCP<const map_type>& map,
00093           const Teuchos::ArrayView<const Scalar>& values)
00094     : base_type (map, values, values.size (), 1)
00095   {}
00096 
00097   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00098   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00099   ~Vector ()
00100   {}
00101 
00102   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00103   void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::replaceGlobalValue(GlobalOrdinal globalRow, const Scalar &value) {
00104     this->base_type::replaceGlobalValue(globalRow,0,value);
00105   }
00106 
00107   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00108   void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::sumIntoGlobalValue(GlobalOrdinal globalRow, const Scalar &value) {
00109     this->base_type::sumIntoGlobalValue(globalRow,0,value);
00110   }
00111 
00112   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00113   void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::replaceLocalValue(LocalOrdinal myRow, const Scalar &value) {
00114     this->base_type::replaceLocalValue(myRow,0,value);
00115   }
00116 
00117   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00118   void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::sumIntoLocalValue(LocalOrdinal myRow, const Scalar &value) {
00119     this->base_type::sumIntoLocalValue(myRow,0,value);
00120   }
00121 
00122   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00123   void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::get1dCopy(ArrayView<Scalar> A) const {
00124     size_t lda = this->getLocalLength();
00125     this->get1dCopy(A,lda);
00126   }
00127 
00128   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00129   Scalar Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::dot(const Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &a) const {
00130     using Teuchos::outArg;
00131 #ifdef HAVE_TPETRA_DEBUG
00132     TEUCHOS_TEST_FOR_EXCEPTION( !this->getMap()->isCompatible(*a.getMap()), std::runtime_error,
00133         "Tpetra::Vector::dots(): Vectors do not have compatible Maps:" << std::endl
00134         << "this->getMap(): " << std::endl << *this->getMap()
00135         << "a.getMap(): " << std::endl << *a.getMap() << std::endl);
00136 #else
00137     TEUCHOS_TEST_FOR_EXCEPTION( this->getLocalLength() != a.getLocalLength(), std::runtime_error,
00138         "Tpetra::Vector::dots(): Vectors do not have the same local length.");
00139 #endif
00140     Scalar gbldot;
00141     gbldot = MVT::Dot(this->lclMV_,a.lclMV_);
00142     if (this->isDistributed()) {
00143       Scalar lcldot = gbldot;
00144       Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_SUM,lcldot,outArg(gbldot));
00145     }
00146     return gbldot;
00147   }
00148 
00149   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00150   Scalar
00151   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::meanValue () const
00152   {
00153     Scalar mean;
00154     this->meanValue (Teuchos::arrayView (&mean, 1));
00155     return mean;
00156   }
00157 
00158   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00159   typename Teuchos::ScalarTraits<Scalar>::magnitudeType
00160   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00161   norm1 () const
00162   {
00163     mag_type norm;
00164     this->norm1 (Teuchos::arrayView (&norm, 1));
00165     return norm;
00166   }
00167 
00168   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00169   typename Teuchos::ScalarTraits<Scalar>::magnitudeType
00170   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::norm2 () const
00171   {
00172     mag_type norm;
00173     this->norm2 (Teuchos::arrayView (&norm, 1));
00174     return norm;
00175   }
00176 
00177   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00178   typename Teuchos::ScalarTraits<Scalar>::magnitudeType
00179   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::normInf () const
00180   {
00181     mag_type norm;
00182     this->normInf (Teuchos::arrayView (&norm, 1));
00183     return norm;
00184   }
00185 
00186   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00187   typename Teuchos::ScalarTraits<Scalar>::magnitudeType
00188   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00189   normWeighted (const Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& weights) const
00190   {
00191     mag_type norm;
00192     this->normWeighted (weights, Teuchos::arrayView (&norm, 1));
00193     return norm;
00194   }
00195 
00196 
00197   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00198   Teuchos::RCP<const Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> >
00199   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00200   offsetView (const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& subMap,
00201               size_t offset) const
00202   {
00203     using Teuchos::RCP;
00204     using Teuchos::rcp;
00205     typedef Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> V;
00206 
00207     const size_t newNumRows = subMap->getNodeNumElements ();
00208     const bool tooManyElts = newNumRows + offset > this->lclMV_.getOrigNumRows ();
00209     if (tooManyElts) {
00210       const int myRank = this->getMap ()->getComm ()->getRank ();
00211       TEUCHOS_TEST_FOR_EXCEPTION(
00212         newNumRows + offset > MVT::getNumRows (this->lclMV_),
00213         std::runtime_error,
00214         "Tpetra::Vector::offsetView: Invalid input Map.  Input Map owns "
00215         << subMap->getNodeNumElements () << " elements on process " << myRank
00216         << ".  offset = " << offset << ".  Yet, the Vector contains only "
00217         << this->lclMV_.getOrigNumRows () << " on this process.");
00218     }
00219 
00220     KokkosClassic::MultiVector<Scalar, Node> newLocalMV =
00221       this->lclMV_.offsetView (newNumRows, this->lclMV_.getNumCols (), offset, 0);
00222     return rcp (new V (subMap, newLocalMV.getValuesNonConst (), COMPUTE_VIEW_CONSTRUCTOR));
00223   }
00224 
00225 
00226   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00227   Teuchos::RCP<Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> >
00228   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00229   offsetViewNonConst (const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& subMap,
00230                       size_t offset)
00231   {
00232     using Teuchos::RCP;
00233     using Teuchos::rcp;
00234     typedef Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> V;
00235 
00236     const size_t newNumRows = subMap->getNodeNumElements ();
00237     const bool tooManyElts = newNumRows + offset > this->lclMV_.getOrigNumRows ();
00238     if (tooManyElts) {
00239       const int myRank = this->getMap ()->getComm ()->getRank ();
00240       TEUCHOS_TEST_FOR_EXCEPTION(
00241         newNumRows + offset > MVT::getNumRows (this->lclMV_),
00242         std::runtime_error,
00243         "Tpetra::Vector::offsetViewNonConst: Invalid input Map.  Input Map owns "
00244         << subMap->getNodeNumElements () << " elements on process " << myRank
00245         << ".  offset = " << offset << ".  Yet, the MultiVector contains only "
00246         << this->lclMV_.getOrigNumRows () << " on this process.");
00247     }
00248 
00249     KokkosClassic::MultiVector<Scalar, Node> newLocalMV =
00250       this->lclMV_.offsetViewNonConst (newNumRows, this->lclMV_.getNumCols (), offset, 0);
00251     return rcp (new V (subMap, newLocalMV.getValuesNonConst (), COMPUTE_VIEW_CONSTRUCTOR));
00252   }
00253 
00254 
00255   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00256   std::string
00257   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::description() const
00258   {
00259     using Teuchos::TypeNameTraits;
00260 
00261     std::ostringstream oss;
00262     oss << "\"Tpetra::Vector\": {"
00263         << "Template parameters: {"
00264         << "Scalar: " << TypeNameTraits<Scalar>::name ()
00265         << ", LocalOrdinal: " << TypeNameTraits<LocalOrdinal>::name ()
00266         << ", GlobalOrdinal: " << TypeNameTraits<GlobalOrdinal>::name ()
00267         << ", Node: " << TypeNameTraits<Node>::name ()
00268         << "}";
00269     if (this->getObjectLabel () != "") {
00270       oss << ", Label: \"" << this->getObjectLabel () << "\", ";
00271     }
00272     oss << "Global length: " << this->getGlobalLength ()
00273         << "}";
00274     return oss.str ();
00275   }
00276 
00277   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00278   void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
00279   describe (Teuchos::FancyOStream& out,
00280             const Teuchos::EVerbosityLevel verbLevel) const
00281   {
00282     using std::endl;
00283     using std::setw;
00284     using Teuchos::Comm;
00285     using Teuchos::RCP;
00286     using Teuchos::TypeNameTraits;
00287     using Teuchos::VERB_DEFAULT;
00288     using Teuchos::VERB_NONE;
00289     using Teuchos::VERB_LOW;
00290     using Teuchos::VERB_MEDIUM;
00291     using Teuchos::VERB_HIGH;
00292     using Teuchos::VERB_EXTREME;
00293 
00294     const Teuchos::EVerbosityLevel vl =
00295       (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
00296 
00297     const Map<LocalOrdinal, GlobalOrdinal, Node>& map = * (this->getMap ());
00298     RCP<const Comm<int> > comm = map.getComm ();
00299     const int myImageID = comm->getRank ();
00300     const int numImages = comm->getSize ();
00301     Teuchos::OSTab tab0 (out);
00302 
00303     if (vl != VERB_NONE) {
00304       if (myImageID == 0) {
00305         out << "\"Tpetra::Vector\":" << endl;
00306       }
00307       Teuchos::OSTab tab1 (out);// applies to all processes
00308       if (myImageID == 0) {
00309         out << "Template parameters:";
00310         {
00311           Teuchos::OSTab tab2 (out);
00312           out << "Scalar: " << TypeNameTraits<Scalar>::name () << endl
00313               << "LocalOrdinal: " << TypeNameTraits<LocalOrdinal>::name () << endl
00314               << "GlobalOrdinal: " << TypeNameTraits<GlobalOrdinal>::name () << endl
00315               << "Node: " << TypeNameTraits<Node>::name () << endl;
00316         }
00317         out << endl;
00318         if (this->getObjectLabel () != "") {
00319           out << "Label: \"" << this->getObjectLabel () << "\"" << endl;
00320         }
00321         out << "Global length: " << this->getGlobalLength () << endl;
00322       }
00323       for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
00324         if (myImageID == imageCtr) {
00325           if (vl != VERB_LOW) {
00326             // VERB_MEDIUM and higher prints getLocalLength()
00327             out << "Process: " << myImageID << endl;
00328             Teuchos::OSTab tab2 (out);
00329             out << "Local length: " << this->getLocalLength () << endl;
00330 
00331             if (vl == VERB_EXTREME && this->getLocalLength () > 0) {
00332               // VERB_EXTREME prints values
00333               out << "Global indices and values:" << endl;
00334               Teuchos::OSTab tab3 (out);
00335               RCP<Node> node = this->lclMV_.getNode ();
00336               ArrayRCP<const Scalar> myview =
00337                 node->template viewBuffer<Scalar> (this->getLocalLength (),
00338                                                    MVT::getValues (this->lclMV_));
00339               for (size_t i = 0; i < this->getLocalLength (); ++i) {
00340                 out << map.getGlobalElement (i) << ": " << myview[i] << endl;
00341               }
00342             }
00343           }
00344           std::flush (out); // give output time to complete
00345         }
00346         comm->barrier (); // give output time to complete
00347         comm->barrier ();
00348         comm->barrier ();
00349       }
00350     }
00351   }
00352 
00353   template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00354   Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node >
00355   createCopy (const Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node >& src)
00356   {
00357     if (src.getCopyOrView () == Teuchos::Copy) { // copy semantics
00358       return src; // Copy constructor will make a deep copy.
00359     } else { // view semantics
00360       // Create a deep copy using the two-argument copy constructor,
00361       // set the result 'dst' to have view semantics (which 'src' also
00362       // has), and return it.
00363       Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> dst (src, Teuchos::Copy);
00364       dst.setCopyOrView (Teuchos::View);
00365       return dst;
00366     }
00367   }
00368 } // namespace Tpetra
00369 
00370 //
00371 // Explicit instantiation macro
00372 //
00373 // Must be expanded from within the Tpetra namespace!
00374 //
00375 
00376 #if defined(TPETRA_HAVE_KOKKOS_REFACTOR)
00377 #include "Tpetra_KokkosRefactor_Vector_def.hpp"
00378 #endif
00379 
00380 
00381 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
00382   \
00383   template class Vector< SCALAR , LO , GO , NODE >; \
00384   template Vector< SCALAR , LO , GO , NODE > createCopy( const Vector< SCALAR , LO , GO , NODE >& src); \
00385 
00386 
00387 #endif // TPETRA_VECTOR_DEF_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines