Get rid of mpi4py compile-time dependency, disable MPI wrapper by default

This commit is contained in:
Matthias Hochsteger 2024-05-28 10:55:58 +02:00
parent 246dfd734d
commit 1e7624c7f5
7 changed files with 44 additions and 55 deletions

View File

@ -16,8 +16,7 @@ option( USE_GUI "build with GUI" ON )
option( USE_PYTHON "build with python interface" ON ) option( USE_PYTHON "build with python interface" ON )
cmake_dependent_option( PREFER_SYSTEM_PYBIND11 "Use system wide PyBind11" OFF "USE_PYTHON" OFF) cmake_dependent_option( PREFER_SYSTEM_PYBIND11 "Use system wide PyBind11" OFF "USE_PYTHON" OFF)
option( USE_MPI "enable mpi parallelization" OFF ) option( USE_MPI "enable mpi parallelization" OFF )
option( USE_MPI4PY "enable mpi4py interface" ON ) option( USE_MPI_WRAPPER "enable mpi wrapper (run-time dispatch of MPI library calls)" OFF )
option( USE_MPI_WRAPPER "enable mpi wrapper (run-time dispatch of MPI library calls)" ON )
option( USE_OCC "build with OpenCascade geometry kernel interface" ON) option( USE_OCC "build with OpenCascade geometry kernel interface" ON)
option( USE_STLGEOM "build with STL geometry support" ON) option( USE_STLGEOM "build with STL geometry support" ON)
option( USE_CSG "build with CSG kernel" ON) option( USE_CSG "build with CSG kernel" ON)
@ -323,6 +322,7 @@ if (USE_PYTHON)
add_subdirectory(external_dependencies/pybind11) add_subdirectory(external_dependencies/pybind11)
endif() endif()
target_compile_definitions(netgen_python INTERFACE NG_PYTHON NETGEN_PYTHON)
target_include_directories(netgen_python INTERFACE ${pybind11_INCLUDE_DIR} ${Python3_INCLUDE_DIRS}) target_include_directories(netgen_python INTERFACE ${pybind11_INCLUDE_DIR} ${Python3_INCLUDE_DIRS})
target_include_directories(nglib PRIVATE ${pybind11_INCLUDE_DIR} ${Python3_INCLUDE_DIRS}) target_include_directories(nglib PRIVATE ${pybind11_INCLUDE_DIR} ${Python3_INCLUDE_DIRS})
if(Python3_LIBRARIES AND (WIN32 OR NOT BUILD_FOR_CONDA)) if(Python3_LIBRARIES AND (WIN32 OR NOT BUILD_FOR_CONDA))
@ -345,14 +345,6 @@ if (USE_MPI)
target_include_directories(netgen_metis INTERFACE ${METIS_INCLUDE_DIR}) target_include_directories(netgen_metis INTERFACE ${METIS_INCLUDE_DIR})
target_link_libraries(netgen_metis INTERFACE ${METIS_LIBRARY} ) target_link_libraries(netgen_metis INTERFACE ${METIS_LIBRARY} )
target_compile_definitions(netgen_metis INTERFACE METIS ) target_compile_definitions(netgen_metis INTERFACE METIS )
if(USE_MPI4PY AND USE_PYTHON)
execute_process(COMMAND ${Python3_EXECUTABLE} -c "import mpi4py;print(mpi4py.get_include())" OUTPUT_VARIABLE mpi4py_path OUTPUT_STRIP_TRAILING_WHITESPACE)
find_path(MPI4PY_INCLUDE_DIR mpi4py.h HINTS ${mpi4py_path}/mpi4py NO_DEFAULT_PATH REQUIRED)
target_include_directories(netgen_python INTERFACE ${MPI4PY_INCLUDE_DIR})
target_compile_definitions(netgen_python INTERFACE NG_MPI4PY )
message(STATUS "Found mpi4py: ${MPI4PY_INCLUDE_DIR}")
endif(USE_MPI4PY AND USE_PYTHON)
endif (USE_MPI) endif (USE_MPI)
####################################################################### #######################################################################

View File

@ -93,7 +93,7 @@ install(FILES ngcore.hpp archive.hpp type_traits.hpp version.hpp ngcore_api.hpp
xbool.hpp signal.hpp bitarray.hpp table.hpp hashtable.hpp ranges.hpp ngstream.hpp xbool.hpp signal.hpp bitarray.hpp table.hpp hashtable.hpp ranges.hpp ngstream.hpp
simd.hpp simd_avx.hpp simd_avx512.hpp simd_generic.hpp simd_sse.hpp simd_arm64.hpp simd.hpp simd_avx.hpp simd_avx512.hpp simd_generic.hpp simd_sse.hpp simd_arm64.hpp
register_archive.hpp autodiff.hpp autodiffdiff.hpp register_archive.hpp autodiff.hpp autodiffdiff.hpp
ng_mpi.hpp ng_mpi_generated_declarations.hpp ng_mpi_native.hpp ng_mpi.hpp ng_mpi_generated_declarations.hpp ng_mpi_native.hpp mpi4py_pycapi.h
DESTINATION ${NG_INSTALL_DIR_INCLUDE}/core COMPONENT netgen_devel) DESTINATION ${NG_INSTALL_DIR_INCLUDE}/core COMPONENT netgen_devel)
if(ENABLE_CPP_CORE_GUIDELINES_CHECK) if(ENABLE_CPP_CORE_GUIDELINES_CHECK)
@ -166,6 +166,7 @@ if(USE_MPI)
endif() endif()
else() else()
target_link_libraries(ngcore PUBLIC ${MPI_C_LIBRARIES}) target_link_libraries(ngcore PUBLIC ${MPI_C_LIBRARIES})
target_include_directories(ngcore PUBLIC ${MPI_C_INCLUDE_PATH})
endif(USE_MPI_WRAPPER) endif(USE_MPI_WRAPPER)
endif(USE_MPI) endif(USE_MPI)

View File

