1 #include <petscdmforest.h> 2 #include <petsc/private/petscimpl.h> 3 #include "petsc_p4est_package.h" 4 5 static const char *const SCLogTypes[] = {"DEFAULT", "ALWAYS", "TRACE", "DEBUG", "VERBOSE", "INFO", "STATISTICS", "PRODUCTION", "ESSENTIAL", "ERROR", "SILENT", "SCLogTypes", "SC_LP_", NULL}; 6 7 static PetscBool PetscP4estInitialized = PETSC_FALSE; 8 static PetscBool PetscBeganSc = PETSC_FALSE; 9 static PetscClassId P4ESTLOGGING_CLASSID; 10 11 PetscObject P4estLoggingObject; /* Just a vehicle for its classid */ 12 13 static void PetscScLogHandler(FILE *log_stream, const char *filename, int lineno, int package, int category, int priority, const char *msg) { 14 PetscInfo_Private(filename, P4estLoggingObject, ":%d{%s} %s", lineno, package == sc_package_id ? "sc" : package == p4est_package_id ? "p4est" : "", msg); 15 } 16 17 /* p4est tries to abort: if possible, use setjmp to enable at least a little unwinding */ 18 #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_USE_DEBUG) 19 #include <setjmp.h> 20 PETSC_VISIBILITY_INTERNAL jmp_buf PetscScJumpBuf; 21 PETSC_INTERN void PetscScAbort_longjmp(void) { 22 PetscError(PETSC_COMM_SELF, -1, "p4est function", "p4est file", PETSC_ERR_LIB, PETSC_ERROR_INITIAL, "Error in p4est stack call\n"); 23 longjmp(PetscScJumpBuf, 1); 24 return; 25 } 26 27 #define PetscScAbort PetscScAbort_longjmp 28 #else 29 #define PetscScAbort NULL 30 #endif 31 32 static PetscErrorCode PetscP4estFinalize(void) { 33 PetscFunctionBegin; 34 if (PetscBeganSc) { 35 /* We do not want libsc to abort on a mismatched allocation and prevent further Petsc unwinding */ 36 PetscCallP4est(sc_package_set_abort_alloc_mismatch, (sc_package_id, 0)); 37 PetscCallP4est(sc_package_set_abort_alloc_mismatch, (p4est_package_id, 0)); 38 PetscCallP4est(sc_package_set_abort_alloc_mismatch, (-1, 0)); 39 PetscCallP4est(sc_finalize, ()); 40 } 41 PetscCall(PetscHeaderDestroy(&P4estLoggingObject)); 42 PetscFunctionReturn(0); 43 } 44 45 PetscErrorCode PetscP4estInitialize(void) { 46 PetscBool psc_catch_signals = PETSC_FALSE; 47 PetscBool psc_print_backtrace = PETSC_TRUE; 48 int psc_log_threshold = SC_LP_DEFAULT; 49 int pp4est_log_threshold = SC_LP_DEFAULT; 50 char logList[256]; 51 PetscBool opt, pkg; 52 53 PetscFunctionBegin; 54 if (PetscP4estInitialized) PetscFunctionReturn(0); 55 PetscP4estInitialized = PETSC_TRUE; 56 57 /* Register Classes */ 58 PetscCall(PetscClassIdRegister("p4est logging", &P4ESTLOGGING_CLASSID)); 59 /* Process Info */ 60 { 61 PetscClassId classids[1]; 62 63 classids[0] = P4ESTLOGGING_CLASSID; 64 PetscCall(PetscInfoProcessClass("p4est", 1, classids)); 65 } 66 /* Process summary exclusions */ 67 PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt)); 68 if (opt) { 69 PetscCall(PetscStrInList("p4est", logList, ',', &pkg)); 70 if (pkg) PetscCall(PetscLogEventExcludeClass(P4ESTLOGGING_CLASSID)); 71 } 72 PetscCall(PetscHeaderCreate(P4estLoggingObject, P4ESTLOGGING_CLASSID, "p4est", "p4est logging", "DM", PETSC_COMM_WORLD, NULL, PetscObjectView)); 73 if (sc_package_id == -1) { 74 int log_threshold_shifted = psc_log_threshold + 1; 75 PetscBool set; 76 #if defined(PETSC_HAVE_MPIUNI) 77 sc_MPI_Comm comm_world = sc_MPI_COMM_WORLD; 78 #else 79 MPI_Comm comm_world = PETSC_COMM_WORLD; 80 #endif 81 82 PetscBeganSc = PETSC_TRUE; 83 PetscCall(PetscOptionsGetBool(NULL, NULL, "-petsc_sc_catch_signals", &psc_catch_signals, NULL)); 84 PetscCall(PetscOptionsGetBool(NULL, NULL, "-petsc_sc_print_backtrace", &psc_print_backtrace, NULL)); 85 PetscCall(PetscOptionsGetEnum(NULL, NULL, "-petsc_sc_log_threshold", SCLogTypes, (PetscEnum *)&log_threshold_shifted, &set)); 86 if (set) psc_log_threshold = log_threshold_shifted - 1; 87 sc_init(comm_world, (int)psc_catch_signals, (int)psc_print_backtrace, PetscScLogHandler, psc_log_threshold); 88 PetscCheck(sc_package_id != -1, PETSC_COMM_WORLD, PETSC_ERR_LIB, "Could not initialize libsc package used by p4est"); 89 sc_set_abort_handler(PetscScAbort); 90 } 91 if (p4est_package_id == -1) { 92 int log_threshold_shifted = pp4est_log_threshold + 1; 93 PetscBool set; 94 95 PetscCall(PetscOptionsGetEnum(NULL, NULL, "-petsc_p4est_log_threshold", SCLogTypes, (PetscEnum *)&log_threshold_shifted, &set)); 96 if (set) pp4est_log_threshold = log_threshold_shifted - 1; 97 PetscCallP4est(p4est_init, (PetscScLogHandler, pp4est_log_threshold)); 98 PetscCheck(p4est_package_id != -1, PETSC_COMM_WORLD, PETSC_ERR_LIB, "Could not initialize p4est"); 99 } 100 PetscCall(DMForestRegisterType(DMP4EST)); 101 PetscCall(DMForestRegisterType(DMP8EST)); 102 PetscCall(PetscRegisterFinalize(PetscP4estFinalize)); 103 PetscFunctionReturn(0); 104 } 105