xref: /petsc/src/dm/impls/forest/p4est/petsc_p4est_package.c (revision 4e8afd12df985612b40e26aef947da2f566aee4e)
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 static void PetscScLogHandler(FILE *log_stream, const char *filename, int lineno,int package, int category,int priority, const char *msg)
15 {
16   PetscInfo_Private(filename,P4estLoggingObject,":%d{%s} %s",lineno,package == sc_package_id ? "sc" : package == p4est_package_id ? "p4est" : "",msg);
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_DEBUG)
21 #include <setjmp.h>
22 PETSC_VISIBILITY_INTERNAL jmp_buf PetscScJumpBuf;
23 PETSC_INTERN void PetscScAbort_longjmp(void)
24 {
25   PetscError(PETSC_COMM_SELF,-1,"p4est function","p4est file",PETSC_ERR_LIB,PETSC_ERROR_INITIAL,"Error in p4est stack call\n");
26   longjmp(PetscScJumpBuf,1);
27   return;
28 }
29 
30 #define PetscScAbort PetscScAbort_longjmp
31 #else
32 #define PetscScAbort NULL
33 #endif
34 
35 static PetscErrorCode PetscP4estFinalize(void)
36 {
37   PetscErrorCode ierr;
38 
39   PetscFunctionBegin;
40   if (PetscBeganSc) {
41     /* We do not want libsc to abort on a mismatched allocation and prevent further Petsc unwinding */
42     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(sc_package_id,0));
43     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(p4est_package_id,0));
44     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(-1,0));
45     PetscStackCallP4est(sc_finalize,());
46   }
47   ierr = PetscHeaderDestroy(&P4estLoggingObject);CHKERRQ(ierr);
48   PetscFunctionReturn(0);
49 }
50 
51 PetscErrorCode PetscP4estInitialize(void)
52 {
53   PetscBool      psc_catch_signals    = PETSC_FALSE;
54   PetscBool      psc_print_backtrace  = PETSC_TRUE;
55   int            psc_log_threshold    = SC_LP_DEFAULT;
56   int            pp4est_log_threshold = SC_LP_DEFAULT;
57   char           logList[256];
58   PetscBool      opt,pkg;
59   PetscErrorCode ierr;
60 
61   PetscFunctionBegin;
62   if (PetscP4estInitialized) PetscFunctionReturn(0);
63   PetscP4estInitialized = PETSC_TRUE;
64 
65   /* Register Classes */
66   ierr = PetscClassIdRegister("p4est logging",&P4ESTLOGGING_CLASSID);CHKERRQ(ierr);
67   /* Process Info */
68   {
69     PetscClassId  classids[1];
70 
71     classids[0] = P4ESTLOGGING_CLASSID;
72     ierr = PetscInfoProcessClass("p4est", 1, classids);CHKERRQ(ierr);
73   }
74   /* Process summary exclusions */
75   ierr = PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);CHKERRQ(ierr);
76   if (opt) {
77     ierr = PetscStrInList("p4est",logList,',',&pkg);CHKERRQ(ierr);
78     if (pkg) {ierr = PetscLogEventExcludeClass(P4ESTLOGGING_CLASSID);CHKERRQ(ierr);}
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) psc_log_threshold = log_threshold_shifted - 1;
90     sc_init(PETSC_COMM_WORLD,(int)psc_catch_signals,(int)psc_print_backtrace,PetscScLogHandler,psc_log_threshold);
91     if (sc_package_id == -1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize libsc package used by p4est");
92     sc_set_abort_handler(PetscScAbort);
93   }
94   if (p4est_package_id == -1) {
95     int       log_threshold_shifted = pp4est_log_threshold + 1;
96     PetscBool set;
97 
98     ierr = PetscOptionsGetEnum(NULL,NULL,"-petsc_p4est_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);CHKERRQ(ierr);
99     if (set) pp4est_log_threshold = log_threshold_shifted - 1;
100     PetscStackCallP4est(p4est_init,(PetscScLogHandler,pp4est_log_threshold));
101     if (p4est_package_id == -1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_LIB,"Could not initialize p4est");
102   }
103   ierr = DMForestRegisterType(DMP4EST);CHKERRQ(ierr);
104   ierr = DMForestRegisterType(DMP8EST);CHKERRQ(ierr);
105   ierr = PetscRegisterFinalize(PetscP4estFinalize);CHKERRQ(ierr);
106   PetscFunctionReturn(0);
107 }
108