Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Kokkos_ScratchSpace.hpp
00001 /*
00002 //@HEADER
00003 // ************************************************************************
00004 //
00005 //   Kokkos: Manycore Performance-Portable Multidimensional Arrays
00006 //              Copyright (2012) Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions?  Contact  H. Carter Edwards (hcedwar@sandia.gov)
00039 //
00040 // ************************************************************************
00041 //@HEADER
00042 */
00043 
00044 #ifndef KOKKOS_SCRATCHSPACE_HPP
00045 #define KOKKOS_SCRATCHSPACE_HPP
00046 
00047 #include <stdio.h>
00048 #include <Kokkos_Core_fwd.hpp>
00049 #include <impl/Kokkos_Tags.hpp>
00050 
00051 /*--------------------------------------------------------------------------*/
00052 
00053 namespace Kokkos {
00054 
00058 template< class ExecSpace >
00059 class ScratchMemorySpace {
00060 public:
00061 
00062   // Alignment of memory chunks returned by 'get'
00063   // must be a power of two
00064   enum { ALIGN = 8 };
00065 
00066 private:
00067 
00068   mutable char * m_iter ;
00069   char *         m_end ;
00070 
00071   ScratchMemorySpace();
00072   ScratchMemorySpace & operator = ( const ScratchMemorySpace & );
00073 
00074   enum { MASK = ALIGN - 1 }; // Alignment used by View::shmem_size
00075 
00076 public:
00077 
00078   typedef Impl::MemorySpaceTag              kokkos_tag ;
00079   typedef ScratchMemorySpace                memory_space ;
00080   typedef ExecSpace                         execution_space ;
00081   typedef typename ExecSpace::array_layout  array_layout ;
00082 
00083   template< typename IntType >
00084   KOKKOS_INLINE_FUNCTION static
00085   IntType align( const IntType & size )
00086     { return ( size + MASK ) & ~MASK ; }
00087 
00088   template< typename IntType >
00089   KOKKOS_INLINE_FUNCTION
00090   void * get_shmem( const IntType & size ) const
00091     {
00092       void * tmp = m_iter ;
00093       if ( m_end < ( m_iter += align( size ) ) ) {
00094         printf("ScratchMemorySpace<...>::get_shmem overflow %ld\n",long(m_end-m_iter));
00095         tmp = 0 ;
00096       }
00097       return tmp ;
00098     }
00099 
00100   template< typename IntType >
00101   KOKKOS_INLINE_FUNCTION
00102   ScratchMemorySpace( void * ptr , const IntType & size )
00103     : m_iter( (char *) ptr )
00104     , m_end(  m_iter + size )
00105     {}
00106 };
00107 
00108 } // namespace Kokkos
00109 
00110 #endif /* #ifndef KOKKOS_SCRATCHSPACE_HPP */
00111 
00112 //----------------------------------------------------------------------------
00113 //----------------------------------------------------------------------------
00114 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends