Linux下编译安装PETSc

发布于:2025-02-11 ⋅ 阅读:(108) ⋅ 点赞:(0)

本文记录在Linux下编译安装PETSc的流程。

零、环境

操作系统 Ubuntu 22.04.4 LTS
VS Code 1.92.1
Git 2.34.1
GCC 11.4.0
CMake 3.22.1
oneAPI 2024.2.1

一、安装依赖

1.1 安装oneAPI

参见:Get the Intel® oneAPI Base ToolkitGet the Intel® oneAPI HPC Toolkit 

1.2 安装CUDA

参见:NVIDIA CUDA Installation Guide for Linux 

二、编译安装

2.1 下载源码

git clone https://gitlab.com/petsc/petsc.git
cd ./petsc
git checkout v3.22.2

2.2 配置

export PETSC_ARCH=intel-opt-zmo
export I_MPI_CC=icx 
export I_MPI_CXX=icpx
export I_MPI_F90=ifx
python3 ./configure --prefix=~/dev/3rdparty/install/intel-opt-zmo --with-scalar-type=complex --with-debugging=0 --with-cc=mpiicx --with-cxx=mpiicpx --with-fc=mpiifx  --with-mpiexec='/opt/intel/oneapi/mpi/2021.13/bin/mpiexec'  --with-blaslapack-dir=$MKLROOT

2.3 编译安装

make PETSC_DIR=/home/nene/dev/3rdparty/src/petsc PETSC_ARCH=intel-opt-zmo all
make PETSC_DIR=/home/nene/dev/3rdparty/src/petsc PETSC_ARCH=intel-opt-zmo install
make PETSC_DIR=/home/nene/dev/3rdparty/install/intel-opt-zmo PETSC_ARCH="" check

三、PETSc C++ Bindings

3.1 相关开源代码

MFEM
libMesh
DEAL.II
OOFEM
OpenFOAM

3.2 主要组件

附录I: VS Code CMakeUserPresets.json

{
    "version": 4,
    "configurePresets": [
        {
            "name": "linux_intel",
            "inherits": "linux_gcc",
            "displayName": "Intel(R) oneAPI DPC++/C++ Compiler 2024.0.2",
            "description": "Using compilers: C = /opt/intel/oneapi/compiler/latest/bin/icx, CXX = /opt/intel/oneapi/compiler/latest/bin/icpx",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "/opt/intel/oneapi/compiler/latest/bin/icx",
                "CMAKE_CXX_COMPILER": "/opt/intel/oneapi/compiler/latest/bin/icpx"
            },
            "environment": {
                "PETSC_DIR": "~/dev/3rdparty/install",
                "PETSC_ARCH": "intel-opt-zmo"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "linux_intel",
            "description": "",
            "displayName": "",
            "configurePreset": "linux_intel"
        }
    ]
}

附录II: CMake FindPETSc.cmake

# FindPETSc
# ---------
#
# Locates the PETSc library using pkg-config module PETSc
#
# Imported Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines the following IMPORTED target:
#
#  PETSc::PETSc        - the PETSc library
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# This module will set the following variables in your project:
#
#  PETSc_FOUND          - if false, do not try to link to PETSc
#  PETSc_LIBRARIES      - a list of the full paths to all libraries
#  PETSc_INCLUDE_DIRS   - a list of all include directories
#  PETSc_VERSION        - the full version of PETSc MAJOR.MINOR.PATCH
#  PETSc_VERSION_MAJOR  - the MAJOR part of PETSc_VERSION
#  PETSc_VERSION_MINOR  - the MINOR part of PETSc_VERSION
#  PETSc_VERSION_PATCH  - the PATCH part of PETSc_VERSION
#
# Setting these changes the behavior of the search
#  PETSC_DIR - directory in which PETSc resides
#  PETSC_ARCH - build architecture
#
# Author: Frédéric Simonis @fsimonis
# Improver: Nene Lee
# 
# References:
# - https://petsc.org/release/faq/#can-i-use-cmake-to-build-my-own-project-that-depends-on-petsc
# - https://petsc.org/release/manual/getting_started/#sec-writing-application-codes
# - https://github.com/precice/precice/blob/develop/cmake/modules/FindPETSc.cmake
# - https://github.com/oofem/oofem/blob/master/cmake/Modules/FindPETSc.cmake
#

cmake_policy(VERSION 3.10)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# set root of location to find PETSc's pkg-config
set(PETSC $ENV{PETSC_DIR}/$ENV{PETSC_ARCH})
set(ENV{PKG_CONFIG_PATH} ${PETSC}/lib/pkgconfig)

