xref: /petsc/src/sys/error/err.c (revision 668f157ea6d169bb50bcdb9ebcdd418abd089fa7)
1e5c89e4eSSatish Balay #define PETSC_DLL
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay       Code that allows one to set the error handlers
4e5c89e4eSSatish Balay */
5d382aafbSBarry Smith #include "petscsys.h"           /*I "petscsys.h" I*/
6e5c89e4eSSatish Balay #include <stdarg.h>
7e5c89e4eSSatish Balay #if defined(PETSC_HAVE_STDLIB_H)
8e5c89e4eSSatish Balay #include <stdlib.h>
9e5c89e4eSSatish Balay #endif
10e5c89e4eSSatish Balay 
11e5c89e4eSSatish Balay typedef struct _EH *EH;
12e5c89e4eSSatish Balay struct _EH {
130700a824SBarry Smith   int            classid;
14*668f157eSBarry Smith   PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,const char *,PetscErrorCode,PetscErrorType,const char*,void *);
15e5c89e4eSSatish Balay   void           *ctx;
16e5c89e4eSSatish Balay   EH             previous;
17e5c89e4eSSatish Balay };
18e5c89e4eSSatish Balay 
19e5c89e4eSSatish Balay static EH eh = 0;
20e5c89e4eSSatish Balay 
21e5c89e4eSSatish Balay #undef __FUNCT__
22e5c89e4eSSatish Balay #define __FUNCT__ "PetscEmacsClientErrorHandler"
23e5c89e4eSSatish Balay /*@C
24e5c89e4eSSatish Balay    PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to
25e5c89e4eSSatish Balay     load the file where the error occured. Then calls the "previous" error handler.
26e5c89e4eSSatish Balay 
27e5c89e4eSSatish Balay    Not Collective
28e5c89e4eSSatish Balay 
29e5c89e4eSSatish Balay    Input Parameters:
30e32f2f54SBarry Smith +  comm - communicator over which error occured
31e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
32e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
33e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
34e5c89e4eSSatish Balay .  dir - the directory of the file (indicated by __SDIR__)
35e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
36e5c89e4eSSatish Balay .  n - the generic error number
37e5c89e4eSSatish Balay .  p - specific error number
38e5c89e4eSSatish Balay -  ctx - error handler context
39e5c89e4eSSatish Balay 
40e5c89e4eSSatish Balay    Options Database Key:
41e5c89e4eSSatish Balay .   -on_error_emacs <machinename>
42e5c89e4eSSatish Balay 
43e5c89e4eSSatish Balay    Level: developer
44e5c89e4eSSatish Balay 
45e5c89e4eSSatish Balay    Notes:
46e5c89e4eSSatish Balay    You must put (server-start) in your .emacs file for the emacsclient software to work
47e5c89e4eSSatish Balay 
48e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
49e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
50e5c89e4eSSatish Balay    the calling sequence
51e32f2f54SBarry Smith $     SETERRQ(PETSC_COMM_SELF,number,p,mess)
52e5c89e4eSSatish Balay 
53e5c89e4eSSatish Balay    Notes for experienced users:
54e93bc3c1Svictor    Use PetscPushErrorHandler() to set the desired error handler.
55e5c89e4eSSatish Balay 
56e5c89e4eSSatish Balay    Concepts: emacs^going to on error
57e5c89e4eSSatish Balay    Concepts: error handler^going to line in emacs
58e5c89e4eSSatish Balay 
59e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
60e5c89e4eSSatish Balay           PetscAbortErrorHandler()
61e5c89e4eSSatish Balay  @*/
62*668f157eSBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscEmacsClientErrorHandler(MPI_Comm comm,int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
63e5c89e4eSSatish Balay {
64e5c89e4eSSatish Balay   PetscErrorCode ierr;
65e5c89e4eSSatish Balay   char        command[PETSC_MAX_PATH_LEN];
66e5c89e4eSSatish Balay   const char  *pdir;
67e5c89e4eSSatish Balay   FILE        *fp;
68e5c89e4eSSatish Balay 
69e5c89e4eSSatish Balay   PetscFunctionBegin;
70e5c89e4eSSatish Balay   /* Note: don't check error codes since this an error handler :-) */
71e5c89e4eSSatish Balay   ierr = PetscGetPetscDir(&pdir);CHKERRQ(ierr);
72e5c89e4eSSatish Balay   sprintf(command,"emacsclient +%d %s/%s%s\n",line,pdir,dir,file);
73e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN)
74e5c89e4eSSatish Balay   ierr = PetscPOpen(MPI_COMM_WORLD,(char*)ctx,command,"r",&fp);
75e5c89e4eSSatish Balay   ierr = PetscPClose(MPI_COMM_WORLD,fp);
76e5c89e4eSSatish Balay #else
77e32f2f54SBarry Smith   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
78e5c89e4eSSatish Balay #endif
79e5c89e4eSSatish Balay   ierr = PetscPopErrorHandler(); /* remove this handler from the stack of handlers */
80e32f2f54SBarry Smith   if (!eh)     ierr = PetscTraceBackErrorHandler(comm,line,fun,file,dir,n,p,mess,0);
81e32f2f54SBarry Smith   else         ierr = (*eh->handler)(comm,line,fun,file,dir,n,p,mess,eh->ctx);
82e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
83e5c89e4eSSatish Balay }
84e5c89e4eSSatish Balay 
85e5c89e4eSSatish Balay #undef __FUNCT__
86e5c89e4eSSatish Balay #define __FUNCT__ "PetscPushErrorHandler"
87e5c89e4eSSatish Balay /*@C
88e5c89e4eSSatish Balay    PetscPushErrorHandler - Sets a routine to be called on detection of errors.
89e5c89e4eSSatish Balay 
90e5c89e4eSSatish Balay    Not Collective
91e5c89e4eSSatish Balay 
92e5c89e4eSSatish Balay    Input Parameters:
93e5c89e4eSSatish Balay +  handler - error handler routine
94e5c89e4eSSatish Balay -  ctx - optional handler context that contains information needed by the handler (for
95e5c89e4eSSatish Balay          example file pointers for error messages etc.)
96e5c89e4eSSatish Balay 
97e5c89e4eSSatish Balay    Calling sequence of handler:
98e32f2f54SBarry Smith $    int handler(MPI_Comm comm,int line,char *func,char *file,char *dir,PetscErrorCode n,int p,char *mess,void *ctx);
99e5c89e4eSSatish Balay 
100e32f2f54SBarry Smith +  comm - communicator over which error occured
101e32f2f54SBarry Smith .  func - the function where the error occured (indicated by __FUNCT__)
102e5c89e4eSSatish Balay .  line - the line number of the error (indicated by __LINE__)
103e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
104e5c89e4eSSatish Balay .  dir - the directory of the file (indicated by __SDIR__)
105e5c89e4eSSatish Balay .  n - the generic error number (see list defined in include/petscerror.h)
106*668f157eSBarry Smith .  p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT
107e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
108e5c89e4eSSatish Balay -  ctx - the error handler context
109e5c89e4eSSatish Balay 
110e5c89e4eSSatish Balay    Options Database Keys:
111e5c89e4eSSatish Balay +   -on_error_attach_debugger <noxterm,gdb or dbx>
112e5c89e4eSSatish Balay -   -on_error_abort
113e5c89e4eSSatish Balay 
114e5c89e4eSSatish Balay    Level: intermediate
115e5c89e4eSSatish Balay 
116e93bc3c1Svictor    Notes:
1177850c7c0SBarry Smith    The currently available PETSc error handlers include PetscTraceBackErrorHandler(),
118e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler(), PetscReturnErrorHandler().
119e93bc3c1Svictor 
1207850c7c0SBarry Smith    Fortran Notes: You can only push one error handler from Fortran before poping it.
1217850c7c0SBarry Smith 
122e5c89e4eSSatish Balay .seealso: PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
123e5c89e4eSSatish Balay 
124e5c89e4eSSatish Balay @*/
125*668f157eSBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm,int,const char *,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void *ctx)
126e5c89e4eSSatish Balay {
127e5c89e4eSSatish Balay   EH             neweh;
128e5c89e4eSSatish Balay   PetscErrorCode ierr;
129e5c89e4eSSatish Balay 
130e5c89e4eSSatish Balay   PetscFunctionBegin;
131e5c89e4eSSatish Balay   ierr = PetscNew(struct _EH,&neweh);CHKERRQ(ierr);
132e5c89e4eSSatish Balay   if (eh) {neweh->previous = eh;}
133e5c89e4eSSatish Balay   else    {neweh->previous = 0;}
134e5c89e4eSSatish Balay   neweh->handler = handler;
135e5c89e4eSSatish Balay   neweh->ctx     = ctx;
136e5c89e4eSSatish Balay   eh             = neweh;
137e5c89e4eSSatish Balay   PetscFunctionReturn(0);
138e5c89e4eSSatish Balay }
139e5c89e4eSSatish Balay 
140e5c89e4eSSatish Balay #undef __FUNCT__
141e5c89e4eSSatish Balay #define __FUNCT__ "PetscPopErrorHandler"
142e30d2299SSatish Balay /*@
143e5c89e4eSSatish Balay    PetscPopErrorHandler - Removes the latest error handler that was
144e5c89e4eSSatish Balay    pushed with PetscPushErrorHandler().
145e5c89e4eSSatish Balay 
146e5c89e4eSSatish Balay    Not Collective
147e5c89e4eSSatish Balay 
148e5c89e4eSSatish Balay    Level: intermediate
149e5c89e4eSSatish Balay 
150e5c89e4eSSatish Balay    Concepts: error handler^setting
151e5c89e4eSSatish Balay 
152e5c89e4eSSatish Balay .seealso: PetscPushErrorHandler()
153e5c89e4eSSatish Balay @*/
154e5c89e4eSSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscPopErrorHandler(void)
155e5c89e4eSSatish Balay {
156e5c89e4eSSatish Balay   EH             tmp;
157e5c89e4eSSatish Balay   PetscErrorCode ierr;
158e5c89e4eSSatish Balay 
159e5c89e4eSSatish Balay   PetscFunctionBegin;
160e5c89e4eSSatish Balay   if (!eh) PetscFunctionReturn(0);
161e5c89e4eSSatish Balay   tmp  = eh;
162e5c89e4eSSatish Balay   eh   = eh->previous;
163e5c89e4eSSatish Balay   ierr = PetscFree(tmp);CHKERRQ(ierr);
164e5c89e4eSSatish Balay 
165e5c89e4eSSatish Balay   PetscFunctionReturn(0);
166e5c89e4eSSatish Balay }
167e5c89e4eSSatish Balay 
168e93bc3c1Svictor #undef __FUNCT__
169e93bc3c1Svictor #define __FUNCT__ "PetscReturnErrorHandler"
170e93bc3c1Svictor /*@C
171e93bc3c1Svictor   PetscReturnErrorHandler - Error handler that causes a return to the current
172e93bc3c1Svictor   level.
173e93bc3c1Svictor 
174e93bc3c1Svictor    Not Collective
175e93bc3c1Svictor 
176e93bc3c1Svictor    Input Parameters:
177e32f2f54SBarry Smith +  comm - communicator over which error occurred
178e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
179e93bc3c1Svictor .  func - the function where error is detected (indicated by __FUNCT__)
180e93bc3c1Svictor .  file - the file in which the error was detected (indicated by __FILE__)
181e93bc3c1Svictor .  dir - the directory of the file (indicated by __SDIR__)
182e93bc3c1Svictor .  mess - an error text string, usually just printed to the screen
183e93bc3c1Svictor .  n - the generic error number
184e93bc3c1Svictor .  p - specific error number
185e93bc3c1Svictor -  ctx - error handler context
186e93bc3c1Svictor 
187e93bc3c1Svictor    Level: developer
188e93bc3c1Svictor 
189e93bc3c1Svictor    Notes:
190e93bc3c1Svictor    Most users need not directly employ this routine and the other error
191e93bc3c1Svictor    handlers, but can instead use the simplified interface SETERRQ, which has
192e93bc3c1Svictor    the calling sequence
193e32f2f54SBarry Smith $     SETERRQ(comm,number,mess)
194e93bc3c1Svictor 
195e93bc3c1Svictor    Notes for experienced users:
196e93bc3c1Svictor    This routine is good for catching errors such as zero pivots in preconditioners
197e93bc3c1Svictor    or breakdown of iterative methods. It is not appropriate for memory violations
198e93bc3c1Svictor    and similar errors.
199e93bc3c1Svictor 
200e93bc3c1Svictor    Use PetscPushErrorHandler() to set the desired error handler.  The
201e93bc3c1Svictor    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
202e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscAbortErrorHandler()
203e93bc3c1Svictor 
204e93bc3c1Svictor    Concepts: error handler
205e93bc3c1Svictor 
206e93bc3c1Svictor .seealso:  PetscPushErrorHandler(), PetscPopErrorHandler().
207e93bc3c1Svictor  @*/
208e93bc3c1Svictor 
209*668f157eSBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscReturnErrorHandler(MPI_Comm comm,int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
210e93bc3c1Svictor {
211e93bc3c1Svictor   PetscFunctionBegin;
212e93bc3c1Svictor   PetscFunctionReturn(n);
213e93bc3c1Svictor }
214e93bc3c1Svictor 
215e5c89e4eSSatish Balay static char PetscErrorBaseMessage[1024];
216e5c89e4eSSatish Balay /*
217e5c89e4eSSatish Balay        The numerical values for these are defined in include/petscerror.h; any changes
218e5c89e4eSSatish Balay    there must also be made here
219e5c89e4eSSatish Balay */
220e5c89e4eSSatish Balay static const char *PetscErrorStrings[] = {
221e5c89e4eSSatish Balay   /*55 */ "Out of memory",
222e5c89e4eSSatish Balay           "No support for this operation for this object type",
223e5c89e4eSSatish Balay           "No support for this operation on this system",
224e5c89e4eSSatish Balay   /*58 */ "Operation done in wrong order",
225e5c89e4eSSatish Balay   /*59 */ "Signal received",
226e5c89e4eSSatish Balay   /*60 */ "Nonconforming object sizes",
227e5c89e4eSSatish Balay           "Argument aliasing not permitted",
228e5c89e4eSSatish Balay           "Invalid argument",
229e5c89e4eSSatish Balay   /*63 */ "Argument out of range",
23023d101e3SBarry Smith           "Corrupt argument: see http://www.mcs.anl.gov/petsc/petsc-as/documentation/faq.html#valgrind",
231e5c89e4eSSatish Balay           "Unable to open file",
232e5c89e4eSSatish Balay           "Read from file failed",
233e5c89e4eSSatish Balay           "Write to file failed",
234e5c89e4eSSatish Balay           "Invalid pointer",
235e5c89e4eSSatish Balay   /*69 */ "Arguments must have same type",
236e5c89e4eSSatish Balay           "",
23723d101e3SBarry Smith   /*71 */ "Detected zero pivot in LU factorization\nsee http://www.mcs.anl.gov/petsc/petsc-as/documentation/faq.html#ZeroPivot",
238e5c89e4eSSatish Balay   /*72 */ "Floating point exception",
239e5c89e4eSSatish Balay   /*73 */ "Object is in wrong state",
240e5c89e4eSSatish Balay           "Corrupted Petsc object",
241e5c89e4eSSatish Balay           "Arguments are incompatible",
242e5c89e4eSSatish Balay           "Error in external library",
243e5c89e4eSSatish Balay   /*77 */ "Petsc has generated inconsistent data",
244e5c89e4eSSatish Balay           "Memory corruption",
245e5c89e4eSSatish Balay           "Unexpected data in file",
246e5c89e4eSSatish Balay   /*80 */ "Arguments must have same communicators",
24723d101e3SBarry Smith   /*81 */ "Detected zero pivot in Cholesky factorization\nsee http://www.mcs.anl.gov/petsc/petsc-as/documentation/faq.html#ZeroPivot",
248e5c89e4eSSatish Balay           "  ",
249e5c89e4eSSatish Balay           "  ",
250e5c89e4eSSatish Balay           "  ",
251e5c89e4eSSatish Balay   /*85 */ "Null argument, when expecting valid pointer",
252b2a1fa03SBarry Smith   /*86 */ "Unknown type. Check for miss-spelling or missing external package needed for type\n seehttp://www.mcs.anl.gov/petsc/petsc-as/documentation/installation.html#external",
2538cda6cd7SBarry Smith   /*87 */ "Not used",
2548cda6cd7SBarry Smith   /*88 */ "Error in system call",
25523d101e3SBarry Smith   /*89 */ "Object Type not set: see http://www.mcs.anl.gov/petsc/petsc-as/documentation/faq.html#objecttypenotset"};
256e5c89e4eSSatish Balay 
257e5c89e4eSSatish Balay #undef __FUNCT__
258e5c89e4eSSatish Balay #define __FUNCT__ "PetscErrorMessage"
259e5c89e4eSSatish Balay /*@C
260e5c89e4eSSatish Balay    PetscErrorMessage - returns the text string associated with a PETSc error code.
261e5c89e4eSSatish Balay 
262e5c89e4eSSatish Balay    Not Collective
263e5c89e4eSSatish Balay 
264e5c89e4eSSatish Balay    Input Parameter:
265e5c89e4eSSatish Balay .   errnum - the error code
266e5c89e4eSSatish Balay 
267e5c89e4eSSatish Balay    Output Parameter:
268e5c89e4eSSatish Balay +  text - the error message (PETSC_NULL if not desired)
269e5c89e4eSSatish Balay -  specific - the specific error message that was set with SETERRxxx() or PetscError().  (PETSC_NULL if not desired)
270e5c89e4eSSatish Balay 
271e5c89e4eSSatish Balay    Level: developer
272e5c89e4eSSatish Balay 
273e5c89e4eSSatish Balay    Concepts: error handler^messages
274e5c89e4eSSatish Balay 
275e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
276e5c89e4eSSatish Balay           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
277e5c89e4eSSatish Balay  @*/
278e5c89e4eSSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscErrorMessage(int errnum,const char *text[],char **specific)
279e5c89e4eSSatish Balay {
280e5c89e4eSSatish Balay   PetscFunctionBegin;
2812a6744ebSBarry Smith   if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) {
282c2f951e6SSatish Balay     *text = PetscErrorStrings[errnum-PETSC_ERR_MIN_VALUE-1];
283e5c89e4eSSatish Balay   } else if (text) *text = 0;
284e5c89e4eSSatish Balay 
285e5c89e4eSSatish Balay   if (specific) {
286e5c89e4eSSatish Balay     *specific = PetscErrorBaseMessage;
287e5c89e4eSSatish Balay   }
288e5c89e4eSSatish Balay   PetscFunctionReturn(0);
289e5c89e4eSSatish Balay }
290e5c89e4eSSatish Balay 
291e5c89e4eSSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
292e5c89e4eSSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX] = {0};
293e5c89e4eSSatish Balay PetscInt       PETSC_DLLEXPORT PetscErrorUncatchableCount                  = 0;
294e5c89e4eSSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscExceptions[PETSC_EXCEPTIONS_MAX]       = {0};
295e5c89e4eSSatish Balay PetscInt       PETSC_DLLEXPORT PetscExceptionsCount                        = 0;
296e5c89e4eSSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscExceptionTmp                           = 0;
2977b0215c6SHong Zhang PetscErrorCode PETSC_DLLEXPORT PetscExceptionTmp1                          = 0;
298e5c89e4eSSatish Balay 
299e5c89e4eSSatish Balay #undef __FUNCT__
300e5c89e4eSSatish Balay #define __FUNCT__ "PetscErrorIsCatchable"
30121dd7e22SBarry Smith /*@C
30221dd7e22SBarry Smith       PetscErrorIsCatchable - Returns if a PetscErrorCode can be caught with a PetscExceptionTry1() or
30321dd7e22SBarry Smith            PetscExceptionPush()
30421dd7e22SBarry Smith 
30521dd7e22SBarry Smith   Input Parameters:
30621dd7e22SBarry Smith .   err - error code
30721dd7e22SBarry Smith 
30821dd7e22SBarry Smith   Level: advanced
30921dd7e22SBarry Smith 
31021dd7e22SBarry Smith    Notes:
31121dd7e22SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
31221dd7e22SBarry Smith 
31321dd7e22SBarry Smith .seealso: PetscExceptionTry1(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop(), PetscErrorSetCatchable()
31421dd7e22SBarry Smith @*/
31521dd7e22SBarry Smith PetscTruth PETSC_DLLEXPORT PetscErrorIsCatchable(PetscErrorCode err)
316e5c89e4eSSatish Balay {
317e5c89e4eSSatish Balay   PetscInt i;
318e5c89e4eSSatish Balay   for (i=0; i<PetscErrorUncatchableCount; i++) {
319e5c89e4eSSatish Balay     if (err == PetscErrorUncatchable[i]) return PETSC_FALSE;
320e5c89e4eSSatish Balay   }
321e5c89e4eSSatish Balay   return PETSC_TRUE;
322e5c89e4eSSatish Balay }
323e5c89e4eSSatish Balay 
324e5c89e4eSSatish Balay #undef __FUNCT__
325e5c89e4eSSatish Balay #define __FUNCT__ "PetscErrorSetCatchable"
326e5c89e4eSSatish Balay /*@
327e5c89e4eSSatish Balay       PetscErrorSetCatchable - Sets if a PetscErrorCode can be caught with a PetscExceptionTry1()
32821dd7e22SBarry Smith     PetscExceptionCaught() pair, or PetscExceptionPush(). By default all errors are catchable.
329e5c89e4eSSatish Balay 
330e5c89e4eSSatish Balay   Input Parameters:
331e5c89e4eSSatish Balay +   err - error code
332e5c89e4eSSatish Balay -   flg - PETSC_TRUE means allow to be caught, PETSC_FALSE means do not allow to be caught
333e5c89e4eSSatish Balay 
334e5c89e4eSSatish Balay   Level: advanced
335e5c89e4eSSatish Balay 
336e5c89e4eSSatish Balay    Notes:
337e5c89e4eSSatish Balay     PETSc must not be configured using the option --with-errorchecking=0 for this to work
338e5c89e4eSSatish Balay 
33921dd7e22SBarry Smith .seealso: PetscExceptionTry1(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop(), PetscErrorIsCatchable()
340e5c89e4eSSatish Balay @*/
341e5c89e4eSSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscErrorSetCatchable(PetscErrorCode err,PetscTruth flg)
342e5c89e4eSSatish Balay {
343e5c89e4eSSatish Balay   PetscFunctionBegin;
344e5c89e4eSSatish Balay   if (!flg && PetscErrorIsCatchable(err)) {
345e5c89e4eSSatish Balay     /* add to list of uncatchable */
346e32f2f54SBarry Smith     if (PetscErrorUncatchableCount >= PETSC_EXCEPTIONS_MAX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Stack for PetscErrorUncatchable is overflowed, recompile \nsrc/sysd/error/err.c with a larger value for PETSC_EXCEPTIONS_MAX");
347e5c89e4eSSatish Balay     PetscErrorUncatchable[PetscErrorUncatchableCount++] = err;
348e5c89e4eSSatish Balay   } else if (flg && !PetscErrorIsCatchable(err)) {
349e5c89e4eSSatish Balay     /* remove from list of uncatchable */
350e5c89e4eSSatish Balay     PetscInt i;
351e5c89e4eSSatish Balay     for (i=0; i<PetscErrorUncatchableCount; i++) {
352e5c89e4eSSatish Balay       if (PetscErrorUncatchable[i] == err) break;
353e5c89e4eSSatish Balay     }
354e5c89e4eSSatish Balay     for (;i<PetscErrorUncatchableCount; i++) {
355e5c89e4eSSatish Balay       PetscErrorUncatchable[i] = PetscErrorUncatchable[i+1];
356e5c89e4eSSatish Balay     }
357e5c89e4eSSatish Balay     PetscErrorUncatchableCount--;
358e5c89e4eSSatish Balay   }
359e5c89e4eSSatish Balay   PetscFunctionReturn(0);
360e5c89e4eSSatish Balay }
361e5c89e4eSSatish Balay 
362e5c89e4eSSatish Balay #undef __FUNCT__
363e5c89e4eSSatish Balay #define __FUNCT__ "PetscExceptionPush"
36421dd7e22SBarry Smith /*@
36521dd7e22SBarry Smith       PetscExceptionPush - Adds the exception as one to be caught and passed up. If passed up
36621dd7e22SBarry Smith         can be checked with PetscExceptionCaught() or PetscExceptionValue()
36721dd7e22SBarry Smith 
36821dd7e22SBarry Smith   Input Parameters:
36921dd7e22SBarry Smith .   err - the exception to catch
37021dd7e22SBarry Smith 
37121dd7e22SBarry Smith   Level: advanced
37221dd7e22SBarry Smith 
37321dd7e22SBarry Smith    Notes:
37421dd7e22SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
37521dd7e22SBarry Smith 
37621dd7e22SBarry Smith     Use PetscExceptionPop() to remove this as a value to be caught
37721dd7e22SBarry Smith 
37821dd7e22SBarry Smith     This is not usually needed in C/C++ rather use PetscExceptionTry1()
37921dd7e22SBarry Smith 
38021dd7e22SBarry Smith .seealso: PetscExceptionTry1(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop()
38121dd7e22SBarry Smith @*/
382e5c89e4eSSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscExceptionPush(PetscErrorCode err)
383e5c89e4eSSatish Balay {
384e5c89e4eSSatish Balay   PetscFunctionBegin;
385e32f2f54SBarry Smith   if (PetscExceptionsCount >= PETSC_EXCEPTIONS_MAX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Stack for PetscExceptions is overflowed, recompile \nsrc/sysd/error/err.c with a larger value for PETSC_EXCEPTIONS_MAX");
386e5c89e4eSSatish Balay   if (PetscErrorIsCatchable(err)) PetscExceptions[PetscExceptionsCount++] = err;
387e5c89e4eSSatish Balay   PetscFunctionReturn(0);
388e5c89e4eSSatish Balay }
389e5c89e4eSSatish Balay 
390e5c89e4eSSatish Balay #undef __FUNCT__
391e5c89e4eSSatish Balay #define __FUNCT__ "PetscExceptionPop"
39221dd7e22SBarry Smith /*@
39321dd7e22SBarry Smith       PetscExceptionPop - Removes  the most recent exception asked to be caught with PetscExceptionPush()
39421dd7e22SBarry Smith 
39521dd7e22SBarry Smith   Input Parameters:
39621dd7e22SBarry Smith .   err - the exception that was pushed
39721dd7e22SBarry Smith 
39821dd7e22SBarry Smith   Level: advanced
39921dd7e22SBarry Smith 
40021dd7e22SBarry Smith    Notes:
40121dd7e22SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
40221dd7e22SBarry Smith 
40321dd7e22SBarry Smith     This is not usually needed in C/C++ rather use PetscExceptionTry1()
40421dd7e22SBarry Smith 
40521dd7e22SBarry Smith .seealso: PetscExceptionTry1(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop()
40621dd7e22SBarry Smith @*/
4071853238fSBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscExceptionPop(PetscErrorCode err)
408e5c89e4eSSatish Balay {
4091853238fSBarry Smith   PetscFunctionBegin;
410e32f2f54SBarry Smith   if (PetscExceptionsCount <= 0)SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Stack for PetscExceptions is empty");
411e5c89e4eSSatish Balay   if (PetscErrorIsCatchable(err)) PetscExceptionsCount--;
4121853238fSBarry Smith   PetscFunctionReturn(0);
413e5c89e4eSSatish Balay }
414e5c89e4eSSatish Balay #endif
415e5c89e4eSSatish Balay 
416e5c89e4eSSatish Balay #undef __FUNCT__
417e5c89e4eSSatish Balay #define __FUNCT__ "PetscError"
418e5c89e4eSSatish Balay /*@C
419e5c89e4eSSatish Balay    PetscError - Routine that is called when an error has been detected,
420e32f2f54SBarry Smith    usually called through the macro SETERRQ(PETSC_COMM_SELF,).
421e5c89e4eSSatish Balay 
422e5c89e4eSSatish Balay    Not Collective
423e5c89e4eSSatish Balay 
424e5c89e4eSSatish Balay    Input Parameters:
425e32f2f54SBarry Smith +  comm - communicator over which error occurred.  ALL ranks of this communicator MUST call this routine
426e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
427e5c89e4eSSatish Balay .  func - the function where the error occured (indicated by __FUNCT__)
428e5c89e4eSSatish Balay .  dir - the directory of file (indicated by __SDIR__)
429e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
430e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
431e5c89e4eSSatish Balay .  n - the generic error number
432e5c89e4eSSatish Balay .  p - 1 indicates the error was initially detected, 0 indicates this is a traceback from a
433e5c89e4eSSatish Balay    previously detected error
434e5c89e4eSSatish Balay -  mess - formatted message string - aka printf
435e5c89e4eSSatish Balay 
436e5c89e4eSSatish Balay   Level: intermediate
437e5c89e4eSSatish Balay 
438e5c89e4eSSatish Balay    Notes:
439e5c89e4eSSatish Balay    Most users need not directly use this routine and the error handlers, but
440e5c89e4eSSatish Balay    can instead use the simplified interface SETERRQ, which has the calling
441e5c89e4eSSatish Balay    sequence
442e32f2f54SBarry Smith $     SETERRQ(comm,n,mess)
443e5c89e4eSSatish Balay 
444e5c89e4eSSatish Balay    Experienced users can set the error handler with PetscPushErrorHandler().
445e5c89e4eSSatish Balay 
446e5c89e4eSSatish Balay    Concepts: error^setting condition
447e5c89e4eSSatish Balay 
448e5c89e4eSSatish Balay .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), SETERRQ(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
449e5c89e4eSSatish Balay @*/
450*668f157eSBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscError(MPI_Comm comm,int line,const char *func,const char* file,const char *dir,PetscErrorCode n,PetscErrorType p,const char *mess,...)
451e5c89e4eSSatish Balay {
452e5c89e4eSSatish Balay   va_list        Argp;
4532d609e63SMatthew Knepley   int            fullLength;
454e5c89e4eSSatish Balay   PetscErrorCode ierr;
455e5c89e4eSSatish Balay   char           buf[2048],*lbuf = 0;
456e5c89e4eSSatish Balay   PetscTruth     ismain,isunknown;
457e5c89e4eSSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
458e5c89e4eSSatish Balay   PetscInt       i;
459e5c89e4eSSatish Balay #endif
460e5c89e4eSSatish Balay 
461e5c89e4eSSatish Balay   if (!func)  func = "User provided function";
462e5c89e4eSSatish Balay   if (!file)  file = "User file";
463e5c89e4eSSatish Balay   if (!dir)   dir = " ";
464e5c89e4eSSatish Balay 
465e5c89e4eSSatish Balay   PetscFunctionBegin;
466e5c89e4eSSatish Balay   /* Compose the message evaluating the print format */
467e5c89e4eSSatish Balay   if (mess) {
468e5c89e4eSSatish Balay     va_start(Argp,mess);
4692d609e63SMatthew Knepley     PetscVSNPrintf(buf,2048,mess,&fullLength,Argp);
470e5c89e4eSSatish Balay     va_end(Argp);
471e5c89e4eSSatish Balay     lbuf = buf;
472e5c89e4eSSatish Balay     if (p == 1) {
473e5c89e4eSSatish Balay       PetscStrncpy(PetscErrorBaseMessage,lbuf,1023);
474e5c89e4eSSatish Balay     }
475e5c89e4eSSatish Balay   }
476e5c89e4eSSatish Balay 
477e5c89e4eSSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
478e5c89e4eSSatish Balay   /* check if user is catching this exception */
479e5c89e4eSSatish Balay   for (i=0; i<PetscExceptionsCount; i++) {
480e5c89e4eSSatish Balay     if (n == PetscExceptions[i])  PetscFunctionReturn(n);
481e5c89e4eSSatish Balay   }
482e5c89e4eSSatish Balay #endif
483e5c89e4eSSatish Balay 
484e32f2f54SBarry Smith   if (!eh)     ierr = PetscTraceBackErrorHandler(comm,line,func,file,dir,n,p,lbuf,0);
485e32f2f54SBarry Smith   else         ierr = (*eh->handler)(comm,line,func,file,dir,n,p,lbuf,eh->ctx);
486e5c89e4eSSatish Balay 
487e5c89e4eSSatish Balay   /*
488e5c89e4eSSatish Balay       If this is called from the main() routine we call MPI_Abort() instead of
489e5c89e4eSSatish Balay     return to allow the parallel program to be properly shutdown.
490e5c89e4eSSatish Balay 
491e5c89e4eSSatish Balay     Since this is in the error handler we don't check the errors below. Of course,
492e5c89e4eSSatish Balay     PetscStrncmp() does its own error checking which is problamatic
493e5c89e4eSSatish Balay   */
494e5c89e4eSSatish Balay   PetscStrncmp(func,"main",4,&ismain);
495e5c89e4eSSatish Balay   PetscStrncmp(func,"unknown",7,&isunknown);
496e5c89e4eSSatish Balay   if (ismain || isunknown) {
497e5c89e4eSSatish Balay     MPI_Abort(PETSC_COMM_WORLD,(int)ierr);
498e5c89e4eSSatish Balay   }
499e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
500e5c89e4eSSatish Balay }
501e5c89e4eSSatish Balay 
5020577ce7cSSatish Balay #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_EXTERN_CXX)
503fd705b32SMatthew Knepley #undef __FUNCT__
504fd705b32SMatthew Knepley #define __FUNCT__ "PetscErrorCxx"
505fd705b32SMatthew Knepley /*@C
506fd705b32SMatthew Knepley    PetscErrorCxx - Routine that is called when an error has been detected,
507fd705b32SMatthew Knepley    usually called through the macro SETERROR().
508fd705b32SMatthew Knepley 
509fd705b32SMatthew Knepley    Not Collective
510fd705b32SMatthew Knepley 
511fd705b32SMatthew Knepley    Input Parameters:
512e32f2f54SBarry Smith +  comm - communicator over which the error occurred
513e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
514fd705b32SMatthew Knepley .  func - the function where the error occured (indicated by __FUNCT__)
515fd705b32SMatthew Knepley .  dir - the directory of file (indicated by __SDIR__)
516fd705b32SMatthew Knepley .  file - the file in which the error was detected (indicated by __FILE__)
517fd705b32SMatthew Knepley .  n - the generic error number
518fd705b32SMatthew Knepley .  p - 1 indicates the error was initially detected, 0 indicates this is a traceback from a
519fd705b32SMatthew Knepley    previously detected error
520fd705b32SMatthew Knepley 
521fd705b32SMatthew Knepley   Level: intermediate
522fd705b32SMatthew Knepley 
523fd705b32SMatthew Knepley    Notes:
524fd705b32SMatthew Knepley    Most users need not directly use this routine and the error handlers, but
525fd705b32SMatthew Knepley    can instead use the simplified interface SETERRQ, which has the calling
526fd705b32SMatthew Knepley    sequence
527e32f2f54SBarry Smith $     SETERRQ(comm,n,mess)
528fd705b32SMatthew Knepley 
529fd705b32SMatthew Knepley    Experienced users can set the error handler with PetscPushErrorHandler().
530fd705b32SMatthew Knepley 
531fd705b32SMatthew Knepley    Concepts: error^setting condition
532fd705b32SMatthew Knepley 
533fd705b32SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), SETERRQ(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
534fd705b32SMatthew Knepley @*/
535*668f157eSBarry Smith void PETSC_DLLEXPORT PetscErrorCxx(MPI_Comm comm,int line,const char *func,const char* file,const char *dir,PetscErrorCode n,PetscErrorType p)
536fd705b32SMatthew Knepley {
537fd705b32SMatthew Knepley   PetscTruth ismain, isunknown;
538fd705b32SMatthew Knepley #if 0
539fd705b32SMatthew Knepley #if defined(PETSC_USE_ERRORCHECKING)
540fd705b32SMatthew Knepley   PetscInt   i;
541fd705b32SMatthew Knepley #endif
542fd705b32SMatthew Knepley #endif
543fd705b32SMatthew Knepley 
544fd705b32SMatthew Knepley   if (!func) func = "User provided function";
545fd705b32SMatthew Knepley   if (!file) file = "User file";
546fd705b32SMatthew Knepley   if (!dir)  dir  = " ";
547fd705b32SMatthew Knepley 
548fd705b32SMatthew Knepley #if 0
549fd705b32SMatthew Knepley #if defined(PETSC_USE_ERRORCHECKING)
550fd705b32SMatthew Knepley   /* check if user is catching this exception */
551fd705b32SMatthew Knepley   for (i=0; i<PetscExceptionsCount; i++) {
552fd705b32SMatthew Knepley     if (n == PetscExceptions[i])  PetscFunctionReturn(n);
553fd705b32SMatthew Knepley   }
554fd705b32SMatthew Knepley #endif
555fd705b32SMatthew Knepley #endif
556fd705b32SMatthew Knepley 
557fd705b32SMatthew Knepley   std::ostringstream msg;
558fd705b32SMatthew Knepley 
559fc3de776SJed Brown   PetscTraceBackErrorHandlerCxx(PETSC_COMM_SELF,line, func, file, dir, n, p, msg);
560fd705b32SMatthew Knepley 
561fd705b32SMatthew Knepley   /*
562fd705b32SMatthew Knepley       If this is called from the main() routine we call MPI_Abort() instead of
563fd705b32SMatthew Knepley     return to allow the parallel program to be properly shutdown.
564fd705b32SMatthew Knepley 
565fd705b32SMatthew Knepley     Since this is in the error handler we don't check the errors below. Of course,
566fd705b32SMatthew Knepley     PetscStrncmp() does its own error checking which is problamatic
567fd705b32SMatthew Knepley   */
568fd705b32SMatthew Knepley   PetscStrncmp(func,"main",4,&ismain);
569fd705b32SMatthew Knepley   PetscStrncmp(func,"unknown",7,&isunknown);
570fd705b32SMatthew Knepley   if (ismain || isunknown) {
571fd705b32SMatthew Knepley     MPI_Abort(PETSC_COMM_WORLD, (int) n);
572fd705b32SMatthew Knepley   }
573f3afd0ebSMatthew Knepley   throw PETSc::Exception(msg.str().c_str());
574fd705b32SMatthew Knepley }
575fd705b32SMatthew Knepley #endif
576fd705b32SMatthew Knepley 
577e5c89e4eSSatish Balay /* -------------------------------------------------------------------------*/
578e5c89e4eSSatish Balay 
579e5c89e4eSSatish Balay #undef __FUNCT__
580e5c89e4eSSatish Balay #define __FUNCT__ "PetscIntView"
581e5c89e4eSSatish Balay /*@C
582e5c89e4eSSatish Balay     PetscIntView - Prints an array of integers; useful for debugging.
583e5c89e4eSSatish Balay 
584e5c89e4eSSatish Balay     Collective on PetscViewer
585e5c89e4eSSatish Balay 
586e5c89e4eSSatish Balay     Input Parameters:
587e5c89e4eSSatish Balay +   N - number of integers in array
588e5c89e4eSSatish Balay .   idx - array of integers
589e5c89e4eSSatish Balay -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
590e5c89e4eSSatish Balay 
591e5c89e4eSSatish Balay   Level: intermediate
592e5c89e4eSSatish Balay 
593300a7f5bSBarry Smith     Developer Notes: idx cannot be const because may be passed to binary viewer where byte swappping is done
594300a7f5bSBarry Smith 
595e5c89e4eSSatish Balay .seealso: PetscRealView()
596e5c89e4eSSatish Balay @*/
5974e6ed268SMatthew Knepley PetscErrorCode PETSC_DLLEXPORT PetscIntView(PetscInt N,const PetscInt idx[],PetscViewer viewer)
598e5c89e4eSSatish Balay {
599e5c89e4eSSatish Balay   PetscErrorCode ierr;
600e5c89e4eSSatish Balay   PetscInt       j,i,n = N/20,p = N % 20;
6016805f65bSBarry Smith   PetscTruth     iascii,isbinary;
602e5c89e4eSSatish Balay   MPI_Comm       comm;
603e5c89e4eSSatish Balay 
604e5c89e4eSSatish Balay   PetscFunctionBegin;
605e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
6060700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3);
6075ab33896SBarry Smith   if (N) PetscValidIntPointer(idx,2);
608e5c89e4eSSatish Balay   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
609e5c89e4eSSatish Balay 
610e5c89e4eSSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
6116805f65bSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
612e5c89e4eSSatish Balay   if (iascii) {
613e5c89e4eSSatish Balay     for (i=0; i<n; i++) {
614e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*i);CHKERRQ(ierr);
615e5c89e4eSSatish Balay       for (j=0; j<20; j++) {
616e5c89e4eSSatish Balay         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[i*20+j]);CHKERRQ(ierr);
617e5c89e4eSSatish Balay       }
618e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
619e5c89e4eSSatish Balay     }
620e5c89e4eSSatish Balay     if (p) {
621e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*n);CHKERRQ(ierr);
622e5c89e4eSSatish Balay       for (i=0; i<p; i++) { ierr = PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[20*n+i]);CHKERRQ(ierr);}
623e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
624e5c89e4eSSatish Balay     }
625e5c89e4eSSatish Balay     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6266805f65bSBarry Smith   } else if (isbinary) {
6271cbcd9c4SBarry Smith     PetscMPIInt rank,size,*sizes,Ntotal,*displs, NN = PetscMPIIntCast(N);
628e5c89e4eSSatish Balay     PetscInt    *array;
629e5c89e4eSSatish Balay     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
630e5c89e4eSSatish Balay     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
631e5c89e4eSSatish Balay 
632e5c89e4eSSatish Balay     if (size > 1) {
633e5c89e4eSSatish Balay       if (rank) {
634e5c89e4eSSatish Balay         ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr);
635300a7f5bSBarry Smith         ierr = MPI_Gatherv((void*)idx,NN,MPIU_INT,0,0,0,MPIU_INT,0,comm);CHKERRQ(ierr);
636e5c89e4eSSatish Balay       } else {
637e5c89e4eSSatish Balay 	ierr      = PetscMalloc(size*sizeof(PetscMPIInt),&sizes);CHKERRQ(ierr);
638e5c89e4eSSatish Balay         ierr      = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPIU_INT,0,comm);CHKERRQ(ierr);
639e5c89e4eSSatish Balay         Ntotal    = sizes[0];
640e5c89e4eSSatish Balay 	ierr      = PetscMalloc(size*sizeof(PetscMPIInt),&displs);CHKERRQ(ierr);
641e5c89e4eSSatish Balay         displs[0] = 0;
642e5c89e4eSSatish Balay         for (i=1; i<size; i++) {
643e5c89e4eSSatish Balay           Ntotal    += sizes[i];
644e5c89e4eSSatish Balay           displs[i] =  displs[i-1] + sizes[i-1];
645e5c89e4eSSatish Balay         }
646e5c89e4eSSatish Balay 	ierr = PetscMalloc(Ntotal*sizeof(PetscInt),&array);CHKERRQ(ierr);
647300a7f5bSBarry Smith         ierr = MPI_Gatherv((void*)idx,NN,MPIU_INT,array,sizes,displs,MPIU_INT,0,comm);CHKERRQ(ierr);
6486805f65bSBarry Smith         ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
649e5c89e4eSSatish Balay         ierr = PetscFree(sizes);CHKERRQ(ierr);
650e5c89e4eSSatish Balay         ierr = PetscFree(displs);CHKERRQ(ierr);
651e5c89e4eSSatish Balay         ierr = PetscFree(array);CHKERRQ(ierr);
652e5c89e4eSSatish Balay       }
653e5c89e4eSSatish Balay     } else {
65447b06e05SMatthew Knepley       ierr = PetscViewerBinaryWrite(viewer,(void *) idx,N,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
655e5c89e4eSSatish Balay     }
656e5c89e4eSSatish Balay   } else {
657e5c89e4eSSatish Balay     const char *tname;
658e5c89e4eSSatish Balay     ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr);
659e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
660e5c89e4eSSatish Balay   }
661e5c89e4eSSatish Balay   PetscFunctionReturn(0);
662e5c89e4eSSatish Balay }
663e5c89e4eSSatish Balay 
664e5c89e4eSSatish Balay #undef __FUNCT__
665e5c89e4eSSatish Balay #define __FUNCT__ "PetscRealView"
666e5c89e4eSSatish Balay /*@C
667e5c89e4eSSatish Balay     PetscRealView - Prints an array of doubles; useful for debugging.
668e5c89e4eSSatish Balay 
669e5c89e4eSSatish Balay     Collective on PetscViewer
670e5c89e4eSSatish Balay 
671e5c89e4eSSatish Balay     Input Parameters:
672e5c89e4eSSatish Balay +   N - number of doubles in array
673e5c89e4eSSatish Balay .   idx - array of doubles
674e5c89e4eSSatish Balay -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
675e5c89e4eSSatish Balay 
676e5c89e4eSSatish Balay   Level: intermediate
677e5c89e4eSSatish Balay 
678300a7f5bSBarry Smith     Developer Notes: idx cannot be const because may be passed to binary viewer where byte swappping is done
679300a7f5bSBarry Smith 
680e5c89e4eSSatish Balay .seealso: PetscIntView()
681e5c89e4eSSatish Balay @*/
6824e6ed268SMatthew Knepley PetscErrorCode PETSC_DLLEXPORT PetscRealView(PetscInt N,const PetscReal idx[],PetscViewer viewer)
683e5c89e4eSSatish Balay {
684e5c89e4eSSatish Balay   PetscErrorCode ierr;
685e5c89e4eSSatish Balay   PetscInt       j,i,n = N/5,p = N % 5;
6866805f65bSBarry Smith   PetscTruth     iascii,isbinary;
687e5c89e4eSSatish Balay   MPI_Comm       comm;
688e5c89e4eSSatish Balay 
689e5c89e4eSSatish Balay   PetscFunctionBegin;
690e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
6910700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3);
692e5c89e4eSSatish Balay   PetscValidScalarPointer(idx,2);
693e5c89e4eSSatish Balay   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
694e5c89e4eSSatish Balay 
695e5c89e4eSSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
6966805f65bSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
697e5c89e4eSSatish Balay   if (iascii) {
698e5c89e4eSSatish Balay     for (i=0; i<n; i++) {
699e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",5*i);CHKERRQ(ierr);
700e5c89e4eSSatish Balay       for (j=0; j<5; j++) {
701e5c89e4eSSatish Balay          ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",idx[i*5+j]);CHKERRQ(ierr);
702e5c89e4eSSatish Balay       }
703e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
704e5c89e4eSSatish Balay     }
705e5c89e4eSSatish Balay     if (p) {
706e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",5*n);CHKERRQ(ierr);
707e5c89e4eSSatish Balay       for (i=0; i<p; i++) { PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",idx[5*n+i]);CHKERRQ(ierr);}
708e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
709e5c89e4eSSatish Balay     }
710e5c89e4eSSatish Balay     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
7116805f65bSBarry Smith   } else if (isbinary) {
7121cbcd9c4SBarry Smith     PetscMPIInt rank,size,*sizes,*displs, Ntotal,NN = PetscMPIIntCast(N);
713e5c89e4eSSatish Balay     PetscReal   *array;
714e5c89e4eSSatish Balay 
715e5c89e4eSSatish Balay     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
716e5c89e4eSSatish Balay     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
717e5c89e4eSSatish Balay 
718e5c89e4eSSatish Balay     if (size > 1) {
719e5c89e4eSSatish Balay       if (rank) {
720e5c89e4eSSatish Balay         ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr);
721300a7f5bSBarry Smith         ierr = MPI_Gatherv((void*)idx,NN,MPI_DOUBLE,0,0,0,MPI_DOUBLE,0,comm);CHKERRQ(ierr);
722e5c89e4eSSatish Balay       } else {
723e5c89e4eSSatish Balay 	ierr   = PetscMalloc(size*sizeof(PetscMPIInt),&sizes);CHKERRQ(ierr);
724e5c89e4eSSatish Balay         ierr   = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr);
725e5c89e4eSSatish Balay         Ntotal = sizes[0];
726e5c89e4eSSatish Balay 	ierr   = PetscMalloc(size*sizeof(PetscMPIInt),&displs);CHKERRQ(ierr);
727e5c89e4eSSatish Balay         displs[0] = 0;
728e5c89e4eSSatish Balay         for (i=1; i<size; i++) {
729e5c89e4eSSatish Balay           Ntotal    += sizes[i];
730e5c89e4eSSatish Balay           displs[i] =  displs[i-1] + sizes[i-1];
731e5c89e4eSSatish Balay         }
732e5c89e4eSSatish Balay 	ierr = PetscMalloc(Ntotal*sizeof(PetscReal),&array);CHKERRQ(ierr);
733300a7f5bSBarry Smith         ierr = MPI_Gatherv((void*)idx,NN,MPI_DOUBLE,array,sizes,displs,MPI_DOUBLE,0,comm);CHKERRQ(ierr);
7346805f65bSBarry Smith         ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_REAL,PETSC_TRUE);CHKERRQ(ierr);
735e5c89e4eSSatish Balay         ierr = PetscFree(sizes);CHKERRQ(ierr);
736e5c89e4eSSatish Balay         ierr = PetscFree(displs);CHKERRQ(ierr);
737e5c89e4eSSatish Balay         ierr = PetscFree(array);CHKERRQ(ierr);
738e5c89e4eSSatish Balay       }
739e5c89e4eSSatish Balay     } else {
74047b06e05SMatthew Knepley       ierr = PetscViewerBinaryWrite(viewer,(void *) idx,N,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr);
741e5c89e4eSSatish Balay     }
742e5c89e4eSSatish Balay   } else {
743e5c89e4eSSatish Balay     const char *tname;
744e5c89e4eSSatish Balay     ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr);
745e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
746e5c89e4eSSatish Balay   }
747e5c89e4eSSatish Balay   PetscFunctionReturn(0);
748e5c89e4eSSatish Balay }
749e5c89e4eSSatish Balay 
750e5c89e4eSSatish Balay #undef __FUNCT__
751e5c89e4eSSatish Balay #define __FUNCT__ "PetscScalarView"
752e5c89e4eSSatish Balay /*@C
753e5c89e4eSSatish Balay     PetscScalarView - Prints an array of scalars; useful for debugging.
754e5c89e4eSSatish Balay 
755e5c89e4eSSatish Balay     Collective on PetscViewer
756e5c89e4eSSatish Balay 
757e5c89e4eSSatish Balay     Input Parameters:
758e5c89e4eSSatish Balay +   N - number of scalars in array
759e5c89e4eSSatish Balay .   idx - array of scalars
760e5c89e4eSSatish Balay -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
761e5c89e4eSSatish Balay 
762e5c89e4eSSatish Balay   Level: intermediate
763e5c89e4eSSatish Balay 
764300a7f5bSBarry Smith     Developer Notes: idx cannot be const because may be passed to binary viewer where byte swappping is done
765300a7f5bSBarry Smith 
766e5c89e4eSSatish Balay .seealso: PetscIntView(), PetscRealView()
767e5c89e4eSSatish Balay @*/
7684e6ed268SMatthew Knepley PetscErrorCode PETSC_DLLEXPORT PetscScalarView(PetscInt N,const PetscScalar idx[],PetscViewer viewer)
769e5c89e4eSSatish Balay {
770e5c89e4eSSatish Balay   PetscErrorCode ierr;
771e5c89e4eSSatish Balay   PetscInt       j,i,n = N/3,p = N % 3;
7726805f65bSBarry Smith   PetscTruth     iascii,isbinary;
773e5c89e4eSSatish Balay   MPI_Comm       comm;
774e5c89e4eSSatish Balay 
775e5c89e4eSSatish Balay   PetscFunctionBegin;
776e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
777e5c89e4eSSatish Balay   PetscValidHeader(viewer,3);
778e5c89e4eSSatish Balay   PetscValidScalarPointer(idx,2);
779e5c89e4eSSatish Balay   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
780e5c89e4eSSatish Balay 
781e5c89e4eSSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
7826805f65bSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
783e5c89e4eSSatish Balay   if (iascii) {
784e5c89e4eSSatish Balay     for (i=0; i<n; i++) {
785e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*i);CHKERRQ(ierr);
786e5c89e4eSSatish Balay       for (j=0; j<3; j++) {
787e5c89e4eSSatish Balay #if defined (PETSC_USE_COMPLEX)
788e5c89e4eSSatish Balay         ierr = PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)",
789e5c89e4eSSatish Balay                                  PetscRealPart(idx[i*3+j]),PetscImaginaryPart(idx[i*3+j]));CHKERRQ(ierr);
790e5c89e4eSSatish Balay #else
791e5c89e4eSSatish Balay         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",idx[i*3+j]);CHKERRQ(ierr);
792e5c89e4eSSatish Balay #endif
793e5c89e4eSSatish Balay       }
794e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
795e5c89e4eSSatish Balay     }
796e5c89e4eSSatish Balay     if (p) {
797e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*n);CHKERRQ(ierr);
798e5c89e4eSSatish Balay       for (i=0; i<p; i++) {
799e5c89e4eSSatish Balay #if defined (PETSC_USE_COMPLEX)
800e5c89e4eSSatish Balay         ierr = PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)",
801e5c89e4eSSatish Balay                                  PetscRealPart(idx[n*3+i]),PetscImaginaryPart(idx[n*3+i]));CHKERRQ(ierr);
802e5c89e4eSSatish Balay #else
803e5c89e4eSSatish Balay         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",idx[3*n+i]);CHKERRQ(ierr);
804e5c89e4eSSatish Balay #endif
805e5c89e4eSSatish Balay       }
806e5c89e4eSSatish Balay       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
807e5c89e4eSSatish Balay     }
808e5c89e4eSSatish Balay     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8096805f65bSBarry Smith   } else if (isbinary) {
8101cbcd9c4SBarry Smith     PetscMPIInt size,rank,*sizes,Ntotal,*displs,NN = PetscMPIIntCast(N);
811e5c89e4eSSatish Balay     PetscScalar *array;
812e5c89e4eSSatish Balay 
813e5c89e4eSSatish Balay     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
814e5c89e4eSSatish Balay     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
815e5c89e4eSSatish Balay 
816e5c89e4eSSatish Balay     if (size > 1) {
817e5c89e4eSSatish Balay       if (rank) {
818e5c89e4eSSatish Balay         ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr);
819300a7f5bSBarry Smith         ierr = MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,0,0,0,MPIU_SCALAR,0,comm);CHKERRQ(ierr);
820e5c89e4eSSatish Balay       } else {
821e5c89e4eSSatish Balay 	ierr   = PetscMalloc(size*sizeof(PetscMPIInt),&sizes);CHKERRQ(ierr);
822e5c89e4eSSatish Balay         ierr   = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr);
823e5c89e4eSSatish Balay         Ntotal = sizes[0];
824e5c89e4eSSatish Balay 	ierr   = PetscMalloc(size*sizeof(PetscMPIInt),&displs);CHKERRQ(ierr);
825e5c89e4eSSatish Balay         displs[0] = 0;
826e5c89e4eSSatish Balay         for (i=1; i<size; i++) {
827e5c89e4eSSatish Balay           Ntotal    += sizes[i];
828e5c89e4eSSatish Balay           displs[i] =  displs[i-1] + sizes[i-1];
829e5c89e4eSSatish Balay         }
830e5c89e4eSSatish Balay 	ierr = PetscMalloc(Ntotal*sizeof(PetscScalar),&array);CHKERRQ(ierr);
831300a7f5bSBarry Smith         ierr = MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,array,sizes,displs,MPIU_SCALAR,0,comm);CHKERRQ(ierr);
8326805f65bSBarry Smith         ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
833e5c89e4eSSatish Balay         ierr = PetscFree(sizes);CHKERRQ(ierr);
834e5c89e4eSSatish Balay         ierr = PetscFree(displs);CHKERRQ(ierr);
835e5c89e4eSSatish Balay         ierr = PetscFree(array);CHKERRQ(ierr);
836e5c89e4eSSatish Balay       }
837e5c89e4eSSatish Balay     } else {
83847b06e05SMatthew Knepley       ierr = PetscViewerBinaryWrite(viewer,(void *) idx,N,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
839e5c89e4eSSatish Balay     }
840e5c89e4eSSatish Balay   } else {
841e5c89e4eSSatish Balay     const char *tname;
842e5c89e4eSSatish Balay     ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr);
843e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
844e5c89e4eSSatish Balay   }
845e5c89e4eSSatish Balay   PetscFunctionReturn(0);
846e5c89e4eSSatish Balay }
847e5c89e4eSSatish Balay 
848e5c89e4eSSatish Balay 
849e5c89e4eSSatish Balay 
850e5c89e4eSSatish Balay 
851