由于有时需要使用到MPI集群来进行迭代运算,就尝试着自己去搭建着MPI集群。不过这也是到去年工作的事情了。我现在凭我的记忆去把它回忆出来。
从零开始,搭建MPI集群是一件很费周折的事情。MPI是一种编程模型,业界有很多种实现,其中开源版本就有好几个版本。对比过openfabrics公司提供的OFED和俄亥俄州立大学的mvapich2,我最终在选型时选取了mvapich2。
服务器系统有 centos 5.8/6.3,安装的版本mvapich2-1.9a。成功编译及安装上mvapich2-1.9a前,我安装了很多依赖包。安装过程如下:
yum install gcc-c++.x86_64 redhat-rpm-config kernel-devel bison flex zlib-devel tcl-devel gcc-gfortran elfutils-libs-x86_64 rpm-build pciutils tcsh tk libtool libibverbs libibverbs-devel mpi-selector.noarch libibumad-devel libibverbs-devel librdmacm-devel mvapich2-psm-devel infinipath-psm-devel cd /opt/mvapich2-1.9a && ./configure --enable-shared --enable-g=all --enable-error-messages=all --prefix=/usr --libdir=/usr/lib64 --with-device=ch3:nemesis && make && make install
Helloword 测试代码:
#include "mpi.h" #include <stdio.h> int main(argc, argv) int argc; char *argv[]; { int myid, np; int namelen; char proc_name[MPI_MAX_PROCESSOR_NAME]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &np); MPI_Get_processor_name(proc_name, &namelen); fprintf(stderr, "Hello, I am proc, %d of %d on %s\n", myid, np, proc_name); MPI_Finalize(); }
Send_Recv 测试代码:
#include "mpi.h" #include <stdio.h> int main( int argc, char *argv[]) { int myid, np; char greeting[199]; int i, t; int partner; int namelen; char proc_name[MPI_MAX_PROCESSOR_NAME]; MPI_Status stat; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &np); MPI_Get_processor_name(proc_name, &namelen); fprintf(stdout, "[fprintf]Hello, I am proc, %d of %d on %s\n", myid, np, proc_name); sprintf(greeting, "[sprintf]Hello, I am proc, %d of %d on %s\n", myid, np, proc_name); if (myid == 1) for (i = 0; i < 10000; i++) t = i; if(myid == 0){ fputs(greeting , stdout); for (partner = 1; partner < np; partner++){ MPI_Recv(greeting, sizeof(greeting), MPI_BYTE, partner, 1, MPI_COMM_WORLD, &stat); fputs(greeting, stdout); } }else{ MPI_Send(greeting, sizeof(greeting) , MPI_BYTE, 0, 1, MPI_COMM_WORLD); } if (myid== 0) printf("That is all for now!\n"); MPI_Finalize(); }