2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, 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.
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.
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.
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.
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.
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.
37 #include "fatalerror.h"
38 #include "gromacs/legacyheaders/gmx_fatal_collective.h"
52 #include "thread_mpi/threads.h"
54 #include "gromacs/legacyheaders/types/commrec.h"
56 #include "gromacs/fileio/futil.h"
57 #include "gromacs/fileio/gmxfio.h"
58 #include "gromacs/utility/basenetwork.h"
59 #include "gromacs/utility/baseversion.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/utility/gmxmpi.h"
62 #include "gromacs/utility/programcontext.h"
63 #include "gromacs/utility/smalloc.h"
65 static gmx_bool bDebug = FALSE;
66 static FILE *log_file = NULL;
68 static tMPI_Thread_mutex_t debug_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
69 static tMPI_Thread_mutex_t where_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
71 gmx_bool bDebugMode(void)
76 void gmx_fatal_set_log_file(FILE *fp)
81 void _where(const char *file, int line)
83 static gmx_bool bFirst = TRUE;
84 static int nskip = -1;
85 static int nwhere = 0;
91 tMPI_Thread_mutex_lock(&where_mutex);
92 if (bFirst) /* we repeat the check in the locked section because things
95 if ((temp = getenv("WHERE")) != NULL)
97 nskip = strtol(temp, NULL, 10);
101 tMPI_Thread_mutex_unlock(&where_mutex);
106 /* Skip the first n occasions, this allows to see where it goes wrong */
117 fprintf(fp, "WHERE %d, file %s - line %d\n", nwhere, file, line);
123 static int fatal_errno = 0;
125 static void quit_gmx(const char *msg)
127 tMPI_Thread_mutex_lock(&debug_mutex);
128 if (fatal_errno == 0)
132 fprintf(log_file, "%s\n", msg);
134 fprintf(stderr, "%s\n", msg);
135 /* we set it to no-zero because if this function is called, something
141 if (fatal_errno != -1)
149 if (gmx_mpi_initialized())
154 nnodes = gmx_node_num();
155 noderank = gmx_node_rank();
159 fprintf(stderr, "Error on node %d, will try to stop all the nodes\n",
162 gmx_abort(noderank, nnodes, -1);
172 fprintf(stderr, "dump core (y/n):");
174 if (toupper(getc(stdin)) != 'N')
181 tMPI_Thread_mutex_unlock(&debug_mutex);
184 /* The function below should be identical to quit_gmx,
185 * except that is does not actually quit and call gmx_abort.
187 static void quit_gmx_noquit(const char *msg)
189 tMPI_Thread_mutex_lock(&debug_mutex);
194 fprintf(log_file, "%s\n", msg);
196 fprintf(stderr, "%s\n", msg);
197 /* we set it to no-zero because if this function is called, something
203 if (fatal_errno != -1)
217 fprintf(stderr, "dump core (y/n):");
219 if (toupper(getc(stdin)) != 'N')
226 tMPI_Thread_mutex_unlock(&debug_mutex);
229 void gmx_fatal(int f_errno, const char *file, int line, const char *fmt, ...)
235 vsprintf(msg, fmt, ap);
238 tMPI_Thread_mutex_lock(&debug_mutex);
239 fatal_errno = f_errno;
240 tMPI_Thread_mutex_unlock(&debug_mutex);
242 _gmx_error("fatal", msg, file, line);
245 void gmx_fatal_collective(int f_errno, const char *file, int line,
246 const t_commrec *cr, gmx_domdec_t *dd,
247 const char *fmt, ...)
256 /* Check if we are calling on all processes in MPI_COMM_WORLD */
259 MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
263 MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
265 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
266 const bool bFinalize = (result != MPI_UNEQUAL);
268 const bool bFinalize = true;
271 if ((cr != NULL && MASTER(cr) ) ||
272 (dd != NULL && DDMASTER(dd)))
275 vsprintf(msg, fmt, ap);
278 tMPI_Thread_mutex_lock(&debug_mutex);
279 fatal_errno = f_errno;
280 tMPI_Thread_mutex_unlock(&debug_mutex);
284 /* Use an error handler that does not quit */
285 set_gmx_error_handler(quit_gmx_noquit);
288 _gmx_error("fatal", msg, file, line);
294 /* Broadcast the fatal error number possibly modified
295 * on the master process, in case the user would like
296 * to use the return status on a non-master process.
297 * The master process in cr and dd always has global rank 0.
299 MPI_Bcast(&fatal_errno, sizeof(fatal_errno), MPI_BYTE,
302 /* Finalize nicely instead of aborting */
307 /* Let all other processes wait till the master has printed
308 * the error message and issued MPI_Abort.
310 MPI_Barrier(MPI_COMM_WORLD);
318 * These files are global variables in the gromacs preprocessor
319 * Every routine in a file that includes gmx_fatal.h can write to these
320 * debug channels. Depending on the debuglevel used
321 * 0 to 3 of these filed are redirected to /dev/null
325 gmx_bool gmx_debug_at = FALSE;
327 void init_debug(const int dbglevel, const char *dbgfile)
329 tMPI_Thread_mutex_lock(&debug_mutex);
330 if (!bDebug) /* another thread hasn't already run this*/
333 debug = gmx_fio_fopen(dbgfile, "w+");
340 tMPI_Thread_mutex_unlock(&debug_mutex);
343 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
345 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
347 void set_gmx_error_handler(void (*func)(const char *msg))
349 tMPI_Thread_mutex_lock(&debug_mutex);
350 gmx_error_handler = func;
351 tMPI_Thread_mutex_unlock(&debug_mutex);
354 char *gmx_strerror(const char *key)
357 const char *key, *msg;
359 error_msg_t msg[] = {
360 { "bug", "Possible bug" },
361 { "call", "Routine should not have been called" },
362 { "comm", "Communication (parallel processing) problem" },
363 { "fatal", "Fatal error" },
364 { "cmd", "Invalid command line argument" },
365 { "file", "File input/output error" },
366 { "impl", "Implementation restriction" },
367 { "incons", "Software inconsistency error" },
368 { "input", "Input error or input inconsistency" },
369 { "mem", "Memory allocation/freeing error" },
370 { "open", "Can not open file" },
371 { "range", "Range checking error" },
377 return strdup("Empty message");
381 for (size_t i = 0; msg[i].key != NULL; ++i)
383 if (strcmp(key, msg[i].key) == 0)
385 return strdup(msg[i].msg);
389 sprintf(buf, "No error message associated with key %s\n%s", key, gmxuser);
395 void _gmx_error(const char *key, const char *msg, const char *file, int line)
397 char buf[10240], errerrbuf[1024];
398 const char *llines = "-------------------------------------------------------";
401 /* protect the audience from suggestive discussions */
405 sprintf(errerrbuf, "Empty fatal_error message. %s", gmxuser);
407 // In case ProgramInfo is not initialized and there is an issue with the
408 // initialization, fall back to "GROMACS".
409 const char *programName = "GROMACS";
412 programName = gmx::getProgramContext().displayName();
414 catch (const std::exception &)
418 strerr = gmx_strerror(key);
419 sprintf(buf, "\n%s\nProgram %s, %s\n"
420 "Source code file: %s, line: %d\n\n"
421 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
422 "website at http://www.gromacs.org/Documentation/Errors\n%s\n",
423 llines, programName, gmx_version(), file, line,
424 strerr, msg ? msg : errerrbuf, llines);
427 gmx_error_handler(buf);
430 void _range_check(int n, int n_min, int n_max, const char *warn_str,
431 const char *var, const char *file, int line)
435 if ((n < n_min) || (n >= n_max))
437 if (warn_str != NULL)
439 strcpy(buf, warn_str);
447 sprintf(buf+strlen(buf), "Variable %s has value %d. It should have been "
448 "within [ %d .. %d ]\n", var, n, n_min, n_max);
450 _gmx_error("range", buf, file, line);
454 void gmx_warning(const char *fmt, ...)
460 vsprintf(msg, fmt, ap);
463 fprintf(stderr, "\nWARNING: %s\n\n", msg);