ac8fd499b0e6849de70030b19d7e9755ba3feb30
[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, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "smalloc.h"
40
41 #include "config.h"
42
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 #ifdef WITH_DMALLOC
48 #include <dmalloc.h>
49 #endif
50 #ifdef HAVE__ALIGNED_MALLOC
51 #include <malloc.h>
52 #endif
53
54 #include <cstring>
55
56 #include "thread_mpi/threads.h"
57
58 #include "gromacs/utility/dir_separator.h"
59 #include "gromacs/utility/fatalerror.h"
60 #ifdef PRINT_ALLOC_KB
61 #include "gromacs/utility/basenetwork.h"
62 #include "gromacs/utility/gmxmpi.h"
63 #endif
64
65 static gmx_bool            g_bOverAllocDD     = FALSE;
66 static tMPI_Thread_mutex_t g_over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
67
68 #ifdef DEBUG
69 static void log_action(int bMal, const char *what, const char *file, int line,
70                        int nelem, int size, void *ptr)
71 {
72     static int btot = 0;
73     char      *NN   = "NULL";
74     int        bytes;
75
76     bytes = size*nelem;
77     if (!bMal)
78     {
79         bytes = -bytes;
80     }
81
82     tMPI_Thread_mutex_lock(&gmx_logfile_mtx);
83
84     /* This total memory count is not correct, since with realloc
85      * it adds the whole size again, not just the increment.
86      */
87     /* This static variable is protected by the mutex too... */
88     btot += bytes;
89
90     bytes /= 1024;
91     if (debug && (bytes != 0))
92     {
93         fprintf(debug, "%s:%d kB (%7d kB) [%s, line %d, nelem %d, size %d]\n",
94                 what ? what : NN, bytes, btot/1024,
95                 file ? file : NN, line, nelem, size);
96     }
97     /* Print to stderr for things larger than 1 MB */
98     if (bytes >= 1024 || bytes <= -1024)
99     {
100         char *fname = NULL;
101         if (file)
102         {
103             fname = strrchr(file, DIR_SEPARATOR);
104             if (fname)
105             {
106                 fname++;
107             }
108             else
109             {
110                 fname = file;
111             }
112         }
113         printf("%s: %.1f MB [%s, line %d, nelem %d, size %d]\n",
114                what ? what  : NN, bytes/1024.0,
115                file ? fname : NN, line, nelem, size);
116     }
117     tMPI_Thread_mutex_unlock(&gmx_logfile_mtx);
118 }
119 #endif
120
121 void *save_malloc(const char *name, const char *file, int line, size_t size)
122 {
123     void *p;
124
125     p = NULL;
126     if (size == 0)
127     {
128         p = NULL;
129     }
130     else
131     {
132         if ((p = malloc(size)) == NULL)
133         {
134             gmx_fatal(errno, __FILE__, __LINE__,
135                       "Not enough memory. Failed to malloc %" GMX_PRId64 " bytes for %s\n"
136                       "(called from file %s, line %d)",
137                       (gmx_int64_t)size, name, file, line);
138         }
139         (void) memset(p, 0, size);
140     }
141 #ifdef DEBUG
142     log_action(1, name, file, line, 1, size, p);
143 #endif
144     return p;
145 }
146
147 void *save_calloc(const char *name, const char *file, int line,
148                   size_t nelem, size_t elsize)
149 {
150     void *p;
151
152     p = NULL;
153     if ((nelem == 0) || (elsize == 0))
154     {
155         p = NULL;
156     }
157     else
158     {
159 #ifdef PRINT_ALLOC_KB
160         if (nelem*elsize >= PRINT_ALLOC_KB*1024)
161         {
162             int rank = gmx_node_rank();
163             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
164                    nelem*elsize/1048576.0, name, file, line, rank);
165         }
166 #endif
167 #ifdef GMX_BROKEN_CALLOC
168         /* emulate calloc(3) with malloc/memset on machines with
169            a broken calloc, e.g. in -lgmalloc on cray xt3. */
170         if ((p = malloc((size_t)nelem*(size_t)elsize)) == NULL)
171         {
172             gmx_fatal(errno, __FILE__, __LINE__,
173                       "Not enough memory. Failed to calloc %" GMX_PRId64
174                       " elements of size %" GMX_PRId64
175                       " for %s\n(called from file %s, line %d)",
176                       (gmx_int64_t)nelem, (gmx_int64_t)elsize,
177                       name, file, line);
178         }
179         memset(p, 0, (size_t) (nelem * elsize));
180 #else
181         if ((p = calloc((size_t)nelem, (size_t)elsize)) == NULL)
182         {
183             gmx_fatal(errno, __FILE__, __LINE__,
184                       "Not enough memory. Failed to calloc %" GMX_PRId64
185                       " elements of size %" GMX_PRId64
186                       " for %s\n(called from file %s, line %d)",
187                       (gmx_int64_t)nelem, (gmx_int64_t)elsize, name, file, line);
188         }
189 #endif
190     }
191 #ifdef DEBUG
192     log_action(1, name, file, line, nelem, elsize, p);
193 #endif
194     return p;
195 }
196
197 void *save_realloc(const char *name, const char *file, int line, void *ptr,
198                    size_t nelem, size_t elsize)
199 {
200     void  *p;
201     size_t size = nelem*elsize;
202
203     p = NULL;
204     if (size == 0)
205     {
206         save_free(name, file, line, ptr);
207     }
208     else
209     {
210 #ifdef PRINT_ALLOC_KB
211         if (size >= PRINT_ALLOC_KB*1024)
212         {
213             int rank = gmx_node_rank();
214             printf("Reallocating %.1f MB for %s (called from file %s, line %d on %d)\n",
215                    size/1048576.0, name, file, line, rank);
216         }
217 #endif
218         if (ptr == NULL)
219         {
220             p = malloc((size_t)size);
221         }
222         else
223         {
224             p = realloc(ptr, (size_t)size);
225         }
226         if (p == NULL)
227         {
228             gmx_fatal(errno, __FILE__, __LINE__,
229                       "Not enough memory. Failed to realloc %" GMX_PRId64 " bytes for %s, %s=%x\n"
230                       "(called from file %s, line %d)",
231                       (gmx_int64_t)size, name, name, ptr, file, line);
232         }
233 #ifdef DEBUG
234         log_action(1, name, file, line, 1, size, p);
235 #endif
236     }
237     return p;
238 }
239
240 void save_free(const char gmx_unused *name, const char gmx_unused *file, int gmx_unused line, void *ptr)
241 {
242 #ifdef DEBUG
243     log_action(0, name, file, line, 0, 0, ptr);
244 #endif
245     if (ptr != NULL)
246     {
247         free(ptr);
248     }
249 }
250
251 /* If we don't have useful routines for allocating aligned memory,
252  * then we have to use the old-style GROMACS approach bitwise-ANDing
253  * pointers to ensure alignment. We store the pointer to the originally
254  * allocated region in the space before the returned pointer */
255
256 /* we create a positive define for the absence of an system-provided memalign */
257 #if (!defined HAVE_POSIX_MEMALIGN && !defined HAVE_MEMALIGN && \
258      !defined HAVE__ALIGNED_MALLOC)
259 #define GMX_OWN_MEMALIGN
260 #endif
261
262
263 /* Pointers allocated with this routine should only be freed
264  * with save_free_aligned, however this will only matter
265  * on systems that lack posix_memalign() and memalign() when
266  * freeing memory that needed to be adjusted to achieve
267  * the necessary alignment. */
268 void *save_malloc_aligned(const char *name, const char *file, int line,
269                           size_t nelem, size_t elsize, size_t alignment)
270 {
271     void   **aligned  = NULL;
272     void    *malloced = NULL;
273     gmx_bool allocate_fail;
274
275     if (alignment == 0)
276     {
277         gmx_fatal(errno, __FILE__, __LINE__,
278                   "Cannot allocate aligned memory with alignment of zero!\n(called from file %s, line %d)", file, line);
279     }
280
281
282     if (nelem == 0 || elsize == 0)
283     {
284         aligned  = NULL;
285     }
286     else
287     {
288 #ifdef PRINT_ALLOC_KB
289         if (nelem*elsize >= PRINT_ALLOC_KB*1024)
290         {
291             int rank = gmx_node_rank();
292             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
293                    nelem*elsize/1048576.0, name, file, line, rank);
294         }
295 #endif
296
297 #ifdef HAVE_POSIX_MEMALIGN
298         allocate_fail = (0 != posix_memalign(&malloced, alignment, nelem*elsize));
299 #elif defined HAVE_MEMALIGN
300         allocate_fail = ((malloced = memalign(alignment, nelem*elsize)) == NULL);
301 #elif defined HAVE__ALIGNED_MALLOC
302         allocate_fail = ((malloced = _aligned_malloc(nelem*elsize, alignment))
303                          == NULL);
304 #else
305         allocate_fail = ((malloced = malloc(nelem*elsize+alignment+
306                                             sizeof(void*))) == NULL);
307 #endif
308         if (allocate_fail)
309         {
310             gmx_fatal(errno, __FILE__, __LINE__,
311                       "Not enough memory. Failed to allocate %u aligned elements of size %u for %s\n(called from file %s, line %d)", nelem, elsize, name, file, line);
312         }
313         /* we start with the original pointer */
314         aligned = (void**)malloced;
315
316 #ifdef GMX_OWN_MEMALIGN
317         /* Make the aligned pointer, and save the underlying pointer that
318          * we're allowed to free(). */
319
320         /* we first make space to store that underlying pointer: */
321         aligned = aligned + 1;
322         /* then we apply a bit mask */
323         aligned = (void *) (((size_t) aligned + alignment - 1) &
324                             (~((size_t) (alignment-1))));
325         /* and we store the original pointer in the area just before the
326            pointer we're going to return */
327         aligned[-1] = malloced;
328 #endif
329     }
330     return (void*)aligned;
331 }
332
333 void *save_calloc_aligned(const char *name, const char *file, int line,
334                           size_t nelem, size_t elsize, size_t alignment)
335 {
336     void *aligned = save_malloc_aligned(name, file, line, nelem, elsize, alignment);
337     if (aligned != NULL)
338     {
339         memset(aligned, 0, (size_t)(nelem * elsize));
340     }
341     return aligned;
342 }
343
344 /* This routine can NOT be called with any pointer */
345 void save_free_aligned(const char gmx_unused *name, const char gmx_unused *file, int gmx_unused line, void *ptr)
346 {
347     void *free = ptr;
348
349     if (NULL != ptr)
350     {
351 #ifdef GMX_OWN_MEMALIGN
352         /* we get the pointer from just before the memaligned pointer */
353         free = ((void**)ptr)[-1];
354 #endif
355
356 #ifndef HAVE__ALIGNED_MALLOC
357         /* (Now) we're allowed to use a normal free() on this pointer. */
358         save_free(name, file, line, free);
359 #else
360         _aligned_free(free);
361 #endif
362     }
363 }
364
365 void set_over_alloc_dd(gmx_bool set)
366 {
367     tMPI_Thread_mutex_lock(&g_over_alloc_mutex);
368     /* we just make sure that we don't set this at the same time.
369        We don't worry too much about reading this rarely-set variable */
370     g_bOverAllocDD = set;
371     tMPI_Thread_mutex_unlock(&g_over_alloc_mutex);
372 }
373
374 int over_alloc_dd(int n)
375 {
376     if (g_bOverAllocDD)
377     {
378         return static_cast<int>(OVER_ALLOC_FAC*n + 100);
379     }
380     else
381     {
382         return n;
383     }
384 }