Skip to content

Commit

Permalink
Add option to build without MFEM
Browse files Browse the repository at this point in the history
  • Loading branch information
ckendrick committed May 30, 2024
1 parent 78e9756 commit 20c7eec
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 50 deletions.
112 changes: 65 additions & 47 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ project(_pylibROM)
set(CMAKE_BUILD_TYPE Debug)
set(PYBIND11_FINDPYTHON ON)

option (USE_MFEM "Build pylibROM with MFEM" OFF)

#=================== ScaLAPACK (optional) ==================
option(BUILD_SCALAPACK "Build static ScaLAPACK for libROM" OFF)

Expand Down Expand Up @@ -50,14 +52,18 @@ if (BUILD_LIBROM)
# )
# add_custom_target(RUN_LIBROM_BUILD ALL DEPENDS LIBROM_BUILD)

option (LIBROM_BUILD_CMD "Command used to build libROM and dependencies" CACHE STRING "${LIBROM_SCRIPTS_DIR}/compile.sh -t ${LIBROM_DIR}/cmake/toolchains/simple.cmake")
if (USE_MFEM)
set(LIBROM_BUILD_CMD "${LIBROM_BUILD_CMD} -m -g")
endif()
ExternalProject_Add(
libROM
SOURCE_DIR ${LIBROM_SCRIPTS_DIR}
CONFIGURE_COMMAND ""
BINARY_DIR ${LIBROM_DIR}
BUILD_COMMAND ${LIBROM_SCRIPTS_DIR}/compile.sh -m -g -t ${LIBROM_DIR}/cmake/toolchains/simple.cmake
INSTALL_COMMAND ""
)
BUILD_COMMAND ${LIBROM_BUILD_CMD}
INSTALL_COMMAND ""
)
message("Building libROM dependency...")
endif(BUILD_LIBROM)

