1 2 #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/ 3 4 PetscClassId PETSC_VIEWER_CLASSID; 5 6 static PetscBool PetscViewerPackageInitialized = PETSC_FALSE; 7 /*@C 8 PetscViewerFinalizePackage - This function destroys any global objects created in the Petsc viewers. It is 9 called from PetscFinalize(). 10 11 Level: developer 12 13 .keywords: Petsc, destroy, package, mathematica 14 .seealso: PetscFinalize() 15 @*/ 16 PetscErrorCode PetscViewerFinalizePackage(void) 17 { 18 PetscErrorCode ierr; 19 20 PetscFunctionBegin; 21 if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) { 22 ierr = MPI_Comm_free_keyval(&Petsc_Viewer_keyval);CHKERRQ(ierr); 23 } 24 if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) { 25 ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval);CHKERRQ(ierr); 26 } 27 if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) { 28 ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval);CHKERRQ(ierr); 29 } 30 if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) { 31 ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval);CHKERRQ(ierr); 32 } 33 if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) { 34 ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval);CHKERRQ(ierr); 35 } 36 #if defined(PETSC_HAVE_HDF5) 37 if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) { 38 ierr = MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval);CHKERRQ(ierr); 39 } 40 #endif 41 #if defined(PETSC_USE_SOCKETVIEWER) 42 if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) { 43 ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval);CHKERRQ(ierr); 44 } 45 #endif 46 ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr); 47 PetscViewerPackageInitialized = PETSC_FALSE; 48 PetscViewerRegisterAllCalled = PETSC_FALSE; 49 PetscFunctionReturn(0); 50 } 51 52 /*@C 53 PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package. 54 55 Level: developer 56 57 .keywords: Petsc, initialize, package 58 .seealso: PetscInitialize() 59 @*/ 60 PetscErrorCode PetscViewerInitializePackage(void) 61 { 62 char logList[256]; 63 char *className; 64 PetscBool opt; 65 PetscErrorCode ierr; 66 67 PetscFunctionBegin; 68 if (PetscViewerPackageInitialized) PetscFunctionReturn(0); 69 PetscViewerPackageInitialized = PETSC_TRUE; 70 /* Register Classes */ 71 ierr = PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);CHKERRQ(ierr); 72 73 /* Register Constructors */ 74 ierr = PetscViewerRegisterAll();CHKERRQ(ierr); 75 76 /* Process info exclusions */ 77 ierr = PetscOptionsGetString(NULL,NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr); 78 if (opt) { 79 ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr); 80 if (className) { 81 ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr); 82 } 83 } 84 /* Process summary exclusions */ 85 ierr = PetscOptionsGetString(NULL,NULL, "-log_exclude", logList, 256, &opt);CHKERRQ(ierr); 86 if (opt) { 87 ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr); 88 if (className) { 89 ierr = PetscLogEventDeactivateClass(0);CHKERRQ(ierr); 90 } 91 } 92 #if defined(PETSC_HAVE_MATHEMATICA) 93 ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr); 94 #endif 95 ierr = PetscRegisterFinalize(PetscViewerFinalizePackage);CHKERRQ(ierr); 96 PetscFunctionReturn(0); 97 } 98 99 /*@ 100 PetscViewerDestroy - Destroys a PetscViewer. 101 102 Collective on PetscViewer 103 104 Input Parameters: 105 . viewer - the PetscViewer to be destroyed. 106 107 Level: beginner 108 109 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen() 110 111 @*/ 112 PetscErrorCode PetscViewerDestroy(PetscViewer *viewer) 113 { 114 PetscErrorCode ierr; 115 116 PetscFunctionBegin; 117 if (!*viewer) PetscFunctionReturn(0); 118 PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1); 119 120 ierr = PetscViewerFlush(*viewer);CHKERRQ(ierr); 121 if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; PetscFunctionReturn(0);} 122 123 ierr = PetscObjectSAWsViewOff((PetscObject)*viewer);CHKERRQ(ierr); 124 if ((*viewer)->ops->destroy) { 125 ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr); 126 } 127 ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr); 128 PetscFunctionReturn(0); 129 } 130 131 /*@C 132 PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct. 133 134 Collective on PetscViewer 135 136 Input Parameters: 137 + viewer - the viewer 138 - format - the format 139 140 Output Parameter: 141 . vf - viewer and format object 142 143 Notes: This increases the reference count of the viewer so you can destroy the viewer object after this call 144 Level: developer 145 146 This is used as the context variable for many of the TS, SNES, and KSP monitor functions 147 148 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy() 149 150 @*/ 151 PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf) 152 { 153 PetscErrorCode ierr; 154 155 PetscFunctionBegin; 156 ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); 157 ierr = PetscNew(vf);CHKERRQ(ierr); 158 (*vf)->viewer = viewer; 159 (*vf)->format = format; 160 PetscFunctionReturn(0); 161 } 162 163 164 /*@C 165 PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct. 166 167 Collective on PetscViewer 168 169 Input Parameters: 170 . viewer - the PetscViewerAndFormat to be destroyed. 171 172 Level: developer 173 174 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate() 175 176 @*/ 177 PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf) 178 { 179 PetscErrorCode ierr; 180 181 PetscFunctionBegin; 182 ierr = PetscViewerDestroy(&(*vf)->viewer);CHKERRQ(ierr); 183 ierr = PetscFree(*vf);CHKERRQ(ierr); 184 PetscFunctionReturn(0); 185 } 186 187 /*@C 188 PetscViewerGetType - Returns the type of a PetscViewer. 189 190 Not Collective 191 192 Input Parameter: 193 . viewer - the PetscViewer 194 195 Output Parameter: 196 . type - PetscViewer type (see below) 197 198 Available Types Include: 199 . PETSCVIEWERSOCKET - Socket PetscViewer 200 . PETSCVIEWERASCII - ASCII PetscViewer 201 . PETSCVIEWERBINARY - binary file PetscViewer 202 . PETSCVIEWERSTRING - string PetscViewer 203 . PETSCVIEWERDRAW - drawing PetscViewer 204 205 Level: intermediate 206 207 Note: 208 See include/petscviewer.h for a complete list of PetscViewers. 209 210 PetscViewerType is actually a string 211 212 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 213 214 @*/ 215 PetscErrorCode PetscViewerGetType(PetscViewer viewer,PetscViewerType *type) 216 { 217 PetscFunctionBegin; 218 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 219 PetscValidPointer(type,2); 220 *type = ((PetscObject)viewer)->type_name; 221 PetscFunctionReturn(0); 222 } 223 224 /*@C 225 PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all 226 PetscViewer options in the database. 227 228 Logically Collective on PetscViewer 229 230 Input Parameter: 231 + viewer - the PetscViewer context 232 - prefix - the prefix to prepend to all option names 233 234 Notes: 235 A hyphen (-) must NOT be given at the beginning of the prefix name. 236 The first character of all runtime options is AUTOMATICALLY the hyphen. 237 238 Level: advanced 239 240 .keywords: PetscViewer, set, options, prefix, database 241 242 .seealso: PetscViewerSetFromOptions() 243 @*/ 244 PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[]) 245 { 246 PetscErrorCode ierr; 247 248 PetscFunctionBegin; 249 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 250 ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 251 PetscFunctionReturn(0); 252 } 253 254 /*@C 255 PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all 256 PetscViewer options in the database. 257 258 Logically Collective on PetscViewer 259 260 Input Parameters: 261 + viewer - the PetscViewer context 262 - prefix - the prefix to prepend to all option names 263 264 Notes: 265 A hyphen (-) must NOT be given at the beginning of the prefix name. 266 The first character of all runtime options is AUTOMATICALLY the hyphen. 267 268 Level: advanced 269 270 .keywords: PetscViewer, append, options, prefix, database 271 272 .seealso: PetscViewerGetOptionsPrefix() 273 @*/ 274 PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[]) 275 { 276 PetscErrorCode ierr; 277 278 PetscFunctionBegin; 279 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 280 ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 281 PetscFunctionReturn(0); 282 } 283 284 /*@C 285 PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all 286 PetscViewer options in the database. 287 288 Not Collective 289 290 Input Parameter: 291 . viewer - the PetscViewer context 292 293 Output Parameter: 294 . prefix - pointer to the prefix string used 295 296 Notes: On the fortran side, the user should pass in a string 'prefix' of 297 sufficient length to hold the prefix. 298 299 Level: advanced 300 301 .keywords: PetscViewer, get, options, prefix, database 302 303 .seealso: PetscViewerAppendOptionsPrefix() 304 @*/ 305 PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[]) 306 { 307 PetscErrorCode ierr; 308 309 PetscFunctionBegin; 310 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 311 ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 312 PetscFunctionReturn(0); 313 } 314 315 /*@ 316 PetscViewerSetUp - Sets up the internal viewer data structures for the later use. 317 318 Collective on PetscViewer 319 320 Input Parameters: 321 . viewer - the PetscViewer context 322 323 Notes: 324 For basic use of the PetscViewer classes the user need not explicitly call 325 PetscViewerSetUp(), since these actions will happen automatically. 326 327 Level: advanced 328 329 .keywords: PetscViewer, setup 330 331 .seealso: PetscViewerCreate(), PetscViewerDestroy() 332 @*/ 333 PetscErrorCode PetscViewerSetUp(PetscViewer viewer) 334 { 335 PetscErrorCode ierr; 336 337 PetscFunctionBegin; 338 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 339 if (viewer->setupcalled) PetscFunctionReturn(0); 340 if (viewer->ops->setup) { 341 ierr = (*viewer->ops->setup)(viewer);CHKERRQ(ierr); 342 } 343 viewer->setupcalled = PETSC_TRUE; 344 PetscFunctionReturn(0); 345 } 346 347 /*@C 348 PetscViewerView - Visualizes a viewer object. 349 350 Collective on PetscViewer 351 352 Input Parameters: 353 + v - the viewer 354 - viewer - visualization context 355 356 Notes: 357 The available visualization contexts include 358 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 359 . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 360 output where only the first processor opens 361 the file. All other processors send their 362 data to the first processor to print. 363 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 364 365 Level: beginner 366 367 .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 368 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad() 369 @*/ 370 PetscErrorCode PetscViewerView(PetscViewer v,PetscViewer viewer) 371 { 372 PetscErrorCode ierr; 373 PetscBool iascii; 374 PetscViewerFormat format; 375 #if defined(PETSC_HAVE_SAWS) 376 PetscBool issaws; 377 #endif 378 379 PetscFunctionBegin; 380 PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 381 PetscValidType(v,1); 382 if (!viewer) { 383 ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr); 384 } 385 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 386 PetscCheckSameComm(v,1,viewer,2); 387 388 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 389 #if defined(PETSC_HAVE_SAWS) 390 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 391 #endif 392 if (iascii) { 393 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 394 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr); 395 if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 396 if (v->format) { 397 ierr = PetscViewerASCIIPrintf(viewer," Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr); 398 } 399 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 400 if (v->ops->view) { 401 ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr); 402 } 403 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 404 } 405 #if defined(PETSC_HAVE_SAWS) 406 } else if (issaws) { 407 if (!((PetscObject)v)->amsmem) { 408 ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr); 409 if (v->ops->view) { 410 ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr); 411 } 412 } 413 #endif 414 } 415 PetscFunctionReturn(0); 416 } 417 418 /*@C 419 PetscViewerRead - Reads data from a PetscViewer 420 421 Collective on MPI_Comm 422 423 Input Parameters: 424 + viewer - The viewer 425 . data - Location to write the data 426 . num - Number of items of data to read 427 - datatype - Type of data to read 428 429 Output Parameters: 430 . count - number of items of data actually read, or NULL 431 432 Notes: 433 If datatype is PETSC_STRING and num is negative, reads until a newline character is found, 434 until a maximum of (-num - 1) chars. 435 436 Level: beginner 437 438 Concepts: binary files, ascii files 439 440 .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 441 VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 442 PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer 443 @*/ 444 PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype) 445 { 446 PetscErrorCode ierr; 447 448 PetscFunctionBegin; 449 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 450 if (dtype == PETSC_STRING) { 451 PetscInt c, i = 0, cnt; 452 char *s = (char *)data; 453 if (num >= 0) { 454 for (c = 0; c < num; c++) { 455 /* Skip leading whitespaces */ 456 do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;} 457 while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r'); 458 i++; 459 /* Read strings one char at a time */ 460 do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;} 461 while (s[i-1]!='\n' && s[i-1]!='\t' && s[i-1]!=' ' && s[i-1]!='\0' && s[i-1]!='\v' && s[i-1]!='\f' && s[i-1]!='\r'); 462 /* Terminate final string */ 463 if (c == num-1) s[i-1] = '\0'; 464 } 465 } else { 466 /* Read until a \n is encountered (-num is the max size allowed) */ 467 do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (i == -num && !cnt) break;} 468 while (s[i-1]!='\n'); 469 /* Terminate final string */ 470 s[i-1] = '\0'; 471 c = i; 472 } 473 if (count) *count = c; 474 else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num); 475 } else { 476 ierr = (*viewer->ops->read)(viewer, data, num, count, dtype);CHKERRQ(ierr); 477 } 478 PetscFunctionReturn(0); 479 } 480