Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Kokkos_FirstTouchSparseOps.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_FIRSTTOUCHSPARSEOPS_HPP
00030 #define KOKKOS_FIRSTTOUCHSPARSEOPS_HPP
00031 
00032 #include "Kokkos_DefaultSparseOps.hpp"
00033 
00034 namespace Kokkos {
00035 
00036   //=========================================================================================================================
00037   // 
00038   // A first-touch sparse ops
00039   // 
00040   //=========================================================================================================================
00041 
00049   template <class Scalar, class Ordinal, class Node>
00050   class FirstTouchSparseOps : private DefaultHostSparseOps<Scalar,Ordinal,Node> {
00051   public:
00053 
00054 
00056     typedef Scalar  scalar_type;
00058     typedef Ordinal ordinal_type;
00060     typedef Node    node_type;
00062     typedef FirstTouchSparseOps<Scalar,Ordinal,Node> sparse_ops_type;
00063     
00064     using DefaultHostSparseOps<Scalar,Ordinal,Node>::graph;
00065     using DefaultHostSparseOps<Scalar,Ordinal,Node>::matrix;
00066 
00071     template <class S2>
00072     struct bind_scalar {
00073       typedef FirstTouchSparseOps<S2,Ordinal,Node> other_type;
00074     };
00075 
00077 
00078 
00079 
00081     FirstTouchSparseOps(const RCP<Node> &node);
00082 
00084     virtual ~FirstTouchSparseOps();
00085 
00087 
00088 
00089 
00091     static ArrayRCP<size_t> allocRowPtrs(const RCP<Node> &node, const ArrayView<const size_t> &numEntriesPerRow);
00092 
00094     template <class T>
00095     static ArrayRCP<T> allocStorage(const RCP<Node> &node, const ArrayView<const size_t> &rowPtrs);
00096 
00097     using DefaultHostSparseOps<Scalar,Ordinal,Node>::finalizeGraph;
00098     using DefaultHostSparseOps<Scalar,Ordinal,Node>::finalizeMatrix;
00099     using DefaultHostSparseOps<Scalar,Ordinal,Node>::finalizeGraphAndMatrix;
00100     using DefaultHostSparseOps<Scalar,Ordinal,Node>::setGraphAndMatrix;
00101     using DefaultHostSparseOps<Scalar,Ordinal,Node>::multiply;
00102     using DefaultHostSparseOps<Scalar,Ordinal,Node>::solve;
00103     using DefaultHostSparseOps<Scalar,Ordinal,Node>::getNode;
00104 
00106 
00107   protected:
00109     FirstTouchSparseOps(const DefaultHostSparseOps<Scalar,Ordinal,Node>& source);
00110   };
00111 
00112   namespace FirstTouchSparseOpsDetails {
00113 
00114     struct rowPtrsInitKernel {
00115       size_t       *rowPtrs;
00116       inline KERNEL_PREFIX void execute(int i) const {
00117         rowPtrs[i] = 0;
00118       }
00119     };
00120 
00121     template <class T>
00122     struct valsInitKernel {
00123       const size_t *rowPtrs;
00124       T * entries;
00125       inline KERNEL_PREFIX void execute(int i) const {
00126         size_t *beg = entries+rowPtrs[i],
00127                *end = entries+rowPtrs[i+1];
00128         while (beg != end) {
00129           *beg = Teuchos::ScalarTraits<T>::zero();
00130         }
00131       }
00132     };
00133   }
00134 
00135   // ======= pointer allocation ===========
00136   template <class Scalar, class Ordinal, class Node>
00137   ArrayRCP<size_t>
00138   FirstTouchSparseOps<Scalar,Ordinal,Node>::allocRowPtrs(const RCP<Node> &node, const ArrayView<const size_t> &numEntriesPerRow)
00139   {
00140     const size_t numrows = numEntriesPerRow.size();
00141     FirstTouchSparseOpsDetails::rowPtrsInitKernel kern;
00142     // allocate
00143     kern.rowPtrs = new size_t[numrows+1];
00144     // parallel first touch
00145     node->parallel_for(0,numrows+1,kern);
00146     // encapsulate
00147     ArrayRCP<size_t> ptrs = arcp<size_t>(kern.rowPtrs,0,numrows+1,true);
00148     // compute in serial. parallelize later, perhaps; it's only O(N)
00149     ptrs[0] = 0;
00150     std::partial_sum( numEntriesPerRow.getRawPtr(), numEntriesPerRow.getRawPtr()+numEntriesPerRow.size(), ptrs.begin()+1 );
00151     return ptrs;
00152   }
00153 
00154   // ======= other allocation ===========
00155   template <class Scalar, class Ordinal, class Node>
00156   template <class T>
00157   ArrayRCP<T>
00158   FirstTouchSparseOps<Scalar,Ordinal,Node>::allocStorage(const RCP<Node> &node, const ArrayView<const size_t> &rowPtrs)
00159   {
00160     const size_t totalNumEntries = *(rowPtrs.end()-1);
00161     const size_t numRows = rowPtrs.size() - 1;
00162     FirstTouchSparseOpsDetails::valsInitKernel<T> kern;
00163     ArrayRCP<T> vals;
00164     if (totalNumEntries > 0) {
00165       // allocate
00166       kern.entries = new T[totalNumEntries];
00167       kern.rowPtrs = rowPtrs.getRawPtr();
00168       // first touch
00169       node->parallel_for(0,numRows,kern);
00170       // encapsulate
00171       vals = arcp<T>(kern.entries,0,totalNumEntries,true);
00172     }
00173     return vals;
00174   }
00175 
00176   template <class Scalar, class Ordinal, class Node>
00177   FirstTouchSparseOps<Scalar,Ordinal,Node>::FirstTouchSparseOps(const RCP<Node> &node) 
00178   : DefaultHostSparseOps<Scalar,Ordinal,Node>(node) 
00179   {}
00180 
00181   template <class Scalar, class Ordinal, class Node>
00182   FirstTouchSparseOps<Scalar,Ordinal,Node>::~FirstTouchSparseOps()
00183   {}
00184 
00185 } // end namespace Kokkos
00186 
00187 #endif // KOKKOS_FIRSTTOUCH_SPARSEOPS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends