Skip to content

Commit

Permalink
plexreorder: add DMPlexReorder{Set,Get}Default()
Browse files Browse the repository at this point in the history
  • Loading branch information
ksagiyam committed May 24, 2022
1 parent 14a7ced commit 2d0aafb
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 28 deletions.
3 changes: 3 additions & 0 deletions doc/docs/changes/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ In addition to the changes above
- pass determinant Vec, rather than its address
- Change ``DMPlexMetricAverage()``, ``DMPlexMetricAverage2()`` and ``DMPlexMetricAverage3()`` to pass output metric, rather than its address
- Change ``DMPlexMetricIntersection()``, ``DMPlexMetricIntersection2()`` and ``DMPlexMetricIntersection3()`` to pass output metric, rather than its address
- Add capability to specify whether the DMPlex should be reordered by default:
- add ``DMPlexReorderDefaultFlag``
- add ``DMPlexReorderGetDefault()`` and ``DMPlexReorderSetDefault()`` to get and set this flag

.. rubric:: FE/FV:

Expand Down
1 change: 1 addition & 0 deletions include/petsc/finclude/petscdmplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@

#define DMPlexInterpolatedFlag PetscEnum
#define DMPlexTPSType PetscEnum
#define DMPlexReorderDefaultFlag PetscEnum

#endif
33 changes: 18 additions & 15 deletions include/petsc/private/dmpleximpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,22 @@ typedef struct {
we allow additional segregation of by cell type.
*/
typedef struct {
PetscInt refct;

PetscSection coneSection; /* Layout of cones (inedges for DAG) */
PetscInt *cones; /* Cone for each point */
PetscInt *coneOrientations; /* Orientation of each cone point, means cone traveral should start on point 'o', and if negative start on -(o+1) and go in reverse */
PetscSection supportSection; /* Layout of cones (inedges for DAG) */
PetscInt *supports; /* Cone for each point */
PetscBool refinementUniform; /* Flag for uniform cell refinement */
char *transformType; /* Type of transform for uniform cell refinement */
PetscReal refinementLimit; /* Maximum volume for refined cell */
PetscErrorCode (*refinementFunc)(const PetscReal [], PetscReal *); /* Function giving the maximum volume for refined cell */
PetscBool distDefault; /* Distribute the DM by default */
PetscInt overlap; /* Overlap of the partitions as passed to DMPlexDistribute() or DMPlexDistributeOverlap() */
DMPlexInterpolatedFlag interpolated;
DMPlexInterpolatedFlag interpolatedCollective;
PetscInt refct;

PetscSection coneSection; /* Layout of cones (inedges for DAG) */
PetscInt *cones; /* Cone for each point */
PetscInt *coneOrientations; /* Orientation of each cone point, means cone traveral should start on point 'o', and if negative start on -(o+1) and go in reverse */
PetscSection supportSection; /* Layout of cones (inedges for DAG) */
PetscInt *supports; /* Cone for each point */
PetscBool refinementUniform; /* Flag for uniform cell refinement */
char *transformType; /* Type of transform for uniform cell refinement */
PetscReal refinementLimit; /* Maximum volume for refined cell */
PetscErrorCode (*refinementFunc)(const PetscReal [], PetscReal *); /* Function giving the maximum volume for refined cell */
PetscBool distDefault; /* Distribute the DM by default */
DMPlexReorderDefaultFlag reorderDefault; /* Reorder the DM by default */
PetscInt overlap; /* Overlap of the partitions as passed to DMPlexDistribute() or DMPlexDistributeOverlap() */
DMPlexInterpolatedFlag interpolated;
DMPlexInterpolatedFlag interpolatedCollective;

PetscInt *facesTmp; /* Work space for faces operation */

Expand Down Expand Up @@ -312,6 +313,8 @@ PETSC_INTERN PetscErrorCode DMPlexGetOverlap_Plex(DM, PetscInt *);
PETSC_INTERN PetscErrorCode DMPlexSetOverlap_Plex(DM, DM, PetscInt);
PETSC_INTERN PetscErrorCode DMPlexDistributeGetDefault_Plex(DM, PetscBool *);
PETSC_INTERN PetscErrorCode DMPlexDistributeSetDefault_Plex(DM, PetscBool);
PETSC_INTERN PetscErrorCode DMPlexReorderGetDefault_Plex(DM, DMPlexReorderDefaultFlag *);
PETSC_INTERN PetscErrorCode DMPlexReorderSetDefault_Plex(DM, DMPlexReorderDefaultFlag);

#if 1
static inline PetscInt DihedralInvert(PetscInt N, PetscInt a)
Expand Down
21 changes: 21 additions & 0 deletions include/petscdmplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,30 @@ PETSC_EXTERN PetscErrorCode DMPlexGetAdjacency(DM, PetscInt, PetscInt *, PetscIn
PETSC_EXTERN PetscErrorCode DMPlexSetMigrationSF(DM, PetscSF);
PETSC_EXTERN PetscErrorCode DMPlexGetMigrationSF(DM, PetscSF *);

/*E
DMPlexReorderDefaultFlag - Flag indicating whether the DMPlex should be reordered by default
$ DMPLEX_REORDER_DEFAULT_NOTSET - Flag not set.
$ DMPLEX_REORDER_DEFAULT_FALSE - Do not Reorder by default.
$ DMPLEX_REORDER_DEFAULT_TRUE - Reorder by default.
Level: intermediate
Developer Note:
Any additions/changes here MUST also be made in include/petsc/finclude/petscdmplex.h and src/dm/f90-mod/petscdmplex.h
.seealso: `DMPlexReorderSetDefault()`, `DMPlexReorderGetDefault()`
E*/
typedef enum {
DMPLEX_REORDER_DEFAULT_NOTSET = -1,
DMPLEX_REORDER_DEFAULT_FALSE = 0,
DMPLEX_REORDER_DEFAULT_TRUE
} DMPlexReorderDefaultFlag;
PETSC_EXTERN PetscErrorCode DMPlexGetOrdering(DM, MatOrderingType, DMLabel, IS *);
PETSC_EXTERN PetscErrorCode DMPlexGetOrdering1D(DM, IS *);
PETSC_EXTERN PetscErrorCode DMPlexPermute(DM, IS, DM *);
PETSC_EXTERN PetscErrorCode DMPlexReorderGetDefault(DM, DMPlexReorderDefaultFlag *);
PETSC_EXTERN PetscErrorCode DMPlexReorderSetDefault(DM, DMPlexReorderDefaultFlag);

PETSC_EXTERN PetscErrorCode DMPlexCreateProcessSF(DM, PetscSF, IS *, PetscSF *);
PETSC_EXTERN PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM, PetscSF, PetscSection, IS, PetscSection, IS, IS *, PetscSF *);
Expand Down
27 changes: 27 additions & 0 deletions src/binding/petsc4py/src/PETSc/DMPlex.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# --------------------------------------------------------------------

class DMPlexReorderDefaultFlag(object):
NOTSET = DMPLEX_REORDER_DEFAULT_NOTSET
FALSE = DMPLEX_REORDER_DEFAULT_FALSE
TRUE = DMPLEX_REORDER_DEFAULT_TRUE

# --------------------------------------------------------------------

cdef class DMPlex(DM):

ReorderDefaultFlag = DMPlexReorderDefaultFlag

#

def create(self, comm=None):
cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
cdef PetscDM newdm = NULL
Expand Down Expand Up @@ -669,6 +680,16 @@ cdef class DMPlex(DM):
CHKERR( DMPlexPermute(self.dm, perm.iset, &dm.dm) )
return dm

def reorderGetDefault(self):
cdef PetscDMPlexReorderDefaultFlag reorder = DMPLEX_REORDER_DEFAULT_NOTSET
CHKERR( DMPlexReorderGetDefault(self.dm, &reorder) )
return reorder

def reorderSetDefault(self, flag):
cdef PetscDMPlexReorderDefaultFlag reorder = flag
CHKERR( DMPlexReorderSetDefault(self.dm, reorder) )
return

#

def computeCellGeometryFVM(self, cell):
Expand Down Expand Up @@ -915,3 +936,9 @@ cdef class DMPlex(DM):

def localVectorLoad(self, Viewer viewer, DM sectiondm, SF sf, Vec vec):
CHKERR( DMPlexLocalVectorLoad(self.dm, viewer.vwr, sectiondm.dm, sf.sf, vec.vec))

# --------------------------------------------------------------------

del DMPlexReorderDefaultFlag

# --------------------------------------------------------------------
7 changes: 7 additions & 0 deletions src/binding/petsc4py/src/PETSc/petscdmplex.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

cdef extern from * nogil:

ctypedef enum PetscDMPlexReorderDefaultFlag "DMPlexReorderDefaultFlag":
DMPLEX_REORDER_DEFAULT_NOTSET
DMPLEX_REORDER_DEFAULT_FALSE
DMPLEX_REORDER_DEFAULT_TRUE

int DMPlexCreate(MPI_Comm,PetscDM*)
int DMPlexCreateCohesiveSubmesh(PetscDM,PetscBool,const char[],PetscInt,PetscDM*)
int DMPlexCreateFromCellListPetsc(MPI_Comm,PetscInt,PetscInt,PetscInt,PetscInt,PetscBool,PetscInt[],PetscInt,PetscReal[],PetscDM*)
Expand Down Expand Up @@ -108,6 +113,8 @@ cdef extern from * nogil:

int DMPlexGetOrdering(PetscDM,PetscMatOrderingType,PetscDMLabel,PetscIS*)
int DMPlexPermute(PetscDM,PetscIS,PetscDM*)
int DMPlexReorderGetDefault(PetscDM,PetscDMPlexReorderDefaultFlag*)
int DMPlexReorderSetDefault(PetscDM,PetscDMPlexReorderDefaultFlag)

#int DMPlexCreateSubmesh(PetscDM,PetscDMLabel,PetscInt,PetscDM*)
#int DMPlexCreateHybridMesh(PetscDM,PetscDMLabel,PetscDMLabel,PetscInt,PetscDMLabel*,PetscDMLabel*,PetscDM *,PetscDM *)
Expand Down
6 changes: 6 additions & 0 deletions src/dm/f90-mod/petscdmplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@
end type tDMPlexTransform

DMPlexTransform, parameter :: PETSC_NULL_DMPLEXTRANSFORM = tDMPlexTransform(0)
!
! DMPlexReorderDefaultFlag
!
PetscEnum, parameter :: DMPLEX_REORDER_DEFAULT_NOTSET = -1
PetscEnum, parameter :: DMPLEX_REORDER_DEFAULT_FALSE = 0
PetscEnum, parameter :: DMPLEX_REORDER_DEFAULT_TRUE = 1
39 changes: 26 additions & 13 deletions src/dm/impls/plex/plexcreate.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ static PetscErrorCode DMInitialize_Plex(DM dm);
/* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
{
const DMBoundaryType *bd;
const PetscReal *maxCell, *L;
PetscBool isper, dist;
const DMBoundaryType *bd;
const PetscReal *maxCell, *L;
PetscBool isper, dist;
DMPlexReorderDefaultFlag reorder;

PetscFunctionBegin;
if (copyPeriodicity) {
Expand All @@ -25,6 +26,8 @@ PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool
}
PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
PetscCall(DMPlexDistributeSetDefault(dmout, dist));
PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
PetscCall(DMPlexReorderSetDefault(dmout, reorder));
((DM_Plex *) dmout->data)->useHashLocation = ((DM_Plex *) dmin->data)->useHashLocation;
if (copyOverlap) {
PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
Expand Down Expand Up @@ -3427,17 +3430,22 @@ PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOption
static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
{
PetscFunctionList ordlist;
char oname[256];
PetscReal volume = -1.0;
PetscInt prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
PetscBool uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg;
char oname[256];
PetscReal volume = -1.0;
PetscInt prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
PetscBool uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg;
DMPlexReorderDefaultFlag reorder;

PetscFunctionBegin;
PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
PetscOptionsHeadBegin(PetscOptionsObject,"DMPlex Options");
/* Handle automatic creation */
PetscCall(DMGetDimension(dm, &dim));
if (dim < 0) {PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));created = PETSC_TRUE;}
if (dim < 0) {
PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
created = PETSC_TRUE;
}
PetscCall(DMGetDimension(dm, &dim));
/* Handle interpolation before distribution */
PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
if (flg) {
Expand Down Expand Up @@ -3496,9 +3504,12 @@ static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject
extLayers = 0;
}
/* Handle DMPlex reordering before distribution */
PetscCall(DMPlexReorderGetDefault(dm, &reorder));
PetscCall(MatGetOrderingList(&ordlist));
PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
if (flg) {
reorder = (reorder == DMPLEX_REORDER_DEFAULT_TRUE) || flg;
if (reorder) {
DM pdm;
IS perm;

Expand Down Expand Up @@ -3647,16 +3658,15 @@ static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject
PetscCall(DMPlexReplace_Static(dm, &gdm));
}
/* Handle 1D order */
{
if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
DM cdm, rdm;
PetscDS cds;
PetscObject obj;
PetscClassId id = PETSC_OBJECT_CLASSID;
IS perm;
PetscInt dim, Nf;
PetscInt Nf;
PetscBool distributed;

PetscCall(DMGetDimension(dm, &dim));
PetscCall(DMPlexIsDistributed(dm, &distributed));
PetscCall(DMGetCoordinateDM(dm, &cdm));
PetscCall(DMGetDS(cdm, &cds));
Expand All @@ -3665,7 +3675,7 @@ static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject
PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
PetscCall(PetscObjectGetClassId(obj, &id));
}
if (dim == 1 && !distributed && id != PETSCFE_CLASSID) {
if (!distributed && id != PETSCFE_CLASSID) {
PetscCall(DMPlexGetOrdering1D(dm, &perm));
PetscCall(DMPlexPermute(dm, perm, &rdm));
PetscCall(DMPlexReplace_Static(dm, &rdm));
Expand Down Expand Up @@ -3797,6 +3807,8 @@ static PetscErrorCode DMInitialize_Plex(DM dm)
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex));
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex));
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex));
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexReorderGetDefault_C",DMPlexReorderGetDefault_Plex));
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexReorderSetDefault_C",DMPlexReorderSetDefault_Plex));
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeGetDefault_C",DMPlexDistributeGetDefault_Plex));
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeSetDefault_C",DMPlexDistributeSetDefault_Plex));
PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex));
Expand Down Expand Up @@ -3864,6 +3876,7 @@ PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
mesh->refinementUniform = PETSC_TRUE;
mesh->refinementLimit = -1.0;
mesh->distDefault = PETSC_TRUE;
mesh->reorderDefault = DMPLEX_REORDER_DEFAULT_NOTSET;
mesh->interpolated = DMPLEX_INTERPOLATED_INVALID;
mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;

