Replace compat::make_unique with std::make_unique
[alexxy/gromacs.git] / src / gromacs / utility / alignedallocator.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2017,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements AlignedAllocator.
38  *
39  * \author Erik Lindahl <erik.lindahl@gmail.com>
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \ingroup module_utility
42  */
43 #include "gmxpre.h"
44
45 #include "alignedallocator.h"
46
47 #include "config.h"
48
49 #include <cstdlib>
50
51 #include <memory>
52
53 #if HAVE_MM_MALLOC_H
54 #    include <mm_malloc.h>
55 #elif HAVE_MALLOC_H
56 #    include <malloc.h>
57 #elif HAVE_XMMINTRIN_H
58 #    include <xmmintrin.h>
59 #endif
60
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>
63 #endif
64
65 #if GMX_NATIVE_WINDOWS
66 #include <windows.h>  // only for the page size query purposes
67 #endif
68
69 #include "gromacs/utility/gmxassert.h"
70
71 namespace gmx
72 {
73
74 namespace
75 {
76
77 /*! \brief Allocate aligned memory in a fully portable way
78  *
79  *  \param bytes  Amount of memory (bytes) to allocate. The routine will return
80  *                nullptr if the allocation fails. However, note that asking for
81  *                zero bytes will return a pointer that is non-null and properly
82  *                aligned (but obviously you cannot use it, since you promised
83  *                not to access data beyond the 0 bytes you asked for).
84  *
85  *  \param alignment  Alignment specification in bytes, must be a power of 2.
86  *
87  * \return Nonzero pointer if the allocation worked, otherwise nullptr.
88  *  This routine should only be called from alignedMalloc(), which also does
89  *  the checking for valid values. This particular function is used for platforms
90  *  where we have no control of the alignment of memory returned by the system.
91  *  Instead, we increase the amount of memory requested internally such that we
92  *  both can create a pointer inside this memory that fulfills the memory
93  *  alignment requested, and that we have room to store the original pointer
94  *  just before this area.
95  *
96  *  \note This is an internal routine that should only be called from
97  *        gmx::alignedMalloc(). Just like system-provided routines, it provides
98  *        memory that is aligned - but not padded.
99  */
100 gmx_unused void *
101 alignedMallocGeneric(std::size_t bytes, std::size_t alignment)
102 {
103     // The amount of extra memory (beyound what the user asked for) we need is:
104     // - sizeof(void *), to store the original pointer
105     // - alignment, to make sure we have an aligned pointer in the area
106     void * pMalloc = malloc(bytes + sizeof(void *) + alignment);
107
108     if (pMalloc == nullptr)
109     {
110         return nullptr;
111     }
112
113     // Convert pMalloc to size_t (so we work with raw bytes), add the space we
114     // need to save the original pointer, and (alignment-1) bytes, and then mask
115     // out the lowest bits.
116     std::size_t mask     = ~static_cast<std::size_t>(alignment-1);
117     void      * pAligned = reinterpret_cast<void *>((reinterpret_cast<std::size_t>(pMalloc) + sizeof(void *) + alignment - 1) & mask);
118
119     // Store original pointer. Since we allocated at least sizeof(void *) extra
120     // space this is always a valid memory location.
121     reinterpret_cast<void **>(pAligned)[-1] = pMalloc;
122
123     return pAligned;
124 }
125
126
127 /*! \brief Free aligned memory
128  *
129  *  \param p  Memory pointer previously returned from
130  *            gmx::internal::alignedFreePortable().
131  *
132  *  Since this routine relies on the original pointer being stored just before
133  *  the memory area p points to, bad things will happen if you call this routine
134  *  with a pointer obtained any other way, or if you call the system free()
135  *  with a pointer obtained from std::alignedMalloc().
136  *
137  * \note  This is an internal routine that should only be called from
138  *        gmx::alignedFree().
139  */
140 gmx_unused void
141 alignedFreeGeneric(void *p)
142 {
143     if (p)
144     {
145         // Pick up the pointer stored just below p, and use that to call free()
146         free( reinterpret_cast<void **>(p)[-1] );
147     }
148 }
149
150 //! Implement malloc of \c bytes of memory, aligned to \c alignment.
151 void *mallocImpl(std::size_t bytes, std::size_t alignment)
152 {
153     void   *    p;
154
155 #if HAVE__MM_MALLOC
156     p = _mm_malloc( bytes, alignment );
157 #elif HAVE_POSIX_MEMALIGN
158     if (posix_memalign(&p, alignment, bytes) != 0)
159     {
160         p = nullptr;
161     }
162 #elif HAVE_MEMALIGN
163     p = memalign(alignment, bytes);
164 #elif HAVE__ALIGNED_MALLOC
165     p = _aligned_malloc(bytes, alignment);
166 #else
167     p = internal::alignedMallocGeneric(bytes, alignment);
168 #endif
169
170     return p;
171 }
172
173 //! Free aligned memory allocated with mallocImpl().
174 void freeImpl(void *p)
175 {
176     if (p)
177     {
178 #if HAVE__MM_MALLOC
179         _mm_free(p);
180 #elif HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN
181         free(p);
182 #elif HAVE__ALIGNED_MALLOC
183         _aligned_free(p);
184 #else
185         internal::alignedFreeGeneric(p);
186 #endif
187     }
188 }
189
190 }   // namespace
191
192 // === AlignedAllocationPolicy
193
194 std::size_t AlignedAllocationPolicy::alignment()
195 {
196     // For now we always use 128-byte alignment:
197     // 1) IBM Power already has cache lines of 128-bytes, and needs it.
198     // 2) x86 has 64 byte cache lines, but since a future AVX-1024 (rumored?)
199     //    will need 1024/8=128 byte SIMD alignment, it is safer to use that
200     //    already now.
201     // 3) The old Pentium4 used 256-byte cache prefetching (but 64-byte lines).
202     //    However, it's not worth worrying about performance for P4...
203     // 4) ARM & Sparc have 64 byte lines, but will be just fine with
204     //    128-byte alignment (nobody knows what the future brings)
205     //
206     // So, for now we're semi-lazy and just align to 128 bytes!
207     //
208     // TODO LINCS code is copying this assumption independently (for now)
209     return 128;
210 }
211
212 void *
213 AlignedAllocationPolicy::malloc(std::size_t bytes)
214 {
215     // Pad memory at the end with another alignment bytes to avoid false sharing
216     auto size = alignment();
217     bytes += size;
218
219     return mallocImpl(bytes, size);
220 }
221
222 void
223 AlignedAllocationPolicy::free(void *p)
224 {
225     freeImpl(p);
226 }
227
228 // === PageAlignedAllocationPolicy
229
230 //! Return a page size, from a sysconf/WinAPI query if available, or a default guess (4096 bytes).
231 //! \todo Move this function into sysinfo.cpp where other OS-specific code/includes live
232 static std::size_t getPageSize()
233 {
234     long        pageSize;
235 #if GMX_NATIVE_WINDOWS
236     SYSTEM_INFO si;
237     GetNativeSystemInfo(&si);
238     pageSize = si.dwPageSize;
239 #elif defined(_SC_PAGESIZE)
240     /* Note that sysconf returns -1 on its error conditions, which we
241        don't really need to check, nor can really handle at
242        initialization time. */
243     pageSize = sysconf(_SC_PAGESIZE);
244 #elif defined(_SC_PAGE_SIZE)
245     pageSize = sysconf(_SC_PAGE_SIZE);
246 #else
247     pageSize = -1;
248 #endif
249     return ((pageSize == -1) ? 4096 // A useful guess
250             : static_cast<std::size_t>(pageSize));
251 }
252
253 /* Implements the "construct on first use" idiom to avoid the static
254  * initialization order fiasco where a possible static page-aligned
255  * container would be initialized before the alignment variable was.
256  *
257  * Note that thread-safety of the initialization is guaranteed by the
258  * C++11 language standard.
259  *
260  * The size_t has no destructor, so there is no deinitialization
261  * issue.  See https://isocpp.org/wiki/faq/ctors for discussion of
262  * alternatives and trade-offs. */
263 std::size_t PageAlignedAllocationPolicy::alignment()
264 {
265     static size_t thePageSize = getPageSize();
266     return thePageSize;
267 }
268
269 void *
270 PageAlignedAllocationPolicy::malloc(std::size_t bytes)
271 {
272     return mallocImpl(bytes, alignment());
273 }
274
275 void
276 PageAlignedAllocationPolicy::free(void *p)
277 {
278     freeImpl(p);
279 }
280
281 } // namespace gmx