Date: 2026-05-01T06:12:05+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
     23: #if PetscDefined(HAVE_HIP)
     24: PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_ROCTX(PetscLogHandler);
     25: #endif

     27: static PetscErrorCode PetscLogHandlerRegisterAll(void)
     28: {
     29:   PetscFunctionBegin;
     30:   if (PetscLogHandlerRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
     31:   PetscLogHandlerRegisterAllCalled = PETSC_TRUE;
     32:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERDEFAULT, PetscLogHandlerCreate_Default));
     33:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERNESTED, PetscLogHandlerCreate_Nested));
     34:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERTRACE, PetscLogHandlerCreate_Trace));
     35: #if PetscDefined(HAVE_MPE)
     36:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERMPE, PetscLogHandlerCreate_MPE));
     37: #endif
     38: #if PetscDefined(HAVE_TAU_PERFSTUBS)
     39:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERPERFSTUBS, PetscLogHandlerCreate_Perfstubs));
     40: #endif
     41:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERLEGACY, PetscLogHandlerCreate_Legacy));
     42: #if PetscDefined(HAVE_CUDA)
     43:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERNVTX, PetscLogHandlerCreate_NVTX));
     44: #endif
     45: #if PetscDefined(HAVE_ROCTX)
     46:   PetscCall(PetscLogHandlerRegister(PETSCLOGHANDLERROCTX, PetscLogHandlerCreate_ROCTX));
     47: #endif
     48:   PetscFunctionReturn(PETSC_SUCCESS);
     49: }

     51: /*@C
     52:   PetscLogHandlerRegister - Register a new `PetscLogHandler`

     54:   Not Collective, No Fortran Support

     56:   Input Parameters:
     57: + sname    - The name of a new user-defined creation routine
     58: - function - The creation routine

     60:   Example Usage:
     61: .vb
     62:     PetscLogHandlerRegister("my_profiler", MyPetscLogHandlerCreate);
     63: .ve

     65:   Then, your `PetscLogHandler` type can be chosen with the procedural interface via
     66: .vb
     67:     PetscLogHandlerCreate(MPI_Comm, PetscLogHandler *);
     68:     PetscLogHandlerSetType(PetscFE, "my_fe");
     69: .ve

     71:   Level: developer

     73: .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerSetType()`, `PetscLogHandlerGetType()`
     74: @*/
     75: PetscErrorCode PetscLogHandlerRegister(const char sname[], PetscErrorCode (*function)(PetscLogHandler))
     76: {
     77:   PetscFunctionBegin;
     78:   PetscCall(PetscFunctionListAdd(&PetscLogHandlerList, sname, function));
     79:   PetscFunctionReturn(PETSC_SUCCESS);
     80: }

     82: /*@C
     83:   PetscLogHandlerSetType - Set the type of a `PetscLogHandler`

     85:   Input Parameters:
     86: + handler - the `PetscLogHandler`
     87: - name    - The kind of log handler

     89:   Level: developer

     91: .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerGetType()`
     92: @*/
     93: PetscErrorCode PetscLogHandlerSetType(PetscLogHandler handler, PetscLogHandlerType name)
     94: {
     95:   PetscErrorCode (*r)(PetscLogHandler);
     96:   PetscBool match;

     98:   PetscFunctionBegin;
    100:   PetscCall(PetscObjectTypeCompare((PetscObject)handler, name, &match));
    101:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

    103:   PetscCall(PetscLogHandlerRegisterAll());
    104:   PetscCall(PetscFunctionListFind(PetscLogHandlerList, name, &r));
    105:   PetscCheck(r, PetscObjectComm((PetscObject)handler), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscLogHandler type: %s", name);

    107:   PetscTryTypeMethod(handler, destroy);
    108:   handler->ops->destroy = NULL;

    110:   PetscCall((*r)(handler));
    111:   PetscCall(PetscObjectChangeTypeName((PetscObject)handler, name));
    112:   PetscFunctionReturn(PETSC_SUCCESS);
    113: }

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

    118:   Not collective

    120:   Input Parameter:
    121: . handler - the `PetscLogHandler`

    123:   Output Parameter:
    124: . name - The `PetscLogHandlerType` name

    126:   Level: developer

    128: .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerSetType()`
    129: @*/
    130: PetscErrorCode PetscLogHandlerGetType(PetscLogHandler handler, PetscLogHandlerType *name)
    131: {
    132:   PetscFunctionBegin;
    134:   PetscAssertPointer(name, 2);
    135:   PetscCall(PetscLogHandlerRegisterAll());
    136:   PetscCall(PetscObjectGetType((PetscObject)handler, name));
    137:   PetscFunctionReturn(PETSC_SUCCESS);
    138: }

    140: static PetscErrorCode PetscLogHandlerFinalizePackage(void)
    141: {
    142:   PetscFunctionBegin;
    143:   PetscCall(PetscFunctionListDestroy(&PetscLogHandlerList));
    144:   PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
    145:   PetscLogHandlerPackageInitialized = PETSC_FALSE;
    146:   PetscFunctionReturn(PETSC_SUCCESS);
    147: }

    149: PetscErrorCode PetscLogHandlerPackageInitialize(void)
    150: {
    151:   PetscFunctionBegin;
    152:   if (PetscLogHandlerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
    153:   PetscLogHandlerPackageInitialized = PETSC_TRUE;

    155:   PetscCall(PetscClassIdRegister("PETSc Log Handler", &PETSCLOGHANDLER_CLASSID));
    156:   PetscCall(PetscLogHandlerRegisterAll());
    157:   PetscCall(PetscRegisterFinalize(PetscLogHandlerFinalizePackage));
    158:   {
    159:     const PetscClassId classids[] = {PETSCLOGHANDLER_CLASSID};

    161:     PetscCall(PetscInfoProcessClass("loghandler", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
    162:   }
    163:   PetscFunctionReturn(PETSC_SUCCESS);
    164: }