Skip to content

Commit

Permalink
Parametrize ghost_mode; add cpp demo to doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ampdes committed Aug 14, 2024
1 parent 636f742 commit 77f059f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 55 deletions.
82 changes: 42 additions & 40 deletions cpp/demo/checkpointing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,56 @@ int main(int argc, char* argv[])
MPI_COMM_WORLD, {{{0.0, 0.0}, {1.0, 1.0}}}, {4, 4},
mesh::CellType::quadrilateral, part));

try
{
// Set up ADIOS2 IO and Engine
adios2::ADIOS adios(mesh->comm());
try
{
// Set up ADIOS2 IO and Engine
adios2::ADIOS adios(mesh->comm());

adios2::IO io = adios.DeclareIO("mesh-write");
io.SetEngine("BP5");
adios2::Engine engine = io.Open("mesh.bp", adios2::Mode::Write);
adios2::IO io = adios.DeclareIO("mesh-write");
io.SetEngine("BP5");
adios2::Engine engine = io.Open("mesh.bp", adios2::Mode::Write);

io::native::write_mesh(io, engine, *mesh);
io::native::write_mesh(io, engine, *mesh);

engine.Close();
}
catch (std::exception &e)
{
std::cout << "ERROR: ADIOS2 exception: " << e.what() << "\n";
MPI_Abort(MPI_COMM_WORLD, -1);
}
engine.Close();
}
catch (std::exception& e)
{
std::cout << "ERROR: ADIOS2 exception: " << e.what() << "\n";
MPI_Abort(MPI_COMM_WORLD, -1);
}

try
{
// Set up ADIOS2 IO and Engine
adios2::ADIOS adios_read(MPI_COMM_WORLD);
adios2::IO io_read = adios_read.DeclareIO("mesh-read");
io_read.SetEngine("BP5");
adios2::Engine engine_read = io_read.Open("mesh.bp", adios2::Mode::Read);
try
{
// Set up ADIOS2 IO and Engine
adios2::ADIOS adios_read(MPI_COMM_WORLD);
adios2::IO io_read = adios_read.DeclareIO("mesh-read");
io_read.SetEngine("BP5");
adios2::Engine engine_read = io_read.Open("mesh.bp", adios2::Mode::Read);

engine_read.BeginStep();
auto mesh_read = io::native::read_mesh<float>(io_read, engine_read, MPI_COMM_WORLD);
if (engine_read.BetweenStepPairs())
{
engine_read.EndStep();
}
engine_read.BeginStep();
auto mesh_read
= io::native::read_mesh<float>(io_read, engine_read, MPI_COMM_WORLD);
if (engine_read.BetweenStepPairs())
{
engine_read.EndStep();
}

engine_read.Close();
engine_read.Close();

adios2::IO io_write = adios_read.DeclareIO("mesh-write");
io_write.SetEngine("BP5");
adios2::Engine engine_write = io_write.Open("mesh2.bp", adios2::Mode::Write);
adios2::IO io_write = adios_read.DeclareIO("mesh-write");
io_write.SetEngine("BP5");
adios2::Engine engine_write
= io_write.Open("mesh2.bp", adios2::Mode::Write);

io::native::write_mesh(io_write, engine_write, mesh_read);
engine_write.Close();
}
catch (std::exception &e)
{
std::cout << "ERROR: ADIOS2 exception: " << e.what() << "\n";
MPI_Abort(MPI_COMM_WORLD, -1);
}
io::native::write_mesh(io_write, engine_write, mesh_read);
engine_write.Close();
}
catch (std::exception& e)
{
std::cout << "ERROR: ADIOS2 exception: " << e.what() << "\n";
MPI_Abort(MPI_COMM_WORLD, -1);
}

MPI_Finalize();
return 0;
Expand Down
1 change: 1 addition & 0 deletions cpp/doc/source/demo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ Experimental
:maxdepth: 1

