xref: /petsc/include/petsclog.h (revision 0c2fff18d3777e2004c38c700aa5f85e14cc2190)
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[],...) PETSC_PRINTF_FORMAT_CHECK(2,3);
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[], ...)  PETSC_PRINTF_FORMAT_CHECK(2,3);
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   PetscTruth     active;        /* The flag to activate logging */
135   PetscTruth     visible;       /* The flag to print info in summary */
136   int            depth;         /* The nesting depth of the event call */
137   int            count;         /* The number of times this section was executed */
138   PetscLogDouble flops;         /* The flops used in this section */
139   PetscLogDouble time;          /* The time taken for this section */
140   PetscLogDouble numMessages;   /* The number of messages in this section */
141   PetscLogDouble messageLength; /* The total message lengths in this section */
142   PetscLogDouble numReductions; /* The number of reductions in this section */
143 } PerfInfo;
144 
145 /* The structure for logging events */
146 typedef struct _EventLog *EventLog;
147 struct _EventLog {
148   int       numEvents;   /* The number of registered events */
149   int       maxEvents;   /* The maximum number of events */
150   PerfInfo *eventInfo;   /* The performance information for each event */
151 };
152 typedef int PetscEvent;
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   IntStack    stack;        /* The stack for active stages */
181   int         curStage;     /* The current stage (only used in macros so we don't call StackTop) */
182   /* Stage specific information */
183   PerfInfo   *stageInfo;    /* The performance information for each stage */
184   EventLog   *eventLog;     /* The event log for each stage */
185   ClassLog   *classLog;     /* The class information for each stage */
186 };
187 
188 #if defined(PETSC_HAVE_MPE)
189 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) \
190   0; { int _1_ierr; \
191     if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventInfo[e].active) { \
192       _1_ierr = PetscLogEventBegin((e),o1,o2,o3,o4);CHKERRQ(_1_ierr);  \
193       if (UseMPE && PetscLogEventMPEFlags[(e)])                      \
194         MPE_Log_event(MPEBEGIN+2*(e),0,"");                      \
195       _1_ierr = MPI_Barrier(cm);CHKERRQ(_1_ierr);                  \
196       _1_ierr = PetscLogEventEnd((e),o1,o2,o3,o4);CHKERRQ(_1_ierr);    \
197       if (UseMPE && PetscLogEventMPEFlags[(e)])                      \
198         MPE_Log_event(MPEBEGIN+2*((e)+1),0,"");                  \
199     }                                                            \
200     _1_ierr = PetscLogEventBegin(e+1,o1,o2,o3,o4);CHKERRQ(_1_ierr);    \
201     if (UseMPE && PetscLogEventMPEFlags[(e)+1])                      \
202       MPE_Log_event(MPEBEGIN+2*((e)+1),0,"");                    \
203   }
204 #define PetscLogEventBegin(e,o1,o2,o3,o4)  \
205   0; { \
206    if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventInfo[e].active) {\
207      (*_PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
208    if (UseMPE && PetscLogEventMPEFlags[(e)])\
209      MPE_Log_event(MPEBEGIN+2*(e),0,"");\
210   }
211 #else
212 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) \
213   0; { int _2_ierr;\
214     if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventInfo[e].active) { \
215       _2_ierr = PetscLogEventBegin((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);    \
216       _2_ierr = MPI_Barrier(cm);CHKERRQ(_2_ierr);                    \
217       _2_ierr = PetscLogEventEnd((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);      \
218     }                                                              \
219     _2_ierr = PetscLogEventBegin((e)+1,o1,o2,o3,o4);CHKERRQ(_2_ierr);    \
220   }
221 #define PetscLogEventBegin(e,o1,o2,o3,o4)  \
222   0; { \
223    if (_PetscLogPLB && _stageLog->eventLog[_stageLog->curStage]->eventInfo[e].active) {\
224      (*_PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
225   }
226 #endif
227 
228 #if defined(PETSC_HAVE_MPE)
229 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm) PetscLogEventEnd(e+1,o1,o2,o3,o4)
230 #define PetscLogEventEnd(e,o1,o2,o3,o4) \
231   0; { \
232   if (_PetscLogPLE && _stageLog->eventLog[_stageLog->curStage]->eventInfo[e].active) {\
233     (*_PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
234   if (UseMPE && PetscLogEventMPEFlags[(e)])\
235      MPE_Log_event(MPEBEGIN+2*(e)+1,0,"");\
236   }
237 #else
238 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm) PetscLogEventEnd(e+1,o1,o2,o3,o4)
239 #define PetscLogEventEnd(e,o1,o2,o3,o4) \
240   0; { \
241   if (_PetscLogPLE && _stageLog->eventLog[_stageLog->curStage]->eventInfo[e].active) {\
242     (*_PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));}\
243   }
244 #endif
245 
246 /*
247      This does not work for MPI-Uni because our src/mpiuni/mpi.h file
248    uses macros to defined the MPI operations.
249 
250      It does not work correctly from HP-UX because it processes the
251    macros in a way that sometimes it double counts, hence
252    HAVE_BROKEN_RECURSIVE_MACRO
253 
254      It does not work with Windows NT because winmpich lacks MPI_Type_size()
255 */
256 #if !defined(PETSC_HAVE_MPI_UNI) && !defined(HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
257 /*
258    Logging of MPI activities
259 */
260 
261 #define TypeSize(buff,count,type)                                                \
262 (\
263   MPI_Type_size(type,&PETSC_DUMMY_SIZE),buff += ((PetscLogDouble) ((count)*PETSC_DUMMY_SIZE)) \
264 )
265 
266 #define MPI_Irecv(buf,count, datatype,source,tag,comm,request)        \
267 (\
268   PETSC_DUMMY = MPI_Irecv(buf,count, datatype,source,tag,comm,request),            \
269   irecv_ct++,TypeSize(irecv_len,count,datatype),PETSC_DUMMY                            \
270 )
271 
272 #define MPI_Isend(buf,count, datatype,dest,tag,comm,request)          \
273 (\
274   PETSC_DUMMY = MPI_Isend(buf,count, datatype,dest,tag,comm,request),              \
275   isend_ct++,  TypeSize(isend_len,count,datatype),PETSC_DUMMY                          \
276 )
277 
278 #define MPI_Startall_irecv(count,number,requests)                                     \
279 (\
280   PETSC_DUMMY = MPI_Startall(number,requests),                                                    \
281   irecv_ct += (PetscLogDouble)(number),irecv_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY \
282 )
283 
284 #define MPI_Startall_isend(count,number,requests)                                    \
285 (\
286   PETSC_DUMMY = MPI_Startall(number,requests),                                                   \
287   isend_ct += (PetscLogDouble)(number),isend_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY \
288 )
289 
290 #define MPI_Start_isend(count, requests)\
291 (\
292   PETSC_DUMMY = MPI_Start(requests),\
293   isend_ct++,isend_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY\
294 )
295 
296 #define MPI_Recv(buf,count, datatype,source,tag,comm,status)           \
297 (\
298   PETSC_DUMMY = MPI_Recv(buf,count, datatype,source,tag,comm,status),               \
299   recv_ct++,TypeSize(recv_len,count,datatype),PETSC_DUMMY                              \
300 )
301 
302 #define MPI_Send(buf,count, datatype,dest,tag,comm)                     \
303 (\
304   PETSC_DUMMY = MPI_Send(buf,count, datatype,dest,tag,comm),                         \
305   send_ct++, TypeSize(send_len,count,datatype),PETSC_DUMMY                              \
306 )
307 
308 #define MPI_Wait(request,status) \
309 (\
310   wait_ct++,sum_of_waits_ct++,  \
311   MPI_Wait(request,status)       \
312 )
313 
314 #define MPI_Waitany(a,b,c,d)     \
315 (\
316   wait_any_ct++,sum_of_waits_ct++,\
317   MPI_Waitany(a,b,c,d)           \
318 )
319 
320 #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
321 (\
322   wait_all_ct++,sum_of_waits_ct += (PetscLogDouble) (count),       \
323   MPI_Waitall(count,array_of_requests,array_of_statuses)       \
324 )
325 
326 #define MPI_Allreduce(sendbuf, recvbuf,count,datatype,op,comm) \
327 (\
328   allreduce_ct++,MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm)\
329 )
330 
331 #else
332 
333 #define MPI_Startall_irecv(count,number,requests) \
334 (\
335   MPI_Startall(number,requests)                 \
336 )
337 
338 #define MPI_Startall_isend(count,number,requests) \
339 (\
340   MPI_Startall(number,requests)                 \
341 )
342 
343 #define MPI_Start_isend(count, requests) \
344 (\
345   MPI_Start(requests)                   \
346 )
347 
348 #endif /* !PETSC_HAVE_MPI_UNI && ! HAVE_BROKEN_RECURSIVE_MACRO */
349 
350 #else  /* ---Logging is turned off --------------------------------------------*/
351 
352 #define PetscLogFlops(n) 0
353 
354 /*
355      With logging turned off, then MPE has to be turned off
356 */
357 #define MPEBEGIN                  1000
358 #define PetscLogMPEBegin()            0
359 #define PetscLogMPEDump(a)            0
360 #define PetscLogEventMPEActivate(a)   0
361 #define PetscLogEventMPEDeactivate(a) 0
362 
363 #define PetscLogEventActivate(a)   0
364 #define PetscLogEventDeactivate(a) 0
365 
366 #define PetscLogEventActivateClass(a)   0
367 #define PetscLogEventDeactivateClass(a) 0
368 
369 #define _PetscLogPLB                        0
370 #define _PetscLogPLE                        0
371 #define _PetscLogPHC                        0
372 #define _PetscLogPHD                        0
373 #define PetscGetFlops(a)                (*(a) = 0.0,0)
374 #define PetscLogEventBegin(e,o1,o2,o3,o4)   0
375 #define PetscLogEventEnd(e,o1,o2,o3,o4)     0
376 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) 0
377 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm)   0
378 #define PetscLogObjectParent(p,c)
379 #define PetscLogObjectParents(p,n,c)
380 #define PetscLogObjectCreate(h)
381 #define PetscLogObjectDestroy(h)
382 #define PetscLogObjectMemory(p,m)
383 #define PetscLogDestroy()                   0
384 #define PetscLogStagePush(a)                0
385 #define PetscLogStagePop()                  0
386 #define PetscLogStageRegister(a,b)          0
387 #define PetscLogStagePrint(a,flg)           0
388 #define PetscLogPrintSummary(comm,file)     0
389 #define PetscLogBegin()                     0
390 #define PetscLogTraceBegin(file)            0
391 #define PetscLogSet(lb,le)                  0
392 #define PetscLogAllBegin()                  0
393 #define PetscLogDump(c)                     0
394 #define PetscLogEventRegister(a,b,c,d)      0
395 EXTERN int PetscLogObjectState(PetscObject,const char[],...) PETSC_PRINTF_FORMAT_CHECK(2,3);
396 
397 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
398 #define MPI_Startall_irecv(count,number,requests) MPI_Startall(number,requests)
399 
400 #define MPI_Startall_isend(count,number,requests) MPI_Startall(number,requests)
401 
402 #define MPI_Start_isend(count,requests) MPI_Start(requests)
403 
404 #endif   /* PETSC_USE_LOG */
405 
406 extern PetscTruth PetscPreLoadingUsed;       /* true if we are or have done preloading */
407 extern PetscTruth PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
408 
409 #define PreLoadBegin(flag,name) {PetscTruth PreLoading = flag; \
410                                  int        PreLoadMax,PreLoadIt,_stageNum,_3_ierr;\
411                                  _3_ierr = PetscOptionsGetLogical(PETSC_NULL,"-preload",&PreLoading,PETSC_NULL);CHKERRQ(_3_ierr);\
412                                  PreLoadMax = (int)(PreLoading);PetscPreLoadingUsed = PreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
413                                  for (PreLoadIt=0; PreLoadIt<=PreLoadMax; PreLoadIt++) {\
414                                    PetscPreLoadingOn = PreLoading;\
415                                    _3_ierr = PetscBarrier(PETSC_NULL);CHKERRQ(_3_ierr);\
416                                    if (PreLoadIt>0) {\
417                                      _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
418                                    } else {\
419                                      _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
420                                      _3_ierr = PetscLogStageSetVisible(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
421                                    }\
422                                    _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
423 
424 #define PreLoadEnd()               _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);PreLoading = PETSC_FALSE;}}
425 
426 #define PreLoadStage(name)         _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
427                                    if (PreLoadIt>0) {\
428                                      _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
429                                    } else {\
430                                      _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
431                                      _3_ierr = PetscLogStageSetVisible(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
432                                    }\
433                                    _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
434 #endif
435