1e5c89e4eSSatish Balay #define PETSC_DLL 2e5c89e4eSSatish Balay 3107894f0SSatish Balay #include "petscsys.h" /*I "petscsys.h" I*/ 4107894f0SSatish Balay #include "petscconfiginfo.h" 5e5c89e4eSSatish Balay 6e5c89e4eSSatish Balay #undef __FUNCT__ 7e5c89e4eSSatish Balay #define __FUNCT__ "PetscIgnoreErrorHandler" 8e5c89e4eSSatish Balay /*@C 9e5c89e4eSSatish Balay PetscIgnoreErrorHandler - Ignores the error, allows program to continue as if error did not occure 10e5c89e4eSSatish Balay 11e5c89e4eSSatish Balay Not Collective 12e5c89e4eSSatish Balay 13e5c89e4eSSatish Balay Input Parameters: 14e32f2f54SBarry Smith + comm - communicator over which error occurred 15e32f2f54SBarry Smith . line - the line number of the error (indicated by __LINE__) 16e5c89e4eSSatish Balay . func - the function where error is detected (indicated by __FUNCT__) 17e5c89e4eSSatish Balay . file - the file in which the error was detected (indicated by __FILE__) 18e5c89e4eSSatish Balay . dir - the directory of the file (indicated by __SDIR__) 19e5c89e4eSSatish Balay . mess - an error text string, usually just printed to the screen 20e5c89e4eSSatish Balay . n - the generic error number 21e5c89e4eSSatish Balay . p - specific error number 22e5c89e4eSSatish Balay - ctx - error handler context 23e5c89e4eSSatish Balay 24e5c89e4eSSatish Balay Level: developer 25e5c89e4eSSatish Balay 26e5c89e4eSSatish Balay Notes: 27e5c89e4eSSatish Balay Most users need not directly employ this routine and the other error 28e5c89e4eSSatish Balay handlers, but can instead use the simplified interface SETERRQ, which has 29e5c89e4eSSatish Balay the calling sequence 30e32f2f54SBarry Smith $ SETERRQ(comm,number,p,mess) 31e5c89e4eSSatish Balay 32e5c89e4eSSatish Balay Notes for experienced users: 33e5c89e4eSSatish Balay Use PetscPushErrorHandler() to set the desired error handler. The 34e5c89e4eSSatish Balay currently available PETSc error handlers include PetscTraceBackErrorHandler(), 35e8fb0fc0SBarry Smith PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler() 36e5c89e4eSSatish Balay 37e5c89e4eSSatish Balay Concepts: error handler^traceback 38e5c89e4eSSatish Balay Concepts: traceback^generating 39e5c89e4eSSatish Balay 40e5c89e4eSSatish Balay .seealso: PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), 41e5c89e4eSSatish Balay PetscAbortErrorHandler(), PetscTraceBackErrorHandler() 42e5c89e4eSSatish Balay @*/ 43e32f2f54SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p,const char *mess,void *ctx) 44e5c89e4eSSatish Balay { 45e5c89e4eSSatish Balay PetscFunctionBegin; 46e5c89e4eSSatish Balay PetscFunctionReturn(n); 47e5c89e4eSSatish Balay } 48e5c89e4eSSatish Balay 49107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/ 50107894f0SSatish Balay 51107894f0SSatish Balay static char arch[10],hostname[64],username[16],pname[PETSC_MAX_PATH_LEN],date[64]; 52107894f0SSatish Balay static PetscTruth PetscErrorPrintfInitializeCalled = PETSC_FALSE; 533f6e4ae9SSatish Balay static char version[256]; 54107894f0SSatish Balay 55107894f0SSatish Balay #undef __FUNCT__ 56107894f0SSatish Balay #define __FUNCT__ "PetscErrorPrintfInitialize" 57107894f0SSatish Balay /* 58107894f0SSatish Balay Initializes arch, hostname, username,date so that system calls do NOT need 59107894f0SSatish Balay to be made during the error handler. 60107894f0SSatish Balay */ 61107894f0SSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize() 62107894f0SSatish Balay { 63107894f0SSatish Balay PetscErrorCode ierr; 6490d69ab7SBarry Smith PetscTruth use_stdout = PETSC_FALSE,use_none = PETSC_FALSE; 65107894f0SSatish Balay 66107894f0SSatish Balay PetscFunctionBegin; 67107894f0SSatish Balay ierr = PetscGetArchType(arch,10);CHKERRQ(ierr); 68107894f0SSatish Balay ierr = PetscGetHostName(hostname,64);CHKERRQ(ierr); 69107894f0SSatish Balay ierr = PetscGetUserName(username,16);CHKERRQ(ierr); 70107894f0SSatish Balay ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 71107894f0SSatish Balay ierr = PetscGetDate(date,64);CHKERRQ(ierr); 72a523d312SBarry Smith ierr = PetscGetVersion(version,256);CHKERRQ(ierr); 73e8fb0fc0SBarry Smith 7490d69ab7SBarry Smith ierr = PetscOptionsGetTruth(PETSC_NULL,"-error_output_stdout",&use_stdout,PETSC_NULL);CHKERRQ(ierr); 751179db26SBarry Smith if (use_stdout) { 761179db26SBarry Smith PETSC_STDERR = PETSC_STDOUT; 771179db26SBarry Smith } 7890d69ab7SBarry Smith ierr = PetscOptionsGetTruth(PETSC_NULL,"-error_output_none",&use_none,PETSC_NULL);CHKERRQ(ierr); 791179db26SBarry Smith if (use_none) { 801179db26SBarry Smith PetscErrorPrintf = PetscErrorPrintfNone; 81e8fb0fc0SBarry Smith } 82107894f0SSatish Balay PetscErrorPrintfInitializeCalled = PETSC_TRUE; 83107894f0SSatish Balay PetscFunctionReturn(0); 84107894f0SSatish Balay } 85107894f0SSatish Balay 86e8fb0fc0SBarry Smith #undef __FUNCT__ 87e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfNone" 88e8fb0fc0SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfNone(const char format[],...) 89e8fb0fc0SBarry Smith { 90e8fb0fc0SBarry Smith return 0; 91e8fb0fc0SBarry Smith } 92e8fb0fc0SBarry Smith 93e8fb0fc0SBarry Smith #undef __FUNCT__ 94e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfDefault" 95e8fb0fc0SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfDefault(const char format[],...) 96e8fb0fc0SBarry Smith { 97e8fb0fc0SBarry Smith va_list Argp; 98e8fb0fc0SBarry Smith static PetscTruth PetscErrorPrintfCalled = PETSC_FALSE; 99e8fb0fc0SBarry Smith 100e8fb0fc0SBarry Smith /* 101e8fb0fc0SBarry Smith This function does not call PetscFunctionBegin and PetscFunctionReturn() because 102e8fb0fc0SBarry Smith it may be called by PetscStackView(). 103e8fb0fc0SBarry Smith 104e8fb0fc0SBarry Smith This function does not do error checking because it is called by the error handlers. 105e8fb0fc0SBarry Smith */ 106e8fb0fc0SBarry Smith 107e8fb0fc0SBarry Smith if (!PetscErrorPrintfCalled) { 108e8fb0fc0SBarry Smith PetscErrorPrintfCalled = PETSC_TRUE; 109e8fb0fc0SBarry Smith 110e8fb0fc0SBarry Smith /* 111e8fb0fc0SBarry Smith On the SGI machines and Cray T3E, if errors are generated "simultaneously" by 112e8fb0fc0SBarry Smith different processors, the messages are printed all jumbled up; to try to 113e8fb0fc0SBarry Smith prevent this we have each processor wait based on their rank 114e8fb0fc0SBarry Smith */ 115e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR) 116e8fb0fc0SBarry Smith { 117e8fb0fc0SBarry Smith PetscMPIInt rank; 118e8fb0fc0SBarry Smith if (PetscGlobalRank > 8) rank = 8; else rank = PetscGlobalRank; 119a6d0e24fSJed Brown PetscSleep((PetscReal)rank); 120e8fb0fc0SBarry Smith } 121e8fb0fc0SBarry Smith #endif 122e8fb0fc0SBarry Smith } 123e8fb0fc0SBarry Smith 1241179db26SBarry Smith PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank); 125e8fb0fc0SBarry Smith va_start(Argp,format); 1261179db26SBarry Smith (*PetscVFPrintf)(PETSC_STDERR,format,Argp); 127e8fb0fc0SBarry Smith va_end(Argp); 128e8fb0fc0SBarry Smith return 0; 129e8fb0fc0SBarry Smith } 130e8fb0fc0SBarry Smith 131e5c89e4eSSatish Balay #undef __FUNCT__ 132e5c89e4eSSatish Balay #define __FUNCT__ "PetscTraceBackErrorHandler" 133e5c89e4eSSatish Balay /*@C 134e5c89e4eSSatish Balay 135e5c89e4eSSatish Balay PetscTraceBackErrorHandler - Default error handler routine that generates 136e5c89e4eSSatish Balay a traceback on error detection. 137e5c89e4eSSatish Balay 138e5c89e4eSSatish Balay Not Collective 139e5c89e4eSSatish Balay 140e5c89e4eSSatish Balay Input Parameters: 141e32f2f54SBarry Smith + comm - communicator over which error occurred 142e32f2f54SBarry Smith . line - the line number of the error (indicated by __LINE__) 143e5c89e4eSSatish Balay . func - the function where error is detected (indicated by __FUNCT__) 144e5c89e4eSSatish Balay . file - the file in which the error was detected (indicated by __FILE__) 145e5c89e4eSSatish Balay . dir - the directory of the file (indicated by __SDIR__) 146e5c89e4eSSatish Balay . mess - an error text string, usually just printed to the screen 147e5c89e4eSSatish Balay . n - the generic error number 148e5c89e4eSSatish Balay . p - specific error number 149e5c89e4eSSatish Balay - ctx - error handler context 150e5c89e4eSSatish Balay 151e5c89e4eSSatish Balay Level: developer 152e5c89e4eSSatish Balay 153e5c89e4eSSatish Balay Notes: 154e5c89e4eSSatish Balay Most users need not directly employ this routine and the other error 155e5c89e4eSSatish Balay handlers, but can instead use the simplified interface SETERRQ, which has 156e5c89e4eSSatish Balay the calling sequence 157e32f2f54SBarry Smith $ SETERRQ(comm,number,p,mess) 158e5c89e4eSSatish Balay 159e5c89e4eSSatish Balay Notes for experienced users: 160e5c89e4eSSatish Balay Use PetscPushErrorHandler() to set the desired error handler. The 161e5c89e4eSSatish Balay currently available PETSc error handlers include PetscTraceBackErrorHandler(), 162e8fb0fc0SBarry Smith PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler() 163e5c89e4eSSatish Balay 164e5c89e4eSSatish Balay Concepts: error handler^traceback 165e5c89e4eSSatish Balay Concepts: traceback^generating 166e5c89e4eSSatish Balay 167e5c89e4eSSatish Balay .seealso: PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), 168e5c89e4eSSatish Balay PetscAbortErrorHandler() 169e5c89e4eSSatish Balay @*/ 170e32f2f54SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p,const char *mess,void *ctx) 171e5c89e4eSSatish Balay { 172e5c89e4eSSatish Balay PetscLogDouble mem,rss; 17390d69ab7SBarry Smith PetscTruth flg1 = PETSC_FALSE,flg2 = PETSC_FALSE; 174*997adca8SBarry Smith PetscMPIInt rank = 0; 175e5c89e4eSSatish Balay 176e5c89e4eSSatish Balay PetscFunctionBegin; 177*997adca8SBarry Smith if (comm != PETSC_COMM_SELF) { 178*997adca8SBarry Smith MPI_Comm_rank(comm,&rank); 179*997adca8SBarry Smith } 180*997adca8SBarry Smith if (!rank) { 181e5c89e4eSSatish Balay if (p == 1) { 182107894f0SSatish Balay (*PetscErrorPrintf)("--------------------- Error Message ------------------------------------\n"); 183e5c89e4eSSatish Balay if (n == PETSC_ERR_MEM) { 184e5c89e4eSSatish Balay (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n"); 185e5c89e4eSSatish Balay (*PetscErrorPrintf)("too large an object or bleeding by not properly\n"); 186e5c89e4eSSatish Balay (*PetscErrorPrintf)("destroying unneeded objects.\n"); 187e5c89e4eSSatish Balay PetscMallocGetCurrentUsage(&mem); 188e5c89e4eSSatish Balay PetscMemoryGetCurrentUsage(&rss); 18990d69ab7SBarry Smith PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL); 19090d69ab7SBarry Smith PetscOptionsGetTruth(PETSC_NULL,"-malloc_log",&flg2,PETSC_NULL); 191e5c89e4eSSatish Balay if (flg2) { 192e5c89e4eSSatish Balay PetscMallocDumpLog(stdout); 193e5c89e4eSSatish Balay } else { 194e5c89e4eSSatish Balay (*PetscErrorPrintf)("Memory allocated %D Memory used by process %D\n",(PetscInt)mem,(PetscInt)rss); 195e5c89e4eSSatish Balay if (flg1) { 196e5c89e4eSSatish Balay PetscMallocDump(stdout); 197e5c89e4eSSatish Balay } else { 198e5c89e4eSSatish Balay (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n"); 199e5c89e4eSSatish Balay } 200e5c89e4eSSatish Balay } 201e5c89e4eSSatish Balay } else { 202e5c89e4eSSatish Balay const char *text; 203e5c89e4eSSatish Balay PetscErrorMessage(n,&text,PETSC_NULL); 204e5c89e4eSSatish Balay if (text) (*PetscErrorPrintf)("%s!\n",text); 205e5c89e4eSSatish Balay } 206e5c89e4eSSatish Balay if (mess) { 207e5c89e4eSSatish Balay (*PetscErrorPrintf)("%s!\n",mess); 208e5c89e4eSSatish Balay } 209107894f0SSatish Balay (*PetscErrorPrintf)("------------------------------------------------------------------------\n"); 210107894f0SSatish Balay (*PetscErrorPrintf)("%s\n",version); 211107894f0SSatish Balay (*PetscErrorPrintf)("See docs/changes/index.html for recent updates.\n"); 212107894f0SSatish Balay (*PetscErrorPrintf)("See docs/faq.html for hints about trouble shooting.\n"); 213107894f0SSatish Balay (*PetscErrorPrintf)("See docs/index.html for manual pages.\n"); 214107894f0SSatish Balay (*PetscErrorPrintf)("------------------------------------------------------------------------\n"); 215107894f0SSatish Balay if (PetscErrorPrintfInitializeCalled) { 216107894f0SSatish Balay (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date); 217e5c89e4eSSatish Balay } 218107894f0SSatish Balay (*PetscErrorPrintf)("Libraries linked from %s\n",PETSC_LIB_DIR); 219107894f0SSatish Balay (*PetscErrorPrintf)("Configure run at %s\n",petscconfigureruntime); 220107894f0SSatish Balay (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions); 221107894f0SSatish Balay (*PetscErrorPrintf)("------------------------------------------------------------------------\n"); 222107894f0SSatish Balay } 223*997adca8SBarry Smith /* print line of stack trace */ 224f5ad10bcSSatish Balay (*PetscErrorPrintf)("%s() line %d in %s%s\n",fun,line,dir,file); 225*997adca8SBarry Smith } else { 226*997adca8SBarry Smith /* do not print error messages since process 0 will print them, sleep before aborting so will not accidently kill process 0*/ 227*997adca8SBarry Smith PetscSleep(10.0); 228*997adca8SBarry Smith abort(); 229*997adca8SBarry Smith } 230e5c89e4eSSatish Balay PetscFunctionReturn(n); 231e5c89e4eSSatish Balay } 232e5c89e4eSSatish Balay 2330577ce7cSSatish Balay #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_EXTERN_CXX) 234fd705b32SMatthew Knepley #undef __FUNCT__ 235fd705b32SMatthew Knepley #define __FUNCT__ "PetscTraceBackErrorHandlerCxx" 236fd705b32SMatthew Knepley /*@C 237fd705b32SMatthew Knepley 238fd705b32SMatthew Knepley PetscTraceBackErrorHandlerCxx - Default error handler routine that generates 239fd705b32SMatthew Knepley a traceback on error detection. 240fd705b32SMatthew Knepley 241fd705b32SMatthew Knepley Not Collective 242fd705b32SMatthew Knepley 243fd705b32SMatthew Knepley Input Parameters: 244fd705b32SMatthew Knepley + line - the line number of the error (indicated by __LINE__) 245fd705b32SMatthew Knepley . func - the function where error is detected (indicated by __FUNCT__) 246fd705b32SMatthew Knepley . file - the file in which the error was detected (indicated by __FILE__) 247fd705b32SMatthew Knepley . dir - the directory of the file (indicated by __SDIR__) 248fd705b32SMatthew Knepley . n - the generic error number 249fd705b32SMatthew Knepley . p - specific error number 250fd705b32SMatthew Knepley - msg - The message stream 251fd705b32SMatthew Knepley 252fd705b32SMatthew Knepley Level: developer 253fd705b32SMatthew Knepley 254fd705b32SMatthew Knepley Notes: 255fd705b32SMatthew Knepley Most users need not directly employ this routine and the other error 256fd705b32SMatthew Knepley handlers, but can instead use the simplified interface SETERROR, which has 257fd705b32SMatthew Knepley the calling sequence 258fd705b32SMatthew Knepley $ SETERROR(number,p,mess) 259fd705b32SMatthew Knepley 260fd705b32SMatthew Knepley Concepts: error handler^traceback 261fd705b32SMatthew Knepley Concepts: traceback^generating 262fd705b32SMatthew Knepley 263fd705b32SMatthew Knepley .seealso: PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler() 264fd705b32SMatthew Knepley @*/ 265fd705b32SMatthew Knepley void PETSC_DLLEXPORT PetscTraceBackErrorHandlerCxx(int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p, std::ostringstream& msg) 266fd705b32SMatthew Knepley { 267fd705b32SMatthew Knepley if (p == 1) { 268fd705b32SMatthew Knepley PetscLogDouble mem, rss; 26990d69ab7SBarry Smith PetscTruth flg1 = PETSC_FALSE, flg2 = PETSC_FALSE; 270fd705b32SMatthew Knepley 271fd705b32SMatthew Knepley msg << "--------------------- Error Message ------------------------------------" << std::endl; 272fd705b32SMatthew Knepley if (n == PETSC_ERR_MEM) { 273fd705b32SMatthew Knepley msg << "Out of memory. This could be due to allocating" << std::endl; 274fd705b32SMatthew Knepley msg << "too large an object or bleeding by not properly" << std::endl; 275fd705b32SMatthew Knepley msg << "destroying unneeded objects." << std::endl; 276fd705b32SMatthew Knepley PetscMallocGetCurrentUsage(&mem); 277fd705b32SMatthew Knepley PetscMemoryGetCurrentUsage(&rss); 27890d69ab7SBarry Smith PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL); 27990d69ab7SBarry Smith PetscOptionsGetTruth(PETSC_NULL,"-malloc_log",&flg2,PETSC_NULL); 280fd705b32SMatthew Knepley if (flg2) { 281fd705b32SMatthew Knepley //PetscMallocDumpLog(stdout); 282fd705b32SMatthew Knepley msg << "Option -malloc_log does not work in C++." << std::endl; 283fd705b32SMatthew Knepley } else { 284fd705b32SMatthew Knepley msg << "Memory allocated " << mem << " Memory used by process " << rss << std::endl; 285fd705b32SMatthew Knepley if (flg1) { 286fd705b32SMatthew Knepley //PetscMallocDump(stdout); 287fd705b32SMatthew Knepley msg << "Option -malloc_dump does not work in C++." << std::endl; 288fd705b32SMatthew Knepley } else { 289fd705b32SMatthew Knepley msg << "Try running with -malloc_dump or -malloc_log for info." << std::endl; 290fd705b32SMatthew Knepley } 291fd705b32SMatthew Knepley } 292fd705b32SMatthew Knepley } else { 293fd705b32SMatthew Knepley const char *text; 294fd705b32SMatthew Knepley 295fd705b32SMatthew Knepley PetscErrorMessage(n,&text,PETSC_NULL); 296fd705b32SMatthew Knepley if (text) {msg << text << "!" << std::endl;} 297fd705b32SMatthew Knepley } 298fd705b32SMatthew Knepley msg << "------------------------------------------------------------------------" << std::endl; 299fd705b32SMatthew Knepley msg << version << std::endl; 300fd705b32SMatthew Knepley msg << "See docs/changes/index.html for recent updates." << std::endl; 301fd705b32SMatthew Knepley msg << "See docs/faq.html for hints about trouble shooting." << std::endl; 302fd705b32SMatthew Knepley msg << "See docs/index.html for manual pages." << std::endl; 303fd705b32SMatthew Knepley msg << "------------------------------------------------------------------------" << std::endl; 304fd705b32SMatthew Knepley if (PetscErrorPrintfInitializeCalled) { 305fd705b32SMatthew Knepley msg << pname << " on a " << arch << " named " << hostname << " by " << username << " " << date << std::endl; 306fd705b32SMatthew Knepley } 307fd705b32SMatthew Knepley msg << "Libraries linked from " << PETSC_LIB_DIR << std::endl; 308fd705b32SMatthew Knepley msg << "Configure run at " << petscconfigureruntime << std::endl; 309fd705b32SMatthew Knepley msg << "Configure options " << petscconfigureoptions << std::endl; 310fd705b32SMatthew Knepley msg << "------------------------------------------------------------------------" << std::endl; 311fd705b32SMatthew Knepley } else { 312fd705b32SMatthew Knepley msg << fun<<"() line " << line << " in " << dir << file << std::endl; 313fd705b32SMatthew Knepley } 314fd705b32SMatthew Knepley } 315fd705b32SMatthew Knepley #endif 316