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