xref: /petsc/include/petsclog.h (revision d21efd2e5d911db017a545648c4fa4838359bb2d)
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 /* 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   PetscErrorCode ierr;
393   if (type == MPI_DATATYPE_NULL) return 0;
394   ierr     = MPI_Type_size(type,&typesize);CHKERRMPI(ierr);
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   PetscErrorCode ierr;
403 
404   if (type == MPI_DATATYPE_NULL) return 0;
405   ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr);
406   ierr = MPI_Type_size(type,&typesize);CHKERRMPI(ierr);
407   for (p=0; p<size; ++p) {
408     *length += (PetscLogDouble) (counts[p]*typesize);
409   }
410   return 0;
411 }
412 
413 static inline PetscErrorCode PetscMPITypeSizeCount(PetscInt n,const PetscMPIInt *counts,MPI_Datatype type,PetscLogDouble *length)
414 {
415   PetscMPIInt    typesize,p;
416   PetscErrorCode ierr;
417 
418   if (type == MPI_DATATYPE_NULL) return 0;
419   ierr = MPI_Type_size(type,&typesize);CHKERRMPI(ierr);
420   for (p=0; p<n; ++p) {
421     *length += (PetscLogDouble) (counts[p]*typesize);
422   }
423   return 0;
424 }
425 
426 /*
427     Returns 1 if the communicator is parallel else zero
428 */
429 static inline int PetscMPIParallelComm(MPI_Comm comm)
430 {
431   PetscMPIInt size; MPI_Comm_size(comm,&size); return size > 1;
432 }
433 
434 #define MPI_Irecv(buf,count,datatype,source,tag,comm,request) \
435   ((petsc_irecv_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || MPI_Irecv((buf),(count),(datatype),(source),(tag),(comm),(request)))
436 
437 #define MPI_Irecv_c(buf,count,datatype,source,tag,comm,request) \
438   ((petsc_irecv_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || MPI_Irecv_c((buf),(count),(datatype),(source),(tag),(comm),(request)))
439 
440 #define MPI_Isend(buf,count,datatype,dest,tag,comm,request) \
441   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend((buf),(count),(datatype),(dest),(tag),(comm),(request)))
442 
443 #define MPI_Isend_c(buf,count,datatype,dest,tag,comm,request) \
444   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend_c((buf),(count),(datatype),(dest),(tag),(comm),(request)))
445 
446 #define MPI_Startall_irecv(count,datatype,number,requests) \
447   ((petsc_irecv_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || ((number) && MPI_Startall((number),(requests))))
448 
449 #define MPI_Startall_isend(count,datatype,number,requests) \
450   ((petsc_isend_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || ((number) && MPI_Startall((number),(requests))))
451 
452 #define MPI_Start_isend(count,datatype,requests) \
453   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_isend_len)) || MPI_Start((requests)))
454 
455 #define MPI_Recv(buf,count,datatype,source,tag,comm,status) \
456   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv((buf),(count),(datatype),(source),(tag),(comm),(status)))
457 
458 #define MPI_Recv_c(buf,count,datatype,source,tag,comm,status) \
459   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv_c((buf),(count),(datatype),(source),(tag),(comm),(status)))
460 
461 #define MPI_Send(buf,count,datatype,dest,tag,comm) \
462   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send((buf),(count),(datatype),(dest),(tag),(comm)))
463 
464 #define MPI_Send_c(buf,count,datatype,dest,tag,comm) \
465   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send_c((buf),(count),(datatype),(dest),(tag),(comm)))
466 
467 #define MPI_Wait(request,status) \
468   ((petsc_wait_ct++,petsc_sum_of_waits_ct++,0) || MPI_Wait((request),(status)))
469 
470 #define MPI_Waitany(a,b,c,d) \
471   ((petsc_wait_any_ct++,petsc_sum_of_waits_ct++,0) || MPI_Waitany((a),(b),(c),(d)))
472 
473 #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
474   ((petsc_wait_all_ct++,petsc_sum_of_waits_ct += (PetscLogDouble) (count),0) || MPI_Waitall((count),(array_of_requests),(array_of_statuses)))
475 
476 #define MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm) \
477   (petsc_allreduce_ct += PetscMPIParallelComm((comm)),MPI_Allreduce((sendbuf),(recvbuf),(count),(datatype),(op),(comm)))
478 
479 #define MPI_Bcast(buffer,count,datatype,root,comm) \
480   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Bcast((buffer),(count),(datatype),(root),(comm)))
481 
482 #define MPI_Reduce_scatter_block(sendbuf,recvbuf,recvcount,datatype,op,comm) \
483   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Reduce_scatter_block((sendbuf),(recvbuf),(recvcount),(datatype),(op),(comm)))
484 
485 #define MPI_Alltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
486   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Alltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
487 
488 #define MPI_Alltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
489   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(sendcnts),(sendtype),(&petsc_send_len)) || MPI_Alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))
490 
491 #define MPI_Allgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
492   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
493 
494 #define MPI_Allgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm) \
495   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm)))
496 
497 #define MPI_Gather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
498   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
499 
500 #define MPI_Gatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm) \
501   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm)))
502 
503 #define MPI_Scatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
504   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
505 
506 #define MPI_Scatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm) \
507   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
508 
509 #define MPI_Ialltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
510   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Ialltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))
511 
512 #define MPI_Ialltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
513   ((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)))
514 
515 #define MPI_Iallgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
516   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))
517 
518 #define MPI_Iallgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm,request) \
519   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm),(request)))
520 
521 #define MPI_Igather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
522   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
523 
524 #define MPI_Igatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm,request) \
525   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm),(request)))
526 
527 #define MPI_Iscatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
528   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
529 
530 #define MPI_Iscatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
531   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))
532 
533 #else
534 
535 #define MPI_Startall_irecv(count,datatype,number,requests) \
536   ((number) && MPI_Startall((number),(requests)))
537 
538 #define MPI_Startall_isend(count,datatype,number,requests) \
539   ((number) && MPI_Startall((number),(requests)))
540 
541 #define MPI_Start_isend(count,datatype,requests) \
542   (MPI_Start((requests)))
543 
544 #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
545 
546 #else  /* ---Logging is turned off --------------------------------------------*/
547 
548 #define PetscLogMemory                     PETSC_FALSE
549 
550 #define PetscLogFlops(n)                   0
551 #define PetscGetFlops(a)                   (*(a) = 0.0,0)
552 
553 #define PetscLogStageRegister(a,b)         0
554 #define PetscLogStagePush(a)               0
555 #define PetscLogStagePop()                 0
556 #define PetscLogStageSetActive(a,b)        0
557 #define PetscLogStageGetActive(a,b)        0
558 #define PetscLogStageGetVisible(a,b)       0
559 #define PetscLogStageSetVisible(a,b)       0
560 #define PetscLogStageGetId(a,b)            (*(b)=0,0)
561 
562 #define PetscLogEventRegister(a,b,c)       0
563 #define PetscLogEventSetCollective(a,b)    0
564 #define PetscLogEventIncludeClass(a)       0
565 #define PetscLogEventExcludeClass(a)       0
566 #define PetscLogEventActivate(a)           0
567 #define PetscLogEventDeactivate(a)         0
568 #define PetscLogEventDeactivatePush(a)     0
569 #define PetscLogEventDeactivatePop(a)      0
570 #define PetscLogEventActivateClass(a)      0
571 #define PetscLogEventDeactivateClass(a)    0
572 #define PetscLogEventSetActiveAll(a,b)     0
573 #define PetscLogEventGetId(a,b)            (*(b)=0,0)
574 #define PetscLogEventGetPerfInfo(a,b,c)    0
575 #define PetscLogEventSetDof(a,b,c)         0
576 #define PetscLogEventSetError(a,b,c)       0
577 
578 #define PetscLogPLB                        0
579 #define PetscLogPLE                        0
580 #define PetscLogPHC                        0
581 #define PetscLogPHD                        0
582 
583 #define PetscLogObjectParents(p,n,c)       0
584 #define PetscLogObjectCreate(h)            0
585 #define PetscLogObjectDestroy(h)           0
586 PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject,const char[],...) PETSC_ATTRIBUTE_FORMAT(2,3);
587 
588 #define PetscLogDefaultBegin()             0
589 #define PetscLogAllBegin()                 0
590 #define PetscLogNestedBegin()              0
591 #define PetscLogTraceBegin(file)           0
592 #define PetscLogActions(a)                 0
593 #define PetscLogObjects(a)                 0
594 #define PetscLogSetThreshold(a,b)          0
595 #define PetscLogSet(lb,le)                 0
596 #define PetscLogIsActive(flag)             0
597 
598 #define PetscLogView(viewer)               0
599 #define PetscLogViewFromOptions()          0
600 #define PetscLogDump(c)                    0
601 
602 #define PetscLogEventSync(e,comm)          0
603 #define PetscLogEventBegin(e,o1,o2,o3,o4)  0
604 #define PetscLogEventEnd(e,o1,o2,o3,o4)    0
605 
606 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
607 #define MPI_Startall_irecv(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
608 #define MPI_Startall_isend(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
609 #define MPI_Start_isend(count,datatype,requests)           MPI_Start(requests)
610 
611 #endif   /* PETSC_USE_LOG */
612 
613 #if defined (PETSC_USE_LOG) && defined(PETSC_HAVE_DEVICE)
614 
615 /* Global GPU counters */
616 PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
617 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
618 PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
619 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
620 PETSC_EXTERN PetscLogDouble petsc_ctog_ct_scalar;
621 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct_scalar;
622 PETSC_EXTERN PetscLogDouble petsc_ctog_sz_scalar;
623 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz_scalar;
624 PETSC_EXTERN PetscLogDouble petsc_gflops;
625 PETSC_EXTERN PetscLogDouble petsc_gtime;
626 
627 static inline PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size)
628 {
629   PetscFunctionBegin;
630   petsc_ctog_ct += 1;
631   petsc_ctog_sz += size;
632   PetscFunctionReturn(0);
633 }
634 
635 static inline PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size)
636 {
637   PetscFunctionBegin;
638   petsc_gtoc_ct += 1;
639   petsc_gtoc_sz += size;
640   PetscFunctionReturn(0);
641 }
642 
643 static inline PetscErrorCode PetscLogCpuToGpuScalar(PetscLogDouble size)
644 {
645   PetscFunctionBegin;
646   petsc_ctog_ct_scalar += 1;
647   petsc_ctog_sz_scalar += size;
648   PetscFunctionReturn(0);
649 }
650 
651 static inline PetscErrorCode PetscLogGpuToCpuScalar(PetscLogDouble size)
652 {
653   PetscFunctionBegin;
654   petsc_gtoc_ct_scalar += 1;
655   petsc_gtoc_sz_scalar += size;
656   PetscFunctionReturn(0);
657 }
658 
659 /*@C
660        PetscLogGpuFlops - Log how many flops are performed in a calculation on the device
661 
662    Input Parameter:
663 .   flops - the number of flops
664 
665    Notes:
666      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
667      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)
668 
669    Level: intermediate
670 
671 .seealso: PetscLogView(), PetscLogFlops(), PetscLogGpuTimeBegin(), PetscLogGpuTimeEnd()
672 @*/
673 static inline PetscErrorCode PetscLogGpuFlops(PetscLogDouble n)
674 {
675   PetscFunctionBegin;
676   PetscCheck(n >= 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
677   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
678   petsc_gflops += PETSC_FLOPS_PER_OP*n;
679   PetscFunctionReturn(0);
680 }
681 
682 static inline PetscErrorCode PetscLogGpuTimeAdd(PetscLogDouble t)
683 {
684   PetscFunctionBegin;
685   petsc_gtime += t;
686   PetscFunctionReturn(0);
687 }
688 
689 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeBegin(void);
690 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeEnd(void);
691 
692 #else
693 
694 #define PetscLogCpuToGpu(a)                0
695 #define PetscLogGpuToCpu(a)                0
696 #define PetscLogCpuToGpuScalar(a)          0
697 #define PetscLogGpuToCpuScalar(a)          0
698 #define PetscLogGpuFlops(a)                0
699 #define PetscLogGpuTimeAdd(a)              0
700 #define PetscLogGpuTimeBegin()             0
701 #define PetscLogGpuTimeEnd()               0
702 
703 #endif /* PETSC_USE_LOG && PETSC_HAVE_DEVICE */
704 
705 #define PetscPreLoadBegin(flag,name) \
706 do {\
707   PetscBool      PetscPreLoading = flag;\
708   int            PetscPreLoadMax,PetscPreLoadIt;\
709   PetscLogStage  _stageNum;\
710   PetscErrorCode _3_ierr; \
711   _3_ierr = PetscOptionsGetBool(NULL,NULL,"-preload",&PetscPreLoading,NULL);CHKERRQ(_3_ierr); \
712   PetscPreLoadMax = (int)(PetscPreLoading);\
713   PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
714   for (PetscPreLoadIt=0; PetscPreLoadIt<=PetscPreLoadMax; PetscPreLoadIt++) {\
715     PetscPreLoadingOn = PetscPreLoading;\
716     _3_ierr = PetscBarrier(NULL);CHKERRQ(_3_ierr);\
717     if (PetscPreLoadIt>0) {\
718       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
719     } else {\
720       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
721     }\
722     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt));\
723     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
724 
725 #define PetscPreLoadEnd() \
726     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
727     PetscPreLoading = PETSC_FALSE;\
728   }\
729 } while (0)
730 
731 #define PetscPreLoadStage(name) do {                                         \
732     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);                      \
733     if (PetscPreLoadIt>0) {                                                  \
734       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);   \
735     } else {                                                            \
736       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
737     }                                                                   \
738     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt)); \
739     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);            \
740   } while (0)
741 
742 /* some vars for logging */
743 PETSC_EXTERN PetscBool PetscPreLoadingUsed;       /* true if we are or have done preloading */
744 PETSC_EXTERN PetscBool PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
745 
746 #endif
747