Skip to content

Commit

Permalink
Merge branch 'BaseLibMPI' into 'master'
Browse files Browse the repository at this point in the history
Small MPI wrappers for gather and reduce operations

See merge request ogs/ogs!5149
  • Loading branch information
endJunction committed Nov 25, 2024
2 parents b2817c6 + ff9e84a commit a624b9d
Show file tree
Hide file tree
Showing 93 changed files with 637 additions and 1,458 deletions.
7 changes: 4 additions & 3 deletions Applications/ApplicationsLib/LinearSolverLibrarySetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/// The default implementation is empty providing polymorphic behaviour when
/// using this class.

#include "BaseLib/MPI.h"
#include "NumLib/DOF/GlobalMatrixProviders.h"

#if defined(USE_PETSC)
Expand All @@ -28,9 +29,8 @@ namespace ApplicationsLib
{
struct LinearSolverLibrarySetup final
{
LinearSolverLibrarySetup(int argc, char* argv[])
LinearSolverLibrarySetup(int argc, char* argv[]) : mpi_setup(argc, argv)
{
MPI_Init(&argc, &argv);
char help[] = "ogs6 with PETSc \n";
PetscInitialize(&argc, &argv, nullptr, help);
MPI_Comm_set_errhandler(PETSC_COMM_WORLD, MPI_ERRORS_RETURN);
Expand All @@ -40,8 +40,9 @@ struct LinearSolverLibrarySetup final
{
NumLib::cleanupGlobalMatrixProviders();
PetscFinalize();
MPI_Finalize();
}

BaseLib::MPI::Setup mpi_setup;
};
} // ApplicationsLib
#elif defined(USE_LIS)
Expand Down
13 changes: 5 additions & 8 deletions Applications/ApplicationsLib/TestDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#include "BaseLib/ConfigTree.h"
#include "BaseLib/Error.h"
#include "BaseLib/FileTools.h"
#ifdef USE_PETSC
#include <petsc.h>
#include "BaseLib/MPI.h"

#ifdef USE_PETSC
#include "MeshLib/IO/VtkIO/VtuInterface.h" // For petsc file name conversion.
#endif

Expand Down Expand Up @@ -197,16 +197,13 @@ TestDefinition::TestDefinition(BaseLib::ConfigTree const& config_tree,
//! \ogs_file_param{prj__test_definition__vtkdiff__file}
vtkdiff_config.getConfigParameter<std::string>("file");
#ifdef USE_PETSC
int mpi_size;
MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
if (mpi_size > 1)
BaseLib::MPI::Mpi mpi;
if (mpi.size > 1)
{
int rank;
MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
filename =
MeshLib::IO::getVtuFileNameForPetscOutputWithoutExtension(
filename) +
"_" + std::to_string(rank) + ".vtu";
"_" + std::to_string(mpi.rank) + ".vtu";
}
#endif // OGS_USE_PETSC
filenames.push_back(filename);
Expand Down
15 changes: 2 additions & 13 deletions Applications/Utils/FileConverter/ConvertSHPToGLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@

#include <tclap/CmdLine.h>

#ifdef USE_PETSC
#include <mpi.h>
#endif

// STL
#include <fstream>
#include <vector>

// ShapeLib
#include <shapefil.h>

#include "BaseLib/MPI.h"
#include "GeoLib/GEOObjects.h"
#include "GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h"
#include "GeoLib/IO/XmlIO/Qt/XmlStnInterface.h"
Expand Down Expand Up @@ -185,9 +182,7 @@ int main(int argc, char* argv[])

cmd.parse(argc, argv);

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
BaseLib::MPI::Setup mpi_setup(argc, argv);

std::string fname(shapefile_arg.getValue());

Expand All @@ -207,9 +202,6 @@ int main(int argc, char* argv[])
ERR("Shape file contains {:d} polylines.", number_of_elements);
ERR("This programme only handles only files containing points.");
SHPClose(hSHP);
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_SUCCESS;
}
SHPClose(hSHP);
Expand Down Expand Up @@ -304,8 +296,5 @@ int main(int argc, char* argv[])
ERR("Could not open the database file.");
}

#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_SUCCESS;
}
16 changes: 2 additions & 14 deletions Applications/Utils/FileConverter/FEFLOW2OGS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@
// ThirdParty
#include <tclap/CmdLine.h>

#ifdef USE_PETSC
#include <mpi.h>
#endif

// BaseLib
#include "BaseLib/FileTools.h"
#include "BaseLib/MPI.h"
#include "BaseLib/RunTime.h"
#include "InfoLib/GitInfo.h"
#ifndef WIN32
Expand Down Expand Up @@ -62,9 +58,7 @@ int main(int argc, char* argv[])

cmd.parse(argc, argv);

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
BaseLib::MPI::Setup mpi_setup(argc, argv);

// *** read mesh
INFO("Reading {:s}.", feflow_mesh_arg.getValue());
Expand All @@ -81,9 +75,6 @@ int main(int argc, char* argv[])
if (mesh == nullptr)
{
INFO("Could not read mesh from {:s}.", feflow_mesh_arg.getValue());
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_FAILURE;
}
#ifndef WIN32
Expand All @@ -99,8 +90,5 @@ int main(int argc, char* argv[])
INFO("Writing {:s}.", ogs_mesh_fname);
MeshLib::IO::writeMeshToFile(*mesh, ogs_mesh_fname);
INFO("\tDone.");
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_SUCCESS;
}
16 changes: 2 additions & 14 deletions Applications/Utils/FileConverter/GMSH2OGS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
// ThirdParty
#include <tclap/CmdLine.h>

#ifdef USE_PETSC
#include <mpi.h>
#endif

// BaseLib
#include "BaseLib/FileTools.h"
#include "BaseLib/MPI.h"
#include "BaseLib/RunTime.h"
#include "InfoLib/GitInfo.h"
#ifndef WIN32
Expand Down Expand Up @@ -176,9 +172,7 @@ int main(int argc, char* argv[])

cmd.parse(argc, argv);

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
BaseLib::MPI::Setup mpi_setup(argc, argv);

// *** read mesh
INFO("Reading {:s}.", gmsh_mesh_arg.getValue());
Expand All @@ -194,9 +188,6 @@ int main(int argc, char* argv[])
if (mesh == nullptr)
{
INFO("Could not read mesh from {:s}.", gmsh_mesh_arg.getValue());
#ifdef USE_PETSC
MPI_Finalize();
#endif
return -1;
}
#ifndef WIN32
Expand Down Expand Up @@ -279,8 +270,5 @@ int main(int argc, char* argv[])
MeshLib::IO::writeMeshToFile(*mesh, ogs_mesh_arg.getValue());

delete mesh;
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_SUCCESS;
}
12 changes: 2 additions & 10 deletions Applications/Utils/FileConverter/GocadSGridReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
#include <spdlog/spdlog.h>
#include <tclap/CmdLine.h>

#ifdef USE_PETSC
#include <mpi.h>
#endif

#include <fstream>
#include <sstream>
#include <string>

#include "Applications/FileIO/GocadIO/GenerateFaceSetMeshes.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/MPI.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/Elements/Element.h"
#include "MeshLib/IO/writeMeshToFile.h"
Expand Down Expand Up @@ -61,9 +58,7 @@ int main(int argc, char* argv[])

cmd.parse(argc, argv);

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
BaseLib::MPI::Setup mpi_setup(argc, argv);

// read the Gocad SGrid
INFO("Start reading Gocad SGrid.");
Expand All @@ -82,8 +77,5 @@ int main(int argc, char* argv[])

INFO("Writing mesh to '{:s}'.", mesh_output_arg.getValue());
MeshLib::IO::writeMeshToFile(*mesh, mesh_output_arg.getValue());
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_SUCCESS;
}
18 changes: 2 additions & 16 deletions Applications/Utils/FileConverter/GocadTSurfaceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

#include <tclap/CmdLine.h>

#ifdef USE_PETSC
#include <mpi.h>
#endif

#include "Applications/FileIO/GocadIO/GocadAsciiReader.h"
#include "BaseLib/MPI.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/Mesh.h"
Expand Down Expand Up @@ -65,17 +62,12 @@ int main(int argc, char* argv[])

cmd.parse(argc, argv);

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
BaseLib::MPI::Setup mpi_setup(argc, argv);

if (export_lines_arg.isSet() && export_surfaces_arg.isSet())
{
ERR("Both the 'lines-only'-flag and 'surfaces-only'-flag are set. Only "
"one is allowed at a time.");
#ifdef USE_PETSC
MPI_Finalize();
#endif
return 2;
}

Expand All @@ -94,9 +86,6 @@ int main(int argc, char* argv[])
if (!FileIO::Gocad::GocadAsciiReader::readFile(file_name, meshes, t))
{
ERR("Error reading file.");
#ifdef USE_PETSC
MPI_Finalize();
#endif
return 1;
}
INFO("{:d} meshes found.", meshes.size());
Expand All @@ -115,8 +104,5 @@ int main(int argc, char* argv[])
MeshLib::IO::VtuInterface vtu(mesh.get(), data_mode, compressed);
vtu.writeToFile(dir + delim + mesh->getName() + ".vtu");
}
#ifdef USE_PETSC
MPI_Finalize();
#endif
return 0;
}
18 changes: 2 additions & 16 deletions Applications/Utils/FileConverter/Mesh2Raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

#include <tclap/CmdLine.h>

#ifdef USE_PETSC
#include <mpi.h>
#endif

#include <filesystem>
#include <fstream>
#include <memory>
#include <string>

#include "BaseLib/MPI.h"
#include "GeoLib/AABB.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
Expand Down Expand Up @@ -52,28 +49,20 @@ int main(int argc, char* argv[])
cmd.add(input_arg);
cmd.parse(argc, argv);

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
BaseLib::MPI::Setup mpi_setup(argc, argv);

INFO("Rasterising mesh...");
std::unique_ptr<MeshLib::Mesh> const mesh(
MeshLib::IO::readMeshFromFile(input_arg.getValue()));
if (mesh == nullptr)
{
ERR("Error reading mesh file.");
#ifdef USE_PETSC
MPI_Finalize();
#endif
return 1;
}
if (mesh->getDimension() != 2)
{
ERR("The programme requires a mesh containing two-dimensional elements "
"(i.e. triangles or quadrilaterals.");
#ifdef USE_PETSC
MPI_Finalize();
#endif
return 2;
}

Expand Down Expand Up @@ -178,8 +167,5 @@ int main(int argc, char* argv[])
}
out.close();
INFO("Result written to {:s}", output_name);
#ifdef USE_PETSC
MPI_Finalize();
#endif
return 0;
}
15 changes: 2 additions & 13 deletions Applications/Utils/FileConverter/Mesh2Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

#include <tclap/CmdLine.h>

#ifdef USE_PETSC
#include <mpi.h>
#endif

#include "Applications/FileIO/SHPInterface.h"
#include "BaseLib/MPI.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
Expand Down Expand Up @@ -43,22 +40,14 @@ int main(int argc, char* argv[])

cmd.parse(argc, argv);

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
BaseLib::MPI::Setup mpi_setup(argc, argv);

std::string const file_name(input_arg.getValue());
std::unique_ptr<MeshLib::Mesh> const mesh(
MeshLib::IO::readMeshFromFile(file_name));
if (FileIO::SHPInterface::write2dMeshToSHP(output_arg.getValue(), *mesh))
{
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_SUCCESS;
}
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_FAILURE;
}
Loading

0 comments on commit a624b9d

Please sign in to comment.