xref: /petsc/src/dm/impls/forest/p4est/petsc_p4est_package.c (revision bdeda3f1fd1297e3c1f47ff6ff21f720eebc7273)
1 #include <petsc/private/petscimpl.h>
2 #include <p4est_base.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_",0};
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 #undef __FUNCT__
14 #define __FUNCT__ "PetscScLogHandler"
15 static void PetscScLogHandler (FILE *log_stream, const char *filename, int lineno,
16                                int package, int category,
17                                int priority, const char *msg)
18 {
19   PetscInfo_Private(filename,P4estLoggingObject,":%d{%s} %s",lineno,package == sc_package_id ? "sc" : package == p4est_package_id ? "p4est" : "",msg);
20 }
21 
22 /* p4est tries to abort: if possible, use setjmp to enable at least a little unwinding */
23 #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_USE_ERRORCHECKING)
24 #include <setjmp.h>
25 PETSC_VISIBILITY_INTERNAL jmp_buf PetscScJumpBuf;
26 PETSC_INTERN void PetscScAbort_longjmp(void)
27 {
28   PetscError(PETSC_COMM_SELF,-1,"p4est function","p4est file",PETSC_ERR_LIB,PETSC_ERROR_INITIAL,"Error in p4est stack call\n");
29   longjmp(PetscScJumpBuf,1);
30   return;
31 }
32 
33 #define PetscScAbort PetscScAbort_longjmp
34 #else
35 #define PetscScAbort NULL
36 #endif
37 
38 #undef __FUNCT__
39 #define __FUNCT__ "PetscP4estFinalize"
40 static PetscErrorCode PetscP4estFinalize(void)
41 {
42   PetscErrorCode ierr;
43 
44   PetscFunctionBegin;
45   if (PetscBeganSc) {
46     PetscStackCallP4est(sc_finalize,());
47   }
48   ierr = PetscHeaderDestroy(&P4estLoggingObject);CHKERRQ(ierr);
49   PetscFunctionReturn(0);
50 }
51 
52 #undef __FUNCT__
53 #define __FUNCT__ "PetscP4estInitialize"
54 PetscErrorCode PetscP4estInitialize(void)
55 {
56   PetscBool      psc_catch_signals    = PETSC_TRUE;
57   PetscBool      psc_print_backtrace  = PETSC_TRUE;
58   int            psc_log_threshold    = SC_LP_DEFAULT;
59   int            pp4est_log_threshold = SC_LP_DEFAULT;
60   char           logList[256], *className;
61   PetscBool      opt;
62   PetscErrorCode ierr;
63 
64   PetscFunctionBegin;
65   if (PetscP4estInitialized) PetscFunctionReturn(0);
66   PetscP4estInitialized = PETSC_TRUE;
67   ierr = PetscClassIdRegister("p4est logging",&P4ESTLOGGING_CLASSID);CHKERRQ(ierr);
68   ierr = PetscOptionsGetString(NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
69   if (opt) {
70     ierr = PetscStrstr(logList, "p4est", &className);CHKERRQ(ierr);
71     if (className) {
72       ierr = PetscInfoDeactivateClass(P4ESTLOGGING_CLASSID);CHKERRQ(ierr);
73     }
74   }
75   ierr = PetscHeaderCreate(P4estLoggingObject,P4ESTLOGGING_CLASSID,"p4est","p4est logging","DM",PETSC_COMM_WORLD,NULL,PetscObjectView);CHKERRQ(ierr);
76   if (sc_package_id == -1) {
77     int log_threshold_shifted = psc_log_threshold + 1;
78     PetscBool set;
79 
80     PetscBeganSc = PETSC_TRUE;
81     ierr = PetscOptionsGetBool(NULL,"-petsc_sc_catch_signals",&psc_catch_signals,NULL);CHKERRQ(ierr);
82     ierr = PetscOptionsGetBool(NULL,"-petsc_sc_print_backtrace",&psc_print_backtrace,NULL);CHKERRQ(ierr);
83     ierr = PetscOptionsGetEnum(NULL,"-petsc_sc_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);CHKERRQ(ierr);
84     if (set) {
85       psc_log_threshold = log_threshold_shifted - 1;
86     }
87     sc_init(PETSC_COMM_WORLD,(int)psc_catch_signals,(int)psc_print_backtrace,PetscScLogHandler,psc_log_threshold);
88     if (sc_package_id == -1) SETERRQ(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     ierr = PetscOptionsGetEnum(NULL,"-petsc_p4est_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);CHKERRQ(ierr);
96     if (set) {
97       pp4est_log_threshold = log_threshold_shifted - 1;
98     }
99     PetscStackCallP4est(p4est_init,(PetscScLogHandler,pp4est_log_threshold));
100     if (p4est_package_id == -1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize p4est");
101   }
102   ierr = PetscRegisterFinalize(PetscP4estFinalize);CHKERRQ(ierr);
103   PetscFunctionReturn(0);
104 }
105