Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_MemoryTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef STOKHOS_MEMORY_TRAITS_HPP
43 #define STOKHOS_MEMORY_TRAITS_HPP
44 
45 #include <cstdlib>
46 
47 #include "Kokkos_Core_fwd.hpp"
48 
49 // Currently always aligning
50 #define STOKHOS_ALIGN_MEMORY 1
51 
52 // Uncomment this if you know all accesses will be aligned. Tthis is true
53 // if all Stokhos variables are coming from Kokkos allocations, new/delete
54 // or stack variables. However it may not be true for C allocations (e.g.,
55 // through MPI).
56 // #define STOKHOS_ASSUME_ALIGNED
57 
58 // ivdep is necessary to get the intel compiler to vectorize through
59 // expresion template assignent operators
60 #if defined(__INTEL_COMPILER) && ! defined(__CUDA_ARCH__)
61 #define STOKHOS_HAVE_PRAGMA_IVDEP
62 #endif
63 
64 // unrolling appears to slow everything down
65 #if 0 && ( defined(__INTEL_COMPILER) || defined(__CUDA_ARCH__) )
66 #define STOKHOS_HAVE_PRAGMA_UNROLL
67 #endif
68 
69 // assume all memory accesses are aligned appropriately for aligned
70 // vector loads
71 #if defined(STOKHOS_ALIGN_MEMORY) && defined(STOKHOS_ASSUME_ALIGNED) && defined(__INTEL_COMPILER) && ! defined(__CUDA_ARCH__)
72 #define STOKHOS_HAVE_PRAGMA_VECTOR_ALIGNED
73 #endif
74 
75 namespace Stokhos {
76 
78 template <typename MemorySpace>
79 struct MemoryTraits {
80 
82  static const unsigned Alignment = 8;
83 
85  KOKKOS_INLINE_FUNCTION
86  static void* alloc(const size_t size) { return operator new(size); }
87 
89  KOKKOS_INLINE_FUNCTION
90  static void free(void *ptr) { operator delete(ptr); }
91 };
92 
94 template <>
95 struct MemoryTraits< Kokkos::HostSpace > {
96 
98 #if STOKHOS_ALIGN_MEMORY
99 #if defined(__MIC__)
100  static const unsigned Alignment = 64;
101 #elif defined(__AVX__)
102  static const unsigned Alignment = 32;
103 #elif defined(__SSE2__)
104  static const unsigned Alignment = 16;
105 #else
106  static const unsigned Alignment = 8;
107 #endif
108 #else
109  static const unsigned Alignment = 8;
110 #endif
111 
113 
121  KOKKOS_INLINE_FUNCTION
122  static void* alloc(const size_t size) {
123  void* ptr = 0;
124  if (size > 0) {
125 #if STOKHOS_ALIGN_MEMORY
126  const size_t mask = Alignment-1;
127  const size_t total_size = size + mask + sizeof(std::ptrdiff_t);
128  char *ptr_alloc = reinterpret_cast<char*>(std::malloc(total_size));
129  char *ptr_storage = ptr_alloc + sizeof(std::ptrdiff_t);
130  char *ptr_body = reinterpret_cast<char*>(
131  ( reinterpret_cast<size_t>(ptr_storage) + mask ) & ~mask );
132  char *ptr_header = ptr_body - sizeof(std::ptrdiff_t);
133  const std::ptrdiff_t offset = ptr_body - ptr_alloc;
134  *reinterpret_cast<std::ptrdiff_t*>(ptr_header) = offset;
135  ptr = reinterpret_cast<void*>(ptr_body);
136 #else
137  ptr = operator new(size);
138 #endif
139  }
140  return ptr;
141  }
142 
144  KOKKOS_INLINE_FUNCTION
145  static void free(void *ptr) {
146  if (ptr != 0) {
147 #if STOKHOS_ALIGN_MEMORY
148  void *ptr_header = reinterpret_cast<char*>(ptr) - sizeof(std::ptrdiff_t);
149  const std::ptrdiff_t offset = *reinterpret_cast<std::ptrdiff_t*>(ptr_header);
150  void *ptr_alloc = reinterpret_cast<char*>(ptr) - offset;
151  std::free(ptr_alloc);
152 #else
153  operator delete(ptr);
154 #endif
155  }
156  }
157 };
158 
160 template <typename T>
162 public:
163 
164  typedef T value_type;
165  typedef T* pointer;
166  typedef const T* const_pointer;
167  typedef T& reference;
168  typedef const T& const_reference;
169  typedef size_t size_type;
170  typedef std::ptrdiff_t difference_type;
171 
173 
174  template <class U> struct rebind { typedef aligned_allocator<U> other; };
175 
177 
178  template <class U> aligned_allocator(const aligned_allocator<U>&) {}
179 
180  size_type max_size() const {
181  return (size_type(~0) - size_type(Traits::Alignment)) / sizeof(T);
182  }
183 
184  pointer address(reference x) const { return &x; }
185 
186  const_pointer address(const_reference x) const { return &x; }
187 
188  pointer allocate(size_type n, const void* = 0) {
189  size_type count = n * sizeof(T);
190  void* ptr = Traits::alloc(count);
191  if (ptr == 0) throw std::bad_alloc();
192  return reinterpret_cast<pointer>(ptr);
193  }
194 
196 
197  void construct(pointer p, const_reference val) { new (p) T(val); }
198 
199  void destroy(pointer p) { ((T*)p)->~T(); }
200 };
201 
203 template <typename T>
204 class aligned_allocator< const T > {
205 public:
206 
207  typedef T value_type;
208  typedef const T* pointer;
209  typedef const T* const_pointer;
210  typedef const T& reference;
211  typedef const T& const_reference;
212  typedef size_t size_type;
213  typedef std::ptrdiff_t difference_type;
214 
216 
217  template <class U> struct rebind { typedef aligned_allocator<U> other; };
218 
220 
221  template <class U> aligned_allocator(const aligned_allocator<U>&) {}
222 
223  size_type max_size() const {
224  return (size_type(~0) - size_type(Traits::Alignment)) / sizeof(T);
225  }
226 
227  const_pointer address(const_reference x) const { return &x; }
228 
229  pointer allocate(size_type n, const void* = 0) {
230  size_type count = n * sizeof(T);
231  void* ptr = Traits::alloc(count);
232  if (ptr == 0) throw std::bad_alloc();
233  return reinterpret_cast<pointer>(ptr);
234  }
235 
237 
238  void construct(pointer p, const_reference val) { new (p) T(val); }
239 
240  void destroy(pointer p) { ((T*)p)->~T(); }
241 };
242 
243 template <typename T, typename U>
244 inline bool
246 { return true; }
247 
248 template <typename T, typename U>
249 inline bool
251 { return false; }
252 
253 } // namespace Stokhos
254 
255 #endif // STOKHOS_MEMORY_TRAITS_HPP
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc()
An aligned STL allocator.
const_pointer address(const_reference x) const
pointer address(reference x) const
Specialization of MemoryTraits for host memory spaces.
pointer allocate(size_type n, const void *=0)
const_pointer address(const_reference x) const
bool operator==(const aligned_allocator< T > &, const aligned_allocator< U > &)
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory of given size.
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc()
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
Traits class encapsulting memory alignment.
void deallocate(pointer p, size_type)
pointer allocate(size_type n, const void *=0)
static const unsigned Alignment
Bytes to which memory allocations are aligned.
expr val()
void construct(pointer p, const_reference val)
void construct(pointer p, const_reference val)
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory.
aligned_allocator(const aligned_allocator< U > &)
static const unsigned Alignment
Bytes to which memory allocations are aligned.
aligned_allocator(const aligned_allocator< U > &)
bool operator!=(const aligned_allocator< T > &, const aligned_allocator< U > &)