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