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