17d0a6c19SBarry Smith 2e5c89e4eSSatish Balay /* 3e5c89e4eSSatish Balay Code that allows one to set the error handlers 4e5c89e4eSSatish Balay */ 5af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 6665c2dedSJed Brown #include <petscviewer.h> 7e5c89e4eSSatish Balay 87c66cc67SJunchao Zhang /* A table of Petsc source files containing calls to PETSCABORT. We assume this table will 97c66cc67SJunchao Zhang stay stable for a while. When things changed, we just need to add new files to the table. 107c66cc67SJunchao Zhang */ 117c66cc67SJunchao Zhang static const char* PetscAbortSourceFiles[] = { 127233276eSBarry Smith "Souce code of main", /* 0 */ 137233276eSBarry Smith "Not Found", /* 1, not found in petsc, but may be in users' code if they called PETSCABORT. */ 147c66cc67SJunchao Zhang "sys/error/adebug.c", 157c66cc67SJunchao Zhang "src/sys/error/errstop.c", 167c66cc67SJunchao Zhang "sys/error/fp.c", 177c66cc67SJunchao Zhang "sys/error/signal.c", /* 5 */ 187c66cc67SJunchao Zhang "sys/ftn-custom/zutils.c", 197c66cc67SJunchao Zhang "sys/logging/utils/stagelog.c", 207c66cc67SJunchao Zhang "sys/mpiuni/mpitime.c", 217c66cc67SJunchao Zhang "sys/objects/init.c", 227c66cc67SJunchao Zhang "sys/objects/pinit.c", /* 10 */ 237c66cc67SJunchao Zhang "vec/vec/interface/dlregisvec.c", 247c66cc67SJunchao Zhang "vec/vec/utils/comb.c" 257c66cc67SJunchao Zhang }; 267c66cc67SJunchao Zhang 277c66cc67SJunchao Zhang /* Find index of the soure file where a PETSCABORT was called. */ 287c66cc67SJunchao Zhang PetscErrorCode PetscAbortFindSourceFile_Private(const char* filepath, PetscInt *idx) 297c66cc67SJunchao Zhang { 307c66cc67SJunchao Zhang PetscErrorCode ierr; 31dd39110bSPierre Jolivet PetscInt i,n = PETSC_STATIC_ARRAY_LENGTH(PetscAbortSourceFiles); 327c66cc67SJunchao Zhang PetscBool match; 33cf92665aSStefano Zampini char subpath[PETSC_MAX_PATH_LEN]; 347c66cc67SJunchao Zhang 35cf92665aSStefano Zampini ierr = PetscStackView(stderr);if (ierr) return ierr; 367233276eSBarry Smith *idx = 1; 377233276eSBarry Smith for (i=2; i<n; i++) { 38cf92665aSStefano Zampini ierr = PetscFixFilename(PetscAbortSourceFiles[i],subpath);if (ierr) return ierr; 39cf92665aSStefano Zampini ierr = PetscStrendswith(filepath,subpath,&match);if (ierr) return ierr; 407c66cc67SJunchao Zhang if (match) {*idx = i; break;} 417c66cc67SJunchao Zhang } 42cf92665aSStefano Zampini return 0; 437c66cc67SJunchao Zhang } 447c66cc67SJunchao Zhang 45e5c89e4eSSatish Balay typedef struct _EH *EH; 46e5c89e4eSSatish Balay struct _EH { 47efca3c55SSatish Balay PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*); 48e5c89e4eSSatish Balay void *ctx; 49e5c89e4eSSatish Balay EH previous; 50e5c89e4eSSatish Balay }; 51e5c89e4eSSatish Balay 5202c9f0b5SLisandro Dalcin static EH eh = NULL; 53e5c89e4eSSatish Balay 54e5c89e4eSSatish Balay /*@C 55e5c89e4eSSatish Balay PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to 56a5b23f4aSJose E. Roman load the file where the error occurred. Then calls the "previous" error handler. 57e5c89e4eSSatish Balay 58e5c89e4eSSatish Balay Not Collective 59e5c89e4eSSatish Balay 60e5c89e4eSSatish Balay Input Parameters: 61a5b23f4aSJose E. Roman + comm - communicator over which error occurred 62e32f2f54SBarry Smith . line - the line number of the error (indicated by __LINE__) 63e5c89e4eSSatish Balay . file - the file in which the error was detected (indicated by __FILE__) 64e5c89e4eSSatish Balay . mess - an error text string, usually just printed to the screen 65e5c89e4eSSatish Balay . n - the generic error number 66e5c89e4eSSatish Balay . p - specific error number 67e5c89e4eSSatish Balay - ctx - error handler context 68e5c89e4eSSatish Balay 69e5c89e4eSSatish Balay Options Database Key: 7010699b91SBarry Smith . -on_error_emacs <machinename> - will contact machinename to open the Emacs client there 71e5c89e4eSSatish Balay 72e5c89e4eSSatish Balay Level: developer 73e5c89e4eSSatish Balay 74e5c89e4eSSatish Balay Notes: 75e5c89e4eSSatish Balay You must put (server-start) in your .emacs file for the emacsclient software to work 76e5c89e4eSSatish Balay 77cc12936aSPatrick Sanan Developer Note: 789566063dSJacob Faibussowitsch Since this is an error handler it cannot call PetscCall(); thus we just return if an error is detected. 793bf036e2SBarry Smith 80db781477SPatrick Sanan .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, 81db781477SPatrick Sanan `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscReturnErrorHandler()` 82e5c89e4eSSatish Balay @*/ 83efca3c55SSatish Balay PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) 84e5c89e4eSSatish Balay { 85e5c89e4eSSatish Balay PetscErrorCode ierr; 86e5c89e4eSSatish Balay char command[PETSC_MAX_PATH_LEN]; 87e5c89e4eSSatish Balay const char *pdir; 88e5c89e4eSSatish Balay FILE *fp; 89e5c89e4eSSatish Balay 90e5c89e4eSSatish Balay PetscFunctionBegin; 913bf036e2SBarry Smith ierr = PetscGetPetscDir(&pdir);if (ierr) PetscFunctionReturn(ierr); 92efca3c55SSatish Balay sprintf(command,"cd %s; emacsclient --no-wait +%d %s\n",pdir,line,file); 93e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN) 943bf036e2SBarry Smith ierr = PetscPOpen(MPI_COMM_WORLD,(char*)ctx,command,"r",&fp);if (ierr) PetscFunctionReturn(ierr); 95016831caSBarry Smith ierr = PetscPClose(MPI_COMM_WORLD,fp);if (ierr) PetscFunctionReturn(ierr); 96e5c89e4eSSatish Balay #else 97e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 98e5c89e4eSSatish Balay #endif 993bf036e2SBarry Smith ierr = PetscPopErrorHandler();if (ierr) PetscFunctionReturn(ierr); /* remove this handler from the stack of handlers */ 1003bf036e2SBarry Smith if (!eh) { 10102c9f0b5SLisandro Dalcin ierr = PetscTraceBackErrorHandler(comm,line,fun,file,n,p,mess,NULL);if (ierr) PetscFunctionReturn(ierr); 1023bf036e2SBarry Smith } else { 103efca3c55SSatish Balay ierr = (*eh->handler)(comm,line,fun,file,n,p,mess,eh->ctx);if (ierr) PetscFunctionReturn(ierr); 1043bf036e2SBarry Smith } 105e5c89e4eSSatish Balay PetscFunctionReturn(ierr); 106e5c89e4eSSatish Balay } 107e5c89e4eSSatish Balay 108e5c89e4eSSatish Balay /*@C 109e5c89e4eSSatish Balay PetscPushErrorHandler - Sets a routine to be called on detection of errors. 110e5c89e4eSSatish Balay 111e5c89e4eSSatish Balay Not Collective 112e5c89e4eSSatish Balay 113e5c89e4eSSatish Balay Input Parameters: 114e5c89e4eSSatish Balay + handler - error handler routine 115e5c89e4eSSatish Balay - ctx - optional handler context that contains information needed by the handler (for 116e5c89e4eSSatish Balay example file pointers for error messages etc.) 117e5c89e4eSSatish Balay 118e5c89e4eSSatish Balay Calling sequence of handler: 119efca3c55SSatish Balay $ int handler(MPI_Comm comm,int line,char *func,char *file,PetscErrorCode n,int p,char *mess,void *ctx); 120e5c89e4eSSatish Balay 121a5b23f4aSJose E. Roman + comm - communicator over which error occurred 122e5c89e4eSSatish Balay . line - the line number of the error (indicated by __LINE__) 123e5c89e4eSSatish Balay . file - the file in which the error was detected (indicated by __FILE__) 124e5c89e4eSSatish Balay . n - the generic error number (see list defined in include/petscerror.h) 125668f157eSBarry Smith . p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT 126e5c89e4eSSatish Balay . mess - an error text string, usually just printed to the screen 127e5c89e4eSSatish Balay - ctx - the error handler context 128e5c89e4eSSatish Balay 129e5c89e4eSSatish Balay Options Database Keys: 13010699b91SBarry Smith + -on_error_attach_debugger <noxterm,gdb or dbx> - starts up the debugger if an error occurs 13110699b91SBarry Smith - -on_error_abort - aborts the program if an error occurs 132e5c89e4eSSatish Balay 133e5c89e4eSSatish Balay Level: intermediate 134e5c89e4eSSatish Balay 135e93bc3c1Svictor Notes: 1367850c7c0SBarry Smith The currently available PETSc error handlers include PetscTraceBackErrorHandler(), 137e8fb0fc0SBarry Smith PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler(), PetscReturnErrorHandler(). 138e93bc3c1Svictor 13995452b02SPatrick Sanan Fortran Notes: 14095452b02SPatrick Sanan You can only push one error handler from Fortran before poping it. 1417850c7c0SBarry Smith 142db781477SPatrick Sanan .seealso: `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscPushSignalHandler()` 143e5c89e4eSSatish Balay 144e5c89e4eSSatish Balay @*/ 145efca3c55SSatish Balay PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void *ctx) 146e5c89e4eSSatish Balay { 147e5c89e4eSSatish Balay EH neweh; 148e5c89e4eSSatish Balay 149e5c89e4eSSatish Balay PetscFunctionBegin; 1509566063dSJacob Faibussowitsch PetscCall(PetscNew(&neweh)); 151a297a907SKarl Rupp if (eh) neweh->previous = eh; 15202c9f0b5SLisandro Dalcin else neweh->previous = NULL; 153e5c89e4eSSatish Balay neweh->handler = handler; 154e5c89e4eSSatish Balay neweh->ctx = ctx; 155e5c89e4eSSatish Balay eh = neweh; 156e5c89e4eSSatish Balay PetscFunctionReturn(0); 157e5c89e4eSSatish Balay } 158e5c89e4eSSatish Balay 159e30d2299SSatish Balay /*@ 160e5c89e4eSSatish Balay PetscPopErrorHandler - Removes the latest error handler that was 161e5c89e4eSSatish Balay pushed with PetscPushErrorHandler(). 162e5c89e4eSSatish Balay 163e5c89e4eSSatish Balay Not Collective 164e5c89e4eSSatish Balay 165e5c89e4eSSatish Balay Level: intermediate 166e5c89e4eSSatish Balay 167db781477SPatrick Sanan .seealso: `PetscPushErrorHandler()` 168e5c89e4eSSatish Balay @*/ 1697087cfbeSBarry Smith PetscErrorCode PetscPopErrorHandler(void) 170e5c89e4eSSatish Balay { 171e5c89e4eSSatish Balay EH tmp; 172e5c89e4eSSatish Balay 173e5c89e4eSSatish Balay PetscFunctionBegin; 174e5c89e4eSSatish Balay if (!eh) PetscFunctionReturn(0); 175e5c89e4eSSatish Balay tmp = eh; 176e5c89e4eSSatish Balay eh = eh->previous; 1779566063dSJacob Faibussowitsch PetscCall(PetscFree(tmp)); 178e5c89e4eSSatish Balay PetscFunctionReturn(0); 179e5c89e4eSSatish Balay } 180e5c89e4eSSatish Balay 181e93bc3c1Svictor /*@C 18245b666d6SBarry Smith PetscReturnErrorHandler - Error handler that causes a return without printing an error message. 183e93bc3c1Svictor 184e93bc3c1Svictor Not Collective 185e93bc3c1Svictor 186e93bc3c1Svictor Input Parameters: 187e32f2f54SBarry Smith + comm - communicator over which error occurred 188e32f2f54SBarry Smith . line - the line number of the error (indicated by __LINE__) 189e93bc3c1Svictor . file - the file in which the error was detected (indicated by __FILE__) 190e93bc3c1Svictor . mess - an error text string, usually just printed to the screen 191e93bc3c1Svictor . n - the generic error number 192e93bc3c1Svictor . p - specific error number 193e93bc3c1Svictor - ctx - error handler context 194e93bc3c1Svictor 195e93bc3c1Svictor Level: developer 196e93bc3c1Svictor 197e93bc3c1Svictor Notes: 198e93bc3c1Svictor Most users need not directly employ this routine and the other error 199e93bc3c1Svictor handlers, but can instead use the simplified interface SETERRQ, which has 200e93bc3c1Svictor the calling sequence 201e32f2f54SBarry Smith $ SETERRQ(comm,number,mess) 202e93bc3c1Svictor 20345b666d6SBarry Smith PetscIgnoreErrorHandler() does the same thing as this function, but is deprecated, you should use this function. 204e93bc3c1Svictor 20545b666d6SBarry Smith Use PetscPushErrorHandler() to set the desired error handler. 206e93bc3c1Svictor 207db781477SPatrick Sanan .seealso: `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscError()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, 208db781477SPatrick Sanan `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()` 209e93bc3c1Svictor @*/ 210efca3c55SSatish Balay PetscErrorCode PetscReturnErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) 211e93bc3c1Svictor { 212362febeeSStefano Zampini return 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", 230a17b96a8SKyle Gerard Felker "Corrupt argument: https://petsc.org/release/faq/#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", 236a8b45ee7SBarry Smith /*70 */ "Attempt to use a pointer that does not point to a valid accessible location", 237a17b96a8SKyle Gerard Felker /*71 */ "Zero pivot in LU factorization: https://petsc.org/release/faq/#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", 244a17b96a8SKyle Gerard Felker "Memory corruption: https://petsc.org/release/faq/#valgrind", 245e5c89e4eSSatish Balay "Unexpected data in file", 246e5c89e4eSSatish Balay /*80 */ "Arguments must have same communicators", 247a17b96a8SKyle Gerard Felker /*81 */ "Zero pivot in Cholesky factorization: https://petsc.org/release/faq/#zeropivot", 248e5c89e4eSSatish Balay "", 249e5c89e4eSSatish Balay "", 250a17b96a8SKyle Gerard Felker "Overflow in integer operation: https://petsc.org/release/faq/#64-bit-indices", 251e5c89e4eSSatish Balay /*85 */ "Null argument, when expecting valid pointer", 252a17b96a8SKyle Gerard Felker /*86 */ "Unknown type. Check for miss-spelling or missing package: https://petsc.org/release/install/install/#external-packages", 2533d96e996SBarry Smith /*87 */ "MPI library at runtime is not compatible with MPI used at compile time", 2548cda6cd7SBarry Smith /*88 */ "Error in system call", 255a17b96a8SKyle Gerard Felker /*89 */ "Object Type not set: https://petsc.org/release/faq/#object-type-not-set", 25673260a9bSLisandro Dalcin /*90 */ "", 25773260a9bSLisandro Dalcin /* */ "", 258a17b96a8SKyle Gerard Felker /*92 */ "See https://petsc.org/release/overview/linear_solve_table/ for possible LU and Cholesky solvers", 259f560318cSPatrick Sanan /*93 */ "You cannot overwrite this option since that will conflict with other previously set options", 260691b26d3SBarry Smith /*94 */ "Example/application run with number of MPI ranks it does not support", 261691b26d3SBarry Smith /*95 */ "Missing or incorrect user input", 262e57d7714SBarry Smith /*96 */ "GPU resources unavailable", 263764761abSStefano Zampini /*97 */ "GPU error", 264764761abSStefano Zampini /*98 */ "General MPI error" 26573260a9bSLisandro Dalcin }; 266e5c89e4eSSatish Balay 267e5c89e4eSSatish Balay /*@C 268e5c89e4eSSatish Balay PetscErrorMessage - returns the text string associated with a PETSc error code. 269e5c89e4eSSatish Balay 270e5c89e4eSSatish Balay Not Collective 271e5c89e4eSSatish Balay 272e5c89e4eSSatish Balay Input Parameter: 273e5c89e4eSSatish Balay . errnum - the error code 274e5c89e4eSSatish Balay 275d8d19677SJose E. Roman Output Parameters: 2760298fd71SBarry Smith + text - the error message (NULL if not desired) 2770298fd71SBarry Smith - specific - the specific error message that was set with SETERRxxx() or PetscError(). (NULL if not desired) 278e5c89e4eSSatish Balay 279e5c89e4eSSatish Balay Level: developer 280e5c89e4eSSatish Balay 281db781477SPatrick Sanan .seealso: `PetscPushErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscCall()` 282db781477SPatrick Sanan `PetscAbortErrorHandler()`, `PetscTraceBackErrorHandler()` 283e5c89e4eSSatish Balay @*/ 2847087cfbeSBarry Smith PetscErrorCode PetscErrorMessage(int errnum,const char *text[],char **specific) 285e5c89e4eSSatish Balay { 286c35ec7c7SPierre Jolivet size_t len; 287c35ec7c7SPierre Jolivet 288e5c89e4eSSatish Balay PetscFunctionBegin; 289c35ec7c7SPierre Jolivet if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) { 290c35ec7c7SPierre Jolivet *text = PetscErrorStrings[errnum-PETSC_ERR_MIN_VALUE-1]; 291c35ec7c7SPierre Jolivet PetscCall(PetscStrlen(*text,&len)); 292c35ec7c7SPierre Jolivet if (!len) *text = NULL; 293c35ec7c7SPierre Jolivet } 29402c9f0b5SLisandro Dalcin else if (text) *text = NULL; 295e5c89e4eSSatish Balay 296a297a907SKarl Rupp if (specific) *specific = PetscErrorBaseMessage; 297e5c89e4eSSatish Balay PetscFunctionReturn(0); 298e5c89e4eSSatish Balay } 299e5c89e4eSSatish Balay 300984a1229SMatthew G. Knepley #if defined(PETSC_CLANGUAGE_CXX) 301984a1229SMatthew G. Knepley /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software 302984a1229SMatthew G. Knepley * would be broken if implementations did not handle it it some common cases. However, keep in mind 303984a1229SMatthew G. Knepley * 304984a1229SMatthew G. Knepley * Rule 62. Don't allow exceptions to propagate across module boundaries 305984a1229SMatthew G. Knepley * 306984a1229SMatthew G. Knepley * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface 307984a1229SMatthew G. Knepley * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed. 308984a1229SMatthew G. Knepley * 309984a1229SMatthew G. Knepley * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message 310984a1229SMatthew G. Knepley * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that 311984a1229SMatthew G. Knepley * seems crazy to me. 312984a1229SMatthew G. Knepley */ 313984a1229SMatthew G. Knepley #include <sstream> 3144c94c282SMatthew G. Knepley #include <stdexcept> 31570a7d78aSStefano Zampini static void PetscCxxErrorThrow() 31670a7d78aSStefano Zampini { 317984a1229SMatthew G. Knepley const char *str; 318984a1229SMatthew G. Knepley if (eh && eh->ctx) { 319984a1229SMatthew G. Knepley std::ostringstream *msg; 320984a1229SMatthew G. Knepley msg = (std::ostringstream*) eh->ctx; 321984a1229SMatthew G. Knepley str = msg->str().c_str(); 322984a1229SMatthew G. Knepley } else str = "Error detected in C PETSc"; 323984a1229SMatthew G. Knepley 324984a1229SMatthew G. Knepley throw std::runtime_error(str); 325984a1229SMatthew G. Knepley } 326984a1229SMatthew G. Knepley #endif 327984a1229SMatthew G. Knepley 328e5c89e4eSSatish Balay /*@C 32945b666d6SBarry Smith PetscError - Routine that is called when an error has been detected, usually called through the macro SETERRQ(PETSC_COMM_SELF,). 330e5c89e4eSSatish Balay 33145b666d6SBarry Smith Collective on comm 332e5c89e4eSSatish Balay 333e5c89e4eSSatish Balay Input Parameters: 334e32f2f54SBarry Smith + comm - communicator over which error occurred. ALL ranks of this communicator MUST call this routine 335e32f2f54SBarry Smith . line - the line number of the error (indicated by __LINE__) 3363de71b31SHong Zhang . func - the function name in which the error was detected 337e5c89e4eSSatish Balay . file - the file in which the error was detected (indicated by __FILE__) 338e5c89e4eSSatish Balay . n - the generic error number 339d736bfebSBarry Smith . p - PETSC_ERROR_INITIAL indicates the error was initially detected, PETSC_ERROR_REPEAT indicates this is a traceback from a previously detected error 340e5c89e4eSSatish Balay - mess - formatted message string - aka printf 341e5c89e4eSSatish Balay 34245b666d6SBarry Smith Options Database: 34345b666d6SBarry Smith + -error_output_stdout - output the error messages to stdout instead of the default stderr 34445b666d6SBarry Smith - -error_output_none - do not output the error messages 34545b666d6SBarry Smith 346e5c89e4eSSatish Balay Level: intermediate 347e5c89e4eSSatish Balay 348e5c89e4eSSatish Balay Notes: 34945b666d6SBarry Smith PETSc error handling is done with error return codes. A non-zero return indicates an error was detected. Errors are generally not something that the code 35045b666d6SBarry Smith can recover from. Note that numerical errors (potential divide by zero, for example) are not managed by the error return codes; they are managed via, for example, 35145b666d6SBarry Smith KSPGetConvergedReason() that indicates if the solve was successful or not. The option -ksp_error_if_not_converged, for example, turns numerical failures into 35245b666d6SBarry Smith hard errors managed via PetscError(). 35345b666d6SBarry Smith 35445b666d6SBarry Smith PETSc provides a rich supply of error handlers, see the list below, and users can also provide their own error handlers. 35545b666d6SBarry Smith 356e5c89e4eSSatish Balay Most users need not directly use this routine and the error handlers, but 357e5c89e4eSSatish Balay can instead use the simplified interface SETERRQ, which has the calling 358e5c89e4eSSatish Balay sequence 359e32f2f54SBarry Smith $ SETERRQ(comm,n,mess) 360e5c89e4eSSatish Balay 361e3081792SBarry Smith Fortran Note: 362e3081792SBarry Smith This routine is used differently from Fortran 363e3081792SBarry Smith $ PetscError(MPI_Comm comm,PetscErrorCode n,PetscErrorType p,char *message) 364e3081792SBarry Smith 36545b666d6SBarry Smith Set the error handler with PetscPushErrorHandler(). 366e5c89e4eSSatish Balay 3673bf036e2SBarry Smith Developer Note: Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes) 3683bf036e2SBarry Smith BUT this routine does call regular PETSc functions that may call error handlers, this is problematic and could be fixed by never calling other PETSc routines 3693bf036e2SBarry Smith but this annoying. 3703bf036e2SBarry Smith 371db781477SPatrick Sanan .seealso: `PetscErrorCode`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, 372db781477SPatrick Sanan `PetscReturnErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()`, 373db781477SPatrick Sanan `SETERRQ()`, `PetscCall()`, `CHKMEMQ`, `SETERRQ()`, `SETERRQ()`, `PetscErrorMessage()`, `PETSCABORT()` 374e5c89e4eSSatish Balay @*/ 375efca3c55SSatish Balay PetscErrorCode PetscError(MPI_Comm comm,int line,const char *func,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,...) 376e5c89e4eSSatish Balay { 377e5c89e4eSSatish Balay va_list Argp; 378c9a19010SBarry Smith size_t fullLength; 37902c9f0b5SLisandro Dalcin char buf[2048],*lbuf = NULL; 380fbfcfee5SBarry Smith PetscBool ismain; 3813bf036e2SBarry Smith PetscErrorCode ierr; 382e5c89e4eSSatish Balay 38339a651e2SJacob Faibussowitsch if (!PetscErrorHandlingInitialized) return n; 384e5c89e4eSSatish Balay if (!func) func = "User provided function"; 385e5c89e4eSSatish Balay if (!file) file = "User file"; 3860d577829SBarry Smith if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF; 387e5c89e4eSSatish Balay 388e5c89e4eSSatish Balay /* Compose the message evaluating the print format */ 389e5c89e4eSSatish Balay if (mess) { 390e5c89e4eSSatish Balay va_start(Argp,mess); 3912d609e63SMatthew Knepley PetscVSNPrintf(buf,2048,mess,&fullLength,Argp); 392e5c89e4eSSatish Balay va_end(Argp); 393e5c89e4eSSatish Balay lbuf = buf; 39478179f8bSBarry Smith if (p == PETSC_ERROR_INITIAL) PetscStrncpy(PetscErrorBaseMessage,lbuf,1023); 395e5c89e4eSSatish Balay } 396e5c89e4eSSatish Balay 3974ed0ab5bSBarry Smith if (p == PETSC_ERROR_INITIAL && n != PETSC_ERR_MEMC) PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__); 3984ed0ab5bSBarry Smith 39902c9f0b5SLisandro Dalcin if (!eh) ierr = PetscTraceBackErrorHandler(comm,line,func,file,n,p,lbuf,NULL); 400efca3c55SSatish Balay else ierr = (*eh->handler)(comm,line,func,file,n,p,lbuf,eh->ctx); 401362febeeSStefano Zampini PetscStackClearTop; 402e5c89e4eSSatish Balay 403e5c89e4eSSatish Balay /* 4047233276eSBarry Smith If this is called from the main() routine we call MPI_Abort() instead of 405e5c89e4eSSatish Balay return to allow the parallel program to be properly shutdown. 406e5c89e4eSSatish Balay 4077233276eSBarry Smith Does not call PETSCABORT() since that would provide the wrong source file and line number information 408e5c89e4eSSatish Balay */ 409e5c89e4eSSatish Balay PetscStrncmp(func,"main",4,&ismain); 4107233276eSBarry Smith if (ismain) { 411a0760fecSJunchao Zhang PetscMPIInt errcode; 41265f093c6SBarry Smith errcode = (PetscMPIInt)(0 + 0*line*1000 + ierr); 413baae8e41SSatish Balay if (petscwaitonerrorflg) { PetscSleep(1000); } 41465f093c6SBarry Smith MPI_Abort(MPI_COMM_WORLD,errcode); 4157233276eSBarry Smith } 416a297a907SKarl Rupp 4172c280183SJed Brown #if defined(PETSC_CLANGUAGE_CXX) 418d736bfebSBarry Smith if (p == PETSC_ERROR_IN_CXX) { 4192c280183SJed Brown PetscCxxErrorThrow(); 420d736bfebSBarry Smith } 421d736bfebSBarry Smith #endif 422362febeeSStefano Zampini return ierr; 423e5c89e4eSSatish Balay } 424e5c89e4eSSatish Balay 425e5c89e4eSSatish Balay /* -------------------------------------------------------------------------*/ 426e5c89e4eSSatish Balay 427e5c89e4eSSatish Balay /*@C 428e5c89e4eSSatish Balay PetscIntView - Prints an array of integers; useful for debugging. 429e5c89e4eSSatish Balay 430e5c89e4eSSatish Balay Collective on PetscViewer 431e5c89e4eSSatish Balay 432e5c89e4eSSatish Balay Input Parameters: 433e5c89e4eSSatish Balay + N - number of integers in array 434e5c89e4eSSatish Balay . idx - array of integers 435e5c89e4eSSatish Balay - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 436e5c89e4eSSatish Balay 437e5c89e4eSSatish Balay Level: intermediate 438e5c89e4eSSatish Balay 43995452b02SPatrick Sanan Developer Notes: 44095452b02SPatrick Sanan idx cannot be const because may be passed to binary viewer where byte swapping is done 441300a7f5bSBarry Smith 442db781477SPatrick Sanan .seealso: `PetscRealView()` 443e5c89e4eSSatish Balay @*/ 4447087cfbeSBarry Smith PetscErrorCode PetscIntView(PetscInt N,const PetscInt idx[],PetscViewer viewer) 445e5c89e4eSSatish Balay { 446ca0c3be5SJacob Faibussowitsch PetscMPIInt rank,size; 447e5c89e4eSSatish Balay PetscInt j,i,n = N/20,p = N % 20; 448ace3abfcSBarry Smith PetscBool iascii,isbinary; 449e5c89e4eSSatish Balay MPI_Comm comm; 450e5c89e4eSSatish Balay 451e5c89e4eSSatish Balay PetscFunctionBegin; 452e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 4530700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 4545ab33896SBarry Smith if (N) PetscValidIntPointer(idx,2); 4559566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 4569566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm,&size)); 4579566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 458e5c89e4eSSatish Balay 4599566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 4609566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 461e5c89e4eSSatish Balay if (iascii) { 4629566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 463e5c89e4eSSatish Balay for (i=0; i<n; i++) { 464ca0c3be5SJacob Faibussowitsch if (size > 1) { 4659566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %" PetscInt_FMT ":", rank, 20*i)); 466ca0c3be5SJacob Faibussowitsch } else { 4679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%" PetscInt_FMT ":",20*i)); 468ca0c3be5SJacob Faibussowitsch } 469e5c89e4eSSatish Balay for (j=0; j<20; j++) { 4709566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %" PetscInt_FMT,idx[i*20+j])); 471e5c89e4eSSatish Balay } 4729566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 473e5c89e4eSSatish Balay } 474e5c89e4eSSatish Balay if (p) { 475ca0c3be5SJacob Faibussowitsch if (size > 1) { 4769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %" PetscInt_FMT ":",rank ,20*n)); 477ca0c3be5SJacob Faibussowitsch } else { 4789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%" PetscInt_FMT ":",20*n)); 479ca0c3be5SJacob Faibussowitsch } 4809566063dSJacob Faibussowitsch for (i=0; i<p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %" PetscInt_FMT,idx[20*n+i])); 4819566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 482e5c89e4eSSatish Balay } 4839566063dSJacob Faibussowitsch PetscCall(PetscViewerFlush(viewer)); 4849566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 4856805f65bSBarry Smith } else if (isbinary) { 486ca0c3be5SJacob Faibussowitsch PetscMPIInt *sizes,Ntotal,*displs,NN; 487e5c89e4eSSatish Balay PetscInt *array; 488783b601eSJed Brown 4899566063dSJacob Faibussowitsch PetscCall(PetscMPIIntCast(N,&NN)); 490e5c89e4eSSatish Balay 491e5c89e4eSSatish Balay if (size > 1) { 492e5c89e4eSSatish Balay if (rank) { 4939566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 4949566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_INT,NULL,NULL,NULL,MPIU_INT,0,comm)); 495e5c89e4eSSatish Balay } else { 4969566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size,&sizes)); 4979566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 498e5c89e4eSSatish Balay Ntotal = sizes[0]; 4999566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size,&displs)); 500e5c89e4eSSatish Balay displs[0] = 0; 501e5c89e4eSSatish Balay for (i=1; i<size; i++) { 502e5c89e4eSSatish Balay Ntotal += sizes[i]; 503e5c89e4eSSatish Balay displs[i] = displs[i-1] + sizes[i-1]; 504e5c89e4eSSatish Balay } 5059566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Ntotal,&array)); 5069566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_INT,array,sizes,displs,MPIU_INT,0,comm)); 5079566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_INT)); 5089566063dSJacob Faibussowitsch PetscCall(PetscFree(sizes)); 5099566063dSJacob Faibussowitsch PetscCall(PetscFree(displs)); 5109566063dSJacob Faibussowitsch PetscCall(PetscFree(array)); 511e5c89e4eSSatish Balay } 512e5c89e4eSSatish Balay } else { 5139566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,idx,N,PETSC_INT)); 514e5c89e4eSSatish Balay } 515e5c89e4eSSatish Balay } else { 516e5c89e4eSSatish Balay const char *tname; 5179566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 51898921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 519e5c89e4eSSatish Balay } 520e5c89e4eSSatish Balay PetscFunctionReturn(0); 521e5c89e4eSSatish Balay } 522e5c89e4eSSatish Balay 523e5c89e4eSSatish Balay /*@C 524e5c89e4eSSatish Balay PetscRealView - Prints an array of doubles; useful for debugging. 525e5c89e4eSSatish Balay 526e5c89e4eSSatish Balay Collective on PetscViewer 527e5c89e4eSSatish Balay 528e5c89e4eSSatish Balay Input Parameters: 529cba51d77SBarry Smith + N - number of PetscReal in array 530cba51d77SBarry Smith . idx - array of PetscReal 531e5c89e4eSSatish Balay - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 532e5c89e4eSSatish Balay 533e5c89e4eSSatish Balay Level: intermediate 534e5c89e4eSSatish Balay 53595452b02SPatrick Sanan Developer Notes: 53695452b02SPatrick Sanan idx cannot be const because may be passed to binary viewer where byte swapping is done 537300a7f5bSBarry Smith 538db781477SPatrick Sanan .seealso: `PetscIntView()` 539e5c89e4eSSatish Balay @*/ 5407087cfbeSBarry Smith PetscErrorCode PetscRealView(PetscInt N,const PetscReal idx[],PetscViewer viewer) 541e5c89e4eSSatish Balay { 542ca0c3be5SJacob Faibussowitsch PetscMPIInt rank,size; 543e5c89e4eSSatish Balay PetscInt j,i,n = N/5,p = N % 5; 544ace3abfcSBarry Smith PetscBool iascii,isbinary; 545e5c89e4eSSatish Balay MPI_Comm comm; 546e5c89e4eSSatish Balay 547e5c89e4eSSatish Balay PetscFunctionBegin; 548e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 5490700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 550064a246eSJacob Faibussowitsch PetscValidRealPointer(idx,2); 5519566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 5529566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm,&size)); 5539566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 554e5c89e4eSSatish Balay 5559566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 5569566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 557e5c89e4eSSatish Balay if (iascii) { 5581a989b97SToby Isaac PetscInt tab; 5591a989b97SToby Isaac 5609566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 5619566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tab)); 562e5c89e4eSSatish Balay for (i=0; i<n; i++) { 5639566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tab)); 564ca0c3be5SJacob Faibussowitsch if (size > 1) { 5659566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,5*i)); 566ca0c3be5SJacob Faibussowitsch } else { 5679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",5*i)); 568ca0c3be5SJacob Faibussowitsch } 5699566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, 0)); 570e5c89e4eSSatish Balay for (j=0; j<5; j++) { 5719566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*5+j])); 572e5c89e4eSSatish Balay } 5739566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 574e5c89e4eSSatish Balay } 575e5c89e4eSSatish Balay if (p) { 5769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tab)); 577ca0c3be5SJacob Faibussowitsch if (size > 1) { 5789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,5*n)); 579ca0c3be5SJacob Faibussowitsch } else { 5809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",5*n)); 581ca0c3be5SJacob Faibussowitsch } 5829566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, 0)); 5839566063dSJacob Faibussowitsch for (i=0; i<p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[5*n+i])); 5849566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 585e5c89e4eSSatish Balay } 5869566063dSJacob Faibussowitsch PetscCall(PetscViewerFlush(viewer)); 5879566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tab)); 5889566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 5896805f65bSBarry Smith } else if (isbinary) { 590ca0c3be5SJacob Faibussowitsch PetscMPIInt *sizes,*displs, Ntotal,NN; 591e5c89e4eSSatish Balay PetscReal *array; 592e5c89e4eSSatish Balay 5939566063dSJacob Faibussowitsch PetscCall(PetscMPIIntCast(N,&NN)); 594e5c89e4eSSatish Balay 595e5c89e4eSSatish Balay if (size > 1) { 596e5c89e4eSSatish Balay if (rank) { 5979566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 5989566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,NULL,NULL,NULL,MPIU_REAL,0,comm)); 599e5c89e4eSSatish Balay } else { 6009566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size,&sizes)); 6019566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 602e5c89e4eSSatish Balay Ntotal = sizes[0]; 6039566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size,&displs)); 604e5c89e4eSSatish Balay displs[0] = 0; 605e5c89e4eSSatish Balay for (i=1; i<size; i++) { 606e5c89e4eSSatish Balay Ntotal += sizes[i]; 607e5c89e4eSSatish Balay displs[i] = displs[i-1] + sizes[i-1]; 608e5c89e4eSSatish Balay } 6099566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Ntotal,&array)); 6109566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,array,sizes,displs,MPIU_REAL,0,comm)); 6119566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_REAL)); 6129566063dSJacob Faibussowitsch PetscCall(PetscFree(sizes)); 6139566063dSJacob Faibussowitsch PetscCall(PetscFree(displs)); 6149566063dSJacob Faibussowitsch PetscCall(PetscFree(array)); 615e5c89e4eSSatish Balay } 616e5c89e4eSSatish Balay } else { 6179566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_REAL)); 618e5c89e4eSSatish Balay } 619e5c89e4eSSatish Balay } else { 620e5c89e4eSSatish Balay const char *tname; 6219566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 62298921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 623e5c89e4eSSatish Balay } 624e5c89e4eSSatish Balay PetscFunctionReturn(0); 625e5c89e4eSSatish Balay } 626e5c89e4eSSatish Balay 627e5c89e4eSSatish Balay /*@C 628e5c89e4eSSatish Balay PetscScalarView - Prints an array of scalars; useful for debugging. 629e5c89e4eSSatish Balay 630e5c89e4eSSatish Balay Collective on PetscViewer 631e5c89e4eSSatish Balay 632e5c89e4eSSatish Balay Input Parameters: 633e5c89e4eSSatish Balay + N - number of scalars in array 634e5c89e4eSSatish Balay . idx - array of scalars 635e5c89e4eSSatish Balay - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 636e5c89e4eSSatish Balay 637e5c89e4eSSatish Balay Level: intermediate 638e5c89e4eSSatish Balay 63995452b02SPatrick Sanan Developer Notes: 64095452b02SPatrick Sanan idx cannot be const because may be passed to binary viewer where byte swapping is done 641300a7f5bSBarry Smith 642db781477SPatrick Sanan .seealso: `PetscIntView()`, `PetscRealView()` 643e5c89e4eSSatish Balay @*/ 6447087cfbeSBarry Smith PetscErrorCode PetscScalarView(PetscInt N,const PetscScalar idx[],PetscViewer viewer) 645e5c89e4eSSatish Balay { 646ca0c3be5SJacob Faibussowitsch PetscMPIInt rank,size; 647e5c89e4eSSatish Balay PetscInt j,i,n = N/3,p = N % 3; 648ace3abfcSBarry Smith PetscBool iascii,isbinary; 649e5c89e4eSSatish Balay MPI_Comm comm; 650e5c89e4eSSatish Balay 651e5c89e4eSSatish Balay PetscFunctionBegin; 652e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 653e5c89e4eSSatish Balay PetscValidHeader(viewer,3); 6548c34849bSStefano Zampini if (N) PetscValidScalarPointer(idx,2); 6559566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 6569566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm,&size)); 6579566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 658e5c89e4eSSatish Balay 6599566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 6609566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 661e5c89e4eSSatish Balay if (iascii) { 6629566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 663e5c89e4eSSatish Balay for (i=0; i<n; i++) { 664ca0c3be5SJacob Faibussowitsch if (size > 1) { 6659566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,3*i)); 666ca0c3be5SJacob Faibussowitsch } else { 6679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",3*i)); 668ca0c3be5SJacob Faibussowitsch } 669e5c89e4eSSatish Balay for (j=0; j<3; j++) { 670e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX) 6719566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[i*3+j]),(double)PetscImaginaryPart(idx[i*3+j]))); 672e5c89e4eSSatish Balay #else 6739566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*3+j])); 674e5c89e4eSSatish Balay #endif 675e5c89e4eSSatish Balay } 6769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 677e5c89e4eSSatish Balay } 678e5c89e4eSSatish Balay if (p) { 679ca0c3be5SJacob Faibussowitsch if (size > 1) { 6809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,3*n)); 681ca0c3be5SJacob Faibussowitsch } else { 6829566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",3*n)); 683ca0c3be5SJacob Faibussowitsch } 684e5c89e4eSSatish Balay for (i=0; i<p; i++) { 685e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX) 6869566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[n*3+i]),(double)PetscImaginaryPart(idx[n*3+i]))); 687e5c89e4eSSatish Balay #else 6889566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[3*n+i])); 689e5c89e4eSSatish Balay #endif 690e5c89e4eSSatish Balay } 6919566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 692e5c89e4eSSatish Balay } 6939566063dSJacob Faibussowitsch PetscCall(PetscViewerFlush(viewer)); 6949566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 6956805f65bSBarry Smith } else if (isbinary) { 696ca0c3be5SJacob Faibussowitsch PetscMPIInt *sizes,Ntotal,*displs,NN; 697e5c89e4eSSatish Balay PetscScalar *array; 698e5c89e4eSSatish Balay 6999566063dSJacob Faibussowitsch PetscCall(PetscMPIIntCast(N,&NN)); 700e5c89e4eSSatish Balay 701e5c89e4eSSatish Balay if (size > 1) { 702e5c89e4eSSatish Balay if (rank) { 7039566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 7049566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,NULL,NULL,NULL,MPIU_SCALAR,0,comm)); 705e5c89e4eSSatish Balay } else { 7069566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size,&sizes)); 7079566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 708e5c89e4eSSatish Balay Ntotal = sizes[0]; 7099566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size,&displs)); 710e5c89e4eSSatish Balay displs[0] = 0; 711e5c89e4eSSatish Balay for (i=1; i<size; i++) { 712e5c89e4eSSatish Balay Ntotal += sizes[i]; 713e5c89e4eSSatish Balay displs[i] = displs[i-1] + sizes[i-1]; 714e5c89e4eSSatish Balay } 7159566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Ntotal,&array)); 7169566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,array,sizes,displs,MPIU_SCALAR,0,comm)); 7179566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_SCALAR)); 7189566063dSJacob Faibussowitsch PetscCall(PetscFree(sizes)); 7199566063dSJacob Faibussowitsch PetscCall(PetscFree(displs)); 7209566063dSJacob Faibussowitsch PetscCall(PetscFree(array)); 721e5c89e4eSSatish Balay } 722e5c89e4eSSatish Balay } else { 7239566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_SCALAR)); 724e5c89e4eSSatish Balay } 725e5c89e4eSSatish Balay } else { 726e5c89e4eSSatish Balay const char *tname; 7279566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 72898921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 729e5c89e4eSSatish Balay } 730e5c89e4eSSatish Balay PetscFunctionReturn(0); 731e5c89e4eSSatish Balay } 732e5c89e4eSSatish Balay 733e22e20c5SJunchao Zhang #if defined(PETSC_HAVE_CUDA) 734030f984aSJacob Faibussowitsch #include <petscdevice.h> 735e22e20c5SJunchao Zhang PETSC_EXTERN const char* PetscCUBLASGetErrorName(cublasStatus_t status) 736e22e20c5SJunchao Zhang { 737e22e20c5SJunchao Zhang switch(status) { 738e22e20c5SJunchao Zhang #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 739e22e20c5SJunchao Zhang case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS"; 740e22e20c5SJunchao Zhang case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED"; 741e22e20c5SJunchao Zhang case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED"; 742e22e20c5SJunchao Zhang case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE"; 743e22e20c5SJunchao Zhang case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH"; 744e22e20c5SJunchao Zhang case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR"; 745e22e20c5SJunchao Zhang case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED"; 746e22e20c5SJunchao Zhang case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR"; 747e22e20c5SJunchao Zhang case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED"; 748e22e20c5SJunchao Zhang case CUBLAS_STATUS_LICENSE_ERROR: return "CUBLAS_STATUS_LICENSE_ERROR"; 749e22e20c5SJunchao Zhang #endif 750e22e20c5SJunchao Zhang default: return "unknown error"; 751e22e20c5SJunchao Zhang } 752e22e20c5SJunchao Zhang } 753a4b895e1SBarry Smith PETSC_EXTERN const char* PetscCUSolverGetErrorName(cusolverStatus_t status) 754a4b895e1SBarry Smith { 755a4b895e1SBarry Smith switch(status) { 756a4b895e1SBarry Smith #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 757a4b895e1SBarry Smith case CUSOLVER_STATUS_SUCCESS: return "CUSOLVER_STATUS_SUCCESS"; 758a4b895e1SBarry Smith case CUSOLVER_STATUS_NOT_INITIALIZED: return "CUSOLVER_STATUS_NOT_INITIALIZED"; 759a4b895e1SBarry Smith case CUSOLVER_STATUS_INVALID_VALUE: return "CUSOLVER_STATUS_INVALID_VALUE"; 760a4b895e1SBarry Smith case CUSOLVER_STATUS_ARCH_MISMATCH: return "CUSOLVER_STATUS_ARCH_MISMATCH"; 761a4b895e1SBarry Smith case CUSOLVER_STATUS_INTERNAL_ERROR: return "CUSOLVER_STATUS_INTERNAL_ERROR"; 762030f984aSJacob Faibussowitsch #if (CUDART_VERSION >= 9000) /* CUDA 9.0 had these defined on June 2021 */ 763030f984aSJacob Faibussowitsch case CUSOLVER_STATUS_ALLOC_FAILED: return "CUSOLVER_STATUS_ALLOC_FAILED"; 764030f984aSJacob Faibussowitsch case CUSOLVER_STATUS_MAPPING_ERROR: return "CUSOLVER_STATUS_MAPPING_ERROR"; 765030f984aSJacob Faibussowitsch case CUSOLVER_STATUS_EXECUTION_FAILED: return "CUSOLVER_STATUS_EXECUTION_FAILED"; 766030f984aSJacob Faibussowitsch case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED: return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED"; 767030f984aSJacob Faibussowitsch case CUSOLVER_STATUS_NOT_SUPPORTED : return "CUSOLVER_STATUS_NOT_SUPPORTED "; 768030f984aSJacob Faibussowitsch case CUSOLVER_STATUS_ZERO_PIVOT: return "CUSOLVER_STATUS_ZERO_PIVOT"; 769030f984aSJacob Faibussowitsch case CUSOLVER_STATUS_INVALID_LICENSE: return "CUSOLVER_STATUS_INVALID_LICENSE"; 770a4b895e1SBarry Smith #endif 771030f984aSJacob Faibussowitsch #endif 772030f984aSJacob Faibussowitsch default: return "unknown error"; 773030f984aSJacob Faibussowitsch } 774030f984aSJacob Faibussowitsch } 775030f984aSJacob Faibussowitsch PETSC_EXTERN const char* PetscCUFFTGetErrorName(cufftResult result) 776030f984aSJacob Faibussowitsch { 777030f984aSJacob Faibussowitsch switch (result) { 778030f984aSJacob Faibussowitsch case CUFFT_SUCCESS: return "CUFFT_SUCCESS"; 779030f984aSJacob Faibussowitsch case CUFFT_INVALID_PLAN: return "CUFFT_INVALID_PLAN"; 780030f984aSJacob Faibussowitsch case CUFFT_ALLOC_FAILED: return "CUFFT_ALLOC_FAILED"; 781030f984aSJacob Faibussowitsch case CUFFT_INVALID_TYPE: return "CUFFT_INVALID_TYPE"; 782030f984aSJacob Faibussowitsch case CUFFT_INVALID_VALUE: return "CUFFT_INVALID_VALUE"; 783030f984aSJacob Faibussowitsch case CUFFT_INTERNAL_ERROR: return "CUFFT_INTERNAL_ERROR"; 784030f984aSJacob Faibussowitsch case CUFFT_EXEC_FAILED: return "CUFFT_EXEC_FAILED"; 785030f984aSJacob Faibussowitsch case CUFFT_SETUP_FAILED: return "CUFFT_SETUP_FAILED"; 786030f984aSJacob Faibussowitsch case CUFFT_INVALID_SIZE: return "CUFFT_INVALID_SIZE"; 787030f984aSJacob Faibussowitsch case CUFFT_UNALIGNED_DATA: return "CUFFT_UNALIGNED_DATA"; 788030f984aSJacob Faibussowitsch case CUFFT_INCOMPLETE_PARAMETER_LIST: return "CUFFT_INCOMPLETE_PARAMETER_LIST"; 789030f984aSJacob Faibussowitsch case CUFFT_INVALID_DEVICE: return "CUFFT_INVALID_DEVICE"; 790030f984aSJacob Faibussowitsch case CUFFT_PARSE_ERROR: return "CUFFT_PARSE_ERROR"; 791030f984aSJacob Faibussowitsch case CUFFT_NO_WORKSPACE: return "CUFFT_NO_WORKSPACE"; 792030f984aSJacob Faibussowitsch case CUFFT_NOT_IMPLEMENTED: return "CUFFT_NOT_IMPLEMENTED"; 793030f984aSJacob Faibussowitsch case CUFFT_LICENSE_ERROR: return "CUFFT_LICENSE_ERROR"; 794030f984aSJacob Faibussowitsch case CUFFT_NOT_SUPPORTED: return "CUFFT_NOT_SUPPORTED"; 795a4b895e1SBarry Smith default: return "unknown error"; 796a4b895e1SBarry Smith } 797a4b895e1SBarry Smith } 798e22e20c5SJunchao Zhang #endif 79959af0bd3SScott Kruger 80059af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP) 801030f984aSJacob Faibussowitsch #include <petscdevice.h> 80259af0bd3SScott Kruger PETSC_EXTERN const char* PetscHIPBLASGetErrorName(hipblasStatus_t status) 80359af0bd3SScott Kruger { 80459af0bd3SScott Kruger switch(status) { 80559af0bd3SScott Kruger case HIPBLAS_STATUS_SUCCESS: return "HIPBLAS_STATUS_SUCCESS"; 80659af0bd3SScott Kruger case HIPBLAS_STATUS_NOT_INITIALIZED: return "HIPBLAS_STATUS_NOT_INITIALIZED"; 80759af0bd3SScott Kruger case HIPBLAS_STATUS_ALLOC_FAILED: return "HIPBLAS_STATUS_ALLOC_FAILED"; 80859af0bd3SScott Kruger case HIPBLAS_STATUS_INVALID_VALUE: return "HIPBLAS_STATUS_INVALID_VALUE"; 80959af0bd3SScott Kruger case HIPBLAS_STATUS_ARCH_MISMATCH: return "HIPBLAS_STATUS_ARCH_MISMATCH"; 81059af0bd3SScott Kruger case HIPBLAS_STATUS_MAPPING_ERROR: return "HIPBLAS_STATUS_MAPPING_ERROR"; 81159af0bd3SScott Kruger case HIPBLAS_STATUS_EXECUTION_FAILED: return "HIPBLAS_STATUS_EXECUTION_FAILED"; 81259af0bd3SScott Kruger case HIPBLAS_STATUS_INTERNAL_ERROR: return "HIPBLAS_STATUS_INTERNAL_ERROR"; 81359af0bd3SScott Kruger case HIPBLAS_STATUS_NOT_SUPPORTED: return "HIPBLAS_STATUS_NOT_SUPPORTED"; 81459af0bd3SScott Kruger default: return "unknown error"; 81559af0bd3SScott Kruger } 81659af0bd3SScott Kruger } 81759af0bd3SScott Kruger #endif 818*db9cea48SBarry Smith 819*db9cea48SBarry Smith /*@ 820*db9cea48SBarry Smith PetscMPIErrorString - Given an MPI error code returns the MPI_Error_string() appropriately 821*db9cea48SBarry Smith formatted for displaying with the PETSc error handlers. 822*db9cea48SBarry Smith 823*db9cea48SBarry Smith Input Parameter: 824*db9cea48SBarry Smith . err - the MPI error code 825*db9cea48SBarry Smith 826*db9cea48SBarry Smith Output Parameter: 827*db9cea48SBarry Smith . string - the MPI error message, should declare its length to be larger than MPI_MAX_ERROR_STRING 828*db9cea48SBarry Smith 829*db9cea48SBarry Smith Notes: 830*db9cea48SBarry Smith Does not return an error code or do error handling because it may be called from inside an error handler 831*db9cea48SBarry Smith 832*db9cea48SBarry Smith @*/ 833*db9cea48SBarry Smith void PetscMPIErrorString(PetscMPIInt err, char* string) 834*db9cea48SBarry Smith { 835*db9cea48SBarry Smith char errorstring[MPI_MAX_ERROR_STRING]; 836*db9cea48SBarry Smith PetscMPIInt len, j = 0; 837*db9cea48SBarry Smith 838*db9cea48SBarry Smith MPI_Error_string(err,(char*)errorstring,&len); 839*db9cea48SBarry Smith for (PetscMPIInt i=0; i<len; i++) { 840*db9cea48SBarry Smith string[j++] = errorstring[i]; 841*db9cea48SBarry Smith if (errorstring[i] == '\n') { 842*db9cea48SBarry Smith for (PetscMPIInt k=0; k<16; k++) string[j++] = ' '; 843*db9cea48SBarry Smith } 844*db9cea48SBarry Smith } 845*db9cea48SBarry Smith string[j] = 0; 846*db9cea48SBarry Smith } 847*db9cea48SBarry Smith 848