xref: /petsc/include/petsclog.h (revision 534a8f05a7a8aff70dd8cfd53d9cd834400a8dbf)
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[],void*,const char[],...);
12 #if defined(PETSC_USE_INFO)
13 #define PetscInfo(A,S)                       PetscInfo_Private(PETSC_FUNCTION_NAME,A,S)
14 #define PetscInfo1(A,S,a1)                   PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1)
15 #define PetscInfo2(A,S,a1,a2)                PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2)
16 #define PetscInfo3(A,S,a1,a2,a3)             PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3)
17 #define PetscInfo4(A,S,a1,a2,a3,a4)          PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4)
18 #define PetscInfo5(A,S,a1,a2,a3,a4,a5)       PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5)
19 #define PetscInfo6(A,S,a1,a2,a3,a4,a5,a6)    PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5,a6)
20 #define PetscInfo7(A,S,a1,a2,a3,a4,a5,a6,a7) PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5,a6,a7)
21 #else
22 #define PetscInfo(A,S)                       0
23 #define PetscInfo1(A,S,a1)                   0
24 #define PetscInfo2(A,S,a1,a2)                0
25 #define PetscInfo3(A,S,a1,a2,a3)             0
26 #define PetscInfo4(A,S,a1,a2,a3,a4)          0
27 #define PetscInfo5(A,S,a1,a2,a3,a4,a5)       0
28 #define PetscInfo6(A,S,a1,a2,a3,a4,a5,a6)    0
29 #define PetscInfo7(A,S,a1,a2,a3,a4,a5,a6,a7) 0
30 #endif
31 PETSC_EXTERN PetscErrorCode PetscInfoDeactivateClass(PetscClassId);
32 PETSC_EXTERN PetscErrorCode PetscInfoActivateClass(PetscClassId);
33 PETSC_EXTERN PetscBool PetscLogPrintInfo;  /* if true, indicates PetscInfo() is turned on */
34 
35 /*MC
36     PetscLogEvent - id used to identify PETSc or user events which timed portions (blocks of executable)
37      code.
38 
39     Level: intermediate
40 
41 .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscLogStage
42 M*/
43 typedef int PetscLogEvent;
44 
45 /*MC
46     PetscLogStage - id used to identify user stages (phases, sections) of runs - for logging
47 
48     Level: intermediate
49 
50 .seealso: PetscLogStageRegister(), PetscLogStagePush(), PetscLogStagePop(), PetscLogEvent
51 M*/
52 typedef int PetscLogStage;
53 
54 #define PETSC_EVENT  1311311
55 PETSC_EXTERN PetscLogEvent PETSC_LARGEST_EVENT;
56 
57 /* Global flop counter */
58 PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
59 PETSC_EXTERN PetscLogDouble petsc_tmp_flops;
60 
61 /* Global GPU counters */
62 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
63 PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
64 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
65 PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
66 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
67 PETSC_EXTERN PetscLogDouble petsc_gflops;
68 PETSC_EXTERN PetscLogDouble petsc_gtime;
69 #endif
70 
71 /* We must make the following structures available to access the event
72      activation flags in the PetscLogEventBegin/End() macros. These are not part of the PETSc public
73      API and are not intended to be used by other parts of PETSc or by users.
74 
75      The code that manipulates these structures is in src/sys/logging/utils.
76 */
77 typedef struct _n_PetscIntStack *PetscIntStack;
78 
79 /* -----------------------------------------------------------------------------------------------------*/
80 /*
81     PetscClassRegInfo, PetscClassPerfInfo - Each class has two data structures associated with it. The first has
82        static information about it, the second collects statistics on how many objects of the class are created,
83        how much memory they use, etc.
84 
85     PetscClassRegLog, PetscClassPerfLog - arrays of the PetscClassRegInfo and PetscClassPerfInfo for all classes.
86 */
87 typedef struct  {
88   char           *name;   /* The class name */
89   PetscClassId   classid; /* The integer identifying this class */
90 } PetscClassRegInfo;
91 
92 typedef struct {
93   PetscClassId   id;           /* The integer identifying this class */
94   int            creations;    /* The number of objects of this class created */
95   int            destructions; /* The number of objects of this class destroyed */
96   PetscLogDouble mem;          /* The total memory allocated by objects of this class */
97   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects */
98 } PetscClassPerfInfo;
99 
100 typedef struct _n_PetscClassRegLog *PetscClassRegLog;
101 struct _n_PetscClassRegLog {
102   int               numClasses; /* The number of classes registered */
103   int               maxClasses; /* The maximum number of classes */
104   PetscClassRegInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
105 };
106 
107 typedef struct _n_PetscClassPerfLog *PetscClassPerfLog;
108 struct _n_PetscClassPerfLog {
109   int                numClasses; /* The number of logging classes */
110   int                maxClasses; /* The maximum number of classes */
111   PetscClassPerfInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
112 };
113 /* -----------------------------------------------------------------------------------------------------*/
114 /*
115     PetscEventRegInfo, PetscEventPerfInfo - Each event has two data structures associated with it. The first has
116        static information about it, the second collects statistics on how many times the event is used, how
117        much time it takes, etc.
118 
119     PetscEventRegLog, PetscEventPerfLog - an array of all PetscEventRegInfo and PetscEventPerfInfo for all events. There is one
120       of these for each stage.
121 
122 */
123 typedef struct {
124   char         *name;         /* The name of this event */
125   PetscClassId classid;       /* The class the event is associated with */
126   PetscBool    collective;    /* Flag this event as collective */
127 #if defined (PETSC_HAVE_MPE)
128   int          mpe_id_begin;  /* MPE IDs that define the event */
129   int          mpe_id_end;
130 #endif
131 } PetscEventRegInfo;
132 
133 typedef struct {
134   int            id;            /* The integer identifying this event */
135   PetscBool      active;        /* The flag to activate logging */
136   PetscBool      visible;       /* The flag to print info in summary */
137   int            depth;         /* The nesting depth of the event call */
138   int            count;         /* The number of times this event was executed */
139   PetscLogDouble flops, flops2, flopsTmp; /* The flops and flops^2 used in this event */
140   PetscLogDouble time, time2, timeTmp;    /* The time and time^2 taken for this event */
141   PetscLogDouble syncTime;                /* The synchronization barrier time */
142   PetscLogDouble dof[8];        /* The number of degrees of freedom associated with this event */
143   PetscLogDouble errors[8];     /* The errors (user-defined) associated with this event */
144   PetscLogDouble numMessages;   /* The number of messages in this event */
145   PetscLogDouble messageLength; /* The total message lengths in this event */
146   PetscLogDouble numReductions; /* The number of reductions in this event */
147   PetscLogDouble memIncrease;   /* How much the resident memory has increased in this event */
148   PetscLogDouble mallocIncrease;/* How much the maximum malloced space has increased in this event */
149   PetscLogDouble mallocSpace;   /* How much the space was malloced and kept during this event */
150   PetscLogDouble mallocIncreaseEvent;  /* Maximum of the high water mark with in event minus memory available at the end of the event */
151   #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
152   PetscLogDouble CpuToGpuCount; /* The total number of CPU to GPU copies */
153   PetscLogDouble GpuToCpuCount; /* The total number of GPU to CPU copies */
154   PetscLogDouble CpuToGpuSize;  /* The total size of CPU to GPU copies */
155   PetscLogDouble GpuToCpuSize;  /* The total size of GPU to CPU copies */
156   PetscLogDouble GpuFlops;      /* The flops done on a GPU in this event */
157   PetscLogDouble GpuTime;       /* The time spent on a GPU in this event */
158   #endif
159 } PetscEventPerfInfo;
160 
161 typedef struct _n_PetscEventRegLog *PetscEventRegLog;
162 struct _n_PetscEventRegLog {
163   int               numEvents;  /* The number of registered events */
164   int               maxEvents;  /* The maximum number of events */
165   PetscEventRegInfo *eventInfo; /* The registration information for each event */
166 };
167 
168 typedef struct _n_PetscEventPerfLog *PetscEventPerfLog;
169 struct _n_PetscEventPerfLog {
170   int                numEvents;  /* The number of logging events */
171   int                maxEvents;  /* The maximum number of events */
172   PetscEventPerfInfo *eventInfo; /* The performance information for each event */
173 };
174 /* ------------------------------------------------------------------------------------------------------------*/
175 /*
176    PetscStageInfo - Contains all the information about a particular stage.
177 
178    PetscStageLog - An array of PetscStageInfo for each registered stage. There is a single one of these in the code.
179 */
180 typedef struct _PetscStageInfo {
181   char               *name;     /* The stage name */
182   PetscBool          used;      /* The stage was pushed on this processor */
183   PetscEventPerfInfo perfInfo;  /* The stage performance information */
184   PetscEventPerfLog  eventLog;  /* The event information for this stage */
185   PetscClassPerfLog  classLog;  /* The class information for this stage */
186 } PetscStageInfo;
187 
188 typedef struct _n_PetscStageLog *PetscStageLog;
189 struct _n_PetscStageLog {
190   int              numStages;   /* The number of registered stages */
191   int              maxStages;   /* The maximum number of stages */
192   PetscIntStack    stack;       /* The stack for active stages */
193   int              curStage;    /* The current stage (only used in macros so we don't call PetscIntStackTop) */
194   PetscStageInfo   *stageInfo;  /* The information for each stage */
195   PetscEventRegLog eventLog;    /* The registered events */
196   PetscClassRegLog classLog;    /* The registered classes */
197 };
198 /* -----------------------------------------------------------------------------------------------------*/
199 
200 PETSC_EXTERN PetscErrorCode PetscLogObjectParent(PetscObject,PetscObject);
201 PETSC_EXTERN PetscErrorCode PetscLogObjectMemory(PetscObject,PetscLogDouble);
202 
203 #if defined(PETSC_USE_LOG)  /* --- Logging is turned on --------------------------------*/
204 PETSC_EXTERN PetscStageLog petsc_stageLog;
205 PETSC_EXTERN PetscErrorCode PetscLogGetStageLog(PetscStageLog*);
206 PETSC_EXTERN PetscErrorCode PetscStageLogGetCurrent(PetscStageLog,int*);
207 PETSC_EXTERN PetscErrorCode PetscStageLogGetEventPerfLog(PetscStageLog,int,PetscEventPerfLog*);
208 
209 /*
210    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
211 
212    For the complex numbers version, note that
213        1 complex addition = 2 flops
214        1 complex multiplication = 6 flops,
215    where we define 1 flop as that for a double precision scalar.  We roughly approximate
216    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
217    to the assumption that we're counting mostly additions and multiplications -- and
218    roughly the same number of each.  More accurate counting could be done by distinguishing
219    among the various arithmetic operations.
220  */
221 
222 #if defined(PETSC_USE_COMPLEX)
223 #define PETSC_FLOPS_PER_OP 4.0
224 #else
225 #define PETSC_FLOPS_PER_OP 1.0
226 #endif
227 
228 PETSC_STATIC_INLINE PetscErrorCode PetscLogFlops(PetscLogDouble n)
229 {
230   PetscFunctionBegin;
231 #if defined(PETSC_USE_DEBUG)
232   if (n < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
233 #endif
234   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
235   PetscFunctionReturn(0);
236 }
237 
238 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
239 PETSC_STATIC_INLINE PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size){
240   PetscFunctionBegin;
241   petsc_ctog_ct += 1;
242   petsc_ctog_sz += size;
243   PetscFunctionReturn(0);
244 }
245 PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size){
246   PetscFunctionBegin;
247   petsc_gtoc_ct += 1;
248   petsc_gtoc_sz += size;
249   PetscFunctionReturn(0);
250 }
251 PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuFlops(PetscLogDouble n){
252   PetscFunctionBegin;
253 #if defined(PETSC_USE_DEBUG)
254   if (n < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
255 #endif
256   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
257   petsc_gflops += PETSC_FLOPS_PER_OP*n;
258   PetscFunctionReturn(0);
259 }
260 PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuTimeBegin(){
261   PetscErrorCode ierr;
262   PetscFunctionBegin;
263   ierr = PetscTimeSubtract(&petsc_gtime);CHKERRQ(ierr);
264   PetscFunctionReturn(0);
265 }
266 PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuTimeEnd(){
267   PetscErrorCode ierr;
268   PetscFunctionBegin;
269   ierr = PetscTimeAdd(&petsc_gtime);CHKERRQ(ierr);
270   PetscFunctionReturn(0);
271 }
272 #endif
273 
274 PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);
275 
276 #if defined (PETSC_HAVE_MPE)
277 PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
278 PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
279 #endif
280 
281 PETSC_EXTERN PetscErrorCode (*PetscLogPLB)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
282 PETSC_EXTERN PetscErrorCode (*PetscLogPLE)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
283 PETSC_EXTERN PetscErrorCode (*PetscLogPHC)(PetscObject);
284 PETSC_EXTERN PetscErrorCode (*PetscLogPHD)(PetscObject);
285 
286 #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)
287 #define PetscLogObjectCreate(h)      ((PetscLogPHC) ? (*PetscLogPHC)((PetscObject)(h)) : 0)
288 #define PetscLogObjectDestroy(h)     ((PetscLogPHD) ? (*PetscLogPHD)((PetscObject)(h)) : 0)
289 PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...);
290 
291 /* Initialization functions */
292 PETSC_EXTERN PetscErrorCode PetscLogDefaultBegin(void);
293 PETSC_EXTERN PetscErrorCode PetscLogAllBegin(void);
294 PETSC_EXTERN PetscErrorCode PetscLogNestedBegin(void);
295 PETSC_EXTERN PetscErrorCode PetscLogTraceBegin(FILE *);
296 PETSC_EXTERN PetscErrorCode PetscLogActions(PetscBool);
297 PETSC_EXTERN PetscErrorCode PetscLogObjects(PetscBool);
298 PETSC_EXTERN PetscErrorCode PetscLogSetThreshold(PetscLogDouble,PetscLogDouble*);
299 PETSC_EXTERN PetscErrorCode PetscLogSet(PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject),
300                                         PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
301 
302 /* Output functions */
303 PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
304 PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
305 PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);
306 
307 /* Stage functions */
308 PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[],PetscLogStage*);
309 PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
310 PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
311 PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage,PetscBool);
312 PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage,PetscBool*);
313 PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage,PetscBool);
314 PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage,PetscBool*);
315 PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char[],PetscLogStage*);
316 
317 /* Event functions */
318 PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[],PetscClassId,PetscLogEvent*);
319 PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent,PetscBool);
320 PETSC_EXTERN PetscErrorCode PetscLogEventIncludeClass(PetscClassId);
321 PETSC_EXTERN PetscErrorCode PetscLogEventExcludeClass(PetscClassId);
322 PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
323 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(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 PETSC_STATIC_INLINE PetscErrorCode PetscMPITypeSize(PetscLogDouble *buff,PetscMPIInt count,MPI_Datatype type)
387 {
388   PetscMPIInt mysize;
389   PetscErrorCode _myierr;
390   if (type == MPI_DATATYPE_NULL) return 0;
391   _myierr = MPI_Type_size(type,&mysize);CHKERRQ(_myierr);
392   *buff += (PetscLogDouble) (count*mysize);
393   return 0;
394 }
395 
396 PETSC_STATIC_INLINE PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm, PetscLogDouble *buff,PetscMPIInt *counts,MPI_Datatype type)
397 {
398   PetscMPIInt mysize, commsize, p;
399   PetscErrorCode _myierr;
400 
401   if (type == MPI_DATATYPE_NULL) return 0;
402   _myierr = MPI_Comm_size(comm,&commsize);CHKERRQ(_myierr);
403   _myierr = MPI_Type_size(type,&mysize);CHKERRQ(_myierr);
404   for (p = 0; p < commsize; ++p) {
405     *buff += (PetscLogDouble) (counts[p]*mysize);
406   }
407   return 0;
408 }
409 
410 /*
411     Returns 1 if the communicator is parallel else zero
412 */
413 PETSC_STATIC_INLINE int PetscMPIParallelComm(MPI_Comm comm)
414 {
415   PetscMPIInt size; MPI_Comm_size(comm,&size); return size > 1;
416 }
417 
418 #define MPI_Irecv(buf,count,datatype,source,tag,comm,request) \
419   ((petsc_irecv_ct++,0) || PetscMPITypeSize(&(petsc_irecv_len),(count),(datatype)) || MPI_Irecv((buf),(count),(datatype),(source),(tag),(comm),(request)))
420 
421 #define MPI_Isend(buf,count,datatype,dest,tag,comm,request) \
422   ((petsc_isend_ct++,0) || PetscMPITypeSize(&(petsc_isend_len),(count),(datatype)) || MPI_Isend((buf),(count),(datatype),(dest),(tag),(comm),(request)))
423 
424 #define MPI_Startall_irecv(count,datatype,number,requests) \
425   ((petsc_irecv_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize(&(petsc_irecv_len),(count),(datatype)) || ((number) && MPI_Startall((number),(requests))))
426 
427 #define MPI_Startall_isend(count,datatype,number,requests) \
428   ((petsc_isend_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize(&(petsc_isend_len),(count),(datatype)) || ((number) && MPI_Startall((number),(requests))))
429 
430 #define MPI_Start_isend(count,datatype,requests) \
431   ((petsc_isend_ct++,0) || PetscMPITypeSize((&petsc_isend_len),(count),(datatype)) || MPI_Start((requests)))
432 
433 #define MPI_Recv(buf,count,datatype,source,tag,comm,status) \
434   ((petsc_recv_ct++,0) || PetscMPITypeSize((&petsc_recv_len),(count),(datatype)) || MPI_Recv((buf),(count),(datatype),(source),(tag),(comm),(status)))
435 
436 #define MPI_Send(buf,count,datatype,dest,tag,comm) \
437   ((petsc_send_ct++,0) || PetscMPITypeSize((&petsc_send_len),(count),(datatype)) || MPI_Send((buf),(count),(datatype),(dest),(tag),(comm)))
438 
439 #define MPI_Wait(request,status) \
440   ((petsc_wait_ct++,petsc_sum_of_waits_ct++,0) || MPI_Wait((request),(status)))
441 
442 #define MPI_Waitany(a,b,c,d) \
443   ((petsc_wait_any_ct++,petsc_sum_of_waits_ct++,0) || MPI_Waitany((a),(b),(c),(d)))
444 
445 #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
446   ((petsc_wait_all_ct++,petsc_sum_of_waits_ct += (PetscLogDouble) (count),0) || MPI_Waitall((count),(array_of_requests),(array_of_statuses)))
447 
448 #define MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm) \
449   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Allreduce((sendbuf),(recvbuf),(count),(datatype),(op),(comm)))
450 
451 #define MPI_Bcast(buffer,count,datatype,root,comm) \
452   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Bcast((buffer),(count),(datatype),(root),(comm)))
453 
454 #define MPI_Reduce_scatter_block(sendbuf,recvbuf,recvcount,datatype,op,comm) \
455   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Reduce_scatter_block((sendbuf),(recvbuf),(recvcount),(datatype),(op),(comm)))
456 
457 #define MPI_Alltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
458   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((&petsc_send_len),(sendcount),(sendtype)) || MPI_Alltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
459 
460 #define MPI_Alltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
461   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(&petsc_send_len),(sendcnts),(sendtype)) || MPI_Alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))
462 
463 #define MPI_Allgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
464   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
465 
466 #define MPI_Allgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm) \
467   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm)))
468 
469 #define MPI_Gather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
470   ((petsc_gather_ct++,0) || PetscMPITypeSize((&petsc_send_len),(sendcount),(sendtype)) || MPI_Gather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
471 
472 #define MPI_Gatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm) \
473   ((petsc_gather_ct++,0) || PetscMPITypeSize((&petsc_send_len),(sendcount),(sendtype)) || MPI_Gatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm)))
474 
475 #define MPI_Scatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
476   ((petsc_scatter_ct++,0) || PetscMPITypeSize((&petsc_recv_len),(recvcount),(recvtype)) || MPI_Scatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
477 
478 #define MPI_Scatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm) \
479   ((petsc_scatter_ct++,0) || PetscMPITypeSize((&petsc_recv_len),(recvcount),(recvtype)) || MPI_Scatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
480 
481 #else
482 
483 #define MPI_Startall_irecv(count,datatype,number,requests) \
484   ((number) && MPI_Startall((number),(requests)))
485 
486 #define MPI_Startall_isend(count,datatype,number,requests) \
487   ((number) && MPI_Startall((number),(requests)))
488 
489 #define MPI_Start_isend(count,datatype,requests) \
490   (MPI_Start((requests)))
491 
492 #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
493 
494 #else  /* ---Logging is turned off --------------------------------------------*/
495 
496 #define PetscLogMemory                     PETSC_FALSE
497 
498 #define PetscLogFlops(n)                   0
499 #define PetscGetFlops(a)                   (*(a) = 0.0,0)
500 
501 #define PetscLogStageRegister(a,b)         0
502 #define PetscLogStagePush(a)               0
503 #define PetscLogStagePop()                 0
504 #define PetscLogStageSetActive(a,b)        0
505 #define PetscLogStageGetActive(a,b)        0
506 #define PetscLogStageGetVisible(a,b)       0
507 #define PetscLogStageSetVisible(a,b)       0
508 #define PetscLogStageGetId(a,b)            (*(b)=0,0)
509 
510 #define PetscLogEventRegister(a,b,c)       0
511 #define PetscLogEventSetCollective(a,b)    0
512 #define PetscLogEventIncludeClass(a)       0
513 #define PetscLogEventExcludeClass(a)       0
514 #define PetscLogEventActivate(a)           0
515 #define PetscLogEventDeactivate(a)         0
516 #define PetscLogEventActivateClass(a)      0
517 #define PetscLogEventDeactivateClass(a)    0
518 #define PetscLogEventSetActiveAll(a,b)     0
519 #define PetscLogEventGetId(a,b)            (*(b)=0,0)
520 #define PetscLogEventGetPerfInfo(a,b,c)    0
521 #define PetscLogEventSetDof(a,b,c)         0
522 #define PetscLogEventSetError(a,b,c)       0
523 
524 #define PetscLogPLB                        0
525 #define PetscLogPLE                        0
526 #define PetscLogPHC                        0
527 #define PetscLogPHD                        0
528 
529 #define PetscLogObjectParents(p,n,c)       0
530 #define PetscLogObjectCreate(h)            0
531 #define PetscLogObjectDestroy(h)           0
532 PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject,const char[],...);
533 
534 #define PetscLogDefaultBegin()             0
535 #define PetscLogAllBegin()                 0
536 #define PetscLogNestedBegin()              0
537 #define PetscLogTraceBegin(file)           0
538 #define PetscLogActions(a)                 0
539 #define PetscLogObjects(a)                 0
540 #define PetscLogSetThreshold(a,b)          0
541 #define PetscLogSet(lb,le)                 0
542 
543 #define PetscLogView(viewer)               0
544 #define PetscLogViewFromOptions()          0
545 #define PetscLogDump(c)                    0
546 
547 #define PetscLogEventSync(e,comm)          0
548 #define PetscLogEventBegin(e,o1,o2,o3,o4)  0
549 #define PetscLogEventEnd(e,o1,o2,o3,o4)    0
550 
551 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
552 #define MPI_Startall_irecv(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
553 #define MPI_Startall_isend(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
554 #define MPI_Start_isend(count,datatype,requests)           MPI_Start(requests)
555 
556 #endif   /* PETSC_USE_LOG */
557 
558 #define PetscPreLoadBegin(flag,name) \
559 do {\
560   PetscBool      PetscPreLoading = flag;\
561   int            PetscPreLoadMax,PetscPreLoadIt;\
562   PetscLogStage  _stageNum;\
563   PetscErrorCode _3_ierr; \
564   _3_ierr = PetscOptionsGetBool(NULL,NULL,"-preload",&PetscPreLoading,NULL);CHKERRQ(_3_ierr); \
565   PetscPreLoadMax = (int)(PetscPreLoading);\
566   PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
567   for (PetscPreLoadIt=0; PetscPreLoadIt<=PetscPreLoadMax; PetscPreLoadIt++) {\
568     PetscPreLoadingOn = PetscPreLoading;\
569     _3_ierr = PetscBarrier(NULL);CHKERRQ(_3_ierr);\
570     if (PetscPreLoadIt>0) {\
571       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
572     } else {\
573       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
574     }\
575     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt));\
576     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
577 
578 #define PetscPreLoadEnd() \
579     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
580     PetscPreLoading = PETSC_FALSE;\
581   }\
582 } while (0)
583 
584 #define PetscPreLoadStage(name) do {                                         \
585     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);                      \
586     if (PetscPreLoadIt>0) {                                                  \
587       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);   \
588     } else {                                                            \
589       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
590     }                                                                   \
591     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt)); \
592     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);            \
593   } while (0)
594 
595 /* some vars for logging */
596 PETSC_EXTERN PetscBool PetscPreLoadingUsed;       /* true if we are or have done preloading */
597 PETSC_EXTERN PetscBool PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
598 
599 #endif
600