Actual source code: logimpl.h

  1: #pragma once

  3: #include <petsc/private/petscimpl.h>

  5: /* --- Macros for resizable arrays that show up frequently in the implementation of logging --- */

  7: #define PETSC_LOG_RESIZABLE_ARRAY(Container, Entry, Key, Constructor, Destructor, Equal) \
  8:   typedef struct _n_PetscLog##Container    *PetscLog##Container; \
  9:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Create(int, PetscLog##Container *); \
 10:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Destroy(PetscLog##Container *); \
 11:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Recapacity(PetscLog##Container, int); \
 12:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Resize(PetscLog##Container, int); \
 13:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Push(PetscLog##Container, Entry); \
 14:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Find(PetscLog##Container, Key, int *); \
 15:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##GetSize(PetscLog##Container, PetscInt *, PetscInt *); \
 16:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Get(PetscLog##Container, PetscInt, Entry *); \
 17:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##GetRef(PetscLog##Container, PetscInt, Entry **); \
 18:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Set(PetscLog##Container, PetscInt, Entry); \
 19:   struct _n_PetscLog##Container { \
 20:     int    num_entries; \
 21:     int    max_entries; \
 22:     Entry *array; \
 23:   }; \
 24:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Create(int max_init, PetscLog##Container *a_p) \
 25:   { \
 26:     PetscLog##Container a; \
 27:     PetscErrorCode (*constructor)(Entry *) = Constructor; \
 28:     PetscFunctionBegin; \
 29:     PetscCall(PetscNew(a_p)); \
 30:     a              = *a_p; \
 31:     a->num_entries = 0; \
 32:     a->max_entries = max_init; \
 33:     if (constructor) { \
 34:       PetscCall(PetscMalloc1(max_init, &a->array)); \
 35:     } else { \
 36:       PetscCall(PetscCalloc1(max_init, &a->array)); \
 37:     } \
 38:     PetscFunctionReturn(PETSC_SUCCESS); \
 39:   } \
 40:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Destroy(PetscLog##Container *a_p) \
 41:   { \
 42:     PetscLog##Container a; \
 43:     PetscErrorCode (*destructor)(Entry *) = Destructor; \
 44:     PetscFunctionBegin; \
 45:     a    = *a_p; \
 46:     *a_p = NULL; \
 47:     if (a == NULL) PetscFunctionReturn(PETSC_SUCCESS); \
 48:     if (destructor) { \
 49:       for (int i = 0; i < a->num_entries; i++) PetscCall((*destructor)(&a->array[i])); \
 50:     } \
 51:     PetscCall(PetscFree(a->array)); \
 52:     PetscCall(PetscFree(a)); \
 53:     PetscFunctionReturn(PETSC_SUCCESS); \
 54:   } \
 55:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Recapacity(PetscLog##Container a, int new_size) \
 56:   { \
 57:     PetscErrorCode (*constructor)(Entry *) = Constructor; \
 58:     PetscFunctionBegin; \
 59:     if (new_size > a->max_entries) { \
 60:       int    new_max_entries = 2; \
 61:       int    rem_size        = PetscMax(0, new_size - 1); \
 62:       Entry *new_array; \
 63:       while (rem_size >>= 1) new_max_entries *= 2; \
 64:       if (constructor) { \
 65:         PetscCall(PetscMalloc1(new_max_entries, &new_array)); \
 66:       } else { \
 67:         PetscCall(PetscCalloc1(new_max_entries, &new_array)); \
 68:       } \
 69:       PetscCall(PetscArraycpy(new_array, a->array, a->num_entries)); \
 70:       PetscCall(PetscFree(a->array)); \
 71:       a->array       = new_array; \
 72:       a->max_entries = new_max_entries; \
 73:     } \
 74:     PetscFunctionReturn(PETSC_SUCCESS); \
 75:   } \
 76:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Resize(PetscLog##Container a, int new_size) \
 77:   { \
 78:     PetscErrorCode (*constructor)(Entry *) = Constructor; \
 79:     PetscFunctionBegin; \
 80:     PetscCall(PetscLog##Container##Recapacity(a, new_size)); \
 81:     if (constructor) \
 82:       for (int i = a->num_entries; i < new_size; i++) PetscCall((*constructor)(&a->array[i])); \
 83:     a->num_entries = PetscMax(a->num_entries, new_size); \
 84:     PetscFunctionReturn(PETSC_SUCCESS); \
 85:   } \
 86:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Push(PetscLog##Container a, Entry new_entry) \
 87:   { \
 88:     PetscFunctionBegin; \
 89:     PetscCall(PetscLog##Container##Recapacity(a, a->num_entries + 1)); \
 90:     a->array[a->num_entries++] = new_entry; \
 91:     PetscFunctionReturn(PETSC_SUCCESS); \
 92:   } \
 93:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Find(PetscLog##Container a, Key key, int *idx_p) \
 94:   { \
 95:     PetscErrorCode (*equal)(Entry *, Key, PetscBool *) = Equal; \
 96:     PetscFunctionBegin; \
 97:     *idx_p = -1; \
 98:     if (equal) { \
 99:       for (int i = 0; i < a->num_entries; i++) { \
100:         PetscBool is_equal; \
101:         PetscCall((*equal)(&a->array[i], key, &is_equal)); \
102:         if (is_equal) { \
103:           *idx_p = i; \
104:           break; \
105:         } \
106:       } \
107:     } \
108:     PetscFunctionReturn(PETSC_SUCCESS); \
109:   } \
110:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##GetSize(PetscLog##Container a, PetscInt *num_entries, PetscInt *max_entries) \
111:   { \
112:     PetscFunctionBegin; \
113:     if (num_entries) *num_entries = a->num_entries; \
114:     if (max_entries) *max_entries = a->max_entries; \
115:     PetscFunctionReturn(PETSC_SUCCESS); \
116:   } \
117:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Get(PetscLog##Container a, PetscInt i, Entry *entry) \
118:   { \
119:     PetscFunctionBegin; \
120:     PetscCheck(i >= 0 && i < a->num_entries, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Index %" PetscInt_FMT " is not in range [0,%d)", i, a->num_entries); \
121:     *entry = a->array[i]; \
122:     PetscFunctionReturn(PETSC_SUCCESS); \
123:   } \
124:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##GetRef(PetscLog##Container a, PetscInt i, Entry **entry) \
125:   { \
126:     PetscFunctionBegin; \
127:     PetscCheck(i >= 0 && i < a->num_entries, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Index %" PetscInt_FMT " is not in range [0,%d)", i, a->num_entries); \
128:     *entry = &a->array[i]; \
129:     PetscFunctionReturn(PETSC_SUCCESS); \
130:   } \
131:   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Set(PetscLog##Container a, PetscInt i, Entry entry) \
132:   { \
133:     PetscFunctionBegin; \
134:     PetscCheck(i >= 0 && i < a->num_entries, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Index %" PetscInt_FMT " is not in range [0,%d)", i, a->num_entries); \
135:     a->array[i] = entry; \
136:     PetscFunctionReturn(PETSC_SUCCESS); \
137:   }

139: /* --- the registry: information about registered things ---

141:    Log handler instances should not change the registry: it is shared
142:    data that should be useful to more than one type of logging

144:  */

146: PETSC_INTERN PetscErrorCode PetscLogRegistryCreate(PetscLogRegistry *);
147: PETSC_INTERN PetscErrorCode PetscLogRegistryDestroy(PetscLogRegistry);
148: PETSC_INTERN PetscErrorCode PetscLogRegistryStageRegister(PetscLogRegistry, const char[], PetscLogStage *);
149: PETSC_INTERN PetscErrorCode PetscLogRegistryEventRegister(PetscLogRegistry, const char[], PetscClassId, PetscLogStage *);
150: PETSC_INTERN PetscErrorCode PetscLogRegistryClassRegister(PetscLogRegistry, const char[], PetscClassId, PetscLogClass *);
151: PETSC_INTERN PetscErrorCode PetscLogRegistryGetEventFromName(PetscLogRegistry, const char[], PetscLogEvent *);
152: PETSC_INTERN PetscErrorCode PetscLogRegistryGetStageFromName(PetscLogRegistry, const char[], PetscLogStage *);
153: PETSC_INTERN PetscErrorCode PetscLogRegistryGetClassFromClassId(PetscLogRegistry, PetscClassId, PetscLogClass *);
154: PETSC_INTERN PetscErrorCode PetscLogRegistryGetClassFromName(PetscLogRegistry, const char[], PetscLogClass *);
155: PETSC_INTERN PetscErrorCode PetscLogRegistryGetNumEvents(PetscLogRegistry, PetscInt *, PetscInt *);
156: PETSC_INTERN PetscErrorCode PetscLogRegistryGetNumStages(PetscLogRegistry, PetscInt *, PetscInt *);
157: PETSC_INTERN PetscErrorCode PetscLogRegistryGetNumClasses(PetscLogRegistry, PetscInt *, PetscInt *);
158: PETSC_INTERN PetscErrorCode PetscLogRegistryEventGetInfo(PetscLogRegistry, PetscLogEvent, PetscLogEventInfo *);
159: PETSC_INTERN PetscErrorCode PetscLogRegistryStageGetInfo(PetscLogRegistry, PetscLogStage, PetscLogStageInfo *);
160: PETSC_INTERN PetscErrorCode PetscLogRegistryClassGetInfo(PetscLogRegistry, PetscLogClass, PetscLogClassInfo *);
161: PETSC_INTERN PetscErrorCode PetscLogRegistryEventSetCollective(PetscLogRegistry, PetscLogEvent, PetscBool);

163: /* --- globally synchronized registry information --- */

165: typedef struct _n_PetscLogGlobalNames *PetscLogGlobalNames;

167: PETSC_INTERN PetscErrorCode PetscLogGlobalNamesCreate(MPI_Comm, PetscInt, const char **, PetscLogGlobalNames *);
168: PETSC_INTERN PetscErrorCode PetscLogGlobalNamesDestroy(PetscLogGlobalNames *);
169: PETSC_INTERN PetscErrorCode PetscLogGlobalNamesGetSize(PetscLogGlobalNames, PetscInt *, PetscInt *);
170: PETSC_INTERN PetscErrorCode PetscLogGlobalNamesGlobalGetName(PetscLogGlobalNames, PetscInt, const char *[]);
171: PETSC_INTERN PetscErrorCode PetscLogGlobalNamesGlobalGetLocal(PetscLogGlobalNames, PetscInt, PetscInt *);
172: PETSC_INTERN PetscErrorCode PetscLogGlobalNamesLocalGetGlobal(PetscLogGlobalNames, PetscInt, PetscInt *);
173: PETSC_INTERN PetscErrorCode PetscLogRegistryCreateGlobalStageNames(MPI_Comm, PetscLogRegistry, PetscLogGlobalNames *);
174: PETSC_INTERN PetscErrorCode PetscLogRegistryCreateGlobalEventNames(MPI_Comm, PetscLogRegistry, PetscLogGlobalNames *);

176: /* A simple stack */
177: struct _n_PetscIntStack {
178:   int  top;   /* The top of the stack */
179:   int  max;   /* The maximum stack size */
180:   int *stack; /* The storage */
181: };

183: /* Thread-safety internals */

185: /* SpinLock for shared Log variables */
186: PETSC_INTERN PetscSpinlock PetscLogSpinLock;

188: #if PetscDefined(HAVE_THREADSAFETY)
189: /* Access PETSc internal thread id */
190: PETSC_INTERN PetscInt PetscLogGetTid(void);
191: #endif

193: PETSC_EXTERN PetscBool PetscLogGpuTimeFlag;
194: PETSC_EXTERN PetscBool PetscLogGpuEnergyFlag;
195: PETSC_EXTERN PetscBool PetscLogGpuEnergyMeterFlag;
196: PETSC_INTERN PetscInt  PetscLogNumViewersCreated;
197: PETSC_INTERN PetscInt  PetscLogNumViewersDestroyed;

199: #if PetscDefined(USE_LOG)
200: PETSC_INTERN PetscErrorCode PetscLogTypeBegin(PetscLogHandlerType type);
201: #else
202:   #define PetscLogTypeBegin(t) ((void)(t), PETSC_SUCCESS)
203: #endif

205: #define PETSC_LOG_VIEW_FROM_OPTIONS_MAX 4

207: #include <petsc/private/logimpldeprecated.h>