From 10745936648b3e16244dc076e981a6509590ad16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Sch=C3=B6berl?= Date: Mon, 11 Feb 2019 20:01:07 +0100 Subject: [PATCH] mpi_wrapper in core --- libsrc/core/CMakeLists.txt | 2 +- libsrc/core/mpi_wrapper.hpp | 108 ++++++++++++++++++++++++++++++++++++ libsrc/core/ngcore.hpp | 1 + 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 libsrc/core/mpi_wrapper.hpp diff --git a/libsrc/core/CMakeLists.txt b/libsrc/core/CMakeLists.txt index 46a2230b..4f7dfdc7 100644 --- a/libsrc/core/CMakeLists.txt +++ b/libsrc/core/CMakeLists.txt @@ -33,7 +33,7 @@ if(USE_PYTHON) endif(USE_PYTHON) install(FILES ngcore.hpp archive.hpp type_traits.hpp version.hpp ngcore_api.hpp logging.hpp - exception.hpp symboltable.hpp paje_trace.hpp utils.hpp profiler.hpp + exception.hpp symboltable.hpp paje_trace.hpp utils.hpp profiler.hpp mpi_wrapper.hpp DESTINATION ${NG_INSTALL_DIR_INCLUDE}/core COMPONENT netgen_devel) if(ENABLE_CPP_CORE_GUIDELINES_CHECK) diff --git a/libsrc/core/mpi_wrapper.hpp b/libsrc/core/mpi_wrapper.hpp new file mode 100644 index 00000000..40b60b5c --- /dev/null +++ b/libsrc/core/mpi_wrapper.hpp @@ -0,0 +1,108 @@ +#ifndef NGCORE_MPIWRAPPER_HPP +#define NGCORE_MPIWRAPPER_HPP + +#ifdef PARALLEL +#define OMPI_SKIP_MPICXX +#include +#endif + + +namespace ngcore +{ + +#ifdef PARALLEL + + template struct MPI_typetrait { }; + + template <> struct MPI_typetrait { + static MPI_Datatype MPIType () { return MPI_INT; } }; + + template <> struct MPI_typetrait { + static MPI_Datatype MPIType () { return MPI_SHORT; } }; + + template <> struct MPI_typetrait { + static MPI_Datatype MPIType () { return MPI_CHAR; } }; + + template <> struct MPI_typetrait { + static MPI_Datatype MPIType () { return MPI_UNIT64_T; } }; + + template <> struct MPI_typetrait { + static MPI_Datatype MPIType () { return MPI_DOUBLE; } }; + + template <> struct MPI_typetrait { + static MPI_Datatype MPIType () { return MPI_C_BOOL; } }; + + + template ::MPIType())> + inline MPI_Datatype GetMPIType () { + return MPI_typetrait::MPIType(); + } + + + class NgMPI_Comm + { + MPI_Comm comm; + int * refcount; + public: + NgMPI_Comm (MPI_Comm _comm, bool owns = false) + : comm(_comm) + { + if (!owns) + refcount = nullptr; + else + refcount = new int{1}; + } + + NgMPI_Comm (const NgMPI_Comm & c) + : comm(c.comm), refcount(c.refcount) + { + if (refcount) (*refcount)++; + } + + NgMPI_Comm (NgMPI_Comm && c) + : comm(c.comm), refcount(c.refcount) + { + c.refcount = nullptr; + } + + ~NgMPI_Comm() + { + if (refcount) + if (--(*refcount) == 0) + MPI_Comm_free(&comm); + } + + operator MPI_Comm() const { return comm; } + + auto Rank() const { int r; MPI_Comm_rank(comm, &r); return r; } + auto Size() const { int s; MPI_Comm_size(comm, &s); return s; } + }; + + +#else + + class NgMPI_Comm + { + + public: + NgMPI_Comm (int _comm, bool owns = false) + { ; } + + size_t Rank() const { return 0; } + size_t Size() const { return 1; } + }; + +#endif + + + + + + + + + +} + +#endif + diff --git a/libsrc/core/ngcore.hpp b/libsrc/core/ngcore.hpp index d73a3ed9..8cc937c0 100644 --- a/libsrc/core/ngcore.hpp +++ b/libsrc/core/ngcore.hpp @@ -7,5 +7,6 @@ #include "profiler.hpp" #include "symboltable.hpp" #include "version.hpp" +#include "mpi_wrapper.hpp" #endif // NETGEN_CORE_NGCORE_HPP