xref: /petsc/include/petsclog.h (revision e600fa544e2bb197ca2af9b6e65ea465976dec56)
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)  0;do{int _i; for (_i=0; _i<(n); _i++) {ierr = PetscLogObjectParent((PetscObject)(p),(PetscObject)(d)[_i]);CHKERRQ(ierr);}}while (0)
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 /* Stage functions */
306 PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[],PetscLogStage*);
307 PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
308 PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
309 PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage,PetscBool);
310 PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage,PetscBool*);
311 PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage,PetscBool);
312 PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage,PetscBool*);
313 PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char[],PetscLogStage*);
314 
315 /* Event functions */
316 PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[],PetscClassId,PetscLogEvent*);
317 PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent,PetscBool);
318 PETSC_EXTERN PetscErrorCode PetscLogEventIncludeClass(PetscClassId);
319 PETSC_EXTERN PetscErrorCode PetscLogEventExcludeClass(PetscClassId);
320 PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
321 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(PetscLogEvent);
322 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent);
323 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent);
324 PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent,PetscBool);
325 PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
326 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
327 PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[],PetscLogEvent*);
328 PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(int,PetscLogEvent,PetscEventPerfInfo*);
329 PETSC_EXTERN PetscErrorCode PetscLogEventSetDof(PetscLogEvent, PetscInt, PetscLogDouble);
330 PETSC_EXTERN PetscErrorCode PetscLogEventSetError(PetscLogEvent, PetscInt, PetscLogDouble);
331 
332 /* Global counters */
333 PETSC_EXTERN PetscLogDouble petsc_irecv_ct;
334 PETSC_EXTERN PetscLogDouble petsc_isend_ct;
335 PETSC_EXTERN PetscLogDouble petsc_recv_ct;
336 PETSC_EXTERN PetscLogDouble petsc_send_ct;
337 PETSC_EXTERN PetscLogDouble petsc_irecv_len;
338 PETSC_EXTERN PetscLogDouble petsc_isend_len;
339 PETSC_EXTERN PetscLogDouble petsc_recv_len;
340 PETSC_EXTERN PetscLogDouble petsc_send_len;
341 PETSC_EXTERN PetscLogDouble petsc_allreduce_ct;
342 PETSC_EXTERN PetscLogDouble petsc_gather_ct;
343 PETSC_EXTERN PetscLogDouble petsc_scatter_ct;
344 PETSC_EXTERN PetscLogDouble petsc_wait_ct;
345 PETSC_EXTERN PetscLogDouble petsc_wait_any_ct;
346 PETSC_EXTERN PetscLogDouble petsc_wait_all_ct;
347 PETSC_EXTERN PetscLogDouble petsc_sum_of_waits_ct;
348 
349 PETSC_EXTERN PetscBool      PetscLogMemory;
350 
351 PETSC_EXTERN PetscBool PetscLogSyncOn;  /* true if logging synchronization is enabled */
352 PETSC_EXTERN PetscErrorCode PetscLogEventSynchronize(PetscLogEvent, MPI_Comm);
353 
354 #define PetscLogEventSync(e,comm) \
355   (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
356     PetscLogEventSynchronize((e),(comm)) : 0))
357 
358 #define PetscLogEventBegin(e,o1,o2,o3,o4) \
359   (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
360     (*PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0))
361 
362 #define PetscLogEventEnd(e,o1,o2,o3,o4) \
363   (((PetscLogPLE && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
364     (*PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0))
365 
366 PETSC_EXTERN PetscErrorCode PetscLogEventGetFlops(PetscLogEvent,PetscLogDouble*);
367 PETSC_EXTERN PetscErrorCode PetscLogEventZeroFlops(PetscLogEvent);
368 
369 /*
370      These are used internally in the PETSc routines to keep a count of MPI messages and
371    their sizes.
372 
373      This does not work for MPI-Uni because our include/petsc/mpiuni/mpi.h file
374    uses macros to defined the MPI operations.
375 
376      It does not work correctly from HP-UX because it processes the
377    macros in a way that sometimes it double counts, hence
378    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
379 
380      It does not work with Windows because winmpich lacks MPI_Type_size()
381 */
382 #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
383 /*
384    Logging of MPI activities
385 */
386 static inline PetscErrorCode PetscMPITypeSize(PetscInt count,MPI_Datatype type,PetscLogDouble *length)
387 {
388   PetscMPIInt    typesize;
389   PetscErrorCode ierr;
390   if (type == MPI_DATATYPE_NULL) return 0;
391   ierr     = MPI_Type_size(type,&typesize);CHKERRMPI(ierr);
392   *length += (PetscLogDouble) (count*typesize);
393   return 0;
394 }
395 
396 static inline PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm,const PetscMPIInt *counts,MPI_Datatype type,PetscLogDouble *length)
397 {
398   PetscMPIInt    typesize,size,p;
399   PetscErrorCode ierr;
400 
401   if (type == MPI_DATATYPE_NULL) return 0;
402   ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr);
403   ierr = MPI_Type_size(type,&typesize);CHKERRMPI(ierr);
404   for (p=0; p<size; ++p) {
405     *length += (PetscLogDouble) (counts[p]*typesize);
406   }
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   PetscErrorCode ierr;
414 
415   if (type == MPI_DATATYPE_NULL) return 0;
416   ierr = MPI_Type_size(type,&typesize);CHKERRMPI(ierr);
417   for (p=0; p<n; ++p) {
418     *length += (PetscLogDouble) (counts[p]*typesize);
419   }
420   return 0;
421 }
422 
423 /*
424     Returns 1 if the communicator is parallel else zero
425 */
426 static inline int PetscMPIParallelComm(MPI_Comm comm)
427 {
428   PetscMPIInt size; MPI_Comm_size(comm,&size); return size > 1;
429 }
430 
431 #define MPI_Irecv(buf,count,datatype,source,tag,comm,request) \
432   ((petsc_irecv_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || MPI_Irecv((buf),(count),(datatype),(source),(tag),(comm),(request)))
433 
434 #define MPI_Irecv_c(buf,count,datatype,source,tag,comm,request) \
435   ((petsc_irecv_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || MPI_Irecv_c((buf),(count),(datatype),(source),(tag),(comm),(request)))
436 
437 #define MPI_Isend(buf,count,datatype,dest,tag,comm,request) \
438   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend((buf),(count),(datatype),(dest),(tag),(comm),(request)))
439 
440 #define MPI_Isend_c(buf,count,datatype,dest,tag,comm,request) \
441   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend_c((buf),(count),(datatype),(dest),(tag),(comm),(request)))
442 
443 #define MPI_Startall_irecv(count,datatype,number,requests) \
444   ((petsc_irecv_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || ((number) && MPI_Startall((number),(requests))))
445 
446 #define MPI_Startall_isend(count,datatype,number,requests) \
447   ((petsc_isend_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || ((number) && MPI_Startall((number),(requests))))
448 
449 #define MPI_Start_isend(count,datatype,requests) \
450   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_isend_len)) || MPI_Start((requests)))
451 
452 #define MPI_Recv(buf,count,datatype,source,tag,comm,status) \
453   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv((buf),(count),(datatype),(source),(tag),(comm),(status)))
454 
455 #define MPI_Recv_c(buf,count,datatype,source,tag,comm,status) \
456   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv_c((buf),(count),(datatype),(source),(tag),(comm),(status)))
457 
458 #define MPI_Send(buf,count,datatype,dest,tag,comm) \
459   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send((buf),(count),(datatype),(dest),(tag),(comm)))
460 
461 #define MPI_Send_c(buf,count,datatype,dest,tag,comm) \
462   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send_c((buf),(count),(datatype),(dest),(tag),(comm)))
463 
464 #define MPI_Wait(request,status) \
465   ((petsc_wait_ct++,petsc_sum_of_waits_ct++,0) || MPI_Wait((request),(status)))
466 
467 #define MPI_Waitany(a,b,c,d) \
468   ((petsc_wait_any_ct++,petsc_sum_of_waits_ct++,0) || MPI_Waitany((a),(b),(c),(d)))
469 
470 #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
471   ((petsc_wait_all_ct++,petsc_sum_of_waits_ct += (PetscLogDouble) (count),0) || MPI_Waitall((count),(array_of_requests),(array_of_statuses)))
472 
473 #define MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm) \
474   (petsc_allreduce_ct += PetscMPIParallelComm((comm)),MPI_Allreduce((sendbuf),(recvbuf),(count),(datatype),(op),(comm)))
475 
476 #define MPI_Bcast(buffer,count,datatype,root,comm) \
477   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Bcast((buffer),(count),(datatype),(root),(comm)))
478 
479 #define MPI_Reduce_scatter_block(sendbuf,recvbuf,recvcount,datatype,op,comm) \
480   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Reduce_scatter_block((sendbuf),(recvbuf),(recvcount),(datatype),(op),(comm)))
481 
482 #define MPI_Alltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
483   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Alltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
484 
485 #define MPI_Alltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
486   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(sendcnts),(sendtype),(&petsc_send_len)) || MPI_Alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))
487 
488 #define MPI_Allgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
489   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
490 
491 #define MPI_Allgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm) \
492   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm)))
493 
494 #define MPI_Gather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
495   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
496 
497 #define MPI_Gatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm) \
498   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm)))
499 
500 #define MPI_Scatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
501   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
502 
503 #define MPI_Scatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm) \
504   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
505 
506 #define MPI_Ialltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
507   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Ialltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))
508 
509 #define MPI_Ialltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
510   ((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)))
511 
512 #define MPI_Iallgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
513   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))
514 
515 #define MPI_Iallgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm,request) \
516   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm),(request)))
517 
518 #define MPI_Igather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
519   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
520 
521 #define MPI_Igatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm,request) \
522   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm),(request)))
523 
524 #define MPI_Iscatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
525   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
526 
527 #define MPI_Iscatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
528   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
529 
530 #else
531 
532 #define MPI_Startall_irecv(count,datatype,number,requests) \
533   ((number) && MPI_Startall((number),(requests)))
534 
535 #define MPI_Startall_isend(count,datatype,number,requests) \
536   ((number) && MPI_Startall((number),(requests)))
537 
538 #define MPI_Start_isend(count,datatype,requests) \
539   (MPI_Start((requests)))
540 
541 #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
542 
543 #else  /* ---Logging is turned off --------------------------------------------*/
544 
545 #define PetscLogMemory                     PETSC_FALSE
546 
547 #define PetscLogFlops(n)                   0
548 #define PetscGetFlops(a)                   (*(a) = 0.0,0)
549 
550 #define PetscLogStageRegister(a,b)         0
551 #define PetscLogStagePush(a)               0
552 #define PetscLogStagePop()                 0
553 #define PetscLogStageSetActive(a,b)        0
554 #define PetscLogStageGetActive(a,b)        0
555 #define PetscLogStageGetVisible(a,b)       0
556 #define PetscLogStageSetVisible(a,b)       0
557 #define PetscLogStageGetId(a,b)            (*(b)=0,0)
558 
559 #define PetscLogEventRegister(a,b,c)       0
560 #define PetscLogEventSetCollective(a,b)    0
561 #define PetscLogEventIncludeClass(a)       0
562 #define PetscLogEventExcludeClass(a)       0
563 #define PetscLogEventActivate(a)           0
564 #define PetscLogEventDeactivate(a)         0
565 #define PetscLogEventDeactivatePush(a)     0
566 #define PetscLogEventDeactivatePop(a)      0
567 #define PetscLogEventActivateClass(a)      0
568 #define PetscLogEventDeactivateClass(a)    0
569 #define PetscLogEventSetActiveAll(a,b)     0
570 #define PetscLogEventGetId(a,b)            (*(b)=0,0)
571 #define PetscLogEventGetPerfInfo(a,b,c)    0
572 #define PetscLogEventSetDof(a,b,c)         0
573 #define PetscLogEventSetError(a,b,c)       0
574 
575 #define PetscLogPLB                        0
576 #define PetscLogPLE                        0
577 #define PetscLogPHC                        0
578 #define PetscLogPHD                        0
579 
580 #define PetscLogObjectParents(p,n,c)       0
581 #define PetscLogObjectCreate(h)            0
582 #define PetscLogObjectDestroy(h)           0
583 PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject,const char[],...) PETSC_ATTRIBUTE_FORMAT(2,3);
584 
585 #define PetscLogDefaultBegin()             0
586 #define PetscLogAllBegin()                 0
587 #define PetscLogNestedBegin()              0
588 #define PetscLogTraceBegin(file)           0
589 #define PetscLogActions(a)                 0
590 #define PetscLogObjects(a)                 0
591 #define PetscLogSetThreshold(a,b)          0
592 #define PetscLogSet(lb,le)                 0
593 
594 #define PetscLogView(viewer)               0
595 #define PetscLogViewFromOptions()          0
596 #define PetscLogDump(c)                    0
597 
598 #define PetscLogEventSync(e,comm)          0
599 #define PetscLogEventBegin(e,o1,o2,o3,o4)  0
600 #define PetscLogEventEnd(e,o1,o2,o3,o4)    0
601 
602 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
603 #define MPI_Startall_irecv(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
604 #define MPI_Startall_isend(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
605 #define MPI_Start_isend(count,datatype,requests)           MPI_Start(requests)
606 
607 #endif   /* PETSC_USE_LOG */
608 
609 #if defined (PETSC_USE_LOG) && defined(PETSC_HAVE_DEVICE)
610 
611 /* Global GPU counters */
612 PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
613 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
614 PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
615 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
616 PETSC_EXTERN PetscLogDouble petsc_ctog_ct_scalar;
617 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct_scalar;
618 PETSC_EXTERN PetscLogDouble petsc_ctog_sz_scalar;
619 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz_scalar;
620 PETSC_EXTERN PetscLogDouble petsc_gflops;
621 PETSC_EXTERN PetscLogDouble petsc_gtime;
622 
623 static inline PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size)
624 {
625   PetscFunctionBegin;
626   petsc_ctog_ct += 1;
627   petsc_ctog_sz += size;
628   PetscFunctionReturn(0);
629 }
630 
631 static inline PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size)
632 {
633   PetscFunctionBegin;
634   petsc_gtoc_ct += 1;
635   petsc_gtoc_sz += size;
636   PetscFunctionReturn(0);
637 }
638 
639 static inline PetscErrorCode PetscLogCpuToGpuScalar(PetscLogDouble size)
640 {
641   PetscFunctionBegin;
642   petsc_ctog_ct_scalar += 1;
643   petsc_ctog_sz_scalar += size;
644   PetscFunctionReturn(0);
645 }
646 
647 static inline PetscErrorCode PetscLogGpuToCpuScalar(PetscLogDouble size)
648 {
649   PetscFunctionBegin;
650   petsc_gtoc_ct_scalar += 1;
651   petsc_gtoc_sz_scalar += size;
652   PetscFunctionReturn(0);
653 }
654 
655 /*@C
656        PetscLogGpuFlops - Log how many flops are performed in a calculation on the device
657 
658    Input Parameter:
659 .   flops - the number of flops
660 
661    Notes:
662      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
663      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)
664 
665    Level: intermediate
666 
667 .seealso: PetscLogView(), PetscLogFlops(), PetscLogGpuTimeBegin(), PetscLogGpuTimeEnd()
668 @*/
669 static inline PetscErrorCode PetscLogGpuFlops(PetscLogDouble n)
670 {
671   PetscFunctionBegin;
672   PetscCheck(n >= 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
673   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
674   petsc_gflops += PETSC_FLOPS_PER_OP*n;
675   PetscFunctionReturn(0);
676 }
677 
678 static inline PetscErrorCode PetscLogGpuTimeAdd(PetscLogDouble t)
679 {
680   PetscFunctionBegin;
681   petsc_gtime += t;
682   PetscFunctionReturn(0);
683 }
684 
685 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeBegin(void);
686 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeEnd(void);
687 
688 #else
689 
690 #define PetscLogCpuToGpu(a)                0
691 #define PetscLogGpuToCpu(a)                0
692 #define PetscLogCpuToGpuScalar(a)          0
693 #define PetscLogGpuToCpuScalar(a)          0
694 #define PetscLogGpuFlops(a)                0
695 #define PetscLogGpuTimeAdd(a)              0
696 #define PetscLogGpuTimeBegin()             0
697 #define PetscLogGpuTimeEnd()               0
698 
699 #endif /* PETSC_USE_LOG && PETSC_HAVE_DEVICE */
700 
701 #define PetscPreLoadBegin(flag,name) \
702 do {\
703   PetscBool      PetscPreLoading = flag;\
704   int            PetscPreLoadMax,PetscPreLoadIt;\
705   PetscLogStage  _stageNum;\
706   PetscErrorCode _3_ierr; \
707   _3_ierr = PetscOptionsGetBool(NULL,NULL,"-preload",&PetscPreLoading,NULL);CHKERRQ(_3_ierr); \
708   PetscPreLoadMax = (int)(PetscPreLoading);\
709   PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
710   for (PetscPreLoadIt=0; PetscPreLoadIt<=PetscPreLoadMax; PetscPreLoadIt++) {\
711     PetscPreLoadingOn = PetscPreLoading;\
712     _3_ierr = PetscBarrier(NULL);CHKERRQ(_3_ierr);\
713     if (PetscPreLoadIt>0) {\
714       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
715     } else {\
716       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
717     }\
718     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt));\
719     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
720 
721 #define PetscPreLoadEnd() \
722     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
723     PetscPreLoading = PETSC_FALSE;\
724   }\
725 } while (0)
726 
727 #define PetscPreLoadStage(name) do {                                         \
728     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);                      \
729     if (PetscPreLoadIt>0) {                                                  \
730       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);   \
731     } else {                                                            \
732       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
733     }                                                                   \
734     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt)); \
735     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);            \
736   } while (0)
737 
738 /* some vars for logging */
739 PETSC_EXTERN PetscBool PetscPreLoadingUsed;       /* true if we are or have done preloading */
740 PETSC_EXTERN PetscBool PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
741 
742 #endif
743