xref: /petsc/src/sys/error/err.c (revision f560318ce50f43192aed8957aa022ffb5d44929d) !
1 
2 /*
3       Code that allows one to set the error handlers
4 */
5 #include <petsc/private/petscimpl.h>           /*I "petscsys.h" I*/
6 #include <petscviewer.h>
7 
8 typedef struct _EH *EH;
9 struct _EH {
10   PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
11   void           *ctx;
12   EH             previous;
13 };
14 
15 static EH eh = 0;
16 
17 /*@C
18    PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to
19     load the file where the error occured. Then calls the "previous" error handler.
20 
21    Not Collective
22 
23    Input Parameters:
24 +  comm - communicator over which error occured
25 .  line - the line number of the error (indicated by __LINE__)
26 .  file - the file in which the error was detected (indicated by __FILE__)
27 .  mess - an error text string, usually just printed to the screen
28 .  n - the generic error number
29 .  p - specific error number
30 -  ctx - error handler context
31 
32    Options Database Key:
33 .   -on_error_emacs <machinename>
34 
35    Level: developer
36 
37    Notes:
38    You must put (server-start) in your .emacs file for the emacsclient software to work
39 
40    Most users need not directly employ this routine and the other error
41    handlers, but can instead use the simplified interface SETERRQ, which has
42    the calling sequence
43 $     SETERRQ(PETSC_COMM_SELF,number,p,mess)
44 
45    Notes for experienced users:
46    Use PetscPushErrorHandler() to set the desired error handler.
47 
48    Developer Note: Since this is an error handler it cannot call CHKERRQ(); thus we just return if an error is detected.
49 
50    Concepts: emacs^going to on error
51    Concepts: error handler^going to line in emacs
52 
53 .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
54           PetscAbortErrorHandler()
55  @*/
56 PetscErrorCode  PetscEmacsClientErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
57 {
58   PetscErrorCode ierr;
59   char           command[PETSC_MAX_PATH_LEN];
60   const char     *pdir;
61   FILE           *fp;
62 
63   PetscFunctionBegin;
64   ierr = PetscGetPetscDir(&pdir);if (ierr) PetscFunctionReturn(ierr);
65   sprintf(command,"cd %s; emacsclient --no-wait +%d %s\n",pdir,line,file);
66 #if defined(PETSC_HAVE_POPEN)
67   ierr = PetscPOpen(MPI_COMM_WORLD,(char*)ctx,command,"r",&fp);if (ierr) PetscFunctionReturn(ierr);
68   ierr = PetscPClose(MPI_COMM_WORLD,fp);if (ierr) PetscFunctionReturn(ierr);
69 #else
70   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
71 #endif
72   ierr = PetscPopErrorHandler();if (ierr) PetscFunctionReturn(ierr); /* remove this handler from the stack of handlers */
73   if (!eh) {
74     ierr = PetscTraceBackErrorHandler(comm,line,fun,file,n,p,mess,0);if (ierr) PetscFunctionReturn(ierr);
75   } else {
76     ierr = (*eh->handler)(comm,line,fun,file,n,p,mess,eh->ctx);if (ierr) PetscFunctionReturn(ierr);
77   }
78   PetscFunctionReturn(ierr);
79 }
80 
81 /*@C
82    PetscPushErrorHandler - Sets a routine to be called on detection of errors.
83 
84    Not Collective
85 
86    Input Parameters:
87 +  handler - error handler routine
88 -  ctx - optional handler context that contains information needed by the handler (for
89          example file pointers for error messages etc.)
90 
91    Calling sequence of handler:
92 $    int handler(MPI_Comm comm,int line,char *func,char *file,PetscErrorCode n,int p,char *mess,void *ctx);
93 
94 +  comm - communicator over which error occured
95 .  line - the line number of the error (indicated by __LINE__)
96 .  file - the file in which the error was detected (indicated by __FILE__)
97 .  n - the generic error number (see list defined in include/petscerror.h)
98 .  p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT
99 .  mess - an error text string, usually just printed to the screen
100 -  ctx - the error handler context
101 
102    Options Database Keys:
103 +   -on_error_attach_debugger <noxterm,gdb or dbx>
104 -   -on_error_abort
105 
106    Level: intermediate
107 
108    Notes:
109    The currently available PETSc error handlers include PetscTraceBackErrorHandler(),
110    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler(), PetscReturnErrorHandler().
111 
112    Fortran Notes:
113     You can only push one error handler from Fortran before poping it.
114 
115 .seealso: PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), PetscTraceBackErrorHandler(), PetscPushSignalHandler()
116 
117 @*/
118 PetscErrorCode  PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void *ctx)
119 {
120   EH             neweh;
121   PetscErrorCode ierr;
122 
123   PetscFunctionBegin;
124   ierr = PetscNew(&neweh);CHKERRQ(ierr);
125   if (eh) neweh->previous = eh;
126   else    neweh->previous = 0;
127   neweh->handler = handler;
128   neweh->ctx     = ctx;
129   eh             = neweh;
130   PetscFunctionReturn(0);
131 }
132 
133 /*@
134    PetscPopErrorHandler - Removes the latest error handler that was
135    pushed with PetscPushErrorHandler().
136 
137    Not Collective
138 
139    Level: intermediate
140 
141    Concepts: error handler^setting
142 
143 .seealso: PetscPushErrorHandler()
144 @*/
145 PetscErrorCode  PetscPopErrorHandler(void)
146 {
147   EH             tmp;
148   PetscErrorCode ierr;
149 
150   PetscFunctionBegin;
151   if (!eh) PetscFunctionReturn(0);
152   tmp  = eh;
153   eh   = eh->previous;
154   ierr = PetscFree(tmp);CHKERRQ(ierr);
155   PetscFunctionReturn(0);
156 }
157 
158 /*@C
159   PetscReturnErrorHandler - Error handler that causes a return to the current
160   level.
161 
162    Not Collective
163 
164    Input Parameters:
165 +  comm - communicator over which error occurred
166 .  line - the line number of the error (indicated by __LINE__)
167 .  file - the file in which the error was detected (indicated by __FILE__)
168 .  mess - an error text string, usually just printed to the screen
169 .  n - the generic error number
170 .  p - specific error number
171 -  ctx - error handler context
172 
173    Level: developer
174 
175    Notes:
176    Most users need not directly employ this routine and the other error
177    handlers, but can instead use the simplified interface SETERRQ, which has
178    the calling sequence
179 $     SETERRQ(comm,number,mess)
180 
181    Notes for experienced users:
182    This routine is good for catching errors such as zero pivots in preconditioners
183    or breakdown of iterative methods. It is not appropriate for memory violations
184    and similar errors.
185 
186    Use PetscPushErrorHandler() to set the desired error handler.  The
187    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
188    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscAbortErrorHandler()
189 
190    Concepts: error handler
191 
192 .seealso:  PetscPushErrorHandler(), PetscPopErrorHandler().
193  @*/
194 
195 PetscErrorCode  PetscReturnErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
196 {
197   PetscFunctionBegin;
198   PetscFunctionReturn(n);
199 }
200 
201 static char PetscErrorBaseMessage[1024];
202 /*
203        The numerical values for these are defined in include/petscerror.h; any changes
204    there must also be made here
205 */
206 static const char *PetscErrorStrings[] = {
207   /*55 */ "Out of memory",
208           "No support for this operation for this object type",
209           "No support for this operation on this system",
210   /*58 */ "Operation done in wrong order",
211   /*59 */ "Signal received",
212   /*60 */ "Nonconforming object sizes",
213           "Argument aliasing not permitted",
214           "Invalid argument",
215   /*63 */ "Argument out of range",
216           "Corrupt argument: http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind",
217           "Unable to open file",
218           "Read from file failed",
219           "Write to file failed",
220           "Invalid pointer",
221   /*69 */ "Arguments must have same type",
222   /*70 */ "Attempt to use a pointer that does not point to a valid accessible location",
223   /*71 */ "Zero pivot in LU factorization: http://www.mcs.anl.gov/petsc/documentation/faq.html#zeropivot",
224   /*72 */ "Floating point exception",
225   /*73 */ "Object is in wrong state",
226           "Corrupted Petsc object",
227           "Arguments are incompatible",
228           "Error in external library",
229   /*77 */ "Petsc has generated inconsistent data",
230           "Memory corruption: http://www.mcs.anl.gov/petsc/documentation/installation.html#valgrind",
231           "Unexpected data in file",
232   /*80 */ "Arguments must have same communicators",
233   /*81 */ "Zero pivot in Cholesky factorization: http://www.mcs.anl.gov/petsc/documentation/faq.html#zeropivot",
234           "  ",
235           "  ",
236           "Overflow in integer operation: http://www.mcs.anl.gov/petsc/documentation/faq.html#64-bit-indices",
237   /*85 */ "Null argument, when expecting valid pointer",
238   /*86 */ "Unknown type. Check for miss-spelling or missing package: http://www.mcs.anl.gov/petsc/documentation/installation.html#external",
239   /*87 */ "MPI library at runtime is not compatible with MPI used at compile time",
240   /*88 */ "Error in system call",
241   /*89 */ "Object Type not set: http://www.mcs.anl.gov/petsc/documentation/faq.html#objecttypenotset",
242   /*90 */ "  ",
243   /*   */ "  ",
244   /*92 */ "See http://www.mcs.anl.gov/petsc/documentation/linearsolvertable.html for possible LU and Cholesky solvers",
245   /*93 */ "You cannot overwrite this option since that will conflict with other previously set options",
246 };
247 
248 /*@C
249    PetscErrorMessage - returns the text string associated with a PETSc error code.
250 
251    Not Collective
252 
253    Input Parameter:
254 .   errnum - the error code
255 
256    Output Parameter:
257 +  text - the error message (NULL if not desired)
258 -  specific - the specific error message that was set with SETERRxxx() or PetscError().  (NULL if not desired)
259 
260    Level: developer
261 
262    Concepts: error handler^messages
263 
264 .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscError(), SETERRQ(), CHKERRQ()
265           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
266  @*/
267 PetscErrorCode  PetscErrorMessage(int errnum,const char *text[],char **specific)
268 {
269   PetscFunctionBegin;
270   if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) *text = PetscErrorStrings[errnum-PETSC_ERR_MIN_VALUE-1];
271   else if (text) *text = 0;
272 
273   if (specific) *specific = PetscErrorBaseMessage;
274   PetscFunctionReturn(0);
275 }
276 
277 #if defined(PETSC_CLANGUAGE_CXX)
278 /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software
279  * would be broken if implementations did not handle it it some common cases. However, keep in mind
280  *
281  *   Rule 62. Don't allow exceptions to propagate across module boundaries
282  *
283  * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface
284  * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed.
285  *
286  * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message
287  * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that
288  * seems crazy to me.
289  */
290 #include <sstream>
291 #include <stdexcept>
292 static void PetscCxxErrorThrow() {
293   const char *str;
294   if (eh && eh->ctx) {
295     std::ostringstream *msg;
296     msg = (std::ostringstream*) eh->ctx;
297     str = msg->str().c_str();
298   } else str = "Error detected in C PETSc";
299 
300   throw std::runtime_error(str);
301 }
302 #endif
303 
304 /*@C
305    PetscError - Routine that is called when an error has been detected,
306    usually called through the macro SETERRQ(PETSC_COMM_SELF,).
307 
308    Not Collective
309 
310    Input Parameters:
311 +  comm - communicator over which error occurred.  ALL ranks of this communicator MUST call this routine
312 .  line - the line number of the error (indicated by __LINE__)
313 .  func - the function name in which the error was detected
314 .  file - the file in which the error was detected (indicated by __FILE__)
315 .  n - the generic error number
316 .  p - PETSC_ERROR_INITIAL indicates the error was initially detected, PETSC_ERROR_REPEAT indicates this is a traceback from a previously detected error
317 -  mess - formatted message string - aka printf
318 
319   Level: intermediate
320 
321    Notes:
322    Most users need not directly use this routine and the error handlers, but
323    can instead use the simplified interface SETERRQ, which has the calling
324    sequence
325 $     SETERRQ(comm,n,mess)
326 
327    Fortran Note:
328    This routine is used differently from Fortran
329 $    PetscError(MPI_Comm comm,PetscErrorCode n,PetscErrorType p,char *message)
330 
331    Experienced users can set the error handler with PetscPushErrorHandler().
332 
333    Developer Note: Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes)
334    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
335    but this annoying.
336 
337    Concepts: error^setting condition
338 
339 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), SETERRQ(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), PetscErrorMessage()
340 @*/
341 PetscErrorCode PetscError(MPI_Comm comm,int line,const char *func,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,...)
342 {
343   va_list        Argp;
344   size_t         fullLength;
345   char           buf[2048],*lbuf = 0;
346   PetscBool      ismain;
347   PetscErrorCode ierr;
348 
349   PetscFunctionBegin;
350   if (!func) func = "User provided function";
351   if (!file) file = "User file";
352   if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF;
353 
354   /* Compose the message evaluating the print format */
355   if (mess) {
356     va_start(Argp,mess);
357     PetscVSNPrintf(buf,2048,mess,&fullLength,Argp);
358     va_end(Argp);
359     lbuf = buf;
360     if (p == PETSC_ERROR_INITIAL) PetscStrncpy(PetscErrorBaseMessage,lbuf,1023);
361   }
362 
363   if (!eh) ierr = PetscTraceBackErrorHandler(comm,line,func,file,n,p,lbuf,0);
364   else     ierr = (*eh->handler)(comm,line,func,file,n,p,lbuf,eh->ctx);
365 
366   /*
367       If this is called from the main() routine we call MPI_Abort() instead of
368     return to allow the parallel program to be properly shutdown.
369 
370     Since this is in the error handler we don't check the errors below. Of course,
371     PetscStrncmp() does its own error checking which is problamatic
372   */
373   PetscStrncmp(func,"main",4,&ismain);
374   if (ismain) MPI_Abort(PETSC_COMM_WORLD,(int)ierr);
375 
376 #if defined(PETSC_CLANGUAGE_CXX)
377   if (p == PETSC_ERROR_IN_CXX) {
378     PetscCxxErrorThrow();
379   }
380 #endif
381   PetscFunctionReturn(ierr);
382 }
383 
384 /* -------------------------------------------------------------------------*/
385 
386 /*@C
387     PetscIntView - Prints an array of integers; useful for debugging.
388 
389     Collective on PetscViewer
390 
391     Input Parameters:
392 +   N - number of integers in array
393 .   idx - array of integers
394 -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
395 
396   Level: intermediate
397 
398     Developer Notes:
399     idx cannot be const because may be passed to binary viewer where byte swapping is done
400 
401 .seealso: PetscRealView()
402 @*/
403 PetscErrorCode  PetscIntView(PetscInt N,const PetscInt idx[],PetscViewer viewer)
404 {
405   PetscErrorCode ierr;
406   PetscInt       j,i,n = N/20,p = N % 20;
407   PetscBool      iascii,isbinary;
408   MPI_Comm       comm;
409 
410   PetscFunctionBegin;
411   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
412   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3);
413   if (N) PetscValidIntPointer(idx,2);
414   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
415 
416   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
417   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
418   if (iascii) {
419     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
420     for (i=0; i<n; i++) {
421       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*i);CHKERRQ(ierr);
422       for (j=0; j<20; j++) {
423         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[i*20+j]);CHKERRQ(ierr);
424       }
425       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
426     }
427     if (p) {
428       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*n);CHKERRQ(ierr);
429       for (i=0; i<p; i++) { ierr = PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[20*n+i]);CHKERRQ(ierr);}
430       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
431     }
432     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
433     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
434   } else if (isbinary) {
435     PetscMPIInt rank,size,*sizes,Ntotal,*displs,NN;
436     PetscInt    *array;
437 
438     ierr = PetscMPIIntCast(N,&NN);CHKERRQ(ierr);
439     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
440     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
441 
442     if (size > 1) {
443       if (rank) {
444         ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr);
445         ierr = MPI_Gatherv((void*)idx,NN,MPIU_INT,0,0,0,MPIU_INT,0,comm);CHKERRQ(ierr);
446       } else {
447         ierr      = PetscMalloc1(size,&sizes);CHKERRQ(ierr);
448         ierr      = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr);
449         Ntotal    = sizes[0];
450         ierr      = PetscMalloc1(size,&displs);CHKERRQ(ierr);
451         displs[0] = 0;
452         for (i=1; i<size; i++) {
453           Ntotal   += sizes[i];
454           displs[i] =  displs[i-1] + sizes[i-1];
455         }
456         ierr = PetscMalloc1(Ntotal,&array);CHKERRQ(ierr);
457         ierr = MPI_Gatherv((void*)idx,NN,MPIU_INT,array,sizes,displs,MPIU_INT,0,comm);CHKERRQ(ierr);
458         ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
459         ierr = PetscFree(sizes);CHKERRQ(ierr);
460         ierr = PetscFree(displs);CHKERRQ(ierr);
461         ierr = PetscFree(array);CHKERRQ(ierr);
462       }
463     } else {
464       ierr = PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
465     }
466   } else {
467     const char *tname;
468     ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr);
469     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
470   }
471   PetscFunctionReturn(0);
472 }
473 
474 /*@C
475     PetscRealView - Prints an array of doubles; useful for debugging.
476 
477     Collective on PetscViewer
478 
479     Input Parameters:
480 +   N - number of PetscReal in array
481 .   idx - array of PetscReal
482 -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
483 
484   Level: intermediate
485 
486     Developer Notes:
487     idx cannot be const because may be passed to binary viewer where byte swapping is done
488 
489 .seealso: PetscIntView()
490 @*/
491 PetscErrorCode  PetscRealView(PetscInt N,const PetscReal idx[],PetscViewer viewer)
492 {
493   PetscErrorCode ierr;
494   PetscInt       j,i,n = N/5,p = N % 5;
495   PetscBool      iascii,isbinary;
496   MPI_Comm       comm;
497 
498   PetscFunctionBegin;
499   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
500   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3);
501   PetscValidScalarPointer(idx,2);
502   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
503 
504   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
505   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
506   if (iascii) {
507     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
508     for (i=0; i<n; i++) {
509       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",(int)5*i);CHKERRQ(ierr);
510       for (j=0; j<5; j++) {
511         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*5+j]);CHKERRQ(ierr);
512       }
513       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
514     }
515     if (p) {
516       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",(int)5*n);CHKERRQ(ierr);
517       for (i=0; i<p; i++) { PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[5*n+i]);CHKERRQ(ierr);}
518       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
519     }
520     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
521     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
522   } else if (isbinary) {
523     PetscMPIInt rank,size,*sizes,*displs, Ntotal,NN;
524     PetscReal   *array;
525 
526     ierr = PetscMPIIntCast(N,&NN);CHKERRQ(ierr);
527     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
528     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
529 
530     if (size > 1) {
531       if (rank) {
532         ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr);
533         ierr = MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,0,0,0,MPIU_REAL,0,comm);CHKERRQ(ierr);
534       } else {
535         ierr      = PetscMalloc1(size,&sizes);CHKERRQ(ierr);
536         ierr      = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr);
537         Ntotal    = sizes[0];
538         ierr      = PetscMalloc1(size,&displs);CHKERRQ(ierr);
539         displs[0] = 0;
540         for (i=1; i<size; i++) {
541           Ntotal   += sizes[i];
542           displs[i] =  displs[i-1] + sizes[i-1];
543         }
544         ierr = PetscMalloc1(Ntotal,&array);CHKERRQ(ierr);
545         ierr = MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,array,sizes,displs,MPIU_REAL,0,comm);CHKERRQ(ierr);
546         ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_REAL,PETSC_TRUE);CHKERRQ(ierr);
547         ierr = PetscFree(sizes);CHKERRQ(ierr);
548         ierr = PetscFree(displs);CHKERRQ(ierr);
549         ierr = PetscFree(array);CHKERRQ(ierr);
550       }
551     } else {
552       ierr = PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr);
553     }
554   } else {
555     const char *tname;
556     ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr);
557     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
558   }
559   PetscFunctionReturn(0);
560 }
561 
562 /*@C
563     PetscScalarView - Prints an array of scalars; useful for debugging.
564 
565     Collective on PetscViewer
566 
567     Input Parameters:
568 +   N - number of scalars in array
569 .   idx - array of scalars
570 -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0
571 
572   Level: intermediate
573 
574     Developer Notes:
575     idx cannot be const because may be passed to binary viewer where byte swapping is done
576 
577 .seealso: PetscIntView(), PetscRealView()
578 @*/
579 PetscErrorCode  PetscScalarView(PetscInt N,const PetscScalar idx[],PetscViewer viewer)
580 {
581   PetscErrorCode ierr;
582   PetscInt       j,i,n = N/3,p = N % 3;
583   PetscBool      iascii,isbinary;
584   MPI_Comm       comm;
585 
586   PetscFunctionBegin;
587   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
588   PetscValidHeader(viewer,3);
589   if (N) PetscValidScalarPointer(idx,2);
590   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
591 
592   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
593   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
594   if (iascii) {
595     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
596     for (i=0; i<n; i++) {
597       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*i);CHKERRQ(ierr);
598       for (j=0; j<3; j++) {
599 #if defined(PETSC_USE_COMPLEX)
600         ierr = PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[i*3+j]),(double)PetscImaginaryPart(idx[i*3+j]));CHKERRQ(ierr);
601 #else
602         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*3+j]);CHKERRQ(ierr);
603 #endif
604       }
605       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
606     }
607     if (p) {
608       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*n);CHKERRQ(ierr);
609       for (i=0; i<p; i++) {
610 #if defined(PETSC_USE_COMPLEX)
611         ierr = PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[n*3+i]),(double)PetscImaginaryPart(idx[n*3+i]));CHKERRQ(ierr);
612 #else
613         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[3*n+i]);CHKERRQ(ierr);
614 #endif
615       }
616       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
617     }
618     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
619     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
620   } else if (isbinary) {
621     PetscMPIInt size,rank,*sizes,Ntotal,*displs,NN;
622     PetscScalar *array;
623 
624     ierr = PetscMPIIntCast(N,&NN);CHKERRQ(ierr);
625     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
626     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
627 
628     if (size > 1) {
629       if (rank) {
630         ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr);
631         ierr = MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,0,0,0,MPIU_SCALAR,0,comm);CHKERRQ(ierr);
632       } else {
633         ierr      = PetscMalloc1(size,&sizes);CHKERRQ(ierr);
634         ierr      = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr);
635         Ntotal    = sizes[0];
636         ierr      = PetscMalloc1(size,&displs);CHKERRQ(ierr);
637         displs[0] = 0;
638         for (i=1; i<size; i++) {
639           Ntotal   += sizes[i];
640           displs[i] =  displs[i-1] + sizes[i-1];
641         }
642         ierr = PetscMalloc1(Ntotal,&array);CHKERRQ(ierr);
643         ierr = MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,array,sizes,displs,MPIU_SCALAR,0,comm);CHKERRQ(ierr);
644         ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
645         ierr = PetscFree(sizes);CHKERRQ(ierr);
646         ierr = PetscFree(displs);CHKERRQ(ierr);
647         ierr = PetscFree(array);CHKERRQ(ierr);
648       }
649     } else {
650       ierr = PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
651     }
652   } else {
653     const char *tname;
654     ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr);
655     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
656   }
657   PetscFunctionReturn(0);
658 }
659 
660 
661 
662 
663