Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Kokkos_CrsMatrix.hpp
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2009) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ************************************************************************
00027 //@HEADER
00028 
00029 #ifndef KOKKOS_CRSMATRIX_HPP
00030 #define KOKKOS_CRSMATRIX_HPP
00031 
00032 #include <Teuchos_RCP.hpp>
00033 #include <Teuchos_TypeNameTraits.hpp>
00034 #include <Teuchos_TestForException.hpp>
00035 #include <Teuchos_ArrayRCP.hpp>
00036 
00037 #include "Kokkos_ConfigDefs.hpp"
00038 #include "Kokkos_DefaultNode.hpp"
00039 #include "Kokkos_CrsGraph.hpp"
00040 
00041 namespace Kokkos {
00042 
00043   //=========================================================================================================================
00044   // 
00045   // A host-resident CrsMatrix
00046   // 
00047   //=========================================================================================================================
00048 
00052   template <class Scalar, 
00053             class Ordinal, 
00054             class Node,
00055             class LocalMatOps>
00056   class CrsMatrixHostCompute {
00057   public:
00058 
00059     typedef Scalar                ScalarType;
00060     typedef Ordinal               OrdinalType;
00061     typedef Node                  NodeType;
00062     typedef LocalMatOps           LocalMatOpsType;
00063 
00065 
00066 
00068     CrsMatrixHostCompute();
00069 
00071     CrsMatrixHostCompute(CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &graph);
00072 
00074     CrsMatrixHostCompute(const CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &graph);
00075 
00077     virtual ~CrsMatrixHostCompute();
00078 
00080 
00082 
00083 
00085     void setOwnedGraph(CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &graph);
00086 
00088     void setStaticGraph(const CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &graph);
00089     
00091 
00093 
00094 
00096     RCP<Node> getNode() const;
00097 
00099     size_t getNumRows() const;
00100 
00102     size_t getNumEntries() const;
00103 
00105     bool isEmpty() const;
00106 
00108     bool isFinalized() const;
00109 
00112     bool is1DStructure() const;
00113 
00116     bool is2DStructure() const;
00117 
00119     bool isOptimized() const;
00120 
00122 
00125     void set1DValues(ArrayRCP<Scalar> vals);
00126 
00128 
00131     void set2DValues(ArrayRCP<ArrayRCP<Scalar> > vals);
00132 
00134 
00138     void get1DValues(ArrayRCP<Scalar> &vals);
00139 
00141 
00145     void get2DValues(ArrayRCP<ArrayRCP<Scalar> > &inds);
00146 
00148 
00154     void finalize(bool OptimizeStorage);
00155 
00157     virtual void clear();
00158 
00160 
00161   protected:
00163     CrsMatrixHostCompute(const CrsMatrixHostCompute& sources);
00164 
00165     CrsGraphHostCompute<Ordinal,Node,LocalMatOps> *myGraph_;
00166     const CrsGraphHostCompute<Ordinal,Node,LocalMatOps> *staticGraph_;
00167     bool isFinalized_;
00168 
00169     // 2D storage
00170     ArrayRCP<ArrayRCP<Scalar> >  values2D_;
00171     // 1D storage
00172     ArrayRCP<Scalar>             values1D_;
00173   };
00174 
00175 
00176   //==============================================================================
00177   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00178   CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::CrsMatrixHostCompute()
00179   {
00180     myGraph_ = NULL;
00181     staticGraph_ = NULL;
00182     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::clear();
00183   }
00184 
00185   //==============================================================================
00186   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00187   CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::CrsMatrixHostCompute(const CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &graph)
00188   {
00189     setStaticGraph(graph);
00190     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::clear();
00191   }
00192 
00193   //==============================================================================
00194   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00195   CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::CrsMatrixHostCompute(CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &graph)
00196   {
00197     setOwnedGraph(graph);
00198     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::clear();
00199   }
00200 
00201   //==============================================================================
00202   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00203   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setStaticGraph(const CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &hgraph)
00204   {
00205     myGraph_ = NULL;
00206     staticGraph_ = &hgraph;
00207     TEST_FOR_EXCEPTION( staticGraph_->isFinalized() == false, std::runtime_error, 
00208         Teuchos::typeName(*this) << ": construction with static graph requires that the graph is already finalized.");
00209   }
00210 
00211   //==============================================================================
00212   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00213   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setOwnedGraph(CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &hgraph)
00214   {
00215     myGraph_ = &hgraph;
00216     staticGraph_ = &hgraph;
00217     TEST_FOR_EXCEPTION( myGraph_->isFinalized() == true, std::runtime_error,
00218         Teuchos::typeName(*this) << ": construction with matrix-owned graph requires that the graph is not yet finalized.");
00219   }
00220 
00221   //==============================================================================
00222   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00223   CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::~CrsMatrixHostCompute() {
00224   }
00225 
00226   // ======= clear ===========
00227   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00228   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::clear() {
00229     isFinalized_   = false;
00230     values1D_      = null;
00231     values2D_      = null;
00232   }
00233 
00234   // ======= node ===========
00235   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00236   RCP<Node> CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::getNode() const {
00237     return staticGraph_->getNode();
00238   }
00239 
00240   // ======= numrows ===========
00241   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00242   size_t CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::getNumRows() const {
00243     return staticGraph_->getNumRows();
00244   }
00245 
00246   // ======= numentries ===========
00247   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00248   size_t CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::getNumEntries() const {
00249     return staticGraph_->getNumEntries();
00250   }
00251 
00252   // ======= isempty ===========
00253   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00254   bool CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::isEmpty() const {
00255     return staticGraph_->isEmpty();
00256   }
00257 
00258   // ======= isfinalized ===========
00259   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00260   bool CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::isFinalized() const {
00261     return isFinalized_;
00262   }
00263 
00264   // ======= is1d ===========
00265   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00266   bool CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::is1DStructure() const {
00267     return staticGraph_->is1DStructure();
00268   }
00269 
00270   // ======= is2d ===========
00271   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00272   bool CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::is2DStructure() const {
00273     return staticGraph_->is2DStructure();
00274   }
00275 
00276   // ======= isopt ===========
00277   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00278   bool CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::isOptimized() const {
00279     return staticGraph_->isOptimized();
00280   }
00281 
00282   // ======= get 1d ===========
00283   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00284   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::get1DValues(ArrayRCP<Scalar> &vals)
00285   {
00286     vals = values1D_;
00287   }
00288 
00289   // ======= get 2d ===========
00290   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00291   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::get2DValues(ArrayRCP<ArrayRCP<Scalar> > &vals)
00292   {
00293     vals = values2D_;
00294   }
00295 
00296   // ======= set 1d ===========
00297   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00298   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::set1DValues(ArrayRCP<Scalar> vals)
00299   {
00300     this->clear();
00301     TEST_FOR_EXCEPTION( is1DStructure() == false, std::runtime_error, 
00302         Teuchos::typeName(*this) << "::set1DValues(vals): graph must have 1D structure and must be set before matrix.");
00303     values1D_ = vals;
00304     TEST_FOR_EXCEPTION( (size_t)values1D_.size() < getNumEntries(), std::runtime_error,
00305         Teuchos::typeName(*this) << "::set1DValues(vals): inds must have as many entries as the number of entries in the associated graph, and the graph entries must be set first.");
00306   }
00307 
00308   // ======= set 2d ===========
00309   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00310   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::set2DValues(ArrayRCP<ArrayRCP<Scalar> > vals)
00311   {
00312     TEST_FOR_EXCEPTION( is2DStructure() == false, std::runtime_error, 
00313         Teuchos::typeName(*this) << "::set2DValues(vals): graph must have 2D structure and must be set before matrix.");
00314     TEST_FOR_EXCEPTION( (size_t)vals.size() != getNumRows(), std::runtime_error,
00315         Teuchos::typeName(*this) << "::set2DValues(inds): vals must have as many entries as the number of rows in the associated graph.");
00316     this->clear();
00317     values2D_  = vals;
00318   }
00319 
00320 
00321   // ======= finalize ===========
00322   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00323   void CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::finalize(bool OptimizeStorage)
00324   {
00325     if (isFinalized_ && !(OptimizeStorage == true && staticGraph_->isOptimized() == false)) return;
00326     // we only have something to do if we have a matrix-owned graph
00327     if (myGraph_ != NULL) {
00328       myGraph_->template finalize<Scalar>(OptimizeStorage, values2D_, values1D_);
00329     }
00330     else {
00331       TEST_FOR_EXCEPTION(OptimizeStorage == true && staticGraph_->isOptimized() == false, std::runtime_error, 
00332           Teuchos::typeName(*this) << "::finalize(OptimizeStorage == true): underlying static graph is not optimized and cannot be optimized.");
00333     }
00334     isFinalized_ = true;
00335   }
00336 
00337 
00338   //=========================================================================================================================
00339   // 
00340   // A device-resident CrsMatrix
00341   // 
00342   //=========================================================================================================================
00343 
00344 
00352   template <class Scalar,
00353             class Ordinal, 
00354             class Node,
00355             class LocalMatOps>
00356   class CrsMatrixDeviceCompute : public CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps> {
00357   public:
00358 
00360 
00361 
00363     CrsMatrixDeviceCompute();
00364 
00366     CrsMatrixDeviceCompute(CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &graph);
00367 
00369     CrsMatrixDeviceCompute(const CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &graph);
00370 
00372     ~CrsMatrixDeviceCompute();
00373 
00375 
00377 
00378 
00380     void setOwnedGraph(CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &graph);
00381 
00383     void setStaticGraph(const CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &graph);
00384     
00386 
00390     void finalize(bool OptimizeStorage);
00391 
00393     void getDeviceBuffer(ArrayRCP<Scalar> &d_vals) const;
00394 
00396     virtual void clear();
00397 
00399 
00400   protected:
00402     CrsMatrixDeviceCompute(const CrsMatrixDeviceCompute& sources);
00403 
00404     // pointers to device-based graph
00405     CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> *myDeviceGraph_;
00406     const CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> *staticDeviceGraph_;
00407     // device storage (always 1D packed)
00408     ArrayRCP<Scalar> pbuf_values_;
00409   };
00410 
00411   //==============================================================================
00412   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00413   CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::CrsMatrixDeviceCompute()
00414   : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>()
00415   , myDeviceGraph_(NULL)
00416   , staticDeviceGraph_(NULL)
00417   {}
00418 
00419   //==============================================================================
00420   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00421   CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::CrsMatrixDeviceCompute(const CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &graph)
00422   : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>(graph)
00423   , myDeviceGraph_(NULL)
00424   , staticDeviceGraph_(&graph)
00425   {}
00426 
00427   //==============================================================================
00428   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00429   CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::CrsMatrixDeviceCompute(CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &graph)
00430   : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>(graph)
00431   , myDeviceGraph_(&graph)
00432   , staticDeviceGraph_(&graph)
00433   {}
00434 
00435   //===== destructor =====
00436   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00437   CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::~CrsMatrixDeviceCompute() 
00438   {}
00439 
00440   //===== clear =====
00441   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00442   void CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::clear() { 
00443     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::clear();
00444     pbuf_values_ = null;
00445   }
00446 
00447   // ======= get device ===========
00448   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00449   void CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::getDeviceBuffer(ArrayRCP<Scalar> &vals) const
00450   {
00451     vals = pbuf_values_;
00452   }
00453 
00454   //==============================================================================
00455   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00456   void CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::setStaticGraph(const CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &dgraph)
00457   {
00458     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setStaticGraph(dgraph);
00459     myDeviceGraph_     = NULL;
00460     staticDeviceGraph_ = &dgraph;
00461   }
00462 
00463   //==============================================================================
00464   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00465   void CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::setOwnedGraph(CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> &dgraph)
00466   {
00467     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setOwnedGraph(dgraph);
00468     myDeviceGraph_     = &dgraph;
00469     staticDeviceGraph_ = &dgraph;
00470   }
00471 
00472   //==============================================================================
00473   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00474   void CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps>::finalize(bool OptimizeStorage)
00475   {
00476     if (this->isFinalized() && !(OptimizeStorage == true && staticDeviceGraph_->isOptimized() == false)) return;
00477     if (myDeviceGraph_ != NULL) {
00478       // this will finalize both classes and copy data to the device
00479       myDeviceGraph_->template finalize<Scalar>(OptimizeStorage, this->values2D_, this->values1D_, pbuf_values_);
00480     }
00481     else {
00482       TEST_FOR_EXCEPTION(OptimizeStorage == true && this->staticGraph_->isOptimized() == false, std::runtime_error, 
00483           Teuchos::typeName(*this) << "::finalize(OptimizeStorage == true): underlying static graph is not optimized and cannot be optimized.");
00484       // static graph. no changes, but need to copy values to device.
00485       if (this->isEmpty()) {
00486         pbuf_values_  = null;
00487       }
00488       else {
00489         // allocate space on the device and copy data there, in a packed format
00490         pbuf_values_ = this->getNode()->template allocBuffer<Scalar>(this->getNumEntries());
00491         ArrayRCP<size_t> rowBegs, rowEnds, numEntriesPerRow;
00492         if (this->is1DStructure()) {
00493           ArrayRCP<Ordinal> inds;
00494           const_cast<CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> *>(staticDeviceGraph_)->get1DStructure(inds, rowBegs, rowEnds);
00495         }
00496         else {
00497           ArrayRCP<ArrayRCP<Ordinal> > inds;
00498           const_cast<CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps> *>(staticDeviceGraph_)->get2DStructure(inds, numEntriesPerRow);
00499         }
00500         if (this->isOptimized()) {
00501           // should be packed now; single copy should do, and offsets are rowBegs_
00502           this->getNode()->template copyToBuffer<Scalar >(this->getNumEntries(), this->values1D_(0,this->getNumEntries()), pbuf_values_);
00503         }
00504         else {
00505           // need to get graph structure from graph
00506           ArrayRCP<Scalar> view_values = this->getNode()->template viewBufferNonConst<Scalar >(WriteOnly, pbuf_values_.size(), pbuf_values_);
00507           typename ArrayRCP<Scalar >::iterator oldvals, newvals;
00508           newvals = view_values.begin();
00509           size_t curnuminds;
00510           for (size_t i=0; i < this->getNumRows(); ++i) {
00511             if (this->is1DStructure()) {
00512               curnuminds = rowEnds[i] - rowBegs[i];
00513               oldvals = this->values1D_.begin() + rowBegs[i];
00514             }
00515             else {
00516               curnuminds = numEntriesPerRow[i];
00517               oldvals = this->values2D_[i].begin();
00518             }
00519             std::copy(oldvals, oldvals+curnuminds, newvals);
00520             newvals += curnuminds;
00521           }
00522           view_values  = null;
00523         }
00524       }
00525     }
00526     this->isFinalized_ = true;
00527   }
00528 
00529 
00530   //=========================================================================================================================
00531   // 
00532   // A first-touch allocation host-resident CrsMatrix
00533   // 
00534   //=========================================================================================================================
00535 
00539   template <class Scalar, 
00540             class Ordinal, 
00541             class Node,
00542             class LocalMatOps>
00543   class FirstTouchHostCrsMatrix : public CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps> {
00544   public:
00545 
00546     typedef typename CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::ScalarType        ScalarType;
00547     typedef typename CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::OrdinalType       OrdinalType;
00548     typedef typename CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::NodeType          NodeType;
00549     typedef typename CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::LocalMatOpsType   LocalMatOpsType;
00550 
00552 
00553 
00555     FirstTouchHostCrsMatrix();
00556 
00558     FirstTouchHostCrsMatrix(FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &graph);
00559 
00561     FirstTouchHostCrsMatrix(const FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &graph);
00562 
00564     virtual ~FirstTouchHostCrsMatrix();
00565 
00567 
00569 
00570 
00572     void setOwnedGraph(FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &graph);
00573 
00575     void setStaticGraph(const FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &graph);
00576     
00578 
00580 
00581 
00583 
00589     void finalize(bool OptimizeStorage);
00590 
00592 
00593   protected:
00595     FirstTouchHostCrsMatrix(const FirstTouchHostCrsMatrix& sources);
00596     // pointers to first-touch graphs
00597     FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps>           *myFTGraph_;
00598     const FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> *staticFTGraph_;
00599   };
00600 
00601 
00602   //==============================================================================
00603   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00604   FirstTouchHostCrsMatrix<Scalar,Ordinal,Node,LocalMatOps>::FirstTouchHostCrsMatrix()
00605   : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>() 
00606   , myFTGraph_(NULL)
00607   , staticFTGraph_(NULL)
00608   {}
00609 
00610   //==============================================================================
00611   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00612   FirstTouchHostCrsMatrix<Scalar,Ordinal,Node,LocalMatOps>::FirstTouchHostCrsMatrix(const FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &graph)
00613   : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>(graph) 
00614   , myFTGraph_(NULL)
00615   , staticFTGraph_(&graph)
00616   {}
00617 
00618   //==============================================================================
00619   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00620   FirstTouchHostCrsMatrix<Scalar,Ordinal,Node,LocalMatOps>::FirstTouchHostCrsMatrix(FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &graph)
00621   : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>(graph) 
00622   , myFTGraph_(&graph)
00623   , staticFTGraph_(&graph)
00624   {}
00625 
00626   //==============================================================================
00627   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00628   FirstTouchHostCrsMatrix<Scalar,Ordinal,Node,LocalMatOps>::~FirstTouchHostCrsMatrix() {
00629   }
00630 
00631   //==============================================================================
00632   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00633   void FirstTouchHostCrsMatrix<Scalar,Ordinal,Node,LocalMatOps>::setStaticGraph(const FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &dgraph)
00634   {
00635     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setStaticGraph(dgraph);
00636     myFTGraph_     = NULL;
00637     staticFTGraph_ = &dgraph;
00638   }
00639 
00640   //==============================================================================
00641   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00642   void FirstTouchHostCrsMatrix<Scalar,Ordinal,Node,LocalMatOps>::setOwnedGraph(FirstTouchHostCrsGraph<Ordinal,Node,LocalMatOps> &dgraph)
00643   {
00644     CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setOwnedGraph(dgraph);
00645     myFTGraph_     = &dgraph;
00646     staticFTGraph_ = &dgraph;
00647   }
00648 
00649   // ======= finalize ===========
00650   template <class Scalar, class Ordinal, class Node, class LocalMatOps>
00651   void FirstTouchHostCrsMatrix<Scalar,Ordinal,Node,LocalMatOps>::finalize(bool OptimizeStorage)
00652   {
00653     if (this->isFinalized_ == false || 
00654        (OptimizeStorage == true && staticFTGraph_->isOptimized() == false))
00655     {
00656       if (this->myFTGraph_ != NULL) {
00657         // call graph finalize to finalize/first-touch graph and matrix at the same time
00658         myFTGraph_->template finalize<Scalar>(OptimizeStorage, this->values2D_, this->values1D_);
00659       }
00660       else {
00661         TEST_FOR_EXCEPTION(OptimizeStorage == true && staticFTGraph_->isOptimized() == false, std::runtime_error, 
00662             Teuchos::typeName(*this) << "::finalize(OptimizeStorage == true): underlying static graph is not already optimized and cannot be optimized.");
00663       }
00664       this->isFinalized_ = true;
00665     }
00666 #ifdef HAVE_KOKKOS_DEBUG
00667     TEST_FOR_EXCEPTION(this->isFinalized_ == false || staticFTGraph_->isFinalized() == false, std::logic_error,
00668         Teuchos::typeName(*this) << "::finalize(): logic error. Post-condition violated. Please contact Tpetra team.");
00669     TEST_FOR_EXCEPTION(OptimizeStorage == true && (staticFTGraph_->isOptimized() == false && staticFTGraph_->isEmpty() == false), std::logic_error,
00670         Teuchos::typeName(*this) << "::finalize(): logic error. Post-condition violated. Please contact Tpetra team.");
00671 #endif
00672   }
00673 
00674 
00675   //=========================================================================================================================
00676   // 
00677   // Specializations
00678   // 
00679   //=========================================================================================================================
00680   // C++0x: it would be nice if these were template aliases
00681 
00687   template <class Scalar,
00688             class Ordinal, 
00689             class Node,
00690             class LocalMatOps>
00691   class CrsMatrix : public CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps> {
00692   public:
00693     CrsMatrix() : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>() {}
00694     CrsMatrix(CrsGraph<Ordinal,Node,LocalMatOps> &graph) : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>(graph) {}
00695     CrsMatrix(const CrsGraph<Ordinal,Node,LocalMatOps> &graph) : CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>(graph) {}
00696     void setStaticGraph(const CrsGraph<Ordinal,Node,LocalMatOps> &graph) {
00697       const CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &hgraph = dynamic_cast<const CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &>(graph);
00698       CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setStaticGraph(hgraph);
00699     }
00700     void setOwnedGraph(CrsGraph<Ordinal,Node,LocalMatOps> &graph) {
00701       CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &hgraph = dynamic_cast<CrsGraphHostCompute<Ordinal,Node,LocalMatOps> &>(graph);
00702       CrsMatrixHostCompute<Scalar,Ordinal,Node,LocalMatOps>::setOwnedGraph(hgraph);
00703     }
00704   private:
00705     CrsMatrix(const CrsMatrix<Scalar,Ordinal,Node,LocalMatOps> &mat); // not implemented
00706   };
00707 
00708 #ifndef HAVE_KOKKOS_NO_FIRST_TOUCH_MATVEC_ALLOCATION
00709 #ifdef HAVE_KOKKOS_TBB
00710 
00715   class TBBNode;
00716   template <class Scalar,
00717             class Ordinal, 
00718             class LocalMatOps>
00719   class CrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps> : public FirstTouchHostCrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps> {
00720   public:
00721     CrsMatrix() : FirstTouchHostCrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps>() {}
00722     CrsMatrix(CrsGraph<Ordinal,TBBNode,LocalMatOps> &graph) : FirstTouchHostCrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps>(graph) {}
00723     CrsMatrix(const CrsGraph<Ordinal,TBBNode,LocalMatOps> &graph) : FirstTouchHostCrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps>(graph) {}
00724     void setStaticGraph(const CrsGraph<Ordinal,TBBNode,LocalMatOps> &graph) {
00725       const FirstTouchHostCrsGraph<Ordinal,TBBNode,LocalMatOps> &hgraph = dynamic_cast<const FirstTouchHostCrsGraph<Ordinal,TBBNode,LocalMatOps> &>(graph);
00726       FirstTouchHostCrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps>::setStaticGraph(hgraph);
00727     }
00728     void setOwnedGraph(CrsGraph<Ordinal,TBBNode,LocalMatOps> &graph) {
00729       FirstTouchHostCrsGraph<Ordinal,TBBNode,LocalMatOps> &hgraph = dynamic_cast<FirstTouchHostCrsGraph<Ordinal,TBBNode,LocalMatOps> &>(graph);
00730       FirstTouchHostCrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps>::setOwnedGraph(hgraph);
00731     }
00732   private:
00733     CrsMatrix(const CrsMatrix<Scalar,Ordinal,TBBNode,LocalMatOps> &mat); // not implemented
00734   };
00735 #endif
00736 #ifdef HAVE_KOKKOS_THREADPOOL
00737   class TPINode;
00743   template <class Scalar,
00744             class Ordinal, 
00745             class LocalMatOps>
00746   class CrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps> : public FirstTouchHostCrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps> {
00747   public:
00748     CrsMatrix() : FirstTouchHostCrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps>() {}
00749     CrsMatrix(CrsGraph<Ordinal,TPINode,LocalMatOps> &graph) : FirstTouchHostCrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps>(graph) {}
00750     CrsMatrix(const CrsGraph<Ordinal,TPINode,LocalMatOps> &graph) : FirstTouchHostCrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps>(graph) {}
00751     void setStaticGraph(const CrsGraph<Ordinal,TPINode,LocalMatOps> &graph) {
00752       const FirstTouchHostCrsGraph<Ordinal,TPINode,LocalMatOps> &hgraph = dynamic_cast<const FirstTouchHostCrsGraph<Ordinal,TPINode,LocalMatOps> &>(graph);
00753       FirstTouchHostCrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps>::setStaticGraph(hgraph);
00754     }
00755     void setOwnedGraph(CrsGraph<Ordinal,TPINode,LocalMatOps> &graph) {
00756       FirstTouchHostCrsGraph<Ordinal,TPINode,LocalMatOps> &hgraph = dynamic_cast<FirstTouchHostCrsGraph<Ordinal,TPINode,LocalMatOps> &>(graph);
00757       FirstTouchHostCrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps>::setOwnedGraph(hgraph);
00758     }
00759   private:
00760     CrsMatrix(const CrsMatrix<Scalar,Ordinal,TPINode,LocalMatOps> &mat); // not implemented
00761   };
00762 #endif
00763 #endif
00764 
00770   template <class S, class O, class N> class DefaultDeviceSparseOps;
00771   template <class Scalar,
00772             class Ordinal,
00773             class Node>
00774   class CrsMatrix<Scalar,Ordinal,Node,DefaultDeviceSparseOps<void,Ordinal,Node> > : public CrsMatrixDeviceCompute<Scalar,Ordinal,Node,DefaultDeviceSparseOps<void,Ordinal,Node> > {
00775   public:
00776     typedef DefaultDeviceSparseOps<void,Ordinal,Node> LocalMatOps;
00777     CrsMatrix() : CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps >() {}
00778     CrsMatrix(CrsGraph<Ordinal,Node,LocalMatOps > &graph) : CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps >(graph) {}
00779     CrsMatrix(const CrsGraph<Ordinal,Node,LocalMatOps > &graph) : CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps >(graph) {}
00780     void setStaticGraph(const CrsGraph<Ordinal,Node,LocalMatOps > &graph) {
00781       const CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps > &dgraph = dynamic_cast<const CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps > &>(graph);
00782       CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps >::setStaticGraph(dgraph);
00783     }
00784     void setOwnedGraph(CrsGraph<Ordinal,Node,LocalMatOps > &graph) {
00785       CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps > &dgraph = dynamic_cast<CrsGraphDeviceCompute<Ordinal,Node,LocalMatOps > &>(graph);
00786       CrsMatrixDeviceCompute<Scalar,Ordinal,Node,LocalMatOps >::setOwnedGraph(dgraph);
00787     }
00788   private:
00789     CrsMatrix(const CrsMatrix<Scalar,Ordinal,Node,LocalMatOps > &mat); // not implemented
00790   };
00791 
00792 } // namespace Kokkos
00793 
00794 #endif /* KOKKOS_CRSMATRIX_HPP */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends