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 size_t len; 287 288 PetscFunctionBegin; 289 if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) { 290 *text = PetscErrorStrings[errnum-PETSC_ERR_MIN_VALUE-1]; 291 PetscCall(PetscStrlen(*text,&len)); 292 if (!len) *text = NULL; 293 } 294 else if (text) *text = NULL; 295 296 if (specific) *specific = PetscErrorBaseMessage; 297 PetscFunctionReturn(0); 298 } 299 300 #if defined(PETSC_CLANGUAGE_CXX) 301 /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software 302 * would be broken if implementations did not handle it it some common cases. However, keep in mind 303 * 304 * Rule 62. Don't allow exceptions to propagate across module boundaries 305 * 306 * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface 307 * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed. 308 * 309 * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message 310 * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that 311 * seems crazy to me. 312 */ 313 #include <sstream> 314 #include <stdexcept> 315 static void PetscCxxErrorThrow() 316 { 317 const char *str; 318 if (eh && eh->ctx) { 319 std::ostringstream *msg; 320 msg = (std::ostringstream*) eh->ctx; 321 str = msg->str().c_str(); 322 } else str = "Error detected in C PETSc"; 323 324 throw std::runtime_error(str); 325 } 326 #endif 327 328 /*@C 329 PetscError - Routine that is called when an error has been detected, usually called through the macro SETERRQ(PETSC_COMM_SELF,). 330 331 Collective on comm 332 333 Input Parameters: 334 + comm - communicator over which error occurred. ALL ranks of this communicator MUST call this routine 335 . line - the line number of the error (indicated by __LINE__) 336 . func - the function name in which the error was detected 337 . file - the file in which the error was detected (indicated by __FILE__) 338 . n - the generic error number 339 . p - PETSC_ERROR_INITIAL indicates the error was initially detected, PETSC_ERROR_REPEAT indicates this is a traceback from a previously detected error 340 - mess - formatted message string - aka printf 341 342 Options Database: 343 + -error_output_stdout - output the error messages to stdout instead of the default stderr 344 - -error_output_none - do not output the error messages 345 346 Level: intermediate 347 348 Notes: 349 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 350 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, 351 KSPGetConvergedReason() that indicates if the solve was successful or not. The option -ksp_error_if_not_converged, for example, turns numerical failures into 352 hard errors managed via PetscError(). 353 354 PETSc provides a rich supply of error handlers, see the list below, and users can also provide their own error handlers. 355 356 Most users need not directly use this routine and the error handlers, but 357 can instead use the simplified interface SETERRQ, which has the calling 358 sequence 359 $ SETERRQ(comm,n,mess) 360 361 Fortran Note: 362 This routine is used differently from Fortran 363 $ PetscError(MPI_Comm comm,PetscErrorCode n,PetscErrorType p,char *message) 364 365 Set the error handler with PetscPushErrorHandler(). 366 367 Developer Note: Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes) 368 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 369 but this annoying. 370 371 .seealso: `PetscErrorCode`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, 372 `PetscReturnErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()`, 373 `SETERRQ()`, `PetscCall()`, `CHKMEMQ`, `SETERRQ()`, `SETERRQ()`, `PetscErrorMessage()`, `PETSCABORT()` 374 @*/ 375 PetscErrorCode PetscError(MPI_Comm comm,int line,const char *func,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,...) 376 { 377 va_list Argp; 378 size_t fullLength; 379 char buf[2048],*lbuf = NULL; 380 PetscBool ismain; 381 PetscErrorCode ierr; 382 383 if (!PetscErrorHandlingInitialized) return n; 384 if (!func) func = "User provided function"; 385 if (!file) file = "User file"; 386 if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF; 387 388 /* Compose the message evaluating the print format */ 389 if (mess) { 390 va_start(Argp,mess); 391 PetscVSNPrintf(buf,2048,mess,&fullLength,Argp); 392 va_end(Argp); 393 lbuf = buf; 394 if (p == PETSC_ERROR_INITIAL) PetscStrncpy(PetscErrorBaseMessage,lbuf,1023); 395 } 396 397 if (p == PETSC_ERROR_INITIAL && n != PETSC_ERR_MEMC) PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__); 398 399 if (!eh) ierr = PetscTraceBackErrorHandler(comm,line,func,file,n,p,lbuf,NULL); 400 else ierr = (*eh->handler)(comm,line,func,file,n,p,lbuf,eh->ctx); 401 PetscStackClearTop; 402 403 /* 404 If this is called from the main() routine we call MPI_Abort() instead of 405 return to allow the parallel program to be properly shutdown. 406 407 Does not call PETSCABORT() since that would provide the wrong source file and line number information 408 */ 409 PetscStrncmp(func,"main",4,&ismain); 410 if (ismain) { 411 PetscMPIInt errcode; 412 errcode = (PetscMPIInt)(0 + 0*line*1000 + ierr); 413 if (petscwaitonerrorflg) { PetscSleep(1000); } 414 MPI_Abort(MPI_COMM_WORLD,errcode); 415 } 416 417 #if defined(PETSC_CLANGUAGE_CXX) 418 if (p == PETSC_ERROR_IN_CXX) { 419 PetscCxxErrorThrow(); 420 } 421 #endif 422 return ierr; 423 } 424 425 /* -------------------------------------------------------------------------*/ 426 427 /*@C 428 PetscIntView - Prints an array of integers; useful for debugging. 429 430 Collective on PetscViewer 431 432 Input Parameters: 433 + N - number of integers in array 434 . idx - array of integers 435 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 436 437 Level: intermediate 438 439 Developer Notes: 440 idx cannot be const because may be passed to binary viewer where byte swapping is done 441 442 .seealso: `PetscRealView()` 443 @*/ 444 PetscErrorCode PetscIntView(PetscInt N,const PetscInt idx[],PetscViewer viewer) 445 { 446 PetscMPIInt rank,size; 447 PetscInt j,i,n = N/20,p = N % 20; 448 PetscBool iascii,isbinary; 449 MPI_Comm comm; 450 451 PetscFunctionBegin; 452 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 453 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 454 if (N) PetscValidIntPointer(idx,2); 455 PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 456 PetscCallMPI(MPI_Comm_size(comm,&size)); 457 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 458 459 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 460 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 461 if (iascii) { 462 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 463 for (i=0; i<n; i++) { 464 if (size > 1) { 465 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %" PetscInt_FMT ":", rank, 20*i)); 466 } else { 467 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%" PetscInt_FMT ":",20*i)); 468 } 469 for (j=0; j<20; j++) { 470 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %" PetscInt_FMT,idx[i*20+j])); 471 } 472 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 473 } 474 if (p) { 475 if (size > 1) { 476 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %" PetscInt_FMT ":",rank ,20*n)); 477 } else { 478 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%" PetscInt_FMT ":",20*n)); 479 } 480 for (i=0; i<p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %" PetscInt_FMT,idx[20*n+i])); 481 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 482 } 483 PetscCall(PetscViewerFlush(viewer)); 484 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 485 } else if (isbinary) { 486 PetscMPIInt *sizes,Ntotal,*displs,NN; 487 PetscInt *array; 488 489 PetscCall(PetscMPIIntCast(N,&NN)); 490 491 if (size > 1) { 492 if (rank) { 493 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 494 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_INT,NULL,NULL,NULL,MPIU_INT,0,comm)); 495 } else { 496 PetscCall(PetscMalloc1(size,&sizes)); 497 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 498 Ntotal = sizes[0]; 499 PetscCall(PetscMalloc1(size,&displs)); 500 displs[0] = 0; 501 for (i=1; i<size; i++) { 502 Ntotal += sizes[i]; 503 displs[i] = displs[i-1] + sizes[i-1]; 504 } 505 PetscCall(PetscMalloc1(Ntotal,&array)); 506 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_INT,array,sizes,displs,MPIU_INT,0,comm)); 507 PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_INT)); 508 PetscCall(PetscFree(sizes)); 509 PetscCall(PetscFree(displs)); 510 PetscCall(PetscFree(array)); 511 } 512 } else { 513 PetscCall(PetscViewerBinaryWrite(viewer,idx,N,PETSC_INT)); 514 } 515 } else { 516 const char *tname; 517 PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 518 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 519 } 520 PetscFunctionReturn(0); 521 } 522 523 /*@C 524 PetscRealView - Prints an array of doubles; useful for debugging. 525 526 Collective on PetscViewer 527 528 Input Parameters: 529 + N - number of PetscReal in array 530 . idx - array of PetscReal 531 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 532 533 Level: intermediate 534 535 Developer Notes: 536 idx cannot be const because may be passed to binary viewer where byte swapping is done 537 538 .seealso: `PetscIntView()` 539 @*/ 540 PetscErrorCode PetscRealView(PetscInt N,const PetscReal idx[],PetscViewer viewer) 541 { 542 PetscMPIInt rank,size; 543 PetscInt j,i,n = N/5,p = N % 5; 544 PetscBool iascii,isbinary; 545 MPI_Comm comm; 546 547 PetscFunctionBegin; 548 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 549 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 550 PetscValidRealPointer(idx,2); 551 PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 552 PetscCallMPI(MPI_Comm_size(comm,&size)); 553 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 554 555 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 556 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 557 if (iascii) { 558 PetscInt tab; 559 560 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 561 PetscCall(PetscViewerASCIIGetTab(viewer, &tab)); 562 for (i=0; i<n; i++) { 563 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 564 if (size > 1) { 565 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,5*i)); 566 } else { 567 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",5*i)); 568 } 569 PetscCall(PetscViewerASCIISetTab(viewer, 0)); 570 for (j=0; j<5; j++) { 571 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*5+j])); 572 } 573 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 574 } 575 if (p) { 576 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 577 if (size > 1) { 578 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,5*n)); 579 } else { 580 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",5*n)); 581 } 582 PetscCall(PetscViewerASCIISetTab(viewer, 0)); 583 for (i=0; i<p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[5*n+i])); 584 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 585 } 586 PetscCall(PetscViewerFlush(viewer)); 587 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 588 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 589 } else if (isbinary) { 590 PetscMPIInt *sizes,*displs, Ntotal,NN; 591 PetscReal *array; 592 593 PetscCall(PetscMPIIntCast(N,&NN)); 594 595 if (size > 1) { 596 if (rank) { 597 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 598 PetscCallMPI(MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,NULL,NULL,NULL,MPIU_REAL,0,comm)); 599 } else { 600 PetscCall(PetscMalloc1(size,&sizes)); 601 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 602 Ntotal = sizes[0]; 603 PetscCall(PetscMalloc1(size,&displs)); 604 displs[0] = 0; 605 for (i=1; i<size; i++) { 606 Ntotal += sizes[i]; 607 displs[i] = displs[i-1] + sizes[i-1]; 608 } 609 PetscCall(PetscMalloc1(Ntotal,&array)); 610 PetscCallMPI(MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,array,sizes,displs,MPIU_REAL,0,comm)); 611 PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_REAL)); 612 PetscCall(PetscFree(sizes)); 613 PetscCall(PetscFree(displs)); 614 PetscCall(PetscFree(array)); 615 } 616 } else { 617 PetscCall(PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_REAL)); 618 } 619 } else { 620 const char *tname; 621 PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 622 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 623 } 624 PetscFunctionReturn(0); 625 } 626 627 /*@C 628 PetscScalarView - Prints an array of scalars; useful for debugging. 629 630 Collective on PetscViewer 631 632 Input Parameters: 633 + N - number of scalars in array 634 . idx - array of scalars 635 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 636 637 Level: intermediate 638 639 Developer Notes: 640 idx cannot be const because may be passed to binary viewer where byte swapping is done 641 642 .seealso: `PetscIntView()`, `PetscRealView()` 643 @*/ 644 PetscErrorCode PetscScalarView(PetscInt N,const PetscScalar idx[],PetscViewer viewer) 645 { 646 PetscMPIInt rank,size; 647 PetscInt j,i,n = N/3,p = N % 3; 648 PetscBool iascii,isbinary; 649 MPI_Comm comm; 650 651 PetscFunctionBegin; 652 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 653 PetscValidHeader(viewer,3); 654 if (N) PetscValidScalarPointer(idx,2); 655 PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm)); 656 PetscCallMPI(MPI_Comm_size(comm,&size)); 657 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 658 659 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 660 PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 661 if (iascii) { 662 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 663 for (i=0; i<n; i++) { 664 if (size > 1) { 665 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,3*i)); 666 } else { 667 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",3*i)); 668 } 669 for (j=0; j<3; j++) { 670 #if defined(PETSC_USE_COMPLEX) 671 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[i*3+j]),(double)PetscImaginaryPart(idx[i*3+j]))); 672 #else 673 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*3+j])); 674 #endif 675 } 676 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 677 } 678 if (p) { 679 if (size > 1) { 680 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2" PetscInt_FMT ":",rank ,3*n)); 681 } else { 682 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"%2" PetscInt_FMT ":",3*n)); 683 } 684 for (i=0; i<p; i++) { 685 #if defined(PETSC_USE_COMPLEX) 686 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[n*3+i]),(double)PetscImaginaryPart(idx[n*3+i]))); 687 #else 688 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[3*n+i])); 689 #endif 690 } 691 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer,"\n")); 692 } 693 PetscCall(PetscViewerFlush(viewer)); 694 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 695 } else if (isbinary) { 696 PetscMPIInt *sizes,Ntotal,*displs,NN; 697 PetscScalar *array; 698 699 PetscCall(PetscMPIIntCast(N,&NN)); 700 701 if (size > 1) { 702 if (rank) { 703 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,NULL,0,MPI_INT,0,comm)); 704 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,NULL,NULL,NULL,MPIU_SCALAR,0,comm)); 705 } else { 706 PetscCall(PetscMalloc1(size,&sizes)); 707 PetscCallMPI(MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm)); 708 Ntotal = sizes[0]; 709 PetscCall(PetscMalloc1(size,&displs)); 710 displs[0] = 0; 711 for (i=1; i<size; i++) { 712 Ntotal += sizes[i]; 713 displs[i] = displs[i-1] + sizes[i-1]; 714 } 715 PetscCall(PetscMalloc1(Ntotal,&array)); 716 PetscCallMPI(MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,array,sizes,displs,MPIU_SCALAR,0,comm)); 717 PetscCall(PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_SCALAR)); 718 PetscCall(PetscFree(sizes)); 719 PetscCall(PetscFree(displs)); 720 PetscCall(PetscFree(array)); 721 } 722 } else { 723 PetscCall(PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_SCALAR)); 724 } 725 } else { 726 const char *tname; 727 PetscCall(PetscObjectGetName((PetscObject)viewer,&tname)); 728 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 729 } 730 PetscFunctionReturn(0); 731 } 732 733 #if defined(PETSC_HAVE_CUDA) 734 #include <petscdevice.h> 735 PETSC_EXTERN const char* PetscCUBLASGetErrorName(cublasStatus_t status) 736 { 737 switch(status) { 738 #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 739 case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS"; 740 case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED"; 741 case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED"; 742 case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE"; 743 case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH"; 744 case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR"; 745 case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED"; 746 case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR"; 747 case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED"; 748 case CUBLAS_STATUS_LICENSE_ERROR: return "CUBLAS_STATUS_LICENSE_ERROR"; 749 #endif 750 default: return "unknown error"; 751 } 752 } 753 PETSC_EXTERN const char* PetscCUSolverGetErrorName(cusolverStatus_t status) 754 { 755 switch(status) { 756 #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 757 case CUSOLVER_STATUS_SUCCESS: return "CUSOLVER_STATUS_SUCCESS"; 758 case CUSOLVER_STATUS_NOT_INITIALIZED: return "CUSOLVER_STATUS_NOT_INITIALIZED"; 759 case CUSOLVER_STATUS_INVALID_VALUE: return "CUSOLVER_STATUS_INVALID_VALUE"; 760 case CUSOLVER_STATUS_ARCH_MISMATCH: return "CUSOLVER_STATUS_ARCH_MISMATCH"; 761 case CUSOLVER_STATUS_INTERNAL_ERROR: return "CUSOLVER_STATUS_INTERNAL_ERROR"; 762 #if (CUDART_VERSION >= 9000) /* CUDA 9.0 had these defined on June 2021 */ 763 case CUSOLVER_STATUS_ALLOC_FAILED: return "CUSOLVER_STATUS_ALLOC_FAILED"; 764 case CUSOLVER_STATUS_MAPPING_ERROR: return "CUSOLVER_STATUS_MAPPING_ERROR"; 765 case CUSOLVER_STATUS_EXECUTION_FAILED: return "CUSOLVER_STATUS_EXECUTION_FAILED"; 766 case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED: return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED"; 767 case CUSOLVER_STATUS_NOT_SUPPORTED : return "CUSOLVER_STATUS_NOT_SUPPORTED "; 768 case CUSOLVER_STATUS_ZERO_PIVOT: return "CUSOLVER_STATUS_ZERO_PIVOT"; 769 case CUSOLVER_STATUS_INVALID_LICENSE: return "CUSOLVER_STATUS_INVALID_LICENSE"; 770 #endif 771 #endif 772 default: return "unknown error"; 773 } 774 } 775 PETSC_EXTERN const char* PetscCUFFTGetErrorName(cufftResult result) 776 { 777 switch (result) { 778 case CUFFT_SUCCESS: return "CUFFT_SUCCESS"; 779 case CUFFT_INVALID_PLAN: return "CUFFT_INVALID_PLAN"; 780 case CUFFT_ALLOC_FAILED: return "CUFFT_ALLOC_FAILED"; 781 case CUFFT_INVALID_TYPE: return "CUFFT_INVALID_TYPE"; 782 case CUFFT_INVALID_VALUE: return "CUFFT_INVALID_VALUE"; 783 case CUFFT_INTERNAL_ERROR: return "CUFFT_INTERNAL_ERROR"; 784 case CUFFT_EXEC_FAILED: return "CUFFT_EXEC_FAILED"; 785 case CUFFT_SETUP_FAILED: return "CUFFT_SETUP_FAILED"; 786 case CUFFT_INVALID_SIZE: return "CUFFT_INVALID_SIZE"; 787 case CUFFT_UNALIGNED_DATA: return "CUFFT_UNALIGNED_DATA"; 788 case CUFFT_INCOMPLETE_PARAMETER_LIST: return "CUFFT_INCOMPLETE_PARAMETER_LIST"; 789 case CUFFT_INVALID_DEVICE: return "CUFFT_INVALID_DEVICE"; 790 case CUFFT_PARSE_ERROR: return "CUFFT_PARSE_ERROR"; 791 case CUFFT_NO_WORKSPACE: return "CUFFT_NO_WORKSPACE"; 792 case CUFFT_NOT_IMPLEMENTED: return "CUFFT_NOT_IMPLEMENTED"; 793 case CUFFT_LICENSE_ERROR: return "CUFFT_LICENSE_ERROR"; 794 case CUFFT_NOT_SUPPORTED: return "CUFFT_NOT_SUPPORTED"; 795 default: return "unknown error"; 796 } 797 } 798 #endif 799 800 #if defined(PETSC_HAVE_HIP) 801 #include <petscdevice.h> 802 PETSC_EXTERN const char* PetscHIPBLASGetErrorName(hipblasStatus_t status) 803 { 804 switch(status) { 805 case HIPBLAS_STATUS_SUCCESS: return "HIPBLAS_STATUS_SUCCESS"; 806 case HIPBLAS_STATUS_NOT_INITIALIZED: return "HIPBLAS_STATUS_NOT_INITIALIZED"; 807 case HIPBLAS_STATUS_ALLOC_FAILED: return "HIPBLAS_STATUS_ALLOC_FAILED"; 808 case HIPBLAS_STATUS_INVALID_VALUE: return "HIPBLAS_STATUS_INVALID_VALUE"; 809 case HIPBLAS_STATUS_ARCH_MISMATCH: return "HIPBLAS_STATUS_ARCH_MISMATCH"; 810 case HIPBLAS_STATUS_MAPPING_ERROR: return "HIPBLAS_STATUS_MAPPING_ERROR"; 811 case HIPBLAS_STATUS_EXECUTION_FAILED: return "HIPBLAS_STATUS_EXECUTION_FAILED"; 812 case HIPBLAS_STATUS_INTERNAL_ERROR: return "HIPBLAS_STATUS_INTERNAL_ERROR"; 813 case HIPBLAS_STATUS_NOT_SUPPORTED: return "HIPBLAS_STATUS_NOT_SUPPORTED"; 814 default: return "unknown error"; 815 } 816 } 817 #endif 818