Actual source code: err.c

petsc-3.13.6 2020-09-29
Report Typos and Errors

  2: /*
  3:       Code that allows one to set the error handlers
  4: */
  5:  #include <petsc/private/petscimpl.h>
  6:  #include <petscviewer.h>

  8: /* A table of Petsc source files containing calls to PETSCABORT. We assume this table will
  9:    stay stable for a while. When things changed, we just need to add new files to the table.
 10:  */
 11: static const char* PetscAbortSourceFiles[] = {
 12:   "Souce code of main",          /* 0 */
 13:   "Not Found",                  /* 1, not found in petsc, but may be in users' code if they called PETSCABORT. */
 14:   "sys/error/adebug.c",
 15:   "src/sys/error/errstop.c",
 16:   "sys/error/fp.c",
 17:   "sys/error/signal.c",           /* 5 */
 18:   "sys/ftn-custom/zutils.c",
 19:   "sys/logging/utils/stagelog.c",
 20:   "sys/mpiuni/mpitime.c",
 21:   "sys/objects/init.c",
 22:   "sys/objects/pinit.c",            /* 10 */
 23:   "vec/vec/interface/dlregisvec.c",
 24:   "vec/vec/utils/comb.c"
 25: };

 27: /* Find index of the soure file where a PETSCABORT was called. */
 28: PetscErrorCode PetscAbortFindSourceFile_Private(const char* filepath, PetscInt *idx)
 29: {
 30:   PetscErrorCode  ierr;
 31:   PetscInt        i,n = sizeof(PetscAbortSourceFiles)/sizeof(PetscAbortSourceFiles[0]);
 32:   PetscBool       match;
 33:   char            subpath[256];

 37:   *idx = 1;
 38:   for (i=2; i<n; i++) {
 39:     PetscFixFilename(PetscAbortSourceFiles[i],subpath);
 40:     PetscStrendswith(filepath,subpath,&match);
 41:     if (match) {*idx = i; break;}
 42:   }
 43:   return(0);
 44: }

 46: typedef struct _EH *EH;
 47: struct _EH {
 48:   PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
 49:   void           *ctx;
 50:   EH             previous;
 51: };

 53: static EH eh = NULL;

 55: /*@C
 56:    PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to
 57:     load the file where the error occured. Then calls the "previous" error handler.

 59:    Not Collective

 61:    Input Parameters:
 62: +  comm - communicator over which error occured
 63: .  line - the line number of the error (indicated by __LINE__)
 64: .  file - the file in which the error was detected (indicated by __FILE__)
 65: .  mess - an error text string, usually just printed to the screen
 66: .  n - the generic error number
 67: .  p - specific error number
 68: -  ctx - error handler context

 70:    Options Database Key:
 71: .   -on_error_emacs <machinename>

 73:    Level: developer

 75:    Notes:
 76:    You must put (server-start) in your .emacs file for the emacsclient software to work

 78:    Most users need not directly employ this routine and the other error
 79:    handlers, but can instead use the simplified interface SETERRQ, which has
 80:    the calling sequence
 81: $     SETERRQ(PETSC_COMM_SELF,number,p,mess)

 83:    Notes for experienced users:
 84:    Use PetscPushErrorHandler() to set the desired error handler.

 86:    Developer Note:
 87:    Since this is an error handler it cannot call CHKERRQ(); thus we just return if an error is detected.


 90: .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
 91:           PetscAbortErrorHandler()
 92:  @*/
 93: PetscErrorCode  PetscEmacsClientErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
 94: {
 96:   char           command[PETSC_MAX_PATH_LEN];
 97:   const char     *pdir;
 98:   FILE           *fp;

101:   PetscGetPetscDir(&pdir);if (ierr) PetscFunctionReturn(ierr);
102:   sprintf(command,"cd %s; emacsclient --no-wait +%d %s\n",pdir,line,file);
103: #if defined(PETSC_HAVE_POPEN)
104:   PetscPOpen(MPI_COMM_WORLD,(char*)ctx,command,"r",&fp);if (ierr) PetscFunctionReturn(ierr);
105:   PetscPClose(MPI_COMM_WORLD,fp);if (ierr) PetscFunctionReturn(ierr);
106: #else
107:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
108: #endif
109:   PetscPopErrorHandler();if (ierr) PetscFunctionReturn(ierr); /* remove this handler from the stack of handlers */
110:   if (!eh) {
111:     PetscTraceBackErrorHandler(comm,line,fun,file,n,p,mess,NULL);if (ierr) PetscFunctionReturn(ierr);
112:   } else {
113:     (*eh->handler)(comm,line,fun,file,n,p,mess,eh->ctx);if (ierr) PetscFunctionReturn(ierr);
114:   }
115:   PetscFunctionReturn(ierr);
116: }

