xref: /petsc/src/dm/impls/forest/p4est/petsc_p4est_package.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
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 
81     PetscBeganSc = PETSC_TRUE;
82     PetscCall(PetscOptionsGetBool(NULL,NULL,"-petsc_sc_catch_signals",&psc_catch_signals,NULL));
83     PetscCall(PetscOptionsGetBool(NULL,NULL,"-petsc_sc_print_backtrace",&psc_print_backtrace,NULL));
84     PetscCall(PetscOptionsGetEnum(NULL,NULL,"-petsc_sc_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set));
85     if (set) psc_log_threshold = log_threshold_shifted - 1;
86     sc_init(PETSC_COMM_WORLD,(int)psc_catch_signals,(int)psc_print_backtrace,PetscScLogHandler,psc_log_threshold);
87     PetscCheckFalse(sc_package_id == -1,PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize libsc package used by p4est");
88     sc_set_abort_handler(PetscScAbort);
89   }
90   if (p4est_package_id == -1) {
91     int       log_threshold_shifted = pp4est_log_threshold + 1;
92     PetscBool set;
93 
94     PetscCall(PetscOptionsGetEnum(NULL,NULL,"-petsc_p4est_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set));
95     if (set) pp4est_log_threshold = log_threshold_shifted - 1;
96     PetscStackCallP4est(p4est_init,(PetscScLogHandler,pp4est_log_threshold));
97     PetscCheckFalse(p4est_package_id == -1,PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize p4est");
98   }
99   PetscCall(DMForestRegisterType(DMP4EST));
100   PetscCall(DMForestRegisterType(DMP8EST));
101   PetscCall(PetscRegisterFinalize(PetscP4estFinalize));
102   PetscFunctionReturn(0);
103 }
104