1 2 /* 3 Code that allows one to set the error handlers 4 Portions of this code are under: 5 Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved. 6 */ 7 #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 8 #include <petscviewer.h> 9 10 typedef struct _EH *EH; 11 struct _EH { 12 PetscErrorCode (*handler)(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *); 13 void *ctx; 14 EH previous; 15 }; 16 17 static EH eh = NULL; 18 19 /*@C 20 PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to 21 load the file where the error occurred. Then calls the "previous" error handler. 22 23 Not Collective 24 25 Input Parameters: 26 + comm - communicator over which error occurred 27 . line - the line number of the error (indicated by __LINE__) 28 . file - the file in which the error was detected (indicated by __FILE__) 29 . fun - the function name 30 . mess - an error text string, usually just printed to the screen 31 . n - the generic error number 32 . p - specific error number 33 - ctx - error handler context 34 35 Options Database Key: 36 . -on_error_emacs <machinename> - will contact machinename to open the Emacs client there 37 38 Level: developer 39 40 Note: 41 You must put (server-start) in your .emacs file for the emacsclient software to work 42 43 Developer Notes: 44 Since this is an error handler it cannot call `PetscCall()`; thus we just return if an error is detected. 45 But some of the functions it calls do perform error checking that may not be appropriate in a error handler call. 46 47 .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, 48 `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscReturnErrorHandler()` 49 @*/ 50 PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx) 51 { 52 PetscErrorCode ierr; 53 char command[PETSC_MAX_PATH_LEN]; 54 const char *pdir; 55 FILE *fp; 56 57 ierr = PetscGetPetscDir(&pdir); 58 if (ierr) return ierr; 59 ierr = PetscSNPrintf(command, PETSC_STATIC_ARRAY_LENGTH(command), "cd %s; emacsclient --no-wait +%d %s\n", pdir, line, file); 60 if (ierr) return ierr; 61 #if defined(PETSC_HAVE_POPEN) 62 ierr = PetscPOpen(MPI_COMM_WORLD, (char *)ctx, command, "r", &fp); 63 if (ierr) return ierr; 64 ierr = PetscPClose(MPI_COMM_WORLD, fp); 65 if (ierr) return ierr; 66 #else 67 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine"); 68 #endif 69 ierr = PetscPopErrorHandler(); 70 if (ierr) return ierr; /* remove this handler from the stack of handlers */ 71 if (!eh) { 72 ierr = PetscTraceBackErrorHandler(comm, line, fun, file, n, p, mess, NULL); 73 if (ierr) return ierr; 74 } else { 75 ierr = (*eh->handler)(comm, line, fun, file, n, p, mess, eh->ctx); 76 if (ierr) return ierr; 77 } 78 return PETSC_SUCCESS; 79 } 80 81 /*@C 82 PetscPushErrorHandler - Sets a routine to be called on detection of errors. 83 84 Not Collective 85 86 Input Parameters: 87 + handler - error handler routine 88 - ctx - optional handler context that contains information needed by the handler (for 89 example file pointers for error messages etc.) 90 91 Calling sequence of `handler`: 92 + comm - communicator over which error occurred 93 . line - the line number of the error (indicated by __LINE__) 94 . file - the file in which the error was detected (indicated by __FILE__) 95 . fun - the function name 96 . n - the generic error number (see list defined in include/petscerror.h) 97 . p - `PETSC_ERROR_INITIAL` if error just detected, otherwise `PETSC_ERROR_REPEAT` 98 . mess - an error text string, usually just printed to the screen 99 - ctx - the error handler context 100 101 Options Database Keys: 102 + -on_error_attach_debugger <noxterm,gdb or dbx> - starts up the debugger if an error occurs 103 - -on_error_abort - aborts the program if an error occurs 104 105 Level: intermediate 106 107 Note: 108 The currently available PETSc error handlers include `PetscTraceBackErrorHandler()`, 109 `PetscAttachDebuggerErrorHandler()`, `PetscAbortErrorHandler()`, and `PetscMPIAbortErrorHandler()`, `PetscReturnErrorHandler()`. 110 111 Fortran Notes: 112 You can only push one error handler from Fortran before popping it. 113 114 .seealso: `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscPushSignalHandler()` 115 @*/ 116 PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx), void *ctx) 117 { 118 EH neweh; 119 120 PetscFunctionBegin; 121 PetscCall(PetscNew(&neweh)); 122 if (eh) neweh->previous = eh; 123 else neweh->previous = NULL; 124 neweh->handler = handler; 125 neweh->ctx = ctx; 126 eh = neweh; 127 PetscFunctionReturn(PETSC_SUCCESS); 128 } 129 130 /*@ 131 PetscPopErrorHandler - Removes the latest error handler that was 132 pushed with `PetscPushErrorHandler()`. 133 134 Not Collective 135 136 Level: intermediate 137 138 .seealso: `PetscPushErrorHandler()` 139 @*/ 140 PetscErrorCode PetscPopErrorHandler(void) 141 { 142 EH tmp; 143 144 PetscFunctionBegin; 145 if (!eh) PetscFunctionReturn(PETSC_SUCCESS); 146 tmp = eh; 147 eh = eh->previous; 148 PetscCall(PetscFree(tmp)); 149 PetscFunctionReturn(PETSC_SUCCESS); 150 } 151 152 /*@C 153 PetscReturnErrorHandler - Error handler that causes a return without printing an error message. 154 155 Not Collective 156 157 Input Parameters: 158 + comm - communicator over which error occurred 159 . line - the line number of the error (indicated by __LINE__) 160 . fun - the function name 161 . file - the file in which the error was detected (indicated by __FILE__) 162 . mess - an error text string, usually just printed to the screen 163 . n - the generic error number 164 . p - specific error number 165 - ctx - error handler context 166 167 Level: developer 168 169 Notes: 170 Most users need not directly employ this routine and the other error 171 handlers, but can instead use the simplified interface `SETERRQ()`, which has 172 the calling sequence 173 $ SETERRQ(comm, number, mess) 174 175 `PetscIgnoreErrorHandler()` does the same thing as this function, but is deprecated, you should use this function. 176 177 Use `PetscPushErrorHandler()` to set the desired error handler. 178 179 .seealso: `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscError()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, 180 `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()` 181 @*/ 182 PetscErrorCode PetscReturnErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx) 183 { 184 (void)comm; 185 (void)line; 186 (void)fun; 187 (void)file; 188 (void)p; 189 (void)mess; 190 (void)ctx; 191 return n; 192 } 193 194 static char PetscErrorBaseMessage[1024]; 195 /* 196 The numerical values for these are defined in include/petscerror.h; any changes 197 there must also be made here 198 */ 199 static const char *PetscErrorStrings[] = { 200 /*55 */ "Out of memory", 201 "No support for this operation for this object type", 202 "No support for this operation on this system", 203 /*58 */ "Operation done in wrong order", 204 /*59 */ "Signal received", 205 /*60 */ "Nonconforming object sizes", 206 "Argument aliasing not permitted", 207 "Invalid argument", 208 /*63 */ "Argument out of range", 209 "Corrupt argument: https://petsc.org/release/faq/#valgrind", 210 "Unable to open file", 211 "Read from file failed", 212 "Write to file failed", 213 "Invalid pointer", 214 /*69 */ "Arguments must have same type", 215 /*70 */ "Attempt to use a pointer that does not point to a valid accessible location", 216 /*71 */ "Zero pivot in LU factorization: https://petsc.org/release/faq/#zeropivot", 217 /*72 */ "Floating point exception", 218 /*73 */ "Object is in wrong state", 219 "Corrupted Petsc object", 220 "Arguments are incompatible", 221 "Error in external library", 222 /*77 */ "Petsc has generated inconsistent data", 223 "Memory corruption: https://petsc.org/release/faq/#valgrind", 224 "Unexpected data in file", 225 /*80 */ "Arguments must have same communicators", 226 /*81 */ "Zero pivot in Cholesky factorization: https://petsc.org/release/faq/#zeropivot", 227 "", 228 "", 229 "Overflow in integer operation: https://petsc.org/release/faq/#64-bit-indices", 230 /*85 */ "Null argument, when expecting valid pointer", 231 /*86 */ "Unknown type. Check for miss-spelling or missing package: https://petsc.org/release/install/install/#external-packages", 232 /*87 */ "MPI library at runtime is not compatible with MPI used at compile time", 233 /*88 */ "Error in system call", 234 /*89 */ "Object Type not set: https://petsc.org/release/faq/#object-type-not-set", 235 /*90 */ "", 236 /* */ "", 237 /*92 */ "See https://petsc.org/release/overview/linear_solve_table/ for possible LU and Cholesky solvers", 238 /*93 */ "You cannot overwrite this option since that will conflict with other previously set options", 239 /*94 */ "Example/application run with number of MPI ranks it does not support", 240 /*95 */ "Missing or incorrect user input", 241 /*96 */ "GPU resources unavailable", 242 /*97 */ "GPU error", 243 /*98 */ "General MPI error", 244 /*99 */ "PetscError() incorrectly returned an error code of 0"}; 245 246 /*@C 247 PetscErrorMessage - Returns the text string associated with a PETSc error code. 248 249 Not Collective 250 251 Input Parameter: 252 . errnum - the error code 253 254 Output Parameters: 255 + text - the error message (`NULL` if not desired) 256 - specific - the specific error message that was set with `SETERRQ()` or 257 `PetscError()`. (`NULL` if not desired) 258 259 Level: developer 260 261 .seealso: `PetscErrorCode`, `PetscPushErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, 262 `PetscError()`, `SETERRQ()`, `PetscCall()` `PetscAbortErrorHandler()`, 263 `PetscTraceBackErrorHandler()` 264 @*/ 265 PetscErrorCode PetscErrorMessage(PetscErrorCode errnum, const char *text[], char **specific) 266 { 267 PetscFunctionBegin; 268 if (text) { 269 if (errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) { 270 size_t len; 271 272 *text = PetscErrorStrings[errnum - PETSC_ERR_MIN_VALUE - 1]; 273 PetscCall(PetscStrlen(*text, &len)); 274 if (!len) *text = NULL; 275 } else if (errnum == PETSC_ERR_BOOLEAN_MACRO_FAILURE) { 276 /* this "error code" arises from failures in boolean macros, where the || operator is 277 used to short-circuit the macro call in case of error. This has the side effect of 278 "returning" either 0 (PETSC_SUCCESS) or 1 (PETSC_ERR_UNKNONWN): 279 280 #define PETSC_FOO(x) ((PetscErrorCode)(PetscBar(x) || PetscBaz(x))) 281 282 If PetscBar() fails (returns nonzero) PetscBaz() is not executed but the result of 283 this expression is boolean false, hence PETSC_ERR_UNNOWN 284 */ 285 *text = "Error occurred in boolean shortcuit in macro"; 286 } else { 287 *text = NULL; 288 } 289 } 290 if (specific) *specific = PetscErrorBaseMessage; 291 PetscFunctionReturn(PETSC_SUCCESS); 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 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 Keys: 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 344 was detected. The return-value of this routine is what is ultimately returned by 345 `SETERRQ()`. 346 347 Note that numerical errors (potential divide by zero, for example) are not managed by the 348 error return codes; they are managed via, for example, `KSPGetConvergedReason()` that 349 indicates if the solve was successful or not. The option `-ksp_error_if_not_converged`, for 350 example, turns numerical failures into hard errors managed via `PetscError()`. 351 352 PETSc provides a rich supply of error handlers, see the list below, and users can also 353 provide their own error handlers. 354 355 If the user sets their own error handler (via `PetscPushErrorHandler()`) they may return any 356 arbitrary value from it, but are encouraged to return nonzero values. If the return value is 357 zero, `SETERRQ()` will ignore the value and return `PETSC_ERR_RETURN` (a nonzero value) 358 instead. 359 360 Most users need not directly use this routine and the error handlers, but can instead use 361 the simplified interface `PetscCall()` or `SETERRQ()`. 362 363 Fortran Notes: 364 This routine is used differently from Fortran 365 $ PetscError(MPI_Comm comm, PetscErrorCode n, PetscErrorType p, char *message) 366 367 Developer Notes: 368 Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes) 369 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 370 but this annoying. 371 372 .seealso: `PetscErrorCode`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, 373 `PetscReturnErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()`, 374 `SETERRQ()`, `PetscCall()`, `CHKMEMQ`, `PetscErrorMessage()`, `PETSCABORT()` 375 @*/ 376 PetscErrorCode PetscError(MPI_Comm comm, int line, const char *func, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, ...) 377 { 378 va_list Argp; 379 size_t fullLength; 380 char buf[2048], *lbuf = NULL; 381 PetscBool ismain; 382 PetscErrorCode ierr; 383 384 if (!PetscErrorHandlingInitialized) return n; 385 if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF; 386 387 /* Compose the message evaluating the print format */ 388 if (mess) { 389 va_start(Argp, mess); 390 ierr = PetscVSNPrintf(buf, 2048, mess, &fullLength, Argp); 391 va_end(Argp); 392 lbuf = buf; 393 if (p == PETSC_ERROR_INITIAL) ierr = PetscStrncpy(PetscErrorBaseMessage, lbuf, sizeof(PetscErrorBaseMessage)); 394 } 395 396 if (p == PETSC_ERROR_INITIAL && n != PETSC_ERR_MEMC) ierr = PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__); 397 398 if (!eh) ierr = PetscTraceBackErrorHandler(comm, line, func, file, n, p, lbuf, NULL); 399 else ierr = (*eh->handler)(comm, line, func, file, n, p, lbuf, eh->ctx); 400 PetscStackClearTop; 401 402 /* 403 If this is called from the main() routine we abort the program. 404 We cannot just return because them some MPI processes may continue to attempt to run 405 while this process simply exits. 406 */ 407 if (func) { 408 PetscErrorCode cmp_ierr = PetscStrncmp(func, "main", 4, &ismain); 409 if (ismain) { 410 if (petscwaitonerrorflg) cmp_ierr = PetscSleep(1000); 411 (void)cmp_ierr; 412 PETSCABORT(comm, ierr); 413 } 414 } 415 #if defined(PETSC_CLANGUAGE_CXX) 416 if (p == PETSC_ERROR_IN_CXX) PetscCxxErrorThrow(); 417 #endif 418 return ierr; 419 } 420 421 /* -------------------------------------------------------------------------*/ 422 423 /*@C 424 PetscIntView - Prints an array of integers; useful for debugging. 425 426 Collective 427 428 Input Parameters: 429 + N - number of integers in array 430 . idx - array of integers 431 - viewer - location to print array, `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF` or 0 432 433 Level: intermediate 434 435 Note: 436 This may be called from within the debugger 437 438 Developer Notes: 439 idx cannot be const because may be passed to binary viewer where temporary byte swapping may be done 440 441 .seealso: `PetscViewer`, `PetscRealView()` 442 @*/ 443 PetscErrorCode PetscIntView(PetscInt N, const PetscInt idx[], PetscViewer viewer) 444 { 445 PetscMPIInt rank, size; 446 PetscInt j, i, n = N / 20, p = N % 20; 447 PetscBool iascii, isbinary; 448 MPI_Comm comm; 449 450 PetscFunctionBegin; 451 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 452 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 3); 453 if (N) PetscValidIntPointer(idx, 2); 454 PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm)); 455 PetscCallMPI(MPI_Comm_size(comm, &size)); 456 PetscCallMPI(MPI_Comm_rank(comm, &rank)); 457 458 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 459 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 460 if (iascii) { 461 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 462 for (i = 0; i < n; i++) { 463 if (size > 1) { 464 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %" PetscInt_FMT ":", rank, 20 * i)); 465 } else { 466 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%" PetscInt_FMT ":", 20 * i)); 467 } 468 for (j = 0; j < 20; j++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, idx[i * 20 + j])); 469 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n")); 470 } 471 if (p) { 472 if (size > 1) { 473 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %" PetscInt_FMT ":", rank, 20 * n)); 474 } else { 475 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%" PetscInt_FMT ":", 20 * n)); 476 } 477 for (i = 0; i < p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, idx[20 * n + i])); 478 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n")); 479 } 480 PetscCall(PetscViewerFlush(viewer)); 481 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 482 } else if (isbinary) { 483 PetscMPIInt *sizes, Ntotal, *displs, NN; 484 PetscInt *array; 485 486 PetscCall(PetscMPIIntCast(N, &NN)); 487 488 if (size > 1) { 489 if (rank) { 490 PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm)); 491 PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_INT, NULL, NULL, NULL, MPIU_INT, 0, comm)); 492 } else { 493 PetscCall(PetscMalloc1(size, &sizes)); 494 PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm)); 495 Ntotal = sizes[0]; 496 PetscCall(PetscMalloc1(size, &displs)); 497 displs[0] = 0; 498 for (i = 1; i < size; i++) { 499 Ntotal += sizes[i]; 500 displs[i] = displs[i - 1] + sizes[i - 1]; 501 } 502 PetscCall(PetscMalloc1(Ntotal, &array)); 503 PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_INT, array, sizes, displs, MPIU_INT, 0, comm)); 504 PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_INT)); 505 PetscCall(PetscFree(sizes)); 506 PetscCall(PetscFree(displs)); 507 PetscCall(PetscFree(array)); 508 } 509 } else { 510 PetscCall(PetscViewerBinaryWrite(viewer, idx, N, PETSC_INT)); 511 } 512 } else { 513 const char *tname; 514 PetscCall(PetscObjectGetName((PetscObject)viewer, &tname)); 515 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname); 516 } 517 PetscFunctionReturn(PETSC_SUCCESS); 518 } 519 520 /*@C 521 PetscRealView - Prints an array of doubles; useful for debugging. 522 523 Collective 524 525 Input Parameters: 526 + N - number of `PetscReal` in array 527 . idx - array of `PetscReal` 528 - viewer - location to print array, `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF` or 0 529 530 Level: intermediate 531 532 Note: 533 This may be called from within the debugger 534 535 Developer Notes: 536 idx cannot be const because may be passed to binary viewer where temporary byte swapping may be done 537 538 .seealso: `PetscViewer`, `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++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[i * 5 + j])); 571 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n")); 572 } 573 if (p) { 574 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 575 if (size > 1) { 576 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, 5 * n)); 577 } else { 578 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", 5 * n)); 579 } 580 PetscCall(PetscViewerASCIISetTab(viewer, 0)); 581 for (i = 0; i < p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[5 * n + i])); 582 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n")); 583 } 584 PetscCall(PetscViewerFlush(viewer)); 585 PetscCall(PetscViewerASCIISetTab(viewer, tab)); 586 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 587 } else if (isbinary) { 588 PetscMPIInt *sizes, *displs, Ntotal, NN; 589 PetscReal *array; 590 591 PetscCall(PetscMPIIntCast(N, &NN)); 592 593 if (size > 1) { 594 if (rank) { 595 PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm)); 596 PetscCallMPI(MPI_Gatherv((PetscReal *)idx, NN, MPIU_REAL, NULL, NULL, NULL, MPIU_REAL, 0, comm)); 597 } else { 598 PetscCall(PetscMalloc1(size, &sizes)); 599 PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm)); 600 Ntotal = sizes[0]; 601 PetscCall(PetscMalloc1(size, &displs)); 602 displs[0] = 0; 603 for (i = 1; i < size; i++) { 604 Ntotal += sizes[i]; 605 displs[i] = displs[i - 1] + sizes[i - 1]; 606 } 607 PetscCall(PetscMalloc1(Ntotal, &array)); 608 PetscCallMPI(MPI_Gatherv((PetscReal *)idx, NN, MPIU_REAL, array, sizes, displs, MPIU_REAL, 0, comm)); 609 PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_REAL)); 610 PetscCall(PetscFree(sizes)); 611 PetscCall(PetscFree(displs)); 612 PetscCall(PetscFree(array)); 613 } 614 } else { 615 PetscCall(PetscViewerBinaryWrite(viewer, (void *)idx, N, PETSC_REAL)); 616 } 617 } else { 618 const char *tname; 619 PetscCall(PetscObjectGetName((PetscObject)viewer, &tname)); 620 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname); 621 } 622 PetscFunctionReturn(PETSC_SUCCESS); 623 } 624 625 /*@C 626 PetscScalarView - Prints an array of `PetscScalar`; useful for debugging. 627 628 Collective 629 630 Input Parameters: 631 + N - number of scalars in array 632 . idx - array of scalars 633 - viewer - location to print array, `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF` or 0 634 635 Level: intermediate 636 637 Note: 638 This may be called from within the debugger 639 640 Developer Notes: 641 idx cannot be const because may be passed to binary viewer where byte swapping may be done 642 643 .seealso: `PetscViewer`, `PetscIntView()`, `PetscRealView()` 644 @*/ 645 PetscErrorCode PetscScalarView(PetscInt N, const PetscScalar idx[], PetscViewer viewer) 646 { 647 PetscMPIInt rank, size; 648 PetscInt j, i, n = N / 3, p = N % 3; 649 PetscBool iascii, isbinary; 650 MPI_Comm comm; 651 652 PetscFunctionBegin; 653 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 654 PetscValidHeader(viewer, 3); 655 if (N) PetscValidScalarPointer(idx, 2); 656 PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm)); 657 PetscCallMPI(MPI_Comm_size(comm, &size)); 658 PetscCallMPI(MPI_Comm_rank(comm, &rank)); 659 660 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 661 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 662 if (iascii) { 663 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 664 for (i = 0; i < n; i++) { 665 if (size > 1) { 666 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, 3 * i)); 667 } else { 668 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", 3 * i)); 669 } 670 for (j = 0; j < 3; j++) { 671 #if defined(PETSC_USE_COMPLEX) 672 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%12.4e,%12.4e)", (double)PetscRealPart(idx[i * 3 + j]), (double)PetscImaginaryPart(idx[i * 3 + j]))); 673 #else 674 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[i * 3 + j])); 675 #endif 676 } 677 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n")); 678 } 679 if (p) { 680 if (size > 1) { 681 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, 3 * n)); 682 } else { 683 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", 3 * n)); 684 } 685 for (i = 0; i < p; i++) { 686 #if defined(PETSC_USE_COMPLEX) 687 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%12.4e,%12.4e)", (double)PetscRealPart(idx[n * 3 + i]), (double)PetscImaginaryPart(idx[n * 3 + i]))); 688 #else 689 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[3 * n + i])); 690 #endif 691 } 692 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n")); 693 } 694 PetscCall(PetscViewerFlush(viewer)); 695 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 696 } else if (isbinary) { 697 PetscMPIInt *sizes, Ntotal, *displs, NN; 698 PetscScalar *array; 699 700 PetscCall(PetscMPIIntCast(N, &NN)); 701 702 if (size > 1) { 703 if (rank) { 704 PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm)); 705 PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_SCALAR, NULL, NULL, NULL, MPIU_SCALAR, 0, comm)); 706 } else { 707 PetscCall(PetscMalloc1(size, &sizes)); 708 PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm)); 709 Ntotal = sizes[0]; 710 PetscCall(PetscMalloc1(size, &displs)); 711 displs[0] = 0; 712 for (i = 1; i < size; i++) { 713 Ntotal += sizes[i]; 714 displs[i] = displs[i - 1] + sizes[i - 1]; 715 } 716 PetscCall(PetscMalloc1(Ntotal, &array)); 717 PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_SCALAR, array, sizes, displs, MPIU_SCALAR, 0, comm)); 718 PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_SCALAR)); 719 PetscCall(PetscFree(sizes)); 720 PetscCall(PetscFree(displs)); 721 PetscCall(PetscFree(array)); 722 } 723 } else { 724 PetscCall(PetscViewerBinaryWrite(viewer, (void *)idx, N, PETSC_SCALAR)); 725 } 726 } else { 727 const char *tname; 728 PetscCall(PetscObjectGetName((PetscObject)viewer, &tname)); 729 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname); 730 } 731 PetscFunctionReturn(PETSC_SUCCESS); 732 } 733 734 #if defined(PETSC_HAVE_CUDA) 735 #include <petscdevice_cuda.h> 736 PETSC_EXTERN const char *PetscCUBLASGetErrorName(cublasStatus_t status) 737 { 738 switch (status) { 739 #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 740 case CUBLAS_STATUS_SUCCESS: 741 return "CUBLAS_STATUS_SUCCESS"; 742 case CUBLAS_STATUS_NOT_INITIALIZED: 743 return "CUBLAS_STATUS_NOT_INITIALIZED"; 744 case CUBLAS_STATUS_ALLOC_FAILED: 745 return "CUBLAS_STATUS_ALLOC_FAILED"; 746 case CUBLAS_STATUS_INVALID_VALUE: 747 return "CUBLAS_STATUS_INVALID_VALUE"; 748 case CUBLAS_STATUS_ARCH_MISMATCH: 749 return "CUBLAS_STATUS_ARCH_MISMATCH"; 750 case CUBLAS_STATUS_MAPPING_ERROR: 751 return "CUBLAS_STATUS_MAPPING_ERROR"; 752 case CUBLAS_STATUS_EXECUTION_FAILED: 753 return "CUBLAS_STATUS_EXECUTION_FAILED"; 754 case CUBLAS_STATUS_INTERNAL_ERROR: 755 return "CUBLAS_STATUS_INTERNAL_ERROR"; 756 case CUBLAS_STATUS_NOT_SUPPORTED: 757 return "CUBLAS_STATUS_NOT_SUPPORTED"; 758 case CUBLAS_STATUS_LICENSE_ERROR: 759 return "CUBLAS_STATUS_LICENSE_ERROR"; 760 #endif 761 default: 762 return "unknown error"; 763 } 764 } 765 PETSC_EXTERN const char *PetscCUSolverGetErrorName(cusolverStatus_t status) 766 { 767 switch (status) { 768 #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */ 769 case CUSOLVER_STATUS_SUCCESS: 770 return "CUSOLVER_STATUS_SUCCESS"; 771 case CUSOLVER_STATUS_NOT_INITIALIZED: 772 return "CUSOLVER_STATUS_NOT_INITIALIZED"; 773 case CUSOLVER_STATUS_INVALID_VALUE: 774 return "CUSOLVER_STATUS_INVALID_VALUE"; 775 case CUSOLVER_STATUS_ARCH_MISMATCH: 776 return "CUSOLVER_STATUS_ARCH_MISMATCH"; 777 case CUSOLVER_STATUS_INTERNAL_ERROR: 778 return "CUSOLVER_STATUS_INTERNAL_ERROR"; 779 #if (CUDART_VERSION >= 9000) /* CUDA 9.0 had these defined on June 2021 */ 780 case CUSOLVER_STATUS_ALLOC_FAILED: 781 return "CUSOLVER_STATUS_ALLOC_FAILED"; 782 case CUSOLVER_STATUS_MAPPING_ERROR: 783 return "CUSOLVER_STATUS_MAPPING_ERROR"; 784 case CUSOLVER_STATUS_EXECUTION_FAILED: 785 return "CUSOLVER_STATUS_EXECUTION_FAILED"; 786 case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED: 787 return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED"; 788 case CUSOLVER_STATUS_NOT_SUPPORTED: 789 return "CUSOLVER_STATUS_NOT_SUPPORTED "; 790 case CUSOLVER_STATUS_ZERO_PIVOT: 791 return "CUSOLVER_STATUS_ZERO_PIVOT"; 792 case CUSOLVER_STATUS_INVALID_LICENSE: 793 return "CUSOLVER_STATUS_INVALID_LICENSE"; 794 #endif 795 #endif 796 default: 797 return "unknown error"; 798 } 799 } 800 PETSC_EXTERN const char *PetscCUFFTGetErrorName(cufftResult result) 801 { 802 switch (result) { 803 case CUFFT_SUCCESS: 804 return "CUFFT_SUCCESS"; 805 case CUFFT_INVALID_PLAN: 806 return "CUFFT_INVALID_PLAN"; 807 case CUFFT_ALLOC_FAILED: 808 return "CUFFT_ALLOC_FAILED"; 809 case CUFFT_INVALID_TYPE: 810 return "CUFFT_INVALID_TYPE"; 811 case CUFFT_INVALID_VALUE: 812 return "CUFFT_INVALID_VALUE"; 813 case CUFFT_INTERNAL_ERROR: 814 return "CUFFT_INTERNAL_ERROR"; 815 case CUFFT_EXEC_FAILED: 816 return "CUFFT_EXEC_FAILED"; 817 case CUFFT_SETUP_FAILED: 818 return "CUFFT_SETUP_FAILED"; 819 case CUFFT_INVALID_SIZE: 820 return "CUFFT_INVALID_SIZE"; 821 case CUFFT_UNALIGNED_DATA: 822 return "CUFFT_UNALIGNED_DATA"; 823 case CUFFT_INCOMPLETE_PARAMETER_LIST: 824 return "CUFFT_INCOMPLETE_PARAMETER_LIST"; 825 case CUFFT_INVALID_DEVICE: 826 return "CUFFT_INVALID_DEVICE"; 827 case CUFFT_PARSE_ERROR: 828 return "CUFFT_PARSE_ERROR"; 829 case CUFFT_NO_WORKSPACE: 830 return "CUFFT_NO_WORKSPACE"; 831 case CUFFT_NOT_IMPLEMENTED: 832 return "CUFFT_NOT_IMPLEMENTED"; 833 case CUFFT_LICENSE_ERROR: 834 return "CUFFT_LICENSE_ERROR"; 835 case CUFFT_NOT_SUPPORTED: 836 return "CUFFT_NOT_SUPPORTED"; 837 default: 838 return "unknown error"; 839 } 840 } 841 #endif 842 843 #if defined(PETSC_HAVE_HIP) 844 #include <petscdevice_hip.h> 845 PETSC_EXTERN const char *PetscHIPBLASGetErrorName(hipblasStatus_t status) 846 { 847 switch (status) { 848 case HIPBLAS_STATUS_SUCCESS: 849 return "HIPBLAS_STATUS_SUCCESS"; 850 case HIPBLAS_STATUS_NOT_INITIALIZED: 851 return "HIPBLAS_STATUS_NOT_INITIALIZED"; 852 case HIPBLAS_STATUS_ALLOC_FAILED: 853 return "HIPBLAS_STATUS_ALLOC_FAILED"; 854 case HIPBLAS_STATUS_INVALID_VALUE: 855 return "HIPBLAS_STATUS_INVALID_VALUE"; 856 case HIPBLAS_STATUS_ARCH_MISMATCH: 857 return "HIPBLAS_STATUS_ARCH_MISMATCH"; 858 case HIPBLAS_STATUS_MAPPING_ERROR: 859 return "HIPBLAS_STATUS_MAPPING_ERROR"; 860 case HIPBLAS_STATUS_EXECUTION_FAILED: 861 return "HIPBLAS_STATUS_EXECUTION_FAILED"; 862 case HIPBLAS_STATUS_INTERNAL_ERROR: 863 return "HIPBLAS_STATUS_INTERNAL_ERROR"; 864 case HIPBLAS_STATUS_NOT_SUPPORTED: 865 return "HIPBLAS_STATUS_NOT_SUPPORTED"; 866 default: 867 return "unknown error"; 868 } 869 } 870 PETSC_EXTERN const char *PetscHIPSPARSEGetErrorName(hipsparseStatus_t status) 871 { 872 switch (status) { 873 case HIPSPARSE_STATUS_SUCCESS: 874 return "HIPSPARSE_STATUS_SUCCESS"; 875 case HIPSPARSE_STATUS_NOT_INITIALIZED: 876 return "HIPSPARSE_STATUS_NOT_INITIALIZED"; 877 case HIPSPARSE_STATUS_ALLOC_FAILED: 878 return "HIPSPARSE_STATUS_ALLOC_FAILED"; 879 case HIPSPARSE_STATUS_INVALID_VALUE: 880 return "HIPSPARSE_STATUS_INVALID_VALUE"; 881 case HIPSPARSE_STATUS_ARCH_MISMATCH: 882 return "HIPSPARSE_STATUS_ARCH_MISMATCH"; 883 case HIPSPARSE_STATUS_MAPPING_ERROR: 884 return "HIPSPARSE_STATUS_MAPPING_ERROR"; 885 case HIPSPARSE_STATUS_EXECUTION_FAILED: 886 return "HIPSPARSE_STATUS_EXECUTION_FAILED"; 887 case HIPSPARSE_STATUS_INTERNAL_ERROR: 888 return "HIPSPARSE_STATUS_INTERNAL_ERROR"; 889 case HIPSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED: 890 return "HIPSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED"; 891 case HIPSPARSE_STATUS_ZERO_PIVOT: 892 return "HIPSPARSE_STATUS_ZERO_PIVOT"; 893 case HIPSPARSE_STATUS_NOT_SUPPORTED: 894 return "HIPSPARSE_STATUS_NOT_SUPPORTED"; 895 case HIPSPARSE_STATUS_INSUFFICIENT_RESOURCES: 896 return "HIPSPARSE_STATUS_INSUFFICIENT_RESOURCES"; 897 default: 898 return "unknown error"; 899 } 900 } 901 PETSC_EXTERN const char *PetscHIPSolverGetErrorName(hipsolverStatus_t status) 902 { 903 switch (status) { 904 case HIPSOLVER_STATUS_SUCCESS: 905 return "HIPSOLVER_STATUS_SUCCESS"; 906 case HIPSOLVER_STATUS_NOT_INITIALIZED: 907 return "HIPSOLVER_STATUS_NOT_INITIALIZED"; 908 case HIPSOLVER_STATUS_ALLOC_FAILED: 909 return "HIPSOLVER_STATUS_ALLOC_FAILED"; 910 case HIPSOLVER_STATUS_MAPPING_ERROR: 911 return "HIPSOLVER_STATUS_MAPPING_ERROR"; 912 case HIPSOLVER_STATUS_INVALID_VALUE: 913 return "HIPSOLVER_STATUS_INVALID_VALUE"; 914 case HIPSOLVER_STATUS_EXECUTION_FAILED: 915 return "HIPSOLVER_STATUS_EXECUTION_FAILED"; 916 case HIPSOLVER_STATUS_INTERNAL_ERROR: 917 return "HIPSOLVER_STATUS_INTERNAL_ERROR"; 918 case HIPSOLVER_STATUS_NOT_SUPPORTED: 919 return "HIPSOLVER_STATUS_NOT_SUPPORTED "; 920 case HIPSOLVER_STATUS_ARCH_MISMATCH: 921 return "HIPSOLVER_STATUS_ARCH_MISMATCH"; 922 case HIPSOLVER_STATUS_HANDLE_IS_NULLPTR: 923 return "HIPSOLVER_STATUS_HANDLE_IS_NULLPTR"; 924 case HIPSOLVER_STATUS_INVALID_ENUM: 925 return "HIPSOLVER_STATUS_INVALID_ENUM"; 926 case HIPSOLVER_STATUS_UNKNOWN: 927 default: 928 return "HIPSOLVER_STATUS_UNKNOWN"; 929 } 930 } 931 #endif 932 933 /*@C 934 PetscMPIErrorString - Given an MPI error code returns the `MPI_Error_string()` appropriately 935 formatted for displaying with the PETSc error handlers. 936 937 Input Parameter: 938 . err - the MPI error code 939 940 Output Parameter: 941 . string - the MPI error message, should declare its length to be larger than `MPI_MAX_ERROR_STRING` 942 943 Level: developer 944 945 Note: 946 Does not return an error code or do error handling because it may be called from inside an error handler 947 948 .seealso: `PetscErrorCode` `PetscErrorMessage()` 949 @*/ 950 void PetscMPIErrorString(PetscMPIInt err, char *string) 951 { 952 char errorstring[MPI_MAX_ERROR_STRING]; 953 PetscMPIInt len, j = 0; 954 955 MPI_Error_string(err, (char *)errorstring, &len); 956 for (PetscMPIInt i = 0; i < len; i++) { 957 string[j++] = errorstring[i]; 958 if (errorstring[i] == '\n') { 959 for (PetscMPIInt k = 0; k < 16; k++) string[j++] = ' '; 960 } 961 } 962 string[j] = 0; 963 } 964