Debian Codegraph @

docs/partitioned_mesh.py

raw
1 # # Storing mesh partition
2 # This data is re-ordered when reading in a mesh, as the mesh is partitioned.
3 # This means that when storing the mesh to disk from DOLFINx, the geometry and
4 # connectivity arrays are re-ordered.
5 # If we want to avoid to re-partition the mesh every time you run a simulation
6 # (on a fixed number of processes), one can store the partitioning of the mesh
7 # in the checkpoint. This is done by setting the flag `store_partition_info=True`
8 # when calling {py:func}`io4dolfinx.write_mesh`.
9
10 # +
11 import logging
12 from pathlib import Path
13
14 import ipyparallel as ipp
15
16
17 def write_partitioned_mesh(filename: Path):
18 import subprocess
19
20 from mpi4py import MPI
21
22 import dolfinx
23
24 import io4dolfinx
25
26 # Create a simple unit square mesh
27 mesh = dolfinx.mesh.create_unit_square(
28 MPI.COMM_WORLD,
29 10,
30 10,
31 cell_type=dolfinx.mesh.CellType.quadrilateral,
32 ghost_mode=dolfinx.mesh.GhostMode.shared_facet,
33 )
34
35 # Write mesh checkpoint
36 io4dolfinx.write_mesh(
37 filename, mesh, backend="adios2", backend_args={"engine": "BP4"}, store_partition_info=True
38 )
39 # Inspect checkpoint on rank 0 with `bpls`
40 if mesh.comm.rank == 0:
41 output = subprocess.run(["bpls", "-a", "-l", filename], capture_output=True)
42 print(output.stdout.decode("utf-8"))
43
44
45 # -
46
47 # We inspect the partitioned mesh
48
49 # +
50 mesh_file = Path("partitioned_mesh.bp")
51 n = 3
52
53 with ipp.Cluster(engines="mpi", n=n, log_level=logging.ERROR) as cluster:
54 query = cluster[:].apply_async(write_partitioned_mesh, mesh_file)
55 query.wait()
56 assert query.successful(), query.error
57 print("".join(query.stdout))
58 # -
59
60 # # Reading a partitioned mesh
61
62 # If we try to read the mesh in on a different number of processes, we will get an error.
63 # We illustrate this below, by first trying to read the mesh using partitioning information,
64 # which is done by setting the flag `read_from_partition=True` when calling
65 # {py:func}`io4dolfinx.read_mesh`.
66
67
68 def read_partitioned_mesh(filename: Path, read_from_partition: bool = True):
69 from mpi4py import MPI
70
71 import io4dolfinx
72
73 prefix = f"{MPI.COMM_WORLD.rank + 1}/{MPI.COMM_WORLD.size}: "
74 try:
75 mesh = io4dolfinx.read_mesh(
76 filename,
77 comm=MPI.COMM_WORLD,
78 backend="adios2",
79 backend_args={"engine": "BP4"},
80 read_from_partition=read_from_partition,
81 )
82 print(f"{prefix} Mesh: {mesh.name} read successfully with {read_from_partition=}")
83 except ValueError as e:
84 print(f"{prefix} Caught exception: ", e)
85
86
87 with ipp.Cluster(engines="mpi", n=n + 1, log_level=logging.ERROR) as cluster:
88 # Read mesh from file with different number of processes
89 query = cluster[:].apply_async(read_partitioned_mesh, mesh_file)
90 query.wait()
91 assert query.successful(), query.error
92 print("".join(query.stdout))
93
94 # Read mesh from file with different number of processes (not using partitioning information).
95 # If we instead turn of `read_from_partition`, we can read the mesh on a
96 # different number of processes.
97
98 with ipp.Cluster(engines="mpi", n=n + 1, log_level=logging.ERROR) as cluster:
99 query = cluster[:].apply_async(read_partitioned_mesh, mesh_file, False)
100 query.wait()
101 assert query.successful(), query.error
102 print("".join(query.stdout))
103
104 # Read mesh from file with same number of processes as was written,
105 # re-using partitioning information.
106
107 with ipp.Cluster(engines="mpi", n=n, log_level=logging.ERROR) as cluster:
108 query = cluster[:].apply_async(read_partitioned_mesh, mesh_file, True)
109 query.wait()
110 assert query.successful(), query.error
111 print("".join(query.stdout))