From e287ea4af5795d6846d360d4107d55d5964b6bf9 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Thu, 23 May 2024 10:37:26 +0200 Subject: [PATCH 1/9] Add newer OCC urls (currently commented out) --- cmake/SuperBuild.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmake/SuperBuild.cmake b/cmake/SuperBuild.cmake index 8b895cc7..5c10fc15 100644 --- a/cmake/SuperBuild.cmake +++ b/cmake/SuperBuild.cmake @@ -91,8 +91,10 @@ if(BUILD_OCC) ExternalProject_Add(project_occ URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_6_3.zip URL_MD5 2426e373903faabbd4f96a01a934b66d - # URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_8_0.zip - # URL_MD5 f4432df8e42cb6178ea09a7448427f6c + # URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_7_2.zip + # URL_MD5 533eb4f18af0f77ae321b158caeaee79 + # URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_8_1.zip + # URL_MD5 bf62952a03696dab9e4272aa8efacb1a DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external_dependencies ${SUBPROJECT_ARGS} CMAKE_ARGS From eaa797d7f658743150596959e15d2b3e9940a2d6 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Thu, 23 May 2024 10:37:42 +0200 Subject: [PATCH 2/9] Wrap MPI_Comm_c2f --- libsrc/core/generate_mpi_sources.py | 2 ++ libsrc/core/ng_mpi_generated_declarations.hpp | 4 ++++ libsrc/core/ng_mpi_generated_dummy_init.hpp | 2 ++ libsrc/core/ng_mpi_generated_init.hpp | 2 ++ 4 files changed, 10 insertions(+) diff --git a/libsrc/core/generate_mpi_sources.py b/libsrc/core/generate_mpi_sources.py index 1a491065..8c1e4db3 100644 --- a/libsrc/core/generate_mpi_sources.py +++ b/libsrc/core/generate_mpi_sources.py @@ -40,9 +40,11 @@ functions = [ ("int", "MPI_Wait", "MPI_Request*", "MPI_Status*"), ("int", "MPI_Waitall", "int", "MPI_Request*:0", "MPI_Status*"), ("int", "MPI_Waitany", "int", "MPI_Request*:0", "int*", "MPI_Status*"), + ("int", "MPI_Comm_c2f", "MPI_Comm"), ] constants = [ + ("MPI_Comm", "MPI_COMM_NULL"), ("MPI_Comm", "MPI_COMM_WORLD"), ("MPI_Datatype", "MPI_CHAR"), ("MPI_Datatype", "MPI_CXX_DOUBLE_COMPLEX"), diff --git a/libsrc/core/ng_mpi_generated_declarations.hpp b/libsrc/core/ng_mpi_generated_declarations.hpp index d6a9e33f..870ed34f 100644 --- a/libsrc/core/ng_mpi_generated_declarations.hpp +++ b/libsrc/core/ng_mpi_generated_declarations.hpp @@ -40,6 +40,8 @@ NGCORE_API extern int (*NG_MPI_Type_size)(NG_MPI_Datatype, int*); NGCORE_API extern int (*NG_MPI_Wait)(NG_MPI_Request*, NG_MPI_Status*); NGCORE_API extern int (*NG_MPI_Waitall)(int, NG_MPI_Request*, NG_MPI_Status*); NGCORE_API extern int (*NG_MPI_Waitany)(int, NG_MPI_Request*, int*, NG_MPI_Status*); +NGCORE_API extern int (*NG_MPI_Comm_c2f)(NG_MPI_Comm); +NGCORE_API extern NG_MPI_Comm NG_MPI_COMM_NULL; NGCORE_API extern NG_MPI_Comm NG_MPI_COMM_WORLD; NGCORE_API extern NG_MPI_Datatype NG_MPI_CHAR; NGCORE_API extern NG_MPI_Datatype NG_MPI_CXX_DOUBLE_COMPLEX; @@ -107,6 +109,8 @@ static const auto NG_MPI_Type_size = MPI_Type_size; static const auto NG_MPI_Wait = MPI_Wait; static const auto NG_MPI_Waitall = MPI_Waitall; static const auto NG_MPI_Waitany = MPI_Waitany; +static const auto NG_MPI_Comm_c2f = MPI_Comm_c2f; +static const decltype(MPI_COMM_NULL) NG_MPI_COMM_NULL = MPI_COMM_NULL; static const decltype(MPI_COMM_WORLD) NG_MPI_COMM_WORLD = MPI_COMM_WORLD; static const decltype(MPI_CHAR) NG_MPI_CHAR = MPI_CHAR; static const decltype(MPI_CXX_DOUBLE_COMPLEX) NG_MPI_CXX_DOUBLE_COMPLEX = MPI_CXX_DOUBLE_COMPLEX; diff --git a/libsrc/core/ng_mpi_generated_dummy_init.hpp b/libsrc/core/ng_mpi_generated_dummy_init.hpp index 6050bdee..9ea6fced 100644 --- a/libsrc/core/ng_mpi_generated_dummy_init.hpp +++ b/libsrc/core/ng_mpi_generated_dummy_init.hpp @@ -39,6 +39,8 @@ decltype(NG_MPI_Type_size) NG_MPI_Type_size = [](NG_MPI_Datatype, int*)->int { t decltype(NG_MPI_Wait) NG_MPI_Wait = [](NG_MPI_Request*, NG_MPI_Status*)->int { throw no_mpi(); }; decltype(NG_MPI_Waitall) NG_MPI_Waitall = [](int, NG_MPI_Request*, NG_MPI_Status*)->int { throw no_mpi(); }; decltype(NG_MPI_Waitany) NG_MPI_Waitany = [](int, NG_MPI_Request*, int*, NG_MPI_Status*)->int { throw no_mpi(); }; +decltype(NG_MPI_Comm_c2f) NG_MPI_Comm_c2f = [](NG_MPI_Comm)->int { throw no_mpi(); }; +NG_MPI_Comm NG_MPI_COMM_NULL = 0; NG_MPI_Comm NG_MPI_COMM_WORLD = 0; NG_MPI_Datatype NG_MPI_CHAR = 0; NG_MPI_Datatype NG_MPI_CXX_DOUBLE_COMPLEX = 0; diff --git a/libsrc/core/ng_mpi_generated_init.hpp b/libsrc/core/ng_mpi_generated_init.hpp index b44bc2a7..2529f409 100644 --- a/libsrc/core/ng_mpi_generated_init.hpp +++ b/libsrc/core/ng_mpi_generated_init.hpp @@ -39,6 +39,8 @@ NG_MPI_Type_size = [](NG_MPI_Datatype arg0, int* arg1)->int { return MPI_Type_si NG_MPI_Wait = [](NG_MPI_Request* arg0, NG_MPI_Status* arg1)->int { return MPI_Wait( ng2mpi(arg0), ng2mpi(arg1)); }; NG_MPI_Waitall = [](int arg0, NG_MPI_Request* arg1, NG_MPI_Status* arg2)->int { return MPI_Waitall( arg0, ng2mpi(arg1, arg0), ng2mpi(arg2)); }; NG_MPI_Waitany = [](int arg0, NG_MPI_Request* arg1, int* arg2, NG_MPI_Status* arg3)->int { return MPI_Waitany( arg0, ng2mpi(arg1, arg0), arg2, ng2mpi(arg3)); }; +NG_MPI_Comm_c2f = [](NG_MPI_Comm arg0)->int { return MPI_Comm_c2f( ng2mpi(arg0)); }; +NG_MPI_COMM_NULL = mpi2ng(MPI_COMM_NULL); NG_MPI_COMM_WORLD = mpi2ng(MPI_COMM_WORLD); NG_MPI_CHAR = mpi2ng(MPI_CHAR); NG_MPI_CXX_DOUBLE_COMPLEX = mpi2ng(MPI_CXX_DOUBLE_COMPLEX); From 35feeff7abdfbaebc2a012a695031159a9a0e537 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Thu, 23 May 2024 21:57:50 +0200 Subject: [PATCH 3/9] activate occ 7.8 on mac --- cmake/SuperBuild.cmake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmake/SuperBuild.cmake b/cmake/SuperBuild.cmake index 5c10fc15..3bbda589 100644 --- a/cmake/SuperBuild.cmake +++ b/cmake/SuperBuild.cmake @@ -89,12 +89,12 @@ if(BUILD_OCC) set(OCC_DIR ${CMAKE_CURRENT_BINARY_DIR}/dependencies/occ) ExternalProject_Add(project_occ - URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_6_3.zip - URL_MD5 2426e373903faabbd4f96a01a934b66d + # URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_6_3.zip + # URL_MD5 2426e373903faabbd4f96a01a934b66d # URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_7_2.zip # URL_MD5 533eb4f18af0f77ae321b158caeaee79 - # URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_8_1.zip - # URL_MD5 bf62952a03696dab9e4272aa8efacb1a + URL https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_8_1.zip + URL_MD5 bf62952a03696dab9e4272aa8efacb1a DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external_dependencies ${SUBPROJECT_ARGS} CMAKE_ARGS From e404ce737bd87eff286f0553225073eededfe453 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Fri, 24 May 2024 08:30:24 +0200 Subject: [PATCH 4/9] export localh set/get from mesh --- libsrc/meshing/python_mesh.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libsrc/meshing/python_mesh.cpp b/libsrc/meshing/python_mesh.cpp index f93ed62f..3a652e02 100644 --- a/libsrc/meshing/python_mesh.cpp +++ b/libsrc/meshing/python_mesh.cpp @@ -87,6 +87,8 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m) py::class_(m, "NGDummyArgument") .def("__bool__", []( NGDummyArgument &self ) { return false; } ) ; + + py::class_>(m, "LocalH"); py::class_> (m, "Point2d") .def(py::init()) @@ -1249,7 +1251,11 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m) else mp.optsteps3d = 5; OptimizeVolume (mp, self); }, py::arg("mp"), py::call_guard()) - + .def("SetLocalH",[](Mesh& self, shared_ptr localh, int layer) + { + self.SetLocalH(localh, layer); + }, py::arg("localh"), py::arg("layer")=1) + .def("GetLocalH", &Mesh::GetLocalH) .def ("OptimizeMesh2d", [](Mesh & self, MeshingParameters* pars, int faceindex) { self.CalcLocalH(0.5); From 246dfd734d42830beb5eddda2f20426b17620b97 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Mon, 27 May 2024 14:24:32 +0300 Subject: [PATCH 5/9] mpi4py: Support limited API and ABI compatibility --- libsrc/core/mpi4py_pycapi.h | 245 ++++++++++++++++++++++++++++++++++++ libsrc/core/ng_mpi.cpp | 4 + 2 files changed, 249 insertions(+) create mode 100644 libsrc/core/mpi4py_pycapi.h diff --git a/libsrc/core/mpi4py_pycapi.h b/libsrc/core/mpi4py_pycapi.h new file mode 100644 index 00000000..87d0c69e --- /dev/null +++ b/libsrc/core/mpi4py_pycapi.h @@ -0,0 +1,245 @@ +/* Author: Lisandro Dalcin */ +/* Contact: dalcinl@gmail.com */ + +#ifndef MPI4PY_PYCAPI_H +#define MPI4PY_PYCAPI_H + +#include +#include + +#define _mpi4py_declare_pycapi(Type, star) \ +static PyTypeObject *_mpi4py_PyMPI##Type = NULL; \ +static PyObject *(*_mpi4py_PyMPI##Type##_New)(MPI_##Type star) = NULL; \ +static MPI_##Type *(*_mpi4py_PyMPI##Type##_Get)(PyObject *) = NULL; + +#ifndef MPI4PY_LIMITED_API_SKIP_DATATYPE +_mpi4py_declare_pycapi(Datatype,) +#define PyMPIDatatype_Type (*_mpi4py_PyMPIDatatype) +#define PyMPIDatatype_New _mpi4py_PyMPIDatatype_New +#define PyMPIDatatype_Get _mpi4py_PyMPIDatatype_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_STATUS +_mpi4py_declare_pycapi(Status,*) +#define PyMPIStatus_Type (*_mpi4py_PyMPIStatus) +#define PyMPIStatus_New _mpi4py_PyMPIStatus_New +#define PyMPIStatus_Get _mpi4py_PyMPIStatus_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_REQUEST +_mpi4py_declare_pycapi(Request,) +#define PyMPIRequest_Type (*_mpi4py_PyMPIRequest) +#define PyMPIRequest_New _mpi4py_PyMPIRequest_New +#define PyMPIRequest_Get _mpi4py_PyMPIRequest_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_MESSAGE +_mpi4py_declare_pycapi(Message,) +#define PyMPIMessage_Type (*_mpi4py_PyMPIMessage) +#define PyMPIMessage_New _mpi4py_PyMPIMessage_New +#define PyMPIMessage_Get _mpi4py_PyMPIMessage_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_OP +_mpi4py_declare_pycapi(Op,) +#define PyMPIOp_Type (*_mpi4py_PyMPIOp) +#define PyMPIOp_New _mpi4py_PyMPIOp_New +#define PyMPIOp_Get _mpi4py_PyMPIOp_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_GROUP +_mpi4py_declare_pycapi(Group,) +#define PyMPIGroup_Type (*_mpi4py_PyMPIGroup) +#define PyMPIGroup_New _mpi4py_PyMPIGroup_New +#define PyMPIGroup_Get _mpi4py_PyMPIGroup_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_INFO +_mpi4py_declare_pycapi(Info,) +#define PyMPIInfo_Type (*_mpi4py_PyMPIInfo) +#define PyMPIInfo_New _mpi4py_PyMPIInfo_New +#define PyMPIInfo_Get _mpi4py_PyMPIInfo_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_ERRHANDLER +_mpi4py_declare_pycapi(Errhandler,) +#define PyMPIErrhandler_Type (*_mpi4py_PyMPIErrhandler) +#define PyMPIErrhandler_New _mpi4py_PyMPIErrhandler_New +#define PyMPIErrhandler_Get _mpi4py_PyMPIErrhandler_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_SESSION +_mpi4py_declare_pycapi(Session,) +#define PyMPISession_Type (*_mpi4py_PyMPISession) +#define PyMPISession_New _mpi4py_PyMPISession_New +#define PyMPISession_Get _mpi4py_PyMPISession_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_COMM +_mpi4py_declare_pycapi(Comm,) +#define PyMPIComm_Type (*_mpi4py_PyMPIComm) +#define PyMPIComm_New _mpi4py_PyMPIComm_New +#define PyMPIComm_Get _mpi4py_PyMPIComm_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_WIN +_mpi4py_declare_pycapi(Win,) +#define PyMPIWin_Type (*_mpi4py_PyMPIWin) +#define PyMPIWin_New _mpi4py_PyMPIWin_New +#define PyMPIWin_Get _mpi4py_PyMPIWin_Get +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_FILE +_mpi4py_declare_pycapi(File,) +#define PyMPIFile_Type (*_mpi4py_PyMPIFile) +#define PyMPIFile_New _mpi4py_PyMPIFile_New +#define PyMPIFile_Get _mpi4py_PyMPIFile_Get +#endif + +#undef _mpi4py_define_pycapi + +static int _mpi4py_ImportType(PyObject *module, + const char *type_name, + PyTypeObject **type) +{ + PyObject *attr = NULL; + attr = PyObject_GetAttrString(module, type_name); + if (!attr) + goto fn_fail; + if (!PyType_Check(attr)) { + PyErr_Format(PyExc_TypeError, + "%.200s.%.200s is not a type object", + PyModule_GetName(module), type_name); + goto fn_fail; + } + *type = (PyTypeObject *)attr; + return 0; + fn_fail: + Py_DecRef(attr); + return -1; +} + +static int _mpi4py_ImportFunc(PyObject *module, + const char *func_name, + const char *signature, + void (**func)(void)) +{ + PyObject *pyxcapi = NULL; + PyObject *capsule = NULL; + union { void *obj; void (*fcn)(void); } ptr; + pyxcapi = PyObject_GetAttrString(module, (char *)"__pyx_capi__"); + if (!pyxcapi) + goto fn_fail; + + capsule = PyDict_GetItemString(pyxcapi, func_name); + if (!capsule) { + PyErr_Format(PyExc_ImportError, + "%.200s does not export expected C function %.200s", + PyModule_GetName(module), func_name); + goto fn_fail; + } + if (!PyCapsule_CheckExact(capsule)) { + PyErr_Format(PyExc_TypeError, + "%.200s.%.200s is not a capsule", + PyModule_GetName(module), func_name); + } + if (!signature) { + signature = PyCapsule_GetName(capsule); + } + if (!PyCapsule_IsValid(capsule, signature)) { + PyErr_Format(PyExc_TypeError, + "C function %.200s.%.200s has wrong signature " + "(expected %.500s, got %.500s)", + PyModule_GetName(module), func_name, + signature, PyCapsule_GetName(capsule)); + goto fn_fail; + } + ptr.obj = PyCapsule_GetPointer(capsule, signature); + if (!ptr.obj) + goto fn_fail; + *func = ptr.fcn; + Py_DecRef(pyxcapi); + return 0; + fn_fail: + Py_DecRef(pyxcapi); + return -1; +} + +static int import_mpi4py_MPI(void) +{ + PyObject *module = PyImport_ImportModule("mpi4py.MPI"); + if (!module) + goto fn_fail; + +#define _mpi4py_import_pycapi(Type) do { \ + if (_mpi4py_ImportType(module, #Type, &_mpi4py_PyMPI##Type) < 0) \ + goto fn_fail; \ + if (_mpi4py_ImportFunc(module, "PyMPI" #Type "_New", NULL, \ + (void (**)(void))&_mpi4py_PyMPI##Type##_New) < 0) \ + goto fn_fail; \ + if (_mpi4py_ImportFunc(module, "PyMPI" #Type "_Get", NULL, \ + (void (**)(void))&_mpi4py_PyMPI##Type##_Get) < 0) \ + goto fn_fail; \ + } while (0) + +#ifndef MPI4PY_LIMITED_API_SKIP_DATATYPE + _mpi4py_import_pycapi(Datatype); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_STATUS + _mpi4py_import_pycapi(Status); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_REQUEST + _mpi4py_import_pycapi(Request); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_MESSAGE + _mpi4py_import_pycapi(Message); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_OP + _mpi4py_import_pycapi(Op); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_GROUP + _mpi4py_import_pycapi(Group); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_INFO + _mpi4py_import_pycapi(Info); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_ERRHANDLER + _mpi4py_import_pycapi(Errhandler); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_SESSION + _mpi4py_import_pycapi(Session); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_COMM + _mpi4py_import_pycapi(Comm); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_WIN + _mpi4py_import_pycapi(Win); +#endif + +#ifndef MPI4PY_LIMITED_API_SKIP_FILE + _mpi4py_import_pycapi(File); +#endif + +#undef _mpi4py_import_pycapi + + Py_DecRef(module); + return 0; + fn_fail: + Py_DecRef(module); + return -1; +} + +#define __PYX_HAVE_API__mpi4py__MPI +#define import_mpi4py__MPI import_mpi4py_MPI + +#endif /* MPI4PY_PYCAPI_H */ diff --git a/libsrc/core/ng_mpi.cpp b/libsrc/core/ng_mpi.cpp index 9dce98c8..30075ed0 100644 --- a/libsrc/core/ng_mpi.cpp +++ b/libsrc/core/ng_mpi.cpp @@ -10,6 +10,10 @@ #include "pybind11/pytypes.h" #if defined(NG_PYTHON) && defined(NG_MPI4PY) +#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 #include #include "python_ngcore.hpp" From 1e7624c7f589cd08c7915a73a10da665a8cc4eec Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Tue, 28 May 2024 10:55:58 +0200 Subject: [PATCH 6/9] Get rid of mpi4py compile-time dependency, disable MPI wrapper by default --- CMakeLists.txt | 12 ++---------- libsrc/core/CMakeLists.txt | 3 ++- libsrc/core/ng_mpi.cpp | 22 ++++++++++------------ libsrc/core/ng_mpi.hpp | 20 +++----------------- libsrc/core/ng_mpi_wrapper.cpp | 26 ++++++++++++++++---------- libsrc/core/python_ngcore.hpp | 14 +++++++++----- libsrc/core/python_ngcore_export.cpp | 2 ++ 7 files changed, 44 insertions(+), 55 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71ac2d86..31faba7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,8 +16,7 @@ option( USE_GUI "build with GUI" ON ) option( USE_PYTHON "build with python interface" ON ) cmake_dependent_option( PREFER_SYSTEM_PYBIND11 "Use system wide PyBind11" OFF "USE_PYTHON" 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)" ON ) +option( USE_MPI_WRAPPER "enable mpi wrapper (run-time dispatch of MPI library calls)" OFF ) option( USE_OCC "build with OpenCascade geometry kernel interface" ON) option( USE_STLGEOM "build with STL geometry support" ON) option( USE_CSG "build with CSG kernel" ON) @@ -323,6 +322,7 @@ if (USE_PYTHON) add_subdirectory(external_dependencies/pybind11) 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(nglib PRIVATE ${pybind11_INCLUDE_DIR} ${Python3_INCLUDE_DIRS}) 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_link_libraries(netgen_metis INTERFACE ${METIS_LIBRARY} ) 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) ####################################################################### diff --git a/libsrc/core/CMakeLists.txt b/libsrc/core/CMakeLists.txt index eca94b8c..69245779 100644 --- a/libsrc/core/CMakeLists.txt +++ b/libsrc/core/CMakeLists.txt @@ -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 simd.hpp simd_avx.hpp simd_avx512.hpp simd_generic.hpp simd_sse.hpp simd_arm64.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) if(ENABLE_CPP_CORE_GUIDELINES_CHECK) @@ -166,6 +166,7 @@ if(USE_MPI) endif() else() target_link_libraries(ngcore PUBLIC ${MPI_C_LIBRARIES}) + target_include_directories(ngcore PUBLIC ${MPI_C_INCLUDE_PATH}) endif(USE_MPI_WRAPPER) endif(USE_MPI) diff --git a/libsrc/core/ng_mpi.cpp b/libsrc/core/ng_mpi.cpp index 30075ed0..0cb2847f 100644 --- a/libsrc/core/ng_mpi.cpp +++ b/libsrc/core/ng_mpi.cpp @@ -6,20 +6,18 @@ #include +#include "array.hpp" #include "ngcore_api.hpp" #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_SKIP_MESSAGE 1 #define MPI4PY_LIMITED_API_SKIP_SESSION 1 -#include "mpi4py_pycapi.h" // mpi4py < 4.0.0 -#include - -#include "python_ngcore.hpp" - -namespace py = pybind11; -#endif +#include "mpi4py_pycapi.h" // mpi4py < 4.0.0 #ifdef MSMPI_VER 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; 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 { if (!imported_mpi4py) { - import_mpi4py(); + import_mpi4py__MPI(); imported_mpi4py = true; } PyObject* py_src = src.ptr(); @@ -176,12 +174,12 @@ void ng_init_mpi() { }; NG_MPI_CommToMPI4Py = [](NG_MPI_Comm src) -> py::handle { if (!imported_mpi4py) { - import_mpi4py(); + import_mpi4py__MPI(); imported_mpi4py = true; } return py::handle(PyMPIComm_New(ng2mpi(src))); }; -#endif +#endif // NG_PYTHON #include "ng_mpi_generated_init.hpp" } diff --git a/libsrc/core/ng_mpi.hpp b/libsrc/core/ng_mpi.hpp index f29bb664..9843a516 100644 --- a/libsrc/core/ng_mpi.hpp +++ b/libsrc/core/ng_mpi.hpp @@ -9,17 +9,8 @@ #include "ngcore_api.hpp" -#if defined(NG_PYTHON) && defined(NG_MPI4PY) -#include - -namespace py = pybind11; -#endif - #ifndef NG_MPI_WRAPPER #include -#if defined(NG_PYTHON) && defined(NG_MPI4PY) -#include -#endif #endif // NG_MPI_WRAPPER namespace ngcore { @@ -83,23 +74,18 @@ struct NG_MPI_Aint { NG_MPI_Aint() = default; }; -#else +#else // NG_MPI_WRAPPER +using NG_MPI_Comm = MPI_Comm; using NG_MPI_Status = MPI_Status; -using NG_MPI_Comm = MPI_Comm; using NG_MPI_Datatype = MPI_Datatype; using NG_MPI_Request = MPI_Request; using NG_MPI_Op = MPI_Op; using NG_MPI_Group = MPI_Group; using NG_MPI_Aint = MPI_Aint; -#endif +#endif // NG_MPI_WRAPPER #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 #endif // PARALLEL diff --git a/libsrc/core/ng_mpi_wrapper.cpp b/libsrc/core/ng_mpi_wrapper.cpp index ac9d3894..eedaf1b4 100644 --- a/libsrc/core/ng_mpi_wrapper.cpp +++ b/libsrc/core/ng_mpi_wrapper.cpp @@ -13,6 +13,14 @@ using std::cerr; using std::cout; using std::endl; +#ifndef NG_MPI_WRAPPER +#include +#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 { #ifdef NG_MPI_WRAPPER @@ -28,9 +36,7 @@ struct MPIFinalizer { } } mpi_finalizer; -bool MPI_Loaded() { - return ng_mpi_lib != nullptr; -} +bool MPI_Loaded() { return ng_mpi_lib != nullptr; } void InitMPI(std::optional mpi_lib_path) { if (ng_mpi_lib) return; @@ -128,7 +134,7 @@ static std::runtime_error no_mpi() { return std::runtime_error("MPI not enabled"); } -#if defined(NG_PYTHON) && defined(NG_MPI4PY) +#ifdef NG_PYTHON decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py = [](py::handle py_obj, NG_MPI_Comm &ng_comm) -> bool { // 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 = [](NG_MPI_Comm) -> py::handle { throw no_mpi(); }; -#endif +#endif // NG_PYTHON #include "ng_mpi_generated_dummy_init.hpp" #else // NG_MPI_WRAPPER static bool imported_mpi4py = false; -#if defined(NG_PYTHON) && defined(NG_MPI4PY) +#ifdef NG_PYTHON decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py = [](py::handle src, NG_MPI_Comm &dst) -> bool { if (!imported_mpi4py) { - import_mpi4py(); + import_mpi4py__MPI(); imported_mpi4py = true; } PyObject *py_src = src.ptr(); @@ -177,19 +183,19 @@ decltype(NG_MPI_CommFromMPI4Py) NG_MPI_CommFromMPI4Py = decltype(NG_MPI_CommToMPI4Py) NG_MPI_CommToMPI4Py = [](NG_MPI_Comm src) -> py::handle { if (!imported_mpi4py) { - import_mpi4py(); + import_mpi4py__MPI(); imported_mpi4py = true; } return py::handle(PyMPIComm_New(src)); }; -#endif +#endif // NG_PYTHON +bool MPI_Loaded() { return true; } void InitMPI(std::optional) {} #endif // NG_MPI_WRAPPER - } // namespace ngcore #endif // PARALLEL diff --git a/libsrc/core/python_ngcore.hpp b/libsrc/core/python_ngcore.hpp index cd3d14fb..c8ad53ac 100644 --- a/libsrc/core/python_ngcore.hpp +++ b/libsrc/core/python_ngcore.hpp @@ -13,13 +13,17 @@ #include "archive.hpp" #include "flags.hpp" #include "ngcore_api.hpp" -#include "profiler.hpp" #include "ng_mpi.hpp" namespace py = pybind11; 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 { template @@ -34,15 +38,15 @@ namespace ngcore }; } // namespace detail +#ifdef PARALLEL struct mpi4py_comm { mpi4py_comm() = default; -#ifdef PARALLEL mpi4py_comm(NG_MPI_Comm value) : value(value) {} operator NG_MPI_Comm () { return value; } NG_MPI_Comm value; -#endif // PARALLEL }; +#endif // PARALLEL } // namespace ngcore @@ -51,7 +55,7 @@ namespace ngcore namespace pybind11 { namespace detail { -#ifdef NG_MPI4PY +#ifdef PARALLEL template <> struct type_caster { public: PYBIND11_TYPE_CASTER(ngcore::mpi4py_comm, _("mpi4py_comm")); @@ -70,7 +74,7 @@ template <> struct type_caster { return ngcore::NG_MPI_CommToMPI4Py(src.value); } }; -#endif // NG_MPI4PY +#endif // PARALLEL template struct ngcore_list_caster { using value_conv = make_caster; diff --git a/libsrc/core/python_ngcore_export.cpp b/libsrc/core/python_ngcore_export.cpp index 8c9e1a0e..fdcc4bb2 100644 --- a/libsrc/core/python_ngcore_export.cpp +++ b/libsrc/core/python_ngcore_export.cpp @@ -371,5 +371,7 @@ threads : int ; +#ifdef PARALLEL py::implicitly_convertible(); +#endif // PARALLEL } From f70200e5aa7c0996d41b93ff7d342f342824000c Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Tue, 28 May 2024 12:43:27 +0200 Subject: [PATCH 7/9] Don't include MPI cxx symbols properly --- libsrc/core/CMakeLists.txt | 2 +- libsrc/core/ng_mpi.cpp | 3 +-- libsrc/core/ng_mpi.hpp | 1 + libsrc/core/ng_mpi_native.hpp | 21 --------------------- libsrc/core/ng_mpi_wrapper.cpp | 1 - 5 files changed, 3 insertions(+), 25 deletions(-) delete mode 100644 libsrc/core/ng_mpi_native.hpp diff --git a/libsrc/core/CMakeLists.txt b/libsrc/core/CMakeLists.txt index 69245779..6b60875d 100644 --- a/libsrc/core/CMakeLists.txt +++ b/libsrc/core/CMakeLists.txt @@ -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 simd.hpp simd_avx.hpp simd_avx512.hpp simd_generic.hpp simd_sse.hpp simd_arm64.hpp register_archive.hpp autodiff.hpp autodiffdiff.hpp - ng_mpi.hpp ng_mpi_generated_declarations.hpp ng_mpi_native.hpp mpi4py_pycapi.h + ng_mpi.hpp ng_mpi_generated_declarations.hpp mpi4py_pycapi.h DESTINATION ${NG_INSTALL_DIR_INCLUDE}/core COMPONENT netgen_devel) if(ENABLE_CPP_CORE_GUIDELINES_CHECK) diff --git a/libsrc/core/ng_mpi.cpp b/libsrc/core/ng_mpi.cpp index 0cb2847f..bac75e47 100644 --- a/libsrc/core/ng_mpi.cpp +++ b/libsrc/core/ng_mpi.cpp @@ -1,9 +1,8 @@ #define OMPI_SKIP_MPICXX +#include #include "ng_mpi.hpp" -#include - #include #include "array.hpp" diff --git a/libsrc/core/ng_mpi.hpp b/libsrc/core/ng_mpi.hpp index 9843a516..36151c09 100644 --- a/libsrc/core/ng_mpi.hpp +++ b/libsrc/core/ng_mpi.hpp @@ -10,6 +10,7 @@ #include "ngcore_api.hpp" #ifndef NG_MPI_WRAPPER +#define OMPI_SKIP_MPICXX #include #endif // NG_MPI_WRAPPER diff --git a/libsrc/core/ng_mpi_native.hpp b/libsrc/core/ng_mpi_native.hpp deleted file mode 100644 index 6c8f40ce..00000000 --- a/libsrc/core/ng_mpi_native.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef NG_MPI_NATIVE_HPP -#define NG_MPI_NATIVE_HPP - -#include - -#include "mpi_wrapper.hpp" -#include "ng_mpi.hpp" - -namespace ngcore { - -MPI_Comm NG_MPI_Native(NG_MPI_Comm comm) { - return reinterpret_cast(comm.value); -} - -MPI_Comm NG_MPI_Native(NgMPI_Comm comm) { - return reinterpret_cast(static_cast(comm).value); -} - -} // namespace ngcore - -#endif // NG_MPI_NATIVE_HPP diff --git a/libsrc/core/ng_mpi_wrapper.cpp b/libsrc/core/ng_mpi_wrapper.cpp index eedaf1b4..835b0c31 100644 --- a/libsrc/core/ng_mpi_wrapper.cpp +++ b/libsrc/core/ng_mpi_wrapper.cpp @@ -14,7 +14,6 @@ using std::cout; using std::endl; #ifndef NG_MPI_WRAPPER -#include #define MPI4PY_LIMITED_API 1 #define MPI4PY_LIMITED_API_SKIP_MESSAGE 1 #define MPI4PY_LIMITED_API_SKIP_SESSION 1 From b887b5d7c744e140ffd218f9f5c81a97b149f7bb Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 29 May 2024 10:51:45 +0200 Subject: [PATCH 8/9] Enable MPI wrapper for pip builds --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 90b06d0f..80371bab 100644 --- a/setup.py +++ b/setup.py @@ -120,6 +120,7 @@ elif 'linux' in sys.platform: if have_mpi: cmake_args += [ '-DUSE_MPI=ON', + '-DUSE_MPI_WRAPPER=ON', ] cmake_args += [ From fefea90133869956a262e6b4eb87e9129ae9deb9 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 29 May 2024 20:32:32 +0200 Subject: [PATCH 9/9] Fix pyodide build --- libsrc/meshing/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsrc/meshing/CMakeLists.txt b/libsrc/meshing/CMakeLists.txt index 8ca9a0cc..697e8ad1 100644 --- a/libsrc/meshing/CMakeLists.txt +++ b/libsrc/meshing/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources(nglib PRIVATE boundarylayer2d.cpp ) -target_link_libraries( nglib PRIVATE netgen_metis "$" ) +target_link_libraries( nglib PRIVATE $ $ ) install(FILES adfront2.hpp adfront3.hpp basegeom.hpp bcfunctions.hpp bisect.hpp