81895f9c3ed0eaea61bd8cfe262fb8f20afd7287
[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, __FILE__, __LINE__,
81                       "Not enough memory. Failed to malloc %" PRId64
82                       " bytes for %s\n"
83                       "(called from file %s, line %d)",
84                       static_cast<int64_t>(size), name, file, line);
85         }
86         (void)memset(p, 0, size);
87     }
88     return p;
89 }
90
91 void* save_calloc(const char* name, const char* file, int line, size_t nelem, size_t elsize)
92 {
93     void* p;
94
95     p = nullptr;
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, name, file, line, rank);
108         }
109 #endif
110 #if GMX_BROKEN_CALLOC
111         /* emulate calloc(3) with malloc/memset on machines with
112            a broken calloc, e.g. in -lgmalloc on cray xt3. */
113         if ((p = malloc((size_t)nelem * (size_t)elsize)) == NULL)
114         {
115             gmx_fatal(errno, __FILE__, __LINE__,
116                       "Not enough memory. Failed to calloc %" PRId64 " elements of size %" PRId64
117                       " for %s\n(called from file %s, line %d)",
118                       (int64_t)nelem, (int64_t)elsize, name, file, line);
119         }
120         memset(p, 0, (size_t)(nelem * elsize));
121 #else
122         if ((p = calloc(nelem, elsize)) == nullptr)
123         {
124             gmx_fatal(errno, __FILE__, __LINE__,
125                       "Not enough memory. Failed to calloc %" PRId64 " elements of size %" PRId64
126                       " for %s\n(called from file %s, line %d)",
127                       static_cast<int64_t>(nelem), static_cast<int64_t>(elsize), name, file, line);
128         }
129 #endif
130     }
131     return p;
132 }
133
134 void* save_realloc(const char* name, const char* file, int line, void* ptr, size_t nelem, size_t elsize)
135 {
136     void*  p;
137     size_t size = nelem * elsize;
138
139     p = nullptr;
140     if (size == 0)
141     {
142         save_free(name, file, line, ptr);
143     }
144     else
145     {
146 #ifdef PRINT_ALLOC_KB
147         if (size >= PRINT_ALLOC_KB * 1024)
148         {
149             int rank = gmx_node_rank();
150             printf("Reallocating %.1f MB for %s (called from file %s, line %d on %d)\n",
151                    size / 1048576.0, name, file, line, rank);
152         }
153 #endif
154         if (ptr == nullptr)
155         {
156             p = malloc(size);
157         }
158         else
159         {
160             p = realloc(ptr, size);
161         }
162         if (p == nullptr)
163         {
164             gmx_fatal(errno, __FILE__, __LINE__,
165                       "Not enough memory. Failed to realloc %zu bytes for %s, %s=%p\n"
166                       "(called from file %s, line %d)",
167                       size, name, name, ptr, file, line);
168         }
169     }
170     return p;
171 }
172
173 void save_free(const char gmx_unused* name, const char gmx_unused* file, int gmx_unused line, void* ptr)
174 {
175     if (ptr != nullptr)
176     {
177         free(ptr);
178     }
179 }
180
181 /* Pointers allocated with this routine should only be freed
182  * with save_free_aligned, however this will only matter
183  * on systems that lack posix_memalign() and memalign() when
184  * freeing memory that needed to be adjusted to achieve
185  * the necessary alignment. */
186 void* save_malloc_aligned(const char* name, const char* file, int line, size_t nelem, size_t elsize, size_t alignment)
187 {
188     void* p;
189
190     if (alignment == 0)
191     {
192         gmx_fatal(errno, __FILE__, __LINE__,
193                   "Cannot allocate aligned memory with alignment of zero!\n(called from file %s, "
194                   "line %d)",
195                   file, line);
196     }
197
198     size_t alignmentSize = gmx::AlignedAllocationPolicy::alignment();
199     if (alignment > alignmentSize)
200     {
201         gmx_fatal(errno, __FILE__, __LINE__,
202                   "Cannot allocate aligned memory with alignment > %zu bytes\n(called from file "
203                   "%s, line %d)",
204                   alignmentSize, file, line);
205     }
206
207
208     if (nelem == 0 || elsize == 0)
209     {
210         p = nullptr;
211     }
212     else
213     {
214 #ifdef PRINT_ALLOC_KB
215         if (nelem * elsize >= PRINT_ALLOC_KB * 1024)
216         {
217             int rank = gmx_node_rank();
218             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
219                    nelem * elsize / 1048576.0, name, file, line, rank);
220         }
221 #endif
222
223         p = gmx::AlignedAllocationPolicy::malloc(nelem * elsize);
224
225         if (p == nullptr)
226         {
227             gmx_fatal(errno, __FILE__, __LINE__,
228                       "Not enough memory. Failed to allocate %zu aligned elements of size %zu for "
229                       "%s\n(called from file %s, line %d)",
230                       nelem, elsize, name, file, line);
231         }
232     }
233     return p;
234 }
235
236 void* save_calloc_aligned(const char* name, const char* file, int line, size_t nelem, size_t elsize, size_t alignment)
237 {
238     void* aligned = save_malloc_aligned(name, file, line, nelem, elsize, alignment);
239     if (aligned != nullptr)
240     {
241         memset(aligned, 0, static_cast<size_t>(nelem * elsize));
242     }
243     return aligned;
244 }
245
246 /* This routine can NOT be called with any pointer */
247 void save_free_aligned(const char gmx_unused* name, const char gmx_unused* file, int gmx_unused line, void* ptr)
248 {
249     gmx::AlignedAllocationPolicy::free(ptr);
250 }
251
252 void set_over_alloc_dd(gmx_bool set)
253 {
254     tMPI_Thread_mutex_lock(&g_over_alloc_mutex);
255     /* we just make sure that we don't set this at the same time.
256        We don't worry too much about reading this rarely-set variable */
257     g_bOverAllocDD = set;
258     tMPI_Thread_mutex_unlock(&g_over_alloc_mutex);
259 }
260
261 int over_alloc_dd(int n)
262 {
263     if (g_bOverAllocDD)
264     {
265         return static_cast<int>(OVER_ALLOC_FAC * n + 100);
266     }
267     else
268     {
269         return n;
270     }
271 }