Skip to content

remove mesh.init() #4201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ jobs:
: # because they rely on non-PyPI versions of petsc4py.
pip install --no-build-isolation --no-deps \
"$PETSC_DIR"/"$PETSC_ARCH"/externalpackages/git.slepc/src/binding/slepc4py
pip install --no-deps ngsPETSc netgen-mesher netgen-occt
pip install --no-deps git+https://github.com/ksagiyam/ngsPETSc.git@ksagiyam/remove_mesh_init netgen-mesher netgen-occt

: # We have to pass '--no-build-isolation' to use a custom petsc4py
EXTRA_BUILD_ARGS='--no-isolation'
Expand Down
4 changes: 0 additions & 4 deletions firedrake/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,6 @@ def wrapper(self, *args, **kwargs):

def __init__(self, form, bcs=None, form_compiler_parameters=None):
super().__init__(form, bcs=bcs, form_compiler_parameters=form_compiler_parameters)
# Ensure mesh is 'initialised' as we could have got here without building a
# function space (e.g. if integrating a constant).
for mesh in form.ufl_domains():
mesh.init()
if any(c.dat.dtype != ScalarType for c in form.coefficients()):
raise ValueError("Cannot assemble a form containing coefficients where the "
"dtype is not the PETSc scalar type.")
Expand Down
24 changes: 6 additions & 18 deletions firedrake/checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,6 @@ def save_mesh(self, mesh, distribution_name=None, permutation_name=None):
:kwarg distribution_name: the name under which distribution is saved; if `None`, auto-generated name will be used.
:kwarg permutation_name: the name under which permutation is saved; if `None`, auto-generated name will be used.
"""
mesh.init()
# Handle extruded mesh
tmesh = mesh.topology
if mesh.extruded:
Expand Down Expand Up @@ -669,7 +668,6 @@ def save_mesh(self, mesh, distribution_name=None, permutation_name=None):
@PETSc.Log.EventDecorator("SaveMeshTopology")
def _save_mesh_topology(self, tmesh):
# -- Save DMPlex --
tmesh.init()
topology_dm = tmesh.topology_dm
tmesh_name = topology_dm.getName()
distribution_name = tmesh._distribution_name
Expand Down Expand Up @@ -1048,7 +1046,6 @@ def load_mesh(self, name=DEFAULT_MESH_NAME, reorder=None, distribution_parameter
# -- Load mesh topology --
base_tmesh_name = self.get_attr(path, PREFIX_EXTRUDED + "_base_mesh")
base_tmesh = self._load_mesh_topology(base_tmesh_name, reorder, distribution_parameters)
base_tmesh.init()
periodic = self.get_attr(path, PREFIX_EXTRUDED + "_periodic") if self.has_attr(path, PREFIX_EXTRUDED + "_periodic") else False
variable_layers = self.get_attr(path, PREFIX_EXTRUDED + "_variable_layers")
if variable_layers:
Expand Down Expand Up @@ -1106,9 +1103,6 @@ def load_mesh(self, name=DEFAULT_MESH_NAME, reorder=None, distribution_parameter
# tmesh.topology_dm has already been redistributed.
path = self._path_to_mesh(tmesh_name, name)
# Load firedrake coordinates directly.
# When implementing checkpointing for MeshHierarchy in the future,
# we will need to postpone calling tmesh.init().
tmesh.init()
coord_element = self._load_ufl_element(path, PREFIX + "_coordinate_element")
coord_name = self.get_attr(path, PREFIX + "_coordinates")
coordinates = self._load_function_topology(tmesh, coord_element, coord_name)
Expand Down Expand Up @@ -1194,7 +1188,13 @@ def _load_mesh_topology(self, tmesh_name, reorder, distribution_parameters):
plex.distributionSetName(distribution_name)
sfXB = plex.topologyLoad(self.viewer)
plex.distributionSetName(None)
plex.labelsLoad(self.viewer, sfXB)
self.viewer.popFormat()
# These labels are distribution dependent.
# We should be able to save/load labels selectively.
plex.removeLabel("pyop2_core")
plex.removeLabel("pyop2_owned")
plex.removeLabel("pyop2_ghost")
if load_distribution_permutation:
chart_size = np.empty(1, dtype=utils.IntType)
chart_sizes_iset = PETSc.IS().createGeneral(chart_size, comm=self._comm)
Expand All @@ -1220,21 +1220,10 @@ def _load_mesh_topology(self, tmesh_name, reorder, distribution_parameters):
distribution_parameters=distribution_parameters, sfXB=sfXB, perm_is=perm_is,
distribution_name=distribution_name, permutation_name=permutation_name,
comm=self.comm)
self.viewer.pushFormat(format=format)
# tmesh.topology_dm has already been redistributed.
sfXCtemp = tmesh.sfXB.compose(tmesh.sfBC) if tmesh.sfBC is not None else tmesh.sfXB
plex.labelsLoad(self.viewer, sfXCtemp)
self.viewer.popFormat()
# These labels are distribution dependent.
# We should be able to save/load labels selectively.
plex.removeLabel("pyop2_core")
plex.removeLabel("pyop2_owned")
plex.removeLabel("pyop2_ghost")
return tmesh

@PETSc.Log.EventDecorator("LoadFunctionSpace")
def _load_function_space(self, mesh, name):
mesh.init()
mesh_key = self._generate_mesh_key_from_names(mesh.name,
mesh.topology._distribution_name,
mesh.topology._permutation_name)
Expand Down Expand Up @@ -1271,7 +1260,6 @@ def _load_function_space(self, mesh, name):

@PETSc.Log.EventDecorator("LoadFunctionSpaceTopology")
def _load_function_space_topology(self, tmesh, element):
tmesh.init()
if element.family() == "Real":
return impl.RealFunctionSpace(tmesh, element, "unused_name")
tmesh_key = self._generate_mesh_key_from_names(tmesh.name,
Expand Down
40 changes: 38 additions & 2 deletions firedrake/cython/dmcommon.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3818,7 +3818,8 @@ def create_halo_exchange_sf(PETSc.DM dm):
def submesh_create(PETSc.DM dm,
PetscInt subdim,
label_name,
PetscInt label_value):
PetscInt label_value,
PetscBool ignore_label_halo):
"""Create submesh.

