xref: /petsc/include/petsclog.h (revision c5356aad3b75ffed3351f37d6b11f06abebbe048)
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 EXTERN int        PetscLogMPEBegin(void);
57 EXTERN int        PetscLogMPEDump(const char[]);
58 extern PetscTruth UseMPE;
59 #define PETSC_LOG_EVENT_MPE_BEGIN(e) \
60   if(UseMPE && _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) \
61     MPE_Log_event(_stageLog->eventLog->eventInfo[e].mpe_id_begin,0,"");
62 
63 #define PETSC_LOG_EVENT_MPE_END(e) \
64   if(UseMPE && _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) \
65     MPE_Log_event(_stageLog->eventLog->eventInfo[e].mpe_id_end,0,"");
66 
67 #else
68 #define PETSC_LOG_EVENT_MPE_BEGIN(e)
69 #define PETSC_LOG_EVENT_MPE_END(e)
70 #endif
71 
72 EXTERN int (*_PetscLogPLB)(int,int,PetscObject,PetscObject,PetscObject,PetscObject);
73 EXTERN int (*_PetscLogPLE)(int,int,PetscObject,PetscObject,PetscObject,PetscObject);
74 EXTERN int (*_PetscLogPHC)(PetscObject);
75 EXTERN int (*_PetscLogPHD)(PetscObject);
76 
77 #define PetscLogObjectParent(p,c) \
78   if (c) {\
79     PetscValidHeader((PetscObject)(c));\
80     PetscValidHeader((PetscObject)(p));\
81     ((PetscObject)(c))->parent = (PetscObject)(p);\
82     ((PetscObject)(c))->parentid = ((PetscObject)p)->id;\
83   }
84 #define PetscLogObjectParents(p,n,d) {int _i; for (_i=0; _i<n; _i++) PetscLogObjectParent(p,(d)[_i]);}
85 #define PetscLogObjectCreate(h)      {if (_PetscLogPHC) (*_PetscLogPHC)((PetscObject)h);}
86 #define PetscLogObjectDestroy(h)     {if (_PetscLogPHD) (*_PetscLogPHD)((PetscObject)h);}
87 #define PetscLogObjectMemory(p,m)    {PetscValidHeader((PetscObject)p);((PetscObject)(p))->mem += (m);}
88 /* Initialization functions */
89 EXTERN int PetscLogBegin(void);
90 EXTERN int PetscLogAllBegin(void);
91 EXTERN int PetscLogTraceBegin(FILE *);
92 /* General functions */
93 EXTERN int PetscLogGetRGBColor(char **);
94 EXTERN int PetscLogDestroy(void);
95 EXTERN int PetscLogSet(int (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject),
96                    int (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
97 EXTERN int PetscLogObjectState(PetscObject, const char[], ...)  PETSC_PRINTF_FORMAT_CHECK(2,3);
98 /* Output functions */
99 EXTERN int PetscLogPrintSummary(MPI_Comm, const char[]);
100 EXTERN int PetscLogDump(const char[]);
101 /* Counter functions */
102 EXTERN int PetscGetFlops(PetscLogDouble *);
103 /* Stage functions */
104 EXTERN int PetscLogStageRegister(int *, const char[]);
105 EXTERN int PetscLogStagePush(int);
106 EXTERN int PetscLogStagePop(void);
107 EXTERN int PetscLogStageSetVisible(int, PetscTruth);
108 EXTERN int PetscLogStageGetVisible(int, PetscTruth *);
109 EXTERN int PetscLogStageGetId(const char [], int *);
110 /* Event functions */
111 EXTERN int PetscLogEventRegister(int *, const char[], int);
112 EXTERN int PetscLogEventActivate(int);
113 EXTERN int PetscLogEventDeactivate(int);
114 EXTERN int PetscLogEventActivateClass(int);
115 EXTERN int PetscLogEventDeactivateClass(int);
116 /* Class functions */
117 EXTERN int PetscLogClassRegister(int *, const char []);
118 
119 /* Global counters */
120 extern PetscLogDouble irecv_ct,  isend_ct,  recv_ct,  send_ct;
121 extern PetscLogDouble irecv_len, isend_len, recv_len, send_len;
122 extern PetscLogDouble allreduce_ct;
123 extern PetscLogDouble wait_ct, wait_any_ct, wait_all_ct, sum_of_waits_ct;
124 extern int            PETSC_DUMMY, PETSC_DUMMY_SIZE;
125 
126 /* We must make these structures available if we are to access the event
127    activation flags in the PetscLogEventBegin/End() macros. If we forced a
128    function call each time, we could leave these structures in plog.h
129 */
130 /* Default log */
131 typedef struct _StageLog *StageLog;
132 extern StageLog _stageLog;
133 
134 /* A simple stack (should replace) */
135 typedef struct _IntStack *IntStack;
136 
137 /* The structures for logging performance */
138 typedef struct _EventPerfInfo {
139   int            id;            /* The integer identifying this section */
140   PetscTruth     active;        /* The flag to activate logging */
141   PetscTruth     visible;       /* The flag to print info in summary */
142   int            depth;         /* The nesting depth of the event call */
143   int            count;         /* The number of times this section was executed */
144   PetscLogDouble flops;         /* The flops used in this section */
145   PetscLogDouble time;          /* The time taken for this section */
146   PetscLogDouble numMessages;   /* The number of messages in this section */
147   PetscLogDouble messageLength; /* The total message lengths in this section */
148   PetscLogDouble numReductions; /* The number of reductions in this section */
149 } EventPerfInfo;
150 
151 typedef struct _ClassPerfInfo {
152   int            id;           /* The integer identifying this class */
153   int            creations;    /* The number of objects of this class created */
154   int            destructions; /* The number of objects of this class destroyed */
155   PetscLogDouble mem;          /* The total memory allocated by objects of this class */
156   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects */
157 } ClassPerfInfo;
158 
159 /* The structures for logging registration */
160 typedef struct _ClassRegInfo {
161   char *name;   /* The class name */
162   int   cookie; /* The integer identifying this class */
163 } ClassRegInfo;
164 
165 typedef struct _EventRegInfo {
166   char *name;   /* The name of this event */
167   int   cookie; /* The class id for this event (should maybe give class ID instead) */
168 #if defined (PETSC_HAVE_MPE)
169   int   mpe_id_begin; /* MPE IDs that define the event */
170   int   mpe_id_end;
171 #endif
172 } EventRegInfo;
173 
174 /* The structure for logging events */
175 typedef int PetscEvent;
176 
177 typedef struct _EventRegLog *EventRegLog;
178 struct _EventRegLog {
179   int           numEvents; /* The number of registered events */
180   int           maxEvents; /* The maximum number of events */
181   EventRegInfo *eventInfo; /* The registration information for each event */
182 };
183 
184 typedef struct _EventPerfLog *EventPerfLog;
185 struct _EventPerfLog {
186   int            numEvents; /* The number of logging events */
187   int            maxEvents; /* The maximum number of events */
188   EventPerfInfo *eventInfo; /* The performance information for each event */
189 };
190 
191 /* The structure for logging class information */
192 typedef struct _ClassRegLog *ClassRegLog;
193 struct _ClassRegLog {
194   int           numClasses; /* The number of classes registered */
195   int           maxClasses; /* The maximum number of classes */
196   ClassRegInfo *classInfo;  /* The structure for class information (cookies are monotonicly increasing) */
197 };
198 
199 typedef struct _ClassPerfLog *ClassPerfLog;
200 struct _ClassPerfLog {
201   int            numClasses; /* The number of logging classes */
202   int            maxClasses; /* The maximum number of classes */
203   ClassPerfInfo *classInfo;  /* The structure for class information (cookies are monotonicly increasing) */
204 };
205 
206 /* The structures for logging in stages */
207 typedef struct _StageInfo {
208   char         *name;     /* The stage name */
209   EventPerfInfo perfInfo; /* The stage performance information */
210   EventPerfLog  eventLog; /* The event information for this stage */
211   ClassPerfLog  classLog; /* The class information for this stage */
212 } StageInfo;
213 
214 struct _StageLog {
215   /* Size information */
216   int         numStages; /* The number of registered stages */
217   int         maxStages; /* The maximum number of stages */
218   /* Runtime information */
219   IntStack    stack;     /* The stack for active stages */
220   int         curStage;  /* The current stage (only used in macros so we don't call StackTop) */
221   /* Stage specific information */
222   StageInfo  *stageInfo; /* The information for each stage */
223   EventRegLog eventLog;  /* The registered events */
224   ClassRegLog classLog;  /* The registered classes */
225 };
226 
227 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) 0; \
228 {\
229   int _2_ierr;\
230   if (_PetscLogPLB && _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) {\
231     _2_ierr = PetscLogEventBegin((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);\
232     _2_ierr = MPI_Barrier(cm);CHKERRQ(_2_ierr);\
233     _2_ierr = PetscLogEventEnd((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);\
234   }\
235   _2_ierr = PetscLogEventBegin((e)+1,o1,o2,o3,o4);CHKERRQ(_2_ierr);\
236 }
237 
238 #define PetscLogEventBegin(e,o1,o2,o3,o4) 0; \
239 {\
240   if (_PetscLogPLB && _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) {\
241     (*_PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));\
242   }\
243   PETSC_LOG_EVENT_MPE_BEGIN(e); \
244 }
245 
246 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm) PetscLogEventEnd(e+1,o1,o2,o3,o4)
247 
248 #define PetscLogEventEnd(e,o1,o2,o3,o4) 0; \
249 {\
250   if (_PetscLogPLE && _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) {\
251     (*_PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));\
252   }\
253   PETSC_LOG_EVENT_MPE_END(e); \
254 }
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(PETSC_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 /* !PETSC_HAVE_MPI_UNI && ! 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 PetscLogMPEBegin()         0
368 #define PetscLogMPEDump(a)         0
369 
370 #define PetscLogEventActivate(a)   0
371 #define PetscLogEventDeactivate(a) 0
372 
373 #define PetscLogEventActivateClass(a)   0
374 #define PetscLogEventDeactivateClass(a) 0
375 
376 #define _PetscLogPLB                        0
377 #define _PetscLogPLE                        0
378 #define _PetscLogPHC                        0
379 #define _PetscLogPHD                        0
380 #define PetscGetFlops(a)                (*(a) = 0.0,0)
381 #define PetscLogEventBegin(e,o1,o2,o3,o4)   0
382 #define PetscLogEventEnd(e,o1,o2,o3,o4)     0
383 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) 0
384 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm)   0
385 #define PetscLogObjectParent(p,c)
386 #define PetscLogObjectParents(p,n,c)
387 #define PetscLogObjectCreate(h)
388 #define PetscLogObjectDestroy(h)
389 #define PetscLogObjectMemory(p,m)
390 #define PetscLogDestroy()                   0
391 #define PetscLogStagePush(a)                0
392 #define PetscLogStagePop()                  0
393 #define PetscLogStageRegister(a,b)          0
394 #define PetscLogStagePrint(a,flg)           0
395 #define PetscLogPrintSummary(comm,file)     0
396 #define PetscLogBegin()                     0
397 #define PetscLogTraceBegin(file)            0
398 #define PetscLogSet(lb,le)                  0
399 #define PetscLogAllBegin()                  0
400 #define PetscLogDump(c)                     0
401 #define PetscLogEventRegister(a,b,c)        0
402 EXTERN int PetscLogObjectState(PetscObject,const char[],...) PETSC_PRINTF_FORMAT_CHECK(2,3);
403 
404 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
405 #define MPI_Startall_irecv(count,number,requests) MPI_Startall(number,requests)
406 #define MPI_Startall_isend(count,number,requests) MPI_Startall(number,requests)
407 #define MPI_Start_isend(count,requests) MPI_Start(requests)
408 
409 #endif   /* PETSC_USE_LOG */
410 
411 extern PetscTruth PetscPreLoadingUsed;       /* true if we are or have done preloading */
412 extern PetscTruth PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
413 
414 #define PreLoadBegin(flag,name) \
415 {\
416   PetscTruth PreLoading = flag;\
417   int        PreLoadMax,PreLoadIt,_stageNum,_3_ierr;\
418   _3_ierr = PetscOptionsGetLogical(PETSC_NULL,"-preload",&PreLoading,PETSC_NULL);CHKERRQ(_3_ierr);\
419   PreLoadMax = (int)(PreLoading);\
420   PetscPreLoadingUsed = PreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
421   for (PreLoadIt=0; PreLoadIt<=PreLoadMax; PreLoadIt++) {\
422     PetscPreLoadingOn = PreLoading;\
423     _3_ierr = PetscBarrier(PETSC_NULL);CHKERRQ(_3_ierr);\
424     if (PreLoadIt>0) {\
425       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
426     } else {\
427       _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
428       _3_ierr = PetscLogStageSetVisible(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
429     }\
430     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
431 
432 #define PreLoadEnd() \
433     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
434     PreLoading = PETSC_FALSE;\
435   }\
436 }
437 
438 #define PreLoadStage(name) \
439   _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
440   if (PreLoadIt>0) {\
441     _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
442   } else {\
443     _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
444     _3_ierr = PetscLogStageSetVisible(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
445   }\
446   _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
447 #endif
448