118: /*@C
119:    PetscPushErrorHandler - Sets a routine to be called on detection of errors.

121:    Not Collective

123:    Input Parameters:
124: +  handler - error handler routine
125: -  ctx - optional handler context that contains information needed by the handler (for
126:          example file pointers for error messages etc.)

128:    Calling sequence of handler:
129: $    int handler(MPI_Comm comm,int line,char *func,char *file,PetscErrorCode n,int p,char *mess,void *ctx);

131: +  comm - communicator over which error occured
132: .  line - the line number of the error (indicated by __LINE__)
133: .  file - the file in which the error was detected (indicated by __FILE__)
134: .  n - the generic error number (see list defined in include/petscerror.h)
135: .  p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT
136: .  mess - an error text string, usually just printed to the screen
137: -  ctx - the error handler context

139:    Options Database Keys:
140: +   -on_error_attach_debugger <noxterm,gdb or dbx>
141: -   -on_error_abort

143:    Level: intermediate

145:    Notes:
146:    The currently available PETSc error handlers include PetscTraceBackErrorHandler(),
147:    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler(), PetscReturnErrorHandler().

149:    Fortran Notes:
150:     You can only push one error handler from Fortran before poping it.

152: .seealso: PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), PetscTraceBackErrorHandler(), PetscPushSignalHandler()

154: @*/
155: PetscErrorCode  PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void *ctx)
156: {
157:   EH             neweh;

161:   PetscNew(&neweh);
162:   if (eh) neweh->previous = eh;
163:   else    neweh->previous = NULL;
164:   neweh->handler = handler;
165:   neweh->ctx     = ctx;
166:   eh             = neweh;
167:   return(0);
168: }

170: /*@
171:    PetscPopErrorHandler - Removes the latest error handler that was
172:    pushed with PetscPushErrorHandler().

174:    Not Collective

176:    Level: intermediate

178: .seealso: PetscPushErrorHandler()
179: @*/
180: PetscErrorCode  PetscPopErrorHandler(void)
181: {
182:   EH             tmp;

186:   if (!eh) return(0);
187:   tmp  = eh;
188:   eh   = eh->previous;
189:   PetscFree(tmp);
190:   return(0);
191: }

193: /*@C
194:   PetscReturnErrorHandler - Error handler that causes a return to the current
195:   level.

197:    Not Collective

199:    Input Parameters:
200: +  comm - communicator over which error occurred
201: .  line - the line number of the error (indicated by __LINE__)
202: .  file - the file in which the error was detected (indicated by __FILE__)
203: .  mess - an error text string, usually just printed to the screen
204: .  n - the generic error number
205: .  p - specific error number
206: -  ctx - error handler context

208:    Level: developer

210:    Notes:
211:    Most users need not directly employ this routine and the other error
212:    handlers, but can instead use the simplified interface SETERRQ, which has
213:    the calling sequence
214: $     SETERRQ(comm,number,mess)

216:    Notes for experienced users:
217:    This routine is good for catching errors such as zero pivots in preconditioners
218:    or breakdown of iterative methods. It is not appropriate for memory violations
219:    and similar errors.

221:    Use PetscPushErrorHandler() to set the desired error handler.  The
222:    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
223:    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscAbortErrorHandler()

225: .seealso:  PetscPushErrorHandler(), PetscPopErrorHandler().
226:  @*/

