xref: /petsc/src/dm/impls/forest/p4est/petsc_p4est_package.c (revision cd37523727fa3705b97d2196665581ff5be07b09)
1 #include "petsc_p4est_package.h"
2 #include <p4est_base.h>
3 
4 static const char *const SCLogTypes[] = {"DEFAULT","ALWAYS","TRACE","DEBUG","VERBOSE","INFO","STATISTICS","PRODUCTION","ESSENTIAL","ERROR","SILENT","SCLogTypes","SC_LP_",0};
5 
6 static PetscBool PetscP4estInitialized = PETSC_FALSE;
7 static PetscBool PetscBeganSc          = PETSC_FALSE;
8 
9 #undef __FUNCT__
10 #define __FUNCT__ "PetscScLogHandler"
11 static void PetscScLogHandler (FILE *log_stream, const char *filename, int lineno,
12                                int package, int category,
13                                int priority, const char *msg)
14 {
15   PetscInfo_Private(filename,NULL,":%d{%s} %s",lineno,package == sc_package_id ? "sc" : package == p4est_package_id ? "p4est" : "",msg);
16 }
17 
18 
19 /* p4est tries to abort: if possible, use setjmp to enable at least a little unwinding */
20 #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_USE_ERRORCHECKING)
21 #include <setjmp.h>
22 PETSC_VISIBILITY_INTERNAL jmp_buf PetscScJumpBuf;
23 PETSC_INTERN void PetscScAbort_longjmp(void)
24 {
25   longjmp(PetscScJumpBuf,1);
26   return;
27 }
28 
29 #define PetscScAbort PetscScAbort_longjmp
30 #else
31 #define PetscScAbort NULL
32 #endif
33 
34 #undef __FUNCT__
35 #define __FUNCT__ "PetscP4estFinalize"
36 static PetscErrorCode PetscP4estFinalize()
37 {
38   PetscFunctionBegin;
39   if (PetscBeganSc) {
40     PetscStackCallP4est(sc_finalize,());
41   }
42   PetscFunctionReturn(0);
43 }
44 
45 #undef __FUNCT__
46 #define __FUNCT__ "PetscP4estInitialize"
47 PetscErrorCode PetscP4estInitialize()
48 {
49   PetscBool psc_catch_signals      = PETSC_TRUE;
50   PetscBool psc_print_backtrace    = PETSC_TRUE;
51   int       psc_log_threshold      = SC_LP_DEFAULT;
52   int       pp4est_log_threshold   = SC_LP_DEFAULT;
53   PetscErrorCode ierr;
54 
55   PetscFunctionBegin;
56   if (PetscP4estInitialized) PetscFunctionReturn(0);
57   PetscP4estInitialized = PETSC_TRUE;
58   if (sc_package_id == -1) {
59     int log_threshold_shifted = psc_log_threshold + 1;
60     PetscBool set;
61 
62     PetscBeganSc = PETSC_TRUE;
63     ierr = PetscOptionsGetBool(NULL,"-petsc_sc_catch_signals",&psc_catch_signals,NULL);CHKERRQ(ierr);
64     ierr = PetscOptionsGetBool(NULL,"-petsc_sc_print_backtrace",&psc_print_backtrace,NULL);CHKERRQ(ierr);
65     ierr = PetscOptionsGetEnum(NULL,"-petsc_sc_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);CHKERRQ(ierr);
66     if (set) {
67       psc_log_threshold = log_threshold_shifted - 1;
68     }
69     sc_init(PETSC_COMM_WORLD,psc_catch_signals,psc_print_backtrace,PetscScLogHandler,psc_log_threshold);
70     if (sc_package_id == -1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize libsc package used by p4est");
71     sc_set_abort_handler(PetscScAbort);
72   }
73   if (p4est_package_id == -1) {
74     int log_threshold_shifted = pp4est_log_threshold + 1;
75     PetscBool set;
76 
77     ierr = PetscOptionsGetEnum(NULL,"-petsc_p4est_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);CHKERRQ(ierr);
78     if (set) {
79       pp4est_log_threshold = log_threshold_shifted - 1;
80     }
81     PetscStackCallP4est(p4est_init,(PetscScLogHandler,pp4est_log_threshold));
82     if (p4est_package_id == -1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize p4est");
83   }
84   ierr = PetscRegisterFinalize(PetscP4estFinalize);CHKERRQ(ierr);
85   PetscFunctionReturn(0);
86 }
87