Expand All @@ -72,63 +78,70 @@ execute_process(COMMAND python3 -c "import mpi4py; print(mpi4py.get_include())"
# # TODO(kevin): We do not bind mfem-related functions until we figure out how to type-cast SWIG Object.
# # Until then, mfem-related functions need to be re-implemented on python-end, using PyMFEM.

find_library(MFEM mfem
"$ENV{MFEM_DIR}/lib"
"$ENV{MFEM_DIR}"
"${LIBROM_DIR}/dependencies/mfem")
find_library(HYPRE HYPRE
"$ENV{HYPRE_DIR}/lib"
"${LIBROM_DIR}/dependencies/hypre/src/hypre/lib")
find_library(PARMETIS parmetis
"$ENV{PARMETIS_DIR}/lib"
"$ENV{PARMETIS_DIR}/build/lib/libparmetis"
"${LIBROM_DIR}/dependencies/parmetis-4.0.3/build/lib/libparmetis")
find_library(METIS metis
"$ENV{METIS_DIR}/lib"
"$ENV{PARMETIS_DIR}/build/lib/libmetis"
"${LIBROM_DIR}/dependencies/parmetis-4.0.3/build/lib/libmetis")
find_path(MFEM_INCLUDES mfem.hpp
"$ENV{MFEM_DIR}/include"
"$ENV{MFEM_DIR}"
"${LIBROM_DIR}/dependencies/mfem")
find_path(HYPRE_INCLUDES HYPRE.h
"$ENV{HYPRE_DIR}/include"
"${LIBROM_DIR}/dependencies/hypre/src/hypre/include")
find_path(PARMETIS_INCLUDES metis.h
"$ENV{PARMETIS_DIR}/metis/include"
"${LIBROM_DIR}/dependencies/parmetis-4.0.3/metis/include")

if (USE_MFEM)
find_library(MFEM mfem
"$ENV{MFEM_DIR}/lib"
"$ENV{MFEM_DIR}"
"${LIBROM_DIR}/dependencies/mfem")
find_library(HYPRE HYPRE
"$ENV{HYPRE_DIR}/lib"
"${LIBROM_DIR}/dependencies/hypre/src/hypre/lib")
find_library(PARMETIS parmetis
"$ENV{PARMETIS_DIR}/lib"
"$ENV{PARMETIS_DIR}/build/lib/libparmetis"
"${LIBROM_DIR}/dependencies/parmetis-4.0.3/build/lib/libparmetis")
find_library(METIS metis
"$ENV{METIS_DIR}/lib"
"$ENV{PARMETIS_DIR}/build/lib/libmetis"
"${LIBROM_DIR}/dependencies/parmetis-4.0.3/build/lib/libmetis")
find_path(MFEM_INCLUDES mfem.hpp
"$ENV{MFEM_DIR}/include"
"$ENV{MFEM_DIR}"
"${LIBROM_DIR}/dependencies/mfem")
find_path(HYPRE_INCLUDES HYPRE.h
"$ENV{HYPRE_DIR}/include"
"${LIBROM_DIR}/dependencies/hypre/src/hypre/include")
find_path(PARMETIS_INCLUDES metis.h
"$ENV{PARMETIS_DIR}/metis/include"
"${LIBROM_DIR}/dependencies/parmetis-4.0.3/metis/include")
endif()
#===================== pylibROM =============================


set(CMAKE_CXX_STANDARD 14)

find_package(MPI REQUIRED)

set(SOURCE_DIR "bindings/pylibROM")
set(SOURCE_DIR "bindings/pylibROM")
include_directories(
${SOURCE_DIR}
${LIBROM_INCLUDE_DIR}
${MPI_INCLUDE_PATH}
${MPI4PY}
${HDF5_C_INCLUDE_DIRS}
${MFEM_INCLUDES}
${HYPRE_INCLUDES}
${PARMETIS_INCLUDES}
${MFEM_C_INCLUDE_DIRS}
)
link_libraries(
${HDF5_LIBRARIES}
${MFEM}
${HYPRE}
${PARMETIS}
${METIS}
)
link_libraries(${HDF5_LIBRARIES})

if (USE_MFEM)
target_include_directories(
_pylibROM
${MFEM_INCLUDES}
${HYPRE_INCLUDES}
${PARMETIS_INCLUDES}
${MFEM_C_INCLUDE_DIRS})

target_link_libraries(
_pylibROM
${MFEM}
${HYPRE}
${PARMETIS}
${METIS})
endif()

add_subdirectory("extern/pybind11")

pybind11_add_module(_pylibROM
bindings/pylibROM/pylibROM.cpp
set(PYLIBROM_SOURCES
bindings/pylibROM/pylibROM.cpp

bindings/pylibROM/linalg/pyMatrix.cpp
bindings/pylibROM/linalg/pyVector.cpp
Expand Down Expand Up @@ -162,12 +175,17 @@ pybind11_add_module(_pylibROM
bindings/pylibROM/utils/pyHDFDatabase.cpp
bindings/pylibROM/utils/pyCSVDatabase.cpp

bindings/pylibROM/mfem/pyUtilities.cpp
bindings/pylibROM/mfem/pyPointwiseSnapshot.cpp
bindings/pylibROM/mfem/pySampleMesh.cpp

bindings/pylibROM/python_utils/cpp_utils.hpp
)

if (USE_MFEM)
set(PYLIBROM_SOURCES ${PYLIBROM_SOURCES}
bindings/pylibROM/mfem/pyUtilities.cpp
bindings/pylibROM/mfem/pyPointwiseSnapshot.cpp
bindings/pylibROM/mfem/pySampleMesh.cpp)
endif()

pybind11_add_module(_pylibROM ${PYLIBROM_SOURCES})
message("building pylibROM...")

target_link_libraries(_pylibROM PRIVATE ROM)
12 changes: 11 additions & 1 deletion bindings/pylibROM/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@
# either define/import the python routine in this file.
# This will combine both c++ bindings/pure python routines into this module.

from _pylibROM import *
from _pylibROM.algo import *
from _pylibROM.hyperreduction import *
from _pylibROM.linalg import *

try:
import _pylibROM.mfem
from _pylibROM.mfem import *
except:
pass

from _pylibROM.utils import *
6 changes: 6 additions & 0 deletions bindings/pylibROM/pylibROM.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <pybind11/pybind11.h>

#include "CAROM_config.h"

namespace py = pybind11;

//linalg
Expand Down Expand Up @@ -42,10 +44,12 @@ void init_Database(pybind11::module_ &m);
void init_HDFDatabase(pybind11::module_ &m);
void init_CSVDatabase(pybind11::module_ &m);

#ifdef CAROM_HAS_MFEM
//mfem
void init_mfem_Utilities(pybind11::module_ &m);
void init_mfem_PointwiseSnapshot(pybind11::module_ &m);
void init_mfem_SampleMesh(pybind11::module_ &m);
#endif

PYBIND11_MODULE(_pylibROM, m) {
py::module utils = m.def_submodule("utils");
Expand Down Expand Up @@ -87,10 +91,12 @@ PYBIND11_MODULE(_pylibROM, m) {
init_STSampling(hyperreduction);
init_Utilities(hyperreduction);

#ifdef CAROM_HAS_MFEM
py::module mfem = m.def_submodule("mfem");
init_mfem_Utilities(mfem);
init_mfem_PointwiseSnapshot(mfem);
init_mfem_SampleMesh(mfem);
#endif

// py::module python_utils = m.def_submodule("python_utils");
}
Expand Down
14 changes: 12 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
# Take the global option for pre-installed librom directory.
librom_dir = None
install_scalapack = False
use_mfem = True
for arg in sys.argv:
if (arg[:13] == "--librom_dir="):
librom_dir = arg[13:]
sys.argv.remove(arg)
if "--install_scalapack" in sys.argv:
install_scalapack = True
sys.argv.remove("--install_scalapack")
if "--no-mfem" in sys.argv:
use_mfem = False
sys.argv.remove("--no-mfem")
if "--use-mfem" in sys.argv:
use_mfem = True
sys.argv.remove("--use-mfem")

# Convert distutils Windows platform specifiers to CMake -A arguments
PLAT_TO_CMAKE = {
Expand Down Expand Up @@ -64,15 +71,18 @@ def build_extension(self, ext: CMakeExtension) -> None:
librom_dir += "/extern/libROM"
print("Installing libROM library: %s" % librom_dir)

librom_cmd = "cd %s && ./scripts/compile.sh -m -g -t ./cmake/toolchains/simple.cmake" % librom_dir
librom_cmd = "cd %s && ./scripts/compile.sh -t ./cmake/toolchains/simple.cmake" % librom_dir
if (install_scalapack): librom_cmd += " -s"
if (use_mfem): librom_cmd += " -m -g"
print("libROM installation command: %s" % librom_cmd)
subprocess.run(
librom_cmd, shell=True, check=True
)
else:
print("Using pre-installed libROM library: %s" % librom_dir)
cmake_args += [f"-DLIBROM_DIR=%s" % librom_dir]
if (not use_mfem):
cmake_args += ["-DUSE_MFEM=OFF"]

# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
Expand Down Expand Up @@ -165,7 +175,7 @@ def build_extension(self, ext: CMakeExtension) -> None:
# author_email="[email protected]",
description="Python Interface for LLNL libROM",
long_description="",
packages=find_packages(where='bindings'),
packages=find_packages(where='bindings', exclude=['pylibROM.mfem'] if use_mfem == False else ['']),
package_dir={"":"bindings"},
# packages=['bindings/pylibROM'],
ext_modules=[CMakeExtension("_pylibROM")],
Expand Down

0 comments on commit 20c7eec

Please sign in to comment.