228: PetscErrorCode  PetscReturnErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
229: {
231:   PetscFunctionReturn(n);
232: }

234: static char PetscErrorBaseMessage[1024];
235: /*
236:        The numerical values for these are defined in include/petscerror.h; any changes
237:    there must also be made here
238: */
239: static const char *PetscErrorStrings[] = {
240:   /*55 */ "Out of memory",
241:           "No support for this operation for this object type",
242:           "No support for this operation on this system",
243:   /*58 */ "Operation done in wrong order",
244:   /*59 */ "Signal received",
245:   /*60 */ "Nonconforming object sizes",
246:           "Argument aliasing not permitted",
247:           "Invalid argument",
248:   /*63 */ "Argument out of range",
249:           "Corrupt argument: https://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind",
250:           "Unable to open file",
251:           "Read from file failed",
252:           "Write to file failed",
253:           "Invalid pointer",
254:   /*69 */ "Arguments must have same type",
255:   /*70 */ "Attempt to use a pointer that does not point to a valid accessible location",
256:   /*71 */ "Zero pivot in LU factorization: https://www.mcs.anl.gov/petsc/documentation/faq.html#zeropivot",
257:   /*72 */ "Floating point exception",
258:   /*73 */ "Object is in wrong state",
259:           "Corrupted Petsc object",
260:           "Arguments are incompatible",
261:           "Error in external library",
262:   /*77 */ "Petsc has generated inconsistent data",
263:           "Memory corruption: https://www.mcs.anl.gov/petsc/documentation/installation.html#valgrind",
264:           "Unexpected data in file",
265:   /*80 */ "Arguments must have same communicators",
266:   /*81 */ "Zero pivot in Cholesky factorization: https://www.mcs.anl.gov/petsc/documentation/faq.html#zeropivot",
267:           "  ",
268:           "  ",
269:           "Overflow in integer operation: https://www.mcs.anl.gov/petsc/documentation/faq.html#64-bit-indices",
270:   /*85 */ "Null argument, when expecting valid pointer",
271:   /*86 */ "Unknown type. Check for miss-spelling or missing package: https://www.mcs.anl.gov/petsc/documentation/installation.html#external",
272:   /*87 */ "MPI library at runtime is not compatible with MPI used at compile time",
273:   /*88 */ "Error in system call",
274:   /*89 */ "Object Type not set: https://www.mcs.anl.gov/petsc/documentation/faq.html#objecttypenotset",
275:   /*90 */ "  ",
276:   /*   */ "  ",
277:   /*92 */ "See https://www.mcs.anl.gov/petsc/documentation/linearsolvertable.html for possible LU and Cholesky solvers",
278:   /*93 */ "You cannot overwrite this option since that will conflict with other previously set options",
279:   /*94 */ "Example/Section 1.5 Writing Application Codes with PETSc run with number of MPI ranks it does not support",
280:   /*95 */ "Missing or incorrect user input ",
281: };

283: /*@C
284:    PetscErrorMessage - returns the text string associated with a PETSc error code.

286:    Not Collective

288:    Input Parameter:
289: .   errnum - the error code

291:    Output Parameter:
292: +  text - the error message (NULL if not desired)
293: -  specific - the specific error message that was set with SETERRxxx() or PetscError().  (NULL if not desired)

295:    Level: developer

297: .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscError(), SETERRQ(), CHKERRQ() 
298:           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
299:  @*/
300: PetscErrorCode  PetscErrorMessage(int errnum,const char *text[],char **specific)
301: {
303:   if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) *text = PetscErrorStrings[errnum-PETSC_ERR_MIN_VALUE-1];
304:   else if (text) *text = NULL;

