Actual source code: vcreatea.c
1: #define PETSC_DLL
3: #include ../src/sys/viewer/impls/ascii/asciiimpl.h
5: /* ---------------------------------------------------------------------*/
6: /*
7: The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
8: is attached to a communicator, in this case the attribute is a PetscViewer.
9: */
10: static PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
14: /*@C
15: PetscViewerASCIIGetStdout - Creates a ASCII PetscViewer shared by all processors
16: in a communicator. Error returning version of PETSC_VIEWER_STDOUT_()
18: Collective on MPI_Comm
20: Input Parameter:
21: . comm - the MPI communicator to share the PetscViewer
23: Level: beginner
25: Notes:
26: This should be used in all PETSc source code instead of PETSC_VIEWER_STDOUT_()
28: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
29: PETSC_VIEWER_STDOUT_SELF
31: @*/
32: PetscErrorCode PetscViewerASCIIGetStdout(MPI_Comm comm,PetscViewer *viewer)
33: {
35: PetscTruth flg;
38: if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
39: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);
40: }
41: MPI_Attr_get(comm,Petsc_Viewer_Stdout_keyval,(void **)viewer,(PetscMPIInt*)&flg);
42: if (!flg) { /* PetscViewer not yet created */
43: PetscViewerASCIIOpen(comm,"stdout",viewer);
44: PetscObjectRegisterDestroy((PetscObject)*viewer);
45: MPI_Attr_put(comm,Petsc_Viewer_Stdout_keyval,(void*)*viewer);
46: }
47: return(0);
48: }
52: /*@C
53: PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors
54: in a communicator.
56: Collective on MPI_Comm
58: Input Parameter:
59: . comm - the MPI communicator to share the PetscViewer
61: Level: beginner
63: Notes:
64: Unlike almost all other PETSc routines, this does not return
65: an error code. Usually used in the form
66: $ XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
68: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
69: PETSC_VIEWER_STDOUT_SELF
71: @*/
72: PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
73: {
75: PetscViewer viewer;
78: PetscViewerASCIIGetStdout(comm,&viewer);
79: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
80: PetscFunctionReturn(viewer);
81: }
83: /* ---------------------------------------------------------------------*/
84: /*
85: The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
86: is attached to a communicator, in this case the attribute is a PetscViewer.
87: */
88: static PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
92: /*@C
93: PetscViewerASCIIGetStderr - Creates a ASCII PetscViewer shared by all processors
94: in a communicator. Error returning version of PETSC_VIEWER_STDERR_()
96: Collective on MPI_Comm
98: Input Parameter:
99: . comm - the MPI communicator to share the PetscViewer
101: Level: beginner
103: Notes:
104: This should be used in all PETSc source code instead of PETSC_VIEWER_STDERR_()
106: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDERR_WORLD,
107: PETSC_VIEWER_STDERR_SELF
109: @*/
110: PetscErrorCode PetscViewerASCIIGetStderr(MPI_Comm comm,PetscViewer *viewer)
111: {
113: PetscTruth flg;
116: if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
117: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);
118: }
119: MPI_Attr_get(comm,Petsc_Viewer_Stderr_keyval,(void **)viewer,(PetscMPIInt*)&flg);
120: if (!flg) { /* PetscViewer not yet created */
121: PetscViewerASCIIOpen(comm,"stderr",viewer);
122: PetscObjectRegisterDestroy((PetscObject)*viewer);
123: MPI_Attr_put(comm,Petsc_Viewer_Stderr_keyval,(void*)*viewer);
124: }
125: return(0);
126: }
130: /*@C
131: PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
132: in a communicator.
134: Collective on MPI_Comm
136: Input Parameter:
137: . comm - the MPI communicator to share the PetscViewer
139: Level: beginner
141: Note:
142: Unlike almost all other PETSc routines, this does not return
143: an error code. Usually used in the form
144: $ XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
146: .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
147: PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
148: @*/
149: PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
150: {
152: PetscViewer viewer;
155: PetscViewerASCIIGetStderr(comm,&viewer);
156: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
157: PetscFunctionReturn(viewer);
158: }
161: PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
165: /*
166: Private routine to delete internal storage when a communicator is freed.
167: This is called by MPI, not by users.
169: */
170: PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void* attr_val,void* extra_state)
171: {
175: PetscInfo1(0,"Deleting viewer data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
176: PetscFunctionReturn(MPI_SUCCESS);
177: }
182: /*@C
183: PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.
185: Collective on MPI_Comm
187: Input Parameters:
188: + comm - the communicator
189: - name - the file name
191: Output Parameter:
192: . lab - the PetscViewer to use with the specified file
194: Level: beginner
196: Notes:
197: This PetscViewer can be destroyed with PetscViewerDestroy().
199: If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
200: then only the first processor in the group opens the file. All other
201: processors send their data to the first processor to print.
203: Each processor can instead write its own independent output by
204: specifying the communicator PETSC_COMM_SELF.
206: As shown below, PetscViewerASCIIOpen() is useful in conjunction with
207: MatView() and VecView()
208: .vb
209: PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
210: MatView(matrix,viewer);
211: .ve
213: Concepts: PetscViewerASCII^creating
214: Concepts: printf
215: Concepts: printing
216: Concepts: accessing remote file
217: Concepts: remote file
219: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
220: PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
221: PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF,
222: @*/
223: PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
224: {
225: PetscErrorCode ierr;
226: PetscViewerLink *vlink,*nv;
227: PetscTruth flg,eq;
228: size_t len;
231: PetscStrlen(name,&len);
232: if (!len) {
233: PetscViewerASCIIGetStdout(comm,lab);
234: PetscObjectReference((PetscObject)*lab);
235: return(0);
236: }
237: if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
238: MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);
239: }
240: /* make sure communicator is a PETSc communicator */
241: PetscCommDuplicate(comm,&comm,PETSC_NULL);
242: /* has file already been opened into a viewer */
243: MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
244: if (flg) {
245: while (vlink) {
246: PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);
247: if (eq) {
248: PetscObjectReference((PetscObject)vlink->viewer);
249: *lab = vlink->viewer;
250: PetscCommDestroy(&comm);
251: return(0);
252: }
253: vlink = vlink->next;
254: }
255: }
256: PetscViewerCreate(comm,lab);
257: PetscViewerSetType(*lab,PETSC_VIEWER_ASCII);
258: if (name) {
259: PetscViewerFileSetName(*lab,name);
260: }
261: /* save viewer into communicator if needed later */
262: PetscNew(PetscViewerLink,&nv);
263: nv->viewer = *lab;
264: if (!flg) {
265: MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
266: } else {
267: MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
268: if (vlink) {
269: while (vlink->next) vlink = vlink->next;
270: vlink->next = nv;
271: } else {
272: MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
273: }
274: }
275: PetscCommDestroy(&comm);
276: return(0);
277: }