xref: /petsc/src/sys/logging/handler/interface/lhreg.c (revision ca0d62ff0b92f3b3ec10dd93d500c979243edb4e)
1 
2 #include <petsc/private/petscimpl.h>
3 #include <petsc/private/loghandlerimpl.h>
4 
5 PetscClassId PETSCLOGHANDLER_CLASSID = 0;
6 
7 PetscFunctionList PetscLogHandlerList               = NULL;
8 PetscBool         PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
9 PetscBool         PetscLogHandlerPackageInitialized = PETSC_FALSE;
10 
11 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Default(PetscLogHandler);
12 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Nested(PetscLogHandler);
13 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Trace(PetscLogHandler);
14 #if PetscDefined(HAVE_MPE)
15 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_MPE(PetscLogHandler);
16 #endif
17 #if PetscDefined(HAVE_TAU_PERFSTUBS)
18 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Perfstubs(PetscLogHandler);
19 #endif
20 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Legacy(PetscLogHandler);
21 #if PetscDefined(HAVE_CUDA)
22 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_NVTX(PetscLogHandler);
23 #endif
24 
25 static PetscErrorCode PetscLogHandlerRegisterAll(void)
26 {
27   PetscFunctionBegin;
28   if (PetscLogHandlerRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
29   PetscLogHandlerRegisterAllCalled = PETSC_TRUE;
30   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERDEFAULT, PetscLogHandlerCreate_Default));
31   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERNESTED, PetscLogHandlerCreate_Nested));
32   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERTRACE, PetscLogHandlerCreate_Trace));
33 #if PetscDefined(HAVE_MPE)
34   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERMPE, PetscLogHandlerCreate_MPE));
35 #endif
36 #if PetscDefined(HAVE_TAU_PERFSTUBS)
37   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERPERFSTUBS, PetscLogHandlerCreate_Perfstubs));
38 #endif
39   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERLEGACY, PetscLogHandlerCreate_Legacy));
40 #if PetscDefined(HAVE_CUDA)
41   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERNVTX, PetscLogHandlerCreate_NVTX));
42 #endif
43   PetscFunctionReturn(PETSC_SUCCESS);
44 }
45 
46 /*@C
47   PetscLogHandlerRegister - Register a new `PetscLogHandler`
48 
49   Not collective
50 
51   Input Parameters:
52 + sname    - The name of a new user-defined creation routine
53 - function - The creation routine
54 
55   Example Usage:
56 .vb
57     PetscLogHandlerRegister("my_profiler", MyPetscLogHandlerCreate);
58 .ve
59 
60   Then, your `PetscLogHandler` type can be chosen with the procedural interface via
61 .vb
62     PetscLogHandlerCreate(MPI_Comm, PetscLogHandler *);
63     PetscLogHandlerSetType(PetscFE, "my_fe");
64 .ve
65 
66   Level: developer
67 
68 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerSetType()`, `PetscLogHandlerGetType()`
69 @*/
70 PetscErrorCode PetscLogHandlerRegister(const char sname[], PetscErrorCode (*function)(PetscLogHandler))
71 {
72   PetscFunctionBegin;
73   PetscCall(PetscFunctionListAdd(&PetscLogHandlerList, sname, function));
74   PetscFunctionReturn(PETSC_SUCCESS);
75 }
76 
77 /*@C
78   PetscLogHandlerSetType - Set the type of a `PetscLogHandler`
79 
80   Input Parameters:
81 + handler - the `PetscLogHandler`
82 - name    - The kind of log handler
83 
84   Level: developer
85 
86 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerGetType()`
87 @*/
88 PetscErrorCode PetscLogHandlerSetType(PetscLogHandler handler, PetscLogHandlerType name)
89 {
90   PetscErrorCode (*r)(PetscLogHandler);
91   PetscBool match;
92 
93   PetscFunctionBegin;
94   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
95   PetscCall(PetscObjectTypeCompare((PetscObject)handler, name, &match));
96   if (match) PetscFunctionReturn(PETSC_SUCCESS);
97 
98   PetscCall(PetscLogHandlerRegisterAll());
99   PetscCall(PetscFunctionListFind(PetscLogHandlerList, name, &r));
100   PetscCheck(r, PetscObjectComm((PetscObject)handler), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscLogHandler type: %s", name);
101 
102   PetscTryTypeMethod(handler, destroy);
103   handler->ops->destroy = NULL;
104 
105   PetscCall((*r)(handler));
106   PetscCall(PetscObjectChangeTypeName((PetscObject)handler, name));
107   PetscFunctionReturn(PETSC_SUCCESS);
108 }
109 
110 /*@C
111   PetscLogHandlerGetType - Gets the `PetscLoagHandlerType` (as a string) from the `PetscLogHandler` object.
112 
113   Not collective
114 
115   Input Parameter:
116 . handler - the `PetscLogHandler`
117 
118   Output Parameter:
119 . name - The `PetscLogHandlerType` name
120 
121   Level: developer
122 
123 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerSetType()`
124 @*/
125 PetscErrorCode PetscLogHandlerGetType(PetscLogHandler handler, PetscLogHandlerType *name)
126 {
127   PetscFunctionBegin;
128   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
129   PetscAssertPointer(name, 2);
130   PetscCall(PetscLogHandlerRegisterAll());
131   PetscCall(PetscObjectGetType((PetscObject)handler, name));
132   PetscFunctionReturn(PETSC_SUCCESS);
133 }
134 
135 static PetscErrorCode PetscLogHandlerFinalizePackage(void)
136 {
137   PetscFunctionBegin;
138   PetscCall(PetscFunctionListDestroy(&PetscLogHandlerList));
139   PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
140   PetscLogHandlerPackageInitialized = PETSC_FALSE;
141   PetscFunctionReturn(PETSC_SUCCESS);
142 }
143 
144 PetscErrorCode PetscLogHandlerPackageInitialize(void)
145 {
146   PetscFunctionBegin;
147   if (PetscLogHandlerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
148   PetscLogHandlerPackageInitialized = PETSC_TRUE;
149 
150   PetscCall(PetscClassIdRegister("Petsc Log Handler", &PETSCLOGHANDLER_CLASSID));
151   PetscCall(PetscLogHandlerRegisterAll());
152   PetscCall(PetscRegisterFinalize(PetscLogHandlerFinalizePackage));
153   {
154     const PetscClassId classids[] = {PETSCLOGHANDLER_CLASSID};
155 
156     PetscCall(PetscInfoProcessClass("loghandler", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
157   }
158   PetscFunctionReturn(PETSC_SUCCESS);
159 }
160