306:   if (specific) *specific = PetscErrorBaseMessage;
307:   return(0);
308: }

310: #if defined(PETSC_CLANGUAGE_CXX)
311: /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software
312:  * would be broken if implementations did not handle it it some common cases. However, keep in mind
313:  *
314:  *   Rule 62. Don't allow exceptions to propagate across module boundaries
315:  *
316:  * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface
317:  * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed.
318:  *
319:  * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message
320:  * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that
321:  * seems crazy to me.
322:  */
323: #include <sstream>
324: #include <stdexcept>
325: static void PetscCxxErrorThrow() {
326:   const char *str;
327:   if (eh && eh->ctx) {
328:     std::ostringstream *msg;
329:     msg = (std::ostringstream*) eh->ctx;
330:     str = msg->str().c_str();
331:   } else str = "Error detected in C PETSc";

333:   throw std::runtime_error(str);
334: }
335: #endif

337: /*@C
338:    PetscError - Routine that is called when an error has been detected,
339:    usually called through the macro SETERRQ(PETSC_COMM_SELF,).

341:    Not Collective

343:    Input Parameters:
344: +  comm - communicator over which error occurred.  ALL ranks of this communicator MUST call this routine
345: .  line - the line number of the error (indicated by __LINE__)
346: .  func - the function name in which the error was detected
347: .  file - the file in which the error was detected (indicated by __FILE__)
348: .  n - the generic error number
349: .  p - PETSC_ERROR_INITIAL indicates the error was initially detected, PETSC_ERROR_REPEAT indicates this is a traceback from a previously detected error
350: -  mess - formatted message string - aka printf

352:   Level: intermediate

354:    Notes:
355:    Most users need not directly use this routine and the error handlers, but
356:    can instead use the simplified interface SETERRQ, which has the calling
357:    sequence
358: $     SETERRQ(comm,n,mess)

360:    Fortran Note:
361:    This routine is used differently from Fortran
362: $    PetscError(MPI_Comm comm,PetscErrorCode n,PetscErrorType p,char *message)

364:    Experienced users can set the error handler with PetscPushErrorHandler().

366:    Developer Note: Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes)
367:    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
368:    but this annoying.

370: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), SETERRQ(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), PetscErrorMessage(), PETSCABORT()
371: @*/
372: PetscErrorCode PetscError(MPI_Comm comm,int line,const char *func,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,...)
373: {
374:   va_list        Argp;
375:   size_t         fullLength;
376:   char           buf[2048],*lbuf = NULL;
377:   PetscBool      ismain;

381:   if (!func) func = "User provided function";
382:   if (!file) file = "User file";
383:   if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF;

385:   /* Compose the message evaluating the print format */
386:   if (mess) {
387:     va_start(Argp,mess);
388:     PetscVSNPrintf(buf,2048,mess,&fullLength,Argp);
389:     va_end(Argp);
390:     lbuf = buf;
391:     if (p == PETSC_ERROR_INITIAL) PetscStrncpy(PetscErrorBaseMessage,lbuf,1023);
392:   }

394:   if (!eh) PetscTraceBackErrorHandler(comm,line,func,file,n,p,lbuf,NULL);
395:   else     (*eh->handler)(comm,line,func,file,n,p,lbuf,eh->ctx);

397:   /*
398:       If this is called from the main() routine we call MPI_Abort() instead of
399:     return to allow the parallel program to be properly shutdown.

401:     Does not call PETSCABORT() since that would provide the wrong source file and line number information
402:   */
403:   PetscStrncmp(func,"main",4,&ismain);
404:   if (ismain) {
405:     PetscMPIInt    errcode;
406:     errcode = (PetscMPIInt)(0 + line*1000 + ierr);
407:     MPI_Abort(comm,errcode);
408:   }

410: #if defined(PETSC_CLANGUAGE_CXX)
411:   if (p == PETSC_ERROR_IN_CXX) {
412:     PetscCxxErrorThrow();
413:   }
414: #endif
415:   PetscFunctionReturn(ierr);
416: }

