xref: /petsc/include/petsclog.h (revision d5ba7fb7d3f82433fc93946f26018f4b1c7683c8)
1 /* $Id: petsclog.h,v 1.155 2001/09/06 14:51:20 bsmith Exp $ */
2 
3 /*
4     Defines profile/logging in PETSc.
5 */
6 
7 #if !defined(__PetscLog_H)
8 #define __PetscLog_H
9 #include "petsc.h"
10 
11 /*
12   Each PETSc object class has it's own cookie (internal integer in the
13   data structure used for error checking). These are all defined by an offset
14   from the lowest one, PETSC_COOKIE.
15 */
16 #define PETSC_COOKIE 1211211
17 extern int PETSC_LARGEST_COOKIE;
18 #define PETSC_EVENT  1311311
19 extern int PETSC_LARGEST_EVENT;
20 
21 /* Events for the Petsc standard library */
22 extern int PETSC_Barrier;
23 
24 /* Global flop counter */
25 extern PetscLogDouble _TotalFlops;
26 
27 /* General logging of information; different from event logging */
28 EXTERN int        PetscLogInfo(void*,const char[],...);
29 EXTERN int        PetscLogInfoDeactivateClass(int);
30 EXTERN int        PetscLogInfoActivateClass(int);
31 extern PetscTruth PetscLogPrintInfo;  /* if true, indicates PetscLogInfo() is turned on */
32 
33 #if defined(PETSC_USE_LOG)  /* --- Logging is turned on --------------------------------*/
34 
35 /*
36    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
37 
38    For the complex numbers version, note that
39        1 complex addition = 2 flops
40        1 complex multiplication = 6 flops,
41    where we define 1 flop as that for a double precision scalar.  We roughly approximate
42    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
43    to the assumption that we're counting mostly additions and multiplications -- and
44    roughly the same number of each.  More accurate counting could be done by distinguishing
45    among the various arithmetic operations.
46  */
47 
48 #if defined(PETSC_USE_COMPLEX)
49 #define PetscLogFlops(n) (_TotalFlops += (4*n),0)
50 #else
51 #define PetscLogFlops(n) (_TotalFlops += (n),0)
52 #endif
53 
54 #if defined (PETSC_HAVE_MPE)
55 #include "mpe.h"
56 #define MPEBEGIN    1000
57 EXTERN int        PetscLogMPEBegin(void);
58 EXTERN int        PetscLogMPEDump(const char[]);
59 extern PetscTruth UseMPE;
60 extern int        PetscLogEventMPEFlags[];
61 EXTERN int        PetscLogEventMPEActivate(int);
62 EXTERN int        PetscLogEventMPEDeactivate(int);
63 #else
64 #define PetscLogEventMPEActivate(a) 0
65 #define PetscLogEventMPEDeactivate(a) 0
66 #endif
67 
68 EXTERN int (*_PetscLogPLB)(int,int,PetscObject,PetscObject,PetscObject,PetscObject);
69 EXTERN int (*_PetscLogPLE)(int,int,PetscObject,PetscObject,PetscObject,PetscObject);
70 EXTERN int (*_PetscLogPHC)(PetscObject);
71 EXTERN int (*_PetscLogPHD)(PetscObject);
72 
73 #define PetscLogObjectParent(p,c)       if (c) {PetscValidHeader((PetscObject)(c)); \
74                                                 PetscValidHeader((PetscObject)(p));\
75                                                 ((PetscObject)(c))->parent = (PetscObject)(p);\
76 				                ((PetscObject)(c))->parentid = ((PetscObject)p)->id;}
77 #define PetscLogObjectParents(p,n,d)    {int _i; for (_i=0; _i<n; _i++) \
78                                          PetscLogObjectParent(p,(d)[_i]);}
79 #define PetscLogObjectCreate(h)         {if (_PetscLogPHC) (*_PetscLogPHC)((PetscObject)h);}
80 #define PetscLogObjectDestroy(h)        {if (_PetscLogPHD) (*_PetscLogPHD)((PetscObject)h);}
81 #define PetscLogObjectMemory(p,m)       {PetscValidHeader((PetscObject)p);\
82                                          ((PetscObject)(p))->mem += (m);}
83 /* Initialization functions */
84 EXTERN int PetscLogBegin(void);
85 EXTERN int PetscLogAllBegin(void);
86 EXTERN int PetscLogTraceBegin(FILE *);
87 /* General functions */
88 EXTERN int PetscLogDestroy(void);
89 EXTERN int PetscLogSet(int (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject),
90                    int (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
91 EXTERN int PetscLogObjectState(PetscObject, const char[], ...);
92 /* Output functions */
93 EXTERN int PetscLogPrintSummary(MPI_Comm, const char[]);
94 EXTERN int PetscLogDump(const char[]);
95 /* Counter functions */
96 EXTERN int PetscGetFlops(PetscLogDouble *);
97 /* Stage functions */
98 EXTERN int PetscLogStageRegister(int *, const char[]);
99 EXTERN int PetscLogStagePush(int);
100 EXTERN int PetscLogStagePop(void);
101 EXTERN int PetscLogStageSetVisible(int, PetscTruth);
102 EXTERN int PetscLogStageGetVisible(int, PetscTruth *);
103 EXTERN int PetscLogStageGetId(const char [], int *);
104 /* Event functions */
105 EXTERN int PetscLogEventRegister(int *, const char[], const char[], int);
106 EXTERN int PetscLogEventActivate(int);
107 EXTERN int PetscLogEventDeactivate(int);
108 EXTERN int PetscLogEventActivateClass(int);
109 EXTERN int PetscLogEventDeactivateClass(int);
110 /* Class functions */
111 EXTERN int PetscLogClassRegister(int *, const char []);
112 
113 /* Default log */
114 typedef struct _StageLog *StageLog;
115 extern StageLog _stageLog;
116 
117 /* Global counters */
118 extern PetscLogDouble irecv_ct,  isend_ct,  recv_ct,  send_ct;
119 extern PetscLogDouble irecv_len, isend_len, recv_len, send_len;
120 extern PetscLogDouble allreduce_ct;
121 extern PetscLogDouble wait_ct, wait_any_ct, wait_all_ct, sum_of_waits_ct;
122 extern int            PETSC_DUMMY, PETSC_DUMMY_SIZE;
123 
124 /* We must make these structures available if we are to access the event
125    activation flags in the PetscLogEventBegin/End() macros. If we forced a
126    function call each time, we could leave these structures in plog.h
127 */
128 /* The structure for logging performance */
129 typedef struct _PerfInfo {
130   char          *name;          /* The name of this section */
131   char          *color;         /* The color of this section */
132   int            id;            /* The integer identifying this event */
133   int            cookie;        /* The class id for this section */
134   int            depth;         /* The nesting depth of the event call */
135   int            count;         /* The number of times this section was executed */
136   PetscLogDouble flops;         /* The flops used in this section */
137   PetscLogDouble time;          /* The time taken for this section */
138   PetscLogDouble numMessages;   /* The number of messages in this section */
139   PetscLogDouble messageLength; /* The total message lengths in this section */
140   PetscLogDouble numReductions; /* The number of reductions in this section */
141 } PerfInfo;
142 
143 /* The structure for logging events */
144 typedef struct _EventLog *EventLog;
145 struct _EventLog {
146   /* Size information */
147   int       numEvents;   /* The number of registered events */
148   int       maxEvents;   /* The maximum number of events */
149   /* Event specific information */
150   int      *eventActive; /* The flag for active events */
151   PerfInfo *eventInfo;   /* The performance information for each event */
152 };
153 
154 /* The structure for logging class information */
155 typedef struct _ClassInfo {
156   char          *name;          /* The class name */
157   int            cookie;        /* The integer identifying this class */
158   int            creations;     /* The number of objects of this class created */
159   int            destructions;  /* The number of objects of this class destroyed */
160   PetscLogDouble mem;           /* The total memory allocated by objects of this class */
161   PetscLogDouble descMem;       /* The total memory allocated by descendents of these objects */
162 } ClassInfo;
163 
164 typedef struct _ClassLog *ClassLog;
165 struct _ClassLog {
166   int        numClasses; /* The number of classes registered */
167   int        maxClasses; /* The maximum number of classes */
168   ClassInfo *classInfo;  /* The structure for classs information (cookies are monotonicly increasing) */
169 };
170 
171 /* A simple stack (should replace) */
172 typedef struct _IntStack *IntStack;
173 
174 /* The structure for logging in stages */
175 struct _StageLog {
176   /* Size information */
177   int         numStages;    /* The number of registered stages */
178   int         maxStages;    /* The maximum number of stages */
179   /* Runtime information */
180   int        *stageUsed;    /* The flags for stages which were executed */
181   PetscTruth *stageVisible; /* The flags for stages which are visible to PetscLogPrintSummary() */
182   IntStack    stack;        /* The stack for active stages */
183   int         curStage;     /* The current stage (only used in macros so we don't call StackTop) */
184   /* Stage specific information */
185   PerfInfo   *stageInfo;    /* The performance information for each stage */
186   EventLog   *eventLog;     /* The event log for each stage */
187   ClassLog   *classLog;     /* The class information for each stage */
188 };
189 
190 EXTERN int EventLogGetEvent(EventLog, int, int *);
191 
192 #if defined(PETSC_HAVE_MPE)
193 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) \
194   0; { int _1_ierr, _1_eventNum; \
195     _1_ierr = EventLogGetEvent(_stageLog->eventLog[_stageLog->curStage], (e), &_1_eventNum); \
196     if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventActive[_1_eventNum]) { \
197       _1_ierr = PetscLogEventBegin((e),o1,o2,o3,o4);CHKERRQ(_1_ierr);  \
198       if (UseMPE && PetscLogEventMPEFlags[(e)])                      \
199         MPE_Log_event(MPEBEGIN+2*(e),0,"");                      \
200       _1_ierr = MPI_Barrier(cm);CHKERRQ(_1_ierr);                  \
201       _1_ierr = PetscLogEventEnd((e),o1,o2,o3,o4);CHKERRQ(_1_ierr);    \
202       if (UseMPE && PetscLogEventMPEFlags[(e)])                      \
203         MPE_Log_event(MPEBEGIN+2*((e)+1),0,"");                  \
204     }                                                            \
205     _1_ierr = PetscLogEventBegin(e+1,o1,o2,o3,o4);CHKERRQ(_1_ierr);    \
206     if (UseMPE && PetscLogEventMPEFlags[(e)+1])                      \
207       MPE_Log_event(MPEBEGIN+2*((e)+1),0,"");                    \
208   }
209 #define PetscLogEventBegin(e,o1,o2,o3,o4)  \
210   0; { int _3_ierr, _3_eventNum; \
211     _3_ierr = EventLogGetEvent(_stageLog->eventLog[_stageLog->curStage], (e), &_3_eventNum); \
212    if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventActive[_3_eventNum]) {\
213      (*_PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
214    if (UseMPE && PetscLogEventMPEFlags[(e)])\
215      MPE_Log_event(MPEBEGIN+2*(e),0,"");\
216   }
217 #else
218 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) \
219   0; { int _2_ierr, _2_eventNum;\
220     _2_ierr = EventLogGetEvent(_stageLog->eventLog[_stageLog->curStage], (e), &_2_eventNum); \
221     if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventActive[_2_eventNum]) {                         \
222       _2_ierr = PetscLogEventBegin((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);    \
223       _2_ierr = MPI_Barrier(cm);CHKERRQ(_2_ierr);                    \
224       _2_ierr = PetscLogEventEnd((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);      \
225     }                                                              \
226     _2_ierr = PetscLogEventBegin((e)+1,o1,o2,o3,o4);CHKERRQ(_2_ierr);    \
227   }
228 #define PetscLogEventBegin(e,o1,o2,o3,o4)  \
229   0; { int _4_ierr, _4_eventNum; \
230     _4_ierr = EventLogGetEvent(_stageLog->eventLog[_stageLog->curStage], (e), &_4_eventNum); \
231    if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventActive[_4_eventNum]) {\
232      (*_PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
233   }
234 #endif
235 
236 #if defined(PETSC_HAVE_MPE)
237 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm) PetscLogEventEnd(e+1,o1,o2,o3,o4)
238 #define PetscLogEventEnd(e,o1,o2,o3,o4) \
239   0; { int _5_ierr, _5_eventNum; \
240     _5_ierr = EventLogGetEvent(_stageLog->eventLog[_stageLog->curStage], (e), &_5_eventNum); \
241   if (_PetscLogPLE && _stageLog->eventLog[_stageLog->curStage]->eventActive[_5_eventNum]) {\
242     (*_PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
243   if (UseMPE && PetscLogEventMPEFlags[(e)])\
244      MPE_Log_event(MPEBEGIN+2*(e)+1,0,"");\
245   }
246 #else
247 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm) PetscLogEventEnd(e+1,o1,o2,o3,o4)
248 #define PetscLogEventEnd(e,o1,o2,o3,o4) \
249   0; { int _6_ierr, _6_eventNum; \
250     _6_ierr = EventLogGetEvent(_stageLog->eventLog[_stageLog->curStage], (e), &_6_eventNum); \
251   if (_PetscLogPLE && _stageLog->eventLog[_stageLog->curStage]->eventActive[_6_eventNum]) {\
252     (*_PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
253   }
254 #endif
255 
256 /*
257      This does not work for MPI-Uni because our src/mpiuni/mpi.h file
258    uses macros to defined the MPI operations.
259 
260      It does not work correctly from HP-UX because it processes the
261    macros in a way that sometimes it double counts, hence
262    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
263 
264      It does not work with Windows NT because winmpich lacks MPI_Type_size()
265 */
266 #if !defined(HAVE_MPI_UNI) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
267 /*
268    Logging of MPI activities
269 */
270 
271 #define TypeSize(buff,count,type)                                                \
272 (\
273   MPI_Type_size(type,&PETSC_DUMMY_SIZE),buff += ((PetscLogDouble) ((count)*PETSC_DUMMY_SIZE)) \
274 )
275 
276 #define MPI_Irecv(buf,count, datatype,source,tag,comm,request)        \
277 (\
278   PETSC_DUMMY = MPI_Irecv(buf,count, datatype,source,tag,comm,request),            \
279   irecv_ct++,TypeSize(irecv_len,count,datatype),PETSC_DUMMY                            \
280 )
281 
282 #define MPI_Isend(buf,count, datatype,dest,tag,comm,request)          \
283 (\
284   PETSC_DUMMY = MPI_Isend(buf,count, datatype,dest,tag,comm,request),              \
285   isend_ct++,  TypeSize(isend_len,count,datatype),PETSC_DUMMY                          \
286 )
287 
288 #define MPI_Startall_irecv(count,number,requests)                                     \
289 (\
290   PETSC_DUMMY = MPI_Startall(number,requests),                                                    \
291   irecv_ct += (PetscLogDouble)(number),irecv_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY \
292 )
293 
294 #define MPI_Startall_isend(count,number,requests)                                    \
295 (\
296   PETSC_DUMMY = MPI_Startall(number,requests),                                                   \
297   isend_ct += (PetscLogDouble)(number),isend_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY \
298 )
299 
300 #define MPI_Start_isend(count, requests)\
301 (\
302   PETSC_DUMMY = MPI_Start(requests),\
303   isend_ct++,isend_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY\
304 )
305 
306 #define MPI_Recv(buf,count, datatype,source,tag,comm,status)           \
307 (\
308   PETSC_DUMMY = MPI_Recv(buf,count, datatype,source,tag,comm,status),               \
309   recv_ct++,TypeSize(recv_len,count,datatype),PETSC_DUMMY                              \
310 )
311 
312 #define MPI_Send(buf,count, datatype,dest,tag,comm)                     \
313 (\
314   PETSC_DUMMY = MPI_Send(buf,count, datatype,dest,tag,comm),                         \
315   send_ct++, TypeSize(send_len,count,datatype),PETSC_DUMMY                              \
316 )
317 
318 #define MPI_Wait(request,status) \
319 (\
320   wait_ct++,sum_of_waits_ct++,  \
321   MPI_Wait(request,status)       \
322 )
323 
324 #define MPI_Waitany(a,b,c,d)     \
325 (\
326   wait_any_ct++,sum_of_waits_ct++,\
327   MPI_Waitany(a,b,c,d)           \
328 )
329 
330 #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
331 (\
332   wait_all_ct++,sum_of_waits_ct += (PetscLogDouble) (count),       \
333   MPI_Waitall(count,array_of_requests,array_of_statuses)       \
334 )
335 
336 #define MPI_Allreduce(sendbuf, recvbuf,count,datatype,op,comm) \
337 (\
338   allreduce_ct++,MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm)\
339 )
340 
341 #else
342 
343 #define MPI_Startall_irecv(count,number,requests) \
344 (\
345   MPI_Startall(number,requests)                 \
346 )
347 
348 #define MPI_Startall_isend(count,number,requests) \
349 (\
350   MPI_Startall(number,requests)                 \
351 )
352 
353 #define MPI_Start_isend(count, requests) \
354 (\
355   MPI_Start(requests)                   \
356 )
357 
358 #endif /* !USING_MPIUNI && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
359 
360 #else  /* ---Logging is turned off --------------------------------------------*/
361 
362 #define PetscLogFlops(n) 0
363 
364 /*
365      With logging turned off, then MPE has to be turned off
366 */
367 #define MPEBEGIN                  1000
368 #define PetscLogMPEBegin()            0
369 #define PetscLogMPEDump(a)            0
370 #define PetscLogEventMPEActivate(a)   0
371 #define PetscLogEventMPEDeactivate(a) 0
372 
373 #define PetscLogEventActivate(a)   0
374 #define PetscLogEventDeactivate(a) 0
375 
376 #define PetscLogEventActivateClass(a)   0
377 #define PetscLogEventDeactivateClass(a) 0
378 
379 #define _PetscLogPLB                        0
380 #define _PetscLogPLE                        0
381 #define _PetscLogPHC                        0
382 #define _PetscLogPHD                        0
383 #define PetscGetFlops(a)                (*(a) = 0.0,0)
384 #define PetscLogEventBegin(e,o1,o2,o3,o4)   0
385 #define PetscLogEventEnd(e,o1,o2,o3,o4)     0
386 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) 0
387 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm)   0
388 #define PetscLogObjectParent(p,c)
389 #define PetscLogObjectParents(p,n,c)
390 #define PetscLogObjectCreate(h)
391 #define PetscLogObjectDestroy(h)
392 #define PetscLogObjectMemory(p,m)
393 #define PetscLogDestroy()                   0
394 #define PetscLogStagePush(a)                0
395 #define PetscLogStagePop()                  0
396 #define PetscLogStageRegister(a,b)          0
397 #define PetscLogStagePrint(a,flg)           0
398 #define PetscLogPrintSummary(comm,file)     0
399 #define PetscLogBegin()                     0
400 #define PetscLogTraceBegin(file)            0
401 #define PetscLogSet(lb,le)                  0
402 #define PetscLogAllBegin()                  0
403 #define PetscLogDump(c)                     0
404 #define PetscLogEventRegister(a,b,c)        0
405 EXTERN int PetscLogObjectState(PetscObject,const char[],...);
406 
407 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
408 #define MPI_Startall_irecv(count,number,requests) MPI_Startall(number,requests)
409 
410 #define MPI_Startall_isend(count,number,requests) MPI_Startall(number,requests)
411 
412 #define MPI_Start_isend(count,requests) MPI_Start(requests)
413 
414 #endif   /* PETSC_USE_LOG */
415 
416 extern PetscTruth PetscPreLoadingUsed;       /* true if we are or have done preloading */
417 extern PetscTruth PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
418 
419 #define PreLoadBegin(flag,name) {PetscTruth PreLoading = flag; \
420                                  int        PreLoadMax,PreLoadIt,_stageNum,_3_ierr;\
421                                  _3_ierr = PetscOptionsGetLogical(PETSC_NULL,"-preload",&PreLoading,PETSC_NULL);CHKERRQ(_3_ierr);\
422                                  PreLoadMax = (int)(PreLoading);PetscPreLoadingUsed = PreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
423                                  for (PreLoadIt=0; PreLoadIt<=PreLoadMax; PreLoadIt++) {\
424                                    PetscPreLoadingOn = PreLoading;\
425                                    _3_ierr = PetscBarrier(PETSC_NULL);CHKERRQ(_3_ierr);\
426                                    if (PreLoadIt>0) {\
427                                      _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
428                                    } else {\
429                                      _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
430                                      _3_ierr = PetscLogStageSetVisible(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
431                                    }\
432                                    _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
433 
434 #define PreLoadEnd()               _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);PreLoading = PETSC_FALSE;}}
435 
436 #define PreLoadStage(name)         _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
437                                    if (PreLoadIt>0) {\
438                                      _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
439                                    } else {\
440                                      _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
441                                      _3_ierr = PetscLogStageSetVisible(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
442                                    }\
443                                    _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
444 #endif
445