Skip to content

Commit

Permalink
Make PETSc optional (#3123)
Browse files Browse the repository at this point in the history
* Petsc optional c++ only

* Use main github actions

* Add configure and build without PETSc to Actions

* Add no-PETSc C++ build option and PETSc-free Python

* Revert to main

* Update CMakeLists.txt

* Add lint section

* Syntax fix

* Syntax fix

* Remove duplicate tests

* Add Scotch root dir

* Add import test

* Attempty arch fix

* Remove some lines

* Fix test

* Work on env vars

* Another attempt

* Test CI

* Work work on vars

* More tests

* More testing

* Change approach

* Add conventional path hint

* Syntax fix

* Remove debug comment

* Simplification

---------

Co-authored-by: Garth N. Wells <[email protected]>
  • Loading branch information
chrisrichardson and garth-wells authored Apr 15, 2024
1 parent 6bb72cb commit e2f55cf
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 55 deletions.
36 changes: 33 additions & 3 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
container: ghcr.io/fenics/test-env:current-openmpi
steps:
- uses: actions/checkout@v4
- name: ruff C++ .py file checks
- name: ruff .py files in C++ code
run: |
cd cpp/
ruff check .
Expand Down Expand Up @@ -70,18 +70,48 @@ jobs:
if: "!(contains(github.event.head_commit.message, '[ci skip]') || contains(github.event.head_commit.message, '[skip ci]'))"
runs-on: ubuntu-latest
container: ghcr.io/fenics/test-env:current-openmpi
env:
SCOTCH_DIR: /usr/local/petsc/linux-gnu-real64-32

steps:
- name: Install FEniCS Python components (default branches/tags)
if: github.event_name != 'workflow_dispatch'
run: |
python3 -m pip install git+https://github.com/FEniCS/ufl.git
python3 -m pip install git+https://github.com/FEniCS/basix.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git
- name: Install FEniCS Python components
if: github.event_name == 'workflow_dispatch'
run: |
python3 -m pip install git+https://github.com/FEniCS/ufl.git@${{ github.event.inputs.ufl_ref }}
python3 -m pip install git+https://github.com/FEniCS/basix.git@${{ github.event.inputs.basix_ref }}
python3 -m pip install git+https://github.com/FEniCS/ffcx.git@${{ github.event.inputs.ffcx_ref }}
- uses: actions/checkout@v4
- name: Configure C++
run: cmake -G Ninja -DDOLFINX_ENABLE_PETSC=false -DCMAKE_BUILD_TYPE=Developer -B build -S cpp/
- name: Build and install C++ library
run: |
cmake --build build
cmake --install build
- name: Build Python interface
run: python3 -m pip -v install --check-build-dependencies --no-build-isolation --config-settings=cmake.build-type="Debug" python/
- name: Test Python import
run: python3 -c "import dolfinx; print(dolfinx.__version__)"

build-with-petsc:
if: "!(contains(github.event.head_commit.message, '[ci skip]') || contains(github.event.head_commit.message, '[skip ci]'))"
runs-on: ubuntu-latest
container: ghcr.io/fenics/test-env:current-openmpi
env:
PETSC_ARCH: ${{ matrix.petsc_arch }}
OMPI_ALLOW_RUN_AS_ROOT: 1
OMPI_ALLOW_RUN_AS_ROOT_CONFIRM: 1

strategy:
matrix:
petsc_arch: [linux-gnu-real32-32, linux-gnu-real64-32, linux-gnu-complex64-32, linux-gnu-complex128-32, linux-gnu-real64-64, linux-gnu-complex128-64]

name: Build and test (${{ matrix.petsc_arch }})

steps:
- uses: actions/checkout@v4

Expand Down
81 changes: 46 additions & 35 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ set_package_properties(
PURPOSE "IO, including in parallel"
)

if(DOLFINX_ENABLE_PETSC)
set(_REQUIRE_PETSC TRUE CACHE BOOL "Is PETSc REQUIRED?")
else()
set(_REQUIRE_PETSC FALSE CACHE BOOL "Is PETSc REQUIRED?")
endif()

option(DOLFINX_ENABLE_PETSC "Compile with support for PETSc." ON)
set_package_properties(
PETSc PROPERTIES
TYPE RECOMMENDED
DESCRIPTION "Portable, Extensible Toolkit for Scientific Computation"
URL "https://petsc.org/"
PURPOSE "Linear and nonlinear solvers"
)


if(DOLFINX_ENABLE_PARMETIS)
set(_REQUIRE_PARMETIS
TRUE
Expand Down Expand Up @@ -294,37 +310,6 @@ set_package_properties(
URL "https://github.com/fenics/basix"
)

# Check for PETSc
find_package(PkgConfig REQUIRED)
set(ENV{PKG_CONFIG_PATH}
"$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/pkgconfig:$ENV{PETSC_DIR}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}"
)
pkg_search_module(PETSC REQUIRED IMPORTED_TARGET PETSc>=3.15 petsc>=3.15)

# Check if PETSc build uses real or complex scalars (this is configured in
# DOLFINxConfig.cmake.in)
include(CheckSymbolExists)
set(CMAKE_REQUIRED_INCLUDES ${PETSC_INCLUDE_DIRS})
check_symbol_exists(PETSC_USE_COMPLEX petscsystypes.h HAVE_PETSC_SCALAR_COMPLEX)

# Setting for FeatureSummary
if(PETSC_FOUND)
message(
STATUS "Found PETSc version ${PETSC_VERSION}, prefix: ${PETSC_PREFIX}"
)
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND PETSc)
else()
set_property(GLOBAL APPEND PROPERTY PACKAGES_NOT_FOUND PETSc)
endif()

set_package_properties(
PETSc PROPERTIES
TYPE REQUIRED
DESCRIPTION "Portable, Extensible Toolkit for Scientific Computation (PETSc)"
URL "https://www.mcs.anl.gov/petsc/"
PURPOSE "PETSc linear algebra backend"
)

# Check for HDF5
set(HDF5_PREFER_PARALLEL TRUE)
set(HDF5_FIND_DEBUG TRUE)
Expand All @@ -333,7 +318,7 @@ find_package(HDF5 REQUIRED COMPONENTS C)
if(NOT HDF5_IS_PARALLEL)
message(
FATAL_ERROR
"Found serial HDF5 build, MPI HDF5 build required, try setting HDF5_DIR or HDF5_ROOT"
"Found serial HDF5 build, MPI HDF5 build required, try setting HDF5_DIR or HDF5_ROOT"
)
endif()

Expand Down Expand Up @@ -368,21 +353,45 @@ set_package_properties(

# ------------------------------------------------------------------------------
# Find optional packages

if(DOLFINX_ENABLE_ADIOS2 AND _REQUIRE_ADIOS2)
find_package(ADIOS2 2.8.1 REQUIRED)
elseif(DOLFINX_ENABLE_ADIOS2)
find_package(ADIOS2 2.8.1)
endif()

if(ADIOS2_FOUND AND NOT ADIOS2_HAVE_MPI)
message(
FATAL_ERROR
"Found serial ADIOS2 build, MPI ADIOS2 build required, try setting ADIOS2_DIR or ADIOS2_ROOT"
)
endif()

if(DOLFINX_ENABLE_SLEPC)
if(DOLFINX_ENABLE_PETSC)
find_package(PkgConfig REQUIRED)
set(ENV{PKG_CONFIG_PATH}
"$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/pkgconfig:$ENV{PETSC_DIR}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}"
)
if(_REQUIRE_PETSC)
pkg_search_module(PETSC REQUIRED IMPORTED_TARGET PETSc>=3.15 petsc>=3.15)
else()
pkg_search_module(PETSC OPTIONAL IMPORTED_TARGET PETSc>=3.15 petsc>=3.15)
endif()

# Setting for FeatureSummary
if(PETSC_FOUND)
message(STATUS "Found PETSc version ${PETSC_VERSION}, prefix: ${PETSC_PREFIX}")

# Check if PETSc build uses real or complex scalars (this is
# configured in DOLFINxConfig.cmake.in)
include(CheckSymbolExists)
set(CMAKE_REQUIRED_INCLUDES ${PETSC_INCLUDE_DIRS})
check_symbol_exists(PETSC_USE_COMPLEX petscsystypes.h HAVE_PETSC_SCALAR_COMPLEX)
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND PETSc)
else()
set_property(GLOBAL APPEND PROPERTY PACKAGES_NOT_FOUND PETSc)
endif()
endif()

if(DOLFINX_ENABLE_SLEPC AND PETSC_FOUND)
find_package(PkgConfig REQUIRED)
set(ENV{PKG_CONFIG_PATH}
"$ENV{SLEPC_DIR}/$ENV{PETSC_ARCH}/lib/pkgconfig:$ENV{SLEPC_DIR}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}"
Expand All @@ -408,6 +417,8 @@ if(DOLFINX_ENABLE_SLEPC)
else()
set_property(GLOBAL APPEND PROPERTY PACKAGES_NOT_FOUND SLEPc)
endif()
elseif(_REQUIRE_SLEPC AND NOT PETSC_FOUND)
message(FATAL_ERROR "SLEPc requested, but no configured because PETSc was not found.")
endif()

if(DOLFINX_ENABLE_SCOTCH AND _REQUIRE_SCOTCH)
Expand Down
15 changes: 8 additions & 7 deletions cpp/cmake/modules/FindSCOTCH.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ message(STATUS "Checking for package 'SCOTCH-PT'")
# Check for header file
find_path(
SCOTCH_INCLUDE_DIRS ptscotch.h
HINTS ${SCOTCH_ROOT}/include $ENV{SCOTCH_ROOT}/include ${PETSC_INCLUDE_DIRS}
HINTS ${SCOTCH_DIR}/include $ENV{SCOTCH_DIR}/include
${SCOTCH_ROOT}/include $ENV{SCOTCH_ROOT}/include ${PETSC_INCLUDE_DIRS}
PATH_SUFFIXES scotch
DOC "Directory where the SCOTCH-PT header is located"
)
Expand All @@ -59,7 +60,7 @@ find_path(
find_library(
SCOTCH_LIBRARY
NAMES scotch
HINTS ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
NO_DEFAULT_PATH
DOC "The SCOTCH library"
)
Expand All @@ -73,7 +74,7 @@ find_library(
find_library(
SCOTCHERR_LIBRARY
NAMES scotcherr
HINTS ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib
HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib
NO_DEFAULT_PATH
DOC "The SCOTCH-ERROR library"
)
Expand All @@ -87,7 +88,7 @@ find_library(
find_library(
PTSCOTCH_LIBRARY
NAMES ptscotch
HINTS ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
NO_DEFAULT_PATH
DOC "The PTSCOTCH library"
)
Expand All @@ -101,7 +102,7 @@ find_library(
find_library(
PTESMUMPS_LIBRARY
NAMES ptesmumps esmumps
HINTS ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
NO_DEFAULT_PATH
DOC "The PTSCOTCH-ESMUMPS library"
)
Expand All @@ -115,7 +116,7 @@ find_library(
find_library(
PTSCOTCHERR_LIBRARY
NAMES ptscotcherr
HINTS ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${SCOTCH_ROOT}/lib $ENV{SCOTCH_ROOT}/lib ${PETSC_LIBRARY_DIRS}
NO_DEFAULT_PATH
DOC "The PTSCOTCH-ERROR library"
)
Expand Down Expand Up @@ -374,6 +375,6 @@ endif()

# Standard package handling
find_package_handle_standard_args(
SCOTCH "SCOTCH could not be found. Be sure to set SCOTCH_ROOT."
SCOTCH "SCOTCH could not be found. Be sure to set SCOTCH_DIR."
SCOTCH_LIBRARIES SCOTCH_INCLUDE_DIRS SCOTCH_TEST_RUNS
)
16 changes: 9 additions & 7 deletions cpp/cmake/templates/DOLFINXConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ if(NOT TARGET hdf5::hdf5)
endif()
endif()

set(PETSC_SCALAR_COMPLEX @HAVE_PETSC_SCALAR_COMPLEX@)
if(NOT TARGET PkgConfig::PETSC)
find_package(PkgConfig REQUIRED)
set(ENV{PKG_CONFIG_PATH}
"$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/pkgconfig:$ENV{PETSC_DIR}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}"
)
pkg_search_module(PETSC REQUIRED IMPORTED_TARGET PETSc petsc)
if(@PETSC_FOUND@)
set(PETSC_SCALAR_COMPLEX @HAVE_PETSC_SCALAR_COMPLEX@)
if(NOT TARGET PkgConfig::PETSC)
find_package(PkgConfig REQUIRED)
set(ENV{PKG_CONFIG_PATH}
"$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/pkgconfig:$ENV{PETSC_DIR}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}"
)
pkg_search_module(PETSC REQUIRED IMPORTED_TARGET PETSc petsc)
endif()
endif()

if(@SLEPC_FOUND@)
Expand Down
9 changes: 6 additions & 3 deletions cpp/dolfinx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ target_link_libraries(dolfinx PUBLIC Boost::timer)
# MPI
target_link_libraries(dolfinx PUBLIC MPI::MPI_CXX)

# PETSc
target_link_libraries(dolfinx PUBLIC PkgConfig::PETSC)

# HDF5
target_link_libraries(dolfinx PUBLIC hdf5::hdf5)

Expand All @@ -107,6 +104,12 @@ if(ADIOS2_FOUND)
target_link_libraries(dolfinx PUBLIC adios2::cxx11_mpi)
endif()

# PETSc
if(DOLFINX_ENABLE_PETSC AND PETSC_FOUND)
target_link_libraries(dolfinx PUBLIC PkgConfig::PETSC)
target_compile_definitions(dolfinx PUBLIC HAS_PETSC)
endif()

# SLEPC
if(DOLFINX_ENABLE_SLEPC AND SLEPC_FOUND)
target_link_libraries(dolfinx PUBLIC PkgConfig::SLEPC)
Expand Down
9 changes: 9 additions & 0 deletions cpp/dolfinx/common/defines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ bool dolfinx::has_debug()
#endif
}
//-------------------------------------------------------------------------
bool dolfinx::has_petsc()
{
#ifdef HAS_PETSC
return true;
#else
return false;
#endif
}
//-------------------------------------------------------------------------
bool dolfinx::has_slepc()
{
#ifdef HAS_SLEPC
Expand Down
3 changes: 3 additions & 0 deletions cpp/dolfinx/common/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ std::string git_commit_hash();
/// i.e., with assertions on
bool has_debug();

/// Return true if DOLFINx is compiled with PETSc
bool has_petsc();

/// Return true if DOLFINx is compiled with SLEPc
bool has_slepc();

Expand Down
3 changes: 3 additions & 0 deletions cpp/dolfinx/fem/petsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifdef HAS_PETSC

#include "petsc.h"
#include <dolfinx/common/IndexMap.h>
#include <functional>
Expand Down Expand Up @@ -79,3 +81,4 @@ Vec fem::petsc::create_vector_nest(
return y;
}
//-----------------------------------------------------------------------------
#endif
4 changes: 4 additions & 0 deletions cpp/dolfinx/fem/petsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#pragma once

#ifdef HAS_PETSC

#include "Form.h"
#include "assembler.h"
#include "utils.h"
Expand Down Expand Up @@ -482,3 +484,5 @@ void set_bc(

} // namespace petsc
} // namespace dolfinx::fem

#endif
2 changes: 2 additions & 0 deletions cpp/dolfinx/la/dolfinx_la.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace dolfinx::la
// DOLFINx la interface

#include <dolfinx/la/SparsityPattern.h>
#ifdef HAS_PETSC
#include <dolfinx/la/petsc.h>
#endif
#include <dolfinx/la/slepc.h>
#include <dolfinx/la/utils.h>
4 changes: 4 additions & 0 deletions cpp/dolfinx/la/petsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifdef HAS_PETSC

#include "petsc.h"
#include "SparsityPattern.h"
#include "Vector.h"
Expand Down Expand Up @@ -788,3 +790,5 @@ void petsc::KrylovSolver::set_from_options() const
//-----------------------------------------------------------------------------
KSP petsc::KrylovSolver::ksp() const { return _ksp; }
//-----------------------------------------------------------------------------

#endif
4 changes: 4 additions & 0 deletions cpp/dolfinx/la/petsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once

#ifdef HAS_PETSC

#include "Vector.h"
#include "utils.h"
#include <boost/lexical_cast.hpp>
Expand Down Expand Up @@ -504,3 +506,5 @@ class KrylovSolver
};
} // namespace petsc
} // namespace dolfinx::la

#endif
Loading

0 comments on commit e2f55cf

Please sign in to comment.