418: /* -------------------------------------------------------------------------*/

420: /*@C
421:     PetscIntView - Prints an array of integers; useful for debugging.

423:     Collective on PetscViewer

425:     Input Parameters:
426: +   N - number of integers in array
427: .   idx - array of integers
428: -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0

430:   Level: intermediate

432:     Developer Notes:
433:     idx cannot be const because may be passed to binary viewer where byte swapping is done

435: .seealso: PetscRealView()
436: @*/
437: PetscErrorCode  PetscIntView(PetscInt N,const PetscInt idx[],PetscViewer viewer)
438: {
440:   PetscMPIInt         rank,size;
441:   PetscInt       j,i,n = N/20,p = N % 20;
442:   PetscBool      iascii,isbinary;
443:   MPI_Comm       comm;

446:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
449:   PetscObjectGetComm((PetscObject)viewer,&comm);
450:   MPI_Comm_size(comm,&size);
451:   MPI_Comm_rank(comm,&rank);

453:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
454:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
455:   if (iascii) {
456:     PetscViewerASCIIPushSynchronized(viewer);
457:     for (i=0; i<n; i++) {
458:       if (size > 1) {
459:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D:", rank, 20*i);
460:       } else {
461:         PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*i);
462:       }
463:       for (j=0; j<20; j++) {
464:         PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[i*20+j]);
465:       }
466:       PetscViewerASCIISynchronizedPrintf(viewer,"\n");
467:     }
468:     if (p) {
469:       if (size > 1) {
470:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D:",rank ,20*n);
471:       } else {
472:         PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*n);
473:       }
474:       for (i=0; i<p; i++) { PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[20*n+i]);}
475:       PetscViewerASCIISynchronizedPrintf(viewer,"\n");
476:     }
477:     PetscViewerFlush(viewer);
478:     PetscViewerASCIIPopSynchronized(viewer);
479:   } else if (isbinary) {
480:     PetscMPIInt *sizes,Ntotal,*displs,NN;
481:     PetscInt    *array;

483:     PetscMPIIntCast(N,&NN);

485:     if (size > 1) {
486:       if (rank) {
487:         MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm);
488:         MPI_Gatherv((void*)idx,NN,MPIU_INT,NULL,NULL,NULL,MPIU_INT,0,comm);
489:       } else {
490:         PetscMalloc1(size,&sizes);
491:         MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);
492:         Ntotal    = sizes[0];
493:         PetscMalloc1(size,&displs);
494:         displs[0] = 0;
495:         for (i=1; i<size; i++) {
496:           Ntotal   += sizes[i];
497:           displs[i] =  displs[i-1] + sizes[i-1];
498:         }
499:         PetscMalloc1(Ntotal,&array);
500:         MPI_Gatherv((void*)idx,NN,MPIU_INT,array,sizes,displs,MPIU_INT,0,comm);
501:         PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_INT);
502:         PetscFree(sizes);
503:         PetscFree(displs);
504:         PetscFree(array);
505:       }
506:     } else {
507:       PetscViewerBinaryWrite(viewer,idx,N,PETSC_INT);
508:     }
509:   } else {
510:     const char *tname;
511:     PetscObjectGetName((PetscObject)viewer,&tname);
512:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
513:   }
514:   return(0);
515: }