# Remove the lines below if you do not wish to have PETSc determine the compilers
execute_process ( COMMAND pkg-config PETSc --variable=ccompiler COMMAND tr -d '\n' OUTPUT_VARIABLE C_COMPILER)
SET(CMAKE_C_COMPILER ${C_COMPILER})
execute_process ( COMMAND pkg-config PETSc --variable=cxxcompiler COMMAND tr -d '\n' OUTPUT_VARIABLE CXX_COMPILER)
if (CXX_COMPILER)
  SET(CMAKE_CXX_COMPILER ${CXX_COMPILER})
endif (CXX_COMPILER)
execute_process ( COMMAND pkg-config PETSc --variable=fcompiler COMMAND tr -d '\n' OUTPUT_VARIABLE FORTRAN_COMPILER)
if (FORTRAN_COMPILER)
  SET(CMAKE_Fortran_COMPILER ${FORTRAN_COMPILER})
  enable_language(Fortran)
endif (FORTRAN_COMPILER)

find_package(PkgConfig REQUIRED)

if(PKG_CONFIG_FOUND)
  pkg_search_module(PETSc REQUIRED IMPORTED_TARGET PETSc)

  # Extract version parts from the version information
  if(PETSc_VERSION)
    set(_petsc_versions "")
    string(REGEX MATCHALL "[0-9]+" _petsc_versions ${PETSc_VERSION})
    list(GET _petsc_versions 0 _petsc_version_major)
    list(GET _petsc_versions 1 _petsc_version_minor)
    list(GET _petsc_versions 2 _petsc_version_patch)

    set(PETSc_VERSION ${PETSc_VERSION} CACHE STRING "Full version of PETSc")
    set(PETSc_VERSION_MAJOR ${_petsc_version_major} CACHE INTERNAL "Major version of PETSc")
    set(PETSc_VERSION_MINOR ${_petsc_version_minor} CACHE INTERNAL "Minor version of PETSc")
    set(PETSc_VERSION_PATCH ${_petsc_version_patch} CACHE INTERNAL "Patch version of PETSc")

    unset(_petsc_versions)
    unset(_petsc_version_major)
    unset(_petsc_version_minor)
    unset(_petsc_version_patch)
  endif()  
endif()

include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (PETSc
  REQUIRED_VARS PETSc_FOUND PETSc_INCLUDE_DIRS PETSc_LIBRARIES
  VERSION_VAR PETSc_VERSION
  )

if(NOT TARGET PETSc::PETSc)
  add_library(PETSc::PETSc ALIAS PkgConfig::PETSc)
endif()

mark_as_advanced(PETSc_INCLUDE_DIRS PETSc_LIBRARIES PETSc_VERSION_MAJOR PETSc_VERSION_MINOR PETSc_VERSION_PATCH VERSION_VAR PETSc_VERSION)

参考文献

  • Balay S. PETSc/TAO Users Manual, Revision 3.22. Argonne National Laboratory, 2025.
  • Bueler E. PETSc for Partial Differential Equations: Numerical Solutions in C and Python. 2020.
  • MPI Forum. MPI: a message-passing interface standard. International J. Supercomputing Appli-cations, 1994.
  • William Gropp. Using MPI: Portable Parallel Programming with the Message Passing Interface. MIT Press, 1994.

网络资料 

Configuring PETSc https://petsc.org/release/install/install/https://petsc.org/release/install/install/

Get the Intel® oneAPI Base Toolkithttps://www.intel.cn/content/www/cn/zh/developer/tools/oneapi/base-toolkit-download.html?packages=oneapi-toolkit&oneapi-toolkit-os=linux&oneapi-lin=apthttps://www.intel.cn/content/www/cn/zh/developer/tools/oneapi/base-toolkit-download.html?packages=oneapi-toolkit&oneapi-toolkit-os=linux&oneapi-lin=apt

Get the Intel® oneAPI HPC Toolkithttps://www.intel.cn/content/www/cn/zh/developer/tools/oneapi/hpc-toolkit-download.html?packages=hpc-toolkit&hpc-toolkit-os=linux&hpc-toolkit-lin=apthttps://www.intel.cn/content/www/cn/zh/developer/tools/oneapi/hpc-toolkit-download.html?packages=hpc-toolkit&hpc-toolkit-os=linux&hpc-toolkit-lin=apt

NVIDIA CUDA Installation Guide for Linuxhttps://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.htmlhttps://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html


网站公告

今日签到

点亮在社区的每一天
去签到