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 "NotFound", /* 0, not found in petsc, but may be in users' code if they called PETSCABORT. */ 13 "sys/error/adebug.c", 14 "sys/error/err.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 = sizeof(PetscAbortSourceFiles)/sizeof(PetscAbortSourceFiles[0]); 32 PetscBool match; 33 char subpath[256]; 34 35 PetscFunctionBegin; 36 PetscValidIntPointer(idx,2); 37 *idx = 0; 38 for (i=1; i<n; i++) { 39 ierr = PetscFixFilename(PetscAbortSourceFiles[i],subpath);CHKERRQ(ierr); 40 ierr = PetscStrendswith(filepath,subpath,&match);CHKERRQ(ierr); 41 if (match) {*idx = i; break;} 42 } 43 PetscFunctionReturn(0); 44 } 45 46 typedef struct _EH *EH; 47 struct _EH { 48 PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*); 49 void *ctx; 50 EH previous; 51 }; 52 53 static EH eh = 0; 54 55 /*@C 56 PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to 57 load the file where the error occured. Then calls the "previous" error handler. 58 59 Not Collective 60 61 Input Parameters: 62 + comm - communicator over which error occured 63 . line - the line number of the error (indicated by __LINE__) 64 . file - the file in which the error was detected (indicated by __FILE__) 65 . mess - an error text string, usually just printed to the screen 66 . n - the generic error number 67 . p - specific error number 68 - ctx - error handler context 69 70 Options Database Key: 71 . -on_error_emacs <machinename> 72 73 Level: developer 74 75 Notes: 76 You must put (server-start) in your .emacs file for the emacsclient software to work 77 78 Most users need not directly employ this routine and the other error 79 handlers, but can instead use the simplified interface SETERRQ, which has 80 the calling sequence 81 $ SETERRQ(PETSC_COMM_SELF,number,p,mess) 82 83 Notes for experienced users: 84 Use PetscPushErrorHandler() to set the desired error handler. 85 86 Developer Note: 87 Since this is an error handler it cannot call CHKERRQ(); thus we just return if an error is detected. 88 89 90 .seealso: PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), 91 PetscAbortErrorHandler() 92 @*/ 93 PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) 94 { 95 PetscErrorCode ierr; 96 char command[PETSC_MAX_PATH_LEN]; 97 const char *pdir; 98 FILE *fp; 99 100 PetscFunctionBegin; 101 ierr = PetscGetPetscDir(&pdir);if (ierr) PetscFunctionReturn(ierr); 102 sprintf(command,"cd %s; emacsclient --no-wait +%d %s\n",pdir,line,file); 103 #if defined(PETSC_HAVE_POPEN) 104 ierr = PetscPOpen(MPI_COMM_WORLD,(char*)ctx,command,"r",&fp);if (ierr) PetscFunctionReturn(ierr); 105 ierr = PetscPClose(MPI_COMM_WORLD,fp);if (ierr) PetscFunctionReturn(ierr); 106 #else 107 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 108 #endif 109 ierr = PetscPopErrorHandler();if (ierr) PetscFunctionReturn(ierr); /* remove this handler from the stack of handlers */ 110 if (!eh) { 111 ierr = PetscTraceBackErrorHandler(comm,line,fun,file,n,p,mess,0);if (ierr) PetscFunctionReturn(ierr); 112 } else { 113 ierr = (*eh->handler)(comm,line,fun,file,n,p,mess,eh->ctx);if (ierr) PetscFunctionReturn(ierr); 114 } 115 PetscFunctionReturn(ierr); 116 } 117 118 /*@C 119 PetscPushErrorHandler - Sets a routine to be called on detection of errors. 120 121 Not Collective 122 123 Input Parameters: 124 + handler - error handler routine 125 - ctx - optional handler context that contains information needed by the handler (for 126 example file pointers for error messages etc.) 127 128 Calling sequence of handler: 129 $ int handler(MPI_Comm comm,int line,char *func,char *file,PetscErrorCode n,int p,char *mess,void *ctx); 130 131 + comm - communicator over which error occured 132 . line - the line number of the error (indicated by __LINE__) 133 . file - the file in which the error was detected (indicated by __FILE__) 134 . n - the generic error number (see list defined in include/petscerror.h) 135 . p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT 136 . mess - an error text string, usually just printed to the screen 137 - ctx - the error handler context 138 139 Options Database Keys: 140 + -on_error_attach_debugger <noxterm,gdb or dbx> 141 - -on_error_abort 142 143 Level: intermediate 144 145 Notes: 146 The currently available PETSc error handlers include PetscTraceBackErrorHandler(), 147 PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler(), PetscReturnErrorHandler(). 148 149 Fortran Notes: 150 You can only push one error handler from Fortran before poping it. 151 152 .seealso: PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), PetscTraceBackErrorHandler(), PetscPushSignalHandler() 153 154 @*/ 155 PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void *ctx) 156 { 157 EH neweh; 158 PetscErrorCode ierr; 159 160 PetscFunctionBegin; 161 ierr = PetscNew(&neweh);CHKERRQ(ierr); 162 if (eh) neweh->previous = eh; 163 else neweh->previous = 0; 164 neweh->handler = handler; 165 neweh->ctx = ctx; 166 eh = neweh; 167 PetscFunctionReturn(0); 168 } 169 170 /*@ 171 PetscPopErrorHandler - Removes the latest error handler that was 172 pushed with PetscPushErrorHandler(). 173 174 Not Collective 175 176 Level: intermediate 177 178 .seealso: PetscPushErrorHandler() 179 @*/ 180 PetscErrorCode PetscPopErrorHandler(void) 181 { 182 EH tmp; 183 PetscErrorCode ierr; 184 185 PetscFunctionBegin; 186 if (!eh) PetscFunctionReturn(0); 187 tmp = eh; 188 eh = eh->previous; 189 ierr = PetscFree(tmp);CHKERRQ(ierr); 190 PetscFunctionReturn(0); 191 } 192 193 /*@C 194 PetscReturnErrorHandler - Error handler that causes a return to the current 195 level. 196 197 Not Collective 198 199 Input Parameters: 200 + comm - communicator over which error occurred 201 . line - the line number of the error (indicated by __LINE__) 202 . file - the file in which the error was detected (indicated by __FILE__) 203 . mess - an error text string, usually just printed to the screen 204 . n - the generic error number 205 . p - specific error number 206 - ctx - error handler context 207 208 Level: developer 209 210 Notes: 211 Most users need not directly employ this routine and the other error 212 handlers, but can instead use the simplified interface SETERRQ, which has 213 the calling sequence 214 $ SETERRQ(comm,number,mess) 215 216 Notes for experienced users: 217 This routine is good for catching errors such as zero pivots in preconditioners 218 or breakdown of iterative methods. It is not appropriate for memory violations 219 and similar errors. 220 221 Use PetscPushErrorHandler() to set the desired error handler. The 222 currently available PETSc error handlers include PetscTraceBackErrorHandler(), 223 PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscAbortErrorHandler() 224 225 .seealso: PetscPushErrorHandler(), PetscPopErrorHandler(). 226 @*/ 227 228 PetscErrorCode PetscReturnErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) 229 { 230 PetscFunctionBegin; 231 PetscFunctionReturn(n); 232 } 233 234 static char PetscErrorBaseMessage[1024]; 235 /* 236 The numerical values for these are defined in include/petscerror.h; any changes 237 there must also be made here 238 */ 239 static const char *PetscErrorStrings[] = { 240 /*55 */ "Out of memory", 241 "No support for this operation for this object type", 242 "No support for this operation on this system", 243 /*58 */ "Operation done in wrong order", 244 /*59 */ "Signal received", 245 /*60 */ "Nonconforming object sizes", 246 "Argument aliasing not permitted", 247 "Invalid argument", 248 /*63 */ "Argument out of range", 249 "Corrupt argument: https://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind", 250 "Unable to open file", 251 "Read from file failed", 252 "Write to file failed", 253 "Invalid pointer", 254 /*69 */ "Arguments must have same type", 255 /*70 */ "Attempt to use a pointer that does not point to a valid accessible location", 256 /*71 */ "Zero pivot in LU factorization: https://www.mcs.anl.gov/petsc/documentation/faq.html#zeropivot", 257 /*72 */ "Floating point exception", 258 /*73 */ "Object is in wrong state", 259 "Corrupted Petsc object", 260 "Arguments are incompatible", 261 "Error in external library", 262 /*77 */ "Petsc has generated inconsistent data", 263 "Memory corruption: https://www.mcs.anl.gov/petsc/documentation/installation.html#valgrind", 264 "Unexpected data in file", 265 /*80 */ "Arguments must have same communicators", 266 /*81 */ "Zero pivot in Cholesky factorization: https://www.mcs.anl.gov/petsc/documentation/faq.html#zeropivot", 267 " ", 268 " ", 269 "Overflow in integer operation: https://www.mcs.anl.gov/petsc/documentation/faq.html#64-bit-indices", 270 /*85 */ "Null argument, when expecting valid pointer", 271 /*86 */ "Unknown type. Check for miss-spelling or missing package: https://www.mcs.anl.gov/petsc/documentation/installation.html#external", 272 /*87 */ "MPI library at runtime is not compatible with MPI used at compile time", 273 /*88 */ "Error in system call", 274 /*89 */ "Object Type not set: https://www.mcs.anl.gov/petsc/documentation/faq.html#objecttypenotset", 275 /*90 */ " ", 276 /* */ " ", 277 /*92 */ "See https://www.mcs.anl.gov/petsc/documentation/linearsolvertable.html for possible LU and Cholesky solvers", 278 /*93 */ "You cannot overwrite this option since that will conflict with other previously set options", 279 }; 280 281 /*@C 282 PetscErrorMessage - returns the text string associated with a PETSc error code. 283 284 Not Collective 285 286 Input Parameter: 287 . errnum - the error code 288 289 Output Parameter: 290 + text - the error message (NULL if not desired) 291 - specific - the specific error message that was set with SETERRxxx() or PetscError(). (NULL if not desired) 292 293 Level: developer 294 295 .seealso: PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscError(), SETERRQ(), CHKERRQ() 296 PetscAbortErrorHandler(), PetscTraceBackErrorHandler() 297 @*/ 298 PetscErrorCode PetscErrorMessage(int errnum,const char *text[],char **specific) 299 { 300 PetscFunctionBegin; 301 if (text && errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) *text = PetscErrorStrings[errnum-PETSC_ERR_MIN_VALUE-1]; 302 else if (text) *text = 0; 303 304 if (specific) *specific = PetscErrorBaseMessage; 305 PetscFunctionReturn(0); 306 } 307 308 #if defined(PETSC_CLANGUAGE_CXX) 309 /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software 310 * would be broken if implementations did not handle it it some common cases. However, keep in mind 311 * 312 * Rule 62. Don't allow exceptions to propagate across module boundaries 313 * 314 * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface 315 * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed. 316 * 317 * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message 318 * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that 319 * seems crazy to me. 320 */ 321 #include <sstream> 322 #include <stdexcept> 323 static void PetscCxxErrorThrow() { 324 const char *str; 325 if (eh && eh->ctx) { 326 std::ostringstream *msg; 327 msg = (std::ostringstream*) eh->ctx; 328 str = msg->str().c_str(); 329 } else str = "Error detected in C PETSc"; 330 331 throw std::runtime_error(str); 332 } 333 #endif 334 335 /*@C 336 PetscError - Routine that is called when an error has been detected, 337 usually called through the macro SETERRQ(PETSC_COMM_SELF,). 338 339 Not Collective 340 341 Input Parameters: 342 + comm - communicator over which error occurred. ALL ranks of this communicator MUST call this routine 343 . line - the line number of the error (indicated by __LINE__) 344 . func - the function name in which the error was detected 345 . file - the file in which the error was detected (indicated by __FILE__) 346 . n - the generic error number 347 . p - PETSC_ERROR_INITIAL indicates the error was initially detected, PETSC_ERROR_REPEAT indicates this is a traceback from a previously detected error 348 - mess - formatted message string - aka printf 349 350 Level: intermediate 351 352 Notes: 353 Most users need not directly use this routine and the error handlers, but 354 can instead use the simplified interface SETERRQ, which has the calling 355 sequence 356 $ SETERRQ(comm,n,mess) 357 358 Fortran Note: 359 This routine is used differently from Fortran 360 $ PetscError(MPI_Comm comm,PetscErrorCode n,PetscErrorType p,char *message) 361 362 Experienced users can set the error handler with PetscPushErrorHandler(). 363 364 Developer Note: Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes) 365 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 366 but this annoying. 367 368 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), SETERRQ(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), PetscErrorMessage() 369 @*/ 370 PetscErrorCode PetscError(MPI_Comm comm,int line,const char *func,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,...) 371 { 372 va_list Argp; 373 size_t fullLength; 374 char buf[2048],*lbuf = 0; 375 PetscBool ismain; 376 PetscErrorCode ierr; 377 378 PetscFunctionBegin; 379 if (!func) func = "User provided function"; 380 if (!file) file = "User file"; 381 if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF; 382 383 /* Compose the message evaluating the print format */ 384 if (mess) { 385 va_start(Argp,mess); 386 PetscVSNPrintf(buf,2048,mess,&fullLength,Argp); 387 va_end(Argp); 388 lbuf = buf; 389 if (p == PETSC_ERROR_INITIAL) PetscStrncpy(PetscErrorBaseMessage,lbuf,1023); 390 } 391 392 if (!eh) ierr = PetscTraceBackErrorHandler(comm,line,func,file,n,p,lbuf,0); 393 else ierr = (*eh->handler)(comm,line,func,file,n,p,lbuf,eh->ctx); 394 395 /* 396 If this is called from the main() routine we call PETSCABORT instead of 397 return to allow the parallel program to be properly shutdown. 398 399 Since this is in the error handler we don't check the errors below. Of course, 400 PetscStrncmp() does its own error checking which is problamatic 401 */ 402 PetscStrncmp(func,"main",4,&ismain); 403 if (ismain) PETSCABORT(PETSC_COMM_WORLD,(int)ierr); 404 405 #if defined(PETSC_CLANGUAGE_CXX) 406 if (p == PETSC_ERROR_IN_CXX) { 407 PetscCxxErrorThrow(); 408 } 409 #endif 410 PetscFunctionReturn(ierr); 411 } 412 413 /* -------------------------------------------------------------------------*/ 414 415 /*@C 416 PetscIntView - Prints an array of integers; useful for debugging. 417 418 Collective on PetscViewer 419 420 Input Parameters: 421 + N - number of integers in array 422 . idx - array of integers 423 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 424 425 Level: intermediate 426 427 Developer Notes: 428 idx cannot be const because may be passed to binary viewer where byte swapping is done 429 430 .seealso: PetscRealView() 431 @*/ 432 PetscErrorCode PetscIntView(PetscInt N,const PetscInt idx[],PetscViewer viewer) 433 { 434 PetscErrorCode ierr; 435 PetscMPIInt rank,size; 436 PetscInt j,i,n = N/20,p = N % 20; 437 PetscBool iascii,isbinary; 438 MPI_Comm comm; 439 440 PetscFunctionBegin; 441 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 442 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 443 if (N) PetscValidIntPointer(idx,2); 444 ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 445 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 446 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 447 448 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 449 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 450 if (iascii) { 451 ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr); 452 for (i=0; i<n; i++) { 453 if (size > 1) { 454 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D:", rank, 20*i);CHKERRQ(ierr); 455 } else { 456 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*i);CHKERRQ(ierr); 457 } 458 for (j=0; j<20; j++) { 459 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[i*20+j]);CHKERRQ(ierr); 460 } 461 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 462 } 463 if (p) { 464 if (size > 1) { 465 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D:",rank ,20*n);CHKERRQ(ierr); 466 } else { 467 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%D:",20*n);CHKERRQ(ierr); 468 } 469 for (i=0; i<p; i++) { ierr = PetscViewerASCIISynchronizedPrintf(viewer," %D",idx[20*n+i]);CHKERRQ(ierr);} 470 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 471 } 472 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 473 ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr); 474 } else if (isbinary) { 475 PetscMPIInt *sizes,Ntotal,*displs,NN; 476 PetscInt *array; 477 478 ierr = PetscMPIIntCast(N,&NN);CHKERRQ(ierr); 479 480 if (size > 1) { 481 if (rank) { 482 ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr); 483 ierr = MPI_Gatherv((void*)idx,NN,MPIU_INT,0,0,0,MPIU_INT,0,comm);CHKERRQ(ierr); 484 } else { 485 ierr = PetscMalloc1(size,&sizes);CHKERRQ(ierr); 486 ierr = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr); 487 Ntotal = sizes[0]; 488 ierr = PetscMalloc1(size,&displs);CHKERRQ(ierr); 489 displs[0] = 0; 490 for (i=1; i<size; i++) { 491 Ntotal += sizes[i]; 492 displs[i] = displs[i-1] + sizes[i-1]; 493 } 494 ierr = PetscMalloc1(Ntotal,&array);CHKERRQ(ierr); 495 ierr = MPI_Gatherv((void*)idx,NN,MPIU_INT,array,sizes,displs,MPIU_INT,0,comm);CHKERRQ(ierr); 496 ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 497 ierr = PetscFree(sizes);CHKERRQ(ierr); 498 ierr = PetscFree(displs);CHKERRQ(ierr); 499 ierr = PetscFree(array);CHKERRQ(ierr); 500 } 501 } else { 502 ierr = PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 503 } 504 } else { 505 const char *tname; 506 ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr); 507 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 508 } 509 PetscFunctionReturn(0); 510 } 511 512 /*@C 513 PetscRealView - Prints an array of doubles; useful for debugging. 514 515 Collective on PetscViewer 516 517 Input Parameters: 518 + N - number of PetscReal in array 519 . idx - array of PetscReal 520 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 521 522 Level: intermediate 523 524 Developer Notes: 525 idx cannot be const because may be passed to binary viewer where byte swapping is done 526 527 .seealso: PetscIntView() 528 @*/ 529 PetscErrorCode PetscRealView(PetscInt N,const PetscReal idx[],PetscViewer viewer) 530 { 531 PetscErrorCode ierr; 532 PetscMPIInt rank,size; 533 PetscInt j,i,n = N/5,p = N % 5; 534 PetscBool iascii,isbinary; 535 MPI_Comm comm; 536 537 PetscFunctionBegin; 538 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 539 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3); 540 PetscValidScalarPointer(idx,2); 541 ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 542 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 543 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 544 545 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 546 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 547 if (iascii) { 548 ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr); 549 for (i=0; i<n; i++) { 550 if (size > 1) { 551 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,(int)5*i);CHKERRQ(ierr); 552 } else { 553 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",(int)5*i);CHKERRQ(ierr); 554 } 555 for (j=0; j<5; j++) { 556 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*5+j]);CHKERRQ(ierr); 557 } 558 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 559 } 560 if (p) { 561 if (size > 1) { 562 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,(int)5*n);CHKERRQ(ierr); 563 } else { 564 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",(int)5*n);CHKERRQ(ierr); 565 } 566 for (i=0; i<p; i++) { PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[5*n+i]);CHKERRQ(ierr);} 567 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 568 } 569 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 570 ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr); 571 } else if (isbinary) { 572 PetscMPIInt *sizes,*displs, Ntotal,NN; 573 PetscReal *array; 574 575 ierr = PetscMPIIntCast(N,&NN);CHKERRQ(ierr); 576 577 if (size > 1) { 578 if (rank) { 579 ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr); 580 ierr = MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,0,0,0,MPIU_REAL,0,comm);CHKERRQ(ierr); 581 } else { 582 ierr = PetscMalloc1(size,&sizes);CHKERRQ(ierr); 583 ierr = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr); 584 Ntotal = sizes[0]; 585 ierr = PetscMalloc1(size,&displs);CHKERRQ(ierr); 586 displs[0] = 0; 587 for (i=1; i<size; i++) { 588 Ntotal += sizes[i]; 589 displs[i] = displs[i-1] + sizes[i-1]; 590 } 591 ierr = PetscMalloc1(Ntotal,&array);CHKERRQ(ierr); 592 ierr = MPI_Gatherv((PetscReal*)idx,NN,MPIU_REAL,array,sizes,displs,MPIU_REAL,0,comm);CHKERRQ(ierr); 593 ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_REAL,PETSC_TRUE);CHKERRQ(ierr); 594 ierr = PetscFree(sizes);CHKERRQ(ierr); 595 ierr = PetscFree(displs);CHKERRQ(ierr); 596 ierr = PetscFree(array);CHKERRQ(ierr); 597 } 598 } else { 599 ierr = PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr); 600 } 601 } else { 602 const char *tname; 603 ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr); 604 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 605 } 606 PetscFunctionReturn(0); 607 } 608 609 /*@C 610 PetscScalarView - Prints an array of scalars; useful for debugging. 611 612 Collective on PetscViewer 613 614 Input Parameters: 615 + N - number of scalars in array 616 . idx - array of scalars 617 - viewer - location to print array, PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF or 0 618 619 Level: intermediate 620 621 Developer Notes: 622 idx cannot be const because may be passed to binary viewer where byte swapping is done 623 624 .seealso: PetscIntView(), PetscRealView() 625 @*/ 626 PetscErrorCode PetscScalarView(PetscInt N,const PetscScalar idx[],PetscViewer viewer) 627 { 628 PetscErrorCode ierr; 629 PetscMPIInt rank,size; 630 PetscInt j,i,n = N/3,p = N % 3; 631 PetscBool iascii,isbinary; 632 MPI_Comm comm; 633 634 PetscFunctionBegin; 635 if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 636 PetscValidHeader(viewer,3); 637 if (N) PetscValidScalarPointer(idx,2); 638 ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 639 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 640 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 641 642 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 643 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 644 if (iascii) { 645 ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr); 646 for (i=0; i<n; i++) { 647 if (size > 1) { 648 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,3*i);CHKERRQ(ierr); 649 } else { 650 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*i);CHKERRQ(ierr); 651 } 652 for (j=0; j<3; j++) { 653 #if defined(PETSC_USE_COMPLEX) 654 ierr = PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[i*3+j]),(double)PetscImaginaryPart(idx[i*3+j]));CHKERRQ(ierr); 655 #else 656 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[i*3+j]);CHKERRQ(ierr); 657 #endif 658 } 659 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 660 } 661 if (p) { 662 if (size > 1) { 663 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %2d:",rank ,3*n);CHKERRQ(ierr); 664 } else { 665 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%2d:",3*n);CHKERRQ(ierr); 666 } 667 for (i=0; i<p; i++) { 668 #if defined(PETSC_USE_COMPLEX) 669 ierr = PetscViewerASCIISynchronizedPrintf(viewer," (%12.4e,%12.4e)", (double)PetscRealPart(idx[n*3+i]),(double)PetscImaginaryPart(idx[n*3+i]));CHKERRQ(ierr); 670 #else 671 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %12.4e",(double)idx[3*n+i]);CHKERRQ(ierr); 672 #endif 673 } 674 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 675 } 676 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 677 ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr); 678 } else if (isbinary) { 679 PetscMPIInt *sizes,Ntotal,*displs,NN; 680 PetscScalar *array; 681 682 ierr = PetscMPIIntCast(N,&NN);CHKERRQ(ierr); 683 684 if (size > 1) { 685 if (rank) { 686 ierr = MPI_Gather(&NN,1,MPI_INT,0,0,MPI_INT,0,comm);CHKERRQ(ierr); 687 ierr = MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,0,0,0,MPIU_SCALAR,0,comm);CHKERRQ(ierr); 688 } else { 689 ierr = PetscMalloc1(size,&sizes);CHKERRQ(ierr); 690 ierr = MPI_Gather(&NN,1,MPI_INT,sizes,1,MPI_INT,0,comm);CHKERRQ(ierr); 691 Ntotal = sizes[0]; 692 ierr = PetscMalloc1(size,&displs);CHKERRQ(ierr); 693 displs[0] = 0; 694 for (i=1; i<size; i++) { 695 Ntotal += sizes[i]; 696 displs[i] = displs[i-1] + sizes[i-1]; 697 } 698 ierr = PetscMalloc1(Ntotal,&array);CHKERRQ(ierr); 699 ierr = MPI_Gatherv((void*)idx,NN,MPIU_SCALAR,array,sizes,displs,MPIU_SCALAR,0,comm);CHKERRQ(ierr); 700 ierr = PetscViewerBinaryWrite(viewer,array,Ntotal,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 701 ierr = PetscFree(sizes);CHKERRQ(ierr); 702 ierr = PetscFree(displs);CHKERRQ(ierr); 703 ierr = PetscFree(array);CHKERRQ(ierr); 704 } 705 } else { 706 ierr = PetscViewerBinaryWrite(viewer,(void*) idx,N,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 707 } 708 } else { 709 const char *tname; 710 ierr = PetscObjectGetName((PetscObject)viewer,&tname);CHKERRQ(ierr); 711 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle that PetscViewer of type %s",tname); 712 } 713 PetscFunctionReturn(0); 714 } 715 716 717 718 719