517: /*@C
518:     PetscRealView - Prints an array of doubles; useful for debugging.

520:     Collective on PetscViewer

522:     Input Parameters:
523: +   N - number of PetscReal in array
524: .   idx - array of PetscReal
525: -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0

527:   Level: intermediate

529:     Developer Notes:
530:     idx cannot be const because may be passed to binary viewer where byte swapping is done

532: .seealso: PetscIntView()
533: @*/
534: PetscErrorCode  PetscRealView(PetscInt N,const PetscReal idx[],PetscViewer viewer)
535: {
537:   PetscMPIInt         rank,size;
538:   PetscInt       j,i,n = N/5,p = N % 5;
539:   PetscBool      iascii,isbinary;
540:   MPI_Comm       comm;

543:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
546:   PetscObjectGetComm((PetscObject)viewer,&comm);
547:   MPI_Comm_size(comm,&size);
548:   MPI_Comm_rank(comm,&rank);

550:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
551:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
552:   if (iascii) {
553:     PetscInt tab;

555:     PetscViewerASCIIPushSynchronized(viewer);
556:     PetscViewerASCIIGetTab(viewer, &tab);
557:     for (i=0; i<n; i++) {
558:       PetscViewerASCIISetTab(viewer, tab);
559:       if (size > 1) {
560:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,(int)5*i);
561:       } else {
562:         PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",(int)5*i);
563:       }
564:       PetscViewerASCIISetTab(viewer, 0);
565:       for (j=0; j<5; j++) {
566:         PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*5+j]);
567:       }
568:       PetscViewerASCIISynchronizedPrintf(viewer,"\n");
569:     }
570:     if (p) {
571:       PetscViewerASCIISetTab(viewer, tab);
572:       if (size > 1) {
573:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,(int)5*n);
574:       } else {
575:         PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",(int)5*n);
576:       }
577:       PetscViewerASCIISetTab(viewer, 0);
578:       for (i=0; i<p; i++) { PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[5*n+i]);}
579:       PetscViewerASCIISynchronizedPrintf(viewer,"\n");
580:     }
581:     PetscViewerFlush(viewer);
582:     PetscViewerASCIISetTab(viewer, tab);
583:     PetscViewerASCIIPopSynchronized(viewer);
584:   } else if (isbinary) {
585:     PetscMPIInt *sizes,*displs, Ntotal,NN;
586:     PetscReal   *array;

588:     PetscMPIIntCast(N,&NN);

590:     if (size > 1) {
591:       if (rank) {
592:         MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm);
593:         MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,NULL,NULL,NULL,MPIU_REAL,0,comm);
594:       } else {
595:         PetscMalloc1(size,&sizes);
596:         MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);
597:         Ntotal    = sizes[0];
598:         PetscMalloc1(size,&displs);
599:         displs[0] = 0;
600:         for (i=1; i<size; i++) {
601:           Ntotal   += sizes[i];
602:           displs[i] =  displs[i-1] + sizes[i-1];
603:         }
604:         PetscMalloc1(Ntotal,&array);
605:         MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,array,sizes,displs,MPIU_REAL,0,comm);
606:         PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_REAL);
607:         PetscFree(sizes);
608:         PetscFree(displs);
609:         PetscFree(array);
610:       }
611:     } else {
612:       PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_REAL);
613:     }
614:   } else {
615:     const char *tname;
616:     PetscObjectGetName((PetscObject)viewer,&tname);
617:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
618:   }
619:   return(0);
620: }

