Merge branch release-2016 into release-2018
[gromacs.git] / src / gromacs / utility / alignedallocator.h
blob9936437dceec85926d6a1139f586731e5043bf2c
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017, 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.
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.
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.
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.
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.
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.
35 /*! \file
36 * \brief Declares allocation policy classes and allocators that are
37 * used to make library containers compatible with alignment
38 * requirements of particular hardware, e.g. memory operations for
39 * SIMD or accelerators.
41 * \author Erik Lindahl <erik.lindahl@gmail.com>
42 * \author Mark Abraham <mark.j.abraham@gmail.com>
43 * \inpublicapi
44 * \ingroup module_utility
46 #ifndef GMX_UTILITY_ALIGNEDALLOCATOR_H
47 #define GMX_UTILITY_ALIGNEDALLOCATOR_H
49 #include <cstddef>
51 #include "gromacs/utility/allocator.h"
53 namespace gmx
56 /*! \libinternal \brief Policy class for configuring gmx::Allocator, to manage
57 * allocations of aligned memory for SIMD code.
59 class AlignedAllocationPolicy
61 public:
62 /*! \brief Return the alignment size. */
63 static std::size_t
64 alignment();
65 /*! \brief Allocate memory aligned to alignment() bytes.
67 * \param bytes Amount of memory (bytes) to allocate. It is valid to ask for
68 * 0 bytes, which will return a non-null pointer that is properly
69 * aligned and padded (but that you should not use).
71 * \return Valid pointer if the allocation worked, otherwise nullptr.
73 * The memory will always be aligned to 128 bytes, which is our
74 * estimate of the longest cache lines on architectures currently in use.
75 * It will also be padded by the same amount at the end of the
76 * area, to help avoid false cache sharing.
78 * \note Memory allocated with this routine must be released with
79 * gmx::AlignedAllocationPolicy::free(), and absolutely not the system free().
81 static void *
82 malloc(std::size_t bytes);
83 /*! \brief Free aligned memory
85 * \param p Memory pointer previously returned from malloc()
87 * \note This routine should only be called with pointers obtained from
88 * gmx::AlignedAllocationPolicy::malloc(), and absolutely not any
89 * pointers obtained the system malloc().
91 static void
92 free(void *p);
95 /*! \brief Aligned memory allocator.
97 * \tparam T Type of objects to allocate
99 * This convenience partial specialization can be used for the
100 * optional allocator template parameter in standard library
101 * containers, which is necessary e.g. to use SIMD aligned load and
102 * store operations on data in those containers. The memory will
103 * always be aligned according to the behavior of
104 * AlignedAllocationPolicy.
106 template <class T>
107 using AlignedAllocator = Allocator<T, AlignedAllocationPolicy>;
110 /*! \brief Return the memory page size on this system
112 * Implements the "construct on first use" idiom to avoid the static
113 * initialization order fiasco where a possible static page-aligned
114 * container would be initialized before the alignment variable was.
116 * Note that thread-safety is guaranteed by the C++11 language
117 * standard. */
118 std::size_t pageSize();
120 /*! \libinternal \brief Policy class for configuring gmx::Allocator,
121 * to manage allocations of page-aligned memory that can be locked for
122 * asynchronous transfer to GPU devices.
124 class PageAlignedAllocationPolicy
126 public:
127 /*! \brief Return the alignment size of memory pages on this system.
129 * Queries sysconf/WinAPI, otherwise guesses 4096. */
130 static std::size_t
131 alignment();
132 /*! \brief Allocate memory aligned to alignment() bytes.
134 * \param bytes Amount of memory (bytes) to allocate. It is valid to ask for
135 * 0 bytes, which will return a non-null pointer that is properly
136 * aligned and padded (but that you should not use).
138 * \return Valid pointer if the allocation worked, otherwise nullptr.
140 * \note Memory allocated with this routine must be released with
141 * gmx::PageAlignedAllocationPolicy::free(), and absolutely not the system free().
143 static void *
144 malloc(std::size_t bytes);
145 /*! \brief Free aligned memory
147 * \param p Memory pointer previously returned from malloc()
149 * \note This routine should only be called with pointers obtained from
150 * gmx::PageAlignedAllocationPolicy::malloc(), and absolutely not any
151 * pointers obtained the system malloc().
153 static void
154 free(void *p);
157 /*! \brief PageAligned memory allocator.
159 * \tparam T Type of objects to allocate
161 * This convenience partial specialization can be used for the
162 * optional allocator template parameter in standard library
163 * containers, which is necessary for locking memory pages for
164 * asynchronous transfer between a GPU device and the host. The
165 * memory will always be aligned according to the behavior of
166 * PageAlignedAllocationPolicy.
168 template <class T>
169 using PageAlignedAllocator = Allocator<T, PageAlignedAllocationPolicy>;
171 } // namespace gmx
173 #endif // GMX_UTILITY_ALIGNEDALLOCATOR_H