Windows下编译PETSC用于Visual Studio开发


PETSc

The Portable, Extensible Toolkit for Scientific Computation (PETSc, pronounced PET-see; the S is silent), is a suite of data structures and routines developed by Argonne National Laboratory for the scalable (parallel) solution of scientific applications modeled by partial differential equations. It employs the Message Passing Interface (MPI) standard for all message-passing communication. (From Wikipedia)

Why Build on Windows

The developers of PETSc says “Are you sure you want to use Microsoft Windows? We recommend using Linux if possible and minimize troubleshooting Microsoft windows related issues. “ Yes, it is a good advice for developers. However, the users of our applications really want to use them on Windows for some reasons. So, to make sure our applications based on PETSc run on Windows and Linux functionally, we need to build PETSc on Windows.

Moreover, we need PETSc to work with Visual Studio (VS) too. Although it maybe tedious to use VS to develop some projects in comparison with the Makefile system, the native executive file generated by VS is much more efficient than that generated by Cygwin+Makefile.

How to Build PETSc on Windows for Visual Studio

Actually, there are several configure files in the PETSc folder (petsc/config/examples/misc) that can be used to build PETSc on Windows. Also PETSc provides the win32fe.exe (petsc/lib/petsc/bin/win32fe/win32fe.exe) that can be used to interact with VS compiler. With these tools, to build PETSc on windows for VS, some tricks are needed. The following are the steps that I used to build it successfully on WIndows. I am not an expert on PETSc, please let me know if there were some better methods that will do the job.

1. Install Microsoft MPI

Please go to the Microsoft website and download MPI installers. We need both msmpisdk.msi and msmpisetup.exe for building PETSc. (Intel MPI also works, download and install it if you want. Or if you just want use the non-MPI version, skip this step.) After installing the Microsoft MPI, personally, I copied all the MPI files to a path with a name without spaces, for example: “d:/MSMPI”, because the MS-MPI is installed to some path like “c:\Program Files\Microsoft MPI" which is not good for Linux. Specifically, the files in “c:\Program Files\Microsoft MPI” and “c:\Program Files (x86)\Microsoft SDKs\MPI" are copied to “d:/MSMPI”.

2. Install MSYS2 on Windows

The official site recommends Cygwin, but I already have MSYS2 installed on my Windows 10. I think the steps are similar for Cygwin.

To install MSYS2, just go to the MSYS2 offical webiste and install the windows version of MSYS2. Do remember the architecture of MSYS2 your installed, x64 or x86. Personally, I prefer x64 which is now the mostly used architecture.

After installing MSYS2, you may want to use local mirrors for package installation (just for Chinese users ?). Specifically for Chinese users, please refer to this link to set the mirrors. Now we need to update some packages and install some packages for development (some packages are optional, but python is required):

# Update everything using
pacman -Syu

# Installing gcc using MSYS2
pacman -S base-devel
pacman -S gcc
pacman -S vim
pacman -S cmake

3. Open the Visual Studio Prompt

The step is very important. DO NOT use the common CMD window. Do use the prompt window provided by visual studio which provides you the develop environment (so the cl.exe can be detected). I have Visual Studio 2015 installed on my Windows 10. There are several prompt choices provided by VS 2015 as shown in the figure 1.

Vs prompts

Figure 1. The prompts provided by Visual Studio 2015

Be careful to choose the prompt, if you want to build a x64 version of PETSc, use the VS2015 x64 Native Tools Command Prompt. The Developer Command Prompt for VS2015 is for x86. MAKE SURE the prompt is computable with the architecture of your MPI and MSYS2.

4. Entering the MSYS2 environment

In the VS prompt you selected, entering the MSYS2 environment by typing:

# the path of my MSYS2 installation is D:\SetupPrograms\MSYS2
# change it according to your installation
"D:\SetupPrograms\MSYS2\usr\bin\bash.exe"

# Recovery the MSYS2's PATH
export PATH=/usr/local/bin:/usr/bin:/bin:/opt/bin:$PATH

All the steps below will be operated in this prompt window.

5. Build PETSc

Download PETSc source code using git in the prompt window:

# goto your work dir
cd /f/Github

# clone from git, install git with pacman if you have not
git clone https://github.com/petsc/petsc.git

# in config/configure.py, if you are using MSYS2 instead of Cygwin, comment out the line:
# chkdosfiles()

# goto the PETSc dir
cd /f/Github/petsc
export PETSC_DIR=`pwd`

Now create a python script in /f/Github/petsc/config with name arch-win-msys2-vs2015-msmpi.py. put the following codes in the script (you can change some options for your case):

#!/usr/bin/env python

configure_options = [
  # Use Visual studio cl.exe
  # Use Microsoft MPI
  '--with-cc=win32fe cl',
  '--with-fc=0',
  '--with-cxx=win32fe cl',
  '--doCleanup=1',
  '--useThreads=0',
  '--with-debugging=1',
  '--with-64-bit-indices=1',
  'CXXFLAGS=-DMPICH_SKIP_MPICXX -MT -GR -EHsc',
  '--CC_LINKER_FLAGS=msvcrt.lib',
  '--CXX_LINKER_FLAGS=msvcrt.lib',
  '--with-cuda=0',
  '--with-mpi=1',
  '--with-mpi-include=[/d/MSMPI/Include/,/d/MSMPI/Include/x64]',
  '--with-mpi-lib=[/d/MSMPI/Lib/x64/msmpifec.lib,/d/MSMPI/Lib/x64/msmpi.lib]',
  '--with-mpiexec=/d/MSMPI/Bin/mpiexec.exe',
  '--download-f2cblaslapack=1',
  '--with-shared-libraries=0',
  '--ignore-cygwin-link',
  ]

if __name__ == '__main__':
  import sys,os
  sys.path.insert(0,os.path.abspath('config'))
  import configure
  configure.petsc_configure(configure_options)

Now in the prompt window, type python config/arch-win-msys2-vs2015-msmpi.py to configure PETSc for windows with visual studio. After configuration successfully, you will see a message like make PETSC_DIR=/f/ithub/petsc PETSC_ARCH=arch-win-msys2-vs2015-msmpi all , use this command to make PETSc.

Now the PETSc library should be generated in path /f/ithub/petsc/arch-win-msys2-vs2015-msmpi/lib;