Actual source code: vcreatea.c
2: #include petsc.h
4: /* ---------------------------------------------------------------------*/
5: /*
6: The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
7: is attached to a communicator, in this case the attribute is a PetscViewer.
8: */
9: static PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
13: /*@C
14: PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors
15: in a communicator.
17: Collective on MPI_Comm
19: Input Parameter:
20: . comm - the MPI communicator to share the PetscViewer
22: Level: beginner
24: Notes:
25: Unlike almost all other PETSc routines, this does not return
26: an error code. Usually used in the form
27: $ XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
29: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
30: PETSC_VIEWER_STDOUT_SELF
32: @*/
33: PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
34: {
36: PetscTruth flg;
37: PetscViewer viewer;
40: if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
41: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);
42: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
43: }
44: MPI_Attr_get(comm,Petsc_Viewer_Stdout_keyval,(void **)&viewer,(PetscMPIInt*)&flg);
45: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
46: if (!flg) { /* PetscViewer not yet created */
47: PetscViewerASCIIOpen(comm,"stdout",&viewer);
48: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
49: PetscObjectRegisterDestroy((PetscObject)viewer);
50: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
51: MPI_Attr_put(comm,Petsc_Viewer_Stdout_keyval,(void*)viewer);
52: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
53: }
54: PetscFunctionReturn(viewer);
55: }
57: /* ---------------------------------------------------------------------*/
58: /*
59: The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
60: is attached to a communicator, in this case the attribute is a PetscViewer.
61: */
62: static PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
66: /*@C
67: PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
68: in a communicator.
70: Collective on MPI_Comm
72: Input Parameter:
73: . comm - the MPI communicator to share the PetscViewer
75: Level: beginner
77: Note:
78: Unlike almost all other PETSc routines, this does not return
79: an error code. Usually used in the form
80: $ XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
82: .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
83: PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
84: @*/
85: PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
86: {
88: PetscTruth flg;
89: PetscViewer viewer;
92: if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
93: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);
94: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
95: }
96: MPI_Attr_get(comm,Petsc_Viewer_Stderr_keyval,(void **)&viewer,(PetscMPIInt*)&flg);
97: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
98: if (!flg) { /* PetscViewer not yet created */
99: PetscViewerASCIIOpen(comm,"stderr",&viewer);
100: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
101: PetscObjectRegisterDestroy((PetscObject)viewer);
102: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
103: MPI_Attr_put(comm,Petsc_Viewer_Stderr_keyval,(void*)viewer);
104: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
105: }
106: PetscFunctionReturn(viewer);
107: }
111: /*@C
112: PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.
114: Collective on MPI_Comm
116: Input Parameters:
117: + comm - the communicator
118: - name - the file name
120: Output Parameter:
121: . lab - the PetscViewer to use with the specified file
123: Level: beginner
125: Notes:
126: This PetscViewer can be destroyed with PetscViewerDestroy().
128: If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
129: then only the first processor in the group opens the file. All other
130: processors send their data to the first processor to print.
132: Each processor can instead write its own independent output by
133: specifying the communicator PETSC_COMM_SELF.
135: As shown below, PetscViewerASCIIOpen() is useful in conjunction with
136: MatView() and VecView()
137: .vb
138: PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
139: MatView(matrix,viewer);
140: .ve
142: Concepts: PetscViewerASCII^creating
143: Concepts: printf
144: Concepts: printing
145: Concepts: accessing remote file
146: Concepts: remote file
148: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
149: PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
150: PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF,
151: @*/
152: PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
153: {
157: PetscViewerCreate(comm,lab);
158: PetscViewerSetType(*lab,PETSC_VIEWER_ASCII);
159: if (name) {
160: PetscViewerSetFilename(*lab,name);
161: }
162: return(0);
163: }