From 63f8617731c1e20cd56fc620136cc1698cbbf58c Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 18 Oct 2024 15:55:16 +0200 Subject: [PATCH 01/24] [BL/MPI] Reduce ifdef range. --- BaseLib/MPI.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 3b37773e3e9..9a3b8b48ce0 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -5,28 +5,28 @@ * Distributed under a Modified BSD License. * See accompanying file LICENSE.txt or * http://www.opengeosys.org/project/license - * */ #pragma once +#ifdef USE_PETSC +#include +#endif + namespace BaseLib::MPI { -#ifdef USE_PETSC -// Reduce operations for interprocess communications while using Petsc -static inline int reduceMin(int val) +static inline int reduceMin(int const val) { +#ifdef USE_PETSC + // Reduce operations for interprocess communications while using Petsc int result; MPI_Allreduce(&val, &result, 1, MPI_INTEGER, MPI_MIN, PETSC_COMM_WORLD); return result; -} #else -// Reduce operations for interprocess communications without using Petsc -static inline int reduceMin(int val) -{ + // Reduce operations for interprocess communications without using Petsc return val; -} #endif +} } // namespace BaseLib::MPI From 4aae83e36034b2b5a94ed989ba41440937430cdb Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Tue, 12 Nov 2024 17:33:55 +0100 Subject: [PATCH 02/24] [BL/MPI] Use MPI_COMM_WORLD in reduceMin --- BaseLib/MPI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 9a3b8b48ce0..d71d8abf0e3 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -21,7 +21,7 @@ static inline int reduceMin(int const val) #ifdef USE_PETSC // Reduce operations for interprocess communications while using Petsc int result; - MPI_Allreduce(&val, &result, 1, MPI_INTEGER, MPI_MIN, PETSC_COMM_WORLD); + MPI_Allreduce(&val, &result, 1, MPI_INTEGER, MPI_MIN, MPI_COMM_WORLD); return result; #else // Reduce operations for interprocess communications without using Petsc From 5deca8153505b1ccc074700d927aa7dac00c605a Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 18 Oct 2024 15:55:47 +0200 Subject: [PATCH 03/24] [BL/MPI] Struct for communicator, size, and rank --- BaseLib/MPI.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index d71d8abf0e3..bdab781de42 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -9,6 +9,8 @@ #pragma once +#include "Error.h" + #ifdef USE_PETSC #include #endif @@ -29,4 +31,25 @@ static inline int reduceMin(int const val) #endif } +#ifdef USE_PETSC +struct Mpi +{ + Mpi(MPI_Comm const communicator = MPI_COMM_WORLD) + : communicator(communicator) + { + int mpi_init; + MPI_Initialized(&mpi_init); + if (mpi_init != 1) + { + OGS_FATAL("MPI is not initialized."); + } + MPI_Comm_size(communicator, &size); + MPI_Comm_rank(communicator, &rank); + } + + MPI_Comm communicator; + int size; + int rank; +}; +#endif } // namespace BaseLib::MPI From d1f8238b94685ce1037984c35163d6ee0b5da9c6 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 31 Oct 2024 14:01:30 +0100 Subject: [PATCH 04/24] [MeL/IO] Use BaseLib::MPI::Mpi --- .../ApplicationsLib/TestDefinition.cpp | 13 +++---- .../IO/MPI_IO/NodePartitionedMeshReader.cpp | 39 +++++++++---------- MeshLib/IO/MPI_IO/NodePartitionedMeshReader.h | 19 ++++----- MeshLib/IO/VtkIO/VtuInterface.cpp | 30 +++++--------- MeshLib/IO/readMeshFromFile.cpp | 39 ++++++++----------- 5 files changed, 56 insertions(+), 84 deletions(-) diff --git a/Applications/ApplicationsLib/TestDefinition.cpp b/Applications/ApplicationsLib/TestDefinition.cpp index 6c936e4eff3..43856010bb4 100644 --- a/Applications/ApplicationsLib/TestDefinition.cpp +++ b/Applications/ApplicationsLib/TestDefinition.cpp @@ -20,9 +20,9 @@ #include "BaseLib/ConfigTree.h" #include "BaseLib/Error.h" #include "BaseLib/FileTools.h" -#ifdef USE_PETSC -#include +#include "BaseLib/MPI.h" +#ifdef USE_PETSC #include "MeshLib/IO/VtkIO/VtuInterface.h" // For petsc file name conversion. #endif @@ -197,16 +197,13 @@ TestDefinition::TestDefinition(BaseLib::ConfigTree const& config_tree, //! \ogs_file_param{prj__test_definition__vtkdiff__file} vtkdiff_config.getConfigParameter("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); diff --git a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp index ed7000c2286..7564c1773dd 100644 --- a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp +++ b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp @@ -45,12 +45,8 @@ namespace MeshLib { namespace IO { -NodePartitionedMeshReader::NodePartitionedMeshReader(MPI_Comm comm) - : _mpi_comm(comm) +NodePartitionedMeshReader::NodePartitionedMeshReader(MPI_Comm comm) : mpi_(comm) { - MPI_Comm_size(_mpi_comm, &_mpi_comm_size); - MPI_Comm_rank(_mpi_comm, &_mpi_rank); - registerNodeDataMpiType(); } @@ -79,13 +75,13 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::read( // Always try binary file first std::string const fname_new = file_name_base + "_partitioned_msh_cfg" + - std::to_string(_mpi_comm_size) + ".bin"; + std::to_string(mpi_.size) + ".bin"; if (!BaseLib::IsFileExisting(fname_new)) // binary file does not exist. { std::string const fname_ascii = file_name_base + "_partitioned_msh_cfg" + - std::to_string(_mpi_comm_size) + ".msh"; + std::to_string(mpi_.size) + ".msh"; if (BaseLib::IsFileExisting(fname_ascii)) { ERR("Reading of ASCII mesh file {:s} is not supported since OGS " @@ -102,7 +98,7 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::read( INFO("[time] Reading the mesh took {:f} s.", timer.elapsed()); - MPI_Barrier(_mpi_comm); + MPI_Barrier(mpi_.communicator); return mesh; } @@ -124,8 +120,9 @@ bool NodePartitionedMeshReader::readDataFromFile(std::string const& filename, MPI_File file; char* filename_char = const_cast(filename.data()); - int const file_status = MPI_File_open( - _mpi_comm, filename_char, MPI_MODE_RDONLY, MPI_INFO_NULL, &file); + int const file_status = + MPI_File_open(mpi_.communicator, filename_char, MPI_MODE_RDONLY, + MPI_INFO_NULL, &file); if (file_status != 0) { @@ -151,13 +148,13 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::readMesh( //---------------------------------------------------------------------------------- // Read headers const std::string fname_header = file_name_base + "_partitioned_msh_"; - const std::string fname_num_p_ext = std::to_string(_mpi_comm_size) + ".bin"; + const std::string fname_num_p_ext = std::to_string(mpi_.size) + ".bin"; // Read the config meta data from *cfg* file into struct PartitionedMeshInfo // _mesh_info if (!readDataFromFile( fname_header + "cfg" + fname_num_p_ext, - static_cast(static_cast(_mpi_rank) * + static_cast(static_cast(mpi_.rank) * sizeof(_mesh_info)), MPI_LONG, _mesh_info)) return nullptr; @@ -228,7 +225,7 @@ void NodePartitionedMeshReader::readProperties( const std::string fname_cfg = file_name_base + "_partitioned_" + item_type + "_properties_cfg" + - std::to_string(_mpi_comm_size) + ".bin"; + std::to_string(mpi_.size) + ".bin"; std::ifstream is(fname_cfg.c_str(), std::ios::binary | std::ios::in); if (!is) { @@ -262,7 +259,7 @@ void NodePartitionedMeshReader::readProperties( auto pos = is.tellg(); auto offset = static_cast(pos) + - static_cast(_mpi_rank * + static_cast(mpi_.rank * sizeof(MeshLib::IO::PropertyVectorPartitionMetaData)); is.seekg(offset); std::optional pvpmd( @@ -270,13 +267,13 @@ void NodePartitionedMeshReader::readProperties( bool pvpmd_read_ok = static_cast(pvpmd); bool all_pvpmd_read_ok; MPI_Allreduce(&pvpmd_read_ok, &all_pvpmd_read_ok, 1, MPI_C_BOOL, MPI_LOR, - _mpi_comm); + mpi_.communicator); if (!all_pvpmd_read_ok) { OGS_FATAL( "Error in NodePartitionedMeshReader::readProperties: " "Could not read the partition meta data for the mpi process {:d}", - _mpi_rank); + mpi_.rank); } DBUG("offset in the PropertyVector: {:d}", pvpmd->offset); DBUG("{:d} tuples in partition.", pvpmd->number_of_tuples); @@ -284,7 +281,7 @@ void NodePartitionedMeshReader::readProperties( const std::string fname_val = file_name_base + "_partitioned_" + item_type + "_properties_val" + - std::to_string(_mpi_comm_size) + ".bin"; + std::to_string(mpi_.size) + ".bin"; is.open(fname_val.c_str(), std::ios::binary | std::ios::in); if (!is) { @@ -407,7 +404,7 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( std::vector const& mesh_elems, MeshLib::Properties const& properties) const { - std::vector gathered_n_regular_base_nodes(_mpi_comm_size); + std::vector gathered_n_regular_base_nodes(mpi_.size); MPI_Allgather(&_mesh_info.number_of_regular_base_nodes, 1, @@ -415,7 +412,7 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( gathered_n_regular_base_nodes.data(), 1, MPI_UNSIGNED_LONG, - _mpi_comm); + mpi_.communicator); std::vector n_regular_base_nodes_at_rank; n_regular_base_nodes_at_rank.push_back(0); @@ -424,7 +421,7 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( back_inserter(n_regular_base_nodes_at_rank)); std::vector gathered_n_regular_high_order_nodes( - _mpi_comm_size); + mpi_.size); std::size_t const n_regular_high_order_nodes = _mesh_info.number_of_regular_nodes - _mesh_info.number_of_regular_base_nodes; @@ -434,7 +431,7 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( gathered_n_regular_high_order_nodes.data(), 1, MPI_UNSIGNED_LONG, - _mpi_comm); + mpi_.communicator); std::vector n_regular_high_order_nodes_at_rank; n_regular_high_order_nodes_at_rank.push_back(0); diff --git a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.h b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.h index 9874ed6bc3f..5d531b822f7 100644 --- a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.h +++ b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.h @@ -19,6 +19,7 @@ #include #include +#include "BaseLib/MPI.h" #include "MeshLib/IO/MPI_IO/PropertyVectorMetaData.h" #include "MeshLib/IO/NodeData.h" #include "MeshLib/NodePartitionedMesh.h" @@ -53,14 +54,8 @@ class NodePartitionedMeshReader final MeshLib::NodePartitionedMesh* read(const std::string& file_name_base); private: - /// Pointer to MPI communicator. - MPI_Comm _mpi_comm; - - /// Number of processes in the communicator: _mpi_comm. - int _mpi_comm_size; - - /// Rank of compute core. - int _mpi_rank; + /// Pointer to MPI communicator, the rank and the size. + BaseLib::MPI::Mpi mpi_; /// MPI data type for struct NodeData. MPI_Datatype _mpi_node_type; @@ -205,7 +200,7 @@ class NodePartitionedMeshReader final OGS_FATAL( "Error in NodePartitionedMeshReader::readProperties: " "Could not read part {:d} of the PropertyVector.", - _mpi_rank); + mpi_.rank); } /// Read data for property OGS_VERSION or IntegrationPointMetaData, and @@ -220,12 +215,12 @@ class NodePartitionedMeshReader final pvmd.property_name, t, pvmd.number_of_components); std::size_t const property_vector_size = - pvmd.number_of_tuples / _mpi_comm_size; + pvmd.number_of_tuples / mpi_.size; pv->resize(property_vector_size); // Locate the start position of the data in the file for the current // rank. - is.seekg(global_offset + property_vector_size * sizeof(T) * _mpi_rank); + is.seekg(global_offset + property_vector_size * sizeof(T) * mpi_.rank); // read the values if (!is.read(reinterpret_cast(pv->data()), @@ -234,7 +229,7 @@ class NodePartitionedMeshReader final OGS_FATAL( "Error in NodePartitionedMeshReader::readProperties: " "Could not read part {:d} of the PropertyVector.", - _mpi_rank); + mpi_.rank); } } diff --git a/MeshLib/IO/VtkIO/VtuInterface.cpp b/MeshLib/IO/VtkIO/VtuInterface.cpp index ee28445da54..5c43b0dd61f 100644 --- a/MeshLib/IO/VtkIO/VtuInterface.cpp +++ b/MeshLib/IO/VtkIO/VtuInterface.cpp @@ -26,13 +26,9 @@ #include #include "BaseLib/DisableFPE.h" -#include "BaseLib/Logging.h" - -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/FileTools.h" +#include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "MeshLib/Mesh.h" #include "MeshLib/Vtk/VtkMappedMeshSource.h" #include "VtkMeshConverter.h" @@ -148,23 +144,15 @@ std::string getVtuFileNameForPetscOutputWithoutExtension( bool VtuInterface::writeToFile(std::filesystem::path const& file_path) { #ifdef USE_PETSC - int mpi_init; - MPI_Initialized(&mpi_init); - if (mpi_init == 1) + BaseLib::MPI::Mpi mpi; + if (mpi.size == 1) { - int mpi_size; - MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); - if (mpi_size == 1) - { - return writeVTU(file_path.string()); - } - auto const vtu_file_name = - getVtuFileNameForPetscOutputWithoutExtension(file_path.string()); - int rank; - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - return writeVTU(vtu_file_name + ".pvtu", - mpi_size, rank); + return writeVTU(file_path.string()); } + auto const vtu_file_name = + getVtuFileNameForPetscOutputWithoutExtension(file_path.string()); + return writeVTU(vtu_file_name + ".pvtu", + mpi.size, mpi.rank); #endif return writeVTU(file_path.string()); } diff --git a/MeshLib/IO/readMeshFromFile.cpp b/MeshLib/IO/readMeshFromFile.cpp index 3545a04b858..515ac9738ba 100644 --- a/MeshLib/IO/readMeshFromFile.cpp +++ b/MeshLib/IO/readMeshFromFile.cpp @@ -21,6 +21,7 @@ #include "BaseLib/FileTools.h" #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "MeshLib/IO/Legacy/MeshIO.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" @@ -68,33 +69,27 @@ MeshLib::Mesh* readMeshFromFile(const std::string& file_name, bool const compute_element_neighbors) { #ifdef USE_PETSC - int mpi_init; - MPI_Initialized(&mpi_init); - if (mpi_init == 1) + BaseLib::MPI::Mpi mpi; + if (mpi.size > 1) { - int world_size; - MPI_Comm_size(MPI_COMM_WORLD, &world_size); - if (world_size > 1) + MeshLib::IO::NodePartitionedMeshReader read_pmesh(mpi.communicator); + const std::string file_name_base = + BaseLib::dropFileExtension(file_name); + return read_pmesh.read(file_name_base); + } + if (mpi.size == 1) + { + std::unique_ptr mesh{ + readMeshFromFileSerial(file_name, compute_element_neighbors)}; + + if (!mesh) { - MeshLib::IO::NodePartitionedMeshReader read_pmesh(MPI_COMM_WORLD); - const std::string file_name_base = - BaseLib::dropFileExtension(file_name); - return read_pmesh.read(file_name_base); + return nullptr; } - if (world_size == 1) - { - std::unique_ptr mesh{ - readMeshFromFileSerial(file_name, compute_element_neighbors)}; - if (!mesh) - { - return nullptr; - } - - return new MeshLib::NodePartitionedMesh(*mesh); - } - return nullptr; + return new MeshLib::NodePartitionedMesh(*mesh); } + return nullptr; #endif return readMeshFromFileSerial(file_name, compute_element_neighbors); } From f4beffdf304ce4e07ff5b976922b710d12a3080e Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 18 Oct 2024 16:37:30 +0200 Subject: [PATCH 05/24] [BL] allgather for single value; type conversion --- BaseLib/MPI.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index bdab781de42..de83e3fc4dd 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -51,5 +51,53 @@ struct Mpi int size; int rank; }; + +template +constexpr MPI_Datatype mpiType() +{ + using U = std::remove_const_t; + if constexpr (std::is_same_v) + { + return MPI_C_BOOL; + } + if constexpr (std::is_same_v) + { + return MPI_CHAR; + } + if constexpr (std::is_same_v) + { + return MPI_DOUBLE; + } + if constexpr (std::is_same_v) + { + return MPI_FLOAT; + } + if constexpr (std::is_same_v) + { + return MPI_INT; + } + if constexpr (std::is_same_v) + { + return MPI_UNSIGNED_LONG; + } + if constexpr (std::is_same_v) + { + return MPI_UNSIGNED; + } +} + +template +static std::vector allgather(T const& value, Mpi const& mpi) +{ + std::vector result(mpi.size); + + result[mpi.rank] = value; + + MPI_Allgather(&result[mpi.rank], 1, mpiType(), result.data(), 1, + mpiType(), mpi.communicator); + + return result; +} + #endif } // namespace BaseLib::MPI From 1aff347718a9e6053fd15f9271f71c2ec1e25596 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Wed, 30 Oct 2024 11:58:02 +0100 Subject: [PATCH 06/24] Use BaseLib::MPI::allgather --- MathLib/LinAlg/PETSc/PETScVector.cpp | 14 ++-- .../IO/MPI_IO/NodePartitionedMeshReader.cpp | 23 ++----- MeshLib/IO/XDMF/mpi/partition.cpp | 21 ++---- .../transformMeshToNodePartitionedMesh.cpp | 69 +++++++------------ 4 files changed, 38 insertions(+), 89 deletions(-) diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp index bf12c6d3878..018d5c3ea21 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.cpp +++ b/MathLib/LinAlg/PETSc/PETScVector.cpp @@ -24,6 +24,7 @@ #include #include "BaseLib/Error.h" +#include "BaseLib/MPI.h" namespace MathLib { @@ -116,21 +117,16 @@ void PETScVector::finalizeAssembly() void PETScVector::gatherLocalVectors(PetscScalar local_array[], PetscScalar global_array[]) const { - // Collect vectors from processors. - int size_rank; - MPI_Comm_size(PETSC_COMM_WORLD, &size_rank); + BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD}; // number of elements to be sent for each rank - std::vector i_cnt(size_rank); - - MPI_Allgather(&size_loc_, 1, MPI_INT, &i_cnt[0], 1, MPI_INT, - PETSC_COMM_WORLD); + std::vector const i_cnt = BaseLib::MPI::allgather(size_loc_, mpi); // collect local array PetscInt offset = 0; // offset in the receive vector of the data from each rank - std::vector i_disp(size_rank); - for (PetscInt i = 0; i < size_rank; i++) + std::vector i_disp(mpi.size); + for (PetscInt i = 0; i < mpi.size; i++) { i_disp[i] = offset; offset += i_cnt[i]; diff --git a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp index 7564c1773dd..97bf7bd8b3f 100644 --- a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp +++ b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp @@ -22,6 +22,7 @@ #include "BaseLib/FileTools.h" #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "MeshLib/Elements/Elements.h" #include "MeshLib/MeshEnums.h" @@ -404,15 +405,8 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( std::vector const& mesh_elems, MeshLib::Properties const& properties) const { - std::vector gathered_n_regular_base_nodes(mpi_.size); - - MPI_Allgather(&_mesh_info.number_of_regular_base_nodes, - 1, - MPI_UNSIGNED_LONG, - gathered_n_regular_base_nodes.data(), - 1, - MPI_UNSIGNED_LONG, - mpi_.communicator); + std::vector const gathered_n_regular_base_nodes = + BaseLib::MPI::allgather(_mesh_info.number_of_regular_base_nodes, mpi_); std::vector n_regular_base_nodes_at_rank; n_regular_base_nodes_at_rank.push_back(0); @@ -420,18 +414,11 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( end(gathered_n_regular_base_nodes), back_inserter(n_regular_base_nodes_at_rank)); - std::vector gathered_n_regular_high_order_nodes( - mpi_.size); std::size_t const n_regular_high_order_nodes = _mesh_info.number_of_regular_nodes - _mesh_info.number_of_regular_base_nodes; - MPI_Allgather(&n_regular_high_order_nodes, - 1, - MPI_UNSIGNED_LONG, - gathered_n_regular_high_order_nodes.data(), - 1, - MPI_UNSIGNED_LONG, - mpi_.communicator); + std::vector const gathered_n_regular_high_order_nodes = + BaseLib::MPI::allgather(n_regular_high_order_nodes, mpi_); std::vector n_regular_high_order_nodes_at_rank; n_regular_high_order_nodes_at_rank.push_back(0); diff --git a/MeshLib/IO/XDMF/mpi/partition.cpp b/MeshLib/IO/XDMF/mpi/partition.cpp index c1855dc2ac0..f6e31333849 100644 --- a/MeshLib/IO/XDMF/mpi/partition.cpp +++ b/MeshLib/IO/XDMF/mpi/partition.cpp @@ -18,6 +18,7 @@ #include #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "MeshLib/IO/XDMF/fileIO.h" #include "getCommunicator.h" @@ -33,22 +34,10 @@ bool isFileManager() PartitionInfo getPartitionInfo(std::size_t const size, unsigned int const n_files) { - MPI_Comm const mpi_comm = getCommunicator(n_files).mpi_communicator; - int mpi_size; - int mpi_rank; - MPI_Comm_size(mpi_comm, &mpi_size); - MPI_Comm_rank(mpi_comm, &mpi_rank); - - std::vector partition_sizes; - partition_sizes.resize(mpi_size); + BaseLib::MPI::Mpi const mpi{getCommunicator(n_files).mpi_communicator}; - MPI_Allgather(&size, - 1, - MPI_UNSIGNED_LONG, - partition_sizes.data(), - 1, - MPI_UNSIGNED_LONG, - mpi_comm); + std::vector const partition_sizes = + BaseLib::MPI::allgather(size, mpi); // the first partition's offset is zero, offsets for subsequent // partitions are the accumulated sum of all preceding size (excluding @@ -63,7 +52,7 @@ PartitionInfo getPartitionInfo(std::size_t const size, *max_element(partition_sizes.begin(), partition_sizes.end()); // local_offset, local_length, longest_local_length, global_length - return {partition_offsets[mpi_rank], size, longest_partition, + return {partition_offsets[mpi.rank], size, longest_partition, partition_offsets.back()}; } } // namespace MeshLib::IO diff --git a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp index 0c627e33cb4..aab43d6d209 100644 --- a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp +++ b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp @@ -22,6 +22,7 @@ #include #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/Mesh.h" #include "MeshLib/Node.h" @@ -42,15 +43,6 @@ bool isRegularNode( namespace MeshLib { -std::pair getMPIRankAndSize(MPI_Comm const& mpi_comm) -{ - int mpi_comm_size; - MPI_Comm_size(mpi_comm, &mpi_comm_size); - int mpi_comm_rank; - MPI_Comm_rank(mpi_comm, &mpi_comm_rank); - return {mpi_comm_rank, mpi_comm_size}; -} - std::pair, std::vector> copyNodesAndElements( std::vector const& input_elements) { @@ -136,15 +128,12 @@ computeRegularBaseNodeGlobalNodeIDsOfSubDomainPartition( std::size_t const number_of_regular_nodes = computeNumberOfRegularNodes(bulk_mesh, subdomain_mesh); - // in the following information exchange with other ranks is required - MPI_Comm mpi_comm = MPI_COMM_WORLD; - auto const [mpi_comm_rank, mpi_comm_size] = getMPIRankAndSize(mpi_comm); + BaseLib::MPI::Mpi mpi; // send own number of regular nodes to all others - std::vector gathered_number_of_regular_nodes(mpi_comm_size); - MPI_Allgather(&number_of_regular_nodes, 1, MPI_UNSIGNED_LONG, - gathered_number_of_regular_nodes.data(), 1, MPI_UNSIGNED_LONG, - mpi_comm); + std::vector const gathered_number_of_regular_nodes = + BaseLib::MPI::allgather(number_of_regular_nodes, mpi); + // compute the 'offset' in the global_node_ids std::vector numbers_of_regular_nodes_at_rank; numbers_of_regular_nodes_at_rank.push_back(0); @@ -155,8 +144,7 @@ computeRegularBaseNodeGlobalNodeIDsOfSubDomainPartition( // add the offset to the partitioned-owned subdomain std::vector subdomain_global_node_ids; subdomain_global_node_ids.reserve(subdomain_nodes.size()); - auto const partition_offset = - numbers_of_regular_nodes_at_rank[mpi_comm_rank]; + auto const partition_offset = numbers_of_regular_nodes_at_rank[mpi.rank]; DBUG("[{}] partition offset: {}", subdomain_mesh->getName(), partition_offset); // set the global id for the regular base nodes @@ -175,9 +163,7 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( Mesh const* subdomain_mesh, std::vector const& global_regular_base_node_ids) { - // in the following information exchange with other ranks is required - MPI_Comm const mpi_comm = MPI_COMM_WORLD; - auto const [mpi_comm_rank, mpi_comm_size] = getMPIRankAndSize(mpi_comm); + BaseLib::MPI::Mpi mpi; // count regular nodes that is the offset in the mapping auto const local_bulk_node_ids_for_subdomain = @@ -203,15 +189,15 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( auto const size = subdomain_node_id_to_bulk_node_id.size(); std::size_t global_number_of_subdomain_node_id_to_bulk_node_id = 0; MPI_Allreduce(&size, &global_number_of_subdomain_node_id_to_bulk_node_id, 1, - MPI_UNSIGNED_LONG, MPI_SUM, mpi_comm); + MPI_UNSIGNED_LONG, MPI_SUM, mpi.communicator); DBUG("[{}] global_number_of_subdomain_node_id_to_bulk_node_id: '{}' ", subdomain_mesh->getName(), global_number_of_subdomain_node_id_to_bulk_node_id); - std::vector numbers_of_ids_at_ranks(mpi_comm_size); - MPI_Allgather(&size, 1, MPI_INT, numbers_of_ids_at_ranks.data(), 1, MPI_INT, - mpi_comm); + std::vector const numbers_of_ids_at_ranks = + BaseLib::MPI::allgather(static_cast(size), mpi); + std::vector offsets; offsets.push_back(0); std::partial_sum(begin(numbers_of_ids_at_ranks), @@ -225,7 +211,7 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( numbers_of_ids_at_ranks.data(), /* recvcounts */ offsets.data(), /* displs */ MPI_UNSIGNED_LONG, /* recvtype */ - mpi_comm); + mpi.communicator); // construct a map for fast search of local bulk node ids std::map global_to_local_bulk_node_ids; @@ -248,9 +234,9 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( global_number_of_subdomain_node_id_to_bulk_node_id, std::numeric_limits::max()); // search in all ranks within the bulk ids for the corresponding id - for (int rank = 0; rank < mpi_comm_size; ++rank) + for (int rank = 0; rank < mpi.size; ++rank) { - if (rank == mpi_comm_rank) + if (rank == mpi.rank) { continue; } @@ -285,13 +271,13 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( global_number_of_subdomain_node_id_to_bulk_node_id, /* sendcount */ MPI_UNSIGNED_LONG, /* sendtype */ MPI_MAX, /* operation */ - mpi_comm); + mpi.communicator); std::vector global_ids_for_subdomain_ghost_nodes( computed_global_ids_for_subdomain_ghost_nodes.begin() + - offsets[mpi_comm_rank], + offsets[mpi.rank], computed_global_ids_for_subdomain_ghost_nodes.begin() + - offsets[mpi_comm_rank + 1]); + offsets[mpi.rank + 1]); return global_ids_for_subdomain_ghost_nodes; } @@ -301,14 +287,10 @@ std::vector computeNumberOfRegularBaseNodesAtRank( auto const number_of_regular_base_nodes = subdomain_mesh->computeNumberOfBaseNodes(); - MPI_Comm const mpi_comm = MPI_COMM_WORLD; - auto const [mpi_comm_rank, mpi_comm_size] = getMPIRankAndSize(mpi_comm); + BaseLib::MPI::Mpi mpi; - std::vector gathered_number_of_regular_base_nodes( - mpi_comm_size); - MPI_Allgather(&number_of_regular_base_nodes, 1, MPI_UNSIGNED_LONG, - gathered_number_of_regular_base_nodes.data(), 1, - MPI_UNSIGNED_LONG, mpi_comm); + std::vector const gathered_number_of_regular_base_nodes = + BaseLib::MPI::allgather(number_of_regular_base_nodes, mpi); std::vector numbers_of_regular_base_nodes_at_rank; numbers_of_regular_base_nodes_at_rank.push_back(0); @@ -323,20 +305,15 @@ std::vector computeNumberOfRegularBaseNodesAtRank( std::vector computeNumberOfRegularHigherOrderNodesAtRank( Mesh const* subdomain_mesh) { - // in the following information exchange with other ranks is required - MPI_Comm const mpi_comm = MPI_COMM_WORLD; - auto [mpi_comm_rank, mpi_comm_size] = getMPIRankAndSize(mpi_comm); + BaseLib::MPI::Mpi mpi; auto const number_of_regular_base_nodes = subdomain_mesh->computeNumberOfBaseNodes(); - std::vector gathered_number_of_regular_higher_order_nodes( - mpi_comm_size); auto const number_of_regular_higher_order_nodes = subdomain_mesh->getNumberOfNodes() - number_of_regular_base_nodes; - MPI_Allgather(&number_of_regular_higher_order_nodes, 1, MPI_UNSIGNED_LONG, - gathered_number_of_regular_higher_order_nodes.data(), 1, - MPI_UNSIGNED_LONG, mpi_comm); + std::vector gathered_number_of_regular_higher_order_nodes = + BaseLib::MPI::allgather(number_of_regular_higher_order_nodes, mpi); std::vector numbers_of_regular_higher_order_nodes_at_rank; numbers_of_regular_higher_order_nodes_at_rank.push_back(0); From ebd8a07a9f57cf7ed51e2693c1bb1bc01bde5766 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Wed, 30 Oct 2024 11:58:02 +0100 Subject: [PATCH 07/24] Use BaseLib::MPI::allgather --- MathLib/LinAlg/PETSc/PETScVector.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp index 018d5c3ea21..b6a781a3fb1 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.cpp +++ b/MathLib/LinAlg/PETSc/PETScVector.cpp @@ -125,8 +125,8 @@ void PETScVector::gatherLocalVectors(PetscScalar local_array[], // collect local array PetscInt offset = 0; // offset in the receive vector of the data from each rank - std::vector i_disp(mpi.size); - for (PetscInt i = 0; i < mpi.size; i++) + std::vector i_disp(mpi.rank); + for (PetscInt i = 0; i < mpi.rank; i++) { i_disp[i] = offset; offset += i_cnt[i]; From 4209d7ec804554ba72f2563e207b77eda513e920 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 18 Oct 2024 19:26:13 +0200 Subject: [PATCH 08/24] [BL] sizesToOffsets() w/ last entry sum(sizes) --- BaseLib/Algorithm.h | 16 ++++++++++++++++ BaseLib/CMakeLists.txt | 1 + 2 files changed, 17 insertions(+) diff --git a/BaseLib/Algorithm.h b/BaseLib/Algorithm.h index 371f14622cd..28ee2516578 100644 --- a/BaseLib/Algorithm.h +++ b/BaseLib/Algorithm.h @@ -16,6 +16,10 @@ #include #include #include +#include +#include +#include +#include #include #include #include @@ -272,6 +276,18 @@ void cleanupVectorElements(std::vector& dependent_items, Args&&... args) cleanupVectorElements(std::forward(args)...); } +/// Converts range of sizes to a vector of offsets. First offset is 0 and the +/// resulting vector size is the size of the input range plus one. +template + requires std::is_integral_v> +std::vector> sizesToOffsets(R const& sizes) +{ + return ranges::views::concat( + ranges::views::single(ranges::range_value_t{0}), + ranges::views::partial_sum(sizes)) | + ranges::to>>(); +} + /// Checks if any of the elements in the given list is true. template constexpr bool any_of(List const& values) diff --git a/BaseLib/CMakeLists.txt b/BaseLib/CMakeLists.txt index 3af91ae9e08..34dea6cfcee 100644 --- a/BaseLib/CMakeLists.txt +++ b/BaseLib/CMakeLists.txt @@ -12,6 +12,7 @@ target_link_libraries( BaseLib PUBLIC Boost::algorithm Boost::property_tree + range-v3 spdlog tclap $<$:WinMM> # needed for timeGetTime From b6cc676cc616661762fcd0d7867933a8b796e279 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Wed, 30 Oct 2024 18:02:11 +0100 Subject: [PATCH 09/24] Use BaseLib::sizesToOffsets replacing partial sums --- .../Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp | 5 ++- MathLib/LinAlg/PETSc/PETScVector.cpp | 10 ++---- .../IO/MPI_IO/NodePartitionedMeshReader.cpp | 14 +++----- MeshLib/IO/XDMF/mpi/partition.cpp | 10 +++--- .../transformMeshToNodePartitionedMesh.cpp | 32 +++++-------------- NumLib/DOF/LocalToGlobalIndexMap.cpp | 17 ++-------- 6 files changed, 22 insertions(+), 66 deletions(-) diff --git a/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp b/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp index 3e166e0734b..d5438197f93 100644 --- a/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp +++ b/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp @@ -229,9 +229,8 @@ getMergedNodesVector(std::vector> const& meshes) ranges::transform(meshes, std::back_inserter(number_of_nodes_per_partition), [](auto const& mesh) { return mesh->getNumberOfNodes(); }); - std::vector offsets(meshes.size() + 1, 0); - std::partial_sum(number_of_nodes_per_partition.begin(), - number_of_nodes_per_partition.end(), offsets.begin() + 1); + std::vector const offsets = + BaseLib::sizesToOffsets(number_of_nodes_per_partition); std::vector all_nodes; all_nodes.reserve(offsets.back()); diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp index b6a781a3fb1..13ca4b1ee4c 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.cpp +++ b/MathLib/LinAlg/PETSc/PETScVector.cpp @@ -23,6 +23,7 @@ #include #include +#include "BaseLib/Algorithm.h" #include "BaseLib/Error.h" #include "BaseLib/MPI.h" @@ -122,15 +123,8 @@ void PETScVector::gatherLocalVectors(PetscScalar local_array[], // number of elements to be sent for each rank std::vector const i_cnt = BaseLib::MPI::allgather(size_loc_, mpi); - // collect local array - PetscInt offset = 0; // offset in the receive vector of the data from each rank - std::vector i_disp(mpi.rank); - for (PetscInt i = 0; i < mpi.rank; i++) - { - i_disp[i] = offset; - offset += i_cnt[i]; - } + std::vector const i_disp = BaseLib::sizesToOffsets(i_cnt); MPI_Allgatherv(local_array, size_loc_, MPI_DOUBLE, global_array, &i_cnt[0], &i_disp[0], MPI_DOUBLE, PETSC_COMM_WORLD); diff --git a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp index 97bf7bd8b3f..f0bb10ddf41 100644 --- a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp +++ b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp @@ -408,11 +408,8 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( std::vector const gathered_n_regular_base_nodes = BaseLib::MPI::allgather(_mesh_info.number_of_regular_base_nodes, mpi_); - std::vector n_regular_base_nodes_at_rank; - n_regular_base_nodes_at_rank.push_back(0); - std::partial_sum(begin(gathered_n_regular_base_nodes), - end(gathered_n_regular_base_nodes), - back_inserter(n_regular_base_nodes_at_rank)); + std::vector n_regular_base_nodes_at_rank = + BaseLib::sizesToOffsets(gathered_n_regular_base_nodes); std::size_t const n_regular_high_order_nodes = _mesh_info.number_of_regular_nodes - @@ -420,11 +417,8 @@ MeshLib::NodePartitionedMesh* NodePartitionedMeshReader::newMesh( std::vector const gathered_n_regular_high_order_nodes = BaseLib::MPI::allgather(n_regular_high_order_nodes, mpi_); - std::vector n_regular_high_order_nodes_at_rank; - n_regular_high_order_nodes_at_rank.push_back(0); - std::partial_sum(begin(gathered_n_regular_high_order_nodes), - end(gathered_n_regular_high_order_nodes), - back_inserter(n_regular_high_order_nodes_at_rank)); + std::vector n_regular_high_order_nodes_at_rank = + BaseLib::sizesToOffsets(gathered_n_regular_high_order_nodes); return new MeshLib::NodePartitionedMesh( mesh_name, mesh_nodes, glb_node_ids, mesh_elems, properties, diff --git a/MeshLib/IO/XDMF/mpi/partition.cpp b/MeshLib/IO/XDMF/mpi/partition.cpp index f6e31333849..19255358885 100644 --- a/MeshLib/IO/XDMF/mpi/partition.cpp +++ b/MeshLib/IO/XDMF/mpi/partition.cpp @@ -17,6 +17,7 @@ #include +#include "BaseLib/Algorithm.h" #include "BaseLib/Logging.h" #include "BaseLib/MPI.h" #include "MeshLib/IO/XDMF/fileIO.h" @@ -40,12 +41,9 @@ PartitionInfo getPartitionInfo(std::size_t const size, BaseLib::MPI::allgather(size, mpi); // the first partition's offset is zero, offsets for subsequent - // partitions are the accumulated sum of all preceding size (excluding - // own size) - std::vector partition_offsets(1, 0); - std::partial_sum(partition_sizes.begin(), - partition_sizes.end(), - back_inserter(partition_offsets)); + // partitions are the accumulated sum of all preceding size. + std::vector const partition_offsets = + BaseLib::sizesToOffsets(partition_sizes); // chunked std::size_t longest_partition = diff --git a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp index aab43d6d209..3e7aa4fb87e 100644 --- a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp +++ b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp @@ -135,11 +135,8 @@ computeRegularBaseNodeGlobalNodeIDsOfSubDomainPartition( BaseLib::MPI::allgather(number_of_regular_nodes, mpi); // compute the 'offset' in the global_node_ids - std::vector numbers_of_regular_nodes_at_rank; - numbers_of_regular_nodes_at_rank.push_back(0); - std::partial_sum(begin(gathered_number_of_regular_nodes), - end(gathered_number_of_regular_nodes), - back_inserter(numbers_of_regular_nodes_at_rank)); + std::vector const numbers_of_regular_nodes_at_rank = + BaseLib::sizesToOffsets(gathered_number_of_regular_nodes); // add the offset to the partitioned-owned subdomain std::vector subdomain_global_node_ids; @@ -198,10 +195,9 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( std::vector const numbers_of_ids_at_ranks = BaseLib::MPI::allgather(static_cast(size), mpi); - std::vector offsets; - offsets.push_back(0); - std::partial_sum(begin(numbers_of_ids_at_ranks), - end(numbers_of_ids_at_ranks), back_inserter(offsets)); + std::vector const offsets = + BaseLib::sizesToOffsets(numbers_of_ids_at_ranks); + std::vector ghost_node_ids_of_all_ranks( global_number_of_subdomain_node_id_to_bulk_node_id); MPI_Allgatherv(subdomain_node_id_to_bulk_node_id.data(), /* sendbuf */ @@ -292,13 +288,7 @@ std::vector computeNumberOfRegularBaseNodesAtRank( std::vector const gathered_number_of_regular_base_nodes = BaseLib::MPI::allgather(number_of_regular_base_nodes, mpi); - std::vector numbers_of_regular_base_nodes_at_rank; - numbers_of_regular_base_nodes_at_rank.push_back(0); - std::partial_sum(begin(gathered_number_of_regular_base_nodes), - end(gathered_number_of_regular_base_nodes), - back_inserter(numbers_of_regular_base_nodes_at_rank)); - - return numbers_of_regular_base_nodes_at_rank; + return BaseLib::sizesToOffsets(gathered_number_of_regular_base_nodes); } // similar to the above only with regular higher order nodes @@ -315,14 +305,8 @@ std::vector computeNumberOfRegularHigherOrderNodesAtRank( std::vector gathered_number_of_regular_higher_order_nodes = BaseLib::MPI::allgather(number_of_regular_higher_order_nodes, mpi); - std::vector numbers_of_regular_higher_order_nodes_at_rank; - numbers_of_regular_higher_order_nodes_at_rank.push_back(0); - std::partial_sum( - begin(gathered_number_of_regular_higher_order_nodes), - end(gathered_number_of_regular_higher_order_nodes), - back_inserter(numbers_of_regular_higher_order_nodes_at_rank)); - - return numbers_of_regular_higher_order_nodes_at_rank; + return BaseLib::sizesToOffsets( + gathered_number_of_regular_higher_order_nodes); } std::vector computeGlobalNodeIDsOfSubDomainPartition( diff --git a/NumLib/DOF/LocalToGlobalIndexMap.cpp b/NumLib/DOF/LocalToGlobalIndexMap.cpp index 1d37e1a3856..4ac7cea9902 100644 --- a/NumLib/DOF/LocalToGlobalIndexMap.cpp +++ b/NumLib/DOF/LocalToGlobalIndexMap.cpp @@ -16,19 +16,6 @@ namespace NumLib { -namespace -{ -// Make the cumulative sum of an array, which starts with zero -template -std::vector to_cumulative(std::vector const& vec) -{ - std::vector result(vec.size() + 1, 0); - std::partial_sum(vec.begin(), vec.end(), result.begin() + 1); - - return result; -} - -} // namespace int LocalToGlobalIndexMap::getGlobalComponent(int const variable_id, int const component_id) const @@ -129,7 +116,7 @@ LocalToGlobalIndexMap::LocalToGlobalIndexMap( NumLib::ComponentOrder const order) : _mesh_subsets(std::move(mesh_subsets)), _mesh_component_map(_mesh_subsets, order), - _variable_component_offsets(to_cumulative(vec_var_n_components)) + _variable_component_offsets(BaseLib::sizesToOffsets(vec_var_n_components)) { // For each element of that MeshSubset save a line of global indices. for (int variable_id = 0; @@ -160,7 +147,7 @@ LocalToGlobalIndexMap::LocalToGlobalIndexMap( NumLib::ComponentOrder const order) : _mesh_subsets(std::move(mesh_subsets)), _mesh_component_map(_mesh_subsets, order), - _variable_component_offsets(to_cumulative(vec_var_n_components)) + _variable_component_offsets(BaseLib::sizesToOffsets(vec_var_n_components)) { assert(vec_var_n_components.size() == vec_var_elements.size()); From af88c06887e59f6f54671d86e2aaab8c4c98992a Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 31 Oct 2024 11:05:21 +0100 Subject: [PATCH 10/24] [BL/MPI] allreduce for single values --- BaseLib/MPI.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index de83e3fc4dd..9318cf354e2 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -99,5 +99,13 @@ static std::vector allgather(T const& value, Mpi const& mpi) return result; } +template +static T allreduce(T const& value, MPI_Op const& mpi_op, Mpi const& mpi) +{ + T result{}; + + MPI_Allreduce(&value, &result, 1, mpiType(), mpi_op, mpi.communicator); + return result; +} #endif } // namespace BaseLib::MPI From c2cead6f203f5705e60c7ecf626e2da44cfef64d Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 31 Oct 2024 13:54:34 +0100 Subject: [PATCH 11/24] Use BaseLib::MPI::allreduce --- BaseLib/MPI.h | 25 +++++---- MeshGeoToolsLib/BoundaryElementsAtPoint.cpp | 11 ++-- .../IO/MPI_IO/NodePartitionedMeshReader.cpp | 6 +-- .../transformMeshToNodePartitionedMesh.cpp | 14 ++--- NumLib/DOF/DOFTableUtil.cpp | 27 ++++------ ProcessLib/PhaseField/PhaseFieldProcess.cpp | 22 ++++---- Tests/MathLib/TestGlobalMatrixInterface.cpp | 53 ++++++++----------- 7 files changed, 62 insertions(+), 96 deletions(-) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 9318cf354e2..1d58b374b14 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -18,19 +18,6 @@ namespace BaseLib::MPI { -static inline int reduceMin(int const val) -{ -#ifdef USE_PETSC - // Reduce operations for interprocess communications while using Petsc - int result; - MPI_Allreduce(&val, &result, 1, MPI_INTEGER, MPI_MIN, MPI_COMM_WORLD); - return result; -#else - // Reduce operations for interprocess communications without using Petsc - return val; -#endif -} - #ifdef USE_PETSC struct Mpi { @@ -108,4 +95,16 @@ static T allreduce(T const& value, MPI_Op const& mpi_op, Mpi const& mpi) return result; } #endif + +/// The reduction is implemented transparently for with and without MPI. In the +/// latter case the input value is returned. +static inline int reduceMin(int const val) +{ +#ifdef USE_PETSC + return allreduce(val, MPI_MIN, Mpi{MPI_COMM_WORLD}); +#else + return val; +#endif +} + } // namespace BaseLib::MPI diff --git a/MeshGeoToolsLib/BoundaryElementsAtPoint.cpp b/MeshGeoToolsLib/BoundaryElementsAtPoint.cpp index 3979edd184f..56e83a22c75 100644 --- a/MeshGeoToolsLib/BoundaryElementsAtPoint.cpp +++ b/MeshGeoToolsLib/BoundaryElementsAtPoint.cpp @@ -9,10 +9,7 @@ #include "BoundaryElementsAtPoint.h" -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "GeoLib/Point.h" #include "MathLib/Point3d.h" #include "MeshGeoToolsLib/MeshNodeSearcher.h" @@ -32,10 +29,8 @@ BoundaryElementsAtPoint::BoundaryElementsAtPoint( #ifdef USE_PETSC std::size_t const number_of_found_nodes_at_rank = node_ids.size(); - std::size_t number_of_total_found_nodes = 0; - - MPI_Allreduce(&number_of_found_nodes_at_rank, &number_of_total_found_nodes, - 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD); + std::size_t const number_of_total_found_nodes = BaseLib::MPI::allreduce( + number_of_found_nodes_at_rank, MPI_SUM, BaseLib::MPI::Mpi{}); if (number_of_total_found_nodes == 0) { diff --git a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp index f0bb10ddf41..faf10327ae8 100644 --- a/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp +++ b/MeshLib/IO/MPI_IO/NodePartitionedMeshReader.cpp @@ -265,10 +265,8 @@ void NodePartitionedMeshReader::readProperties( is.seekg(offset); std::optional pvpmd( MeshLib::IO::readPropertyVectorPartitionMetaData(is)); - bool pvpmd_read_ok = static_cast(pvpmd); - bool all_pvpmd_read_ok; - MPI_Allreduce(&pvpmd_read_ok, &all_pvpmd_read_ok, 1, MPI_C_BOOL, MPI_LOR, - mpi_.communicator); + bool const all_pvpmd_read_ok = + BaseLib::MPI::allreduce(static_cast(pvpmd), MPI_LOR, mpi_); if (!all_pvpmd_read_ok) { OGS_FATAL( diff --git a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp index 3e7aa4fb87e..9eeee236a6b 100644 --- a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp +++ b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp @@ -184,9 +184,8 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( // other ranks and at the same time receive from all other ranks // first send the sizes to all other to are able to allocate buffer auto const size = subdomain_node_id_to_bulk_node_id.size(); - std::size_t global_number_of_subdomain_node_id_to_bulk_node_id = 0; - MPI_Allreduce(&size, &global_number_of_subdomain_node_id_to_bulk_node_id, 1, - MPI_UNSIGNED_LONG, MPI_SUM, mpi.communicator); + std::size_t const global_number_of_subdomain_node_id_to_bulk_node_id = + BaseLib::MPI::allreduce(size, MPI_SUM, mpi); DBUG("[{}] global_number_of_subdomain_node_id_to_bulk_node_id: '{}' ", subdomain_mesh->getName(), @@ -343,13 +342,8 @@ std::vector computeGlobalNodeIDsOfSubDomainPartition( unsigned long getNumberOfGlobalNodes(Mesh const* subdomain_mesh) { // sum all nodes over all partitions in number_of_global_nodes - unsigned long number_of_local_nodes = subdomain_mesh->getNodes().size(); - unsigned long number_of_global_nodes = 0; - - MPI_Comm mpi_comm = MPI_COMM_WORLD; - - MPI_Allreduce(&number_of_local_nodes, &number_of_global_nodes, 1, - MPI_UNSIGNED_LONG, MPI_SUM, mpi_comm); + unsigned long const number_of_global_nodes = BaseLib::MPI::allreduce( + subdomain_mesh->getNodes().size(), MPI_SUM, BaseLib::MPI::Mpi{}); DBUG("[{}] number_of_global_nodes: {}'", subdomain_mesh->getName(), number_of_global_nodes); return number_of_global_nodes; diff --git a/NumLib/DOF/DOFTableUtil.cpp b/NumLib/DOF/DOFTableUtil.cpp index 93266fc6586..4241919a6ec 100644 --- a/NumLib/DOF/DOFTableUtil.cpp +++ b/NumLib/DOF/DOFTableUtil.cpp @@ -14,6 +14,7 @@ #include #include +#include "BaseLib/MPI.h" #include "MathLib/LinAlg/Eigen/EigenMapTools.h" namespace NumLib { @@ -44,15 +45,12 @@ double norm(GlobalVector const& x, unsigned const global_component, double norm1(GlobalVector const& x, unsigned const global_component, LocalToGlobalIndexMap const& dof_table, MeshLib::Mesh const& mesh) { - double res = + double const res = norm(x, global_component, dof_table, mesh, [](double res, double value) { return res + std::abs(value); }); #ifdef USE_PETSC - double global_result = 0.0; - MPI_Allreduce(&res, &global_result, 1, MPI_DOUBLE, MPI_SUM, - PETSC_COMM_WORLD); - res = global_result; + return BaseLib::MPI::allreduce(res, MPI_SUM, BaseLib::MPI::Mpi{}); #endif return res; } @@ -60,15 +58,13 @@ double norm1(GlobalVector const& x, unsigned const global_component, double norm2(GlobalVector const& x, unsigned const global_component, LocalToGlobalIndexMap const& dof_table, MeshLib::Mesh const& mesh) { - double res = + double const res = norm(x, global_component, dof_table, mesh, [](double res, double value) { return res + value * value; }); #ifdef USE_PETSC - double global_result = 0.0; - MPI_Allreduce(&res, &global_result, 1, MPI_DOUBLE, MPI_SUM, - PETSC_COMM_WORLD); - res = global_result; + return std::sqrt( + BaseLib::MPI::allreduce(res, MPI_SUM, BaseLib::MPI::Mpi{})); #endif return std::sqrt(res); } @@ -77,15 +73,12 @@ double normInfinity(GlobalVector const& x, unsigned const global_component, LocalToGlobalIndexMap const& dof_table, MeshLib::Mesh const& mesh) { - double res = norm(x, global_component, dof_table, mesh, - [](double res, double value) - { return std::max(res, std::abs(value)); }); + double const res = + norm(x, global_component, dof_table, mesh, [](double res, double value) + { return std::max(res, std::abs(value)); }); #ifdef USE_PETSC - double global_result = 0.0; - MPI_Allreduce(&res, &global_result, 1, MPI_DOUBLE, MPI_MAX, - PETSC_COMM_WORLD); - res = global_result; + return BaseLib::MPI::allreduce(res, MPI_MAX, BaseLib::MPI::Mpi{}); #endif return res; } diff --git a/ProcessLib/PhaseField/PhaseFieldProcess.cpp b/ProcessLib/PhaseField/PhaseFieldProcess.cpp index 3020a935702..8c150546174 100644 --- a/ProcessLib/PhaseField/PhaseFieldProcess.cpp +++ b/ProcessLib/PhaseField/PhaseFieldProcess.cpp @@ -12,6 +12,7 @@ #include +#include "BaseLib/MPI.h" #include "MeshLib/Utils/getOrCreateMeshProperty.h" #include "NumLib/DOF/ComputeSparsityPattern.h" #include "PhaseFieldFEM.h" @@ -307,15 +308,13 @@ void PhaseFieldProcess::postTimestepConcreteProcess( _process_data.pressure_work); #ifdef USE_PETSC - double const elastic_energy = _process_data.elastic_energy; - MPI_Allreduce(&elastic_energy, &_process_data.elastic_energy, 1, - MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD); - double const surface_energy = _process_data.surface_energy; - MPI_Allreduce(&surface_energy, &_process_data.surface_energy, 1, - MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD); - double const pressure_work = _process_data.pressure_work; - MPI_Allreduce(&pressure_work, &_process_data.pressure_work, 1, - MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD); + BaseLib::MPI::Mpi mpi{}; + _process_data.elastic_energy = + BaseLib::MPI::allreduce(_process_data.elastic_energy, MPI_SUM, mpi); + _process_data.surface_energy = + BaseLib::MPI::allreduce(_process_data.surface_energy, MPI_SUM, mpi); + _process_data.pressure_work = + BaseLib::MPI::allreduce(_process_data.pressure_work, MPI_SUM, mpi); #endif INFO( @@ -361,9 +360,8 @@ void PhaseFieldProcess::postNonLinearSolverConcreteProcess( getActiveElementIDs(), dof_tables, x, t, _process_data.crack_volume); #ifdef USE_PETSC - double const crack_volume = _process_data.crack_volume; - MPI_Allreduce(&crack_volume, &_process_data.crack_volume, 1, MPI_DOUBLE, - MPI_SUM, PETSC_COMM_WORLD); + _process_data.crack_volume = BaseLib::MPI::allreduce( + _process_data.crack_volume, MPI_SUM, BaseLib::MPI::Mpi{}); #endif INFO("Integral of crack: {:g}", _process_data.crack_volume); diff --git a/Tests/MathLib/TestGlobalMatrixInterface.cpp b/Tests/MathLib/TestGlobalMatrixInterface.cpp index 075084ac1ef..1e2337c1b1a 100644 --- a/Tests/MathLib/TestGlobalMatrixInterface.cpp +++ b/Tests/MathLib/TestGlobalMatrixInterface.cpp @@ -17,6 +17,7 @@ #include +#include "BaseLib/MPI.h" #include "MathLib/LinAlg/LinAlg.h" #if defined(USE_PETSC) @@ -58,24 +59,17 @@ void checkGlobalMatrixInterface(T_MATRIX& m) template void checkGlobalMatrixInterfaceMPI(T_MATRIX& m, T_VECTOR& v) { - int msize; - MPI_Comm_size(PETSC_COMM_WORLD, &msize); - int mrank; - MPI_Comm_rank(PETSC_COMM_WORLD, &mrank); + BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD}; - ASSERT_EQ(3u, msize); + ASSERT_EQ(3u, mpi.size); ASSERT_EQ(m.getRangeEnd() - m.getRangeBegin(), m.getNumberOfLocalRows()); - int gathered_rows; - int local_rows = m.getNumberOfLocalRows(); - MPI_Allreduce(&local_rows, &gathered_rows, 1, MPI_INT, MPI_SUM, - PETSC_COMM_WORLD); + int const gathered_rows = + BaseLib::MPI::allreduce(m.getNumberOfLocalRows(), MPI_SUM, mpi); ASSERT_EQ(m.getNumberOfRows(), gathered_rows); - int gathered_cols; - int local_cols = m.getNumberOfLocalColumns(); - MPI_Allreduce(&local_cols, &gathered_cols, 1, MPI_INT, MPI_SUM, - PETSC_COMM_WORLD); + int const gathered_cols = + BaseLib::MPI::allreduce(m.getNumberOfLocalColumns(), MPI_SUM, mpi); ASSERT_EQ(m.getNumberOfColumns(), gathered_cols); // Add entries @@ -87,8 +81,8 @@ void checkGlobalMatrixInterfaceMPI(T_MATRIX& m, T_VECTOR& v) std::vector row_pos(2); std::vector col_pos(2); - row_pos[0] = 2 * mrank; - row_pos[1] = 2 * mrank + 1; + row_pos[0] = 2 * mpi.rank; + row_pos[1] = 2 * mpi.rank + 1; col_pos[0] = row_pos[0]; col_pos[1] = row_pos[1]; @@ -112,10 +106,10 @@ void checkGlobalMatrixInterfaceMPI(T_MATRIX& m, T_VECTOR& v) ASSERT_EQ(sqrt(3 * (3 * 3 + 7 * 7)), norm2(y)); // set a value - m_c.set(2 * mrank, 2 * mrank, 5.0); + m_c.set(2 * mpi.rank, 2 * mpi.rank, 5.0); MathLib::finalizeMatrixAssembly(m_c); // add a value - m_c.add(2 * mrank + 1, 2 * mrank + 1, 5.0); + m_c.add(2 * mpi.rank + 1, 2 * mpi.rank + 1, 5.0); MathLib::finalizeMatrixAssembly(m_c); matMult(m_c, v, y); @@ -127,21 +121,16 @@ void checkGlobalMatrixInterfaceMPI(T_MATRIX& m, T_VECTOR& v) template void checkGlobalRectangularMatrixInterfaceMPI(T_MATRIX& m, T_VECTOR& v) { - int mrank; - MPI_Comm_rank(PETSC_COMM_WORLD, &mrank); + BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD}; ASSERT_EQ(m.getRangeEnd() - m.getRangeBegin(), m.getNumberOfLocalRows()); - int gathered_rows; - int local_rows = m.getNumberOfLocalRows(); - MPI_Allreduce(&local_rows, &gathered_rows, 1, MPI_INT, MPI_SUM, - PETSC_COMM_WORLD); + int const gathered_rows = + BaseLib::MPI::allreduce(m.getNumberOfLocalRows(), MPI_SUM, mpi); ASSERT_EQ(m.getNumberOfRows(), gathered_rows); - int gathered_cols; - int local_cols = m.getNumberOfLocalColumns(); - MPI_Allreduce(&local_cols, &gathered_cols, 1, MPI_INT, MPI_SUM, - PETSC_COMM_WORLD); + int const gathered_cols = + BaseLib::MPI::allreduce(m.getNumberOfLocalColumns(), MPI_SUM, mpi); ASSERT_EQ(m.getNumberOfColumns(), gathered_cols); // Add entries @@ -155,11 +144,11 @@ void checkGlobalRectangularMatrixInterfaceMPI(T_MATRIX& m, T_VECTOR& v) std::vector row_pos(2); std::vector col_pos(3); - row_pos[0] = 2 * mrank; - row_pos[1] = 2 * mrank + 1; - col_pos[0] = 3 * mrank; - col_pos[1] = 3 * mrank + 1; - col_pos[2] = 3 * mrank + 2; + row_pos[0] = 2 * mpi.rank; + row_pos[1] = 2 * mpi.rank + 1; + col_pos[0] = 3 * mpi.rank; + col_pos[1] = 3 * mpi.rank + 1; + col_pos[2] = 3 * mpi.rank + 2; m.add(row_pos, col_pos, loc_m); From 2981f4fdb5223bf99cda7c26ed3643c2bafa60df Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 18 Oct 2024 19:23:28 +0200 Subject: [PATCH 12/24] [BL/MPI] allgather version for vectors --- BaseLib/MPI.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 1d58b374b14..8601dc4976a 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -9,6 +9,8 @@ #pragma once +#include + #include "Error.h" #ifdef USE_PETSC @@ -86,6 +88,21 @@ static std::vector allgather(T const& value, Mpi const& mpi) return result; } +template +static std::vector allgather(std::vector const& vector, Mpi const& mpi) +{ + std::size_t const size = vector.size(); + // Flat in memory over all ranks; + std::vector result(mpi.size * size); + + std::copy_n(vector.begin(), size, &result[mpi.rank * size]); + + MPI_Allgather(&result[mpi.rank * size], size, mpiType(), result.data(), + size, mpiType(), mpi.communicator); + + return result; +} + template static T allreduce(T const& value, MPI_Op const& mpi_op, Mpi const& mpi) { From 8a69e0a281782f705daaa4e27dab575d52dc1087 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sun, 20 Oct 2024 09:41:49 +0200 Subject: [PATCH 13/24] [BL/MPI] allreduceInplace --- BaseLib/MPI.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 8601dc4976a..8939563ae2c 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -111,6 +111,19 @@ static T allreduce(T const& value, MPI_Op const& mpi_op, Mpi const& mpi) MPI_Allreduce(&value, &result, 1, mpiType(), mpi_op, mpi.communicator); return result; } + +template +static void allreduceInplace(std::vector& vector, + MPI_Op const& mpi_op, + Mpi const& mpi) +{ + MPI_Allreduce(MPI_IN_PLACE, + vector.data(), + vector.size(), + mpiType(), + mpi_op, + mpi.communicator); +} #endif /// The reduction is implemented transparently for with and without MPI. In the From 709b8af66236d0f9918936ea3399e61555ff158f Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 31 Oct 2024 15:42:07 +0100 Subject: [PATCH 14/24] [BL/MPI] allreduce version for vectors --- BaseLib/MPI.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 8939563ae2c..891b81f98b3 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -112,6 +112,18 @@ static T allreduce(T const& value, MPI_Op const& mpi_op, Mpi const& mpi) return result; } +template +static std::vector allreduce(std::vector const& vector, + MPI_Op const& mpi_op, Mpi const& mpi) +{ + std::size_t const size = vector.size(); + std::vector result(vector.size()); + + MPI_Allreduce(vector.data(), result.data(), size, mpiType(), mpi_op, + mpi.communicator); + return result; +} + template static void allreduceInplace(std::vector& vector, MPI_Op const& mpi_op, From d9362bf1b24a926731e632aa5fb806bff0ac89b7 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 31 Oct 2024 15:49:11 +0100 Subject: [PATCH 15/24] [MeL] Use vector allreduce --- .../Utils/transformMeshToNodePartitionedMesh.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp index 9eeee236a6b..c69891c5cad 100644 --- a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp +++ b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp @@ -257,16 +257,9 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( } // send the computed ids back - std::vector computed_global_ids_for_subdomain_ghost_nodes( - global_number_of_subdomain_node_id_to_bulk_node_id); - MPI_Allreduce( - local_subdomain_node_ids_of_all_ranks.data(), /* sendbuf */ - computed_global_ids_for_subdomain_ghost_nodes - .data(), /* recvbuf (out) */ - global_number_of_subdomain_node_id_to_bulk_node_id, /* sendcount */ - MPI_UNSIGNED_LONG, /* sendtype */ - MPI_MAX, /* operation */ - mpi.communicator); + std::vector const + computed_global_ids_for_subdomain_ghost_nodes = BaseLib::MPI::allreduce( + local_subdomain_node_ids_of_all_ranks, MPI_MAX, mpi); std::vector global_ids_for_subdomain_ghost_nodes( computed_global_ids_for_subdomain_ghost_nodes.begin() + From 4ee86862ebf0bbd6456814652d07b71584ca0002 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 31 Oct 2024 16:24:20 +0100 Subject: [PATCH 16/24] [MeL/U] Use inplace mpi reduction saving memory Rename local variable --- .../transformMeshToNodePartitionedMesh.cpp | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp index c69891c5cad..477566dd5b6 100644 --- a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp +++ b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp @@ -225,7 +225,7 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( local_subdomain_node_ids[local_bulk_node_ids_for_subdomain[id]] = id; } - std::vector local_subdomain_node_ids_of_all_ranks( + std::vector subdomain_node_ids_of_all_ranks( global_number_of_subdomain_node_id_to_bulk_node_id, std::numeric_limits::max()); // search in all ranks within the bulk ids for the corresponding id @@ -244,28 +244,24 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( continue; } auto const local_bulk_node_id = it->second; - local_subdomain_node_ids_of_all_ranks[i] = - global_regular_base_node_ids - [local_subdomain_node_ids.find(local_bulk_node_id)->second]; + subdomain_node_ids_of_all_ranks[i] = global_regular_base_node_ids + [local_subdomain_node_ids.find(local_bulk_node_id)->second]; DBUG( "[{}] found global subdomain node id: '{}' for global bulk " "node id {} ", subdomain_mesh->getName(), - local_subdomain_node_ids_of_all_ranks[i], + subdomain_node_ids_of_all_ranks[i], ghost_node_ids_of_all_ranks[i]); } } - // send the computed ids back - std::vector const - computed_global_ids_for_subdomain_ghost_nodes = BaseLib::MPI::allreduce( - local_subdomain_node_ids_of_all_ranks, MPI_MAX, mpi); + // find maximum over all ranks + BaseLib::MPI::allreduceInplace(subdomain_node_ids_of_all_ranks, MPI_MAX, + mpi); std::vector global_ids_for_subdomain_ghost_nodes( - computed_global_ids_for_subdomain_ghost_nodes.begin() + - offsets[mpi.rank], - computed_global_ids_for_subdomain_ghost_nodes.begin() + - offsets[mpi.rank + 1]); + subdomain_node_ids_of_all_ranks.begin() + offsets[mpi.rank], + subdomain_node_ids_of_all_ranks.begin() + offsets[mpi.rank + 1]); return global_ids_for_subdomain_ghost_nodes; } From 8411edf44f43d5549ab66e180f359860ae4cd031 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 1 Nov 2024 11:15:44 +0100 Subject: [PATCH 17/24] [BL/MPI] allgatherv for variable size data exchg --- BaseLib/MPI.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 891b81f98b3..3d60e44a61b 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -11,6 +11,7 @@ #include +#include "Algorithm.h" #include "Error.h" #ifdef USE_PETSC @@ -136,6 +137,33 @@ static void allreduceInplace(std::vector& vector, mpi_op, mpi.communicator); } + +/// Allgather for variable data. Returns offsets in the receive buffer. +/// The receive buffer is resized to accommodate the gathered data. +template +static std::vector allgatherv( + std::span const send_buffer, + std::vector>& receive_buffer, + Mpi const& mpi) +{ + // Determine the number of elements to send + int const size = static_cast(send_buffer.size()); + + // Gather sizes from all ranks + std::vector const sizes = allgather(size, mpi); + + // Compute offsets based on counts + std::vector const offsets = BaseLib::sizesToOffsets(sizes); + + // Resize receive buffer to hold all gathered data + receive_buffer.resize(offsets.back()); + + MPI_Allgatherv(send_buffer.data(), size, mpiType(), + receive_buffer.data(), sizes.data(), offsets.data(), + mpiType(), mpi.communicator); + + return offsets; +} #endif /// The reduction is implemented transparently for with and without MPI. In the From 25bff5949b34430aa173f3819529e2924ec76a20 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 1 Nov 2024 11:19:15 +0100 Subject: [PATCH 18/24] [MeL] Use BaseLib::MPI::allgatherv --- .../transformMeshToNodePartitionedMesh.cpp | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp index 477566dd5b6..4a8fed12f10 100644 --- a/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp +++ b/MeshLib/Utils/transformMeshToNodePartitionedMesh.cpp @@ -180,34 +180,19 @@ std::vector computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition( } } - // send ids of bulk ghost nodes belonging to the subdomain mesh to all - // other ranks and at the same time receive from all other ranks - // first send the sizes to all other to are able to allocate buffer - auto const size = subdomain_node_id_to_bulk_node_id.size(); - std::size_t const global_number_of_subdomain_node_id_to_bulk_node_id = - BaseLib::MPI::allreduce(size, MPI_SUM, mpi); + // Send ids of bulk ghost nodes belonging to the subdomain mesh to all + // other ranks and at the same time receive from all other ranks. + std::vector ghost_node_ids_of_all_ranks; + std::vector const offsets = + BaseLib::MPI::allgatherv(std::span{subdomain_node_id_to_bulk_node_id}, + ghost_node_ids_of_all_ranks, mpi); + std::size_t const global_number_of_subdomain_node_id_to_bulk_node_id = + offsets.back(); DBUG("[{}] global_number_of_subdomain_node_id_to_bulk_node_id: '{}' ", subdomain_mesh->getName(), global_number_of_subdomain_node_id_to_bulk_node_id); - std::vector const numbers_of_ids_at_ranks = - BaseLib::MPI::allgather(static_cast(size), mpi); - - std::vector const offsets = - BaseLib::sizesToOffsets(numbers_of_ids_at_ranks); - - std::vector ghost_node_ids_of_all_ranks( - global_number_of_subdomain_node_id_to_bulk_node_id); - MPI_Allgatherv(subdomain_node_id_to_bulk_node_id.data(), /* sendbuf */ - size, /* sendcount */ - MPI_UNSIGNED_LONG, /* sendtype */ - ghost_node_ids_of_all_ranks.data(), /* recvbuf (out) */ - numbers_of_ids_at_ranks.data(), /* recvcounts */ - offsets.data(), /* displs */ - MPI_UNSIGNED_LONG, /* recvtype */ - mpi.communicator); - // construct a map for fast search of local bulk node ids std::map global_to_local_bulk_node_ids; for (auto const id : bulk_mesh->getNodes() | MeshLib::views::ids) From 6cea389e1f920e0f2d0b7c1cca1621628ff6bfb4 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 1 Nov 2024 15:46:50 +0100 Subject: [PATCH 19/24] [MaL/PETSc] allgatherv replaces gatherLocalVectors --- MathLib/LinAlg/PETSc/PETScVector.cpp | 18 ++---------------- MathLib/LinAlg/PETSc/PETScVector.h | 8 -------- 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp index 13ca4b1ee4c..a6201f116d8 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.cpp +++ b/MathLib/LinAlg/PETSc/PETScVector.cpp @@ -115,21 +115,6 @@ void PETScVector::finalizeAssembly() VecAssemblyEnd(v_); } -void PETScVector::gatherLocalVectors(PetscScalar local_array[], - PetscScalar global_array[]) const -{ - BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD}; - - // number of elements to be sent for each rank - std::vector const i_cnt = BaseLib::MPI::allgather(size_loc_, mpi); - - // offset in the receive vector of the data from each rank - std::vector const i_disp = BaseLib::sizesToOffsets(i_cnt); - - MPI_Allgatherv(local_array, size_loc_, MPI_DOUBLE, global_array, &i_cnt[0], - &i_disp[0], MPI_DOUBLE, PETSC_COMM_WORLD); -} - void PETScVector::getGlobalVector(std::vector& u) const { #ifdef TEST_MEM_PETSC @@ -149,7 +134,8 @@ void PETScVector::getGlobalVector(std::vector& u) const PetscScalar* xp = nullptr; VecGetArray(v_, &xp); - gatherLocalVectors(xp, u.data()); + BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD}; + BaseLib::MPI::allgatherv(std::span(xp, size_loc_), u, mpi); // This following line may be needed late on // for a communication load balance: diff --git a/MathLib/LinAlg/PETSc/PETScVector.h b/MathLib/LinAlg/PETSc/PETScVector.h index ed2734878fb..20983dacf2d 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.h +++ b/MathLib/LinAlg/PETSc/PETScVector.h @@ -267,14 +267,6 @@ class PETScVector /// Map global indices of ghost entries to local indices mutable std::map global_ids2local_ids_ghost_; - /*! - \brief Collect local vectors - \param local_array Local array - \param global_array Global array - */ - void gatherLocalVectors(PetscScalar local_array[], - PetscScalar global_array[]) const; - /*! Get local vector, i.e. entries in the same rank */ From 61ce09c69d9a8db84c7a0dc61f28a19a9b32bbac Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sun, 3 Nov 2024 13:11:44 +0100 Subject: [PATCH 20/24] [T/BL] MPI tests for allgather, allreduce, etc. --- Tests/BaseLib/MPI.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Tests/BaseLib/MPI.cpp diff --git a/Tests/BaseLib/MPI.cpp b/Tests/BaseLib/MPI.cpp new file mode 100644 index 00000000000..16aadb59b89 --- /dev/null +++ b/Tests/BaseLib/MPI.cpp @@ -0,0 +1,113 @@ + +#include "BaseLib/MPI.h" + +#include + +using namespace BaseLib::MPI; + +#ifdef USE_PETSC +// Fixture for MPI tests +struct MPI_BaseLib : public ::testing::Test +{ + Mpi mpi{}; +}; + +TEST_F(MPI_BaseLib, CommunicatorRankAndSize) +{ + EXPECT_EQ(mpi.size, 3); + EXPECT_GE(mpi.rank, 0); + EXPECT_LT(mpi.rank, 3); +} + +TEST_F(MPI_BaseLib, Allgather) +{ + std::vector const gathered_values = allgather(mpi.rank, mpi); + + std::vector const expected_values = {0, 1, 2}; + // Each rank's value should match its rank + EXPECT_EQ(expected_values, gathered_values); +} + +TEST_F(MPI_BaseLib, AllgatherVector) +{ + std::vector const send_values(2, mpi.rank); + std::vector const gathered_values = allgather(send_values, mpi); + + std::vector const expected_values = {0, 0, 1, 1, 2, 2}; + EXPECT_EQ(expected_values, gathered_values); +} + +TEST_F(MPI_BaseLib, Allgatherv) +{ + int const rank_value = + mpi.rank + 1; // Each rank contributes rank+1 elements + std::vector const send_data( + rank_value, mpi.rank); // Each element is set to the rank number + + std::vector gathered_values(1 + 2 + 3); + + std::vector const offsets = + allgatherv(std::span(send_data), gathered_values, mpi); + std::vector const expected_offsets = {0, 1, 3, 6}; // last element is + // size. + std::vector const expected_gathered_values = {0, 1, 1, 2, 2, 2}; + EXPECT_EQ(expected_offsets, offsets); + EXPECT_EQ(expected_gathered_values, gathered_values); +} + +TEST_F(MPI_BaseLib, Allreduce) +{ + int const sum = allreduce(mpi.rank + 1, MPI_SUM, mpi); + int const expected_sum = 1 + 2 + 3; + EXPECT_EQ(expected_sum, sum); + + int const product = allreduce(mpi.rank + 1, MPI_PROD, mpi); + int const expected_product = 1 * 2 * 3; + EXPECT_EQ(expected_product, product); + + int const min = allreduce(mpi.rank + 1, MPI_MIN, mpi); + int const expected_min = 1; + EXPECT_EQ(expected_min, min); +} + +TEST_F(MPI_BaseLib, AllreduceVector) +{ + std::vector const send_values = {mpi.rank + 1, + mpi.size + mpi.rank + 1}; + std::vector const sum = allreduce(send_values, MPI_SUM, mpi); + std::vector const expected_sum = {(1 + 2 + 3), (4 + 5 + 6)}; + EXPECT_EQ(expected_sum, sum); + + std::vector const product = allreduce(send_values, MPI_PROD, mpi); + std::vector const expected_product = {(1 * 2 * 3), (4 * 5 * 6)}; + EXPECT_EQ(expected_product, product); + + std::vector const min = allreduce(send_values, MPI_MIN, mpi); + std::vector const expected_min = {1, 4}; + EXPECT_EQ(expected_min, min); +} + +TEST_F(MPI_BaseLib, AllreduceVectorInplace) +{ + { + std::vector values = {mpi.rank + 1, mpi.size + mpi.rank + 1}; + allreduceInplace(values, MPI_SUM, mpi); + std::vector const expected_sum = {(1 + 2 + 3), (4 + 5 + 6)}; + EXPECT_EQ(expected_sum, values); + } + + { + std::vector values = {mpi.rank + 1, mpi.size + mpi.rank + 1}; + allreduceInplace(values, MPI_PROD, mpi); + std::vector const expected_product = {(1 * 2 * 3), (4 * 5 * 6)}; + EXPECT_EQ(expected_product, values); + } + + { + std::vector values = {mpi.rank + 1, mpi.size + mpi.rank + 1}; + allreduceInplace(values, MPI_MIN, mpi); + std::vector const expected_min = {1, 4}; + EXPECT_EQ(expected_min, values); + } +} +#endif From 2ae44c26085283862ce86c7cc2b79f12e7a4ac06 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sun, 3 Nov 2024 21:27:59 +0100 Subject: [PATCH 21/24] [T] Shorten MPITest prefix to MPI --- Tests/CMakeLists.txt | 4 ++-- Tests/MathLib/TestGlobalMatrixInterface.cpp | 8 ++++---- Tests/MathLib/TestGlobalVectorInterface.cpp | 6 +++--- Tests/MathLib/TestLinearSolver.cpp | 6 +++--- Tests/NumLib/TestComponentNorms.cpp | 8 ++++---- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 1a450db81a8..b92d80da952 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -190,10 +190,10 @@ if(OGS_USE_PETSC) if("${HOSTNAME}" MATCHES "frontend.*") list(APPEND MPIRUN_ARGS --mca btl_openib_allow_ib 1) endif() - set(TEST_FILTER_MPI --gtest_filter=-MPITest*) + set(TEST_FILTER_MPI --gtest_filter=-MPI*) add_custom_target(tests mpirun ${MPIRUN_ARGS} -np 1 $ ${TESTRUNNER_ADDITIONAL_ARGUMENTS} ${TEST_FILTER_MPI} - COMMAND mpirun ${MPIRUN_ARGS} -np 3 $ --gtest_filter=MPITest* + COMMAND mpirun ${MPIRUN_ARGS} -np 3 $ --gtest_filter=MPI* DEPENDS testrunner tests-cleanup ) else() diff --git a/Tests/MathLib/TestGlobalMatrixInterface.cpp b/Tests/MathLib/TestGlobalMatrixInterface.cpp index 1e2337c1b1a..46f3f29cebf 100644 --- a/Tests/MathLib/TestGlobalMatrixInterface.cpp +++ b/Tests/MathLib/TestGlobalMatrixInterface.cpp @@ -167,7 +167,7 @@ void checkGlobalRectangularMatrixInterfaceMPI(T_MATRIX& m, T_VECTOR& v) } // end namespace #if defined(USE_PETSC) -TEST(MPITest_Math, CheckInterface_PETScMatrix_Local_Size) +TEST(MPI_Math, CheckInterface_PETScMatrix_Local_Size) { MathLib::PETScMatrixOption opt; opt.d_nz = 2; @@ -182,7 +182,7 @@ TEST(MPITest_Math, CheckInterface_PETScMatrix_Local_Size) checkGlobalMatrixInterfaceMPI(A, x); } -TEST(MPITest_Math, CheckInterface_PETScMatrix_Global_Size) +TEST(MPI_Math, CheckInterface_PETScMatrix_Global_Size) { MathLib::PETScMatrixOption opt; opt.d_nz = 2; @@ -194,7 +194,7 @@ TEST(MPITest_Math, CheckInterface_PETScMatrix_Global_Size) checkGlobalMatrixInterfaceMPI(A, x); } -TEST(MPITest_Math, CheckInterface_PETSc_Rectangular_Matrix_Local_Size) +TEST(MPI_Math, CheckInterface_PETSc_Rectangular_Matrix_Local_Size) { MathLib::PETScMatrixOption opt; opt.d_nz = 3; @@ -209,7 +209,7 @@ TEST(MPITest_Math, CheckInterface_PETSc_Rectangular_Matrix_Local_Size) checkGlobalRectangularMatrixInterfaceMPI(A, x); } -TEST(MPITest_Math, CheckInterface_PETSc_Rectangular_Matrix_Global_Size) +TEST(MPI_Math, CheckInterface_PETSc_Rectangular_Matrix_Global_Size) { MathLib::PETScMatrixOption opt; opt.d_nz = 3; diff --git a/Tests/MathLib/TestGlobalVectorInterface.cpp b/Tests/MathLib/TestGlobalVectorInterface.cpp index dca634e5a22..4430b29ec10 100644 --- a/Tests/MathLib/TestGlobalVectorInterface.cpp +++ b/Tests/MathLib/TestGlobalVectorInterface.cpp @@ -260,7 +260,7 @@ void checkPETScVectorExplictGhostID() //-------------------------------------------- #if defined(USE_PETSC) -TEST(MPITest_Math, PETScVectorPatitionedAutomatically) +TEST(MPI_Math, PETScVectorPatitionedAutomatically) { int msize; MPI_Comm_size(PETSC_COMM_WORLD, &msize); @@ -272,7 +272,7 @@ TEST(MPITest_Math, PETScVectorPatitionedAutomatically) checkPETScVectorNoExplictGhostID(x, msize); } -TEST(MPITest_Math, PETScVectorFixedPartition) +TEST(MPI_Math, PETScVectorFixedPartition) { int msize; MPI_Comm_size(PETSC_COMM_WORLD, &msize); @@ -291,7 +291,7 @@ TEST(MPITest_Math, PETScVectorFixedPartition) checkPETScVectorNoExplictGhostID(x_fixed_p, msize); } -TEST(MPITest_Math, CheckPETScVectorExplictGhostID) +TEST(MPI_Math, CheckPETScVectorExplictGhostID) { checkPETScVectorExplictGhostID(); } diff --git a/Tests/MathLib/TestLinearSolver.cpp b/Tests/MathLib/TestLinearSolver.cpp index c3140099cc8..3aada8e8b0d 100644 --- a/Tests/MathLib/TestLinearSolver.cpp +++ b/Tests/MathLib/TestLinearSolver.cpp @@ -347,7 +347,7 @@ TEST(Math, CheckInterface_EigenLis) #endif #ifdef USE_PETSC -TEST(MPITest_Math, CheckInterface_PETSc_Linear_Solver_basic) +TEST(MPI_Math, CheckInterface_PETSc_Linear_Solver_basic) { MathLib::PETScMatrixOption opt; opt.d_nz = 2; @@ -377,7 +377,7 @@ TEST(MPITest_Math, CheckInterface_PETSc_Linear_Solver_basic) A, b, "" /*prefix, not specified*/, getConfigTree(xml)); } -TEST(MPITest_Math, CheckInterface_PETSc_Linear_Solver_chebyshev_sor) +TEST(MPI_Math, CheckInterface_PETSc_Linear_Solver_chebyshev_sor) { MathLib::PETScMatrixOption opt; opt.d_nz = 2; @@ -407,7 +407,7 @@ TEST(MPITest_Math, CheckInterface_PETSc_Linear_Solver_chebyshev_sor) A, b, "" /*prefix, not specified*/, getConfigTree(xml)); } -TEST(MPITest_Math, CheckInterface_PETSc_Linear_Solver_gmres_amg) +TEST(MPI_Math, CheckInterface_PETSc_Linear_Solver_gmres_amg) { MathLib::PETScMatrixOption opt; opt.d_nz = 2; diff --git a/Tests/NumLib/TestComponentNorms.cpp b/Tests/NumLib/TestComponentNorms.cpp index 22a43bed9be..4373acaca70 100644 --- a/Tests/NumLib/TestComponentNorms.cpp +++ b/Tests/NumLib/TestComponentNorms.cpp @@ -124,7 +124,7 @@ void do_test(unsigned const num_components, #ifndef USE_PETSC TEST(NumLib, ComponentNormSingleComponent) #else -TEST(MPITest_NumLib, ComponentNormSingleComponent) +TEST(MPI_NumLib, ComponentNormSingleComponent) #endif { unsigned const num_components = 1; @@ -145,7 +145,7 @@ TEST(MPITest_NumLib, ComponentNormSingleComponent) #ifndef USE_PETSC TEST(NumLib, ComponentNormMultiComponent1) #else -TEST(MPITest_NumLib, ComponentNormMultiComponent1) +TEST(MPI_NumLib, ComponentNormMultiComponent1) #endif { unsigned const num_components = 3; @@ -162,7 +162,7 @@ TEST(MPITest_NumLib, ComponentNormMultiComponent1) #ifndef USE_PETSC TEST(NumLib, ComponentNormMultiComponent2) #else -TEST(MPITest_NumLib, ComponentNormMultiComponent2) +TEST(MPI_NumLib, ComponentNormMultiComponent2) #endif { unsigned const num_components = 3; @@ -179,7 +179,7 @@ TEST(MPITest_NumLib, ComponentNormMultiComponent2) #ifndef USE_PETSC TEST(NumLib, ComponentNormMultiComponentInfinity) #else -TEST(MPITest_NumLib, ComponentNormMultiComponentInfinity) +TEST(MPI_NumLib, ComponentNormMultiComponentInfinity) #endif { unsigned const num_components = 3; From deaed9836d9bcb4a57868e053c7cefe3e86cebf8 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Mon, 4 Nov 2024 10:30:42 +0100 Subject: [PATCH 22/24] [App/U] Add missing MPI init and finalize calls --- Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp | 14 ++++++++++++++ Applications/Utils/MeshEdit/ReorderMesh.cpp | 14 ++++++++++++++ Applications/Utils/MeshEdit/ipDataToPointCloud.cpp | 11 +++++++++++ 3 files changed, 39 insertions(+) diff --git a/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp b/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp index d5438197f93..0073291797d 100644 --- a/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp +++ b/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp @@ -24,6 +24,10 @@ #include #include +#ifdef USE_PETSC +#include +#endif + #include "BaseLib/FileTools.h" #include "BaseLib/RunTime.h" #include "GeoLib/AABB.h" @@ -386,6 +390,10 @@ int main(int argc, char* argv[]) cmd.add(input_arg); cmd.parse(argc, argv); +#ifdef USE_PETSC + MPI_Init(&argc, &argv); +#endif + if (BaseLib::getFileExtension(input_arg.getValue()) != ".pvtu") { OGS_FATAL("The extension of input file name {:s} is not \"pvtu\"", @@ -494,6 +502,9 @@ int main(int argc, char* argv[]) if (!result) { ERR("Could not write mesh to '{:s}'.", output_arg.getValue()); +#ifdef USE_PETSC + MPI_Finalize(); +#endif return EXIT_FAILURE; } INFO("writing mesh took {} s", writing_timer.elapsed()); @@ -505,5 +516,8 @@ int main(int argc, char* argv[]) // cleaned. merged_mesh.shallowClean(); +#ifdef USE_PETSC + MPI_Finalize(); +#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ReorderMesh.cpp b/Applications/Utils/MeshEdit/ReorderMesh.cpp index 220571ce22a..fd5334d5ee1 100644 --- a/Applications/Utils/MeshEdit/ReorderMesh.cpp +++ b/Applications/Utils/MeshEdit/ReorderMesh.cpp @@ -12,6 +12,10 @@ #include #include +#ifdef USE_PETSC +#include +#endif + #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -132,6 +136,10 @@ int main(int argc, char* argv[]) cmd.parse(argc, argv); +#ifdef USE_PETSC + MPI_Init(&argc, &argv); +#endif + const std::string filename(mesh_in_arg.getValue()); // read the mesh file @@ -139,6 +147,9 @@ int main(int argc, char* argv[]) std::unique_ptr(MeshLib::IO::readMeshFromFile(filename)); if (!mesh) { +#ifdef USE_PETSC + MPI_Finalize(); +#endif return EXIT_FAILURE; } @@ -236,5 +247,8 @@ int main(int argc, char* argv[]) "transferred to the reordered mesh."); } +#ifdef USE_PETSC + MPI_Finalize(); +#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp b/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp index 0004f85b724..a8a2600b640 100644 --- a/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp +++ b/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp @@ -11,6 +11,10 @@ #include +#ifdef USE_PETSC +#include +#endif + #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -251,6 +255,10 @@ int main(int argc, char** argv) cmd.parse(argc, argv); +#ifdef USE_PETSC + MPI_Init(&argc, &argv); +#endif + std::unique_ptr mesh_in( MeshLib::IO::readMeshFromFile(arg_in_file.getValue())); @@ -265,5 +273,8 @@ int main(int argc, char** argv) MeshLib::IO::writeMeshToFile(point_cloud, arg_out_file.getValue()); +#ifdef USE_PETSC + MPI_Finalize(); +#endif return EXIT_SUCCESS; } From 3c63e935377eaddb9d7944e30bf77cbfe660d218 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sat, 23 Nov 2024 08:43:35 +0100 Subject: [PATCH 23/24] [BL/MPI] Wrap MPI_Init and Finalize calls --- BaseLib/MPI.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/BaseLib/MPI.h b/BaseLib/MPI.h index 3d60e44a61b..649769740e6 100644 --- a/BaseLib/MPI.h +++ b/BaseLib/MPI.h @@ -21,6 +21,26 @@ namespace BaseLib::MPI { +struct Setup +{ + Setup(int argc, char* argv[]) + { +#ifdef USE_PETSC + MPI_Init(&argc, &argv); +#else + (void)argc; + (void)argv; +#endif // USE_PETSC + } + + ~Setup() + { +#ifdef USE_PETSC + MPI_Finalize(); +#endif // USE_PETSC + } +}; + #ifdef USE_PETSC struct Mpi { From ff9e84a73f8b3b8f4d6c326a2a190ffee1fc7cb1 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sat, 23 Nov 2024 09:26:24 +0100 Subject: [PATCH 24/24] Use MPI::Setup wrapper for MPI_Init and Finalize --- .../LinearSolverLibrarySetup.h | 7 ++-- .../Utils/FileConverter/ConvertSHPToGLI.cpp | 15 ++------- .../Utils/FileConverter/FEFLOW2OGS.cpp | 16 ++------- Applications/Utils/FileConverter/GMSH2OGS.cpp | 16 ++------- .../Utils/FileConverter/GocadSGridReader.cpp | 12 ++----- .../FileConverter/GocadTSurfaceReader.cpp | 18 ++-------- .../Utils/FileConverter/Mesh2Raster.cpp | 18 ++-------- .../Utils/FileConverter/Mesh2Shape.cpp | 15 ++------- .../Utils/FileConverter/NetCdfConverter.cpp | 27 ++------------- Applications/Utils/FileConverter/OGS2VTK.cpp | 15 ++------- Applications/Utils/FileConverter/PVD2XDMF.cpp | 12 ++----- .../Utils/FileConverter/Raster2ASC.cpp | 12 ++----- Applications/Utils/FileConverter/TIN2VTK.cpp | 16 ++------- .../Utils/FileConverter/TecPlotTools.cpp | 24 ++------------ Applications/Utils/FileConverter/VTK2OGS.cpp | 12 ++----- Applications/Utils/FileConverter/VTK2TIN.cpp | 13 ++------ .../Utils/FileConverter/convertGEO.cpp | 12 ++----- .../generateMatPropsFromMatID.cpp | 18 ++-------- Applications/Utils/GeoTools/MoveGeometry.cpp | 18 ++-------- .../Utils/GeoTools/addDataToRaster.cpp | 12 ++----- Applications/Utils/GeoTools/createRaster.cpp | 12 ++----- .../Utils/GeoTools/generateGeometry.cpp | 15 ++------- .../Utils/MeshEdit/AddElementQuality.cpp | 15 ++------- .../Utils/MeshEdit/AddFaultToVoxelGrid.cpp | 21 ++---------- Applications/Utils/MeshEdit/AddLayer.cpp | 21 ++---------- ...CreateBoundaryConditionsAlongPolylines.cpp | 18 ++-------- .../Utils/MeshEdit/ExtractBoundary.cpp | 15 ++------- .../Utils/MeshEdit/ExtractMaterials.cpp | 24 ++------------ .../Utils/MeshEdit/ExtractSurface.cpp | 18 ++-------- Applications/Utils/MeshEdit/Layers2Grid.cpp | 24 ++------------ .../MeshEdit/MapGeometryToMeshSurface.cpp | 15 ++------- Applications/Utils/MeshEdit/MeshMapping.cpp | 30 ++--------------- Applications/Utils/MeshEdit/MoveMesh.cpp | 15 ++------- .../Utils/MeshEdit/NodeReordering.cpp | 15 ++------- .../Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp | 15 ++------- .../Utils/MeshEdit/RemoveGhostData.cpp | 12 ++----- Applications/Utils/MeshEdit/ReorderMesh.cpp | 15 ++------- .../ResetPropertiesInPolygonalRegion.cpp | 24 ++------------ Applications/Utils/MeshEdit/Vtu2Grid.cpp | 26 ++------------- .../MeshEdit/appendLinesAlongPolyline.cpp | 21 ++---------- Applications/Utils/MeshEdit/checkMesh.cpp | 18 ++-------- .../Utils/MeshEdit/convertToLinearMesh.cpp | 18 ++-------- .../MeshEdit/createLayeredMeshFromRasters.cpp | 33 ++----------------- .../Utils/MeshEdit/createQuadraticMesh.cpp | 15 ++------- .../MeshEdit/createTetgenSmeshFromRasters.cpp | 33 ++----------------- .../Utils/MeshEdit/editMaterialID.cpp | 24 ++------------ .../Utils/MeshEdit/ipDataToPointCloud.cpp | 12 ++----- Applications/Utils/MeshEdit/queryMesh.cpp | 15 ++------- .../Utils/MeshEdit/removeMeshElements.cpp | 30 ++--------------- Applications/Utils/MeshEdit/reviseMesh.cpp | 15 ++------- .../Utils/MeshEdit/swapNodeCoordinateAxes.cpp | 18 ++-------- .../MeshGeoTools/AssignRasterDataToMesh.cpp | 21 ++---------- .../IntegrateBoreholesIntoMesh.cpp | 33 ++----------------- .../Utils/MeshGeoTools/Raster2Mesh.cpp | 15 ++------- .../MeshGeoTools/VerticalSliceFromLayers.cpp | 24 ++------------ ...computeSurfaceNodeIDsInPolygonalRegion.cpp | 12 ++----- .../constructMeshesFromGeometry.cpp | 14 ++------ .../createIntermediateRasters.cpp | 24 ++------------ .../Utils/MeshGeoTools/geometryToGmshGeo.cpp | 21 ++---------- .../Utils/MeshGeoTools/identifySubdomains.cpp | 12 ++----- .../ComputeNodeAreasFromSurfaceMesh.cpp | 15 ++------- .../PartitionMesh/BinaryToPVTU.cpp | 9 ++--- .../PartitionMesh/PartitionMesh.cpp | 18 ++-------- .../convertVtkDataArrayToVtkDataArray.cpp | 18 ++-------- .../ModelPreparation/createNeumannBc.cpp | 15 ++------- .../Utils/ModelPreparation/scaleProperty.cpp | 12 ++----- .../PostProcessing/Raster2PointCloud.cpp | 11 ++----- Applications/Utils/PostProcessing/postLIE.cpp | 12 ++----- .../Utils/SWMMConverter/SWMMConverter.cpp | 18 ++-------- .../createMeshElemPropertiesFromASCRaster.cpp | 12 ++----- .../generateStructuredMesh.cpp | 18 ++-------- 71 files changed, 145 insertions(+), 1099 deletions(-) diff --git a/Applications/ApplicationsLib/LinearSolverLibrarySetup.h b/Applications/ApplicationsLib/LinearSolverLibrarySetup.h index 59c94cd317d..e6b48ce2f6a 100644 --- a/Applications/ApplicationsLib/LinearSolverLibrarySetup.h +++ b/Applications/ApplicationsLib/LinearSolverLibrarySetup.h @@ -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) @@ -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); @@ -40,8 +40,9 @@ struct LinearSolverLibrarySetup final { NumLib::cleanupGlobalMatrixProviders(); PetscFinalize(); - MPI_Finalize(); } + + BaseLib::MPI::Setup mpi_setup; }; } // ApplicationsLib #elif defined(USE_LIS) diff --git a/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp b/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp index 2fb39d512d6..6023763a1b0 100644 --- a/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp +++ b/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp @@ -14,10 +14,6 @@ #include -#ifdef USE_PETSC -#include -#endif - // STL #include #include @@ -25,6 +21,7 @@ // ShapeLib #include +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h" #include "GeoLib/IO/XmlIO/Qt/XmlStnInterface.h" @@ -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()); @@ -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); @@ -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; } diff --git a/Applications/Utils/FileConverter/FEFLOW2OGS.cpp b/Applications/Utils/FileConverter/FEFLOW2OGS.cpp index acd33c06c89..e299c2b56d2 100644 --- a/Applications/Utils/FileConverter/FEFLOW2OGS.cpp +++ b/Applications/Utils/FileConverter/FEFLOW2OGS.cpp @@ -14,12 +14,8 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - -// BaseLib #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "InfoLib/GitInfo.h" #ifndef WIN32 @@ -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()); @@ -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 @@ -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; } diff --git a/Applications/Utils/FileConverter/GMSH2OGS.cpp b/Applications/Utils/FileConverter/GMSH2OGS.cpp index a528076749b..6674dbc902f 100644 --- a/Applications/Utils/FileConverter/GMSH2OGS.cpp +++ b/Applications/Utils/FileConverter/GMSH2OGS.cpp @@ -19,12 +19,8 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - -// BaseLib #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "InfoLib/GitInfo.h" #ifndef WIN32 @@ -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()); @@ -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 @@ -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; } diff --git a/Applications/Utils/FileConverter/GocadSGridReader.cpp b/Applications/Utils/FileConverter/GocadSGridReader.cpp index 0f3e6784c14..a28124cc5c3 100644 --- a/Applications/Utils/FileConverter/GocadSGridReader.cpp +++ b/Applications/Utils/FileConverter/GocadSGridReader.cpp @@ -12,16 +12,13 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #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" @@ -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."); @@ -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; } diff --git a/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp b/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp index dd93c5024e1..8bb1391f3e4 100644 --- a/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp +++ b/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp @@ -9,11 +9,8 @@ #include -#ifdef USE_PETSC -#include -#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" @@ -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; } @@ -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()); @@ -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; } diff --git a/Applications/Utils/FileConverter/Mesh2Raster.cpp b/Applications/Utils/FileConverter/Mesh2Raster.cpp index 4e32f111d57..9b090554776 100644 --- a/Applications/Utils/FileConverter/Mesh2Raster.cpp +++ b/Applications/Utils/FileConverter/Mesh2Raster.cpp @@ -9,15 +9,12 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include +#include "BaseLib/MPI.h" #include "GeoLib/AABB.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -52,9 +49,7 @@ 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 const mesh( @@ -62,18 +57,12 @@ int main(int argc, char* argv[]) 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; } @@ -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; } diff --git a/Applications/Utils/FileConverter/Mesh2Shape.cpp b/Applications/Utils/FileConverter/Mesh2Shape.cpp index b9ffd3f0493..44bb45b383b 100644 --- a/Applications/Utils/FileConverter/Mesh2Shape.cpp +++ b/Applications/Utils/FileConverter/Mesh2Shape.cpp @@ -9,11 +9,8 @@ #include -#ifdef USE_PETSC -#include -#endif - #include "Applications/FileIO/SHPInterface.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/Mesh.h" @@ -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 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; } diff --git a/Applications/Utils/FileConverter/NetCdfConverter.cpp b/Applications/Utils/FileConverter/NetCdfConverter.cpp index 84e3be1b7ab..8009dd19e5f 100644 --- a/Applications/Utils/FileConverter/NetCdfConverter.cpp +++ b/Applications/Utils/FileConverter/NetCdfConverter.cpp @@ -9,10 +9,6 @@ #include -#ifdef USE_PETSC -#include -#endif - // STL #include #include @@ -26,6 +22,7 @@ #include "BaseLib/FileTools.h" #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Raster.h" #include "InfoLib/GitInfo.h" @@ -740,18 +737,13 @@ int main(int argc, char* argv[]) cmd.add(arg_input); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); NcFile dataset(arg_input.getValue().c_str(), NcFile::read); if (dataset.isNull()) { ERR("Error opening file."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } @@ -761,9 +753,6 @@ int main(int argc, char* argv[]) { ERR("Only one output format can be specified (single-file, multi-file, " "or images)"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -783,9 +772,6 @@ int main(int argc, char* argv[]) if (var.isNull()) { ERR("Variable \"{:s}\" not found in file.", arg_varname.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -797,9 +783,6 @@ int main(int argc, char* argv[]) if (!assignDimParams(var, dim_idx_map, arg_dim_time, arg_dim1, arg_dim2, arg_dim3)) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -856,15 +839,9 @@ int main(int argc, char* argv[]) if (!convert(dataset, var, output_name, dim_idx_map, is_time_dep, time_bounds, output, elem_type)) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } std::cout << "Conversion finished successfully.\n"; -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/OGS2VTK.cpp b/Applications/Utils/FileConverter/OGS2VTK.cpp index 78f360c2883..46bc603a270 100644 --- a/Applications/Utils/FileConverter/OGS2VTK.cpp +++ b/Applications/Utils/FileConverter/OGS2VTK.cpp @@ -13,13 +13,10 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -52,17 +49,12 @@ int main(int argc, char* argv[]) cmd.add(use_ascii_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); std::unique_ptr mesh( MeshLib::IO::readMeshFromFile(mesh_in.getValue())); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(), @@ -73,8 +65,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeVtu(*mesh, mesh_out.getValue(), data_mode); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/PVD2XDMF.cpp b/Applications/Utils/FileConverter/PVD2XDMF.cpp index 838aa202559..6c114ef6673 100644 --- a/Applications/Utils/FileConverter/PVD2XDMF.cpp +++ b/Applications/Utils/FileConverter/PVD2XDMF.cpp @@ -9,10 +9,6 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include @@ -20,6 +16,7 @@ #include "BaseLib/FileTools.h" #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "BaseLib/MemWatch.h" #include "BaseLib/RunTime.h" #include "BaseLib/StringTools.h" @@ -141,9 +138,7 @@ int main(int argc, char* argv[]) cmd.add(outdir_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); BaseLib::setConsoleLogLevel(log_level_arg.getValue()); auto const pvd_file_dir = BaseLib::extractPath(pvd_file_arg.getValue()); @@ -237,8 +232,5 @@ int main(int argc, char* argv[]) mesh_xdmf_hdf_writer->writeStep(time); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/Raster2ASC.cpp b/Applications/Utils/FileConverter/Raster2ASC.cpp index de81adf7d19..398d8665c11 100644 --- a/Applications/Utils/FileConverter/Raster2ASC.cpp +++ b/Applications/Utils/FileConverter/Raster2ASC.cpp @@ -11,10 +11,7 @@ #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Raster.h" #include "InfoLib/GitInfo.h" @@ -41,9 +38,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::unique_ptr raster( FileIO::AsciiRasterInterface::readRaster(input_arg.getValue())); @@ -61,8 +56,5 @@ int main(int argc, char* argv[]) } FileIO::AsciiRasterInterface::writeRasterAsASC(*raster, output_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/TIN2VTK.cpp b/Applications/Utils/FileConverter/TIN2VTK.cpp index 91cc0a1962b..1d2b7290baa 100644 --- a/Applications/Utils/FileConverter/TIN2VTK.cpp +++ b/Applications/Utils/FileConverter/TIN2VTK.cpp @@ -9,17 +9,13 @@ #include -#ifdef USE_PETSC -#include -#endif - // STL #include #include #include -// BaseLib #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" // GeoLib @@ -53,9 +49,7 @@ int main(int argc, char* argv[]) cmd.add(outArg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); INFO("reading the TIN file..."); const std::string tinFileName(inArg.getValue()); @@ -66,9 +60,6 @@ int main(int argc, char* argv[]) GeoLib::IO::TINInterface::readTIN(tinFileName, point_vec)); if (!sfc) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("TIN read: {:d} points, {:d} triangles", point_vec.size(), @@ -85,8 +76,5 @@ int main(int argc, char* argv[]) MeshLib::IO::VtuInterface writer(mesh.get()); writer.writeToFile(outArg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/TecPlotTools.cpp b/Applications/Utils/FileConverter/TecPlotTools.cpp index 0d70176cf19..bf209269396 100644 --- a/Applications/Utils/FileConverter/TecPlotTools.cpp +++ b/Applications/Utils/FileConverter/TecPlotTools.cpp @@ -9,16 +9,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "GeoLib/Point.h" #include "InfoLib/GitInfo.h" @@ -480,25 +477,17 @@ 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); if (!input_arg.isSet()) { ERR("No input file given. Please specify TecPlot (*.plt) file"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } if (convert_arg.getValue() && !output_arg.isSet()) { ERR("No output file given. Please specify OGS mesh (*.vtu) file"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } @@ -506,18 +495,12 @@ int main(int argc, char* argv[]) if (!in.is_open()) { ERR("Could not open file {:s}.", input_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -2; } if (!convert_arg.isSet() && !split_arg.isSet()) { INFO("Nothing to do. Use -s to split or -c to convert."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 0; } @@ -534,8 +517,5 @@ int main(int argc, char* argv[]) } in.close(); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return return_val; } diff --git a/Applications/Utils/FileConverter/VTK2OGS.cpp b/Applications/Utils/FileConverter/VTK2OGS.cpp index 2ec723dc2e7..80c424df8a1 100644 --- a/Applications/Utils/FileConverter/VTK2OGS.cpp +++ b/Applications/Utils/FileConverter/VTK2OGS.cpp @@ -14,12 +14,9 @@ // STL #include -#ifdef USE_PETSC -#include -#endif - #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/Legacy/MeshIO.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" @@ -47,9 +44,7 @@ int main(int argc, char* argv[]) cmd.add(mesh_out); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); MeshLib::Mesh* mesh( MeshLib::IO::VtuInterface::readVTUFile(mesh_in.getValue())); @@ -60,8 +55,5 @@ int main(int argc, char* argv[]) meshIO.setMesh(mesh); BaseLib::IO::writeStringToFile(meshIO.writeToString(), mesh_out.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/VTK2TIN.cpp b/Applications/Utils/FileConverter/VTK2TIN.cpp index b0401d1b199..bde167facc2 100644 --- a/Applications/Utils/FileConverter/VTK2TIN.cpp +++ b/Applications/Utils/FileConverter/VTK2TIN.cpp @@ -9,17 +9,13 @@ #include -#ifdef USE_PETSC -#include -#endif - // STL #include #include #include -// BaseLib #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" // GeoLib @@ -56,9 +52,7 @@ int main(int argc, char* argv[]) cmd.add(mesh_out); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); std::unique_ptr mesh( MeshLib::IO::VtuInterface::readVTUFile(mesh_in.getValue())); INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(), @@ -74,8 +68,5 @@ int main(int argc, char* argv[]) mesh_out.getValue()); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/convertGEO.cpp b/Applications/Utils/FileConverter/convertGEO.cpp index c7f76b7f641..3a7d859d519 100644 --- a/Applications/Utils/FileConverter/convertGEO.cpp +++ b/Applications/Utils/FileConverter/convertGEO.cpp @@ -10,15 +10,12 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include "Applications/FileIO/readGeometryFromFile.h" #include "Applications/FileIO/writeGeometryToFile.h" +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "InfoLib/GitInfo.h" @@ -51,9 +48,7 @@ int main(int argc, char* argv[]) cmd.add(gmsh_path_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); GeoLib::GEOObjects geoObjects; FileIO::readGeometryFromFile(argInputFileName.getValue(), geoObjects, @@ -64,8 +59,5 @@ int main(int argc, char* argv[]) FileIO::writeGeometryToFile(geo_names[0], geoObjects, argOutputFileName.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp b/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp index 9291eacb3b7..88032d6354e 100644 --- a/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp +++ b/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp @@ -14,14 +14,11 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -50,9 +47,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 std::unique_ptr mesh( @@ -61,9 +56,6 @@ int main(int argc, char* argv[]) if (!mesh) { INFO("Could not read mesh from file '{:s}'.", mesh_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -92,9 +84,6 @@ int main(int argc, char* argv[]) else { ERR("Could not create property '{:s}' file.", new_matname); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -106,8 +95,5 @@ int main(int argc, char* argv[]) INFO("New files '{:s}' and '{:s}' written.", new_mshname, new_matname); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/GeoTools/MoveGeometry.cpp b/Applications/Utils/GeoTools/MoveGeometry.cpp index 33505f1d594..dd14e7210d2 100644 --- a/Applications/Utils/GeoTools/MoveGeometry.cpp +++ b/Applications/Utils/GeoTools/MoveGeometry.cpp @@ -14,10 +14,7 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h" #include "InfoLib/GitInfo.h" @@ -49,9 +46,7 @@ int main(int argc, char* argv[]) cmd.add(geo_input_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); GeoLib::GEOObjects geo_objects; GeoLib::IO::BoostXmlGmlInterface xml(geo_objects); @@ -59,9 +54,6 @@ int main(int argc, char* argv[]) { if (!xml.readFile(geo_input_arg.getValue())) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -69,9 +61,6 @@ int main(int argc, char* argv[]) { ERR("Failed to read file `{:s}'.", geo_input_arg.getValue()); ERR("{:s}", err.what()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -106,8 +95,5 @@ int main(int argc, char* argv[]) BaseLib::IO::writeStringToFile(xml.writeToString(), geo_output_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/GeoTools/addDataToRaster.cpp b/Applications/Utils/GeoTools/addDataToRaster.cpp index 425bfb5d689..c78dc0549cc 100644 --- a/Applications/Utils/GeoTools/addDataToRaster.cpp +++ b/Applications/Utils/GeoTools/addDataToRaster.cpp @@ -11,16 +11,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include +#include "BaseLib/MPI.h" #include "GeoLib/AABB.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Point.h" @@ -153,9 +150,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::array input_points = { GeoLib::Point{{ll_x_arg.getValue(), ll_y_arg.getValue(), 0}}, @@ -210,8 +205,5 @@ int main(int argc, char* argv[]) FileIO::AsciiRasterInterface::writeRasterAsASC(*raster, out_raster_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/GeoTools/createRaster.cpp b/Applications/Utils/GeoTools/createRaster.cpp index 8d36fc335aa..b3aa8b4b8f0 100644 --- a/Applications/Utils/GeoTools/createRaster.cpp +++ b/Applications/Utils/GeoTools/createRaster.cpp @@ -11,12 +11,9 @@ #include -#ifdef USE_PETSC -#include -#endif - #include +#include "BaseLib/MPI.h" #include "GeoLib/AABB.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Point.h" @@ -71,9 +68,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); GeoLib::RasterHeader header{ n_cols.getValue(), @@ -88,8 +83,5 @@ int main(int argc, char* argv[]) FileIO::AsciiRasterInterface::writeRasterAsASC(raster, output_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/GeoTools/generateGeometry.cpp b/Applications/Utils/GeoTools/generateGeometry.cpp index b84b167e37f..56c054e79e0 100644 --- a/Applications/Utils/GeoTools/generateGeometry.cpp +++ b/Applications/Utils/GeoTools/generateGeometry.cpp @@ -11,12 +11,9 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - #include +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h" #include "GeoLib/Point.h" @@ -241,9 +238,7 @@ int main(int argc, char* argv[]) cmd.add(geo_output_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); auto const p0 = GeoLib::Point{x0.getValue(), y0.getValue(), z0.getValue()}; auto const p1 = GeoLib::Point{x1.getValue(), y1.getValue(), z1.getValue()}; @@ -286,9 +281,6 @@ int main(int argc, char* argv[]) std::move(polyline_name.getValue()), geometry_name.getValue(), geometry) == EXIT_FAILURE) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -298,8 +290,5 @@ int main(int argc, char* argv[]) BaseLib::IO::writeStringToFile(xml.writeToString(), geo_output_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/AddElementQuality.cpp b/Applications/Utils/MeshEdit/AddElementQuality.cpp index 25753925c28..08c7087db3b 100644 --- a/Applications/Utils/MeshEdit/AddElementQuality.cpp +++ b/Applications/Utils/MeshEdit/AddElementQuality.cpp @@ -9,13 +9,10 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -53,9 +50,7 @@ int main(int argc, char* argv[]) cmd.add(mesh_in_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); // read the mesh file BaseLib::RunTime run_time; @@ -64,9 +59,6 @@ int main(int argc, char* argv[]) mesh_in_arg.getValue(), true /* compute_element_neighbors */)); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Time for reading: {:g} s", run_time.elapsed()); @@ -82,8 +74,5 @@ int main(int argc, char* argv[]) INFO("Writing mesh '{:s}' ... ", mesh_out_arg.getValue()); MeshLib::IO::writeMeshToFile(*mesh, mesh_out_arg.getValue()); INFO("done."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/AddFaultToVoxelGrid.cpp b/Applications/Utils/MeshEdit/AddFaultToVoxelGrid.cpp index e27be76f846..2d4e47d92a5 100644 --- a/Applications/Utils/MeshEdit/AddFaultToVoxelGrid.cpp +++ b/Applications/Utils/MeshEdit/AddFaultToVoxelGrid.cpp @@ -15,10 +15,7 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -64,9 +61,7 @@ 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); std::string const input_name = input_arg.getValue(); std::string const fault_name = fault_arg.getValue(); @@ -81,17 +76,11 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("Input mesh not found..."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } auto const& mat_ids = MeshLib::materialIDs(*mesh); if (!mat_ids) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif ERR("Input mesh has no material IDs"); return EXIT_FAILURE; } @@ -105,15 +94,9 @@ int main(int argc, char* argv[]) { MeshLib::IO::VtuInterface vtu(mesh.get()); vtu.writeToFile(output_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif INFO("The fault was successfully added."); return EXIT_SUCCESS; } -#ifdef USE_PETSC - MPI_Finalize(); -#endif ERR("No fault could be added."); return EXIT_FAILURE; } diff --git a/Applications/Utils/MeshEdit/AddLayer.cpp b/Applications/Utils/MeshEdit/AddLayer.cpp index 7958e404e2d..90e2d51efb7 100644 --- a/Applications/Utils/MeshEdit/AddLayer.cpp +++ b/Applications/Utils/MeshEdit/AddLayer.cpp @@ -11,13 +11,10 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -78,15 +75,10 @@ int main(int argc, char* argv[]) { ERR("It is not possible to set both options '--copy-material-ids' and " "'--set-material-id'."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue()); auto subsfc_mesh = std::unique_ptr( @@ -94,9 +86,6 @@ int main(int argc, char* argv[]) if (!subsfc_mesh) { ERR("Error reading mesh '{:s}'.", mesh_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("done."); @@ -114,9 +103,6 @@ int main(int argc, char* argv[]) if (!result) { ERR("Failure while adding layer."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -124,8 +110,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*result, mesh_out_arg.getValue()); INFO("done."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp b/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp index 070bcedc532..1debc20fb54 100644 --- a/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp +++ b/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp @@ -12,10 +12,6 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include @@ -24,6 +20,7 @@ #include "Applications/FileIO/readGeometryFromFile.h" #include "Applications/FileIO/writeGeometryToFile.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/Point.h" #include "InfoLib/GitInfo.h" @@ -183,9 +180,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 mesh '{:s}' ... ", mesh_arg.getValue()); @@ -216,9 +211,6 @@ int main(int argc, char* argv[]) { ERR("Could not get vector of polylines out of geometry '{:s}'.", geo_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -251,9 +243,6 @@ int main(int argc, char* argv[]) if (geo_names.empty()) { ERR("Did not find mesh nodes along polylines."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -313,8 +302,5 @@ int main(int argc, char* argv[]) BaseLib::dropFileExtension(output_base_fname.getValue())); writeBCsAndGeometry(geometry_sets, surface_name, base_fname, bc_type.getValue(), gml_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ExtractBoundary.cpp b/Applications/Utils/MeshEdit/ExtractBoundary.cpp index 744929e8c64..f643dbd7d10 100644 --- a/Applications/Utils/MeshEdit/ExtractBoundary.cpp +++ b/Applications/Utils/MeshEdit/ExtractBoundary.cpp @@ -11,16 +11,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" @@ -63,18 +60,13 @@ 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::unique_ptr mesh(MeshLib::IO::readMeshFromFile( mesh_in.getValue(), true /* compute_element_neighbors */)); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -102,8 +94,5 @@ int main(int argc, char* argv[]) use_ascii_arg.getValue() ? vtkXMLWriter::Ascii : vtkXMLWriter::Binary; MeshLib::IO::writeVtu(*surface_mesh, out_fname, data_mode); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ExtractMaterials.cpp b/Applications/Utils/MeshEdit/ExtractMaterials.cpp index 9edf690aae1..050cf13656a 100644 --- a/Applications/Utils/MeshEdit/ExtractMaterials.cpp +++ b/Applications/Utils/MeshEdit/ExtractMaterials.cpp @@ -12,11 +12,8 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -73,9 +70,7 @@ 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); std::string const input_name = input_arg.getValue(); std::string const output_name = output_arg.getValue(); @@ -87,9 +82,6 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("Error reading input mesh. Aborting..."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -97,9 +89,6 @@ int main(int argc, char* argv[]) if (mat_ids == nullptr) { ERR("No material IDs found in mesh. Aborting..."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -107,9 +96,6 @@ int main(int argc, char* argv[]) if (id_range.first == id_range.second) { ERR("Mesh only contains one material, no extraction required."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } int min_id, max_id; @@ -119,9 +105,6 @@ int main(int argc, char* argv[]) if (min_id < *id_range.first || min_id > *id_range.second) { ERR("Specified material ID does not exist."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } max_id = min_id; @@ -160,8 +143,5 @@ int main(int argc, char* argv[]) { ostream.close(); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ExtractSurface.cpp b/Applications/Utils/MeshEdit/ExtractSurface.cpp index da959739497..f8a95e304de 100644 --- a/Applications/Utils/MeshEdit/ExtractSurface.cpp +++ b/Applications/Utils/MeshEdit/ExtractSurface.cpp @@ -11,16 +11,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" @@ -75,9 +72,7 @@ int main(int argc, char* argv[]) cmd.add(mesh_in); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); std::unique_ptr mesh(MeshLib::IO::readMeshFromFile( mesh_in.getValue(), true /* compute_element_neighbors */)); @@ -85,18 +80,12 @@ int main(int argc, char* argv[]) if (!mesh) { ERR("Error reading mesh file."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (mesh->getDimension() != 3) { ERR("Surfaces can currently only be extracted from 3D meshes."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -122,8 +111,5 @@ int main(int argc, char* argv[]) use_ascii_arg.getValue() ? vtkXMLWriter::Ascii : vtkXMLWriter::Binary; MeshLib::IO::writeVtu(*surface_mesh, out_fname, data_mode); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/Layers2Grid.cpp b/Applications/Utils/MeshEdit/Layers2Grid.cpp index 6108bee99fd..12525d1326c 100644 --- a/Applications/Utils/MeshEdit/Layers2Grid.cpp +++ b/Applications/Utils/MeshEdit/Layers2Grid.cpp @@ -15,11 +15,8 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/IO/readStringListFromFile.h" +#include "BaseLib/MPI.h" #include "GeoLib/AABB.h" #include "InfoLib/GitInfo.h" #include "MathLib/Point3d.h" @@ -78,18 +75,13 @@ 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); if ((y_arg.isSet() && !z_arg.isSet()) || ((!y_arg.isSet() && z_arg.isSet()))) { ERR("For equilateral cubes, only x needs to be set. For unequal " "cuboids, all three edge lengths (x/y/z) need to be specified."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -104,9 +96,6 @@ int main(int argc, char* argv[]) if (layer_names.size() < 2) { ERR("At least two layers are required to create a 3D Mesh"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -124,9 +113,6 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("Input layer '{:s}' not found. Aborting...", layer); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } layers.emplace_back(mesh); @@ -141,15 +127,9 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("The VoxelGrid could not be created."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } MeshLib::IO::VtuInterface vtu(mesh.get()); vtu.writeToFile(output_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp b/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp index d32118f004c..106e8305d44 100644 --- a/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp +++ b/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp @@ -11,14 +11,11 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h" #include "InfoLib/GitInfo.h" @@ -60,9 +57,7 @@ int main(int argc, char* argv[]) cmd.add(output_geometry_fname); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); // *** read geometry GeoLib::GEOObjects geometries; @@ -75,9 +70,6 @@ int main(int argc, char* argv[]) } else { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -105,8 +97,5 @@ int main(int argc, char* argv[]) BaseLib::IO::writeStringToFile(xml_io.writeToString(), output_geometry_fname.getValue()); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/MeshMapping.cpp b/Applications/Utils/MeshEdit/MeshMapping.cpp index 6b939c1821d..3d3f3139fda 100644 --- a/Applications/Utils/MeshEdit/MeshMapping.cpp +++ b/Applications/Utils/MeshEdit/MeshMapping.cpp @@ -15,11 +15,8 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "GeoLib/AABB.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Raster.h" @@ -54,9 +51,7 @@ double getClosestPointElevation(MeshLib::Node const& p, int main(int argc, char* argv[]) { -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); TCLAP::CmdLine cmd( "Changes the elevation of 2D mesh nodes based on either raster data or " "another 2D mesh. In addition, a low pass filter can be applied to " @@ -107,9 +102,6 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("Error reading mesh file."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -118,18 +110,12 @@ int main(int argc, char* argv[]) { ERR("Nothing to do. Please choose mapping based on a raster or mesh " "file, or to a static value."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (map_raster_arg.isSet() && map_mesh_arg.isSet()) { ERR("Please select mapping based on *either* a mesh or a raster file."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -148,9 +134,6 @@ int main(int argc, char* argv[]) if (!file_stream.good()) { ERR("Opening raster file {} failed.", raster_path); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } file_stream.close(); @@ -168,9 +151,6 @@ int main(int argc, char* argv[]) if (ground_truth == nullptr) { ERR("Error reading mesh file."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -243,15 +223,9 @@ int main(int argc, char* argv[]) if (MeshLib::IO::writeMeshToFile(*mesh, output_arg.getValue()) != 0) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Result successfully written."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/MoveMesh.cpp b/Applications/Utils/MeshEdit/MoveMesh.cpp index a755a792143..65cefe2c3bb 100644 --- a/Applications/Utils/MeshEdit/MoveMesh.cpp +++ b/Applications/Utils/MeshEdit/MoveMesh.cpp @@ -11,11 +11,8 @@ #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "GeoLib/AABB.h" #include "InfoLib/GitInfo.h" @@ -64,9 +61,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(mesh_arg.getValue()); std::unique_ptr mesh(MeshLib::IO::readMeshFromFile(fname)); @@ -74,9 +69,6 @@ int main(int argc, char* argv[]) if (!mesh) { ERR("Could not read mesh from file '{:s}'.", fname); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -107,8 +99,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*mesh, out_fname); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/NodeReordering.cpp b/Applications/Utils/MeshEdit/NodeReordering.cpp index cc83457c3a4..f98f0063975 100644 --- a/Applications/Utils/MeshEdit/NodeReordering.cpp +++ b/Applications/Utils/MeshEdit/NodeReordering.cpp @@ -11,16 +11,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include "BaseLib/Algorithm.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -191,18 +188,13 @@ int main(int argc, char* argv[]) cmd.add(input_mesh_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); std::unique_ptr mesh( MeshLib::IO::readMeshFromFile(input_mesh_arg.getValue())); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -228,8 +220,5 @@ int main(int argc, char* argv[]) INFO("VTU file written."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp b/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp index 0073291797d..753004bf2bc 100644 --- a/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp +++ b/Applications/Utils/MeshEdit/PVTU2VTU/PVTU2VTU.cpp @@ -24,11 +24,8 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "GeoLib/AABB.h" #include "GeoLib/OctTree.h" @@ -390,9 +387,7 @@ 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); if (BaseLib::getFileExtension(input_arg.getValue()) != ".pvtu") { @@ -502,9 +497,6 @@ int main(int argc, char* argv[]) if (!result) { ERR("Could not write mesh to '{:s}'.", output_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("writing mesh took {} s", writing_timer.elapsed()); @@ -516,8 +508,5 @@ int main(int argc, char* argv[]) // cleaned. merged_mesh.shallowClean(); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/RemoveGhostData.cpp b/Applications/Utils/MeshEdit/RemoveGhostData.cpp index 1824b8661c7..1384ac736b2 100644 --- a/Applications/Utils/MeshEdit/RemoveGhostData.cpp +++ b/Applications/Utils/MeshEdit/RemoveGhostData.cpp @@ -15,11 +15,8 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -51,9 +48,7 @@ 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); vtkSmartPointer reader = vtkSmartPointer::New(); @@ -73,8 +68,5 @@ int main(int argc, char* argv[]) writer->SetFileName(output_arg.getValue().c_str()); writer->Write(); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ReorderMesh.cpp b/Applications/Utils/MeshEdit/ReorderMesh.cpp index fd5334d5ee1..5de0c9879c8 100644 --- a/Applications/Utils/MeshEdit/ReorderMesh.cpp +++ b/Applications/Utils/MeshEdit/ReorderMesh.cpp @@ -12,10 +12,7 @@ #include #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -136,9 +133,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); const std::string filename(mesh_in_arg.getValue()); @@ -147,9 +142,6 @@ int main(int argc, char* argv[]) std::unique_ptr(MeshLib::IO::readMeshFromFile(filename)); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -247,8 +239,5 @@ int main(int argc, char* argv[]) "transferred to the reordered mesh."); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp b/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp index 76631d3ea97..ada2f29b6dd 100644 --- a/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp +++ b/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp @@ -16,11 +16,8 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include "Applications/FileIO/readGeometryFromFile.h" +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/Polygon.h" #include "InfoLib/GitInfo.h" @@ -99,9 +96,7 @@ int main(int argc, char* argv[]) cmd.add(gmsh_path_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); // *** read geometry GeoLib::GEOObjects geometries; @@ -117,9 +112,6 @@ int main(int argc, char* argv[]) { ERR("Could not get vector of polylines out of geometry '{:s}'.", geo_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -129,9 +121,6 @@ int main(int argc, char* argv[]) if (!ply) { ERR("Polyline '{:s}' not found.", polygon_name_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -140,9 +129,6 @@ int main(int argc, char* argv[]) { ERR("Polyline '{:s}' is not closed, i.e. does not describe a region.", polygon_name_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -154,9 +140,6 @@ int main(int argc, char* argv[]) if (!mesh) { // error message written already by readMeshFromFile -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } std::string const& property_name(property_name_arg.getValue()); @@ -186,8 +169,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/Vtu2Grid.cpp b/Applications/Utils/MeshEdit/Vtu2Grid.cpp index 7429e1d9f72..55a8bc6f582 100644 --- a/Applications/Utils/MeshEdit/Vtu2Grid.cpp +++ b/Applications/Utils/MeshEdit/Vtu2Grid.cpp @@ -10,6 +10,7 @@ #include #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/writeMeshToFile.h" #include "MeshLib/Mesh.h" @@ -18,9 +19,6 @@ #include "MeshToolsLib/MeshGenerators/MeshGenerator.h" #include "MeshToolsLib/MeshGenerators/VoxelGridFromMesh.h" -#ifdef USE_PETSC -#include -#endif int main(int argc, char* argv[]) { TCLAP::CmdLine cmd( @@ -64,18 +62,13 @@ 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); if ((y_arg.isSet() && !z_arg.isSet()) || ((!y_arg.isSet() && z_arg.isSet()))) { ERR("For equilateral cubes, only x needs to be set. For unequal " "cuboids, all three edge lengths (x/y/z) need to be specified."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } using namespace MeshToolsLib::MeshGenerator; @@ -88,9 +81,6 @@ int main(int argc, char* argv[]) { ERR("A cellsize ({},{},{}) is not allowed to be <= 0", x_size, y_size, z_size); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } @@ -113,9 +103,6 @@ int main(int argc, char* argv[]) { ERR("The range ({},{},{}) is not allowed to be < 0", ranges[0], ranges[1], ranges[2]); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } std::array const dims = @@ -134,9 +121,6 @@ int main(int argc, char* argv[]) if (!VoxelGridFromMesh::removeUnusedGridCells(mesh, grid)) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -144,14 +128,8 @@ int main(int argc, char* argv[]) if (MeshLib::IO::writeMeshToFile(*grid, output_arg.getValue()) != 0) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/appendLinesAlongPolyline.cpp b/Applications/Utils/MeshEdit/appendLinesAlongPolyline.cpp index e7ab944bd29..69e3baa1522 100644 --- a/Applications/Utils/MeshEdit/appendLinesAlongPolyline.cpp +++ b/Applications/Utils/MeshEdit/appendLinesAlongPolyline.cpp @@ -11,12 +11,9 @@ #include -#ifdef USE_PETSC -#include -#endif - #include "Applications/FileIO/readGeometryFromFile.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/PolylineVec.h" #include "InfoLib/GitInfo.h" @@ -58,9 +55,7 @@ int main(int argc, char* argv[]) // parse arguments cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); // read GEO objects GeoLib::GEOObjects geo_objs; @@ -71,9 +66,6 @@ int main(int argc, char* argv[]) if (geo_names.empty()) { ERR("No geometries found."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } const GeoLib::PolylineVec* ply_vec( @@ -81,9 +73,6 @@ int main(int argc, char* argv[]) if (!ply_vec) { ERR("Could not find polylines in geometry '{:s}'.", geo_names.front()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -93,9 +82,6 @@ int main(int argc, char* argv[]) if (!mesh) { ERR("Mesh file '{:s}' not found", mesh_in.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(), @@ -109,8 +95,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*new_mesh, mesh_out.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/checkMesh.cpp b/Applications/Utils/MeshEdit/checkMesh.cpp index 4795937702c..2d31816fd60 100644 --- a/Applications/Utils/MeshEdit/checkMesh.cpp +++ b/Applications/Utils/MeshEdit/checkMesh.cpp @@ -7,18 +7,14 @@ * http://www.opengeosys.org/project/license */ -#include - -#ifdef USE_PETSC -#include -#endif - #include +#include #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/MemWatch.h" #include "BaseLib/RunTime.h" #include "BaseLib/StringTools.h" @@ -52,9 +48,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 mesh file BaseLib::MemWatch mem_watch; @@ -65,9 +59,6 @@ int main(int argc, char* argv[]) mesh_arg.getValue(), true /* compute_element_neighbors */)); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -106,8 +97,5 @@ int main(int argc, char* argv[]) // Remark: MeshValidation can modify the original mesh MeshToolsLib::MeshInformation::writeMeshValidationResults(*mesh); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/convertToLinearMesh.cpp b/Applications/Utils/MeshEdit/convertToLinearMesh.cpp index 2eeea03f024..61569eaaba6 100644 --- a/Applications/Utils/MeshEdit/convertToLinearMesh.cpp +++ b/Applications/Utils/MeshEdit/convertToLinearMesh.cpp @@ -11,13 +11,10 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -42,25 +39,17 @@ 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::unique_ptr mesh( MeshLib::IO::readMeshFromFile(input_arg.getValue())); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (!mesh->hasNonlinearElement()) { ERR("The input mesh is linear. Exit."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -71,8 +60,5 @@ int main(int argc, char* argv[]) INFO("Save the new mesh into a file"); MeshLib::IO::writeMeshToFile(*new_mesh, output_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp b/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp index 91ab223b4bc..56805e54f94 100644 --- a/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp +++ b/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp @@ -18,12 +18,9 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/FileTools.h" #include "BaseLib/IO/readStringListFromFile.h" +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" @@ -79,9 +76,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); if (min_thickness_arg.isSet()) { @@ -89,9 +84,6 @@ int main(int argc, char* argv[]) if (min_thickness < 0) { ERR("Minimum layer thickness must be non-negative value."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -102,17 +94,11 @@ int main(int argc, char* argv[]) if (!sfc_mesh) { ERR("Error reading mesh '{:s}'.", mesh_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (sfc_mesh->getDimension() != 2) { ERR("Input mesh must be a 2D mesh."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("done."); @@ -122,9 +108,6 @@ int main(int argc, char* argv[]) if (raster_paths.size() < 2) { ERR("At least two raster files needed to create 3D mesh."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } std::reverse(raster_paths.begin(), raster_paths.end()); @@ -134,18 +117,12 @@ int main(int argc, char* argv[]) { if (!mapper.createLayers(*sfc_mesh, *rasters, min_thickness)) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } else { ERR("Reading raster files."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -160,9 +137,6 @@ int main(int argc, char* argv[]) if (result_mesh == nullptr) { ERR("Mapper returned empty result for 'SubsurfaceMesh'."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -172,8 +146,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeVtu(*result_mesh, output_name, data_mode); INFO("done."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/createQuadraticMesh.cpp b/Applications/Utils/MeshEdit/createQuadraticMesh.cpp index 735c37537c4..6758cf37146 100644 --- a/Applications/Utils/MeshEdit/createQuadraticMesh.cpp +++ b/Applications/Utils/MeshEdit/createQuadraticMesh.cpp @@ -9,13 +9,10 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -44,17 +41,12 @@ int main(int argc, char* argv[]) cmd.add(add_centre_node_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); std::unique_ptr mesh( MeshLib::IO::readMeshFromFile(input_arg.getValue())); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -65,8 +57,5 @@ int main(int argc, char* argv[]) INFO("Save the new mesh into a file"); MeshLib::IO::writeMeshToFile(*new_mesh, output_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/createTetgenSmeshFromRasters.cpp b/Applications/Utils/MeshEdit/createTetgenSmeshFromRasters.cpp index 838eb9bde55..4b2a881ff69 100644 --- a/Applications/Utils/MeshEdit/createTetgenSmeshFromRasters.cpp +++ b/Applications/Utils/MeshEdit/createTetgenSmeshFromRasters.cpp @@ -18,13 +18,10 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include "Applications/FileIO/TetGenInterface.h" #include "BaseLib/FileTools.h" #include "BaseLib/IO/readStringListFromFile.h" +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" @@ -85,9 +82,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); if (min_thickness_arg.isSet()) { @@ -95,9 +90,6 @@ int main(int argc, char* argv[]) if (min_thickness < 0) { ERR("Minimum layer thickness must be non-negative value."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -108,17 +100,11 @@ int main(int argc, char* argv[]) if (!sfc_mesh) { ERR("Error reading mesh '{:s}'.", mesh_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (sfc_mesh->getDimension() != 2) { ERR("Input mesh must be a 2D mesh."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("done."); @@ -128,9 +114,6 @@ int main(int argc, char* argv[]) if (raster_paths.size() < 2) { ERR("At least two raster files needed to create 3D mesh."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } std::reverse(raster_paths.begin(), raster_paths.end()); @@ -148,18 +131,12 @@ int main(int argc, char* argv[]) if (!lv.createLayers(*sfc_mesh, *rasters, min_thickness)) { ERR("Creating the layers was erroneous."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } else { ERR("The raster files are not accessible."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } std::unique_ptr tg_mesh = @@ -174,14 +151,8 @@ int main(int argc, char* argv[]) else { ERR("The tetgen-smesh could not be created."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/editMaterialID.cpp b/Applications/Utils/MeshEdit/editMaterialID.cpp index 17999779690..61d0892de46 100644 --- a/Applications/Utils/MeshEdit/editMaterialID.cpp +++ b/Applications/Utils/MeshEdit/editMaterialID.cpp @@ -9,12 +9,9 @@ #include -#ifdef USE_PETSC -#include -#endif - #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -68,24 +65,16 @@ 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 (!replaceArg.isSet() && !condenseArg.isSet() && !specifyArg.isSet()) { INFO("Please select editing mode: -r or -c or -s"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 0; } if (replaceArg.isSet() && condenseArg.isSet()) { INFO("Please select only one editing mode: -r or -c or -s"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 0; } if (replaceArg.isSet()) @@ -95,9 +84,6 @@ int main(int argc, char* argv[]) INFO( "current and new material IDs must be provided for " "replacement"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 0; } } @@ -108,9 +94,6 @@ int main(int argc, char* argv[]) INFO( "element type and new material IDs must be provided to specify " "elements"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 0; } } @@ -170,8 +153,5 @@ int main(int argc, char* argv[]) } MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp b/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp index a8a2600b640..d1fda1f1f9d 100644 --- a/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp +++ b/Applications/Utils/MeshEdit/ipDataToPointCloud.cpp @@ -11,10 +11,7 @@ #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -255,9 +252,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::unique_ptr mesh_in( MeshLib::IO::readMeshFromFile(arg_in_file.getValue())); @@ -273,8 +268,5 @@ int main(int argc, char** argv) MeshLib::IO::writeMeshToFile(point_cloud, arg_out_file.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/queryMesh.cpp b/Applications/Utils/MeshEdit/queryMesh.cpp index fd713e8fe74..c4a3cb9d3e0 100644 --- a/Applications/Utils/MeshEdit/queryMesh.cpp +++ b/Applications/Utils/MeshEdit/queryMesh.cpp @@ -9,16 +9,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" @@ -52,9 +49,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); const std::string filename(mesh_arg.getValue()); @@ -64,9 +59,6 @@ int main(int argc, char* argv[]) filename, true /* compute_element_neighbors */)); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -155,8 +147,5 @@ int main(int argc, char* argv[]) out << std::endl; INFO("{:s}", out.str()); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/removeMeshElements.cpp b/Applications/Utils/MeshEdit/removeMeshElements.cpp index 456fe2635f1..bd20620b226 100644 --- a/Applications/Utils/MeshEdit/removeMeshElements.cpp +++ b/Applications/Utils/MeshEdit/removeMeshElements.cpp @@ -13,12 +13,9 @@ #include -#ifdef USE_PETSC -#include -#endif - #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -182,17 +179,12 @@ int main(int argc, char* argv[]) cmd.add(mesh_in); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); std::unique_ptr mesh( MeshLib::IO::readMeshFromFile(mesh_in.getValue())); if (mesh == nullptr) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -229,9 +221,6 @@ int main(int argc, char* argv[]) !property_name_arg.isSet()) { ERR("Specify a property name for the value/range selected."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -241,9 +230,6 @@ int main(int argc, char* argv[]) { ERR("Specify a value or range ('-min-value' and '-max_value') for " "the property selected."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -264,9 +250,6 @@ int main(int argc, char* argv[]) { ERR("Specify if the inside or the outside of the selected " "range should be removed."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -299,9 +282,6 @@ int main(int argc, char* argv[]) } if (aabb_error) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -324,17 +304,11 @@ int main(int argc, char* argv[]) if (new_mesh == nullptr) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } // write into a file MeshLib::IO::writeMeshToFile(*new_mesh, mesh_out.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/reviseMesh.cpp b/Applications/Utils/MeshEdit/reviseMesh.cpp index ff9c0d3509a..ca06a00b084 100644 --- a/Applications/Utils/MeshEdit/reviseMesh.cpp +++ b/Applications/Utils/MeshEdit/reviseMesh.cpp @@ -9,15 +9,12 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" @@ -59,18 +56,13 @@ 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); // read a mesh file std::unique_ptr org_mesh( MeshLib::IO::readMeshFromFile(input_arg.getValue())); if (!org_mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Mesh read: {:d} nodes, {:d} elements.", org_mesh->getNumberOfNodes(), @@ -92,8 +84,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*new_mesh, output_arg.getValue()); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshEdit/swapNodeCoordinateAxes.cpp b/Applications/Utils/MeshEdit/swapNodeCoordinateAxes.cpp index dcc9558a7f1..0eaadc7013e 100644 --- a/Applications/Utils/MeshEdit/swapNodeCoordinateAxes.cpp +++ b/Applications/Utils/MeshEdit/swapNodeCoordinateAxes.cpp @@ -9,15 +9,12 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include "BaseLib/Logging.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -114,17 +111,12 @@ int main(int argc, char* argv[]) cmd.add(new_order_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); const std::string str_order = new_order_arg.getValue(); std::array new_order = {{}}; if (!parseNewOrder(str_order, new_order)) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -132,9 +124,6 @@ int main(int argc, char* argv[]) MeshLib::IO::readMeshFromFile(input_arg.getValue())); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -152,8 +141,5 @@ int main(int argc, char* argv[]) INFO("Save the new mesh into a file"); MeshLib::IO::writeMeshToFile(*mesh, output_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/AssignRasterDataToMesh.cpp b/Applications/Utils/MeshGeoTools/AssignRasterDataToMesh.cpp index ceada5695ec..338354d0d6e 100644 --- a/Applications/Utils/MeshGeoTools/AssignRasterDataToMesh.cpp +++ b/Applications/Utils/MeshGeoTools/AssignRasterDataToMesh.cpp @@ -13,10 +13,7 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Raster.h" #include "InfoLib/GitInfo.h" @@ -74,9 +71,7 @@ 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); bool const create_cell_array(set_cells_arg.isSet()); bool const create_node_array = @@ -91,9 +86,6 @@ int main(int argc, char* argv[]) if (mesh->getDimension() > 2) { ERR("Method can not be applied to 3D meshes."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -108,9 +100,6 @@ int main(int argc, char* argv[]) if (!assigned) { ERR("Error assigning raster data to scalar node array"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Created node array {:s}", array_name_arg.getValue()); @@ -124,9 +113,6 @@ int main(int argc, char* argv[]) if (!assigned) { ERR("Error assigning raster data to scalar cell array"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Created cell array {:s}", array_name_arg.getValue()); @@ -134,8 +120,5 @@ int main(int argc, char* argv[]) MeshLib::IO::VtuInterface vtu(mesh.get()); vtu.writeToFile(output_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/IntegrateBoreholesIntoMesh.cpp b/Applications/Utils/MeshGeoTools/IntegrateBoreholesIntoMesh.cpp index 6f0f5b701cd..9c88e921588 100644 --- a/Applications/Utils/MeshGeoTools/IntegrateBoreholesIntoMesh.cpp +++ b/Applications/Utils/MeshGeoTools/IntegrateBoreholesIntoMesh.cpp @@ -16,10 +16,7 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h" #include "InfoLib/GitInfo.h" @@ -122,9 +119,7 @@ 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); std::pair mat_limits(0, std::numeric_limits::max()); std::pair elevation_limits( @@ -134,9 +129,6 @@ int main(int argc, char* argv[]) { ERR("If minimum MaterialID is set, maximum ID must be set, too (and " "vice versa)."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (min_id_arg.isSet() && max_id_arg.isSet()) @@ -151,18 +143,12 @@ int main(int argc, char* argv[]) if (min_id_arg.isSet() && (mat_limits.first < 0 || mat_limits.second < 0)) { ERR("Specified MaterialIDs must have non-negative values."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (min_elevation_arg.isSet() != max_elevation_arg.isSet()) { ERR("If minimum elevation is set, maximum elevation must be set, too " "(and vice versa)."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (min_elevation_arg.isSet() && max_elevation_arg.isSet()) @@ -184,9 +170,6 @@ int main(int argc, char* argv[]) if (!xml_io.readFile(geo_name)) { ERR("Failed to read geometry file `{:s}'.", geo_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } std::vector const& points = @@ -197,17 +180,11 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("Failed to read input mesh file `{:s}'.", mesh_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (mesh->getDimension() != 3) { ERR("Method can only be applied to 3D meshes."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -216,9 +193,6 @@ int main(int argc, char* argv[]) if (mat_ids == nullptr) { ERR("Mesh is required to have MaterialIDs"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -256,8 +230,5 @@ int main(int argc, char* argv[]) true /* compute_element_neighbors */, props); MeshLib::IO::VtuInterface vtu(&result); vtu.writeToFile(output_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/Raster2Mesh.cpp b/Applications/Utils/MeshGeoTools/Raster2Mesh.cpp index c4905205da3..423ecc4d961 100644 --- a/Applications/Utils/MeshGeoTools/Raster2Mesh.cpp +++ b/Applications/Utils/MeshGeoTools/Raster2Mesh.cpp @@ -13,10 +13,7 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Raster.h" #include "InfoLib/GitInfo.h" @@ -72,9 +69,7 @@ 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); std::string const input_name = input_arg.getValue().c_str(); std::string const output_name = output_arg.getValue().c_str(); @@ -107,16 +102,10 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("Conversion failed."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } MeshLib::IO::VtuInterface vtu(mesh.get()); vtu.writeToFile(output_name); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp b/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp index 671cdaeeae9..f9193a588cd 100644 --- a/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp +++ b/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp @@ -16,16 +16,13 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - #include #include "Applications/FileIO/Gmsh/GMSHInterface.h" #include "Applications/FileIO/Gmsh/GmshReader.h" #include "BaseLib/FileTools.h" #include "BaseLib/IO/readStringListFromFile.h" +#include "BaseLib/MPI.h" #include "GeoLib/AABB.h" #include "GeoLib/AnalyticalGeometry.h" #include "GeoLib/GEOObjects.h" @@ -418,9 +415,7 @@ 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); std::string const input_name = input_arg.getValue(); std::string const output_name = output_arg.getValue(); @@ -439,9 +434,6 @@ int main(int argc, char* argv[]) if (layer_names.size() < 2) { ERR("At least two layers are required to extract a slice."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -453,9 +445,6 @@ int main(int argc, char* argv[]) { ERR("Less than two geometries could be created from layers. Aborting " "extraction..."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -475,9 +464,6 @@ int main(int argc, char* argv[]) if (mesh == nullptr) { ERR("Error generating mesh... (GMSH was unable to output mesh)"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } rotateMesh(*mesh, rot_mat, z_shift); @@ -485,9 +471,6 @@ int main(int argc, char* argv[]) if (new_mesh == nullptr) { ERR("Error generating mesh... (GMSH created line mesh)"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -506,8 +489,5 @@ int main(int argc, char* argv[]) } MeshLib::IO::VtuInterface vtu(revised_mesh.get()); vtu.writeToFile(output_name + ".vtu"); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/computeSurfaceNodeIDsInPolygonalRegion.cpp b/Applications/Utils/MeshGeoTools/computeSurfaceNodeIDsInPolygonalRegion.cpp index 5329c0b9ad9..4ca7478f6d4 100644 --- a/Applications/Utils/MeshGeoTools/computeSurfaceNodeIDsInPolygonalRegion.cpp +++ b/Applications/Utils/MeshGeoTools/computeSurfaceNodeIDsInPolygonalRegion.cpp @@ -12,10 +12,6 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include @@ -25,6 +21,7 @@ #include "Applications/FileIO/readGeometryFromFile.h" #include "BaseLib/Error.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/Polygon.h" #include "InfoLib/GitInfo.h" @@ -101,9 +98,7 @@ int main(int argc, char* argv[]) cmd.add(gmsh_path_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); std::unique_ptr mesh( MeshLib::IO::readMeshFromFile(mesh_in.getValue())); @@ -194,8 +189,5 @@ int main(int argc, char* argv[]) std::for_each(all_sfc_nodes.begin(), all_sfc_nodes.end(), std::default_delete()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/constructMeshesFromGeometry.cpp b/Applications/Utils/MeshGeoTools/constructMeshesFromGeometry.cpp index d57974e0476..9d99fc3e19d 100644 --- a/Applications/Utils/MeshGeoTools/constructMeshesFromGeometry.cpp +++ b/Applications/Utils/MeshGeoTools/constructMeshesFromGeometry.cpp @@ -10,6 +10,7 @@ #include +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h" #include "InfoLib/GitInfo.h" @@ -19,9 +20,6 @@ #include "MeshLib/IO/writeMeshToFile.h" #include "MeshLib/Mesh.h" -#ifdef USE_PETSC -#include -#endif std::unique_ptr readGeometry(std::string const& filename) { auto geo_objects = std::make_unique(); @@ -80,9 +78,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::unique_ptr mesh{ MeshLib::IO::readMeshFromFile(mesh_arg.getValue())}; @@ -101,9 +97,6 @@ int main(int argc, char* argv[]) if (!m_ptr) { ERR("Could not create a mesh for each given geometry."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } if (m_ptr->getNodes().empty()) @@ -117,8 +110,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*m_ptr, m_ptr->getName() + ".vtu"); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/createIntermediateRasters.cpp b/Applications/Utils/MeshGeoTools/createIntermediateRasters.cpp index 2a98a25c57d..75a7572a964 100644 --- a/Applications/Utils/MeshGeoTools/createIntermediateRasters.cpp +++ b/Applications/Utils/MeshGeoTools/createIntermediateRasters.cpp @@ -9,14 +9,11 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Raster.h" #include "InfoLib/GitInfo.h" @@ -50,9 +47,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::unique_ptr dem1( FileIO::AsciiRasterInterface::readRaster(input1_arg.getValue())); @@ -61,9 +56,6 @@ int main(int argc, char* argv[]) if (dem1 == nullptr || dem2 == nullptr) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 1; } @@ -99,9 +91,6 @@ int main(int argc, char* argv[]) if (errors_found) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 2; } @@ -120,9 +109,6 @@ int main(int argc, char* argv[]) if (it2 == dem2->end()) { ERR("Error: File 2 is shorter than File 1."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 3; } if (*it1 == h1.no_data || *it2 == h2.no_data) @@ -147,9 +133,6 @@ int main(int argc, char* argv[]) if (it2 != dem2->end()) { ERR("Error: File 1 is shorter than File 2."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 3; } @@ -164,8 +147,5 @@ int main(int argc, char* argv[]) r, basename + std::to_string(i) + ext); INFO("Layer {:d} written.", i + 1); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/geometryToGmshGeo.cpp b/Applications/Utils/MeshGeoTools/geometryToGmshGeo.cpp index 87777c243a7..87d735950a4 100644 --- a/Applications/Utils/MeshGeoTools/geometryToGmshGeo.cpp +++ b/Applications/Utils/MeshGeoTools/geometryToGmshGeo.cpp @@ -12,11 +12,8 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif - #include "Applications/FileIO/Gmsh/GMSHInterface.h" +#include "BaseLib/MPI.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h" #include "InfoLib/GitInfo.h" @@ -70,9 +67,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); GeoLib::GEOObjects geo_objects; for (auto const& geometry_name : geo_input_arg.getValue()) @@ -82,9 +77,6 @@ int main(int argc, char* argv[]) { if (!xml.readFile(geometry_name)) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -92,9 +84,6 @@ int main(int argc, char* argv[]) { ERR("Failed to read file '{:s}'.", geometry_name); ERR("{:s}", err.what()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } INFO("Successfully read file '{:s}'.", geometry_name); @@ -155,13 +144,7 @@ int main(int argc, char* argv[]) "for better analysis of the problem."); } ERR("An error has occurred - programme will be terminated."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/identifySubdomains.cpp b/Applications/Utils/MeshGeoTools/identifySubdomains.cpp index 46e44c801d5..fcfba24983f 100644 --- a/Applications/Utils/MeshGeoTools/identifySubdomains.cpp +++ b/Applications/Utils/MeshGeoTools/identifySubdomains.cpp @@ -10,10 +10,7 @@ #include -#ifdef USE_PETSC -#include -#endif - +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "InfoLib/GitInfo.h" #include "MeshGeoToolsLib/IdentifySubdomainMesh.h" @@ -98,9 +95,7 @@ int main(int argc, char* argv[]) cmd.add(subdomain_meshes_filenames_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); // // The bulk mesh. @@ -165,9 +160,6 @@ int main(int argc, char* argv[]) } INFO("writing time: {:g} s", writing_time.elapsed()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif INFO("Entire run time: {:g} s", run_time.elapsed()); return EXIT_SUCCESS; } diff --git a/Applications/Utils/ModelPreparation/ComputeNodeAreasFromSurfaceMesh.cpp b/Applications/Utils/ModelPreparation/ComputeNodeAreasFromSurfaceMesh.cpp index c29a8bc4bbf..9bad7b3970a 100644 --- a/Applications/Utils/ModelPreparation/ComputeNodeAreasFromSurfaceMesh.cpp +++ b/Applications/Utils/ModelPreparation/ComputeNodeAreasFromSurfaceMesh.cpp @@ -11,10 +11,6 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include @@ -23,6 +19,7 @@ #include "BaseLib/Error.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/Mesh.h" @@ -90,9 +87,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::unique_ptr surface_mesh( MeshLib::IO::readMeshFromFile(mesh_in.getValue())); @@ -112,9 +107,6 @@ int main(int argc, char* argv[]) if (!orig_node_ids) { ERR("Fatal error: could not create property."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } orig_node_ids->resize(surface_mesh->getNumberOfNodes()); @@ -149,8 +141,5 @@ int main(int argc, char* argv[]) writeToFile(id_and_area_fname, csv_fname, ids_and_areas, surface_mesh->getNodes()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/ModelPreparation/PartitionMesh/BinaryToPVTU.cpp b/Applications/Utils/ModelPreparation/PartitionMesh/BinaryToPVTU.cpp index fd7b3baca9c..81ce322f9ec 100644 --- a/Applications/Utils/ModelPreparation/PartitionMesh/BinaryToPVTU.cpp +++ b/Applications/Utils/ModelPreparation/PartitionMesh/BinaryToPVTU.cpp @@ -10,7 +10,6 @@ */ -#include #include #include #include @@ -18,6 +17,7 @@ #include "BaseLib/CPUTime.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" @@ -72,9 +72,7 @@ int main(int argc, char* argv[]) OGS_FATAL("spdlog logger error occurred."); }); - // init MPI - MPI_Init(&argc, &argv); - + BaseLib::MPI::Setup mpi_setup(argc, argv); // start the timer BaseLib::RunTime run_timer; run_timer.start(); @@ -111,8 +109,5 @@ int main(int argc, char* argv[]) INFO("Total runtime: {:g} s.", run_timer.elapsed()); INFO("Total CPU time: {:g} s.", CPU_timer.elapsed()); - - MPI_Finalize(); - return EXIT_SUCCESS; } diff --git a/Applications/Utils/ModelPreparation/PartitionMesh/PartitionMesh.cpp b/Applications/Utils/ModelPreparation/PartitionMesh/PartitionMesh.cpp index daa2aa43cd3..3e531631385 100644 --- a/Applications/Utils/ModelPreparation/PartitionMesh/PartitionMesh.cpp +++ b/Applications/Utils/ModelPreparation/PartitionMesh/PartitionMesh.cpp @@ -15,12 +15,9 @@ #include #include -#ifdef USE_PETSC -#include -#endif - #include "BaseLib/CPUTime.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/RunTime.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -102,9 +99,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); BaseLib::setConsoleLogLevel(log_level_arg.getValue()); spdlog::set_pattern("%^%l:%$ %v"); @@ -144,9 +139,6 @@ int main(int argc, char* argv[]) INFO("Total runtime: {:g} s.", run_timer.elapsed()); INFO("Total CPU time: {:g} s.", CPU_timer.elapsed()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } @@ -192,9 +184,6 @@ int main(int argc, char* argv[]) { INFO("Failed in system calling."); INFO("Return value of system call {:d} ", status); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -244,8 +233,5 @@ int main(int argc, char* argv[]) INFO("Total runtime: {:g} s.", run_timer.elapsed()); INFO("Total CPU time: {:g} s.", CPU_timer.elapsed()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/ModelPreparation/convertVtkDataArrayToVtkDataArray.cpp b/Applications/Utils/ModelPreparation/convertVtkDataArrayToVtkDataArray.cpp index dbb156149c0..0fd23a6fe3c 100644 --- a/Applications/Utils/ModelPreparation/convertVtkDataArrayToVtkDataArray.cpp +++ b/Applications/Utils/ModelPreparation/convertVtkDataArrayToVtkDataArray.cpp @@ -11,15 +11,12 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -105,18 +102,13 @@ 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::unique_ptr mesh( MeshLib::IO::readMeshFromFile(mesh_arg.getValue())); if (!mesh) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } @@ -163,16 +155,10 @@ int main(int argc, char* argv[]) if (!success) { ERR("{:s}", err_msg); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/ModelPreparation/createNeumannBc.cpp b/Applications/Utils/ModelPreparation/createNeumannBc.cpp index 7f108478b1f..b69f69c4861 100644 --- a/Applications/Utils/ModelPreparation/createNeumannBc.cpp +++ b/Applications/Utils/ModelPreparation/createNeumannBc.cpp @@ -10,12 +10,9 @@ #include -#ifdef USE_PETSC -#include -#endif - #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/Elements/Element.h" #include "MeshLib/IO/readMeshFromFile.h" @@ -130,9 +127,7 @@ int main(int argc, char* argv[]) cmd.add(result_file); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); // read surface mesh std::unique_ptr surface_mesh( @@ -155,9 +150,6 @@ int main(int argc, char* argv[]) }(); if (!node_id_pv) { -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } @@ -192,8 +184,5 @@ int main(int argc, char* argv[]) result_out << p.first << " " << p.second << "\n"; } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/ModelPreparation/scaleProperty.cpp b/Applications/Utils/ModelPreparation/scaleProperty.cpp index 03eb59846b7..562ee1d9212 100644 --- a/Applications/Utils/ModelPreparation/scaleProperty.cpp +++ b/Applications/Utils/ModelPreparation/scaleProperty.cpp @@ -11,15 +11,12 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -72,9 +69,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::unique_ptr mesh( MeshLib::IO::readMeshFromFile(mesh_arg.getValue())); @@ -97,8 +92,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/PostProcessing/Raster2PointCloud.cpp b/Applications/Utils/PostProcessing/Raster2PointCloud.cpp index 2b22e4fde07..44fa7de536f 100644 --- a/Applications/Utils/PostProcessing/Raster2PointCloud.cpp +++ b/Applications/Utils/PostProcessing/Raster2PointCloud.cpp @@ -13,9 +13,6 @@ // ThirdParty #include -#ifdef USE_PETSC -#include -#endif #include #include #include @@ -23,6 +20,7 @@ #include "Applications/DataExplorer/VtkVis/VtkGeoImageSource.h" #include "Applications/DataExplorer/VtkVis/VtkImageDataToPointCloudFilter.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "InfoLib/GitInfo.h" @@ -80,9 +78,7 @@ 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); std::string const input_name = input_arg.getValue().c_str(); std::string const output_name = output_arg.getValue().c_str(); @@ -161,8 +157,5 @@ int main(int argc, char* argv[]) } } std::cout << "done." << std::endl; -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/PostProcessing/postLIE.cpp b/Applications/Utils/PostProcessing/postLIE.cpp index 86199ac4055..dac283371f5 100644 --- a/Applications/Utils/PostProcessing/postLIE.cpp +++ b/Applications/Utils/PostProcessing/postLIE.cpp @@ -9,10 +9,6 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include @@ -20,6 +16,7 @@ #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "InfoLib/GitInfo.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/writeMeshToFile.h" @@ -151,9 +148,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); auto const in_file_ext = BaseLib::getFileExtension(arg_in_file.getValue()); if (in_file_ext == ".pvd") @@ -170,8 +165,5 @@ int main(int argc, char* argv[]) OGS_FATAL("The given file type ({:s}) is not supported.", in_file_ext); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/SWMMConverter/SWMMConverter.cpp b/Applications/Utils/SWMMConverter/SWMMConverter.cpp index 7db44c2b4af..be95e7c6128 100644 --- a/Applications/Utils/SWMMConverter/SWMMConverter.cpp +++ b/Applications/Utils/SWMMConverter/SWMMConverter.cpp @@ -9,12 +9,9 @@ #include -#ifdef USE_PETSC -#include -#endif - #include "Applications/FileIO/SWMM/SWMMInterface.h" #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/StringTools.h" #include "GeoLib/GEOObjects.h" #include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h" @@ -199,18 +196,13 @@ int main(int argc, char* argv[]) cmd.add(add_system_arg); cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); if (!(geo_output_arg.isSet() || mesh_output_arg.isSet() || csv_output_arg.isSet())) { ERR("No output format given. Please specify OGS geometry or mesh " "output file."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } @@ -219,9 +211,6 @@ int main(int argc, char* argv[]) { ERR("Please specify csv output file for exporting subcatchment or " "system parameters."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return -1; } @@ -240,8 +229,5 @@ int main(int argc, char* argv[]) add_subcatchments_arg.getValue(), add_system_arg.getValue()); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return 0; } diff --git a/Applications/Utils/SimpleMeshCreation/createMeshElemPropertiesFromASCRaster.cpp b/Applications/Utils/SimpleMeshCreation/createMeshElemPropertiesFromASCRaster.cpp index 9039a44a134..00b7dfedf5a 100644 --- a/Applications/Utils/SimpleMeshCreation/createMeshElemPropertiesFromASCRaster.cpp +++ b/Applications/Utils/SimpleMeshCreation/createMeshElemPropertiesFromASCRaster.cpp @@ -12,16 +12,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include "BaseLib/FileTools.h" +#include "BaseLib/MPI.h" #include "BaseLib/quicksort.h" #include "GeoLib/IO/AsciiRasterInterface.h" #include "GeoLib/Raster.h" @@ -94,9 +91,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 std::unique_ptr dest_mesh( @@ -138,8 +133,5 @@ int main(int argc, char* argv[]) MeshLib::IO::writeMeshToFile(*dest_mesh, out_mesh_arg.getValue()); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; } diff --git a/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp b/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp index 7e3062698a3..0392559aae0 100644 --- a/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp +++ b/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp @@ -9,16 +9,13 @@ #include -#ifdef USE_PETSC -#include -#endif - #include #include #include #include #include "BaseLib/Error.h" +#include "BaseLib/MPI.h" #include "BaseLib/Subdivision.h" #include "BaseLib/TCLAPCustomOutput.h" #include "InfoLib/GitInfo.h" @@ -161,9 +158,7 @@ int main(int argc, char* argv[]) // parse arguments cmd.parse(argc, argv); -#ifdef USE_PETSC - MPI_Init(&argc, &argv); -#endif + BaseLib::MPI::Setup mpi_setup(argc, argv); const std::string eleTypeName(eleTypeArg.getValue()); const MeshLib::MeshElemType eleType = MeshLib::String2MeshElemType(eleTypeName); @@ -194,9 +189,6 @@ int main(int argc, char* argv[]) if (!isLengthSet) { ERR("Missing input: Length information is not provided at all."); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } for (unsigned i = 0; i < 3; i++) @@ -206,9 +198,6 @@ int main(int argc, char* argv[]) ERR("Missing input: Length for dimension [{:d}] is required but " "missing.", i); -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_FAILURE; } } @@ -304,8 +293,5 @@ int main(int argc, char* argv[]) *(mesh.get()), std::filesystem::path(mesh_out.getValue())); } -#ifdef USE_PETSC - MPI_Finalize(); -#endif return EXIT_SUCCESS; }