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