Actual source code: petsc_p4est_package.c

  1: #include <petscdmforest.h>
  2: #include <petsc/private/petscimpl.h>
  3: #include "petsc_p4est_package.h"

  5: static const char*const SCLogTypes[] = {"DEFAULT","ALWAYS","TRACE","DEBUG","VERBOSE","INFO","STATISTICS","PRODUCTION","ESSENTIAL","ERROR","SILENT","SCLogTypes","SC_LP_", NULL};

  7: static PetscBool    PetscP4estInitialized = PETSC_FALSE;
  8: static PetscBool    PetscBeganSc          = PETSC_FALSE;
  9: static PetscClassId P4ESTLOGGING_CLASSID;

 11: PetscObject P4estLoggingObject; /* Just a vehicle for its classid */

 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: }

 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: }

 29: #define PetscScAbort PetscScAbort_longjmp
 30: #else
 31: #define PetscScAbort NULL
 32: #endif

 34: static PetscErrorCode PetscP4estFinalize(void)
 35: {
 36:   if (PetscBeganSc) {
 37:     /* We do not want libsc to abort on a mismatched allocation and prevent further Petsc unwinding */
 38:     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(sc_package_id,0));
 39:     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(p4est_package_id,0));
 40:     PetscStackCallP4est(sc_package_set_abort_alloc_mismatch,(-1,0));
 41:     PetscStackCallP4est(sc_finalize,());
 42:   }
 43:   PetscHeaderDestroy(&P4estLoggingObject);
 44:   return 0;
 45: }

 47: PetscErrorCode PetscP4estInitialize(void)
 48: {
 49:   PetscBool psc_catch_signals    = PETSC_FALSE;
 50:   PetscBool psc_print_backtrace  = PETSC_TRUE;
 51:   int       psc_log_threshold    = SC_LP_DEFAULT;
 52:   int       pp4est_log_threshold = SC_LP_DEFAULT;
 53:   char      logList[256];
 54:   PetscBool opt,pkg;

 56:   if (PetscP4estInitialized) return 0;
 57:   PetscP4estInitialized = PETSC_TRUE;

 59:   /* Register Classes */
 60:   PetscClassIdRegister("p4est logging",&P4ESTLOGGING_CLASSID);
 61:   /* Process Info */
 62:   {
 63:     PetscClassId  classids[1];

 65:     classids[0] = P4ESTLOGGING_CLASSID;
 66:     PetscInfoProcessClass("p4est", 1, classids);
 67:   }
 68:   /* Process summary exclusions */
 69:   PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
 70:   if (opt) {
 71:     PetscStrInList("p4est",logList,',',&pkg);
 72:     if (pkg) PetscLogEventExcludeClass(P4ESTLOGGING_CLASSID);
 73:   }
 74:   PetscHeaderCreate(P4estLoggingObject,P4ESTLOGGING_CLASSID,"p4est","p4est logging","DM",PETSC_COMM_WORLD,NULL,PetscObjectView);
 75:   if (sc_package_id == -1) {
 76:     int       log_threshold_shifted = psc_log_threshold + 1;
 77:     PetscBool set;

 79:     PetscBeganSc = PETSC_TRUE;
 80:     PetscOptionsGetBool(NULL,NULL,"-petsc_sc_catch_signals",&psc_catch_signals,NULL);
 81:     PetscOptionsGetBool(NULL,NULL,"-petsc_sc_print_backtrace",&psc_print_backtrace,NULL);
 82:     PetscOptionsGetEnum(NULL,NULL,"-petsc_sc_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);
 83:     if (set) psc_log_threshold = log_threshold_shifted - 1;
 84:     sc_init(PETSC_COMM_WORLD,(int)psc_catch_signals,(int)psc_print_backtrace,PetscScLogHandler,psc_log_threshold);
 86:     sc_set_abort_handler(PetscScAbort);
 87:   }
 88:   if (p4est_package_id == -1) {
 89:     int       log_threshold_shifted = pp4est_log_threshold + 1;
 90:     PetscBool set;

 92:     PetscOptionsGetEnum(NULL,NULL,"-petsc_p4est_log_threshold",SCLogTypes,(PetscEnum*)&log_threshold_shifted,&set);
 93:     if (set) pp4est_log_threshold = log_threshold_shifted - 1;
 94:     PetscStackCallP4est(p4est_init,(PetscScLogHandler,pp4est_log_threshold));
 96:   }
 97:   DMForestRegisterType(DMP4EST);
 98:   DMForestRegisterType(DMP8EST);
 99:   PetscRegisterFinalize(PetscP4estFinalize);
100:   return 0;
101: }