xref: /petsc/src/sys/error/err.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay       Code that allows one to set the error handlers
4e5c89e4eSSatish Balay */
5af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/
6665c2dedSJed Brown #include <petscviewer.h>
7e5c89e4eSSatish Balay 
8e5c89e4eSSatish Balay typedef struct _EH *EH;
9e5c89e4eSSatish Balay struct _EH {
10efca3c55SSatish Balay   PetscErrorCode (*handler)(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *);
11e5c89e4eSSatish Balay   void *ctx;
12e5c89e4eSSatish Balay   EH    previous;
13e5c89e4eSSatish Balay };
14e5c89e4eSSatish Balay 
1502c9f0b5SLisandro Dalcin static EH eh = NULL;
16e5c89e4eSSatish Balay 
17e5c89e4eSSatish Balay /*@C
18e5c89e4eSSatish Balay    PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to
19a5b23f4aSJose E. Roman     load the file where the error occurred. Then calls the "previous" error handler.
20e5c89e4eSSatish Balay 
21e5c89e4eSSatish Balay    Not Collective
22e5c89e4eSSatish Balay 
23e5c89e4eSSatish Balay    Input Parameters:
24a5b23f4aSJose E. Roman +  comm - communicator over which error occurred
25e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
26e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
27e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
28e5c89e4eSSatish Balay .  n - the generic error number
29e5c89e4eSSatish Balay .  p - specific error number
30e5c89e4eSSatish Balay -  ctx - error handler context
31e5c89e4eSSatish Balay 
32e5c89e4eSSatish Balay    Options Database Key:
3310699b91SBarry Smith .   -on_error_emacs <machinename> - will contact machinename to open the Emacs client there
34e5c89e4eSSatish Balay 
35e5c89e4eSSatish Balay    Level: developer
36e5c89e4eSSatish Balay 
37e5c89e4eSSatish Balay    Notes:
38e5c89e4eSSatish Balay    You must put (server-start) in your .emacs file for the emacsclient software to work
39e5c89e4eSSatish Balay 
40cc12936aSPatrick Sanan    Developer Note:
419566063dSJacob Faibussowitsch    Since this is an error handler it cannot call PetscCall(); thus we just return if an error is detected.
423bf036e2SBarry Smith 
43db781477SPatrick Sanan .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
44db781477SPatrick Sanan           `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscReturnErrorHandler()`
45e5c89e4eSSatish Balay  @*/
469371c9d4SSatish Balay PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx) {
47e5c89e4eSSatish Balay   PetscErrorCode ierr;
48e5c89e4eSSatish Balay   char           command[PETSC_MAX_PATH_LEN];
49e5c89e4eSSatish Balay   const char    *pdir;
50e5c89e4eSSatish Balay   FILE          *fp;
51e5c89e4eSSatish Balay 
52e5c89e4eSSatish Balay   PetscFunctionBegin;
539371c9d4SSatish Balay   ierr = PetscGetPetscDir(&pdir);
549371c9d4SSatish Balay   if (ierr) PetscFunctionReturn(ierr);
55efca3c55SSatish Balay   sprintf(command, "cd %s; emacsclient --no-wait +%d %s\n", pdir, line, file);
56e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN)
579371c9d4SSatish Balay   ierr = PetscPOpen(MPI_COMM_WORLD, (char *)ctx, command, "r", &fp);
589371c9d4SSatish Balay   if (ierr) PetscFunctionReturn(ierr);
599371c9d4SSatish Balay   ierr = PetscPClose(MPI_COMM_WORLD, fp);
609371c9d4SSatish Balay   if (ierr) PetscFunctionReturn(ierr);
61e5c89e4eSSatish Balay #else
62e32f2f54SBarry Smith   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
63e5c89e4eSSatish Balay #endif
649371c9d4SSatish Balay   ierr = PetscPopErrorHandler();
659371c9d4SSatish Balay   if (ierr) PetscFunctionReturn(ierr); /* remove this handler from the stack of handlers */
663bf036e2SBarry Smith   if (!eh) {
679371c9d4SSatish Balay     ierr = PetscTraceBackErrorHandler(comm, line, fun, file, n, p, mess, NULL);
689371c9d4SSatish Balay     if (ierr) PetscFunctionReturn(ierr);
693bf036e2SBarry Smith   } else {
709371c9d4SSatish Balay     ierr = (*eh->handler)(comm, line, fun, file, n, p, mess, eh->ctx);
719371c9d4SSatish Balay     if (ierr) PetscFunctionReturn(ierr);
723bf036e2SBarry Smith   }
73e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
74e5c89e4eSSatish Balay }
75e5c89e4eSSatish Balay 
76e5c89e4eSSatish Balay /*@C
77e5c89e4eSSatish Balay    PetscPushErrorHandler - Sets a routine to be called on detection of errors.
78e5c89e4eSSatish Balay 
79e5c89e4eSSatish Balay    Not Collective
80e5c89e4eSSatish Balay 
81e5c89e4eSSatish Balay    Input Parameters:
82e5c89e4eSSatish Balay +  handler - error handler routine
83e5c89e4eSSatish Balay -  ctx - optional handler context that contains information needed by the handler (for
84e5c89e4eSSatish Balay          example file pointers for error messages etc.)
85e5c89e4eSSatish Balay 
86e5c89e4eSSatish Balay    Calling sequence of handler:
87efca3c55SSatish Balay $    int handler(MPI_Comm comm,int line,char *func,char *file,PetscErrorCode n,int p,char *mess,void *ctx);
88e5c89e4eSSatish Balay 
89a5b23f4aSJose E. Roman +  comm - communicator over which error occurred
90e5c89e4eSSatish Balay .  line - the line number of the error (indicated by __LINE__)
91e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
92e5c89e4eSSatish Balay .  n - the generic error number (see list defined in include/petscerror.h)
93668f157eSBarry Smith .  p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT
94e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
95e5c89e4eSSatish Balay -  ctx - the error handler context
96e5c89e4eSSatish Balay 
97e5c89e4eSSatish Balay    Options Database Keys:
9810699b91SBarry Smith +   -on_error_attach_debugger <noxterm,gdb or dbx> - starts up the debugger if an error occurs
9910699b91SBarry Smith -   -on_error_abort - aborts the program if an error occurs
100e5c89e4eSSatish Balay 
101e5c89e4eSSatish Balay    Level: intermediate
102e5c89e4eSSatish Balay 
103e93bc3c1Svictor    Notes:
1047850c7c0SBarry Smith    The currently available PETSc error handlers include PetscTraceBackErrorHandler(),
105e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler(), PetscReturnErrorHandler().
106e93bc3c1Svictor 
10795452b02SPatrick Sanan    Fortran Notes:
10895452b02SPatrick Sanan     You can only push one error handler from Fortran before poping it.
1097850c7c0SBarry Smith 
110db781477SPatrick Sanan .seealso: `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscPushSignalHandler()`
111e5c89e4eSSatish Balay 
112e5c89e4eSSatish Balay @*/
1139371c9d4SSatish Balay PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *), void *ctx) {
114e5c89e4eSSatish Balay   EH neweh;
115e5c89e4eSSatish Balay 
116e5c89e4eSSatish Balay   PetscFunctionBegin;
1179566063dSJacob Faibussowitsch   PetscCall(PetscNew(&neweh));
118a297a907SKarl Rupp   if (eh) neweh->previous = eh;
11902c9f0b5SLisandro Dalcin   else neweh->previous = NULL;
120e5c89e4eSSatish Balay   neweh->handler = handler;
121e5c89e4eSSatish Balay   neweh->ctx     = ctx;
122e5c89e4eSSatish Balay   eh             = neweh;
123e5c89e4eSSatish Balay   PetscFunctionReturn(0);
124e5c89e4eSSatish Balay }
125e5c89e4eSSatish Balay 
126e30d2299SSatish Balay /*@
127e5c89e4eSSatish Balay    PetscPopErrorHandler - Removes the latest error handler that was
128e5c89e4eSSatish Balay    pushed with PetscPushErrorHandler().
129e5c89e4eSSatish Balay 
130e5c89e4eSSatish Balay    Not Collective
131e5c89e4eSSatish Balay 
132e5c89e4eSSatish Balay    Level: intermediate
133e5c89e4eSSatish Balay 
134db781477SPatrick Sanan .seealso: `PetscPushErrorHandler()`
135e5c89e4eSSatish Balay @*/
1369371c9d4SSatish Balay PetscErrorCode PetscPopErrorHandler(void) {
137e5c89e4eSSatish Balay   EH tmp;
138e5c89e4eSSatish Balay 
139e5c89e4eSSatish Balay   PetscFunctionBegin;
140e5c89e4eSSatish Balay   if (!eh) PetscFunctionReturn(0);
141e5c89e4eSSatish Balay   tmp = eh;
142e5c89e4eSSatish Balay   eh  = eh->previous;
1439566063dSJacob Faibussowitsch   PetscCall(PetscFree(tmp));
144e5c89e4eSSatish Balay   PetscFunctionReturn(0);
145e5c89e4eSSatish Balay }
146e5c89e4eSSatish Balay 
147e93bc3c1Svictor /*@C
14845b666d6SBarry Smith   PetscReturnErrorHandler - Error handler that causes a return without printing an error message.
149e93bc3c1Svictor 
150e93bc3c1Svictor    Not Collective
151e93bc3c1Svictor 
152e93bc3c1Svictor    Input Parameters:
153e32f2f54SBarry Smith +  comm - communicator over which error occurred
154e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
155e93bc3c1Svictor .  file - the file in which the error was detected (indicated by __FILE__)
156e93bc3c1Svictor .  mess - an error text string, usually just printed to the screen
157e93bc3c1Svictor .  n - the generic error number
158e93bc3c1Svictor .  p - specific error number
159e93bc3c1Svictor -  ctx - error handler context
160e93bc3c1Svictor 
161e93bc3c1Svictor    Level: developer
162e93bc3c1Svictor 
163e93bc3c1Svictor    Notes:
164e93bc3c1Svictor    Most users need not directly employ this routine and the other error
165e93bc3c1Svictor    handlers, but can instead use the simplified interface SETERRQ, which has
166e93bc3c1Svictor    the calling sequence
167e32f2f54SBarry Smith $     SETERRQ(comm,number,mess)
168e93bc3c1Svictor 
16945b666d6SBarry Smith    PetscIgnoreErrorHandler() does the same thing as this function, but is deprecated, you should use this function.
170e93bc3c1Svictor 
17145b666d6SBarry Smith    Use PetscPushErrorHandler() to set the desired error handler.
172e93bc3c1Svictor 
173db781477SPatrick Sanan .seealso: `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscError()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`,
174db781477SPatrick Sanan           `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()`
175e93bc3c1Svictor  @*/
1769371c9d4SSatish Balay PetscErrorCode PetscReturnErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx) {
177362febeeSStefano Zampini   return n;
178e93bc3c1Svictor }
179e93bc3c1Svictor 
180e5c89e4eSSatish Balay static char        PetscErrorBaseMessage[1024];
181e5c89e4eSSatish Balay /*
182e5c89e4eSSatish Balay        The numerical values for these are defined in include/petscerror.h; any changes
183e5c89e4eSSatish Balay    there must also be made here
184e5c89e4eSSatish Balay */
185e5c89e4eSSatish Balay static const char *PetscErrorStrings[] = {
186e5c89e4eSSatish Balay   /*55 */ "Out of memory",
187e5c89e4eSSatish Balay   "No support for this operation for this object type",
188e5c89e4eSSatish Balay   "No support for this operation on this system",
189e5c89e4eSSatish Balay   /*58 */ "Operation done in wrong order",
190e5c89e4eSSatish Balay   /*59 */ "Signal received",
191e5c89e4eSSatish Balay   /*60 */ "Nonconforming object sizes",
192e5c89e4eSSatish Balay   "Argument aliasing not permitted",
193e5c89e4eSSatish Balay   "Invalid argument",
194e5c89e4eSSatish Balay   /*63 */ "Argument out of range",
195a17b96a8SKyle Gerard Felker   "Corrupt argument: https://petsc.org/release/faq/#valgrind",
196e5c89e4eSSatish Balay   "Unable to open file",
197e5c89e4eSSatish Balay   "Read from file failed",
198e5c89e4eSSatish Balay   "Write to file failed",
199e5c89e4eSSatish Balay   "Invalid pointer",
200e5c89e4eSSatish Balay   /*69 */ "Arguments must have same type",
201a8b45ee7SBarry Smith   /*70 */ "Attempt to use a pointer that does not point to a valid accessible location",
202a17b96a8SKyle Gerard Felker   /*71 */ "Zero pivot in LU factorization: https://petsc.org/release/faq/#zeropivot",
203e5c89e4eSSatish Balay   /*72 */ "Floating point exception",
204e5c89e4eSSatish Balay   /*73 */ "Object is in wrong state",
205e5c89e4eSSatish Balay   "Corrupted Petsc object",
206e5c89e4eSSatish Balay   "Arguments are incompatible",
207e5c89e4eSSatish Balay   "Error in external library",
208e5c89e4eSSatish Balay   /*77 */ "Petsc has generated inconsistent data",
209a17b96a8SKyle Gerard Felker   "Memory corruption: https://petsc.org/release/faq/#valgrind",
210e5c89e4eSSatish Balay   "Unexpected data in file",
211e5c89e4eSSatish Balay   /*80 */ "Arguments must have same communicators",
212a17b96a8SKyle Gerard Felker   /*81 */ "Zero pivot in Cholesky factorization: https://petsc.org/release/faq/#zeropivot",
213e5c89e4eSSatish Balay   "",
214e5c89e4eSSatish Balay   "",
215a17b96a8SKyle Gerard Felker   "Overflow in integer operation: https://petsc.org/release/faq/#64-bit-indices",
216e5c89e4eSSatish Balay   /*85 */ "Null argument, when expecting valid pointer",
217a17b96a8SKyle Gerard Felker   /*86 */ "Unknown type. Check for miss-spelling or missing package: https://petsc.org/release/install/install/#external-packages",
2183d96e996SBarry Smith   /*87 */ "MPI library at runtime is not compatible with MPI used at compile time",
2198cda6cd7SBarry Smith   /*88 */ "Error in system call",
220a17b96a8SKyle Gerard Felker   /*89 */ "Object Type not set: https://petsc.org/release/faq/#object-type-not-set",
22173260a9bSLisandro Dalcin   /*90 */ "",
22273260a9bSLisandro Dalcin   /*   */ "",
223a17b96a8SKyle Gerard Felker   /*92 */ "See https://petsc.org/release/overview/linear_solve_table/ for possible LU and Cholesky solvers",
224f560318cSPatrick Sanan   /*93 */ "You cannot overwrite this option since that will conflict with other previously set options",
225691b26d3SBarry Smith   /*94 */ "Example/application run with number of MPI ranks it does not support",
226691b26d3SBarry Smith   /*95 */ "Missing or incorrect user input",
227e57d7714SBarry Smith   /*96 */ "GPU resources unavailable",
228764761abSStefano Zampini   /*97 */ "GPU error",
2299371c9d4SSatish Balay   /*98 */ "General MPI error"};
230e5c89e4eSSatish Balay 
231e5c89e4eSSatish Balay /*@C
232e5c89e4eSSatish Balay    PetscErrorMessage - returns the text string associated with a PETSc error code.
233e5c89e4eSSatish Balay 
234e5c89e4eSSatish Balay    Not Collective
235e5c89e4eSSatish Balay 
236e5c89e4eSSatish Balay    Input Parameter:
237e5c89e4eSSatish Balay .   errnum - the error code
238e5c89e4eSSatish Balay 
239d8d19677SJose E. Roman    Output Parameters:
2400298fd71SBarry Smith +  text - the error message (NULL if not desired)
2410298fd71SBarry Smith -  specific - the specific error message that was set with SETERRxxx() or PetscError().  (NULL if not desired)
242e5c89e4eSSatish Balay 
243e5c89e4eSSatish Balay    Level: developer
244e5c89e4eSSatish Balay 
245db781477SPatrick Sanan .seealso: `PetscPushErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscCall()`
246db781477SPatrick Sanan           `PetscAbortErrorHandler()`, `PetscTraceBackErrorHandler()`
247e5c89e4eSSatish Balay  @*/
2489371c9d4SSatish Balay PetscErrorCode PetscErrorMessage(int errnum, const char *text[], char **specific) {
249c35ec7c7SPierre Jolivet   size_t len;
250c35ec7c7SPierre Jolivet 
251e5c89e4eSSatish Balay   PetscFunctionBegin;
252c35ec7c7SPierre Jolivet   if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) {
253c35ec7c7SPierre Jolivet     *text = PetscErrorStrings[errnum - PETSC_ERR_MIN_VALUE - 1];
254c35ec7c7SPierre Jolivet     PetscCall(PetscStrlen(*text, &len));
255c35ec7c7SPierre Jolivet     if (!len) *text = NULL;
2569371c9d4SSatish Balay   } else if (text) *text = NULL;
257e5c89e4eSSatish Balay 
258a297a907SKarl Rupp   if (specific) *specific = PetscErrorBaseMessage;
259e5c89e4eSSatish Balay   PetscFunctionReturn(0);
260e5c89e4eSSatish Balay }
261e5c89e4eSSatish Balay 
262984a1229SMatthew G. Knepley #if defined(PETSC_CLANGUAGE_CXX)
263984a1229SMatthew G. Knepley /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software
264984a1229SMatthew G. Knepley  * would be broken if implementations did not handle it it some common cases. However, keep in mind
265984a1229SMatthew G. Knepley  *
266984a1229SMatthew G. Knepley  *   Rule 62. Don't allow exceptions to propagate across module boundaries
267984a1229SMatthew G. Knepley  *
268984a1229SMatthew G. Knepley  * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface
269984a1229SMatthew G. Knepley  * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed.
270984a1229SMatthew G. Knepley  *
271984a1229SMatthew G. Knepley  * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message
272984a1229SMatthew G. Knepley  * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that
273984a1229SMatthew G. Knepley  * seems crazy to me.
274984a1229SMatthew G. Knepley  */
275984a1229SMatthew G. Knepley #include <sstream>
2764c94c282SMatthew G. Knepley #include <stdexcept>
2779371c9d4SSatish Balay static void PetscCxxErrorThrow() {
278984a1229SMatthew G. Knepley   const char *str;
279984a1229SMatthew G. Knepley   if (eh && eh->ctx) {
280984a1229SMatthew G. Knepley     std::ostringstream *msg;
281984a1229SMatthew G. Knepley     msg = (std::ostringstream *)eh->ctx;
282984a1229SMatthew G. Knepley     str = msg->str().c_str();
283984a1229SMatthew G. Knepley   } else str = "Error detected in C PETSc";
284984a1229SMatthew G. Knepley 
285984a1229SMatthew G. Knepley   throw std::runtime_error(str);
286984a1229SMatthew G. Knepley }
287984a1229SMatthew G. Knepley #endif
288984a1229SMatthew G. Knepley 
289e5c89e4eSSatish Balay /*@C
29045b666d6SBarry Smith    PetscError - Routine that is called when an error has been detected, usually called through the macro SETERRQ(PETSC_COMM_SELF,).
291e5c89e4eSSatish Balay 
29245b666d6SBarry Smith   Collective on comm
293e5c89e4eSSatish Balay 
294e5c89e4eSSatish Balay    Input Parameters:
295e32f2f54SBarry Smith +  comm - communicator over which error occurred.  ALL ranks of this communicator MUST call this routine
296e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
2973de71b31SHong Zhang .  func - the function name in which the error was detected
298e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
299e5c89e4eSSatish Balay .  n - the generic error number
300d736bfebSBarry Smith .  p - PETSC_ERROR_INITIAL indicates the error was initially detected, PETSC_ERROR_REPEAT indicates this is a traceback from a previously detected error
301e5c89e4eSSatish Balay -  mess - formatted message string - aka printf
302e5c89e4eSSatish Balay 
30345b666d6SBarry Smith   Options Database:
30445b666d6SBarry Smith +  -error_output_stdout - output the error messages to stdout instead of the default stderr
30545b666d6SBarry Smith -  -error_output_none - do not output the error messages
30645b666d6SBarry Smith 
307e5c89e4eSSatish Balay   Level: intermediate
308e5c89e4eSSatish Balay 
309e5c89e4eSSatish Balay    Notes:
31045b666d6SBarry Smith    PETSc error handling is done with error return codes. A non-zero return indicates an error was detected. Errors are generally not something that the code
31145b666d6SBarry Smith    can recover from. Note that numerical errors (potential divide by zero, for example) are not managed by the error return codes; they are managed via, for example,
31245b666d6SBarry Smith    KSPGetConvergedReason() that indicates if the solve was successful or not. The option -ksp_error_if_not_converged, for example, turns numerical failures into
31345b666d6SBarry Smith    hard errors managed via PetscError().
31445b666d6SBarry Smith 
31545b666d6SBarry Smith    PETSc provides a rich supply of error handlers, see the list below, and users can also provide their own error handlers.
31645b666d6SBarry Smith 
317e5c89e4eSSatish Balay    Most users need not directly use this routine and the error handlers, but
318e5c89e4eSSatish Balay    can instead use the simplified interface SETERRQ, which has the calling
319e5c89e4eSSatish Balay    sequence
320e32f2f54SBarry Smith $     SETERRQ(comm,n,mess)
321e5c89e4eSSatish Balay 
322e3081792SBarry Smith    Fortran Note:
323e3081792SBarry Smith    This routine is used differently from Fortran
324e3081792SBarry Smith $    PetscError(MPI_Comm comm,PetscErrorCode n,PetscErrorType p,char *message)
325e3081792SBarry Smith 
32645b666d6SBarry Smith    Set the error handler with PetscPushErrorHandler().
327e5c89e4eSSatish Balay 
3283bf036e2SBarry Smith    Developer Note: Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes)
3293bf036e2SBarry Smith    BUT this routine does call regular PETSc functions that may call error handlers, this is problematic and could be fixed by never calling other PETSc routines
3303bf036e2SBarry Smith    but this annoying.
3313bf036e2SBarry Smith 
332db781477SPatrick Sanan .seealso: `PetscErrorCode`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`,
333db781477SPatrick Sanan           `PetscReturnErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()`,
334db781477SPatrick Sanan           `SETERRQ()`, `PetscCall()`, `CHKMEMQ`, `SETERRQ()`, `SETERRQ()`, `PetscErrorMessage()`, `PETSCABORT()`
335e5c89e4eSSatish Balay @*/
3369371c9d4SSatish Balay PetscErrorCode PetscError(MPI_Comm comm, int line, const char *func, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, ...) {
337e5c89e4eSSatish Balay   va_list        Argp;
338c9a19010SBarry Smith   size_t         fullLength;
33902c9f0b5SLisandro Dalcin   char           buf[2048], *lbuf = NULL;
340fbfcfee5SBarry Smith   PetscBool      ismain;
3413bf036e2SBarry Smith   PetscErrorCode ierr;
342e5c89e4eSSatish Balay 
34339a651e2SJacob Faibussowitsch   if (!PetscErrorHandlingInitialized) return n;
3440d577829SBarry Smith   if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF;
345e5c89e4eSSatish Balay 
346e5c89e4eSSatish Balay   /* Compose the message evaluating the print format */
347e5c89e4eSSatish Balay   if (mess) {
348e5c89e4eSSatish Balay     va_start(Argp, mess);
3492d609e63SMatthew Knepley     PetscVSNPrintf(buf, 2048, mess, &fullLength, Argp);
350e5c89e4eSSatish Balay     va_end(Argp);
351e5c89e4eSSatish Balay     lbuf = buf;
35278179f8bSBarry Smith     if (p == PETSC_ERROR_INITIAL) PetscStrncpy(PetscErrorBaseMessage, lbuf, 1023);
353e5c89e4eSSatish Balay   }
354e5c89e4eSSatish Balay 
3554ed0ab5bSBarry Smith   if (p == PETSC_ERROR_INITIAL && n != PETSC_ERR_MEMC) PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__);
3564ed0ab5bSBarry Smith 
35702c9f0b5SLisandro Dalcin   if (!eh) ierr = PetscTraceBackErrorHandler(comm, line, func, file, n, p, lbuf, NULL);
358efca3c55SSatish Balay   else ierr = (*eh->handler)(comm, line, func, file, n, p, lbuf, eh->ctx);
359362febeeSStefano Zampini   PetscStackClearTop;
360e5c89e4eSSatish Balay 
361e5c89e4eSSatish Balay   /*
3627233276eSBarry Smith       If this is called from the main() routine we call MPI_Abort() instead of
363e5c89e4eSSatish Balay     return to allow the parallel program to be properly shutdown.
364e5c89e4eSSatish Balay 
3657233276eSBarry Smith     Does not call PETSCABORT() since that would provide the wrong source file and line number information
366e5c89e4eSSatish Balay   */
36749c86fc7SBarry Smith   if (func) {
368e5c89e4eSSatish Balay     PetscStrncmp(func, "main", 4, &ismain);
3697233276eSBarry Smith     if (ismain) {
37049c86fc7SBarry Smith       if (petscwaitonerrorflg) PetscSleep(1000);
371660278c0SBarry Smith       PETSCABORT(comm, ierr);
3727233276eSBarry Smith     }
37349c86fc7SBarry Smith   }
3742c280183SJed Brown #if defined(PETSC_CLANGUAGE_CXX)
3759371c9d4SSatish Balay   if (p == PETSC_ERROR_IN_CXX) { PetscCxxErrorThrow(); }
376d736bfebSBarry Smith #endif
377362febeeSStefano Zampini   return ierr;
378e5c89e4eSSatish Balay }
379e5c89e4eSSatish Balay 
380e5c89e4eSSatish Balay /* -------------------------------------------------------------------------*/
381e5c89e4eSSatish Balay 
382e5c89e4eSSatish Balay /*@C
383e5c89e4eSSatish Balay     PetscIntView - Prints an array of integers; useful for debugging.
384e5c89e4eSSatish Balay 
385e5c89e4eSSatish Balay     Collective on PetscViewer
386e5c89e4eSSatish Balay 
387e5c89e4eSSatish Balay     Input Parameters:
388e5c89e4eSSatish Balay +   N - number of integers in array
389e5c89e4eSSatish Balay .   idx - array of integers
390e5c89e4eSSatish Balay -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
391e5c89e4eSSatish Balay 
392e5c89e4eSSatish Balay   Level: intermediate
393e5c89e4eSSatish Balay 
39495452b02SPatrick Sanan     Developer Notes:
39595452b02SPatrick Sanan     idx cannot be const because may be passed to binary viewer where byte swapping is done
396300a7f5bSBarry Smith 
397db781477SPatrick Sanan .seealso: `PetscRealView()`
398e5c89e4eSSatish Balay @*/
3999371c9d4SSatish Balay PetscErrorCode PetscIntView(PetscInt N, const PetscInt idx[], PetscViewer viewer) {
400ca0c3be5SJacob Faibussowitsch   PetscMPIInt rank, size;
401e5c89e4eSSatish Balay   PetscInt    j, i, n = N / 20, p = N % 20;
402ace3abfcSBarry Smith   PetscBool   iascii, isbinary;
403e5c89e4eSSatish Balay   MPI_Comm    comm;
404e5c89e4eSSatish Balay 
405e5c89e4eSSatish Balay   PetscFunctionBegin;
406e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
4070700a824SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 3);
4085ab33896SBarry Smith   if (N) PetscValidIntPointer(idx, 2);
4099566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
4109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
4119566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
412e5c89e4eSSatish Balay 
4139566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
4149566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
415e5c89e4eSSatish Balay   if (iascii) {
4169566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
417e5c89e4eSSatish Balay     for (i = 0; i < n; i++) {
418ca0c3be5SJacob Faibussowitsch       if (size > 1) {
4199566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %" PetscInt_FMT ":", rank, 20 * i));
420ca0c3be5SJacob Faibussowitsch       } else {
4219566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%" PetscInt_FMT ":", 20 * i));
422ca0c3be5SJacob Faibussowitsch       }
423*48a46eb9SPierre Jolivet       for (j = 0; j < 20; j++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, idx[i * 20 + j]));
4249566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
425e5c89e4eSSatish Balay     }
426e5c89e4eSSatish Balay     if (p) {
427ca0c3be5SJacob Faibussowitsch       if (size > 1) {
4289566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %" PetscInt_FMT ":", rank, 20 * n));
429ca0c3be5SJacob Faibussowitsch       } else {
4309566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%" PetscInt_FMT ":", 20 * n));
431ca0c3be5SJacob Faibussowitsch       }
4329566063dSJacob Faibussowitsch       for (i = 0; i < p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, idx[20 * n + i]));
4339566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
434e5c89e4eSSatish Balay     }
4359566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
4369566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
4376805f65bSBarry Smith   } else if (isbinary) {
438ca0c3be5SJacob Faibussowitsch     PetscMPIInt *sizes, Ntotal, *displs, NN;
439e5c89e4eSSatish Balay     PetscInt    *array;
440783b601eSJed Brown 
4419566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(N, &NN));
442e5c89e4eSSatish Balay 
443e5c89e4eSSatish Balay     if (size > 1) {
444e5c89e4eSSatish Balay       if (rank) {
4459566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm));
4469566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_INT, NULL, NULL, NULL, MPIU_INT, 0, comm));
447e5c89e4eSSatish Balay       } else {
4489566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(size, &sizes));
4499566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm));
450e5c89e4eSSatish Balay         Ntotal = sizes[0];
4519566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(size, &displs));
452e5c89e4eSSatish Balay         displs[0] = 0;
453e5c89e4eSSatish Balay         for (i = 1; i < size; i++) {
454e5c89e4eSSatish Balay           Ntotal += sizes[i];
455e5c89e4eSSatish Balay           displs[i] = displs[i - 1] + sizes[i - 1];
456e5c89e4eSSatish Balay         }
4579566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(Ntotal, &array));
4589566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_INT, array, sizes, displs, MPIU_INT, 0, comm));
4599566063dSJacob Faibussowitsch         PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_INT));
4609566063dSJacob Faibussowitsch         PetscCall(PetscFree(sizes));
4619566063dSJacob Faibussowitsch         PetscCall(PetscFree(displs));
4629566063dSJacob Faibussowitsch         PetscCall(PetscFree(array));
463e5c89e4eSSatish Balay       }
464e5c89e4eSSatish Balay     } else {
4659566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer, idx, N, PETSC_INT));
466e5c89e4eSSatish Balay     }
467e5c89e4eSSatish Balay   } else {
468e5c89e4eSSatish Balay     const char *tname;
4699566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)viewer, &tname));
47098921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname);
471e5c89e4eSSatish Balay   }
472e5c89e4eSSatish Balay   PetscFunctionReturn(0);
473e5c89e4eSSatish Balay }
474e5c89e4eSSatish Balay 
475e5c89e4eSSatish Balay /*@C
476e5c89e4eSSatish Balay     PetscRealView - Prints an array of doubles; useful for debugging.
477e5c89e4eSSatish Balay 
478e5c89e4eSSatish Balay     Collective on PetscViewer
479e5c89e4eSSatish Balay 
480e5c89e4eSSatish Balay     Input Parameters:
481cba51d77SBarry Smith +   N - number of PetscReal in array
482cba51d77SBarry Smith .   idx - array of PetscReal
483e5c89e4eSSatish Balay -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
484e5c89e4eSSatish Balay 
485e5c89e4eSSatish Balay   Level: intermediate
486e5c89e4eSSatish Balay 
48795452b02SPatrick Sanan     Developer Notes:
48895452b02SPatrick Sanan     idx cannot be const because may be passed to binary viewer where byte swapping is done
489300a7f5bSBarry Smith 
490db781477SPatrick Sanan .seealso: `PetscIntView()`
491e5c89e4eSSatish Balay @*/
4929371c9d4SSatish Balay PetscErrorCode PetscRealView(PetscInt N, const PetscReal idx[], PetscViewer viewer) {
493ca0c3be5SJacob Faibussowitsch   PetscMPIInt rank, size;
494e5c89e4eSSatish Balay   PetscInt    j, i, n = N / 5, p = N % 5;
495ace3abfcSBarry Smith   PetscBool   iascii, isbinary;
496e5c89e4eSSatish Balay   MPI_Comm    comm;
497e5c89e4eSSatish Balay 
498e5c89e4eSSatish Balay   PetscFunctionBegin;
499e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
5000700a824SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 3);
501064a246eSJacob Faibussowitsch   PetscValidRealPointer(idx, 2);
5029566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
5039566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
5049566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
505e5c89e4eSSatish Balay 
5069566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
5079566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
508e5c89e4eSSatish Balay   if (iascii) {
5091a989b97SToby Isaac     PetscInt tab;
5101a989b97SToby Isaac 
5119566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
5129566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetTab(viewer, &tab));
513e5c89e4eSSatish Balay     for (i = 0; i < n; i++) {
5149566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISetTab(viewer, tab));
515ca0c3be5SJacob Faibussowitsch       if (size > 1) {
5169566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, 5 * i));
517ca0c3be5SJacob Faibussowitsch       } else {
5189566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", 5 * i));
519ca0c3be5SJacob Faibussowitsch       }
5209566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISetTab(viewer, 0));
521*48a46eb9SPierre Jolivet       for (j = 0; j < 5; j++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[i * 5 + j]));
5229566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
523e5c89e4eSSatish Balay     }
524e5c89e4eSSatish Balay     if (p) {
5259566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISetTab(viewer, tab));
526ca0c3be5SJacob Faibussowitsch       if (size > 1) {
5279566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, 5 * n));
528ca0c3be5SJacob Faibussowitsch       } else {
5299566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", 5 * n));
530ca0c3be5SJacob Faibussowitsch       }
5319566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISetTab(viewer, 0));
5329566063dSJacob Faibussowitsch       for (i = 0; i < p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[5 * n + i]));
5339566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
534e5c89e4eSSatish Balay     }
5359566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
5369566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIISetTab(viewer, tab));
5379566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
5386805f65bSBarry Smith   } else if (isbinary) {
539ca0c3be5SJacob Faibussowitsch     PetscMPIInt *sizes, *displs, Ntotal, NN;
540e5c89e4eSSatish Balay     PetscReal   *array;
541e5c89e4eSSatish Balay 
5429566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(N, &NN));
543e5c89e4eSSatish Balay 
544e5c89e4eSSatish Balay     if (size > 1) {
545e5c89e4eSSatish Balay       if (rank) {
5469566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm));
5479566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gatherv((PetscReal *)idx, NN, MPIU_REAL, NULL, NULL, NULL, MPIU_REAL, 0, comm));
548e5c89e4eSSatish Balay       } else {
5499566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(size, &sizes));
5509566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm));
551e5c89e4eSSatish Balay         Ntotal = sizes[0];
5529566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(size, &displs));
553e5c89e4eSSatish Balay         displs[0] = 0;
554e5c89e4eSSatish Balay         for (i = 1; i < size; i++) {
555e5c89e4eSSatish Balay           Ntotal += sizes[i];
556e5c89e4eSSatish Balay           displs[i] = displs[i - 1] + sizes[i - 1];
557e5c89e4eSSatish Balay         }
5589566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(Ntotal, &array));
5599566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gatherv((PetscReal *)idx, NN, MPIU_REAL, array, sizes, displs, MPIU_REAL, 0, comm));
5609566063dSJacob Faibussowitsch         PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_REAL));
5619566063dSJacob Faibussowitsch         PetscCall(PetscFree(sizes));
5629566063dSJacob Faibussowitsch         PetscCall(PetscFree(displs));
5639566063dSJacob Faibussowitsch         PetscCall(PetscFree(array));
564e5c89e4eSSatish Balay       }
565e5c89e4eSSatish Balay     } else {
5669566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer, (void *)idx, N, PETSC_REAL));
567e5c89e4eSSatish Balay     }
568e5c89e4eSSatish Balay   } else {
569e5c89e4eSSatish Balay     const char *tname;
5709566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)viewer, &tname));
57198921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname);
572e5c89e4eSSatish Balay   }
573e5c89e4eSSatish Balay   PetscFunctionReturn(0);
574e5c89e4eSSatish Balay }
575e5c89e4eSSatish Balay 
576e5c89e4eSSatish Balay /*@C
577e5c89e4eSSatish Balay     PetscScalarView - Prints an array of scalars; useful for debugging.
578e5c89e4eSSatish Balay 
579e5c89e4eSSatish Balay     Collective on PetscViewer
580e5c89e4eSSatish Balay 
581e5c89e4eSSatish Balay     Input Parameters:
582e5c89e4eSSatish Balay +   N - number of scalars in array
583e5c89e4eSSatish Balay .   idx - array of scalars
584e5c89e4eSSatish Balay -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
585e5c89e4eSSatish Balay 
586e5c89e4eSSatish Balay   Level: intermediate
587e5c89e4eSSatish Balay 
58895452b02SPatrick Sanan     Developer Notes:
58995452b02SPatrick Sanan     idx cannot be const because may be passed to binary viewer where byte swapping is done
590300a7f5bSBarry Smith 
591db781477SPatrick Sanan .seealso: `PetscIntView()`, `PetscRealView()`
592e5c89e4eSSatish Balay @*/
5939371c9d4SSatish Balay PetscErrorCode PetscScalarView(PetscInt N, const PetscScalar idx[], PetscViewer viewer) {
594ca0c3be5SJacob Faibussowitsch   PetscMPIInt rank, size;
595e5c89e4eSSatish Balay   PetscInt    j, i, n = N / 3, p = N % 3;
596ace3abfcSBarry Smith   PetscBool   iascii, isbinary;
597e5c89e4eSSatish Balay   MPI_Comm    comm;
598e5c89e4eSSatish Balay 
599e5c89e4eSSatish Balay   PetscFunctionBegin;
600e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
601e5c89e4eSSatish Balay   PetscValidHeader(viewer, 3);
6028c34849bSStefano Zampini   if (N) PetscValidScalarPointer(idx, 2);
6039566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
6049566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
6059566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
606e5c89e4eSSatish Balay 
6079566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
6089566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
609e5c89e4eSSatish Balay   if (iascii) {
6109566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
611e5c89e4eSSatish Balay     for (i = 0; i < n; i++) {
612ca0c3be5SJacob Faibussowitsch       if (size > 1) {
6139566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, 3 * i));
614ca0c3be5SJacob Faibussowitsch       } else {
6159566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", 3 * i));
616ca0c3be5SJacob Faibussowitsch       }
617e5c89e4eSSatish Balay       for (j = 0; j < 3; j++) {
618e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
6199566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%12.4e,%12.4e)", (double)PetscRealPart(idx[i * 3 + j]), (double)PetscImaginaryPart(idx[i * 3 + j])));
620e5c89e4eSSatish Balay #else
6219566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[i * 3 + j]));
622e5c89e4eSSatish Balay #endif
623e5c89e4eSSatish Balay       }
6249566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
625e5c89e4eSSatish Balay     }
626e5c89e4eSSatish Balay     if (p) {
627ca0c3be5SJacob Faibussowitsch       if (size > 1) {
6289566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, 3 * n));
629ca0c3be5SJacob Faibussowitsch       } else {
6309566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", 3 * n));
631ca0c3be5SJacob Faibussowitsch       }
632e5c89e4eSSatish Balay       for (i = 0; i < p; i++) {
633e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
6349566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%12.4e,%12.4e)", (double)PetscRealPart(idx[n * 3 + i]), (double)PetscImaginaryPart(idx[n * 3 + i])));
635e5c89e4eSSatish Balay #else
6369566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[3 * n + i]));
637e5c89e4eSSatish Balay #endif
638e5c89e4eSSatish Balay       }
6399566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
640e5c89e4eSSatish Balay     }
6419566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
6429566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
6436805f65bSBarry Smith   } else if (isbinary) {
644ca0c3be5SJacob Faibussowitsch     PetscMPIInt *sizes, Ntotal, *displs, NN;
645e5c89e4eSSatish Balay     PetscScalar *array;
646e5c89e4eSSatish Balay 
6479566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(N, &NN));
648e5c89e4eSSatish Balay 
649e5c89e4eSSatish Balay     if (size > 1) {
650e5c89e4eSSatish Balay       if (rank) {
6519566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm));
6529566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_SCALAR, NULL, NULL, NULL, MPIU_SCALAR, 0, comm));
653e5c89e4eSSatish Balay       } else {
6549566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(size, &sizes));
6559566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm));
656e5c89e4eSSatish Balay         Ntotal = sizes[0];
6579566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(size, &displs));
658e5c89e4eSSatish Balay         displs[0] = 0;
659e5c89e4eSSatish Balay         for (i = 1; i < size; i++) {
660e5c89e4eSSatish Balay           Ntotal += sizes[i];
661e5c89e4eSSatish Balay           displs[i] = displs[i - 1] + sizes[i - 1];
662e5c89e4eSSatish Balay         }
6639566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(Ntotal, &array));
6649566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_SCALAR, array, sizes, displs, MPIU_SCALAR, 0, comm));
6659566063dSJacob Faibussowitsch         PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_SCALAR));
6669566063dSJacob Faibussowitsch         PetscCall(PetscFree(sizes));
6679566063dSJacob Faibussowitsch         PetscCall(PetscFree(displs));
6689566063dSJacob Faibussowitsch         PetscCall(PetscFree(array));
669e5c89e4eSSatish Balay       }
670e5c89e4eSSatish Balay     } else {
6719566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer, (void *)idx, N, PETSC_SCALAR));
672e5c89e4eSSatish Balay     }
673e5c89e4eSSatish Balay   } else {
674e5c89e4eSSatish Balay     const char *tname;
6759566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)viewer, &tname));
67698921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname);
677e5c89e4eSSatish Balay   }
678e5c89e4eSSatish Balay   PetscFunctionReturn(0);
679e5c89e4eSSatish Balay }
680e5c89e4eSSatish Balay 
681e22e20c5SJunchao Zhang #if defined(PETSC_HAVE_CUDA)
682030f984aSJacob Faibussowitsch #include <petscdevice.h>
6839371c9d4SSatish Balay PETSC_EXTERN const char *PetscCUBLASGetErrorName(cublasStatus_t status) {
684e22e20c5SJunchao Zhang   switch (status) {
685e22e20c5SJunchao Zhang #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */
686e22e20c5SJunchao Zhang   case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
687e22e20c5SJunchao Zhang   case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
688e22e20c5SJunchao Zhang   case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
689e22e20c5SJunchao Zhang   case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
690e22e20c5SJunchao Zhang   case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
691e22e20c5SJunchao Zhang   case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
692e22e20c5SJunchao Zhang   case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
693e22e20c5SJunchao Zhang   case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
694e22e20c5SJunchao Zhang   case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
695e22e20c5SJunchao Zhang   case CUBLAS_STATUS_LICENSE_ERROR: return "CUBLAS_STATUS_LICENSE_ERROR";
696e22e20c5SJunchao Zhang #endif
697e22e20c5SJunchao Zhang   default: return "unknown error";
698e22e20c5SJunchao Zhang   }
699e22e20c5SJunchao Zhang }
7009371c9d4SSatish Balay PETSC_EXTERN const char *PetscCUSolverGetErrorName(cusolverStatus_t status) {
701a4b895e1SBarry Smith   switch (status) {
702a4b895e1SBarry Smith #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */
703a4b895e1SBarry Smith   case CUSOLVER_STATUS_SUCCESS: return "CUSOLVER_STATUS_SUCCESS";
704a4b895e1SBarry Smith   case CUSOLVER_STATUS_NOT_INITIALIZED: return "CUSOLVER_STATUS_NOT_INITIALIZED";
705a4b895e1SBarry Smith   case CUSOLVER_STATUS_INVALID_VALUE: return "CUSOLVER_STATUS_INVALID_VALUE";
706a4b895e1SBarry Smith   case CUSOLVER_STATUS_ARCH_MISMATCH: return "CUSOLVER_STATUS_ARCH_MISMATCH";
707a4b895e1SBarry Smith   case CUSOLVER_STATUS_INTERNAL_ERROR: return "CUSOLVER_STATUS_INTERNAL_ERROR";
708030f984aSJacob Faibussowitsch #if (CUDART_VERSION >= 9000) /* CUDA 9.0 had these defined on June 2021 */
709030f984aSJacob Faibussowitsch   case CUSOLVER_STATUS_ALLOC_FAILED: return "CUSOLVER_STATUS_ALLOC_FAILED";
710030f984aSJacob Faibussowitsch   case CUSOLVER_STATUS_MAPPING_ERROR: return "CUSOLVER_STATUS_MAPPING_ERROR";
711030f984aSJacob Faibussowitsch   case CUSOLVER_STATUS_EXECUTION_FAILED: return "CUSOLVER_STATUS_EXECUTION_FAILED";
712030f984aSJacob Faibussowitsch   case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED: return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
713030f984aSJacob Faibussowitsch   case CUSOLVER_STATUS_NOT_SUPPORTED: return "CUSOLVER_STATUS_NOT_SUPPORTED ";
714030f984aSJacob Faibussowitsch   case CUSOLVER_STATUS_ZERO_PIVOT: return "CUSOLVER_STATUS_ZERO_PIVOT";
715030f984aSJacob Faibussowitsch   case CUSOLVER_STATUS_INVALID_LICENSE: return "CUSOLVER_STATUS_INVALID_LICENSE";
716a4b895e1SBarry Smith #endif
717030f984aSJacob Faibussowitsch #endif
718030f984aSJacob Faibussowitsch   default: return "unknown error";
719030f984aSJacob Faibussowitsch   }
720030f984aSJacob Faibussowitsch }
7219371c9d4SSatish Balay PETSC_EXTERN const char *PetscCUFFTGetErrorName(cufftResult result) {
722030f984aSJacob Faibussowitsch   switch (result) {
723030f984aSJacob Faibussowitsch   case CUFFT_SUCCESS: return "CUFFT_SUCCESS";
724030f984aSJacob Faibussowitsch   case CUFFT_INVALID_PLAN: return "CUFFT_INVALID_PLAN";
725030f984aSJacob Faibussowitsch   case CUFFT_ALLOC_FAILED: return "CUFFT_ALLOC_FAILED";
726030f984aSJacob Faibussowitsch   case CUFFT_INVALID_TYPE: return "CUFFT_INVALID_TYPE";
727030f984aSJacob Faibussowitsch   case CUFFT_INVALID_VALUE: return "CUFFT_INVALID_VALUE";
728030f984aSJacob Faibussowitsch   case CUFFT_INTERNAL_ERROR: return "CUFFT_INTERNAL_ERROR";
729030f984aSJacob Faibussowitsch   case CUFFT_EXEC_FAILED: return "CUFFT_EXEC_FAILED";
730030f984aSJacob Faibussowitsch   case CUFFT_SETUP_FAILED: return "CUFFT_SETUP_FAILED";
731030f984aSJacob Faibussowitsch   case CUFFT_INVALID_SIZE: return "CUFFT_INVALID_SIZE";
732030f984aSJacob Faibussowitsch   case CUFFT_UNALIGNED_DATA: return "CUFFT_UNALIGNED_DATA";
733030f984aSJacob Faibussowitsch   case CUFFT_INCOMPLETE_PARAMETER_LIST: return "CUFFT_INCOMPLETE_PARAMETER_LIST";
734030f984aSJacob Faibussowitsch   case CUFFT_INVALID_DEVICE: return "CUFFT_INVALID_DEVICE";
735030f984aSJacob Faibussowitsch   case CUFFT_PARSE_ERROR: return "CUFFT_PARSE_ERROR";
736030f984aSJacob Faibussowitsch   case CUFFT_NO_WORKSPACE: return "CUFFT_NO_WORKSPACE";
737030f984aSJacob Faibussowitsch   case CUFFT_NOT_IMPLEMENTED: return "CUFFT_NOT_IMPLEMENTED";
738030f984aSJacob Faibussowitsch   case CUFFT_LICENSE_ERROR: return "CUFFT_LICENSE_ERROR";
739030f984aSJacob Faibussowitsch   case CUFFT_NOT_SUPPORTED: return "CUFFT_NOT_SUPPORTED";
740a4b895e1SBarry Smith   default: return "unknown error";
741a4b895e1SBarry Smith   }
742a4b895e1SBarry Smith }
743e22e20c5SJunchao Zhang #endif
74459af0bd3SScott Kruger 
74559af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP)
746030f984aSJacob Faibussowitsch #include <petscdevice.h>
7479371c9d4SSatish Balay PETSC_EXTERN const char *PetscHIPBLASGetErrorName(hipblasStatus_t status) {
74859af0bd3SScott Kruger   switch (status) {
74959af0bd3SScott Kruger   case HIPBLAS_STATUS_SUCCESS: return "HIPBLAS_STATUS_SUCCESS";
75059af0bd3SScott Kruger   case HIPBLAS_STATUS_NOT_INITIALIZED: return "HIPBLAS_STATUS_NOT_INITIALIZED";
75159af0bd3SScott Kruger   case HIPBLAS_STATUS_ALLOC_FAILED: return "HIPBLAS_STATUS_ALLOC_FAILED";
75259af0bd3SScott Kruger   case HIPBLAS_STATUS_INVALID_VALUE: return "HIPBLAS_STATUS_INVALID_VALUE";
75359af0bd3SScott Kruger   case HIPBLAS_STATUS_ARCH_MISMATCH: return "HIPBLAS_STATUS_ARCH_MISMATCH";
75459af0bd3SScott Kruger   case HIPBLAS_STATUS_MAPPING_ERROR: return "HIPBLAS_STATUS_MAPPING_ERROR";
75559af0bd3SScott Kruger   case HIPBLAS_STATUS_EXECUTION_FAILED: return "HIPBLAS_STATUS_EXECUTION_FAILED";
75659af0bd3SScott Kruger   case HIPBLAS_STATUS_INTERNAL_ERROR: return "HIPBLAS_STATUS_INTERNAL_ERROR";
75759af0bd3SScott Kruger   case HIPBLAS_STATUS_NOT_SUPPORTED: return "HIPBLAS_STATUS_NOT_SUPPORTED";
75859af0bd3SScott Kruger   default: return "unknown error";
75959af0bd3SScott Kruger   }
76059af0bd3SScott Kruger }
76159af0bd3SScott Kruger #endif
762db9cea48SBarry Smith 
763db9cea48SBarry Smith /*@
764db9cea48SBarry Smith       PetscMPIErrorString - Given an MPI error code returns the MPI_Error_string() appropriately
765db9cea48SBarry Smith            formatted for displaying with the PETSc error handlers.
766db9cea48SBarry Smith 
767db9cea48SBarry Smith  Input Parameter:
768db9cea48SBarry Smith .  err - the MPI error code
769db9cea48SBarry Smith 
770db9cea48SBarry Smith  Output Parameter:
771db9cea48SBarry Smith .  string - the MPI error message, should declare its length to be larger than MPI_MAX_ERROR_STRING
772db9cea48SBarry Smith 
77349c86fc7SBarry Smith    Level: developer
77449c86fc7SBarry Smith 
775db9cea48SBarry Smith  Notes:
776db9cea48SBarry Smith     Does not return an error code or do error handling because it may be called from inside an error handler
777db9cea48SBarry Smith 
778db9cea48SBarry Smith @*/
7799371c9d4SSatish Balay void PetscMPIErrorString(PetscMPIInt err, char *string) {
780db9cea48SBarry Smith   char        errorstring[MPI_MAX_ERROR_STRING];
781db9cea48SBarry Smith   PetscMPIInt len, j = 0;
782db9cea48SBarry Smith 
783db9cea48SBarry Smith   MPI_Error_string(err, (char *)errorstring, &len);
784db9cea48SBarry Smith   for (PetscMPIInt i = 0; i < len; i++) {
785db9cea48SBarry Smith     string[j++] = errorstring[i];
786db9cea48SBarry Smith     if (errorstring[i] == '\n') {
787db9cea48SBarry Smith       for (PetscMPIInt k = 0; k < 16; k++) string[j++] = ' ';
788db9cea48SBarry Smith     }
789db9cea48SBarry Smith   }
790db9cea48SBarry Smith   string[j] = 0;
791db9cea48SBarry Smith }
792