127d4645fSToby Isaac #include <petscdmforest.h>
2d02705aaSToby Isaac #include <petsc/private/petscimpl.h>
3d02705aaSToby Isaac #include "petsc_p4est_package.h"
4cd375237SToby Isaac
5ea78f98cSLisandro Dalcin static const char *const SCLogTypes[] = {"DEFAULT", "ALWAYS", "TRACE", "DEBUG", "VERBOSE", "INFO", "STATISTICS", "PRODUCTION", "ESSENTIAL", "ERROR", "SILENT", "SCLogTypes", "SC_LP_", NULL};
6cd375237SToby Isaac
7cd375237SToby Isaac static PetscBool PetscP4estInitialized = PETSC_FALSE;
8cd375237SToby Isaac static PetscBool PetscBeganSc = PETSC_FALSE;
9d02705aaSToby Isaac static PetscClassId P4ESTLOGGING_CLASSID;
10d02705aaSToby Isaac
11d02705aaSToby Isaac PetscObject P4estLoggingObject; /* Just a vehicle for its classid */
12cd375237SToby Isaac
PetscScLogHandler(FILE * log_stream,const char * filename,int lineno,int package,int category,int priority,const char * msg)13d71ae5a4SJacob Faibussowitsch static void PetscScLogHandler(FILE *log_stream, const char *filename, int lineno, int package, int category, int priority, const char *msg)
14d71ae5a4SJacob Faibussowitsch {
1588dbf2f5SPierre Jolivet PetscCallVoid(PetscInfo_Private(filename, P4estLoggingObject, ":%d{%s} %s", lineno, package == sc_package_id ? "sc" : package == p4est_package_id ? "p4est" : "", msg));
16cd375237SToby Isaac }
17cd375237SToby Isaac
18cd375237SToby Isaac /* p4est tries to abort: if possible, use setjmp to enable at least a little unwinding */
198f3f2bf1SBarry Smith #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_USE_DEBUG)
20cd375237SToby Isaac #include <setjmp.h>
21cd375237SToby Isaac PETSC_VISIBILITY_INTERNAL jmp_buf PetscScJumpBuf;
PetscScAbort_longjmp(void)22d71ae5a4SJacob Faibussowitsch PETSC_INTERN void PetscScAbort_longjmp(void)
23d71ae5a4SJacob Faibussowitsch {
2488dbf2f5SPierre Jolivet PetscErrorCode ierr = PetscError(PETSC_COMM_SELF, -1, "p4est function", "p4est file", PETSC_ERR_LIB, PETSC_ERROR_INITIAL, "Error in p4est stack call\n");
2588dbf2f5SPierre Jolivet (void)ierr;
26cd375237SToby Isaac longjmp(PetscScJumpBuf, 1);
27cd375237SToby Isaac return;
28cd375237SToby Isaac }
29cd375237SToby Isaac
30cd375237SToby Isaac #define PetscScAbort PetscScAbort_longjmp
31cd375237SToby Isaac #else
32cd375237SToby Isaac #define PetscScAbort NULL
33cd375237SToby Isaac #endif
34cd375237SToby Isaac
PetscP4estFinalize(void)35d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscP4estFinalize(void)
36d71ae5a4SJacob Faibussowitsch {
37cd375237SToby Isaac PetscFunctionBegin;
38cd375237SToby Isaac if (PetscBeganSc) {
39*f0b74427SPierre Jolivet /* We do not want libsc to abort on a mismatched allocation and prevent further PETSc unwinding */
40792fecdfSBarry Smith PetscCallP4est(sc_package_set_abort_alloc_mismatch, (sc_package_id, 0));
41792fecdfSBarry Smith PetscCallP4est(sc_package_set_abort_alloc_mismatch, (p4est_package_id, 0));
42792fecdfSBarry Smith PetscCallP4est(sc_package_set_abort_alloc_mismatch, (-1, 0));
43792fecdfSBarry Smith PetscCallP4est(sc_finalize, ());
44cd375237SToby Isaac }
459566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(&P4estLoggingObject));
463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
47cd375237SToby Isaac }
48cd375237SToby Isaac
PetscP4estInitialize(void)49d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscP4estInitialize(void)
50d71ae5a4SJacob Faibussowitsch {
51fc38d178SToby Isaac PetscBool psc_catch_signals = PETSC_FALSE;
52cd375237SToby Isaac PetscBool psc_print_backtrace = PETSC_TRUE;
53cd375237SToby Isaac int psc_log_threshold = SC_LP_DEFAULT;
54cd375237SToby Isaac int pp4est_log_threshold = SC_LP_DEFAULT;
558e81d068SLisandro Dalcin char logList[256];
568e81d068SLisandro Dalcin PetscBool opt, pkg;
57cd375237SToby Isaac
58cd375237SToby Isaac PetscFunctionBegin;
593ba16761SJacob Faibussowitsch if (PetscP4estInitialized) PetscFunctionReturn(PETSC_SUCCESS);
60cd375237SToby Isaac PetscP4estInitialized = PETSC_TRUE;
61f885a11aSToby Isaac
628e81d068SLisandro Dalcin /* Register Classes */
639566063dSJacob Faibussowitsch PetscCall(PetscClassIdRegister("p4est logging", &P4ESTLOGGING_CLASSID));
64e94e781bSJacob Faibussowitsch /* Process Info */
65e94e781bSJacob Faibussowitsch {
66e94e781bSJacob Faibussowitsch PetscClassId classids[1];
67e94e781bSJacob Faibussowitsch
68e94e781bSJacob Faibussowitsch classids[0] = P4ESTLOGGING_CLASSID;
699566063dSJacob Faibussowitsch PetscCall(PetscInfoProcessClass("p4est", 1, classids));
70d02705aaSToby Isaac }
718e81d068SLisandro Dalcin /* Process summary exclusions */
729566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
738e81d068SLisandro Dalcin if (opt) {
749566063dSJacob Faibussowitsch PetscCall(PetscStrInList("p4est", logList, ',', &pkg));
759566063dSJacob Faibussowitsch if (pkg) PetscCall(PetscLogEventExcludeClass(P4ESTLOGGING_CLASSID));
76d02705aaSToby Isaac }
779566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(P4estLoggingObject, P4ESTLOGGING_CLASSID, "p4est", "p4est logging", "DM", PETSC_COMM_WORLD, NULL, PetscObjectView));
78cd375237SToby Isaac if (sc_package_id == -1) {
79cd375237SToby Isaac int log_threshold_shifted = psc_log_threshold + 1;
80cd375237SToby Isaac PetscBool set;
8166c0a4b5SToby Isaac #if defined(PETSC_HAVE_MPIUNI)
8266c0a4b5SToby Isaac sc_MPI_Comm comm_world = sc_MPI_COMM_WORLD;
8366c0a4b5SToby Isaac #else
8466c0a4b5SToby Isaac MPI_Comm comm_world = PETSC_COMM_WORLD;
8566c0a4b5SToby Isaac #endif
86cd375237SToby Isaac
87cd375237SToby Isaac PetscBeganSc = PETSC_TRUE;
889566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-petsc_sc_catch_signals", &psc_catch_signals, NULL));
899566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-petsc_sc_print_backtrace", &psc_print_backtrace, NULL));
909566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetEnum(NULL, NULL, "-petsc_sc_log_threshold", SCLogTypes, (PetscEnum *)&log_threshold_shifted, &set));
91f885a11aSToby Isaac if (set) psc_log_threshold = log_threshold_shifted - 1;
9266c0a4b5SToby Isaac sc_init(comm_world, (int)psc_catch_signals, (int)psc_print_backtrace, PetscScLogHandler, psc_log_threshold);
9308401ef6SPierre Jolivet PetscCheck(sc_package_id != -1, PETSC_COMM_WORLD, PETSC_ERR_LIB, "Could not initialize libsc package used by p4est");
94cd375237SToby Isaac sc_set_abort_handler(PetscScAbort);
95cd375237SToby Isaac }
96cd375237SToby Isaac if (p4est_package_id == -1) {
97cd375237SToby Isaac int log_threshold_shifted = pp4est_log_threshold + 1;
98cd375237SToby Isaac PetscBool set;
99cd375237SToby Isaac
1009566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetEnum(NULL, NULL, "-petsc_p4est_log_threshold", SCLogTypes, (PetscEnum *)&log_threshold_shifted, &set));
101f885a11aSToby Isaac if (set) pp4est_log_threshold = log_threshold_shifted - 1;
102792fecdfSBarry Smith PetscCallP4est(p4est_init, (PetscScLogHandler, pp4est_log_threshold));
10308401ef6SPierre Jolivet PetscCheck(p4est_package_id != -1, PETSC_COMM_WORLD, PETSC_ERR_LIB, "Could not initialize p4est");
104cd375237SToby Isaac }
1059566063dSJacob Faibussowitsch PetscCall(DMForestRegisterType(DMP4EST));
1069566063dSJacob Faibussowitsch PetscCall(DMForestRegisterType(DMP8EST));
1079566063dSJacob Faibussowitsch PetscCall(PetscRegisterFinalize(PetscP4estFinalize));
1083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
109cd375237SToby Isaac }
110