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.
46 #include "thread_mpi/threads.h"
49 #include "types/commrec.h"
51 #include "gmx_fatal.h"
57 #include "gromacs/fileio/futil.h"
58 #include "gromacs/fileio/gmxfio.h"
59 #include "gromacs/utility/gmxmpi.h"
61 static gmx_bool bDebug = FALSE;
62 static char *fatal_tmp_file = NULL;
63 static FILE *log_file = NULL;
65 static tMPI_Thread_mutex_t debug_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
66 static tMPI_Thread_mutex_t where_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
67 static tMPI_Thread_mutex_t fatal_tmp_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
70 gmx_bool bDebugMode(void)
74 tMPI_Thread_mutex_lock(&debug_mutex);
78 tMPI_Thread_mutex_unlock(&debug_mutex);
83 void gmx_fatal_set_log_file(FILE *fp)
88 void _where(const char *file, int line)
90 static gmx_bool bFirst = TRUE;
91 static int nskip = -1;
92 static int nwhere = 0;
98 tMPI_Thread_mutex_lock(&where_mutex);
99 if (bFirst) /* we repeat the check in the locked section because things
100 might have changed */
102 if ((temp = getenv("WHERE")) != NULL)
104 nskip = strtol(temp, NULL, 10);
108 tMPI_Thread_mutex_unlock(&where_mutex);
113 /* Skip the first n occasions, this allows to see where it goes wrong */
124 fprintf(fp, "WHERE %d, file %s - line %d\n", nwhere, file, line);
130 static int fatal_errno = 0;
132 static void quit_gmx(const char *msg)
134 tMPI_Thread_mutex_lock(&debug_mutex);
135 if (fatal_errno == 0)
139 fprintf(log_file, "%s\n", msg);
141 fprintf(stderr, "%s\n", msg);
142 /* we set it to no-zero because if this function is called, something
148 if (fatal_errno != -1)
156 if (gmx_mpi_initialized())
161 nnodes = gmx_node_num();
162 noderank = gmx_node_rank();
166 fprintf(stderr, "Error on node %d, will try to stop all the nodes\n",
169 gmx_abort(noderank, nnodes, -1);
179 fprintf(stderr, "dump core (y/n):");
181 if (toupper(getc(stdin)) != 'N')
188 tMPI_Thread_mutex_unlock(&debug_mutex);
191 /* The function below should be identical to quit_gmx,
192 * except that is does not actually quit and call gmx_abort.
194 static void quit_gmx_noquit(const char *msg)
196 tMPI_Thread_mutex_lock(&debug_mutex);
201 fprintf(log_file, "%s\n", msg);
203 fprintf(stderr, "%s\n", msg);
204 /* we set it to no-zero because if this function is called, something
210 if (fatal_errno != -1)
224 fprintf(stderr, "dump core (y/n):");
226 if (toupper(getc(stdin)) != 'N')
233 tMPI_Thread_mutex_unlock(&debug_mutex);
236 void _set_fatal_tmp_file(const char *fn, const char *file, int line)
238 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
239 if (fatal_tmp_file == NULL)
241 fatal_tmp_file = strdup(fn);
245 fprintf(stderr, "BUGWARNING: fatal_tmp_file already set at %s:%d",
248 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
251 void _unset_fatal_tmp_file(const char *fn, const char *file, int line)
253 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
254 if (strcmp(fn, fatal_tmp_file) == 0)
256 sfree(fatal_tmp_file);
257 fatal_tmp_file = NULL;
261 fprintf(stderr, "BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
264 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
267 static void clean_fatal_tmp_file()
269 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
272 fprintf(stderr, "Cleaning up temporary file %s\n", fatal_tmp_file);
273 remove(fatal_tmp_file);
274 sfree(fatal_tmp_file);
275 fatal_tmp_file = NULL;
277 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
280 void gmx_fatal(int f_errno, const char *file, int line, const char *fmt, ...)
285 clean_fatal_tmp_file();
288 vsprintf(msg, fmt, ap);
291 tMPI_Thread_mutex_lock(&debug_mutex);
292 fatal_errno = f_errno;
293 tMPI_Thread_mutex_unlock(&debug_mutex);
295 _gmx_error("fatal", msg, file, line);
298 void gmx_fatal_collective(int f_errno, const char *file, int line,
299 const t_commrec *cr, gmx_domdec_t *dd,
300 const char *fmt, ...)
312 /* Check if we are calling on all processes in MPI_COMM_WORLD */
315 MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
319 MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
321 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
322 bFinalize = (result != MPI_UNEQUAL);
325 if ((cr != NULL && MASTER(cr) ) ||
326 (dd != NULL && DDMASTER(dd)))
328 clean_fatal_tmp_file();
331 vsprintf(msg, fmt, ap);
334 tMPI_Thread_mutex_lock(&debug_mutex);
335 fatal_errno = f_errno;
336 tMPI_Thread_mutex_unlock(&debug_mutex);
340 /* Use an error handler that does not quit */
341 set_gmx_error_handler(quit_gmx_noquit);
344 _gmx_error("fatal", msg, file, line);
350 /* Broadcast the fatal error number possibly modified
351 * on the master process, in case the user would like
352 * to use the return status on a non-master process.
353 * The master process in cr and dd always has global rank 0.
355 MPI_Bcast(&fatal_errno, sizeof(fatal_errno), MPI_BYTE,
358 /* Finalize nicely instead of aborting */
363 /* Let all other processes wait till the master has printed
364 * the error message and issued MPI_Abort.
366 MPI_Barrier(MPI_COMM_WORLD);
373 void _invalid_case(const char *fn, int line)
375 gmx_fatal(FARGS, "Invalid case in switch statement, file %s, line %d",
379 void _unexpected_eof(const char *fn, int line, const char *srcfn, int srcline)
381 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d\n"
382 "(Source file %s, line %d)", fn, line, srcfn, srcline);
386 * These files are global variables in the gromacs preprocessor
387 * Every routine in a file that includes gmx_fatal.h can write to these
388 * debug channels. Depending on the debuglevel used
389 * 0 to 3 of these filed are redirected to /dev/null
393 gmx_bool gmx_debug_at = FALSE;
395 void init_debug(const int dbglevel, const char *dbgfile)
397 tMPI_Thread_mutex_lock(&debug_mutex);
398 if (!bDebug) /* another thread hasn't already run this*/
401 debug = gmx_fio_fopen(dbgfile, "w+");
408 tMPI_Thread_mutex_unlock(&debug_mutex);
411 #if (defined __sgi && defined USE_SGI_FPE)
412 static void user_routine(unsigned us[5], int ii[2])
414 fprintf(stderr, "User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
415 us[0], us[1], us[2], us[3], us[4], ii[0], ii[1]);
416 fprintf(stderr, "Exception encountered! Dumping core\n");
420 static void abort_routine(unsigned int **ii)
422 fprintf(stderr, "Abort routine\n");
426 static void handle_signals(int n)
428 fprintf(stderr, "Handle signals: n = %d\n", n);
429 fprintf(stderr, "Dumping core\n");
433 void doexceptions(void)
437 int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS };
439 int onoff, en_mask, abort_action, i;
441 tMPI_Thread_mutex_lock(&debug_mutex);
443 en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO |
444 _EN_INVALID | _EN_INT_OVERFL;
445 abort_action = _ABORT_ON_ERROR;
446 handle_sigfpes(onoff, en_mask, user_routine, abort_action, abort_routine);
448 for (i = 0; (i < asize(hs)); i++)
450 signal(hs[i], handle_signals);
452 tMPI_Thread_mutex_unlock(&debug_mutex);
454 #endif /* __sgi and FPE */
456 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
458 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
460 void set_gmx_error_handler(void (*func)(const char *msg))
462 tMPI_Thread_mutex_lock(&debug_mutex);
463 gmx_error_handler = func;
464 tMPI_Thread_mutex_unlock(&debug_mutex);
467 char *gmx_strerror(const char *key)
470 const char *key, *msg;
472 error_msg_t msg[] = {
473 { "bug", "Possible bug" },
474 { "call", "Routine should not have been called" },
475 { "comm", "Communication (parallel processing) problem" },
476 { "fatal", "Fatal error" },
477 { "cmd", "Invalid command line argument" },
478 { "file", "File input/output error" },
479 { "impl", "Implementation restriction" },
480 { "incons", "Software inconsistency error" },
481 { "input", "Input error or input inconsistency" },
482 { "mem", "Memory allocation/freeing error" },
483 { "open", "Can not open file" },
484 { "range", "Range checking error" }
486 #define NMSG asize(msg)
492 return strdup("Empty message");
496 for (i = 0; (i < NMSG); i++)
498 if (strcmp(key, msg[i].key) == 0)
505 sprintf(buf, "No error message associated with key %s\n%s", key, gmxuser);
510 return strdup(msg[i].msg);
516 void _gmx_error(const char *key, const char *msg, const char *file, int line)
518 char buf[10240], errerrbuf[1024];
519 const char *llines = "-------------------------------------------------------";
522 /* protect the audience from suggestive discussions */
526 sprintf(errerrbuf, "Empty fatal_error message. %s", gmxuser);
529 strerr = gmx_strerror(key);
530 sprintf(buf, "\n%s\nProgram %s, %s\n"
531 "Source code file: %s, line: %d\n\n"
532 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
533 "website at http://www.gromacs.org/Documentation/Errors\n%s\n",
534 llines, ShortProgram(), GromacsVersion(), file, line,
535 strerr, msg ? msg : errerrbuf, llines);
538 gmx_error_handler(buf);
541 void _range_check(int n, int n_min, int n_max, const char *warn_str,
542 const char *var, const char *file, int line)
546 if ((n < n_min) || (n >= n_max))
548 if (warn_str != NULL)
550 strcpy(buf, warn_str);
558 sprintf(buf+strlen(buf), "Variable %s has value %d. It should have been "
559 "within [ %d .. %d ]\n", var, n, n_min, n_max);
561 _gmx_error("range", buf, file, line);
565 void gmx_warning(const char *fmt, ...)
571 vsprintf(msg, fmt, ap);
574 fprintf(stderr, "\nWARNING: %s\n\n", msg);