error.h 6.66 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk),
4
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk).
Peter W. Draper's avatar
Peter W. Draper committed
5
 *               2015 Peter W. Draper (p.w.draper@durham.ac.uk)
6
 *
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
11
 *
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
17
18
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 ******************************************************************************/
21
22
#ifndef SWIFT_ERROR_H
#define SWIFT_ERROR_H
23

24
25
26
/* Config parameters. */
#include "../config.h"

27
/* Some standard headers. */
28
#include <stdio.h>
29
#include <stdlib.h>
30

31
32
33
34
35
/* MPI headers. */
#ifdef WITH_MPI
#include <mpi.h>
#endif

36
37
38
/* Local headers. */
#include "clocks.h"

39
#ifdef SWIFT_DEVELOP_MODE
40
#define swift_abort(errcode) abort()
41
#else
42
#define swift_abort(errcode) exit(errcode)
43
44
#endif

45
46
47
48
/**
 * @brief Error macro. Prints the message given in argument and aborts.
 *
 */
49
#ifdef WITH_MPI
50
extern int engine_rank;
51
#define error(s, ...)                                                      \
52
  ({                                                                       \
53
    fflush(stdout);                                                        \
54
55
56
    fprintf(stderr, "[%04i] %s %s:%s():%i: " s "\n", engine_rank,          \
            clocks_get_timesincestart(), __FILE__, __FUNCTION__, __LINE__, \
            ##__VA_ARGS__);                                                \
57
    swift_abort(1);                                                        \
58
  })
59
#else
60
#define error(s, ...)                                                      \
61
  ({                                                                       \
62
    fflush(stdout);                                                        \
63
64
    fprintf(stderr, "%s %s:%s():%i: " s "\n", clocks_get_timesincestart(), \
            __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);              \
65
    swift_abort(1);                                                        \
66
  })
67
68
#endif

Peter W. Draper's avatar
Peter W. Draper committed
69
70
71
72
73
74
#ifdef WITH_MPI
/**
 * @brief MPI error macro. Prints the message given in argument,
 *                         followed by the MPI error string and aborts.
 *
 */
75
#define mpi_error(res, s, ...)                                             \
76
  ({                                                                       \
77
    fflush(stdout);                                                        \
78
79
80
81
82
83
84
85
    fprintf(stderr, "[%04i] %s %s:%s():%i: " s "\n", engine_rank,          \
            clocks_get_timesincestart(), __FILE__, __FUNCTION__, __LINE__, \
            ##__VA_ARGS__);                                                \
    int len = 1024;                                                        \
    char buf[len];                                                         \
    MPI_Error_string(res, buf, &len);                                      \
    fprintf(stderr, "%s\n\n", buf);                                        \
    MPI_Abort(MPI_COMM_WORLD, -1);                                         \
86
  })
Peter W. Draper's avatar
Peter W. Draper committed
87

88
#define mpi_error_string(res, s, ...)                                      \
89
  ({                                                                       \
90
    fflush(stdout);                                                        \
91
92
93
94
95
96
97
    fprintf(stderr, "[%04i] %s %s:%s():%i: " s "\n", engine_rank,          \
            clocks_get_timesincestart(), __FILE__, __FUNCTION__, __LINE__, \
            ##__VA_ARGS__);                                                \
    int len = 1024;                                                        \
    char buf[len];                                                         \
    MPI_Error_string(res, buf, &len);                                      \
    fprintf(stderr, "%s\n\n", buf);                                        \
98
  })
Peter W. Draper's avatar
Peter W. Draper committed
99
100
#endif

101
102
103
104
105
/**
 * @brief Macro to print a localized message with variable arguments.
 *
 */
#ifdef WITH_MPI
106
extern int engine_rank;
107
108
109
110
111
#define message(s, ...)                                                       \
  ({                                                                          \
    printf("[%04i] %s %s: " s "\n", engine_rank, clocks_get_timesincestart(), \
           __FUNCTION__, ##__VA_ARGS__);                                      \
  })
112
#else
113
114
115
116
117
#define message(s, ...)                                                 \
  ({                                                                    \
    printf("%s %s: " s "\n", clocks_get_timesincestart(), __FUNCTION__, \
           ##__VA_ARGS__);                                              \
  })
118
#endif
119

120
121
122
123
124
125
126
/**
 * @brief Assertion macro compatible with MPI
 *
 */
#ifdef WITH_MPI
extern int engine_rank;
#define assert(expr)                                                          \
127
  ({                                                                          \
128
    if (!(expr)) {                                                            \
129
      fflush(stdout);                                                         \
130
      fprintf(stderr, "[%04i] %s %s:%s():%i: FAILED ASSERTION: " #expr " \n", \
131
132
              engine_rank, clocks_get_timesincestart(), __FILE__,             \
              __FUNCTION__, __LINE__);                                        \
133
      fflush(stderr);                                                         \
134
      MPI_Abort(MPI_COMM_WORLD, -1);                                          \
135
    }                                                                         \
136
  })
137
#else
138
#define assert(expr)                                                          \
139
  ({                                                                          \
140
    if (!(expr)) {                                                            \
141
      fflush(stdout);                                                         \
142
143
144
      fprintf(stderr, "%s %s:%s():%i: FAILED ASSERTION: " #expr " \n",        \
              clocks_get_timesincestart(), __FILE__, __FUNCTION__, __LINE__); \
      fflush(stderr);                                                         \
145
      swift_abort(1);                                                         \
146
    }                                                                         \
147
  })
148
149
#endif

150
#endif /* SWIFT_ERROR_H */