Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
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 namespace Stokhos {
53 
55 template <typename MemorySpace>
56 struct MemoryTraits {
57 
59  static const unsigned Alignment = 8;
60  KOKKOS_INLINE_FUNCTION
61 
63  static void* alloc(const size_t size) { return operator new(size); }
64 
66  KOKKOS_INLINE_FUNCTION
67  static void free(void *ptr) { operator delete(ptr); }
68 };
69 
71 template <>
72 struct MemoryTraits< Kokkos::HostSpace > {
73 
75 #if STOKHOS_ALIGN_MEMORY
76 #if defined(__MIC__)
77  static const unsigned Alignment = 64;
78 #elif defined(__AVX__)
79  static const unsigned Alignment = 32;
80 #elif defined(__SSE2__)
81  static const unsigned Alignment = 16;
82 #else
83  static const unsigned Alignment = 8;
84 #endif
85 #else
86  static const unsigned Alignment = 8;
87 #endif
88 
90 
98  KOKKOS_INLINE_FUNCTION
99  static void* alloc(const size_t size) {
100  void* ptr = 0;
101  if (size > 0) {
102 #if STOKHOS_ALIGN_MEMORY
103  const size_t mask = Alignment-1;
104  const size_t total_size = size + mask + sizeof(std::ptrdiff_t);
105  char *ptr_alloc = reinterpret_cast<char*>(std::malloc(total_size));
106  char *ptr_storage = ptr_alloc + sizeof(std::ptrdiff_t);
107  char *ptr_body = reinterpret_cast<char*>(
108  ( reinterpret_cast<size_t>(ptr_storage) + mask ) & ~mask );
109  char *ptr_header = ptr_body - sizeof(std::ptrdiff_t);
110  const std::ptrdiff_t offset = ptr_body - ptr_alloc;
111  *reinterpret_cast<std::ptrdiff_t*>(ptr_header) = offset;
112  ptr = reinterpret_cast<void*>(ptr_body);
113 #else
114  ptr = operator new(size);
115 #endif
116  }
117  return ptr;
118  }
119 
121  KOKKOS_INLINE_FUNCTION
122  static void free(void *ptr) {
123  if (ptr != 0) {
124 #if STOKHOS_ALIGN_MEMORY
125  void *ptr_header = reinterpret_cast<char*>(ptr) - sizeof(std::ptrdiff_t);
126  const std::ptrdiff_t offset = *reinterpret_cast<std::ptrdiff_t*>(ptr_header);
127  void *ptr_alloc = reinterpret_cast<char*>(ptr) - offset;
128  std::free(ptr_alloc);
129 #else
130  operator delete(ptr);
131 #endif
132  }
133  }
134 };
135 
137 template <typename T>
139 public:
140 
141  typedef T value_type;
142  typedef T* pointer;
143  typedef const T* const_pointer;
144  typedef T& reference;
145  typedef const T& const_reference;
146  typedef size_t size_type;
147  typedef std::ptrdiff_t difference_type;
148 
150 
151  template <class U> struct rebind { typedef aligned_allocator<U> other; };
152 
154 
155  template <class U> aligned_allocator(const aligned_allocator<U>&) {}
156 
157  size_type max_size() const {
158  return (size_type(~0) - size_type(Traits::Alignment)) / sizeof(T);
159  }
160 
161  pointer address(reference x) const { return &x; }
162 
164 
165  pointer allocate(size_type n, const void* = 0) {
166  size_type count = n * sizeof(T);
167  void* ptr = Traits::alloc(count);
168  if (ptr == 0) throw std::bad_alloc();
169  return reinterpret_cast<pointer>(ptr);
170  }
171 
173 
174  void construct(pointer p, const_reference val) { new (p) T(val); }
175 
176  void destroy(pointer p) { ((T*)p)->~T(); }
177 };
178 
180 template <typename T>
181 class aligned_allocator< const T > {
182 public:
183 
184  typedef T value_type;
185  typedef const T* pointer;
186  typedef const T* const_pointer;
187  typedef const T& reference;
188  typedef const T& const_reference;
189  typedef size_t size_type;
190  typedef std::ptrdiff_t difference_type;
191 
193 
194  template <class U> struct rebind { typedef aligned_allocator<U> other; };
195 
197 
198  template <class U> aligned_allocator(const aligned_allocator<U>&) {}
199 
200  size_type max_size() const {
201  return (size_type(~0) - size_type(Traits::Alignment)) / sizeof(T);
202  }
203 
205 
206  pointer allocate(size_type n, const void* = 0) {
207  size_type count = n * sizeof(T);
208  void* ptr = Traits::alloc(count);
209  if (ptr == 0) throw std::bad_alloc();
210  return reinterpret_cast<pointer>(ptr);
211  }
212 
214 
215  void construct(pointer p, const_reference val) { new (p) T(val); }
216 
217  void destroy(pointer p) { ((T*)p)->~T(); }
218 };
219 
220 template <typename T, typename U>
221 inline bool
223 { return true; }
224 
225 template <typename T, typename U>
226 inline bool
228 { return false; }
229 
230 } // namespace Stokhos
231 
232 #endif // STOKHOS_MEMORY_TRAITS_HPP
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc()
An aligned STL allocator.
expr val()
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()
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
const_pointer address(const_reference x) const
Top-level namespace for Stokhos classes and functions.
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.
pointer address(reference x) const
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 > &)