0,源码
hello_openmpi.c
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv) {
printf("1 Hello, world! \n");
MPI_Init(&argc, &argv);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("2 Hello, world! I am process %d.\n", rank);
MPI_Finalize();
return 0;
}
1,使用 mpicc驱动编译程序的方式
编译:
$ /home/hipper/ex_openmpi/local/bin/mpicc hello_openmpi.c -o hello_openmpi_mpicc
运行:
$ /home/hipper/ex_openmpi/local/bin/mpirun -np 3 ./hello_openmpi_mpicc
使用 选项 -### 来查看编译过程,
$ /home/hipper/ex_openmpi/local/bin/mpicc hello_openmpi.c -### -o hello_openmpi_mpicc
可以发现mpicc仅仅是个编译器驱动器,实质上还是使用gcc 的编译器组件 cc1 编译源文件生成 汇编语言程序的
主体功能:
/usr/lib/gcc/x86_64-linux-gnu/12/cc1 hello_openmpi.c -o /tmp/ccbROPVa.s
以及 使用 as 汇编器 生成机器码,主体功能:
as -I /home/hipper/ex_openmpi/local/include --64 -o /tmp/ccffZXLZ.o /tmp/ccJjCEMi.s
全部输出信息是这样的:
(base) hipper@hipper-G21:~/ex_rocHPL/tmp/ex/openmpi_use$ /home/hipper/ex_openmpi/local/bin/mpicc hello_openmpi.c -### -o hello_openmpi_mpicc
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 12.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-12 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-12-ALHxjy/gcc-12-12.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-ALHxjy/gcc-12-12.3.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.3.0 (Ubuntu 12.3.0-1ubuntu1~22.04)
COLLECT_GCC_OPTIONS='-o' 'hello_openmpi_mpicc' '-I' '/home/hipper/ex_openmpi/local/include' '-L/home/hipper/ex_openmpi/local/lib' '-mtune=generic' '-march=x86-64' '-dumpdir' 'hello_openmpi_mpicc-'
/usr/lib/gcc/x86_64-linux-gnu/12/cc1 -quiet -I /home/hipper/ex_openmpi/local/include -imultiarch x86_64-linux-gnu hello_openmpi.c -quiet -dumpdir hello_openmpi_mpicc- -dumpbase hello_openmpi.c -dumpbase-ext .c "-mtune=generic" "-march=x86-64" -fasynchronous-unwind-tables -fstack-protector-strong -Wformat -Wformat-security -fstack-clash-protection -fcf-protection -o /tmp/ccbROPVa.s
COLLECT_GCC_OPTIONS='-o' 'hello_openmpi_mpicc' '-I' '/home/hipper/ex_openmpi/local/include' '-L/home/hipper/ex_openmpi/local/lib' '-mtune=generic' '-march=x86-64' '-dumpdir' 'hello_openmpi_mpicc-'
as -I /home/hipper/ex_openmpi/local/include --64 -o /tmp/ccLC96D7.o /tmp/ccbROPVa.s
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-o' 'hello_openmpi_mpicc' '-I' '/home/hipper/ex_openmpi/local/include' '-L/home/hipper/ex_openmpi/local/lib' '-mtune=generic' '-march=x86-64' '-dumpdir' 'hello_openmpi_mpicc.'
/usr/lib/gcc/x86_64-linux-gnu/12/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/12/liblto_plugin.so "-plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper" "-plugin-opt=-fresolution=/tmp/cciM3xJp.res" "-plugin-opt=-pass-through=-lgcc" "-plugin-opt=-pass-through=-lgcc_s" "-plugin-opt=-pass-through=-lc" "-plugin-opt=-pass-through=-lgcc" "-plugin-opt=-pass-through=-lgcc_s" --build-id --eh-frame-hdr -m elf_x86_64 "--hash-style=gnu" --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro -o hello_openmpi_mpicc /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o -L/home/hipper/ex_openmpi/local/lib -L/usr/lib/gcc/x86_64-linux-gnu/12 -L/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/12/../../.. /tmp/ccLC96D7.o -rpath /home/hipper/ex_openmpi/local/lib --enable-new-dtags -lmpi -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-o' 'hello_openmpi_mpicc' '-I' '/home/hipper/ex_openmpi/local/include' '-L/home/hipper/ex_openmpi/local/lib' '-mtune=generic' '-march=x86-64' '-dumpdir' 'hello_openmpi_mpicc.'
效果图:
2,使用 gcc 编译程序的方式
编译:
gcc hello_openmpi.c -L /home/hipper/ex_openmpi/local/lib/ -lmpi -I /home/hipper/ex_openmpi/local/include/ -Wl,-rpath,/home/hipper/ex_openmpi/local/lib/ -o hello_openmpi_gcc
为了不设置 LD_LIBRARY_PATH,以及版本匹配, 加入了 -Wl,-rpath,/home/hipper/ex_openmpi/local/lib/
运行一样:
$ /home/hipper/ex_openmpi/local/bin/mpirun -np 3 ./hello_openmpi_gcc
如果在编译中加入 gcc -### 我们发现输出信息与mpicc -### 的输出是一样的。这说明给mpicc是对 gcc的驱动而已。
这里输出的第三项是(RUNPATH),存储了 -Wl,-rpath,/home/hipper/ex_openmpi/local/lib/ 这个选项的信息,在运行的时候会去寻找对应的lib文件,即ldd的第二行 libmpi.so 文件: