Skip to content

Commit

Permalink
Add ADIOS2Engine class adapted from ADIOS2Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
ampdes committed Jul 9, 2024
1 parent 5dd47c6 commit 0e1b770
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 20 deletions.
7 changes: 7 additions & 0 deletions cpp/demo/checkpointing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ int main(int argc, char* argv[])
MPI_COMM_WORLD, {{{0.0, 0.0}, {1.0, 1.0}}}, {4, 4},
mesh::CellType::quadrilateral, part));

auto writer = io::ADIOS2Engine(mesh->comm(), "mesh.bp", "mesh-write", "BP5",
adios2::Mode::Write);

// auto io = writer.io();
// auto engine = writer.engine();

// io::checkpointing::write(io, engine, mesh);
io::checkpointing::write(mesh->comm(), "mesh.bp", "mesh-write", mesh);

MPI_Finalize();
Expand Down
50 changes: 50 additions & 0 deletions cpp/dolfinx/io/ADIOS2_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2021-2023 Jørgen S. Dokken and Garth N. Wells
//
// This file is part of DOLFINX (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifdef HAS_ADIOS2

#include "ADIOS2_utils.h"
#include <adios2.h>
#include <mpi.h>

using namespace dolfinx::io;

//-----------------------------------------------------------------------------
ADIOS2Engine::ADIOS2Engine(MPI_Comm comm, const std::filesystem::path& filename,
std::string tag, std::string engine,
const adios2::Mode mode)

: _adios(std::make_unique<adios2::ADIOS>(comm)),
_io(std::make_unique<adios2::IO>(_adios->DeclareIO(tag)))
{
_io->SetEngine(engine);
_engine = std::make_unique<adios2::Engine>(_io->Open(filename, mode));
}
//-----------------------------------------------------------------------------
ADIOS2Engine::~ADIOS2Engine() { close(); }
//-----------------------------------------------------------------------------
void ADIOS2Engine::close()
{
assert(_engine);
if (*_engine)
_engine->Close();
}
// //-----------------------------------------------------------------------------
// std::unique_ptr<adios2::IO> ADIOS2Engine::io()
// {
// assert(_io);
// if (*_io)
// return _io;
// }
// //-----------------------------------------------------------------------------
// std::unique_ptr<adios2::Engine> ADIOS2Engine::engine()
// {
// assert(_engine);
// if (*_engine)
// return _engine;
// }

#endif
66 changes: 66 additions & 0 deletions cpp/dolfinx/io/ADIOS2_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2021-2023 Jørgen S. Dokken and Garth N. Wells
//
// This file is part of DOLFINX (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#pragma once

#ifdef HAS_ADIOS2

#include <adios2.h>
#include <cassert>
#include <filesystem>
#include <mpi.h>

/// @file ADIOS2_utils.h
/// @brief Utils for ADIOS2

namespace dolfinx::io
{
class ADIOS2Engine
{
public:
/// @brief Create an ADIOS2-based engine writer/reader
/// @param[in] comm The MPI communicator
/// @param[in] filename Name of output file
/// @param[in] tag The ADIOS2 object name
/// @param[in] engine ADIOS2 engine type. See
/// https://adios2.readthedocs.io/en/latest/engines/engines.html.
ADIOS2Engine(MPI_Comm comm, const std::filesystem::path& filename,
std::string tag, std::string engine = "BP5",
const adios2::Mode mode = adios2::Mode::Write);

/// @brief Move constructor
ADIOS2Engine(ADIOS2Engine&& engine) = default;

/// @brief Copy constructor
ADIOS2Engine(const ADIOS2Engine&) = delete;

/// @brief Destructor
~ADIOS2Engine();

/// @brief Move assignment
ADIOS2Engine& operator=(ADIOS2Engine&& engine) = default;

// Copy assignment
ADIOS2Engine& operator=(const ADIOS2Engine&) = delete;

/// @brief Close the file
void close();

// /// @brief Get the IO object
// std::unique_ptr<adios2::IO> io();

// /// @brief Close the Engine object
// std::unique_ptr<adios2::Engine> engine();

protected:
std::unique_ptr<adios2::ADIOS> _adios;
std::unique_ptr<adios2::IO> _io;
std::unique_ptr<adios2::Engine> _engine;
};

} // namespace dolfinx::io

#endif
4 changes: 3 additions & 1 deletion cpp/dolfinx/io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(HEADERS_io
${CMAKE_CURRENT_SOURCE_DIR}/dolfinx_io.h
${CMAKE_CURRENT_SOURCE_DIR}/ADIOS2_utils.h
${CMAKE_CURRENT_SOURCE_DIR}/ADIOS2Writers.h
${CMAKE_CURRENT_SOURCE_DIR}/cells.h
${CMAKE_CURRENT_SOURCE_DIR}/checkpointing.h
Expand All @@ -15,7 +16,8 @@ set(HEADERS_io

target_sources(
dolfinx
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ADIOS2Writers.cpp
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ADIOS2_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ADIOS2Writers.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cells.cpp
${CMAKE_CURRENT_SOURCE_DIR}/checkpointing.cpp
${CMAKE_CURRENT_SOURCE_DIR}/HDF5Interface.cpp
Expand Down
47 changes: 28 additions & 19 deletions cpp/dolfinx/io/checkpointing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ std::map<basix::element::lagrange_variant, std::string> lagrange_variants{

template <std::floating_point T>
void _write(MPI_Comm comm, std::string filename, std::string tag,
// adios2::IO io, adios2::Engine engine,
std::shared_ptr<dolfinx::mesh::Mesh<T>> mesh)
{
adios2::ADIOS adios(comm);
Expand All @@ -51,7 +52,8 @@ void _write(MPI_Comm comm, std::string filename, std::string tag,

auto cmap = mesh->geometry().cmap();
auto geom_layout = cmap.create_dof_layout();
std::uint32_t num_dofs_per_cell = geom_layout.num_entity_closure_dofs(mesh_dim);
std::uint32_t num_dofs_per_cell
= geom_layout.num_entity_closure_dofs(mesh_dim);

const std::vector<int64_t> mesh_input_global_indices
= geometry.input_global_indices();
Expand All @@ -68,33 +70,36 @@ void _write(MPI_Comm comm, std::string filename, std::string tag,
std::vector<std::int64_t> connectivity_nodes_global(
indices_offsets[num_cells_local]);

std::iota(connectivity_nodes_global.begin(), connectivity_nodes_global.end(), 0);
std::iota(connectivity_nodes_global.begin(), connectivity_nodes_global.end(),
0);

std::cout << indices.size() << "\n";
std::cout << indices_offsets[num_cells_local] << "\n";
std::cout << indices_offsets[num_cells_local-1] << "\n";

// for (std::size_t i = 0; i < connectivity_nodes_global.size(); ++i)
// {
// std::cout << i << " ";
// std::cout << indices[i] << " ";
// std::cout << mesh_input_global_indices[indices[i]] << "\n";
// connectivity_nodes_global[i] = mesh_input_global_indices[indices[i]];
// }

std::cout << indices_span.subspan(0, indices_offsets[num_cells_local]).size() << std::endl;
std::cout << indices_offsets[num_cells_local - 1] << "\n";

// for (std::size_t i = 0; i < connectivity_nodes_global.size(); ++i)
// {
// std::cout << i << " ";
// std::cout << indices[i] << " ";
// std::cout << mesh_input_global_indices[indices[i]] << "\n";
// connectivity_nodes_global[i] =
// mesh_input_global_indices[indices[i]];
// }

std::cout << indices_span.subspan(0, indices_offsets[num_cells_local]).size()
<< std::endl;
std::cout << indices_offsets[num_cells_local] << std::endl;
imap->local_to_global(
indices_span.subspan(0, indices_offsets[num_cells_local]),
connectivity_nodes_global);

for (std::size_t i = 0; i < connectivity_nodes_global.size(); ++i)
{
std::cout << i << " ";
std::cout << indices[i] << " ";
std::cout << connectivity_nodes_global[i] << " ";
std::cout << mesh_input_global_indices[indices[i]] << "\n";
}
{
std::cout << i << " ";
std::cout << indices[i] << " ";
std::cout << connectivity_nodes_global[i] << " ";
std::cout << mesh_input_global_indices[indices[i]] << "\n";
}

for (std::size_t i = 0; i < indices_offsets.size(); ++i)
{
Expand Down Expand Up @@ -159,18 +164,22 @@ using namespace dolfinx::io::checkpointing;
//-----------------------------------------------------------------------------
void dolfinx::io::checkpointing::write(
MPI_Comm comm, std::string filename, std::string tag,
// adios2::IO io, adios2::Engine engine,
std::shared_ptr<dolfinx::mesh::Mesh<float>> mesh)
{

// _write(io, engine, mesh);
_write(comm, filename, tag, mesh);
}

//-----------------------------------------------------------------------------
void dolfinx::io::checkpointing::write(
MPI_Comm comm, std::string filename, std::string tag,
// adios2::IO io, adios2::Engine engine,
std::shared_ptr<dolfinx::mesh::Mesh<double>> mesh)
{

// _write(io, engine, mesh);
_write(comm, filename, tag, mesh);
}

Expand Down
3 changes: 3 additions & 0 deletions cpp/dolfinx/io/checkpointing.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#ifdef HAS_ADIOS2

#include "ADIOS2_utils.h"
#include <adios2.h>
#include <basix/finite-element.h>
#include <dolfinx/mesh/Mesh.h>
Expand All @@ -20,9 +21,11 @@ namespace dolfinx::io::checkpointing
{

void write(MPI_Comm comm, std::string filename, std::string tag,
// adios2::IO io, adios2::Engine engine,
std::shared_ptr<dolfinx::mesh::Mesh<float>> mesh);

void write(MPI_Comm comm, std::string filename, std::string tag,
// adios2::IO io, adios2::Engine engine,
std::shared_ptr<dolfinx::mesh::Mesh<double>> mesh);

} // namespace dolfinx::io::checkpointing
Expand Down
1 change: 1 addition & 0 deletions cpp/dolfinx/io/dolfinx_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace dolfinx::io

// DOLFINx io interface

#include <dolfinx/io/ADIOS2_utils.h>
#include <dolfinx/io/ADIOS2Writers.h>
#include <dolfinx/io/VTKFile.h>
#include <dolfinx/io/checkpointing.h>

0 comments on commit 0e1b770

Please sign in to comment.