622: /*@C
623:     PetscScalarView - Prints an array of scalars; useful for debugging.

625:     Collective on PetscViewer

627:     Input Parameters:
628: +   N - number of scalars in array
629: .   idx - array of scalars
630: -   viewer - location to print array,  PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0

632:   Level: intermediate

634:     Developer Notes:
635:     idx cannot be const because may be passed to binary viewer where byte swapping is done

637: .seealso: PetscIntView(), PetscRealView()
638: @*/
639: PetscErrorCode  PetscScalarView(PetscInt N,const PetscScalar idx[],PetscViewer viewer)
640: {
642:   PetscMPIInt         rank,size;
643:   PetscInt       j,i,n = N/3,p = N % 3;
644:   PetscBool      iascii,isbinary;
645:   MPI_Comm       comm;

648:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
651:   PetscObjectGetComm((PetscObject)viewer,&comm);
652:   MPI_Comm_size(comm,&size);
653:   MPI_Comm_rank(comm,&rank);

655:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
656:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
657:   if (iascii) {
658:     PetscViewerASCIIPushSynchronized(viewer);
659:     for (i=0; i<n; i++) {
660:       if (size > 1) {
661:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,3*i);
662:       } else {
663:         PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*i);
664:       }
665:       for (j=0; j<3; j++) {
666: #if defined(PETSC_USE_COMPLEX)
667:         PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[i*3+j]),(double)PetscImaginaryPart(idx[i*3+j]));
668: #else
669:         PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*3+j]);
670: #endif
671:       }
672:       PetscViewerASCIISynchronizedPrintf(viewer,"\n");
673:     }
674:     if (p) {
675:       if (size > 1) {
676:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,3*n);
677:       } else {
678:         PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*n);
679:       }
680:       for (i=0; i<p; i++) {
681: #if defined(PETSC_USE_COMPLEX)
682:         PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[n*3+i]),(double)PetscImaginaryPart(idx[n*3+i]));
683: #else
684:         PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[3*n+i]);
685: #endif
686:       }
687:       PetscViewerASCIISynchronizedPrintf(viewer,"\n");
688:     }
689:     PetscViewerFlush(viewer);
690:     PetscViewerASCIIPopSynchronized(viewer);
691:   } else if (isbinary) {
692:     PetscMPIInt *sizes,Ntotal,*displs,NN;
693:     PetscScalar *array;

695:     PetscMPIIntCast(N,&NN);

697:     if (size > 1) {
698:       if (rank) {
699:         MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm);
700:         MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,NULL,NULL,NULL,MPIU_SCALAR,0,comm);
701:       } else {
702:         PetscMalloc1(size,&sizes);
703:         MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);
704:         Ntotal    = sizes[0];
705:         PetscMalloc1(size,&displs);
706:         displs[0] = 0;
707:         for (i=1; i<size; i++) {
708:           Ntotal   += sizes[i];
709:           displs[i] =  displs[i-1] + sizes[i-1];
710:         }
711:         PetscMalloc1(Ntotal,&array);
712:         MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,array,sizes,displs,MPIU_SCALAR,0,comm);
713:         PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_SCALAR);
714:         PetscFree(sizes);
715:         PetscFree(displs);
716:         PetscFree(array);
717:       }
718:     } else {
719:       PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_SCALAR);
720:     }
721:   } else {
722:     const char *tname;
723:     PetscObjectGetName((PetscObject)viewer,&tname);
724:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname);
725:   }
726:   return(0);
727: }

729: #if defined(PETSC_HAVE_CUDA)
730:  #include <petsccublas.h>
731: PETSC_EXTERN const char* PetscCUBLASGetErrorName(cublasStatus_t status)
732: {
733:   switch(status) {
734: #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */
735:     case CUBLAS_STATUS_SUCCESS:          return "CUBLAS_STATUS_SUCCESS";
736:     case CUBLAS_STATUS_NOT_INITIALIZED:  return "CUBLAS_STATUS_NOT_INITIALIZED";
737:     case CUBLAS_STATUS_ALLOC_FAILED:     return "CUBLAS_STATUS_ALLOC_FAILED";
738:     case CUBLAS_STATUS_INVALID_VALUE:    return "CUBLAS_STATUS_INVALID_VALUE";
739:     case CUBLAS_STATUS_ARCH_MISMATCH:    return "CUBLAS_STATUS_ARCH_MISMATCH";
740:     case CUBLAS_STATUS_MAPPING_ERROR:    return "CUBLAS_STATUS_MAPPING_ERROR";
741:     case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
742:     case CUBLAS_STATUS_INTERNAL_ERROR:   return "CUBLAS_STATUS_INTERNAL_ERROR";
743:     case CUBLAS_STATUS_NOT_SUPPORTED:    return "CUBLAS_STATUS_NOT_SUPPORTED";
744:     case CUBLAS_STATUS_LICENSE_ERROR:    return "CUBLAS_STATUS_LICENSE_ERROR";
745: #endif
746:     default:                             return "unknown error";
747:   }
748: }
749: #endif