Kokkos Core Kernels Package  Version of the Day
Kokkos_ScratchSpace.hpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact H. Carter Edwards (hcedwar@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_SCRATCHSPACE_HPP
45 #define KOKKOS_SCRATCHSPACE_HPP
46 
47 #include <stdio.h>
48 #include <Kokkos_Core_fwd.hpp>
49 #include <impl/Kokkos_Tags.hpp>
50 
51 /*--------------------------------------------------------------------------*/
52 
53 namespace Kokkos {
54 
58 template< class ExecSpace >
60 public:
61 
62  // Alignment of memory chunks returned by 'get'
63  // must be a power of two
64  enum { ALIGN = 8 };
65 
66 private:
67 
68  mutable char * m_iter ;
69  char * m_end ;
70 
72  ScratchMemorySpace & operator = ( const ScratchMemorySpace & );
73 
74  enum { MASK = ALIGN - 1 }; // Alignment used by View::shmem_size
75 
76 public:
77 
80  typedef ExecSpace execution_space ;
83 
84  typedef typename ExecSpace::array_layout array_layout ;
85  typedef typename ExecSpace::size_type size_type ;
86 
87  template< typename IntType >
88  KOKKOS_INLINE_FUNCTION static
89  IntType align( const IntType & size )
90  { return ( size + MASK ) & ~MASK ; }
91 
92  template< typename IntType >
93  KOKKOS_INLINE_FUNCTION
94  void* get_shmem (const IntType& size) const {
95  void* tmp = m_iter ;
96  if (m_end < (m_iter += align (size))) {
97  m_iter -= align (size); // put it back like it was
98  #ifdef KOKKOS_HAVE_DEBUG
99  // mfh 23 Jun 2015: printf call consumes 25 registers
100  // in a CUDA build, so only print in debug mode. The
101  // function still returns NULL if not enough memory.
102  printf ("ScratchMemorySpace<...>::get_shmem: Failed to allocate "
103  "%ld byte(s); remaining capacity is %ld byte(s)\n", long(size),
104  long(m_end-m_iter));
105  #endif // KOKKOS_HAVE_DEBUG
106  tmp = 0;
107  }
108  return tmp;
109  }
110 
111  template< typename IntType >
112  KOKKOS_INLINE_FUNCTION
113  ScratchMemorySpace( void * ptr , const IntType & size )
114  : m_iter( (char *) ptr )
115  , m_end( m_iter + size )
116  {}
117 };
118 
119 } // namespace Kokkos
120 
121 #endif /* #ifndef KOKKOS_SCRATCHSPACE_HPP */
122 
123 //----------------------------------------------------------------------------
124 //----------------------------------------------------------------------------
125 
Scratch memory space associated with an execution space.
Memory space for main process and CPU execution spaces.
ScratchMemorySpace memory_space
Tag this class as a memory space.
Kokkos::Device< execution_space, memory_space > device_type
This execution space preferred device_type.