c0511c32dcc74543e1dbda6cf9f2bd17b0f90fde
[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, 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 #ifdef WITH_DMALLOC
49 #    include <dmalloc.h>
50 #endif
51
52 #include <cstring>
53
54 #include "thread_mpi/threads.h"
55
56 #include "gromacs/utility/alignedallocator.h"
57 #include "gromacs/utility/dir_separator.h"
58 #include "gromacs/utility/fatalerror.h"
59 #ifdef PRINT_ALLOC_KB
60 #    include "gromacs/utility/basenetwork.h"
61 #    include "gromacs/utility/gmxmpi.h"
62 #endif
63
64 static gmx_bool            g_bOverAllocDD     = FALSE;
65 static tMPI_Thread_mutex_t g_over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
66
67 void* save_malloc(const char* name, const char* file, int line, size_t size)
68 {
69     void* p;
70
71     p = nullptr;
72     if (size == 0)
73     {
74         p = nullptr;
75     }
76     else
77     {
78         if ((p = malloc(size)) == nullptr)
79         {
80             gmx_fatal(errno,
81                       __FILE__,
82                       __LINE__,
83                       "Not enough memory. Failed to malloc %" PRId64
84                       " bytes for %s\n"
85                       "(called from file %s, line %d)",
86                       static_cast<int64_t>(size),
87                       name,
88                       file,
89                       line);
90         }
91         (void)memset(p, 0, size);
92     }
93     return p;
94 }
95
96 void* save_calloc(const char* name, const char* file, int line, size_t nelem, size_t elsize)
97 {
98     void* p;
99
100     p = nullptr;
101     if ((nelem == 0) || (elsize == 0))
102     {
103         p = nullptr;
104     }
105     else
106     {
107 #ifdef PRINT_ALLOC_KB
108         if (nelem * elsize >= PRINT_ALLOC_KB * 1024)
109         {
110             int rank = gmx_node_rank();
111             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
112                    nelem * elsize / 1048576.0,
113                    name,
114                    file,
115                    line,
116                    rank);
117         }
118 #endif
119 #if GMX_BROKEN_CALLOC
120         /* emulate calloc(3) with malloc/memset on machines with
121            a broken calloc, e.g. in -lgmalloc on cray xt3. */
122         if ((p = malloc((size_t)nelem * (size_t)elsize)) == NULL)
123         {
124             gmx_fatal(errno,
125                       __FILE__,
126                       __LINE__,
127                       "Not enough memory. Failed to calloc %" PRId64 " elements of size %" PRId64
128                       " for %s\n(called from file %s, line %d)",
129                       (int64_t)nelem,
130                       (int64_t)elsize,
131                       name,
132                       file,
133                       line);
134         }
135         memset(p, 0, (size_t)(nelem * elsize));
136 #else
137         if ((p = calloc(nelem, elsize)) == nullptr)
138         {
139             gmx_fatal(errno,
140                       __FILE__,
141                       __LINE__,
142                       "Not enough memory. Failed to calloc %" PRId64 " elements of size %" PRId64
143                       " for %s\n(called from file %s, line %d)",
144                       static_cast<int64_t>(nelem),
145                       static_cast<int64_t>(elsize),
146                       name,
147                       file,
148                       line);
149         }
150 #endif
151     }
152     return p;
153 }
154
155 void* save_realloc(const char* name, const char* file, int line, void* ptr, size_t nelem, size_t elsize)
156 {
157     void*  p;
158     size_t size = nelem * elsize;
159
160     p = nullptr;
161     if (size == 0)
162     {
163         save_free(name, file, line, ptr);
164     }
165     else
166     {
167 #ifdef PRINT_ALLOC_KB
168         if (size >= PRINT_ALLOC_KB * 1024)
169         {
170             int rank = gmx_node_rank();
171             printf("Reallocating %.1f MB for %s (called from file %s, line %d on %d)\n",
172                    size / 1048576.0,
173                    name,
174                    file,
175                    line,
176                    rank);
177         }
178 #endif
179         if (ptr == nullptr)
180         {
181             p = malloc(size);
182         }
183         else
184         {
185             p = realloc(ptr, size);
186         }
187         if (p == nullptr)
188         {
189             gmx_fatal(errno,
190                       __FILE__,
191                       __LINE__,
192                       "Not enough memory. Failed to realloc %zu bytes for %s, %s=%p\n"
193                       "(called from file %s, line %d)",
194                       size,
195                       name,
196                       name,
197                       ptr,
198                       file,
199                       line);
200         }
201     }
202     return p;
203 }
204
205 void save_free(const char gmx_unused* name, const char gmx_unused* file, int gmx_unused line, void* ptr)
206 {
207     if (ptr != nullptr)
208     {
209         free(ptr);
210     }
211 }
212
213 /* Pointers allocated with this routine should only be freed
214  * with save_free_aligned, however this will only matter
215  * on systems that lack posix_memalign() and memalign() when
216  * freeing memory that needed to be adjusted to achieve
217  * the necessary alignment. */
218 void* save_malloc_aligned(const char* name, const char* file, int line, size_t nelem, size_t elsize, size_t alignment)
219 {
220     void* p;
221
222     if (alignment == 0)
223     {
224         gmx_fatal(errno,
225                   __FILE__,
226                   __LINE__,
227                   "Cannot allocate aligned memory with alignment of zero!\n(called from file %s, "
228                   "line %d)",
229                   file,
230                   line);
231     }
232
233     size_t alignmentSize = gmx::AlignedAllocationPolicy::alignment();
234     if (alignment > alignmentSize)
235     {
236         gmx_fatal(errno,
237                   __FILE__,
238                   __LINE__,
239                   "Cannot allocate aligned memory with alignment > %zu bytes\n(called from file "
240                   "%s, line %d)",
241                   alignmentSize,
242                   file,
243                   line);
244     }
245
246
247     if (nelem == 0 || elsize == 0)
248     {
249         p = nullptr;
250     }
251     else
252     {
253 #ifdef PRINT_ALLOC_KB
254         if (nelem * elsize >= PRINT_ALLOC_KB * 1024)
255         {
256             int rank = gmx_node_rank();
257             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
258                    nelem * elsize / 1048576.0,
259                    name,
260                    file,
261                    line,
262                    rank);
263         }
264 #endif
265
266         p = gmx::AlignedAllocationPolicy::malloc(nelem * elsize);
267
268         if (p == nullptr)
269         {
270             gmx_fatal(errno,
271                       __FILE__,
272                       __LINE__,
273                       "Not enough memory. Failed to allocate %zu aligned elements of size %zu for "
274                       "%s\n(called from file %s, line %d)",
275                       nelem,
276                       elsize,
277                       name,
278                       file,
279                       line);
280         }
281     }
282     return p;
283 }
284
285 void* save_calloc_aligned(const char* name, const char* file, int line, size_t nelem, size_t elsize, size_t alignment)
286 {
287     void* aligned = save_malloc_aligned(name, file, line, nelem, elsize, alignment);
288     if (aligned != nullptr)
289     {
290         memset(aligned, 0, static_cast<size_t>(nelem * elsize));
291     }
292     return aligned;
293 }
294
295 /* This routine can NOT be called with any pointer */
296 void save_free_aligned(const char gmx_unused* name, const char gmx_unused* file, int gmx_unused line, void* ptr)
297 {
298     gmx::AlignedAllocationPolicy::free(ptr);
299 }
300
301 void set_over_alloc_dd(gmx_bool set)
302 {
303     tMPI_Thread_mutex_lock(&g_over_alloc_mutex);
304     /* we just make sure that we don't set this at the same time.
305        We don't worry too much about reading this rarely-set variable */
306     g_bOverAllocDD = set;
307     tMPI_Thread_mutex_unlock(&g_over_alloc_mutex);
308 }
309
310 int over_alloc_dd(int n)
311 {
312     if (g_bOverAllocDD)
313     {
314         return static_cast<int>(OVER_ALLOC_FAC * n + 100);
315     }
316     else
317     {
318         return n;
319     }
320 }