demos/demo_mixed_topology.md
demos/demo_checkpointing.md
21 changes: 14 additions & 7 deletions cpp/dolfinx/io/checkpointing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ template void write_mesh<double>(adios2::IO& io, adios2::Engine& engine,
//-----------------------------------------------------------------------------
template <std::floating_point T>
dolfinx::mesh::Mesh<T> read_mesh(adios2::IO& io, adios2::Engine& engine,
MPI_Comm comm)
MPI_Comm comm,
dolfinx::mesh::GhostMode ghost_mode)
{

int rank, size;
Expand Down Expand Up @@ -234,7 +235,10 @@ dolfinx::mesh::Mesh<T> read_mesh(adios2::IO& io, adios2::Engine& engine,
fem::CoordinateElement<T> element
= fem::CoordinateElement<T>(cell_type, degree, lagrange_variant);

auto part = mesh::create_cell_partitioner(mesh::GhostMode::shared_facet);
auto part = mesh::create_cell_partitioner(ghost_mode);

if (size == 1)
part = nullptr;

mesh::Mesh<T> mesh = mesh::create_mesh(comm, comm, array, element, comm,
x_reduced, x_shape, part);
Expand All @@ -247,10 +251,12 @@ dolfinx::mesh::Mesh<T> read_mesh(adios2::IO& io, adios2::Engine& engine,
//-----------------------------------------------------------------------------
/// @cond
template dolfinx::mesh::Mesh<float>
read_mesh<float>(adios2::IO& io, adios2::Engine& engine, MPI_Comm comm);
read_mesh<float>(adios2::IO& io, adios2::Engine& engine, MPI_Comm comm,
dolfinx::mesh::GhostMode ghost_mode);

template dolfinx::mesh::Mesh<double>
read_mesh<double>(adios2::IO& io, adios2::Engine& engine, MPI_Comm comm);
read_mesh<double>(adios2::IO& io, adios2::Engine& engine, MPI_Comm comm,
dolfinx::mesh::GhostMode ghost_mode);

/// @endcond

Expand Down Expand Up @@ -354,15 +360,16 @@ std::vector<int64_t> read_topology_data(adios2::IO& io, adios2::Engine& engine,

//-----------------------------------------------------------------------------
std::variant<dolfinx::mesh::Mesh<float>, dolfinx::mesh::Mesh<double>>
read_mesh_variant(adios2::IO& io, adios2::Engine& engine, MPI_Comm comm)
read_mesh_variant(adios2::IO& io, adios2::Engine& engine, MPI_Comm comm,
dolfinx::mesh::GhostMode ghost_mode)
{
engine.BeginStep();
std::string floating_point = io.VariableType("x");

if (floating_point == "float")
{
dolfinx::mesh::Mesh<float> mesh
= dolfinx::io::native::read_mesh<float>(io, engine, comm);
= dolfinx::io::native::read_mesh<float>(io, engine, comm, ghost_mode);
if (engine.BetweenStepPairs())
{
engine.EndStep();
Expand All @@ -372,7 +379,7 @@ read_mesh_variant(adios2::IO& io, adios2::Engine& engine, MPI_Comm comm)
else if (floating_point == "double")
{
dolfinx::mesh::Mesh<double> mesh
= dolfinx::io::native::read_mesh<double>(io, engine, comm);
= dolfinx::io::native::read_mesh<double>(io, engine, comm, ghost_mode);
if (engine.BetweenStepPairs())
{
engine.EndStep();
Expand Down
10 changes: 8 additions & 2 deletions cpp/dolfinx/io/checkpointing.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <adios2.h>
#include <basix/finite-element.h>
#include <dolfinx/mesh/Mesh.h>
#include <dolfinx/mesh/utils.h>
#include <mpi.h>

/// @file checkpointing.h
Expand All @@ -37,7 +38,9 @@ void write_mesh(adios2::IO& io, adios2::Engine& engine,
/// @return mesh reconstructed from the data
template <std::floating_point T>
dolfinx::mesh::Mesh<T> read_mesh(adios2::IO& io, adios2::Engine& engine,
MPI_Comm comm = MPI_COMM_WORLD);
MPI_Comm comm = MPI_COMM_WORLD,
dolfinx::mesh::GhostMode ghost_mode
= dolfinx::mesh::GhostMode::shared_facet);

} // namespace dolfinx::io::native

Expand Down Expand Up @@ -84,10 +87,13 @@ std::vector<int64_t> read_topology_data(adios2::IO& io, adios2::Engine& engine,
/// @param[in] io ADIOS2 IO
/// @param[in] engine ADIOS2 Engine
/// @param[in] comm comm
/// @param[in] ghost_mode The requested type of cell ghosting/overlap
/// @return mesh reconstructed from the data
std::variant<dolfinx::mesh::Mesh<float>, dolfinx::mesh::Mesh<double>>
read_mesh_variant(adios2::IO& io, adios2::Engine& engine,
MPI_Comm comm = MPI_COMM_WORLD);
MPI_Comm comm = MPI_COMM_WORLD,
dolfinx::mesh::GhostMode ghost_mode
= dolfinx::mesh::GhostMode::shared_facet);

} // namespace dolfinx::io::impl_native

Expand Down
6 changes: 4 additions & 2 deletions python/dolfinx/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,11 @@ def write_mesh(ADIOS2: ADIOS2, mesh: Mesh) -> None:

return _writer(ADIOS2, mesh._cpp_object)

def read_mesh(ADIOS2: ADIOS2, comm: _MPI.Comm) -> Mesh:
def read_mesh(
ADIOS2: ADIOS2, comm: _MPI.Comm, ghost_mode: GhostMode = GhostMode.shared_facet
) -> Mesh:
"""Read mesh from a file using ADIOS2"""
msh = _cpp.io.read_mesh(ADIOS2, comm)
msh = _cpp.io.read_mesh(ADIOS2, comm, ghost_mode)

return msh

Expand Down
10 changes: 6 additions & 4 deletions python/dolfinx/wrappers/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,16 @@ void io(nb::module_& m)
// dolfinx::io::impl_native::read_mesh_variant
m.def(
"read_mesh",
[](dolfinx::io::ADIOS2Wrapper& ADIOS2, MPICommWrapper comm)
[](dolfinx::io::ADIOS2Wrapper& ADIOS2, MPICommWrapper comm,
dolfinx::mesh::GhostMode ghost_mode)
{
auto io = ADIOS2.io();
auto engine = ADIOS2.engine();
return dolfinx::io::impl_native::read_mesh_variant(*io, *engine,
comm.get());
return dolfinx::io::impl_native::read_mesh_variant(
*io, *engine, comm.get(), ghost_mode);
},
nb::arg("adios2"), nb::arg("comm"), "Read mesh from file using ADIOS2");
nb::arg("adios2"), nb::arg("comm"), nb::arg("ghost_mode"),
"Read mesh from file using ADIOS2");

declare_write_mesh<float>(m, "float32");
declare_write_mesh<double>(m, "float64");
Expand Down

0 comments on commit 77f059f

Please sign in to comment.