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