Stokhos Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
src
sacado
kokkos
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>
161
class
aligned_allocator
{
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
172
typedef
Stokhos::MemoryTraits< Kokkos::HostSpace >
Traits
;
173
174
template
<
class
U>
struct
rebind
{
typedef
aligned_allocator<U>
other
; };
175
176
aligned_allocator
() {}
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
195
void
deallocate
(
pointer
p,
size_type
) {
Traits::free
(p); }
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
215
typedef
Stokhos::MemoryTraits< Kokkos::HostSpace >
Traits
;
216
217
template
<
class
U>
struct
rebind
{
typedef
aligned_allocator<U>
other
; };
218
219
aligned_allocator
() {}
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
236
void
deallocate
(
pointer
p,
size_type
) {
Traits::free
(p); }
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
245
operator ==
(
const
aligned_allocator<T>
&,
const
aligned_allocator<U>
&)
246
{
return
true
; }
247
248
template
<
typename
T,
typename
U>
249
inline
bool
250
operator !=
(
const
aligned_allocator<T>
&,
const
aligned_allocator<U>
&)
251
{
return
false
; }
252
253
}
// namespace Stokhos
254
255
#endif
// STOKHOS_MEMORY_TRAITS_HPP
val
expr val()
Stokhos::aligned_allocator< const T >::reference
const T & reference
Definition
Stokhos_MemoryTraits.hpp:210
Stokhos::aligned_allocator< const T >::size_type
size_t size_type
Definition
Stokhos_MemoryTraits.hpp:212
Stokhos::aligned_allocator< const T >::const_reference
const T & const_reference
Definition
Stokhos_MemoryTraits.hpp:211
Stokhos::aligned_allocator< const T >::deallocate
void deallocate(pointer p, size_type)
Definition
Stokhos_MemoryTraits.hpp:236
Stokhos::aligned_allocator< const T >::construct
void construct(pointer p, const_reference val)
Definition
Stokhos_MemoryTraits.hpp:238
Stokhos::aligned_allocator< const T >::value_type
T value_type
Definition
Stokhos_MemoryTraits.hpp:207
Stokhos::aligned_allocator< const T >::aligned_allocator
aligned_allocator(const aligned_allocator< U > &)
Definition
Stokhos_MemoryTraits.hpp:221
Stokhos::aligned_allocator< const T >::address
const_pointer address(const_reference x) const
Definition
Stokhos_MemoryTraits.hpp:227
Stokhos::aligned_allocator< const T >::allocate
pointer allocate(size_type n, const void *=0)
Definition
Stokhos_MemoryTraits.hpp:229
Stokhos::aligned_allocator< const T >::max_size
size_type max_size() const
Definition
Stokhos_MemoryTraits.hpp:223
Stokhos::aligned_allocator< const T >::destroy
void destroy(pointer p)
Definition
Stokhos_MemoryTraits.hpp:240
Stokhos::aligned_allocator< const T >::Traits
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
Definition
Stokhos_MemoryTraits.hpp:215
Stokhos::aligned_allocator< const T >::difference_type
std::ptrdiff_t difference_type
Definition
Stokhos_MemoryTraits.hpp:213
Stokhos::aligned_allocator< const T >::aligned_allocator
aligned_allocator()
Definition
Stokhos_MemoryTraits.hpp:219
Stokhos::aligned_allocator< const T >::const_pointer
const T * const_pointer
Definition
Stokhos_MemoryTraits.hpp:209
Stokhos::aligned_allocator< const T >::pointer
const T * pointer
Definition
Stokhos_MemoryTraits.hpp:208
Stokhos::aligned_allocator
An aligned STL allocator.
Definition
Stokhos_MemoryTraits.hpp:161
Stokhos::aligned_allocator::aligned_allocator
aligned_allocator(const aligned_allocator< U > &)
Definition
Stokhos_MemoryTraits.hpp:178
Stokhos::aligned_allocator::const_pointer
const T * const_pointer
Definition
Stokhos_MemoryTraits.hpp:166
Stokhos::aligned_allocator::construct
void construct(pointer p, const_reference val)
Definition
Stokhos_MemoryTraits.hpp:197
Stokhos::aligned_allocator::value_type
T value_type
Definition
Stokhos_MemoryTraits.hpp:164
Stokhos::aligned_allocator::pointer
T * pointer
Definition
Stokhos_MemoryTraits.hpp:165
Stokhos::aligned_allocator::const_reference
const T & const_reference
Definition
Stokhos_MemoryTraits.hpp:168
Stokhos::aligned_allocator::address
pointer address(reference x) const
Definition
Stokhos_MemoryTraits.hpp:184
Stokhos::aligned_allocator::address
const_pointer address(const_reference x) const
Definition
Stokhos_MemoryTraits.hpp:186
Stokhos::aligned_allocator::difference_type
std::ptrdiff_t difference_type
Definition
Stokhos_MemoryTraits.hpp:170
Stokhos::aligned_allocator::destroy
void destroy(pointer p)
Definition
Stokhos_MemoryTraits.hpp:199
Stokhos::aligned_allocator::Traits
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
Definition
Stokhos_MemoryTraits.hpp:172
Stokhos::aligned_allocator::aligned_allocator
aligned_allocator()
Definition
Stokhos_MemoryTraits.hpp:176
Stokhos::aligned_allocator::max_size
size_type max_size() const
Definition
Stokhos_MemoryTraits.hpp:180
Stokhos::aligned_allocator::size_type
size_t size_type
Definition
Stokhos_MemoryTraits.hpp:169
Stokhos::aligned_allocator::deallocate
void deallocate(pointer p, size_type)
Definition
Stokhos_MemoryTraits.hpp:195
Stokhos::aligned_allocator::allocate
pointer allocate(size_type n, const void *=0)
Definition
Stokhos_MemoryTraits.hpp:188
Stokhos::aligned_allocator::reference
T & reference
Definition
Stokhos_MemoryTraits.hpp:167
Kokkos
Definition
Stokhos_CrsMatrix.hpp:663
Stokhos
Top-level namespace for Stokhos classes and functions.
Definition
Stokhos_AbstractPreconditionerFactory.hpp:48
Stokhos::operator!=
bool operator!=(const aligned_allocator< T > &, const aligned_allocator< U > &)
Definition
Stokhos_MemoryTraits.hpp:250
Stokhos::operator==
bool operator==(const aligned_allocator< T > &, const aligned_allocator< U > &)
Definition
Stokhos_MemoryTraits.hpp:245
Stokhos::MemoryTraits< Kokkos::HostSpace >
Specialization of MemoryTraits for host memory spaces.
Definition
Stokhos_MemoryTraits.hpp:95
Stokhos::MemoryTraits< Kokkos::HostSpace >::alloc
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory.
Definition
Stokhos_MemoryTraits.hpp:122
Stokhos::MemoryTraits< Kokkos::HostSpace >::Alignment
static const unsigned Alignment
Bytes to which memory allocations are aligned.
Definition
Stokhos_MemoryTraits.hpp:109
Stokhos::MemoryTraits< Kokkos::HostSpace >::free
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc().
Definition
Stokhos_MemoryTraits.hpp:145
Stokhos::MemoryTraits
Traits class encapsulting memory alignment.
Definition
Stokhos_MemoryTraits.hpp:79
Stokhos::MemoryTraits::free
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc().
Definition
Stokhos_MemoryTraits.hpp:90
Stokhos::MemoryTraits< memory_space >::Alignment
static const unsigned Alignment
Definition
Stokhos_MemoryTraits.hpp:82
Stokhos::MemoryTraits::alloc
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory of given size.
Definition
Stokhos_MemoryTraits.hpp:86
Stokhos::aligned_allocator::rebind
Definition
Stokhos_MemoryTraits.hpp:174
Stokhos::aligned_allocator::rebind::other
aligned_allocator< U > other
Definition
Stokhos_MemoryTraits.hpp:174
Stokhos::aligned_allocator< const T >::rebind
Definition
Stokhos_MemoryTraits.hpp:217
Stokhos::aligned_allocator< const T >::rebind::other
aligned_allocator< U > other
Definition
Stokhos_MemoryTraits.hpp:217
Generated by
1.17.0