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