@ -6,20 +6,18 @@
#include <type_traits> #include <type_traits>
#include "array.hpp"
#include "ngcore_api.hpp" #include "ngcore_api.hpp"
#include "pybind11/pytypes.h" #include "pybind11/pytypes.h"
#if defined(NG_PYTHON) && defined(NG_MPI4PY) #ifdef NG_PYTHON
#include "python_ngcore.hpp"
#endif
#define MPI4PY_LIMITED_API 1 #define MPI4PY_LIMITED_API 1
#define MPI4PY_LIMITED_API_SKIP_MESSAGE 1 #define MPI4PY_LIMITED_API_SKIP_MESSAGE 1
#define MPI4PY_LIMITED_API_SKIP_SESSION 1 #define MPI4PY_LIMITED_API_SKIP_SESSION 1
#include "mpi4py_pycapi.h" // mpi4py < 4.0.0 #include "mpi4py_pycapi.h" // mpi4py < 4.0.0
#include <mpi4py.h>
#include "python_ngcore.hpp"
namespace py = pybind11;
#endif
#ifdef MSMPI_VER #ifdef MSMPI_VER
int MPI_Comm_create_group(MPI_Comm arg0, MPI_Group arg1, int arg2, int MPI_Comm_create_group(MPI_Comm arg0, MPI_Group arg1, int arg2,
@ -160,10 +158,10 @@ NGCORE_API_EXPORT void ng_init_mpi();
static bool imported_mpi4py = false; static bool imported_mpi4py = false;
void ng_init_mpi() { void ng_init_mpi() {
#if defined(NG_PYTHON) && defined(NG_MPI4PY) #ifdef NG_PYTHON
NG_MPI_CommFromMPI4Py = [](py::handle src, NG_MPI_Comm& dst) -> bool { NG_MPI_CommFromMPI4Py = [](py::handle src, NG_MPI_Comm& dst) -> bool {
if (!imported_mpi4py) { if (!imported_mpi4py) {
import_mpi4py(); import_mpi4py__MPI();
imported_mpi4py = true; imported_mpi4py = true;
} }
PyObject* py_src = src.ptr(); PyObject* py_src = src.ptr();
@ -176,12 +174,12 @@ void ng_init_mpi() {
}; };
NG_MPI_CommToMPI4Py = [](NG_MPI_Comm src) -> py::handle { NG_MPI_CommToMPI4Py = [](NG_MPI_Comm src) -> py::handle {
if (!imported_mpi4py) { if (!imported_mpi4py) {
import_mpi4py(); import_mpi4py__MPI();
imported_mpi4py = true; imported_mpi4py = true;
} }
return py::handle(PyMPIComm_New(ng2mpi(src))); return py::handle(PyMPIComm_New(ng2mpi(src)));
}; };
#endif #endif // NG_PYTHON
#include "ng_mpi_generated_init.hpp" #include "ng_mpi_generated_init.hpp"
} }

View File

@ -9,17 +9,8 @@
#include "ngcore_api.hpp" #include "ngcore_api.hpp"
#if defined(NG_PYTHON) && defined(NG_MPI4PY)
#include <pybind11/pybind11.h>
namespace py = pybind11;
#endif
#ifndef NG_MPI_WRAPPER #ifndef NG_MPI_WRAPPER
#include <mpi.h> #include <mpi.h>
#if defined(NG_PYTHON) && defined(NG_MPI4PY)
#include <mpi4py.h>
#endif
#endif // NG_MPI_WRAPPER #endif // NG_MPI_WRAPPER
namespace ngcore { namespace ngcore {
@ -83,23 +74,18 @@ struct NG_MPI_Aint {
NG_MPI_Aint() = default; NG_MPI_Aint() = default;
}; };
#else #else // NG_MPI_WRAPPER
using NG_MPI_Status = MPI_Status;
using NG_MPI_Comm = MPI_Comm; using NG_MPI_Comm = MPI_Comm;
using NG_MPI_Status = MPI_Status;
using NG_MPI_Datatype = MPI_Datatype; using NG_MPI_Datatype = MPI_Datatype;
using NG_MPI_Request = MPI_Request; using NG_MPI_Request = MPI_Request;
using NG_MPI_Op = MPI_Op; using NG_MPI_Op = MPI_Op;
using NG_MPI_Group = MPI_Group; using NG_MPI_Group = MPI_Group;
using NG_MPI_Aint = MPI_Aint; using NG_MPI_Aint = MPI_Aint;
#endif #endif // NG_MPI_WRAPPER
#include "ng_mpi_generated_declarations.hpp" #include "ng_mpi_generated_declarations.hpp"
#if defined(NG_PYTHON) && defined(NG_MPI4PY)
NGCORE_API extern bool (*NG_MPI_CommFromMPI4Py)(py::handle, NG_MPI_Comm &);
NGCORE_API extern py::handle (*NG_MPI_CommToMPI4Py)(NG_MPI_Comm);
#endif
} // namespace ngcore } // namespace ngcore
#endif // PARALLEL #endif // PARALLEL

View File

@ -13,6 +13,14 @@ using std::cerr;
using std::cout; using std::cout;
using std::endl; using std::endl;
#ifndef NG_MPI_WRAPPER
#include <mpi.h>
#define MPI4PY_LIMITED_API 1
#define MPI4PY_LIMITED_API_SKIP_MESSAGE 1
#define MPI4PY_LIMITED_API_SKIP_SESSION 1
#include "mpi4py_pycapi.h" // mpi4py < 4.0.0
#endif // NG_MPI_WRAPPER
namespace ngcore { namespace ngcore {
#ifdef NG_MPI_WRAPPER #ifdef NG_MPI_WRAPPER
@ -28,9 +36,7 @@ struct MPIFinalizer {
} }
} mpi_finalizer; } mpi_finalizer;
bool MPI_Loaded() { bool MPI_Loaded() { return ng_mpi_lib != nullptr; }
return ng_mpi_lib != nullptr;
}
void InitMPI(std::optional<std::filesystem::path> mpi_lib_path) { void InitMPI(std::optional<std::filesystem::path> mpi_lib_path) {
if (ng_mpi_lib) return; if (ng_mpi_lib) return;
@ -128,7 +134,7 @@ static std::runtime_error no_mpi() {
return std::runtime_error("MPI not enabled"); return std::runtime_error("MPI not enabled");
} }
#if defined(NG_PYTHON) && defined(NG_MPI4PY) #ifdef NG_PYTHON
decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py = decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py =
[](py::handle py_obj, NG_MPI_Comm &ng_comm) -> bool { [](py::handle py_obj, NG_MPI_Comm &ng_comm) -> bool {
// If this gets called, it means that we want to convert an mpi4py // If this gets called, it means that we want to convert an mpi4py
@ -152,17 +158,17 @@ decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py =
}; };
decltype(NG_MPI_CommToMPI4Py) NG_MPI_CommToMPI4Py = decltype(NG_MPI_CommToMPI4Py) NG_MPI_CommToMPI4Py =
[](NG_MPI_Comm) -> py::handle { throw no_mpi(); }; [](NG_MPI_Comm) -> py::handle { throw no_mpi(); };
#endif #endif // NG_PYTHON
#include "ng_mpi_generated_dummy_init.hpp" #include "ng_mpi_generated_dummy_init.hpp"
#else // NG_MPI_WRAPPER #else // NG_MPI_WRAPPER
static bool imported_mpi4py = false; static bool imported_mpi4py = false;
#if defined(NG_PYTHON) && defined(NG_MPI4PY) #ifdef NG_PYTHON
decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py = decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py =
[](py::handle src, NG_MPI_Comm &dst) -> bool { [](py::handle src, NG_MPI_Comm &dst) -> bool {
if (!imported_mpi4py) { if (!imported_mpi4py) {
import_mpi4py(); import_mpi4py__MPI();
imported_mpi4py = true; imported_mpi4py = true;
} }
PyObject *py_src = src.ptr(); PyObject *py_src = src.ptr();
@ -177,19 +183,19 @@ decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py =
decltype(NG_MPI_CommToMPI4Py) NG_MPI_CommToMPI4Py = decltype(NG_MPI_CommToMPI4Py) NG_MPI_CommToMPI4Py =
[](NG_MPI_Comm src) -> py::handle { [](NG_MPI_Comm src) -> py::handle {
if (!imported_mpi4py) { if (!imported_mpi4py) {
import_mpi4py(); import_mpi4py__MPI();
imported_mpi4py = true; imported_mpi4py = true;
} }
return py::handle(PyMPIComm_New(src)); return py::handle(PyMPIComm_New(src));
}; };
#endif #endif // NG_PYTHON
bool MPI_Loaded() { return true; }
void InitMPI(std::optional<std::filesystem::path>) {} void InitMPI(std::optional<std::filesystem::path>) {}
#endif // NG_MPI_WRAPPER #endif // NG_MPI_WRAPPER
} // namespace ngcore } // namespace ngcore
#endif // PARALLEL #endif // PARALLEL

View File

@ -13,13 +13,17 @@
#include "archive.hpp" #include "archive.hpp"
#include "flags.hpp" #include "flags.hpp"
#include "ngcore_api.hpp" #include "ngcore_api.hpp"
#include "profiler.hpp"
#include "ng_mpi.hpp" #include "ng_mpi.hpp"
namespace py = pybind11; namespace py = pybind11;
namespace ngcore namespace ngcore
{ {
#ifdef PARALLEL
NGCORE_API extern bool (*NG_MPI_CommFromMPI4Py)(py::handle, NG_MPI_Comm &);
NGCORE_API extern py::handle (*NG_MPI_CommToMPI4Py)(NG_MPI_Comm);
#endif // PARALLEL
namespace detail namespace detail
{ {
template<typename T> template<typename T>
@ -34,15 +38,15 @@ namespace ngcore
}; };
} // namespace detail } // namespace detail
#ifdef PARALLEL
struct mpi4py_comm { struct mpi4py_comm {
mpi4py_comm() = default; mpi4py_comm() = default;
#ifdef PARALLEL
mpi4py_comm(NG_MPI_Comm value) : value(value) {} mpi4py_comm(NG_MPI_Comm value) : value(value) {}
operator NG_MPI_Comm () { return value; } operator NG_MPI_Comm () { return value; }
NG_MPI_Comm value; NG_MPI_Comm value;
#endif // PARALLEL
}; };
#endif // PARALLEL
} // namespace ngcore } // namespace ngcore
@ -51,7 +55,7 @@ namespace ngcore
namespace pybind11 { namespace pybind11 {
namespace detail { namespace detail {
#ifdef NG_MPI4PY #ifdef PARALLEL
template <> struct type_caster<ngcore::mpi4py_comm> { template <> struct type_caster<ngcore::mpi4py_comm> {
public: public:
PYBIND11_TYPE_CASTER(ngcore::mpi4py_comm, _("mpi4py_comm")); PYBIND11_TYPE_CASTER(ngcore::mpi4py_comm, _("mpi4py_comm"));
@ -70,7 +74,7 @@ template <> struct type_caster<ngcore::mpi4py_comm> {
return ngcore::NG_MPI_CommToMPI4Py(src.value); return ngcore::NG_MPI_CommToMPI4Py(src.value);
} }
}; };
#endif // NG_MPI4PY #endif // PARALLEL
template <typename Type, typename Value> struct ngcore_list_caster { template <typename Type, typename Value> struct ngcore_list_caster {
using value_conv = make_caster<Value>; using value_conv = make_caster<Value>;

View File

@ -371,5 +371,7 @@ threads : int
; ;
#ifdef PARALLEL
py::implicitly_convertible<mpi4py_comm, NgMPI_Comm>(); py::implicitly_convertible<mpi4py_comm, NgMPI_Comm>();
#endif // PARALLEL
} }