def _ensure_cachedir(comm=None): """Ensure that the TSFC kernel cache directory exists.""" comm = dup_comm(comm or COMM_WORLD) if comm.rank == 0: if not path.exists(TSFCKernel._cachedir): makedirs(TSFCKernel._cachedir) free_comm(comm)
def _from_cell_list(dim, cells, coords, comm): """ Create a DMPlex from a list of cells and coords. :arg dim: The topological dimension of the mesh :arg cells: The vertices of each cell :arg coords: The coordinates of each vertex :arg comm: communicator to build the mesh on. """ comm = dup_comm(comm) if comm.rank == 0: cells = np.asarray(cells, dtype=PETSc.IntType) coords = np.asarray(coords, dtype=float) comm.bcast(cells.shape, root=0) comm.bcast(coords.shape, root=0) # Provide the actual data on rank 0. plex = PETSc.DMPlex().createFromCellList(dim, cells, coords, comm=comm) else: cell_shape = list(comm.bcast(None, root=0)) coord_shape = list(comm.bcast(None, root=0)) cell_shape[0] = 0 coord_shape[0] = 0 # Provide empty plex on other ranks # A subsequent call to plex.distribute() takes care of parallel partitioning plex = PETSc.DMPlex().createFromCellList(dim, np.zeros(cell_shape, dtype=PETSc.IntType), np.zeros(coord_shape, dtype=float), comm=comm) free_comm(comm) return plex
def clear_cache(comm=None): """Clear the Firedrake TSFC kernel cache.""" comm = dup_comm(comm or COMM_WORLD) if comm.rank == 0: if path.exists(TSFCKernel._cachedir): import shutil shutil.rmtree(TSFCKernel._cachedir, ignore_errors=True) _ensure_cachedir(comm=comm) free_comm(comm)
def __del__(self): self.close() if hasattr(self, "comm"): free_comm(self.comm) del self.comm
from collections import defaultdict from pyop2.mpi import COMM_WORLD, MPI, dup_comm, free_comm from firedrake.logging import debug, warning from firedrake.parameters import parameters from firedrake.petsc import PETSc try: # Estimate the amount of memory per core may use. import psutil memory = np.array([psutil.virtual_memory().total/psutil.cpu_count()]) if COMM_WORLD.size > 1: comm = dup_comm(COMM_WORLD) comm.Allreduce(MPI.IN_PLACE, memory, MPI.MIN) free_comm(comm) except (ImportError, AttributeError): memory = None class _DependencySnapshot(object): """Record the dependencies of a form at a particular point in order to establish whether a cached form is valid.""" def __init__(self, form): # For each dependency, we store a weak reference and the # current version number. ref = lambda dep: (weakref.ref(dep), dep.dat._version) deps = []
def _from_triangle(filename, dim, comm): """Read a set of triangle mesh files from `filename`. :arg dim: The embedding dimension. :arg comm: communicator to build the mesh on. """ basename, ext = os.path.splitext(filename) comm = dup_comm(comm) if comm.rank == 0: try: facetfile = open(basename+".face") tdim = 3 except: try: facetfile = open(basename+".edge") tdim = 2 except: facetfile = None tdim = 1 if dim is None: dim = tdim comm.bcast(tdim, root=0) with open(basename+".node") as nodefile: header = np.fromfile(nodefile, dtype=np.int32, count=2, sep=' ') nodecount = header[0] nodedim = header[1] assert nodedim == dim coordinates = np.loadtxt(nodefile, usecols=range(1, dim+1), skiprows=1) assert nodecount == coordinates.shape[0] with open(basename+".ele") as elefile: header = np.fromfile(elefile, dtype=np.int32, count=2, sep=' ') elecount = header[0] eledim = header[1] eles = np.loadtxt(elefile, usecols=range(1, eledim+1), dtype=np.int32, skiprows=1) assert elecount == eles.shape[0] cells = map(lambda c: c-1, eles) else: tdim = comm.bcast(None, root=0) cells = None coordinates = None plex = _from_cell_list(tdim, cells, coordinates, comm=comm) # Apply boundary IDs if comm.rank == 0: facets = None try: header = np.fromfile(facetfile, dtype=np.int32, count=2, sep=' ') edgecount = header[0] facets = np.loadtxt(facetfile, usecols=range(1, tdim+2), dtype=np.int32, skiprows=0) assert edgecount == facets.shape[0] finally: facetfile.close() if facets is not None: vStart, vEnd = plex.getDepthStratum(0) # vertices for facet in facets: bid = facet[-1] vertices = map(lambda v: v + vStart - 1, facet[:-1]) join = plex.getJoin(vertices) plex.setLabelValue("boundary_ids", join[0], bid) free_comm(comm) return plex