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