1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup memory
//! @{
class memory
{
public:
template<typename eT> arma_malloc inline static eT* acquire(const uword n_elem);
template<typename eT> arma_inline static void release(eT* mem);
template<typename eT> arma_inline static bool is_aligned(const eT* mem);
template<typename eT> arma_inline static void mark_as_aligned( eT*& mem);
template<typename eT> arma_inline static void mark_as_aligned(const eT*& mem);
};
template<typename eT>
arma_malloc
inline
eT*
memory::acquire(const uword n_elem)
{
if(n_elem == 0) { return nullptr; }
arma_debug_check
(
( size_t(n_elem) > (std::numeric_limits<size_t>::max() / sizeof(eT)) ),
"arma::memory::acquire(): requested size is too large"
);
eT* out_memptr;
#if defined(ARMA_ALIEN_MEM_ALLOC_FUNCTION)
{
out_memptr = (eT *) ARMA_ALIEN_MEM_ALLOC_FUNCTION(sizeof(eT)*n_elem);
}
#elif defined(ARMA_USE_TBB_ALLOC)
{
out_memptr = (eT *) scalable_malloc(sizeof(eT)*n_elem);
}
#elif defined(ARMA_USE_MKL_ALLOC)
{
out_memptr = (eT *) mkl_malloc( sizeof(eT)*n_elem, 32 );
}
#elif defined(ARMA_HAVE_POSIX_MEMALIGN)
{
eT* memptr = nullptr;
const size_t n_bytes = sizeof(eT)*size_t(n_elem);
const size_t alignment = (n_bytes >= size_t(1024)) ? size_t(32) : size_t(16);
// TODO: investigate apparent memory leak when using alignment >= 64 (as shown on Fedora 28, glibc 2.27)
int status = posix_memalign((void **)&memptr, ( (alignment >= sizeof(void*)) ? alignment : sizeof(void*) ), n_bytes);
out_memptr = (status == 0) ? memptr : nullptr;
}
#elif defined(_MSC_VER)
{
// Windoze is too primitive to handle C++17 std::aligned_alloc()
//out_memptr = (eT *) malloc(sizeof(eT)*n_elem);
//out_memptr = (eT *) _aligned_malloc( sizeof(eT)*n_elem, 16 ); // lives in malloc.h
const size_t n_bytes = sizeof(eT)*size_t(n_elem);
const size_t alignment = (n_bytes >= size_t(1024)) ? size_t(32) : size_t(16);
out_memptr = (eT *) _aligned_malloc( n_bytes, alignment );
}
#else
{
//return ( new(std::nothrow) eT[n_elem] );
out_memptr = (eT *) malloc(sizeof(eT)*n_elem);
}
#endif
// TODO: for mingw, use __mingw_aligned_malloc
arma_check_bad_alloc( (out_memptr == nullptr), "arma::memory::acquire(): out of memory" );
return out_memptr;
}
template<typename eT>
arma_inline
void
memory::release(eT* mem)
{
if(mem == nullptr) { return; }
#if defined(ARMA_ALIEN_MEM_FREE_FUNCTION)
{
ARMA_ALIEN_MEM_FREE_FUNCTION( (void *)(mem) );
}
#elif defined(ARMA_USE_TBB_ALLOC)
{
scalable_free( (void *)(mem) );
}
#elif defined(ARMA_USE_MKL_ALLOC)
{
mkl_free( (void *)(mem) );
}
#elif defined(ARMA_HAVE_POSIX_MEMALIGN)
{
free( (void *)(mem) );
}
#elif defined(_MSC_VER)
{
//free( (void *)(mem) );
_aligned_free( (void *)(mem) );
}
#else
{
//delete [] mem;
free( (void *)(mem) );
}
#endif
// TODO: for mingw, use __mingw_aligned_free
}
template<typename eT>
arma_inline
bool
memory::is_aligned(const eT* mem)
{
#if (defined(ARMA_HAVE_ICC_ASSUME_ALIGNED) || defined(ARMA_HAVE_GCC_ASSUME_ALIGNED)) && !defined(ARMA_DONT_CHECK_ALIGNMENT)
{
return (sizeof(std::size_t) >= sizeof(eT*)) ? ((std::size_t(mem) & 0x0F) == 0) : false;
}
#else
{
arma_ignore(mem);
return false;
}
#endif
}
template<typename eT>
arma_inline
void
memory::mark_as_aligned(eT*& mem)
{
#if defined(ARMA_HAVE_ICC_ASSUME_ALIGNED)
{
__assume_aligned(mem, 16);
}
#elif defined(ARMA_HAVE_GCC_ASSUME_ALIGNED)
{
mem = (eT*)__builtin_assume_aligned(mem, 16);
}
#else
{
arma_ignore(mem);
}
#endif
// TODO: look into C++20 std::assume_aligned()
// TODO: https://en.cppreference.com/w/cpp/memory/assume_aligned
// TODO: MSVC? __assume( (mem & 0x0F) == 0 );
//
// http://comments.gmane.org/gmane.comp.gcc.patches/239430
// GCC __builtin_assume_aligned is similar to ICC's __assume_aligned,
// so for lvalue first argument ICC's __assume_aligned can be emulated using
// #define __assume_aligned(lvalueptr, align) lvalueptr = __builtin_assume_aligned (lvalueptr, align)
//
// http://www.inf.ethz.ch/personal/markusp/teaching/263-2300-ETH-spring11/slides/class19.pdf
// http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/cpp/lin/index.htm
// http://d3f8ykwhia686p.cloudfront.net/1live/intel/CompilerAutovectorizationGuide.pdf
}
template<typename eT>
arma_inline
void
memory::mark_as_aligned(const eT*& mem)
{
#if defined(ARMA_HAVE_ICC_ASSUME_ALIGNED)
{
__assume_aligned(mem, 16);
}
#elif defined(ARMA_HAVE_GCC_ASSUME_ALIGNED)
{
mem = (const eT*)__builtin_assume_aligned(mem, 16);
}
#else
{
arma_ignore(mem);
}
#endif
}
//! @}
|