Date: 2025-03-29T05:58:44+00:00

Actual source code: lhreg.c

      1: #include <petsc/private/petscimpl.h>
      2: #include <petsc/private/loghandlerimpl.h>

      4: PetscClassId PETSCLOGHANDLER_CLASSID = 0;

      6: PetscFunctionList PetscLogHandlerList               = NULL;
      7: PetscBool         PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
      8: PetscBool         PetscLogHandlerPackageInitialized = PETSC_FALSE;

     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

     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: }

     45: /*@C
     46:   PetscLogHandlerRegister - Register a new `PetscLogHandler`

     48:   Not Collective, No Fortran Support

     50:   Input Parameters:
     51: + sname    - The name of a new user-defined creation routine
     52: - function - The creation routine

     54:   Example Usage:
     55: .vb
     56:     PetscLogHandlerRegister("my_profiler", MyPetscLogHandlerCreate);
     57: .ve

     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

     65:   Level: developer

     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: }

     76: /*@C
     77:   PetscLogHandlerSetType - Set the type of a `PetscLogHandler`

     79:   Input Parameters:
     80: + handler - the `PetscLogHandler`
     81: - name    - The kind of log handler

     83:   Level: developer

     85: .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerGetType()`
     86: @*/
     87: PetscErrorCode PetscLogHandlerSetType(PetscLogHandler handler, PetscLogHandlerType name)
     88: {
     89:   PetscErrorCode (*r)(PetscLogHandler);
     90:   PetscBool match;

     92:   PetscFunctionBegin;
     94:   PetscCall(PetscObjectTypeCompare((PetscObject)handler, name, &match));
     95:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

     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);

    101:   PetscTryTypeMethod(handler, destroy);
    102:   handler->ops->destroy = NULL;

    104:   PetscCall((*r)(handler));
    105:   PetscCall(PetscObjectChangeTypeName((PetscObject)handler, name));
    106:   PetscFunctionReturn(PETSC_SUCCESS);
    107: }

    109: /*@C
    110:   PetscLogHandlerGetType - Gets the `PetscLoagHandlerType` (as a string) from the `PetscLogHandler` object.

    112:   Not collective

    114:   Input Parameter:
    115: . handler - the `PetscLogHandler`

    117:   Output Parameter:
    118: . name - The `PetscLogHandlerType` name

    120:   Level: developer

    122: .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerSetType()`
    123: @*/
    124: PetscErrorCode PetscLogHandlerGetType(PetscLogHandler handler, PetscLogHandlerType *name)
    125: {
    126:   PetscFunctionBegin;
    128:   PetscAssertPointer(name, 2);
    129:   PetscCall(PetscLogHandlerRegisterAll());
    130:   PetscCall(PetscObjectGetType((PetscObject)handler, name));
    131:   PetscFunctionReturn(PETSC_SUCCESS);
    132: }

    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: }

    143: PetscErrorCode PetscLogHandlerPackageInitialize(void)
    144: {
    145:   PetscFunctionBegin;
    146:   if (PetscLogHandlerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
    147:   PetscLogHandlerPackageInitialized = PETSC_TRUE;

    149:   PetscCall(PetscClassIdRegister("Petsc Log Handler", &PETSCLOGHANDLER_CLASSID));
    150:   PetscCall(PetscLogHandlerRegisterAll());
    151:   PetscCall(PetscRegisterFinalize(PetscLogHandlerFinalizePackage));
    152:   {
    153:     const PetscClassId classids[] = {PETSCLOGHANDLER_CLASSID};

    155:     PetscCall(PetscInfoProcessClass("loghandler", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
    156:   }
    157:   PetscFunctionReturn(PETSC_SUCCESS);
    158: }