Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Kokkos_NodeHelpers.hpp
00001 //@HEADER
00002 // ************************************************************************
00003 //
00004 //          Kokkos: Node API and Parallel Node Kernels
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 KOKKOS_NODE_HELPERS_HPP_
00043 #define KOKKOS_NODE_HELPERS_HPP_
00044 
00045 #include <Kokkos_NodeAPIConfigDefs.hpp>
00046 #include "Teuchos_Array.hpp"
00047 #include "Teuchos_ArrayRCP.hpp"
00048 
00049 namespace Kokkos {
00050 
00124   template <class Node>
00125   class ReadyBufferHelper {
00126   public:
00128     ReadyBufferHelper(RCP<Node> node);
00129 
00131     virtual ~ReadyBufferHelper();
00132 
00134     void begin();
00135 
00137     template <class T>
00138     const T* addConstBuffer(ArrayRCP<const T> buff);
00139 
00141     template <class T>
00142     T* addNonConstBuffer(ArrayRCP<T> buff);
00143 
00145     void end();
00146 
00147   protected:
00149     RCP<Node> node_;
00151     Array<ArrayRCP<const char> >  cbufs_;
00153     Array<ArrayRCP<      char> > ncbufs_;
00154   };
00155 
00156   template <class Node>
00157   ReadyBufferHelper<Node>::ReadyBufferHelper (RCP<Node> node)
00158     : node_(node)
00159   {}
00160 
00161   template <class Node>
00162   ReadyBufferHelper<Node>::~ReadyBufferHelper()
00163   {}
00164 
00165   template <class Node>
00166   void ReadyBufferHelper<Node>::begin() {
00167     cbufs_.clear ();
00168     ncbufs_.clear ();
00169   }
00170 
00171   template <class Node>
00172   template <class T>
00173   const T* ReadyBufferHelper<Node>::addConstBuffer (ArrayRCP<const T> buff) {
00174     cbufs_.push_back (arcp_reinterpret_cast<const char> (buff));
00175     return buff.get ();
00176   }
00177 
00178   template <class Node>
00179   template <class T>
00180   T* ReadyBufferHelper<Node>::addNonConstBuffer (ArrayRCP<T> buff) {
00181     cbufs_.push_back (arcp_reinterpret_cast<char> (buff));
00182     return buff.get ();
00183   }
00184 
00185   template <class Node>
00186   void ReadyBufferHelper<Node>::end () {
00187     node_->readyBuffers (cbufs_ (), ncbufs_ ());
00188   }
00189 
00192   template <class Node>
00193   class ArrayOfViewsHelper {
00194   public:
00196     template <class T>
00197     static ArrayRCP<ArrayRCP<T> >
00198     getArrayOfNonConstViews (const RCP<Node> &node,
00199                              ReadWriteOption rw,
00200                              const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers);
00201   private:
00203     ArrayOfViewsHelper();
00204     ~ArrayOfViewsHelper();
00205   };
00206 
00207   template <class Node>
00208   template <class T>
00209   ArrayRCP<ArrayRCP<T> >
00210   ArrayOfViewsHelper<Node>::getArrayOfNonConstViews(const RCP<Node> &node, ReadWriteOption rw, const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers) {
00211     ArrayRCP< ArrayRCP<T> > arrayofviews;
00212     const size_t numBufs = arrayOfBuffers.size();
00213     if (numBufs > 0) {
00214       arrayofviews = arcp< ArrayRCP<T> >(numBufs);
00215       for (size_t i=0; i < numBufs; ++i) {
00216         if (arrayOfBuffers[i].size() > 0) {
00217           arrayofviews[i] = node->template viewBufferNonConst<T>(rw,arrayOfBuffers[i].size(),arrayOfBuffers[i]);
00218         }
00219       }
00220     }
00221     return arrayofviews;
00222   }
00223 
00225   template <class Node>
00226   class ArrayOfViewsHelperTrivialImpl {
00227   public:
00228     template <class T>
00229     static ArrayRCP<ArrayRCP<T> > getArrayOfNonConstViews(const RCP<Node> &node, ReadWriteOption rw, const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers);
00230   private:
00232     ArrayOfViewsHelperTrivialImpl();
00233     ~ArrayOfViewsHelperTrivialImpl();
00234   };
00235 
00236   template <class Node>
00237   template <class T>
00238   ArrayRCP<ArrayRCP<T> >
00239   ArrayOfViewsHelperTrivialImpl<Node>::getArrayOfNonConstViews(const RCP<Node> &node, ReadWriteOption rw, const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers) {
00240     (void)node;
00241     (void)rw;
00242     return arrayOfBuffers;
00243   }
00244 
00245 } // end of namespace Kokkos
00246 
00247 
00248 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends