xref: /petsc/include/petsclog.h (revision dfd676b1a855b7f967ece75a22ee7f6626d10f89)
1 /*
2     Defines profile/logging in PETSc.
3 */
4 
5 #if !defined(PETSCLOG_H)
6 #define PETSCLOG_H
7 #include <petscsys.h>
8 #include <petsctime.h>
9 
10 /* General logging of information; different from event logging */
11 PETSC_EXTERN PetscErrorCode PetscInfo_Private(const char[],PetscObject,const char[],...) PETSC_ATTRIBUTE_FORMAT(3,4);
12 #if defined(PETSC_USE_INFO)
13 #define PetscInfo(A,...) PetscInfo_Private(PETSC_FUNCTION_NAME,((PetscObject)A),__VA_ARGS__)
14 #else
15 #define PetscInfo(A,...) 0
16 #endif
17 
18 #define PetscInfo1(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
19 #define PetscInfo2(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
20 #define PetscInfo3(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
21 #define PetscInfo4(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
22 #define PetscInfo5(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
23 #define PetscInfo6(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
24 #define PetscInfo7(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
25 #define PetscInfo8(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
26 #define PetscInfo9(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
27 
28 /*E
29     PetscInfoCommFlag - Describes the method by which to filter PetscInfo() by communicator size
30 
31     Used as an input for PetscInfoSetFilterCommSelf()
32 
33 $   PETSC_INFO_COMM_ALL - Default uninitialized value. PetscInfo() will not filter based on communicator size (i.e. will
34 print for all communicators)
35 $   PETSC_INFO_COMM_NO_SELF - PetscInfo() will NOT print for communicators with size = 1 (i.e. *_COMM_SELF)
36 $   PETSC_INFO_COMM_ONLY_SELF - PetscInfo will ONLY print for communicators with size = 1
37 
38     Level: intermediate
39 
40 .seealso: PetscInfo(), PetscInfoSetFromOptions(), PetscInfoSetFilterCommSelf()
41 E*/
42 typedef enum {
43   PETSC_INFO_COMM_ALL = -1,
44   PETSC_INFO_COMM_NO_SELF = 0,
45   PETSC_INFO_COMM_ONLY_SELF = 1
46 } PetscInfoCommFlag;
47 
48 PETSC_EXTERN const char * const PetscInfoCommFlags[];
49 PETSC_EXTERN PetscErrorCode PetscInfoDeactivateClass(PetscClassId);
50 PETSC_EXTERN PetscErrorCode PetscInfoActivateClass(PetscClassId);
51 PETSC_EXTERN PetscErrorCode PetscInfoEnabled(PetscClassId, PetscBool *);
52 PETSC_EXTERN PetscErrorCode PetscInfoAllow(PetscBool);
53 PETSC_EXTERN PetscErrorCode PetscInfoSetFile(const char[],const char[]);
54 PETSC_EXTERN PetscErrorCode PetscInfoGetFile(char **,FILE **);
55 PETSC_EXTERN PetscErrorCode PetscInfoSetClasses(PetscBool,PetscInt,const char *const *);
56 PETSC_EXTERN PetscErrorCode PetscInfoGetClass(const char *, PetscBool *);
57 PETSC_EXTERN PetscErrorCode PetscInfoGetInfo(PetscBool *,PetscBool *,PetscBool *,PetscBool *,PetscInfoCommFlag *);
58 PETSC_EXTERN PetscErrorCode PetscInfoProcessClass(const char[],PetscInt,PetscClassId[]);
59 PETSC_EXTERN PetscErrorCode PetscInfoSetFilterCommSelf(PetscInfoCommFlag);
60 PETSC_EXTERN PetscErrorCode PetscInfoSetFromOptions(PetscOptions);
61 PETSC_EXTERN PetscErrorCode PetscInfoDestroy(void);
62 PETSC_EXTERN PetscBool      PetscLogPrintInfo;  /* if true, indicates PetscInfo() is turned on */
63 
64 /*MC
65     PetscLogEvent - id used to identify PETSc or user events which timed portions (blocks of executable)
66      code.
67 
68     Level: intermediate
69 
70 .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscLogStage
71 M*/
72 typedef int PetscLogEvent;
73 
74 /*MC
75     PetscLogStage - id used to identify user stages (phases, sections) of runs - for logging
76 
77     Level: intermediate
78 
79 .seealso: PetscLogStageRegister(), PetscLogStagePush(), PetscLogStagePop(), PetscLogEvent
80 M*/
81 typedef int PetscLogStage;
82 
83 #define PETSC_EVENT  1311311
84 PETSC_EXTERN PetscLogEvent PETSC_LARGEST_EVENT;
85 
86 /* Global flop counter */
87 PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
88 PETSC_EXTERN PetscLogDouble petsc_tmp_flops;
89 
90 /* We must make the following structures available to access the event
91      activation flags in the PetscLogEventBegin/End() macros. These are not part of the PETSc public
92      API and are not intended to be used by other parts of PETSc or by users.
93 
94      The code that manipulates these structures is in src/sys/logging/utils.
95 */
96 typedef struct _n_PetscIntStack *PetscIntStack;
97 
98 /* -----------------------------------------------------------------------------------------------------*/
99 /*
100     PetscClassRegInfo, PetscClassPerfInfo - Each class has two data structures associated with it. The first has
101        static information about it, the second collects statistics on how many objects of the class are created,
102        how much memory they use, etc.
103 
104     PetscClassRegLog, PetscClassPerfLog - arrays of the PetscClassRegInfo and PetscClassPerfInfo for all classes.
105 */
106 typedef struct  {
107   char           *name;   /* The class name */
108   PetscClassId   classid; /* The integer identifying this class */
109 } PetscClassRegInfo;
110 
111 typedef struct {
112   PetscClassId   id;           /* The integer identifying this class */
113   int            creations;    /* The number of objects of this class created */
114   int            destructions; /* The number of objects of this class destroyed */
115   PetscLogDouble mem;          /* The total memory allocated by objects of this class */
116   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects */
117 } PetscClassPerfInfo;
118 
119 typedef struct _n_PetscClassRegLog *PetscClassRegLog;
120 struct _n_PetscClassRegLog {
121   int               numClasses; /* The number of classes registered */
122   int               maxClasses; /* The maximum number of classes */
123   PetscClassRegInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
124 };
125 
126 typedef struct _n_PetscClassPerfLog *PetscClassPerfLog;
127 struct _n_PetscClassPerfLog {
128   int                numClasses; /* The number of logging classes */
129   int                maxClasses; /* The maximum number of classes */
130   PetscClassPerfInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
131 };
132 /* -----------------------------------------------------------------------------------------------------*/
133 /*
134     PetscEventRegInfo, PetscEventPerfInfo - Each event has two data structures associated with it. The first has
135        static information about it, the second collects statistics on how many times the event is used, how
136        much time it takes, etc.
137 
138     PetscEventRegLog, PetscEventPerfLog - an array of all PetscEventRegInfo and PetscEventPerfInfo for all events. There is one
139       of these for each stage.
140 
141 */
142 typedef struct {
143   char         *name;         /* The name of this event */
144   PetscClassId classid;       /* The class the event is associated with */
145   PetscBool    collective;    /* Flag this event as collective */
146 #if defined (PETSC_HAVE_MPE)
147   int          mpe_id_begin;  /* MPE IDs that define the event */
148   int          mpe_id_end;
149 #endif
150 } PetscEventRegInfo;
151 
152 typedef struct {
153   int            id;            /* The integer identifying this event */
154   PetscBool      active;        /* The flag to activate logging */
155   PetscBool      visible;       /* The flag to print info in summary */
156   int            depth;         /* The nesting depth of the event call */
157   int            count;         /* The number of times this event was executed */
158   PetscLogDouble flops, flops2, flopsTmp; /* The flops and flops^2 used in this event */
159   PetscLogDouble time, time2, timeTmp;    /* The time and time^2 taken for this event */
160   PetscLogDouble syncTime;                /* The synchronization barrier time */
161   PetscLogDouble dof[8];        /* The number of degrees of freedom associated with this event */
162   PetscLogDouble errors[8];     /* The errors (user-defined) associated with this event */
163   PetscLogDouble numMessages;   /* The number of messages in this event */
164   PetscLogDouble messageLength; /* The total message lengths in this event */
165   PetscLogDouble numReductions; /* The number of reductions in this event */
166   PetscLogDouble memIncrease;   /* How much the resident memory has increased in this event */
167   PetscLogDouble mallocIncrease;/* How much the maximum malloced space has increased in this event */
168   PetscLogDouble mallocSpace;   /* How much the space was malloced and kept during this event */
169   PetscLogDouble mallocIncreaseEvent;  /* Maximum of the high water mark with in event minus memory available at the end of the event */
170   #if defined(PETSC_HAVE_DEVICE)
171   PetscLogDouble CpuToGpuCount; /* The total number of CPU to GPU copies */
172   PetscLogDouble GpuToCpuCount; /* The total number of GPU to CPU copies */
173   PetscLogDouble CpuToGpuSize;  /* The total size of CPU to GPU copies */
174   PetscLogDouble GpuToCpuSize;  /* The total size of GPU to CPU copies */
175   PetscLogDouble GpuFlops;      /* The flops done on a GPU in this event */
176   PetscLogDouble GpuTime;       /* The time spent on a GPU in this event */
177   #endif
178 } PetscEventPerfInfo;
179 
180 typedef struct _n_PetscEventRegLog *PetscEventRegLog;
181 struct _n_PetscEventRegLog {
182   int               numEvents;  /* The number of registered events */
183   int               maxEvents;  /* The maximum number of events */
184   PetscEventRegInfo *eventInfo; /* The registration information for each event */
185 };
186 
187 typedef struct _n_PetscEventPerfLog *PetscEventPerfLog;
188 struct _n_PetscEventPerfLog {
189   int                numEvents;  /* The number of logging events */
190   int                maxEvents;  /* The maximum number of events */
191   PetscEventPerfInfo *eventInfo; /* The performance information for each event */
192 };
193 /* ------------------------------------------------------------------------------------------------------------*/
194 /*
195    PetscStageInfo - Contains all the information about a particular stage.
196 
197    PetscStageLog - An array of PetscStageInfo for each registered stage. There is a single one of these in the code.
198 */
199 typedef struct _PetscStageInfo {
200   char               *name;     /* The stage name */
201   PetscBool          used;      /* The stage was pushed on this processor */
202   PetscEventPerfInfo perfInfo;  /* The stage performance information */
203   PetscEventPerfLog  eventLog;  /* The event information for this stage */
204   PetscClassPerfLog  classLog;  /* The class information for this stage */
205 } PetscStageInfo;
206 
207 typedef struct _n_PetscStageLog *PetscStageLog;
208 struct _n_PetscStageLog {
209   int              numStages;   /* The number of registered stages */
210   int              maxStages;   /* The maximum number of stages */
211   PetscIntStack    stack;       /* The stack for active stages */
212   int              curStage;    /* The current stage (only used in macros so we don't call PetscIntStackTop) */
213   PetscStageInfo   *stageInfo;  /* The information for each stage */
214   PetscEventRegLog eventLog;    /* The registered events */
215   PetscClassRegLog classLog;    /* The registered classes */
216 };
217 /* -----------------------------------------------------------------------------------------------------*/
218 
219 PETSC_EXTERN PetscErrorCode PetscLogObjectParent(PetscObject,PetscObject);
220 PETSC_EXTERN PetscErrorCode PetscLogObjectMemory(PetscObject,PetscLogDouble);
221 
222 #if defined(PETSC_USE_LOG)  /* --- Logging is turned on --------------------------------*/
223 PETSC_EXTERN PetscStageLog petsc_stageLog;
224 PETSC_EXTERN PetscErrorCode PetscLogGetStageLog(PetscStageLog*);
225 PETSC_EXTERN PetscErrorCode PetscStageLogGetCurrent(PetscStageLog,int*);
226 PETSC_EXTERN PetscErrorCode PetscStageLogGetEventPerfLog(PetscStageLog,int,PetscEventPerfLog*);
227 
228 /*
229    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
230 
231    For the complex numbers version, note that
232        1 complex addition = 2 flops
233        1 complex multiplication = 6 flops,
234    where we define 1 flop as that for a double precision scalar.  We roughly approximate
235    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
236    to the assumption that we're counting mostly additions and multiplications -- and
237    roughly the same number of each.  More accurate counting could be done by distinguishing
238    among the various arithmetic operations.
239  */
240 
241 #if defined(PETSC_USE_COMPLEX)
242 #define PETSC_FLOPS_PER_OP 4.0
243 #else
244 #define PETSC_FLOPS_PER_OP 1.0
245 #endif
246 
247 /*@C
248        PetscLogFlops - Log how many flops are performed in a calculation
249 
250    Input Parameter:
251 .   flops - the number of flops
252 
253    Notes:
254      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
255      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)
256 
257    Level: intermediate
258 
259 .seealso: PetscLogView(), PetscLogGpuFlops()
260 @*/
261 
262 static inline PetscErrorCode PetscLogFlops(PetscLogDouble n)
263 {
264   PetscFunctionBegin;
265 #if defined(PETSC_USE_DEBUG)
266   PetscCheck(n >= 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
267 #endif
268   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
269   PetscFunctionReturn(0);
270 }
271 
272 PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);
273 
274 #if defined (PETSC_HAVE_MPE)
275 PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
276 PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
277 #endif
278 
279 PETSC_EXTERN PetscErrorCode (*PetscLogPLB)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
280 PETSC_EXTERN PetscErrorCode (*PetscLogPLE)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
281 PETSC_EXTERN PetscErrorCode (*PetscLogPHC)(PetscObject);
282 PETSC_EXTERN PetscErrorCode (*PetscLogPHD)(PetscObject);
283 
284 #define PetscLogObjectParents(p,n,d)  PetscMacroReturnStandard(for (int _i=0; _i<(n); ++_i) PetscCall(PetscLogObjectParent((PetscObject)(p),(PetscObject)(d)[_i]));)
285 #define PetscLogObjectCreate(h)      ((PetscLogPHC) ? (*PetscLogPHC)((PetscObject)(h)) : 0)
286 #define PetscLogObjectDestroy(h)     ((PetscLogPHD) ? (*PetscLogPHD)((PetscObject)(h)) : 0)
287 PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2,3);
288 
289 /* Initialization functions */
290 PETSC_EXTERN PetscErrorCode PetscLogDefaultBegin(void);
291 PETSC_EXTERN PetscErrorCode PetscLogAllBegin(void);
292 PETSC_EXTERN PetscErrorCode PetscLogNestedBegin(void);
293 PETSC_EXTERN PetscErrorCode PetscLogTraceBegin(FILE *);
294 PETSC_EXTERN PetscErrorCode PetscLogActions(PetscBool);
295 PETSC_EXTERN PetscErrorCode PetscLogObjects(PetscBool);
296 PETSC_EXTERN PetscErrorCode PetscLogSetThreshold(PetscLogDouble,PetscLogDouble*);
297 PETSC_EXTERN PetscErrorCode PetscLogSet(PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject),
298                                         PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
299 
300 /* Output functions */
301 PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
302 PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
303 PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);
304 
305 /* Status checking functions */
306 PETSC_EXTERN PetscErrorCode PetscLogIsActive(PetscBool*);
307 
308 /* Stage functions */
309 PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[],PetscLogStage*);
310 PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
311 PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
312 PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage,PetscBool);
313 PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage,PetscBool*);
314 PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage,PetscBool);
315 PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage,PetscBool*);
316 PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char[],PetscLogStage*);
317 
318 /* Event functions */
319 PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[],PetscClassId,PetscLogEvent*);
320 PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent,PetscBool);
321 PETSC_EXTERN PetscErrorCode PetscLogEventIncludeClass(PetscClassId);
322 PETSC_EXTERN PetscErrorCode PetscLogEventExcludeClass(PetscClassId);
323 PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
324 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(PetscLogEvent);
325 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent);
326 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent);
327 PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent,PetscBool);
328 PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
329 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
330 PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[],PetscLogEvent*);
331 PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(int,PetscLogEvent,PetscEventPerfInfo*);
332 PETSC_EXTERN PetscErrorCode PetscLogEventSetDof(PetscLogEvent, PetscInt, PetscLogDouble);
333 PETSC_EXTERN PetscErrorCode PetscLogEventSetError(PetscLogEvent, PetscInt, PetscLogDouble);
334 
335 /* Global counters */
336 PETSC_EXTERN PetscLogDouble petsc_irecv_ct;
337 PETSC_EXTERN PetscLogDouble petsc_isend_ct;
338 PETSC_EXTERN PetscLogDouble petsc_recv_ct;
339 PETSC_EXTERN PetscLogDouble petsc_send_ct;
340 PETSC_EXTERN PetscLogDouble petsc_irecv_len;
341 PETSC_EXTERN PetscLogDouble petsc_isend_len;
342 PETSC_EXTERN PetscLogDouble petsc_recv_len;
343 PETSC_EXTERN PetscLogDouble petsc_send_len;
344 PETSC_EXTERN PetscLogDouble petsc_allreduce_ct;
345 PETSC_EXTERN PetscLogDouble petsc_gather_ct;
346 PETSC_EXTERN PetscLogDouble petsc_scatter_ct;
347 PETSC_EXTERN PetscLogDouble petsc_wait_ct;
348 PETSC_EXTERN PetscLogDouble petsc_wait_any_ct;
349 PETSC_EXTERN PetscLogDouble petsc_wait_all_ct;
350 PETSC_EXTERN PetscLogDouble petsc_sum_of_waits_ct;
351 
352 PETSC_EXTERN PetscBool      PetscLogMemory;
353 
354 PETSC_EXTERN PetscBool PetscLogSyncOn;  /* true if logging synchronization is enabled */
355 PETSC_EXTERN PetscErrorCode PetscLogEventSynchronize(PetscLogEvent, MPI_Comm);
356 
357 #define PetscLogEventSync(e,comm) \
358   (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
359     PetscLogEventSynchronize((e),(comm)) : 0))
360 
361 #define PetscLogEventBegin(e,o1,o2,o3,o4) \
362   (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
363     (*PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0))
364 
365 #define PetscLogEventEnd(e,o1,o2,o3,o4) \
366   (((PetscLogPLE && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
367     (*PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0))
368 
369 PETSC_EXTERN PetscErrorCode PetscLogEventGetFlops(PetscLogEvent,PetscLogDouble*);
370 PETSC_EXTERN PetscErrorCode PetscLogEventZeroFlops(PetscLogEvent);
371 
372 /*
373      These are used internally in the PETSc routines to keep a count of MPI messages and
374    their sizes.
375 
376      This does not work for MPI-Uni because our include/petsc/mpiuni/mpi.h file
377    uses macros to defined the MPI operations.
378 
379      It does not work correctly from HP-UX because it processes the
380    macros in a way that sometimes it double counts, hence
381    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
382 
383      It does not work with Windows because winmpich lacks MPI_Type_size()
384 */
385 #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
386 /*
387    Logging of MPI activities
388 */
389 static inline PetscErrorCode PetscMPITypeSize(PetscInt count,MPI_Datatype type,PetscLogDouble *length)
390 {
391   PetscMPIInt typesize;
392 
393   if (type == MPI_DATATYPE_NULL) return 0;
394   PetscCallMPI(MPI_Type_size(type,&typesize));
395   *length += (PetscLogDouble) (count*typesize);
396   return 0;
397 }
398 
399 static inline PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm,const PetscMPIInt *counts,MPI_Datatype type,PetscLogDouble *length)
400 {
401   PetscMPIInt    typesize,size,p;
402 
403   if (type == MPI_DATATYPE_NULL) return 0;
404   PetscCallMPI(MPI_Comm_size(comm,&size));
405   PetscCallMPI(MPI_Type_size(type,&typesize));
406   for (p=0; p<size; ++p) *length += (PetscLogDouble)(counts[p]*typesize);
407   return 0;
408 }
409 
410 static inline PetscErrorCode PetscMPITypeSizeCount(PetscInt n,const PetscMPIInt *counts,MPI_Datatype type,PetscLogDouble *length)
411 {
412   PetscMPIInt typesize,p;
413 
414   if (type == MPI_DATATYPE_NULL) return 0;
415   PetscCallMPI(MPI_Type_size(type,&typesize));
416   for (p=0; p<n; ++p) *length += (PetscLogDouble)(counts[p]*typesize);
417   return 0;
418 }
419 
420 /*
421     Returns 1 if the communicator is parallel else zero
422 */
423 static inline int PetscMPIParallelComm(MPI_Comm comm)
424 {
425   PetscMPIInt size; MPI_Comm_size(comm,&size); return size > 1;
426 }
427 
428 #define MPI_Irecv(buf,count,datatype,source,tag,comm,request) \
429   ((petsc_irecv_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || MPI_Irecv((buf),(count),(datatype),(source),(tag),(comm),(request)))
430 
431 #define MPI_Irecv_c(buf,count,datatype,source,tag,comm,request) \
432   ((petsc_irecv_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || MPI_Irecv_c((buf),(count),(datatype),(source),(tag),(comm),(request)))
433 
434 #define MPI_Isend(buf,count,datatype,dest,tag,comm,request) \
435   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend((buf),(count),(datatype),(dest),(tag),(comm),(request)))
436 
437 #define MPI_Isend_c(buf,count,datatype,dest,tag,comm,request) \
438   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend_c((buf),(count),(datatype),(dest),(tag),(comm),(request)))
439 
440 #define MPI_Startall_irecv(count,datatype,number,requests) \
441   ((petsc_irecv_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || ((number) && MPI_Startall((number),(requests))))
442 
443 #define MPI_Startall_isend(count,datatype,number,requests) \
444   ((petsc_isend_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || ((number) && MPI_Startall((number),(requests))))
445 
446 #define MPI_Start_isend(count,datatype,requests) \
447   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_isend_len)) || MPI_Start((requests)))
448 
449 #define MPI_Recv(buf,count,datatype,source,tag,comm,status) \
450   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv((buf),(count),(datatype),(source),(tag),(comm),(status)))
451 
452 #define MPI_Recv_c(buf,count,datatype,source,tag,comm,status) \
453   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv_c((buf),(count),(datatype),(source),(tag),(comm),(status)))
454 
455 #define MPI_Send(buf,count,datatype,dest,tag,comm) \
456   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send((buf),(count),(datatype),(dest),(tag),(comm)))
457 
458 #define MPI_Send_c(buf,count,datatype,dest,tag,comm) \
459   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send_c((buf),(count),(datatype),(dest),(tag),(comm)))
460 
461 #define MPI_Wait(request,status) \
462   ((petsc_wait_ct++,petsc_sum_of_waits_ct++,0) || MPI_Wait((request),(status)))
463 
464 #define MPI_Waitany(a,b,c,d) \
465   ((petsc_wait_any_ct++,petsc_sum_of_waits_ct++,0) || MPI_Waitany((a),(b),(c),(d)))
466 
467 #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
468   ((petsc_wait_all_ct++,petsc_sum_of_waits_ct += (PetscLogDouble) (count),0) || MPI_Waitall((count),(array_of_requests),(array_of_statuses)))
469 
470 #define MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm) \
471   (petsc_allreduce_ct += PetscMPIParallelComm((comm)),MPI_Allreduce((sendbuf),(recvbuf),(count),(datatype),(op),(comm)))
472 
473 #define MPI_Bcast(buffer,count,datatype,root,comm) \
474   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Bcast((buffer),(count),(datatype),(root),(comm)))
475 
476 #define MPI_Reduce_scatter_block(sendbuf,recvbuf,recvcount,datatype,op,comm) \
477   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Reduce_scatter_block((sendbuf),(recvbuf),(recvcount),(datatype),(op),(comm)))
478 
479 #define MPI_Alltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
480   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Alltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
481 
482 #define MPI_Alltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
483   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(sendcnts),(sendtype),(&petsc_send_len)) || MPI_Alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))
484 
485 #define MPI_Allgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
486   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
487 
488 #define MPI_Allgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm) \
489   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm)))
490 
491 #define MPI_Gather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
492   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
493 
494 #define MPI_Gatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm) \
495   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm)))
496 
497 #define MPI_Scatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
498   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
499 
500 #define MPI_Scatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm) \
501   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
502 
503 #define MPI_Ialltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
504   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Ialltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))
505 
506 #define MPI_Ialltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
507   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(sendcnts),(sendtype),(&petsc_send_len)) || MPI_Ialltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm),(request)))
508 
509 #define MPI_Iallgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
510   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))
511 
512 #define MPI_Iallgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm,request) \
513   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm),(request)))
514 
515 #define MPI_Igather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
516   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
517 
518 #define MPI_Igatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm,request) \
519   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm),(request)))
520 
521 #define MPI_Iscatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
522   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
523 
524 #define MPI_Iscatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
525   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
526 
527 #else
528 
529 #define MPI_Startall_irecv(count,datatype,number,requests) \
530   ((number) && MPI_Startall((number),(requests)))
531 
532 #define MPI_Startall_isend(count,datatype,number,requests) \
533   ((number) && MPI_Startall((number),(requests)))
534 
535 #define MPI_Start_isend(count,datatype,requests) \
536   (MPI_Start((requests)))
537 
538 #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
539 
540 #else  /* ---Logging is turned off --------------------------------------------*/
541 
542 #define PetscLogMemory                     PETSC_FALSE
543 
544 #define PetscLogFlops(n)                   0
545 #define PetscGetFlops(a)                   (*(a) = 0.0,0)
546 
547 #define PetscLogStageRegister(a,b)         0
548 #define PetscLogStagePush(a)               0
549 #define PetscLogStagePop()                 0
550 #define PetscLogStageSetActive(a,b)        0
551 #define PetscLogStageGetActive(a,b)        0
552 #define PetscLogStageGetVisible(a,b)       0
553 #define PetscLogStageSetVisible(a,b)       0
554 #define PetscLogStageGetId(a,b)            (*(b)=0,0)
555 
556 #define PetscLogEventRegister(a,b,c)       0
557 #define PetscLogEventSetCollective(a,b)    0
558 #define PetscLogEventIncludeClass(a)       0
559 #define PetscLogEventExcludeClass(a)       0
560 #define PetscLogEventActivate(a)           0
561 #define PetscLogEventDeactivate(a)         0
562 #define PetscLogEventDeactivatePush(a)     0
563 #define PetscLogEventDeactivatePop(a)      0
564 #define PetscLogEventActivateClass(a)      0
565 #define PetscLogEventDeactivateClass(a)    0
566 #define PetscLogEventSetActiveAll(a,b)     0
567 #define PetscLogEventGetId(a,b)            (*(b)=0,0)
568 #define PetscLogEventGetPerfInfo(a,b,c)    0
569 #define PetscLogEventSetDof(a,b,c)         0
570 #define PetscLogEventSetError(a,b,c)       0
571 
572 #define PetscLogPLB                        0
573 #define PetscLogPLE                        0
574 #define PetscLogPHC                        0
575 #define PetscLogPHD                        0
576 
577 #define PetscLogObjectParents(p,n,c)       0
578 #define PetscLogObjectCreate(h)            0
579 #define PetscLogObjectDestroy(h)           0
580 PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject,const char[],...) PETSC_ATTRIBUTE_FORMAT(2,3);
581 
582 #define PetscLogDefaultBegin()             0
583 #define PetscLogAllBegin()                 0
584 #define PetscLogNestedBegin()              0
585 #define PetscLogTraceBegin(file)           0
586 #define PetscLogActions(a)                 0
587 #define PetscLogObjects(a)                 0
588 #define PetscLogSetThreshold(a,b)          0
589 #define PetscLogSet(lb,le)                 0
590 #define PetscLogIsActive(flag)             (*(flag) = PETSC_FALSE,0)
591 
592 #define PetscLogView(viewer)               0
593 #define PetscLogViewFromOptions()          0
594 #define PetscLogDump(c)                    0
595 
596 #define PetscLogEventSync(e,comm)          0
597 #define PetscLogEventBegin(e,o1,o2,o3,o4)  0
598 #define PetscLogEventEnd(e,o1,o2,o3,o4)    0
599 
600 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
601 #define MPI_Startall_irecv(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
602 #define MPI_Startall_isend(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
603 #define MPI_Start_isend(count,datatype,requests)           MPI_Start(requests)
604 
605 #endif   /* PETSC_USE_LOG */
606 
607 #if defined (PETSC_USE_LOG) && defined(PETSC_HAVE_DEVICE)
608 
609 /* Global GPU counters */
610 PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
611 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
612 PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
613 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
614 PETSC_EXTERN PetscLogDouble petsc_ctog_ct_scalar;
615 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct_scalar;
616 PETSC_EXTERN PetscLogDouble petsc_ctog_sz_scalar;
617 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz_scalar;
618 PETSC_EXTERN PetscLogDouble petsc_gflops;
619 PETSC_EXTERN PetscLogDouble petsc_gtime;
620 
621 static inline PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size)
622 {
623   PetscFunctionBegin;
624   petsc_ctog_ct += 1;
625   petsc_ctog_sz += size;
626   PetscFunctionReturn(0);
627 }
628 
629 static inline PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size)
630 {
631   PetscFunctionBegin;
632   petsc_gtoc_ct += 1;
633   petsc_gtoc_sz += size;
634   PetscFunctionReturn(0);
635 }
636 
637 static inline PetscErrorCode PetscLogCpuToGpuScalar(PetscLogDouble size)
638 {
639   PetscFunctionBegin;
640   petsc_ctog_ct_scalar += 1;
641   petsc_ctog_sz_scalar += size;
642   PetscFunctionReturn(0);
643 }
644 
645 static inline PetscErrorCode PetscLogGpuToCpuScalar(PetscLogDouble size)
646 {
647   PetscFunctionBegin;
648   petsc_gtoc_ct_scalar += 1;
649   petsc_gtoc_sz_scalar += size;
650   PetscFunctionReturn(0);
651 }
652 
653 /*@C
654        PetscLogGpuFlops - Log how many flops are performed in a calculation on the device
655 
656    Input Parameter:
657 .   flops - the number of flops
658 
659    Notes:
660      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
661      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)
662 
663    Level: intermediate
664 
665 .seealso: PetscLogView(), PetscLogFlops(), PetscLogGpuTimeBegin(), PetscLogGpuTimeEnd()
666 @*/
667 static inline PetscErrorCode PetscLogGpuFlops(PetscLogDouble n)
668 {
669   PetscFunctionBegin;
670   PetscCheck(n >= 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
671   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
672   petsc_gflops += PETSC_FLOPS_PER_OP*n;
673   PetscFunctionReturn(0);
674 }
675 
676 static inline PetscErrorCode PetscLogGpuTimeAdd(PetscLogDouble t)
677 {
678   PetscFunctionBegin;
679   petsc_gtime += t;
680   PetscFunctionReturn(0);
681 }
682 
683 PETSC_EXTERN PetscErrorCode PetscLogGpuTime(void);
684 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeBegin(void);
685 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeEnd(void);
686 
687 #else
688 
689 #define PetscLogCpuToGpu(a)                0
690 #define PetscLogGpuToCpu(a)                0
691 #define PetscLogCpuToGpuScalar(a)          0
692 #define PetscLogGpuToCpuScalar(a)          0
693 #define PetscLogGpuFlops(a)                0
694 #define PetscLogGpuTimeAdd(a)              0
695 #define PetscLogGpuTimeBegin()             0
696 #define PetscLogGpuTimeEnd()               0
697 
698 #endif /* PETSC_USE_LOG && PETSC_HAVE_DEVICE */
699 
700 #define PetscPreLoadBegin(flag,name) \
701 do {\
702   PetscBool      PetscPreLoading = flag;\
703   int            PetscPreLoadMax,PetscPreLoadIt;\
704   PetscLogStage  _stageNum;\
705   PetscCall(PetscOptionsGetBool(NULL,NULL,"-preload",&PetscPreLoading,NULL));     \
706   PetscPreLoadMax = (int)(PetscPreLoading);\
707   PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
708   for (PetscPreLoadIt=0; PetscPreLoadIt<=PetscPreLoadMax; PetscPreLoadIt++) {\
709     PetscPreLoadingOn = PetscPreLoading;\
710     PetscCall(PetscBarrier(NULL));\
711     if (PetscPreLoadIt>0) PetscCall(PetscLogStageGetId(name,&_stageNum));\
712     else PetscCall(PetscLogStageRegister(name,&_stageNum));\
713     PetscCall(PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt)));\
714     PetscCall(PetscLogStagePush(_stageNum));
715 
716 #define PetscPreLoadEnd() \
717     PetscCall(PetscLogStagePop());\
718     PetscPreLoading = PETSC_FALSE;\
719   }\
720 } while (0)
721 
722 #define PetscPreLoadStage(name) do {                                                           \
723     PetscCall(PetscLogStagePop());                                                               \
724     if (PetscPreLoadIt>0)   PetscCall(PetscLogStageGetId(name,&_stageNum));                      \
725     else PetscCall(PetscLogStageRegister(name,&_stageNum));                                      \
726     PetscCall(PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt))); \
727     PetscCall(PetscLogStagePush(_stageNum));                                                     \
728   } while (0)
729 
730 /* some vars for logging */
731 PETSC_EXTERN PetscBool PetscPreLoadingUsed;       /* true if we are or have done preloading */
732 PETSC_EXTERN PetscBool PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
733 
734 #endif
735