xref: /petsc/src/dm/impls/forest/p4est/petsc_p4est_package.c (revision 2b8d69ca7ea5fe9190df62c1dce3bbd66fce84dd)
1 #include <petscdmforest.h>
2 #include <petsc/private/petscimpl.h>
3 #include <p4est_base.h>
4 #include "petsc_p4est_package.h"
5 
6 static const char *const SCLogTypes[] = {"DEFAULT","ALWAYS","TRACE","DEBUG","VERBOSE","INFO","STATISTICS","PRODUCTION","ESSENTIAL","ERROR","SILENT","SCLogTypes","SC_LP_",0};
7 
8 static PetscBool PetscP4estInitialized = PETSC_FALSE;
9 static PetscBool PetscBeganSc          = PETSC_FALSE;
10 static PetscClassId P4ESTLOGGING_CLASSID;
11 
12 PetscObject P4estLoggingObject; /* Just a vehicle for its classid */
13 
14 #undef __FUNCT__
15 #define __FUNCT__ "PetscScLogHandler"
16 static void PetscScLogHandler (FILE *log_stream, const char *filename, int lineno,
17                                int package, int category,
18                                int priority, const char *msg)
19 {
20   PetscInfo_Private(filename,P4estLoggingObject,":%d{%s} %s",lineno,package == sc_package_id ? "sc" : package == p4est_package_id ? "p4est" : "",msg);
21 }
22 
23 /* p4est tries to abort: if possible, use setjmp to enable at least a little unwinding */
24 #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_USE_ERRORCHECKING)
25 #include <setjmp.h>
26 PETSC_VISIBILITY_INTERNAL jmp_buf PetscScJumpBuf;
27 PETSC_INTERN void PetscScAbort_longjmp(void)
28 {
29   PetscError(PETSC_COMM_SELF,-1,"p4est function","p4est file",PETSC_ERR_LIB,PETSC_ERROR_INITIAL,"Error in p4est stack call\n");
30   longjmp(PetscScJumpBuf,1);
31   return;
32 }
33 
34 #define PetscScAbort PetscScAbort_longjmp
35 #else
36 #define PetscScAbort NULL
37 #endif
38 
39 #undef __FUNCT__
40 #define __FUNCT__ "PetscP4estFinalize"
41 static PetscErrorCode PetscP4estFinalize(void)
42 {
43   PetscErrorCode ierr;
44 
45   PetscFunctionBegin;
46   if (PetscBeganSc) {
47     /* We do not want libsc to abort on a mismatched allocation and prevent further Petsc unwinding */
48     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(sc_package_id,0));
49     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(p4est_package_id,0));
50     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(-1,0));
51     PetscStackCallP4est(sc_finalize,());
52   }
53   ierr = PetscHeaderDestroy(&P4estLoggingObject);CHKERRQ(ierr);
54   PetscFunctionReturn(0);
55 }
56 
57 #undef __FUNCT__
58 #define __FUNCT__ "PetscP4estInitialize"
59 PetscErrorCode PetscP4estInitialize(void)
60 {
61   PetscBool      psc_catch_signals    = PETSC_FALSE;
62   PetscBool      psc_print_backtrace  = PETSC_TRUE;
63   int            psc_log_threshold    = SC_LP_DEFAULT;
64   int            pp4est_log_threshold = SC_LP_DEFAULT;
65   char           logList[256], *className;
66   PetscBool      opt;
67   PetscErrorCode ierr;
68 
69   PetscFunctionBegin;
70   if (PetscP4estInitialized) PetscFunctionReturn(0);
71   PetscP4estInitialized = PETSC_TRUE;
72   ierr = PetscClassIdRegister("p4est logging",&P4ESTLOGGING_CLASSID);CHKERRQ(ierr);
73   ierr = PetscOptionsGetString(NULL,NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
74   if (opt) {
75     ierr = PetscStrstr(logList, "p4est", &className);CHKERRQ(ierr);
76     if (className) {
77       ierr = PetscInfoDeactivateClass(P4ESTLOGGING_CLASSID);CHKERRQ(ierr);
78     }
79   }
80   ierr = PetscHeaderCreate(P4estLoggingObject,P4ESTLOGGING_CLASSID,"p4est","p4est logging","DM",PETSC_COMM_WORLD,NULL,PetscObjectView);CHKERRQ(ierr);
81   if (sc_package_id == -1) {
82     int log_threshold_shifted = psc_log_threshold + 1;
83     PetscBool set;
84 
85     PetscBeganSc = PETSC_TRUE;
86     ierr = PetscOptionsGetBool(NULL,NULL,"-petsc_sc_catch_signals",&psc_catch_signals,NULL);CHKERRQ(ierr);
87     ierr = PetscOptionsGetBool(NULL,NULL,"-petsc_sc_print_backtrace",&psc_print_backtrace,NULL);CHKERRQ(ierr);
88     ierr = PetscOptionsGetEnum(NULL,NULL,"-petsc_sc_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);CHKERRQ(ierr);
89     if (set) {
90       psc_log_threshold = log_threshold_shifted - 1;
91     }
92     sc_init(PETSC_COMM_WORLD,(int)psc_catch_signals,(int)psc_print_backtrace,PetscScLogHandler,psc_log_threshold);
93     if (sc_package_id == -1) SETERRQ(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     ierr = PetscOptionsGetEnum(NULL,NULL,"-petsc_p4est_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);CHKERRQ(ierr);
101     if (set) {
102       pp4est_log_threshold = log_threshold_shifted - 1;
103     }
104     PetscStackCallP4est(p4est_init,(PetscScLogHandler,pp4est_log_threshold));
105     if (p4est_package_id == -1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize p4est");
106   }
107   ierr = DMForestRegisterType(DMP4EST);CHKERRQ(ierr);
108   ierr = DMForestRegisterType(DMP8EST);CHKERRQ(ierr);
109   ierr = PetscRegisterFinalize(PetscP4estFinalize);CHKERRQ(ierr);
110   PetscFunctionReturn(0);
111 }
112