先说问题,并行上的错误,具体表现如下:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char** argv) {
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Hello from process %d of %d\n", rank, size);
MPI_Finalize();
return 0;
}
使用mpirun/mpiexec运行结果:
$ mpiexec -n 3 ./hello
Hello from process 0 of 1
Hello from process 0 of 1
Hello from process 0 of 1
$ mpirun -n 3 ./hello
Hello from process 0 of 1
Hello from process 0 of 1
Hello from process 0 of 1
正常结果应该是类似这样:
$ mpirun -n 4 ./hello
Hello from process 0 of 4
Hello from process 1 of 4
Hello from process 2 of 4
Hello from process 3 of 4
根本原因:
这通常发生在mpirun与libmpi.so不匹配时。例如,如果你的应用程序是用Open MPI构建的,但你使用MPICH的mpirun。
Petsc原因:
因为我是用petsc编译运行的,petsc是使用MPICH构建的,而我的mpirun是Open MPI 4.1.2(使用mpirun --version查看)。而之前我使用没有问题是因为ubunu原来使用的就是MPICH
其实就是,在petsc下载之后,一起下载的库(–download-mpich --download-fblaslapack --download-metis=1 --download-parmetis=yes --download-flaslapack=1 --download-cmake)我并没有放到路径PATH中去使用,一直使用的是系统的mpi。
首先科普一下,petsc所有额外下载的安装包在petsc-3.19.2/arch-linux-c-debug/externalpackages中。而这些一起安装的软件的可执行文件并不是单独放在他们文件夹,而是全部在petsc-3.19.2/arch-linux-c-debug/bin中,如上图。我们需要让程序在这些bin文件里面找可执行文件,所以要在~/.bashrc
里面加入这段(XXX指的是petsc的路径)
export PATH=XXX/petsc-x.xx.x/arch-linux-c-debug/bin:$PATH
这里一定要注意不要把路径写错了,还必须把$PATH
放到最后。在第一次操作的时候我把$PATH
放到前面了,导致我的vim命令和source命令都不能用了。将$PATH
放到最后的原因是确保新的路径被添加到现有路径的最后,这样系统会先搜索当前目录,然后依次搜索$PATH
中指定的目录,以查找可执行文件。这样做可以确保你的自定义路径不会覆盖系统默认的路径设置,同时保留了你已经设置好的其他路径。
最后更改了~/.bashrc
后source一下。
这里是mpirun --vetsion 前后对比:
之前
$ mpirun --version
mpirun (Open MPI) 4.1.2
Report bugs to http://www.open-mpi.org/community/help/
之后
$ mpirun --version
HYDRA build details:
Version: 4.1.1
Release Date: Mon Mar 6 14:14:15 CST 2023
CC: gcc -fPIC -Wno-lto-type-mismatch -Wno-stringop-overflow -g -O0
Configure options: '--disable-option-checking' '--prefix=/home/xia/Downloads/petsc-3.19.2/arch-linux-c-debug' 'MAKE=/usr/bin/gmake' '--libdir=/home/xia/Downloads/petsc-3.19.2/arch-linux-c-debug/lib' 'CC=gcc' 'CFLAGS=-fPIC -Wno-lto-type-mismatch -Wno-stringop-overflow -g -O0 -O0' 'AR=/usr/bin/ar' 'ARFLAGS=cr' 'CXX=g++' 'CXXFLAGS=-Wno-lto-type-mismatch -Wno-psabi -g -O0 -std=gnu++20 -fPIC -O0' 'FFLAGS=-fPIC -ffree-line-length-none -ffree-line-length-0 -Wno-lto-type-mismatch -g -O0 -fallow-argument-mismatch -O0' 'FC=gfortran' 'F77=gfortran' 'FCFLAGS=-fPIC -ffree-line-length-none -ffree-line-length-0 -Wno-lto-type-mismatch -g -O0 -fallow-argument-mismatch -O0' '--enable-shared' '--with-pm=hydra' '--disable-java' '--with-hwloc=embedded' '--enable-fast=no' '--enable-error-messages=all' '--with-device=ch3:sock' '--enable-g=meminit' 'PYTHON=/usr/bin/python3' '--disable-maintainer-mode' '--disable-dependency-tracking' '--cache-file=/dev/null' '--srcdir=.' 'LDFLAGS=' 'LIBS=' 'CPPFLAGS= -I/home/xia/Downloads/petsc-3.19.2/arch-linux-c-debug/externalpackages/mpich-4.1.1/src/mpl/include -I/home/xia/Downloads/petsc-3.19.2/arch-linux-c-debug/externalpackages/mpich-4.1.1/modules/json-c -I/home/xia/Downloads/petsc-3.19.2/arch-linux-c-debug/externalpackages/mpich-4.1.1/modules/hwloc/include -D_REENTRANT -I/home/xia/Downloads/petsc-3.19.2/arch-linux-c-debug/externalpackages/mpich-4.1.1/src/mpi/romio/include -I/home/xia/Downloads/petsc-3.19.2/arch-linux-c-debug/externalpackages/mpich-4.1.1/src/pmi/include'
Process Manager: pmi
Launchers available: ssh rsh fork slurm ll lsf sge manual persist
Topology libraries available: hwloc
Resource management kernels available: user slurm ll lsf sge pbs cobalt
Demux engines available: poll select
最后当然也能运行hello程序了