SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / utility / smalloc.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "smalloc.h"
41
42 #include "config.h"
43
44 #include <cerrno>
45 #include <cstdio>
46 #include <cstdlib>
47
48 #include <mutex>
49
50 #include <cstring>
51
52 #include "gromacs/utility/alignedallocator.h"
53 #include "gromacs/utility/fatalerror.h"
54 #ifdef PRINT_ALLOC_KB
55 #    include "gromacs/utility/basenetwork.h"
56 #    include "gromacs/utility/gmxmpi.h"
57 #endif
58
59 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
60 static bool g_bOverAllocDD = false;
61 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
62 static std::mutex g_overAllocMutex;
63
64 void* save_malloc(const char* name, const char* file, int line, size_t size)
65 {
66     void* p = nullptr;
67
68     if (size == 0)
69     {
70         p = nullptr;
71     }
72     else
73     {
74         if ((p = malloc(size)) == nullptr)
75         {
76             gmx_fatal(errno,
77                       __FILE__,
78                       __LINE__,
79                       "Not enough memory. Failed to malloc %" PRId64
80                       " bytes for %s\n"
81                       "(called from file %s, line %d)",
82                       static_cast<int64_t>(size),
83                       name,
84                       file,
85                       line);
86         }
87         (void)memset(p, 0, size);
88     }
89     return p;
90 }
91
92 void* save_calloc(const char* name, const char* file, int line, size_t nelem, size_t elsize)
93 {
94     void* p = nullptr;
95
96     if ((nelem == 0) || (elsize == 0))
97     {
98         p = nullptr;
99     }
100     else
101     {
102 #ifdef PRINT_ALLOC_KB
103         if (nelem * elsize >= PRINT_ALLOC_KB * 1024)
104         {
105             int rank = gmx_node_rank();
106             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
107                    nelem * elsize / 1048576.0,
108                    name,
109                    file,
110                    line,
111                    rank);
112         }
113 #endif
114 #if GMX_BROKEN_CALLOC
115         /* emulate calloc(3) with malloc/memset on machines with
116            a broken calloc, e.g. in -lgmalloc on cray xt3. */
117         if ((p = malloc((size_t)nelem * (size_t)elsize)) == NULL)
118         {
119             gmx_fatal(errno,
120                       __FILE__,
121                       __LINE__,
122                       "Not enough memory. Failed to calloc %" PRId64 " elements of size %" PRId64
123                       " for %s\n(called from file %s, line %d)",
124                       (int64_t)nelem,
125                       (int64_t)elsize,
126                       name,
127                       file,
128                       line);
129         }
130         memset(p, 0, (size_t)(nelem * elsize));
131 #else
132         if ((p = calloc(nelem, elsize)) == nullptr)
133         {
134             gmx_fatal(errno,
135                       __FILE__,
136                       __LINE__,
137                       "Not enough memory. Failed to calloc %" PRId64 " elements of size %" PRId64
138                       " for %s\n(called from file %s, line %d)",
139                       static_cast<int64_t>(nelem),
140                       static_cast<int64_t>(elsize),
141                       name,
142                       file,
143                       line);
144         }
145 #endif
146     }
147     return p;
148 }
149
150 void* save_realloc(const char* name, const char* file, int line, void* ptr, size_t nelem, size_t elsize)
151 {
152     void*  p    = nullptr;
153     size_t size = nelem * elsize;
154
155     if (size == 0)
156     {
157         save_free(name, file, line, ptr);
158     }
159     else
160     {
161 #ifdef PRINT_ALLOC_KB
162         if (size >= PRINT_ALLOC_KB * 1024)
163         {
164             int rank = gmx_node_rank();
165             printf("Reallocating %.1f MB for %s (called from file %s, line %d on %d)\n",
166                    size / 1048576.0,
167                    name,
168                    file,
169                    line,
170                    rank);
171         }
172 #endif
173         if (ptr == nullptr)
174         {
175             p = malloc(size);
176         }
177         else
178         {
179             p = realloc(ptr, size);
180         }
181         if (p == nullptr)
182         {
183             gmx_fatal(errno,
184                       __FILE__,
185                       __LINE__,
186                       "Not enough memory. Failed to realloc %zu bytes for %s, %s=%p\n"
187                       "(called from file %s, line %d)",
188                       size,
189                       name,
190                       name,
191                       ptr,
192                       file,
193                       line);
194         }
195     }
196     return p;
197 }
198
199 void save_free(const char gmx_unused* name, const char gmx_unused* file, int gmx_unused line, void* ptr)
200 {
201     if (ptr != nullptr)
202     {
203         free(ptr);
204     }
205 }
206
207 /* Pointers allocated with this routine should only be freed
208  * with save_free_aligned, however this will only matter
209  * on systems that lack posix_memalign() and memalign() when
210  * freeing memory that needed to be adjusted to achieve
211  * the necessary alignment. */
212 void* save_malloc_aligned(const char* name, const char* file, int line, size_t nelem, size_t elsize, size_t alignment)
213 {
214     void* p = nullptr;
215
216     if (alignment == 0)
217     {
218         gmx_fatal(errno,
219                   __FILE__,
220                   __LINE__,
221                   "Cannot allocate aligned memory with alignment of zero!\n(called from file %s, "
222                   "line %d)",
223                   file,
224                   line);
225     }
226
227     size_t alignmentSize = gmx::AlignedAllocationPolicy::alignment();
228     if (alignment > alignmentSize)
229     {
230         gmx_fatal(errno,
231                   __FILE__,
232                   __LINE__,
233                   "Cannot allocate aligned memory with alignment > %zu bytes\n(called from file "
234                   "%s, line %d)",
235                   alignmentSize,
236                   file,
237                   line);
238     }
239
240
241     if (nelem == 0 || elsize == 0)
242     {
243         p = nullptr;
244     }
245     else
246     {
247 #ifdef PRINT_ALLOC_KB
248         if (nelem * elsize >= PRINT_ALLOC_KB * 1024)
249         {
250             int rank = gmx_node_rank();
251             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
252                    nelem * elsize / 1048576.0,
253                    name,
254                    file,
255                    line,
256                    rank);
257         }
258 #endif
259
260         p = gmx::AlignedAllocationPolicy::malloc(nelem * elsize);
261
262         if (p == nullptr)
263         {
264             gmx_fatal(errno,
265                       __FILE__,
266                       __LINE__,
267                       "Not enough memory. Failed to allocate %zu aligned elements of size %zu for "
268                       "%s\n(called from file %s, line %d)",
269                       nelem,
270                       elsize,
271                       name,
272                       file,
273                       line);
274         }
275     }
276     return p;
277 }
278
279 void* save_calloc_aligned(const char* name, const char* file, int line, size_t nelem, size_t elsize, size_t alignment)
280 {
281     void* aligned = save_malloc_aligned(name, file, line, nelem, elsize, alignment);
282     if (aligned != nullptr)
283     {
284         memset(aligned, 0, static_cast<size_t>(nelem * elsize));
285     }
286     return aligned;
287 }
288
289 /* This routine can NOT be called with any pointer */
290 void save_free_aligned(const char gmx_unused* name, const char gmx_unused* file, int gmx_unused line, void* ptr)
291 {
292     gmx::AlignedAllocationPolicy::free(ptr);
293 }
294
295 void set_over_alloc_dd(bool set)
296 {
297     std::lock_guard<std::mutex> lock(g_overAllocMutex);
298     /* we just make sure that we don't set this at the same time.
299        We don't worry too much about reading this rarely-set variable */
300     g_bOverAllocDD = set;
301 }
302
303 int over_alloc_dd(int n)
304 {
305     if (g_bOverAllocDD)
306     {
307         return static_cast<int>(OVER_ALLOC_FAC * n + 100);
308     }
309     else
310     {
311         return n;
312     }
313 }