Expand Down
63 changes: 63 additions & 0 deletions src/dm/impls/plex/plexreorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,66 @@ PetscErrorCode DMPlexPermute(DM dm, IS perm, DM *pdm)
(*pdm)->setupcalled = PETSC_TRUE;
PetscFunctionReturn(0);
}

PetscErrorCode DMPlexReorderSetDefault_Plex(DM dm, DMPlexReorderDefaultFlag reorder)
{
DM_Plex *mesh = (DM_Plex *) dm->data;

PetscFunctionBegin;
mesh->reorderDefault = reorder;
PetscFunctionReturn(0);
}

/*@
DMPlexReorderSetDefault - Set flag indicating whether the DM should be reordered by default
Logically collective
Input Parameters:
+ dm - The DM
- reorder - Flag for reordering
Level: intermediate
.seealso: `DMPlexReorderGetDefault()`
@*/
PetscErrorCode DMPlexReorderSetDefault(DM dm, DMPlexReorderDefaultFlag reorder)
{
PetscFunctionBegin;
PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
PetscTryMethod(dm,"DMPlexReorderSetDefault_C",(DM,DMPlexReorderDefaultFlag),(dm,reorder));
PetscFunctionReturn(0);
}

PetscErrorCode DMPlexReorderGetDefault_Plex(DM dm, DMPlexReorderDefaultFlag *reorder)
{
DM_Plex *mesh = (DM_Plex *) dm->data;

PetscFunctionBegin;
*reorder = mesh->reorderDefault;
PetscFunctionReturn(0);
}

/*@
DMPlexReorderGetDefault - Get flag indicating whether the DM should be reordered by default
Not collective
Input Parameter:
. dm - The DM
Output Parameter:
. reorder - Flag for reordering
Level: intermediate
.seealso: `DMPlexReorderSetDefault()`
@*/
PetscErrorCode DMPlexReorderGetDefault(DM dm, DMPlexReorderDefaultFlag *reorder)
{
PetscFunctionBegin;
PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
PetscValidPointer(reorder, 2);
PetscUseMethod(dm,"DMPlexReorderGetDefault_C",(DM,DMPlexReorderDefaultFlag*),(dm,reorder));
PetscFunctionReturn(0);
}

0 comments on commit 2d0aafb

Please sign in to comment.