Actual source code: verboseinfo.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

  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>

  8: /*
  9:   The next three variables determine which, if any, PetscInfo() calls are used.
 10:   If PetscLogPrintInfo is zero, no info messages are printed.

 12:   If PetscInfoFlags[OBJECT_CLASSID - PETSC_SMALLEST_CLASSID] is zero, no messages related
 13:   to that object are printed. OBJECT_CLASSID is, for example, MAT_CLASSID.
 14: */
 15: PetscBool PetscLogPrintInfo = PETSC_FALSE;
 16: FILE     *PetscInfoFile     = NULL;
 17: int       PetscInfoFlags[]  = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 18:                                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,
 20:                                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 21:                                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

 23: /*@C
 24:     PetscInfoAllow - Causes PetscInfo() messages to be printed to standard output.

 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.

 30:     Input Parameter:
 31: +   flag - PETSC_TRUE or PETSC_FALSE
 32: -   filename - optional name of file to write output to (defaults to stdout)

 34:     Options Database Key:
 35: .   -info [optional filename] - Activates PetscInfoAllow()

 37:     Level: advanced


 40: .seealso: PetscInfo()
 41: @*/
 42: PetscErrorCode  PetscInfoAllow(PetscBool flag, const char filename[])
 43: {
 44:   char           fname[PETSC_MAX_PATH_LEN], tname[11];
 45:   PetscMPIInt    rank;

 49:   if (flag && filename) {
 50:     PetscFixFilename(filename, fname);
 51:     MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
 52:     sprintf(tname, ".%d", rank);
 53:     PetscStrcat(fname, tname);
 54:     PetscFOpen(MPI_COMM_SELF, fname, "w", &PetscInfoFile);
 55:     if (!PetscInfoFile) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open requested file for writing: %s",fname);
 56:   } else if (flag) PetscInfoFile = PETSC_STDOUT;

 58:   PetscLogPrintInfo = flag;
 59:   return(0);
 60: }

 62: /*@
 63:   PetscInfoDeactivateClass - Deactivates PetscInfo() messages for a PETSc object class.

 65:   Not Collective

 67:   Input Parameter:
 68: . classid - The object class,  e.g., MAT_CLASSID, SNES_CLASSID, etc.

 70:   Notes:
 71:   One can pass 0 to deactivate all messages that are not associated with an object.

 73:   Level: developer

 75: .seealso: PetscInfoActivateClass(), PetscInfo(), PetscInfoAllow()
 76: @*/
 77: PetscErrorCode  PetscInfoDeactivateClass(PetscClassId classid)
 78: {
 80:   if (!classid) classid = PETSC_SMALLEST_CLASSID;
 81:   PetscInfoFlags[classid - PETSC_SMALLEST_CLASSID] = 0;
 82:   return(0);
 83: }

 85: /*@
 86:   PetscInfoActivateClass - Activates PetscInfo() messages for a PETSc object class.

 88:   Not Collective

 90:   Input Parameter:
 91: . classid - The object class, e.g., MAT_CLASSID, SNES_CLASSID, etc.

 93:   Notes:
 94:   One can pass 0 to activate all messages that are not associated with an object.

 96:   Level: developer

 98: .seealso: PetscInfoDeactivateClass(), PetscInfo(), PetscInfoAllow()
 99: @*/
100: PetscErrorCode  PetscInfoActivateClass(PetscClassId classid)
101: {
103:   if (!classid) classid = PETSC_SMALLEST_CLASSID;
104:   PetscInfoFlags[classid - PETSC_SMALLEST_CLASSID] = 1;
105:   return(0);
106: }

108: /*
109:    If the option -history was used, then all printed PetscInfo()
110:   messages are also printed to the history file, called by default
111:   .petschistory in ones home directory.
112: */
113: PETSC_INTERN FILE *petsc_history;

115: /*MC
116:     PetscInfo - Logs informative data, which is printed to standard output
117:     or a file when the option -info <file> is specified.

119:    Synopsis:
120:        #include <petscsys.h>
121:        PetscErrorCode PetscInfo(void *vobj, const char message[])
122:        PetscErrorCode PetscInfo1(void *vobj, const char formatmessage[],arg1)
123:        PetscErrorCode PetscInfo2(void *vobj, const char formatmessage[],arg1,arg2)
124:        etc

126:     Collective over PetscObject argument

128:     Input Parameter:
129: +   vobj - object most closely associated with the logging statement or NULL
130: .   message - logging message
131: -   formatmessage - logging message using standard "printf" format

133:     Options Database Key:
134: $    -info : activates printing of PetscInfo() messages

136:     Level: intermediate

138:     Fortran Note: This function does not take the vobj argument, there is only the PetscInfo()
139:      version, not PetscInfo1() etc.

141:     Example of Usage:
142: $
143: $     Mat A
144: $     double alpha
145: $     PetscInfo1(A,"Matrix uses parameter alpha=%g\n",alpha);
146: $

148: .seealso: PetscInfoAllow()
149: M*/
150: PetscErrorCode  PetscInfo_Private(const char func[],void *vobj, const char message[], ...)
151: {
152:   va_list        Argp;
153:   PetscMPIInt    rank = 0,urank;
154:   size_t         len;
155:   PetscObject    obj = (PetscObject)vobj;
156:   PetscClassId   classid;
157:   char           string[8*1024];
159:   size_t         fullLength;
160:   int            err;

165:   if (!PetscLogPrintInfo) return(0);
166:   classid = obj ? obj->classid : PETSC_SMALLEST_CLASSID;
167:   if (!PetscInfoFlags[classid - PETSC_SMALLEST_CLASSID]) return(0);
168:   if (obj) {MPI_Comm_rank(obj->comm, &rank);}
169:   if (rank) return(0);

171:   MPI_Comm_rank(MPI_COMM_WORLD, &urank);
172:   va_start(Argp, message);
173:   sprintf(string, "[%d] %s(): ", urank,func);
174:   PetscStrlen(string, &len);
175:   PetscVSNPrintf(string+len, 8*1024-len,message,&fullLength, Argp);
176:   PetscFPrintf(PETSC_COMM_SELF,PetscInfoFile, "%s", string);
177:   err  = fflush(PetscInfoFile);
178:   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
179:   if (petsc_history) {
180:     va_start(Argp, message);
181:     (*PetscVFPrintf)(petsc_history, message, Argp);
182:   }
183:   va_end(Argp);
184:   return(0);
185: }