xref: /petsc/src/sys/info/verboseinfo.c (revision 609bdbee21ea3be08735c64dbe00a9ab27759925)
1 
2 /*
3       PetscInfo() is contained in a different file from the other profiling to
4    allow it to be replaced at link time by an alternative routine.
5 */
6 #include <petsc/private/petscimpl.h>        /*I    "petscsys.h"   I*/
7 
8 /*
9   The next three variables determine which, if any, PetscInfo() calls are used.
10   If PetscLogPrintInfo is zero, no info messages are printed.
11   If PetscLogPrintInfoNull is zero, no info messages associated with a null object are printed.
12 
13   If PetscInfoFlags[OBJECT_CLASSID - PETSC_SMALLEST_CLASSID] is zero, no messages related
14   to that object are printed. OBJECT_CLASSID is, for example, MAT_CLASSID.
15 */
16 PetscBool PetscLogPrintInfo     = PETSC_FALSE;
17 PetscBool PetscLogPrintInfoNull = PETSC_FALSE;
18 int       PetscInfoFlags[]      = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
19                                    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
20                                    1,1,1,1,1,1,1,1,1,1,1,1};
21 FILE      *PetscInfoFile = NULL;
22 
23 /*@C
24     PetscInfoAllow - Causes PetscInfo() messages to be printed to standard output.
25 
26     Not Collective, each processor may call this separately, but printing is only
27     turned on if the lowest processor number associated with the PetscObject associated
28     with the call to PetscInfo() has called this routine.
29 
30     Input Parameter:
31 +   flag - PETSC_TRUE or PETSC_FALSE
32 -   filename - optional name of file to write output to (defaults to stdout)
33 
34     Options Database Key:
35 .   -info [optional filename] - Activates PetscInfoAllow()
36 
37     Level: advanced
38 
39    Concepts: debugging^detailed runtime information
40    Concepts: dumping detailed runtime information
41 
42 .seealso: PetscInfo()
43 @*/
44 PetscErrorCode  PetscInfoAllow(PetscBool flag, const char filename[])
45 {
46   char           fname[PETSC_MAX_PATH_LEN], tname[5];
47   PetscMPIInt    rank;
48   PetscErrorCode ierr;
49 
50   PetscFunctionBegin;
51   if (flag && filename) {
52     ierr = PetscFixFilename(filename, fname);CHKERRQ(ierr);
53     ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr);
54     sprintf(tname, ".%d", rank);
55     ierr = PetscStrcat(fname, tname);CHKERRQ(ierr);
56     ierr = PetscFOpen(MPI_COMM_SELF, fname, "w", &PetscInfoFile);CHKERRQ(ierr);
57     if (!PetscInfoFile) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open requested file for writing: %s",fname);
58   } else if (flag) PetscInfoFile = PETSC_STDOUT;
59 
60   PetscLogPrintInfo     = flag;
61   PetscLogPrintInfoNull = flag;
62   PetscFunctionReturn(0);
63 }
64 
65 /*@
66   PetscInfoDeactivateClass - Deactivates PlogInfo() messages for a PETSc object class.
67 
68   Not Collective
69 
70   Input Parameter:
71 . objclass - The object class,  e.g., MAT_CLASSID, SNES_CLASSID, etc.
72 
73   Notes:
74   One can pass 0 to deactivate all messages that are not associated with an object.
75 
76   Level: developer
77 
78 .keywords: allow, information, printing, monitoring
79 .seealso: PetscInfoActivateClass(), PetscInfo(), PetscInfoAllow()
80 @*/
81 PetscErrorCode  PetscInfoDeactivateClass(int objclass)
82 {
83   PetscFunctionBegin;
84   if (!objclass) {
85     PetscLogPrintInfoNull = PETSC_FALSE;
86     PetscFunctionReturn(0);
87   }
88   PetscInfoFlags[objclass - PETSC_SMALLEST_CLASSID - 1] = 0;
89   PetscFunctionReturn(0);
90 }
91 
92 /*@
93   PetscInfoActivateClass - Activates PlogInfo() messages for a PETSc object class.
94 
95   Not Collective
96 
97   Input Parameter:
98 . objclass - The object class, e.g., MAT_CLASSID, SNES_CLASSID, etc.
99 
100   Notes:
101   One can pass 0 to activate all messages that are not associated with an object.
102 
103   Level: developer
104 
105 .keywords: allow, information, printing, monitoring
106 .seealso: PetscInfoDeactivateClass(), PetscInfo(), PetscInfoAllow()
107 @*/
108 PetscErrorCode  PetscInfoActivateClass(int objclass)
109 {
110   PetscFunctionBegin;
111   if (!objclass) PetscLogPrintInfoNull = PETSC_TRUE;
112   else PetscInfoFlags[objclass - PETSC_SMALLEST_CLASSID - 1] = 1;
113   PetscFunctionReturn(0);
114 }
115 
116 /*
117    If the option -history was used, then all printed PetscInfo()
118   messages are also printed to the history file, called by default
119   .petschistory in ones home directory.
120 */
121 extern FILE *petsc_history;
122 
123 /*MC
124     PetscInfo - Logs informative data, which is printed to standard output
125     or a file when the option -info <file> is specified.
126 
127    Synopsis:
128        #include <petscsys.h>
129        PetscErrorCode PetscInfo(void *vobj, const char message[])
130        PetscErrorCode PetscInfo1(void *vobj, const char formatmessage[],arg1)
131        PetscErrorCode PetscInfo2(void *vobj, const char formatmessage[],arg1,arg2)
132        etc
133 
134     Collective over PetscObject argument
135 
136     Input Parameter:
137 +   vobj - object most closely associated with the logging statement or NULL
138 .   message - logging message
139 -   formatmessage - logging message using standard "printf" format
140 
141     Options Database Key:
142 $    -info : activates printing of PetscInfo() messages
143 
144     Level: intermediate
145 
146     Fortran Note: This function does not take the vobj argument, there is only the PetscInfo()
147      version, not PetscInfo1() etc.
148 
149     Example of Usage:
150 $
151 $     Mat A
152 $     double alpha
153 $     PetscInfo1(A,"Matrix uses parameter alpha=%g\n",alpha);
154 $
155 
156    Concepts: runtime information
157 
158 .seealso: PetscInfoAllow()
159 M*/
160 PetscErrorCode  PetscInfo_Private(const char func[],void *vobj, const char message[], ...)
161 {
162   va_list        Argp;
163   PetscMPIInt    rank,urank;
164   size_t         len;
165   PetscObject    obj = (PetscObject)vobj;
166   char           string[8*1024];
167   PetscErrorCode ierr;
168   size_t         fullLength;
169   int            err;
170 
171   PetscFunctionBegin;
172   if (obj) PetscValidHeader(obj,2);
173   PetscValidCharPointer(message,3);
174   if (!PetscLogPrintInfo) PetscFunctionReturn(0);
175   if ((!PetscLogPrintInfoNull) && !vobj) PetscFunctionReturn(0);
176   if (obj && !PetscInfoFlags[obj->classid - PETSC_SMALLEST_CLASSID - 1]) PetscFunctionReturn(0);
177   if (!obj) rank = 0;
178   else {
179     ierr = MPI_Comm_rank(obj->comm, &rank);CHKERRQ(ierr);
180   }
181   if (rank) PetscFunctionReturn(0);
182 
183   ierr = MPI_Comm_rank(MPI_COMM_WORLD, &urank);CHKERRQ(ierr);
184   va_start(Argp, message);
185   sprintf(string, "[%d] %s(): ", urank,func);
186   ierr = PetscStrlen(string, &len);CHKERRQ(ierr);
187   ierr = PetscVSNPrintf(string+len, 8*1024-len,message,&fullLength, Argp);CHKERRQ(ierr);
188   ierr = PetscFPrintf(PETSC_COMM_SELF,PetscInfoFile, "%s", string);CHKERRQ(ierr);
189   err  = fflush(PetscInfoFile);
190   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
191   if (petsc_history) {
192     va_start(Argp, message);
193     ierr = (*PetscVFPrintf)(petsc_history, message, Argp);CHKERRQ(ierr);
194   }
195   va_end(Argp);
196   PetscFunctionReturn(0);
197 }
198