Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
75namespace Stokhos {
76
78template <typename MemorySpace>
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
94template <>
95struct 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
160template <typename T>
162public:
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
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
203template <typename T>
204class aligned_allocator< const T > {
205public:
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
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
243template <typename T, typename U>
244inline bool
246{ return true; }
247
248template <typename T, typename U>
249inline bool
251{ return false; }
252
253} // namespace Stokhos
254
255#endif // STOKHOS_MEMORY_TRAITS_HPP
expr val()
void construct(pointer p, const_reference val)
aligned_allocator(const aligned_allocator< U > &)
const_pointer address(const_reference x) const
pointer allocate(size_type n, const void *=0)
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
An aligned STL allocator.
aligned_allocator(const aligned_allocator< U > &)
void construct(pointer p, const_reference val)
pointer address(reference x) const
const_pointer address(const_reference x) const
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
void deallocate(pointer p, size_type)
pointer allocate(size_type n, const void *=0)
Top-level namespace for Stokhos classes and functions.
bool operator!=(const aligned_allocator< T > &, const aligned_allocator< U > &)
bool operator==(const aligned_allocator< T > &, const aligned_allocator< U > &)
Specialization of MemoryTraits for host memory spaces.
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory.
static const unsigned Alignment
Bytes to which memory allocations are aligned.
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc()
Traits class encapsulting memory alignment.
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc()
static const unsigned Alignment
Bytes to which memory allocations are aligned.
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory of given size.