minor fixes in ditribution files
[gromacs/qmmm-gamess-us.git] / src / gmxlib / thread_mpi / errhandler.c
blob3e97bb7fd989779bda0bbfa8100316b4852176ed
1 /*
2 This source code file is part of thread_mpi.
3 Written by Sander Pronk, Erik Lindahl, and possibly others.
5 Copyright (c) 2009, Sander Pronk, Erik Lindahl.
6 All rights reserved.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 1) Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3) Neither the name of the copyright holders nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 If you want to redistribute modifications, please consider that
31 scientific software is very special. Version control is crucial -
32 bugs must be traceable. We will be happy to consider code for
33 inclusion in the official distribution, but derived work should not
34 be called official thread_mpi. Details are found in the README & COPYING
35 files.
38 #ifdef HAVE_TMPI_CONFIG_H
39 #include "tmpi_config.h"
40 #endif
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54 #if ! (defined( _WIN32 ) || defined( _WIN64 ) )
55 #include <sys/time.h>
56 #endif
58 #include "impl.h"
65 struct tmpi_errhandler_ tmpi_errors_are_fatal = { 0, tmpi_errors_are_fatal_fn };
66 struct tmpi_errhandler_ tmpi_errors_return = { 0, tmpi_errors_return_fn };
69 tMPI_Errhandler TMPI_ERRORS_ARE_FATAL=&tmpi_errors_are_fatal;
70 tMPI_Errhandler TMPI_ERRORS_RETURN=&tmpi_errors_return;
75 /* error messages. Must match error codes in thread_mpi.h */
76 static const char *tmpi_errmsg[] =
78 "No error",
79 "malloc failure in tMPI (out of memory)",
80 "tMPI Initialization error",
81 "tMPI Finalize error",
82 "Invalid tMPI_Group",
83 "Invalid tMPI_Comm",
84 "Invalid tMPI_Status",
85 "Invalid tMPI_Group rank",
86 "Invalid Cartesian topology dimensions",
87 "Invalid Cartesian topology coordinates",
88 "Insufficient number processes for Cartesian topology",
89 "Invalid counterpart for MPI transfer",
90 "Receive buffer size too small for transmission",
91 "Overlapping send/receive buffers: probably due to thread-unsafe code.",
92 "Invalid send destination",
93 "Invalid receive source",
94 "Invalid buffer (null pointer in send or receive buffer)",
95 "Multicast operation mismatch (multicast not collective across comm)",
96 "Invalid reduce operator",
97 "Out of receive envelopes: this shouldn't happen (probably a bug).",
98 "Out of receive requests: this shouldn't happen (probably a bug).",
99 "Transmission failure",
100 "Unknown tMPI error"
105 int tMPI_Error(tMPI_Comm comm, int tmpi_errno)
107 if (comm)
109 comm->erh->err=tmpi_errno;
110 comm->erh->fn(&comm, &tmpi_errno);
112 else
114 /* initialization errors have no comm */
115 tmpi_errors_are_fatal_fn(NULL, &tmpi_errno);
117 return tmpi_errno;
121 int tMPI_Error_string(int errorcode, char *strn, size_t *resultlen)
123 if (errorcode<0 || errorcode>=N_TMPI_ERR)
124 errorcode=TMPI_ERR_UNKNOWN;
126 #if ! (defined( _WIN32 ) || defined( _WIN64 ) )
127 strncpy(strn, tmpi_errmsg[errorcode], TMPI_MAX_ERROR_STRING);
128 #else
129 strncpy_s(strn, TMPI_MAX_ERROR_STRING, tmpi_errmsg[errorcode], TMPI_MAX_ERROR_STRING);
130 #endif
131 *resultlen=strlen(strn);
132 return TMPI_SUCCESS;
135 int tMPI_Create_errhandler(tMPI_Errhandler_fn *function,
136 tMPI_Errhandler *errhandler)
138 #ifdef TMPI_TRACE
139 tMPI_Trace_print("tMPI_Create_errhandler(%p, %p)", function, errhandler);
140 #endif
142 /* we don't use a special malloc here because this is the error handler
143 creation function. */
144 *errhandler=(tMPI_Errhandler)malloc(sizeof(struct tmpi_errhandler_));
145 if (!*errhandler)
147 fprintf(stderr, "tMPI fatal error (%s), bailing out\n",
148 tmpi_errmsg[TMPI_ERR_MALLOC]);
149 abort();
151 (*errhandler)->err=0;
152 (*errhandler)->fn=*function;
153 return TMPI_SUCCESS;
156 int tMPI_Errhandler_free(tMPI_Errhandler *errhandler)
158 #ifdef TMPI_TRACE
159 tMPI_Trace_print("tMPI_Errhandler_free(%p)", errhandler);
160 #endif
162 free(*errhandler);
163 return TMPI_SUCCESS;
167 int tMPI_Comm_set_errhandler(tMPI_Comm comm, tMPI_Errhandler errhandler)
169 #ifdef TMPI_TRACE
170 tMPI_Trace_print("tMPI_Comm_set_errhandler(%p, %p)", comm, errhandler);
171 #endif
173 comm->erh = errhandler;
174 return TMPI_SUCCESS;
177 int tMPI_Comm_get_errhandler(tMPI_Comm comm, tMPI_Errhandler *errhandler)
179 #ifdef TMPI_TRACE
180 tMPI_Trace_print("tMPI_Comm_get_errhandler(%p, %p)", comm, errhandler);
181 #endif
183 *errhandler=comm->erh;
184 return TMPI_SUCCESS;
187 void tmpi_errors_are_fatal_fn(tMPI_Comm *comm, int *err)
189 char errstr[TMPI_MAX_ERROR_STRING];
190 size_t len;
192 tMPI_Error_string(*err, errstr, &len);
193 if (comm)
195 fprintf(stderr, "tMPI error: %s (in valid comm)\n", errstr);
197 else
199 fprintf(stderr, "tMPI error: %s\n", errstr);
201 abort();
202 /*exit(0);*/
205 void tmpi_errors_return_fn(tMPI_Comm *comm, int *err)
207 char errstr[TMPI_MAX_ERROR_STRING];
208 size_t len;
210 tMPI_Error_string(*err, errstr, &len);
211 if (comm)
213 fprintf(stderr, "tMPI error: %s (in valid comm)\n", errstr);
215 else
217 fprintf(stderr, "tMPI error: %s\n", errstr);
219 return;