OpenMPI安装

下载链接见:

https://www.open-mpi.org/software/ompi/v4.0/

安装方法详见:

https://www.open-mpi.org/faq/?category=building#easy-build

$ gunzip -c openmpi-4.0.0.tar.gz | tar xf -
$ cd openmpi-4.0.0
$ ./configure --prefix=/home/$USERNAME/openmpi

<...lots of output...>

$ make all install -j32

$USERNAME指定安装目录为用户目录,-j32表示使用32核进行编译。

注意,UCX可能存在不匹配,报错如下:

btl_uct_module.c: In function ‘mca_btl_uct_reg_mem’:
btl_uct_module.c:214:22: error: ‘UCT_MD_MEM_ACCESS_REMOTE_GET’ undeclared (first use in this function)
         uct_flags |= UCT_MD_MEM_ACCESS_REMOTE_GET;
                      ^
btl_uct_module.c:214:22: note: each undeclared identifier is reported only once for each function it appears in
btl_uct_module.c:217:22: error: ‘UCT_MD_MEM_ACCESS_REMOTE_PUT’ undeclared (first use in this function)
         uct_flags |= UCT_MD_MEM_ACCESS_REMOTE_PUT;
                      ^
btl_uct_module.c:220:22: error: ‘UCT_MD_MEM_ACCESS_REMOTE_ATOMIC’ undeclared (first use in this function)
         uct_flags |= UCT_MD_MEM_ACCESS_REMOTE_ATOMIC;
                      ^
btl_uct_module.c:225:21: error: ‘UCT_MD_MEM_ACCESS_ALL’ undeclared (first use in this function)
         uct_flags = UCT_MD_MEM_ACCESS_ALL;

此时,需要关闭UCX(见https://github.com/open-mpi/ompi/issues/6640):

$ ./configure --enable-mca-no-build=btl-uct

或者先安装UCX。安装方法见:

https://github.com/openucx/ucx

然后openmpi配置时,需加入--with-ucx=

 

添加环境变量到~/.bashrc文件(注意要添加到文件头部,否则mpi使用的非交互bash不会执行到文件尾部。见[OMPI users] Problem with 'orted: command not found'):

export PATH="/home/$USERNAME/openmpi/bin:$PATH"
export LD_LIBRARY_PATH="/home/$USERNAME/openmpi/lib/:$LD_LIBRARY_PATH"

 

可供测试多节点MPI的源文件:

#include 
#include 
#include 
#include 
#include 

void read_num(long long int *num_point,int my_rank,MPI_Comm comm);
void compute_pi(long long int num_point,long long int* num_in_cycle,long long int* local_num_point,int comm_sz,long long int *total_num_in_cycle,MPI_Comm comm,int my_rank);

int main(int argc,char** argv){
    long long int num_in_cycle,num_point,total_num_in_cycle,local_num_point;
    int my_rank,comm_sz;
	double begin,end;
    MPI_Comm comm;
    MPI_Init(NULL,NULL);//初始化
    comm=MPI_COMM_WORLD;
    MPI_Comm_size(comm,&comm_sz);//得到进程总数
    MPI_Comm_rank(comm,&my_rank);//得到进程编号
    read_num(&num_point,my_rank,comm);//读取输入数据
	begin=MPI_Wtime();
    compute_pi(num_point,&num_in_cycle,&local_num_point,comm_sz,&total_num_in_cycle,comm,my_rank);
	end=MPI_Wtime();
	if(my_rank==0){
		printf("Elapsing time: %fs\n",end-begin);
	}
    MPI_Finalize();
    return 0;
}

void read_num(long long int* num_point,int my_rank,MPI_Comm comm){
    if(my_rank==0){
        printf("please input num in sqaure \n");
        scanf("%lld",num_point);
    }
    MPI_Bcast(num_point,1,MPI_LONG_LONG,0,comm);

}

void compute_pi(long long int num_point,long long int* num_in_cycle,long long int* local_num_point,int comm_sz,long long int *total_num_in_cycle,MPI_Comm comm,int my_rank){
    *num_in_cycle=0;
    *local_num_point=num_point/comm_sz;
    double x,y,distance_squared; 
    srand(time(NULL));
    for(long long int i=0;i< *local_num_point;i++){     
        x=(double)rand()/(double)RAND_MAX;
        x=x*2-1;
        y=(double)rand()/(double)RAND_MAX;
        y=y*2-1;
        distance_squared=x*x+y*y;
        if(distance_squared<=1)
        *num_in_cycle=*num_in_cycle+1;
    }
      MPI_Reduce(num_in_cycle,total_num_in_cycle,1,MPI_LONG_LONG,MPI_SUM,0,comm);
    if(my_rank==0){
        double pi=(double)*total_num_in_cycle/(double)num_point*4;
        printf("the estimate value of pi is %lf\n",pi);
    }
}

host文件内容(注意最后一行一定要有回车换行符):

host1:1  #运行1个进程
host2:1  #运行1个进程

对openmpi而言,识别host文件要用--hostfile,如

mpirun --hostfile servers -n 2 ./pi.out

 

 

参考:

1. error when install install openmpi 3.0.0, variable undeclared

你可能感兴趣的:(Linux,cluster)