1 2 /* 3 Code that allows one to set the error handlers 4 */ 5 #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 6 #include <petscviewer.h> 7 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 }; 26 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 = PETSC_STATIC_ARRAY_LENGTH(PetscAbortSourceFiles); 32 PetscBool match; 33 char subpath[PETSC_MAX_PATH_LEN]; 34 35 ierr = PetscStackView(stderr);if (ierr) return ierr; 36 *idx = 1; 37 for (i=2; i<n; i++) { 38 ierr = PetscFixFilename(PetscAbortSourceFiles[i],subpath);if (ierr) return ierr; 39 ierr = PetscStrendswith(filepath,subpath,&match);if (ierr) return ierr; 40 if (match) {*idx = i; break;} 41 } 42 return 0; 43 } 44 45 typedef struct _EH *EH; 46 struct _EH { 47 PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*); 48 void *ctx; 49 EH previous; 50 }; 51 52 static EH eh = NULL; 53 54 /*@C 55 PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to 56 load the file where the error occurred. Then calls the "previous" error handler. 57 58 Not Collective 59 60 Input Parameters: 61 + comm - communicator over which error occurred 62 . line - the line number of the error (indicated by __LINE__) 63 . file - the file in which the error was detected (indicated by __FILE__) 64 . mess - an error text string, usually just printed to the screen 65 . n - the generic error number 66 . p - specific error number 67 - ctx - error handler context 68 69 Options Database Key: 70 . -on_error_emacs <machinename> - will contact machinename to open the Emacs client there 71 72 Level: developer 73 74 Notes: 75 You must put (server-start) in your .emacs file for the emacsclient software to work 76 77 Developer Note: 78 Since this is an error handler it cannot call PetscCall(); thus we just return if an error is detected. 79 80 .seealso: PetscError(), PetscPushErrorHandler(), PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(), 81 PetscAbortErrorHandler(), PetscMPIAbortErrorHandler(), PetscTraceBackErrorHandler(), PetscReturnErrorHandler() 82 @*/ 83 PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) 84 { 85 PetscErrorCode ierr; 86 char command[PETSC_MAX_PATH_LEN]; 87 const char *pdir; 88 FILE *fp; 89 90 PetscFunctionBegin; 91 ierr = PetscGetPetscDir(&pdir);if (ierr) PetscFunctionReturn(ierr); 92 sprintf(command,"cd %s; emacsclient --no-wait +%d %s\n",pdir,line,file); 93 #if defined(PETSC_HAVE_POPEN) 94 ierr = PetscPOpen(MPI_COMM_WORLD,(char*)ctx,command,"r",&fp);if (ierr) PetscFunctionReturn(ierr); 95 ierr = PetscPClose(MPI_COMM_WORLD,fp);if (ierr) PetscFunctionReturn(ierr); 96 #else 97 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 98 #endif 99 ierr = PetscPopErrorHandler();if (ierr) PetscFunctionReturn(ierr); /* remove this handler from the stack of handlers */ 100 if (!eh) { 101 ierr = PetscTraceBackErrorHandler(comm,line,fun,file,n,p,mess,NULL);if (ierr) PetscFunctionReturn(ierr); 102 } else { 103 ierr = (*eh->handler)(comm,line,fun,file,n,p,mess,eh->ctx);if (ierr) PetscFunctionReturn(ierr); 104 } 105 PetscFunctionReturn(ierr); 106 } 107 108 /*@C 109 PetscPushErrorHandler - Sets a routine to be called on detection of errors. 110 111 Not Collective 112 113 Input Parameters: 114 + handler - error handler routine 115 - ctx - optional handler context that contains information needed by the handler (for 116 example file pointers for error messages etc.) 117 118 Calling sequence of handler: 119 $ int handler(MPI_Comm comm,int line,char *func,char *file,PetscErrorCode n,int p,char *mess,void *ctx); 120 121 + comm - communicator over which error occurred 122 . line - the line number of the error (indicated by __LINE__) 123 . file - the file in which the error was detected (indicated by __FILE__) 124 . n - the generic error number (see list defined in include/petscerror.h) 125 . p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT 126 . mess - an error text string, usually just printed to the screen 127 - ctx - the error handler context 128 129 Options Database Keys: 130 + -on_error_attach_debugger <noxterm,gdb or dbx> - starts up the debugger if an error occurs 131 - -on_error_abort - aborts the program if an error occurs 132 133 Level: intermediate 134 135 Notes: 136 The currently available PETSc error handlers include PetscTraceBackErrorHandler(), 137 PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler(), PetscReturnErrorHandler(). 138 139 Fortran Notes: 140 You can only push one error handler from Fortran before poping it. 141 142 .seealso: PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), PetscTraceBackErrorHandler(), PetscPushSignalHandler() 143 144 @*/ 145 PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void *ctx) 146 { 147 EH neweh; 148 149 PetscFunctionBegin; 150 PetscCall(PetscNew(&neweh)); 151 if (eh) neweh->previous = eh; 152 else neweh->previous = NULL; 153 neweh->handler = handler; 154 neweh->ctx = ctx; 155 eh = neweh; 156 PetscFunctionReturn(0); 157 } 158 159 /*@ 160 PetscPopErrorHandler - Removes the latest error handler that was 161 pushed with PetscPushErrorHandler(). 162 163 Not Collective 164 165 Level: intermediate 166 167 .seealso: PetscPushErrorHandler() 168 @*/ 169 PetscErrorCode PetscPopErrorHandler(void) 170 { 171 EH tmp; 172 173 PetscFunctionBegin; 174 if (!eh) PetscFunctionReturn(0); 175 tmp = eh; 176 eh = eh->previous; 177 PetscCall(PetscFree(tmp)); 178 PetscFunctionReturn(0); 179 } 180 181 /*@C 182 PetscReturnErrorHandler - Error handler that causes a return without printing an error message. 183 184 Not Collective 185 186 Input Parameters: 187 + comm - communicator over which error occurred 188 . line - the line number of the error (indicated by __LINE__) 189 . file - the file in which the error was detected (indicated by __FILE__) 190 . mess - an error text string, usually just printed to the screen 191 . n - the generic error number 192 . p - specific error number 193 - ctx - error handler context 194 195 Level: developer 196 197 Notes: 198 Most users need not directly employ this routine and the other error 199 handlers, but can instead use the simplified interface SETERRQ, which has 200 the calling sequence 201 $ SETERRQ(comm,number,mess) 202 203 PetscIgnoreErrorHandler() does the same thing as this function, but is deprecated, you should use this function. 204 205 Use PetscPushErrorHandler() to set the desired error handler. 206 207 .seealso: PetscPushErrorHandler(), PetscPopErrorHandler(), PetscError(), PetscAbortErrorHandler(), PetscMPIAbortErrorHandler(), PetscTraceBackErrorHandler(), 208 PetscAttachDebuggerErrorHandler(), PetscEmacsClientErrorHandler() 209 @*/ 210 PetscErrorCode PetscReturnErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) 211 { 212 return n; 213 } 214 215 static char PetscErrorBaseMessage[1024]; 216 /* 217 The numerical values for these are defined in include/petscerror.h; any changes 218 there must also be made here 219 */ 220 static const char *PetscErrorStrings[] = { 221 /*55 */ "Out of memory", 222 "No support for this operation for this object type", 223 "No support for this operation on this system", 224 /*58 */ "Operation done in wrong order", 225 /*59 */ "Signal received", 226 /*60 */ "Nonconforming object sizes", 227 "Argument aliasing not permitted", 228 "Invalid argument", 229 /*63 */ "Argument out of range", 230 "Corrupt argument: https://petsc.org/release/faq/#valgrind", 231 "Unable to open file", 232 "Read from file failed", 233 "Write to file failed", 234 "Invalid pointer", 235 /*69 */ "Arguments must have same type", 236 /*70 */ "Attempt to use a pointer that does not point to a valid accessible location", 237 /*71 */ "Zero pivot in LU factorization: https://petsc.org/release/faq/#zeropivot", 238 /*72 */ "Floating point exception", 239 /*73 */ "Object is in wrong state", 240 "Corrupted Petsc object", 241 "Arguments are incompatible", 242 "Error in external library", 243 /*77 */ "Petsc has generated inconsistent data", 244 "Memory corruption: https://petsc.org/release/faq/#valgrind", 245 "Unexpected data in file", 246 /*80 */ "Arguments must have same communicators", 247 /*81 */ "Zero pivot in Cholesky factorization: https://petsc.org/release/faq/#zeropivot", 248 " ", 249 " ", 250 "Overflow in integer operation: https://petsc.org/release/faq/#64-bit-indices", 251 /*85 */ "Null argument, when expecting valid pointer", 252 /*86 */ "Unknown type. Check for miss-spelling or missing package: https://petsc.org/release/install/install/#external-packages", 253 /*87 */ "MPI library at runtime is not compatible with MPI used at compile time", 254 /*88 */ "Error in system call", 255 /*89 */ "Object Type not set: https://petsc.org/release/faq/#object-type-not-set", 256 /*90 */ " ", 257 /* */ " ", 258 /*92 */ "See https://petsc.org/release/overview/linear_solve_table/ for possible LU and Cholesky solvers", 259 /*93 */ "You cannot overwrite this option since that will conflict with other previously set options", 260 /*94 */ "Example/application run with number of MPI ranks it does not support", 261 /*95 */ "Missing or incorrect user input ", 262 /*96 */ "GPU resources unavailable ", 263 /*97 */ "GPU error ", 264 /*98 */ "General MPI error " 265 }; 266 267 /*@C 268 PetscErrorMessage - returns the text string associated with a PETSc error code. 269 270 Not Collective 271 272 Input Parameter: 273 . errnum - the error code 274 275 Output Parameters: 276 + text - the error message (NULL if not desired) 277 - specific - the specific error message that was set with SETERRxxx() or PetscError(). (NULL if not desired) 278 279 Level: developer 280 281 .seealso: PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscError(), SETERRQ(), PetscCall() 282 PetscAbortErrorHandler(), PetscTraceBackErrorHandler() 283 @*/ 284 PetscErrorCode PetscErrorMessage(int errnum,const char *text[],char **specific) 285 { 286 PetscFunctionBegin; 287 if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) *text = PetscErrorStrings[errnum-PETSC_ERR_MIN_VALUE-1]; 288 else if (text) *text = NULL; 289 290 if (specific) *specific = PetscErrorBaseMessage; 291 PetscFunctionReturn(0); 292 } 293 294 #if defined(PETSC_CLANGUAGE_CXX) 295 /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software 296 * would be broken if implementations did not handle it it some common cases. However, keep in mind 297 * 298 * Rule 62. Don't allow exceptions to propagate across module boundaries 299 * 300 * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface 301 * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed. 302 * 303 * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message 304 * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that 305 * seems crazy to me. 306 */ 307 #include <sstream> 308 #include <stdexcept> 309 static void PetscCxxErrorThrow() 310 { 311 const char *str; 312 if (eh && eh->ctx) { 313 std::ostringstream *msg; 314 msg = (std::ostringstream*) eh->ctx; 315 str = msg->str().c_str(); 316 } else str = "Error detected in C PETSc"; 317 318 throw std::runtime_error(str); 319 } 320 #endif 321 322 /*@C 323 PetscError - Routine that is called when an error has been detected, usually called through the macro SETERRQ(PETSC_COMM_SELF,). 324 325 Collective on comm 326 327 Input Parameters: 328 + comm - communicator over which error occurred. ALL ranks of this communicator MUST call this routine 329 . line - the line number of the error (indicated by __LINE__) 330 . func - the function name in which the error was detected 331 . file - the file in which the error was detected (indicated by __FILE__) 332 . n - the generic error number 333 . p - PETSC_ERROR_INITIAL indicates the error was initially detected, PETSC_ERROR_REPEAT indicates this is a traceback from a previously detected error 334 - mess - formatted message string - aka printf 335 336 Options Database: 337 + -error_output_stdout - output the error messages to stdout instead of the default stderr 338 - -error_output_none - do not output the error messages 339 340 Level: intermediate 341 342 Notes: 343 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 344 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, 345 KSPGetConvergedReason() that indicates if the solve was successful or not. The option -ksp_error_if_not_converged, for example, turns numerical failures into 346 hard errors managed via PetscError(). 347 348 PETSc provides a rich supply of error handlers, see the list below, and users can also provide their own error handlers. 349 350 Most users need not directly use this routine and the error handlers, but 351 can instead use the simplified interface SETERRQ, which has the calling 352 sequence 353 $ SETERRQ(comm,n,mess) 354 355 Fortran Note: 356 This routine is used differently from Fortran 357 $ PetscError(MPI_Comm comm,PetscErrorCode n,PetscErrorType p,char *message) 358 359 Set the error handler with PetscPushErrorHandler(). 360 361 Developer Note: Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes) 362 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 363 but this annoying. 364 365 .seealso: PetscErrorCode, PetscPushErrorHandler(), PetscPopErrorHandler(), PetscTraceBackErrorHandler(), PetscAbortErrorHandler(), PetscMPIAbortErrorHandler(), 366 PetscReturnErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscEmacsClientErrorHandler(), 367 SETERRQ(), PetscCall(), CHKMEMQ, SETERRQ(), SETERRQ(), PetscErrorMessage(), PETSCABORT() 368 @*/ 369 PetscErrorCode PetscError(MPI_Comm comm,int line,const char *func,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,...) 370 { 371 va_list Argp; 372 size_t fullLength; 373 char buf[2048],*lbuf = NULL; 374 PetscBool ismain; 375 PetscErrorCode ierr; 376 377 if (!PetscErrorHandlingInitialized) return n; 378 if (!func) func = "User provided function"; 379 if (!file) file = "User file"; 380 if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF; 381 382 /* Compose the message evaluating the print format */ 383 if (mess) { 384 va_start(Argp,mess); 385 PetscVSNPrintf(buf,2048,mess,&fullLength,Argp); 386 va_end(Argp); 387 lbuf = buf; 388 if (p == PETSC_ERROR_INITIAL) PetscStrncpy(PetscErrorBaseMessage,lbuf,1023); 389 } 390 391 if (p == PETSC_ERROR_INITIAL && n != PETSC_ERR_MEMC) PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__); 392 393 if (!eh) ierr = PetscTraceBackErrorHandler(comm,line,func,file,n,p,lbuf,NULL); 394 else ierr = (*eh->handler)(comm,line,func,file,n,p,lbuf,eh->ctx); 395 PetscStackClearTop; 396 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. 400 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 + 0*line*1000 + ierr); 407 if (petscwaitonerrorflg) { PetscSleep(1000); } 408 MPI_Abort(MPI_COMM_WORLD,errcode); 409 } 410 411 #if defined(PETSC_CLANGUAGE_CXX) 412 if (p == PETSC_ERROR_IN_CXX) { 413 PetscCxxErrorThrow(); 414 } 415 #endif 416 return ierr; 417 } 418 419 /* -------------------------------------------------------------------------*/ 420 421 /*@C 422 PetscIntView - Prints an array of integers; useful for debugging. 423 424 Collective on PetscViewer 425 426 Input Parameters: 427 + N - number of integers in array 428 . idx - array of integers 429 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 430 431 Level: intermediate 432 433 Developer Notes: 434 idx cannot be const because may be passed to binary viewer where byte swapping is done 435 436 .seealso: PetscRealView() 437 @*/ 438 PetscErrorCode PetscIntView(PetscInt N,const PetscInt idx[],PetscViewer viewer) 439 { 440 PetscMPIInt rank,size; 441 PetscInt j,i,n = N/20,p = N % 20; 442 PetscBool iascii,isbinary; 443 MPI_Comm comm; 444 445 PetscFunctionBegin; 446 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 447 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 448 if (N) PetscValidIntPointer(idx,2); 449 PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 450 PetscCallMPI(MPI_Comm_size(comm,&size)); 451 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 452 453 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 454 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 455 if (iascii) { 456 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 457 for (i=0; i<n; i++) { 458 if (size > 1) { 459 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %" PetscInt_FMT ":", rank, 20*i)); 460 } else { 461 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%" PetscInt_FMT ":",20*i)); 462 } 463 for (j=0; j<20; j++) { 464 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %" PetscInt_FMT,idx[i*20+j])); 465 } 466 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 467 } 468 if (p) { 469 if (size > 1) { 470 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %" PetscInt_FMT ":",rank ,20*n)); 471 } else { 472 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%" PetscInt_FMT ":",20*n)); 473 } 474 for (i=0; i<p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %" PetscInt_FMT,idx[20*n+i])); 475 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 476 } 477 PetscCall(PetscViewerFlush(viewer)); 478 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 479 } else if (isbinary) { 480 PetscMPIInt *sizes,Ntotal,*displs,NN; 481 PetscInt *array; 482 483 PetscCall(PetscMPIIntCast(N,&NN)); 484 485 if (size > 1) { 486 if (rank) { 487 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 488 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_INT,NULL,NULL,NULL,MPIU_INT,0,comm)); 489 } else { 490 PetscCall(PetscMalloc1(size,&sizes)); 491 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 492 Ntotal = sizes[0]; 493 PetscCall(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 PetscCall(PetscMalloc1(Ntotal,&array)); 500 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_INT,array,sizes,displs,MPIU_INT,0,comm)); 501 PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_INT)); 502 PetscCall(PetscFree(sizes)); 503 PetscCall(PetscFree(displs)); 504 PetscCall(PetscFree(array)); 505 } 506 } else { 507 PetscCall(PetscViewerBinaryWrite(viewer,idx,N,PETSC_INT)); 508 } 509 } else { 510 const char *tname; 511 PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 512 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 513 } 514 PetscFunctionReturn(0); 515 } 516 517 /*@C 518 PetscRealView - Prints an array of doubles; useful for debugging. 519 520 Collective on PetscViewer 521 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 526 527 Level: intermediate 528 529 Developer Notes: 530 idx cannot be const because may be passed to binary viewer where byte swapping is done 531 532 .seealso: PetscIntView() 533 @*/ 534 PetscErrorCode PetscRealView(PetscInt N,const PetscReal idx[],PetscViewer viewer) 535 { 536 PetscMPIInt rank,size; 537 PetscInt j,i,n = N/5,p = N % 5; 538 PetscBool iascii,isbinary; 539 MPI_Comm comm; 540 541 PetscFunctionBegin; 542 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 543 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 544 PetscValidRealPointer(idx,2); 545 PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 546 PetscCallMPI(MPI_Comm_size(comm,&size)); 547 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 548 549 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 550 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 551 if (iascii) { 552 PetscInt tab; 553 554 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 555 PetscCall(PetscViewerASCIIGetTab(viewer, &tab)); 556 for (i=0; i<n; i++) { 557 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 558 if (size > 1) { 559 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,5*i)); 560 } else { 561 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",5*i)); 562 } 563 PetscCall(PetscViewerASCIISetTab(viewer, 0)); 564 for (j=0; j<5; j++) { 565 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*5+j])); 566 } 567 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 568 } 569 if (p) { 570 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 571 if (size > 1) { 572 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,5*n)); 573 } else { 574 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",5*n)); 575 } 576 PetscCall(PetscViewerASCIISetTab(viewer, 0)); 577 for (i=0; i<p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[5*n+i])); 578 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 579 } 580 PetscCall(PetscViewerFlush(viewer)); 581 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 582 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 583 } else if (isbinary) { 584 PetscMPIInt *sizes,*displs, Ntotal,NN; 585 PetscReal *array; 586 587 PetscCall(PetscMPIIntCast(N,&NN)); 588 589 if (size > 1) { 590 if (rank) { 591 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 592 PetscCallMPI(MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,NULL,NULL,NULL,MPIU_REAL,0,comm)); 593 } else { 594 PetscCall(PetscMalloc1(size,&sizes)); 595 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 596 Ntotal = sizes[0]; 597 PetscCall(PetscMalloc1(size,&displs)); 598 displs[0] = 0; 599 for (i=1; i<size; i++) { 600 Ntotal += sizes[i]; 601 displs[i] = displs[i-1] + sizes[i-1]; 602 } 603 PetscCall(PetscMalloc1(Ntotal,&array)); 604 PetscCallMPI(MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,array,sizes,displs,MPIU_REAL,0,comm)); 605 PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_REAL)); 606 PetscCall(PetscFree(sizes)); 607 PetscCall(PetscFree(displs)); 608 PetscCall(PetscFree(array)); 609 } 610 } else { 611 PetscCall(PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_REAL)); 612 } 613 } else { 614 const char *tname; 615 PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 616 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 617 } 618 PetscFunctionReturn(0); 619 } 620 621 /*@C 622 PetscScalarView - Prints an array of scalars; useful for debugging. 623 624 Collective on PetscViewer 625 626 Input Parameters: 627 + N - number of scalars in array 628 . idx - array of scalars 629 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 630 631 Level: intermediate 632 633 Developer Notes: 634 idx cannot be const because may be passed to binary viewer where byte swapping is done 635 636 .seealso: PetscIntView(), PetscRealView() 637 @*/ 638 PetscErrorCode PetscScalarView(PetscInt N,const PetscScalar idx[],PetscViewer viewer) 639 { 640 PetscMPIInt rank,size; 641 PetscInt j,i,n = N/3,p = N % 3; 642 PetscBool iascii,isbinary; 643 MPI_Comm comm; 644 645 PetscFunctionBegin; 646 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 647 PetscValidHeader(viewer,3); 648 if (N) PetscValidScalarPointer(idx,2); 649 PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 650 PetscCallMPI(MPI_Comm_size(comm,&size)); 651 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 652 653 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 654 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 655 if (iascii) { 656 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 657 for (i=0; i<n; i++) { 658 if (size > 1) { 659 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,3*i)); 660 } else { 661 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",3*i)); 662 } 663 for (j=0; j<3; j++) { 664 #if defined(PETSC_USE_COMPLEX) 665 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[i*3+j]),(double)PetscImaginaryPart(idx[i*3+j]))); 666 #else 667 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*3+j])); 668 #endif 669 } 670 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 671 } 672 if (p) { 673 if (size > 1) { 674 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,3*n)); 675 } else { 676 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",3*n)); 677 } 678 for (i=0; i<p; i++) { 679 #if defined(PETSC_USE_COMPLEX) 680 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[n*3+i]),(double)PetscImaginaryPart(idx[n*3+i]))); 681 #else 682 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[3*n+i])); 683 #endif 684 } 685 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 686 } 687 PetscCall(PetscViewerFlush(viewer)); 688 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 689 } else if (isbinary) { 690 PetscMPIInt *sizes,Ntotal,*displs,NN; 691 PetscScalar *array; 692 693 PetscCall(PetscMPIIntCast(N,&NN)); 694 695 if (size > 1) { 696 if (rank) { 697 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 698 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,NULL,NULL,NULL,MPIU_SCALAR,0,comm)); 699 } else { 700 PetscCall(PetscMalloc1(size,&sizes)); 701 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 702 Ntotal = sizes[0]; 703 PetscCall(PetscMalloc1(size,&displs)); 704 displs[0] = 0; 705 for (i=1; i<size; i++) { 706 Ntotal += sizes[i]; 707 displs[i] = displs[i-1] + sizes[i-1]; 708 } 709 PetscCall(PetscMalloc1(Ntotal,&array)); 710 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,array,sizes,displs,MPIU_SCALAR,0,comm)); 711 PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_SCALAR)); 712 PetscCall(PetscFree(sizes)); 713 PetscCall(PetscFree(displs)); 714 PetscCall(PetscFree(array)); 715 } 716 } else { 717 PetscCall(PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_SCALAR)); 718 } 719 } else { 720 const char *tname; 721 PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 722 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 723 } 724 PetscFunctionReturn(0); 725 } 726 727 #if defined(PETSC_HAVE_CUDA) 728 #include <petscdevice.h> 729 PETSC_EXTERN const char* PetscCUBLASGetErrorName(cublasStatus_t status) 730 { 731 switch(status) { 732 #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 733 case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS"; 734 case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED"; 735 case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED"; 736 case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE"; 737 case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH"; 738 case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR"; 739 case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED"; 740 case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR"; 741 case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED"; 742 case CUBLAS_STATUS_LICENSE_ERROR: return "CUBLAS_STATUS_LICENSE_ERROR"; 743 #endif 744 default: return "unknown error"; 745 } 746 } 747 PETSC_EXTERN const char* PetscCUSolverGetErrorName(cusolverStatus_t status) 748 { 749 switch(status) { 750 #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 751 case CUSOLVER_STATUS_SUCCESS: return "CUSOLVER_STATUS_SUCCESS"; 752 case CUSOLVER_STATUS_NOT_INITIALIZED: return "CUSOLVER_STATUS_NOT_INITIALIZED"; 753 case CUSOLVER_STATUS_INVALID_VALUE: return "CUSOLVER_STATUS_INVALID_VALUE"; 754 case CUSOLVER_STATUS_ARCH_MISMATCH: return "CUSOLVER_STATUS_ARCH_MISMATCH"; 755 case CUSOLVER_STATUS_INTERNAL_ERROR: return "CUSOLVER_STATUS_INTERNAL_ERROR"; 756 #if (CUDART_VERSION >= 9000) /* CUDA 9.0 had these defined on June 2021 */ 757 case CUSOLVER_STATUS_ALLOC_FAILED: return "CUSOLVER_STATUS_ALLOC_FAILED"; 758 case CUSOLVER_STATUS_MAPPING_ERROR: return "CUSOLVER_STATUS_MAPPING_ERROR"; 759 case CUSOLVER_STATUS_EXECUTION_FAILED: return "CUSOLVER_STATUS_EXECUTION_FAILED"; 760 case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED: return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED"; 761 case CUSOLVER_STATUS_NOT_SUPPORTED : return "CUSOLVER_STATUS_NOT_SUPPORTED "; 762 case CUSOLVER_STATUS_ZERO_PIVOT: return "CUSOLVER_STATUS_ZERO_PIVOT"; 763 case CUSOLVER_STATUS_INVALID_LICENSE: return "CUSOLVER_STATUS_INVALID_LICENSE"; 764 #endif 765 #endif 766 default: return "unknown error"; 767 } 768 } 769 PETSC_EXTERN const char* PetscCUFFTGetErrorName(cufftResult result) 770 { 771 switch (result) { 772 case CUFFT_SUCCESS: return "CUFFT_SUCCESS"; 773 case CUFFT_INVALID_PLAN: return "CUFFT_INVALID_PLAN"; 774 case CUFFT_ALLOC_FAILED: return "CUFFT_ALLOC_FAILED"; 775 case CUFFT_INVALID_TYPE: return "CUFFT_INVALID_TYPE"; 776 case CUFFT_INVALID_VALUE: return "CUFFT_INVALID_VALUE"; 777 case CUFFT_INTERNAL_ERROR: return "CUFFT_INTERNAL_ERROR"; 778 case CUFFT_EXEC_FAILED: return "CUFFT_EXEC_FAILED"; 779 case CUFFT_SETUP_FAILED: return "CUFFT_SETUP_FAILED"; 780 case CUFFT_INVALID_SIZE: return "CUFFT_INVALID_SIZE"; 781 case CUFFT_UNALIGNED_DATA: return "CUFFT_UNALIGNED_DATA"; 782 case CUFFT_INCOMPLETE_PARAMETER_LIST: return "CUFFT_INCOMPLETE_PARAMETER_LIST"; 783 case CUFFT_INVALID_DEVICE: return "CUFFT_INVALID_DEVICE"; 784 case CUFFT_PARSE_ERROR: return "CUFFT_PARSE_ERROR"; 785 case CUFFT_NO_WORKSPACE: return "CUFFT_NO_WORKSPACE"; 786 case CUFFT_NOT_IMPLEMENTED: return "CUFFT_NOT_IMPLEMENTED"; 787 case CUFFT_LICENSE_ERROR: return "CUFFT_LICENSE_ERROR"; 788 case CUFFT_NOT_SUPPORTED: return "CUFFT_NOT_SUPPORTED"; 789 default: return "unknown error"; 790 } 791 } 792 #endif 793 794 #if defined(PETSC_HAVE_HIP) 795 #include <petscdevice.h> 796 PETSC_EXTERN const char* PetscHIPBLASGetErrorName(hipblasStatus_t status) 797 { 798 switch(status) { 799 case HIPBLAS_STATUS_SUCCESS: return "HIPBLAS_STATUS_SUCCESS"; 800 case HIPBLAS_STATUS_NOT_INITIALIZED: return "HIPBLAS_STATUS_NOT_INITIALIZED"; 801 case HIPBLAS_STATUS_ALLOC_FAILED: return "HIPBLAS_STATUS_ALLOC_FAILED"; 802 case HIPBLAS_STATUS_INVALID_VALUE: return "HIPBLAS_STATUS_INVALID_VALUE"; 803 case HIPBLAS_STATUS_ARCH_MISMATCH: return "HIPBLAS_STATUS_ARCH_MISMATCH"; 804 case HIPBLAS_STATUS_MAPPING_ERROR: return "HIPBLAS_STATUS_MAPPING_ERROR"; 805 case HIPBLAS_STATUS_EXECUTION_FAILED: return "HIPBLAS_STATUS_EXECUTION_FAILED"; 806 case HIPBLAS_STATUS_INTERNAL_ERROR: return "HIPBLAS_STATUS_INTERNAL_ERROR"; 807 case HIPBLAS_STATUS_NOT_SUPPORTED: return "HIPBLAS_STATUS_NOT_SUPPORTED"; 808 default: return "unknown error"; 809 } 810 } 811 #endif 812