Parameters
Expand All @@ -3831,6 +3832,8 @@ def submesh_create(PETSc.DM dm,
Name of the label
label_value : int
Value in the label
ignore_label_halo : bool
If labeled points in the halo are ignored.

"""
cdef:
Expand Down Expand Up @@ -3860,7 +3863,7 @@ def submesh_create(PETSc.DM dm,
CHKERR(ISRestoreIndices(stratum_is, &stratum_indices))
CHKERR(ISDestroy(&stratum_is))
# Make submesh using temp_label.
CHKERR(DMPlexFilter(dm.dm, temp_label.dmlabel, label_value, PETSC_FALSE, PETSC_TRUE, &ownership_transfer_sf.sf, &subdm.dm))
CHKERR(DMPlexFilter(dm.dm, temp_label.dmlabel, label_value, ignore_label_halo, PETSC_TRUE, &ownership_transfer_sf.sf, &subdm.dm))
# Destroy temp_label.
dm.removeLabel(temp_label_name)
subdm.removeLabel(temp_label_name)
Expand Down Expand Up @@ -4112,3 +4115,36 @@ def submesh_create_cell_closure(
CHKERR(PetscFree(subpoint_indices_inv))
CHKERR(ISRestoreIndices(subpoint_is.iset, &subpoint_indices))
return subcell_closure


@cython.boundscheck(False)
@cython.wraparound(False)
def get_dm_cell_types(PETSc.DM dm):
"""Return all cell types in the mesh.

Parameters
----------
dm : PETSc.DM
The parent dm.

Returns
-------
tuple
Tuple of all cell types in the mesh.

"""
cdef:
PetscInt cStart, cEnd, c
np.ndarray found, found_all
PetscDMPolytopeType celltype

cStart, cEnd = dm.getHeightStratum(0)
found = np.zeros((DM_NUM_POLYTOPES, ), dtype=IntType)
found_all = np.zeros((DM_NUM_POLYTOPES, ), dtype=IntType)
for c in range(cStart, cEnd):
CHKERR(DMPlexGetCellType(dm.dm, c, &celltype))
found[celltype] = 1
dm.comm.tompi4py().Allreduce(found, found_all, op=MPI.MAX)
return tuple(
polytope_type_enum for polytope_type_enum, found in enumerate(found_all) if found
)
24 changes: 24 additions & 0 deletions firedrake/cython/petschdr.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ cdef extern from "petscsys.h" nogil:
int PetscFree2(void*,void*)
int PetscSortIntWithArray(PetscInt,PetscInt[],PetscInt[])

cdef extern from "petscdmtypes.h" nogil:
ctypedef enum PetscDMPolytopeType "DMPolytopeType":
DM_POLYTOPE_POINT
DM_POLYTOPE_SEGMENT
DM_POLYTOPE_POINT_PRISM_TENSOR
DM_POLYTOPE_TRIANGLE
DM_POLYTOPE_QUADRILATERAL
DM_POLYTOPE_SEG_PRISM_TENSOR
DM_POLYTOPE_TETRAHEDRON
DM_POLYTOPE_HEXAHEDRON
DM_POLYTOPE_TRI_PRISM
DM_POLYTOPE_TRI_PRISM_TENSOR
DM_POLYTOPE_QUAD_PRISM_TENSOR
DM_POLYTOPE_PYRAMID
DM_POLYTOPE_FV_GHOST
DM_POLYTOPE_INTERIOR_GHOST
DM_POLYTOPE_UNKNOWN
DM_POLYTOPE_UNKNOWN_CELL
DM_POLYTOPE_UNKNOWN_FACE
DM_NUM_POLYTOPES

cdef extern from "petscdmplex.h" nogil:
int DMPlexGetHeightStratum(PETSc.PetscDM,PetscInt,PetscInt*,PetscInt*)
int DMPlexGetDepthStratum(PETSc.PetscDM,PetscInt,PetscInt*,PetscInt*)
Expand Down Expand Up @@ -56,6 +77,9 @@ cdef extern from "petscdmplex.h" nogil:
int DMPlexGetSubpointMap(PETSc.PetscDM,PETSc.PetscDMLabel*)
int DMPlexSetSubpointMap(PETSc.PetscDM,PETSc.PetscDMLabel)

int DMPlexSetCellType(PETSc.PetscDM,PetscInt,PetscDMPolytopeType)
int DMPlexGetCellType(PETSc.PetscDM,PetscInt,PetscDMPolytopeType*)

cdef extern from "petscdmlabel.h" nogil:
struct _n_DMLabel
ctypedef _n_DMLabel* DMLabel "DMLabel"
Expand Down
1 change: 0 additions & 1 deletion firedrake/functionspaceimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ def collapse(self):
@classmethod
def make_function_space(cls, mesh, element, name=None):
r"""Factory method for :class:`.WithGeometryBase`."""
mesh.init()
topology = mesh.topology
# Create a new abstract (Mixed/Real)FunctionSpace, these are neither primal nor dual.
if type(element) is finat.ufl.MixedElement:
Expand Down
Loading
Loading