Actual source code: vcreatea.c
petsc-3.8.4 2018-03-24
2: #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>
4: /* ---------------------------------------------------------------------*/
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: PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
12: /*@C
13: PetscViewerASCIIGetStdout - Creates a ASCII PetscViewer shared by all processors
14: in a communicator. Error returning version of PETSC_VIEWER_STDOUT_()
16: Collective on MPI_Comm
18: Input Parameter:
19: . comm - the MPI communicator to share the PetscViewer
21: Level: beginner
23: Notes:
24: This should be used in all PETSc source code instead of PETSC_VIEWER_STDOUT_()
26: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
27: PETSC_VIEWER_STDOUT_SELF
29: @*/
30: PetscErrorCode PetscViewerASCIIGetStdout(MPI_Comm comm,PetscViewer *viewer)
31: {
33: PetscBool flg;
34: MPI_Comm ncomm;
37: PetscSpinlockLock(&PetscViewerASCIISpinLockStdout);
38: PetscCommDuplicate(comm,&ncomm,NULL);
39: if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
40: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);
41: }
42: MPI_Attr_get(ncomm,Petsc_Viewer_Stdout_keyval,(void**)viewer,(PetscMPIInt*)&flg);
43: if (!flg) { /* PetscViewer not yet created */
44: PetscViewerASCIIOpen(ncomm,"stdout",viewer);
45: PetscObjectRegisterDestroy((PetscObject)*viewer);
46: MPI_Attr_put(ncomm,Petsc_Viewer_Stdout_keyval,(void*)*viewer);
47: }
48: PetscCommDestroy(&ncomm);
49: PetscSpinlockUnlock(&PetscViewerASCIISpinLockStdout);
50: return(0);
51: }
53: /*@C
54: PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors
55: in a communicator.
57: Collective on MPI_Comm
59: Input Parameter:
60: . comm - the MPI communicator to share the PetscViewer
62: Level: beginner
64: Notes:
65: Unlike almost all other PETSc routines, this does not return
66: an error code. Usually used in the form
67: $ XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
69: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
70: PETSC_VIEWER_STDOUT_SELF
72: @*/
73: PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
74: {
76: PetscViewer viewer;
79: PetscViewerASCIIGetStdout(comm,&viewer);
80: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); return(0);}
81: PetscFunctionReturn(viewer);
82: }
84: /* ---------------------------------------------------------------------*/
86: /*
87: The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
88: is attached to a communicator, in this case the attribute is a PetscViewer.
89: */
90: 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: PetscBool flg;
114: MPI_Comm ncomm;
117: PetscSpinlockLock(&PetscViewerASCIISpinLockStderr);
118: PetscCommDuplicate(comm,&ncomm,NULL);
119: if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
120: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);
121: }
122: MPI_Attr_get(ncomm,Petsc_Viewer_Stderr_keyval,(void**)viewer,(PetscMPIInt*)&flg);
123: if (!flg) { /* PetscViewer not yet created */
124: PetscViewerASCIIOpen(ncomm,"stderr",viewer);
125: PetscObjectRegisterDestroy((PetscObject)*viewer);
126: MPI_Attr_put(ncomm,Petsc_Viewer_Stderr_keyval,(void*)*viewer);
127: }
128: PetscCommDestroy(&ncomm);
129: PetscSpinlockUnlock(&PetscViewerASCIISpinLockStderr);
130: return(0);
131: }
133: /*@C
134: PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
135: in a communicator.
137: Collective on MPI_Comm
139: Input Parameter:
140: . comm - the MPI communicator to share the PetscViewer
142: Level: beginner
144: Note:
145: Unlike almost all other PETSc routines, this does not return
146: an error code. Usually used in the form
147: $ XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
149: .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
150: PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
151: @*/
152: PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
153: {
155: PetscViewer viewer;
158: PetscViewerASCIIGetStderr(comm,&viewer);
159: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); return(0);}
160: PetscFunctionReturn(viewer);
161: }
164: PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
165: /*
166: Called with MPI_Comm_free() is called on a communicator that has a viewer as an attribute. The viewer is not actually destroyed because that is managed by
167: PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
169: This is called by MPI, not by users.
171: */
172: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
173: {
177: PetscInfo1(0,"Removing viewer data attribute in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
178: PetscFunctionReturn(MPI_SUCCESS);
179: }
181: /*@C
182: PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.
184: Collective on MPI_Comm
186: Input Parameters:
187: + comm - the communicator
188: - name - the file name
190: Output Parameter:
191: . lab - the PetscViewer to use with the specified file
193: Level: beginner
195: Notes:
196: This PetscViewer can be destroyed with PetscViewerDestroy().
198: The MPI communicator used here must match that used by the object one is viewing. For example if the
199: Mat was created with a PETSC_COMM_WORLD, then the Viewer must be created with PETSC_COMM_WORLD
201: As shown below, PetscViewerASCIIOpen() is useful in conjunction with
202: MatView() and VecView()
203: .vb
204: PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
205: MatView(matrix,viewer);
206: .ve
208: Concepts: PetscViewerASCII^creating
209: Concepts: printf
210: Concepts: printing
211: Concepts: accessing remote file
212: Concepts: remote file
214: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
215: PetscViewerASCIIGetPointer(), PetscViewerPushFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
216: PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF,
217: @*/
218: PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
219: {
220: PetscErrorCode ierr;
221: PetscViewerLink *vlink,*nv;
222: PetscBool flg,eq;
223: size_t len;
226: PetscStrlen(name,&len);
227: if (!len) {
228: PetscViewerASCIIGetStdout(comm,lab);
229: PetscObjectReference((PetscObject)*lab);
230: return(0);
231: }
232: PetscSpinlockLock(&PetscViewerASCIISpinLockOpen);
233: if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
234: MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);
235: }
236: /*
237: It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
238: we cannot do that, since PetscFileSetName() takes a communicator that already exists.
240: Plus if the original communicator that created the file has since been close this will not detect the old
241: communictor and hence will overwrite the old data. It may be better to simply remove all this code
242: */
243: /* make sure communicator is a PETSc communicator */
244: PetscCommDuplicate(comm,&comm,NULL);
245: /* has file already been opened into a viewer */
246: MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
247: if (flg) {
248: while (vlink) {
249: PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);
250: if (eq) {
251: PetscObjectReference((PetscObject)vlink->viewer);
252: *lab = vlink->viewer;
253: PetscCommDestroy(&comm);
254: PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen);
255: return(0);
256: }
257: vlink = vlink->next;
258: }
259: }
260: PetscViewerCreate(comm,lab);
261: PetscViewerSetType(*lab,PETSCVIEWERASCII);
262: if (name) {
263: PetscViewerFileSetName(*lab,name);
264: }
265: /* save viewer into communicator if needed later */
266: PetscNew(&nv);
267: nv->viewer = *lab;
268: if (!flg) {
269: MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
270: } else {
271: MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
272: if (vlink) {
273: while (vlink->next) vlink = vlink->next;
274: vlink->next = nv;
275: } else {
276: MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
277: }
278: }
279: PetscCommDestroy(&comm);
280: PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen);
281: return(0);
282: }
284: /*@C
285: PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.
287: Collective on MPI_Comm
289: Input Parameters:
290: + comm - the communicator
291: - fd - the FILE pointer
293: Output Parameter:
294: . lab - the PetscViewer to use with the specified file
296: Level: beginner
298: Notes:
299: This PetscViewer can be destroyed with PetscViewerDestroy(), but the fd will NOT be closed.
301: If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
302: then only the first processor in the group uses the file. All other
303: processors send their data to the first processor to print.
305: Concepts: PetscViewerASCII^creating
306: Concepts: printf
307: Concepts: printing
308: Concepts: accessing remote file
309: Concepts: remote file
311: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
312: PetscViewerASCIIGetPointer(), PetscViewerPushFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
313: PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, PetscViewerASCIIOpen()
314: @*/
315: PetscErrorCode PetscViewerASCIIOpenWithFILE(MPI_Comm comm,FILE *fd,PetscViewer *lab)
316: {
320: PetscViewerCreate(comm,lab);
321: PetscViewerSetType(*lab,PETSCVIEWERASCII);
322: PetscViewerASCIISetFILE(*lab,fd);
323: return(0);
324: }
326: PetscErrorCode PetscViewerASCIISetFILE(PetscViewer viewer,FILE *fd)
327: {
328: PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
331: vascii->fd = fd;
332: vascii->closefile = PETSC_FALSE;
333: return(0);
334: }