ubuntu16.04一步一步安装配置mpich

mpi在高性能并行计算中具备重要的地位,做为消息通讯最为流行的编程模型,MPI并行库虽然有着众多缺陷,可是也知足大部分节点间通讯的功能需求。搭建服务集群主要使用linux服务器并安装配置mpi环境,大部分能搜到的配置方法要么年代久远阅读困难、要么未能详细讲清楚配置的过程。 笔者在此以ubuntu16.04为例,主要根据官方的README文件来完成mpich-3.2的安装配置。php


1. 安装前的准备工做linux

须要安装下载的东西:
在官网下载mpich-3.2(stable release)放置到/home/[username]/donwload目录下([username]指某个特定的用户名),运行如下命令解压。shell

$ tar xzf mpich-3.2.tar.gz

检查是否有C/C++/fortran编译器并搞清楚所用系统的shell(默认为bash)编程

$ gcc --version
$ g++ --version
$ gfortran --version
$ echo $SHELL

若是上述均检查经过了,则开始下一部分,不然应该使用sudo apt-get install XXX来安装好编译器。ubuntu


2. 安装配置过程ruby

进入解压文件:bash

$ cd mpich-3.2

建立mpi的安装路径,咱们统一使用/home/[username]/mpich-install文件夹来存放安装文件。服务器

$ mkdir /home/XXX/mpich-install

配置mpich-3.2中的configue文件。因为不一样的linux系统使用的shell不一样,所以配置安装指令不尽相同。主要有csh和bash两种类型的shell,ubuntu默认使用bash,下面的命令只有bash下的,csh下的命令请阅读mpi-3.2中的README文件。app

指定安装文件夹dom

$ ./configure --prefix=/home/<USERNAME>/mpich-install 2>&1 | tee c.txt

构建make文件

$ sudo make 2>&1 | tee m.txt

若是上一步构建出错,运行如下命令从新构建

$ make clean
$ make V=1 2>&1 | tee m.txt

运行makefile进行安装

$ sudo make install 2>&1 | tee mi.txt

将其加入bash环境变量

$ PATH=/home/<USERNAME>/mpich-install/bin:$PATH ; export PATH

检查mpicc和mpiexec两个程序是否加入到环境变量

$ which mpicc
$ which mpiexec

tips:为了在多机上运行MPI,须要保证全部机器上的安装路径、配置保持一致。


3. 启动运行过程

MPICH使用一个叫作process manager的管理器来启动MPI程序,启动程序使用刚才提到的mpiexec
来启动。在mpich-3.2中有一个examples文件夹,里面有有些例程能够在你不懂MPI编程以前先体验一下。其中一个典型的CPI程序是用来计算π的值、
代码以下:

/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/* * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. */

#include "mpi.h"
#include <stdio.h>
#include <math.h>

double f(double);

double f(double a)
{
    return (4.0 / (1.0 + a*a));
}

int main(int argc,char *argv[])
{
    int    n, myid, numprocs, i;
    double PI25DT = 3.141592653589793238462643;
    double mypi, pi, h, sum, x;
    double startwtime = 0.0, endwtime;
    int    namelen;
    char   processor_name[MPI_MAX_PROCESSOR_NAME];

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    MPI_Get_processor_name(processor_name,&namelen);

    fprintf(stdout,"Process %d of %d is on %s\n",
        myid, numprocs, processor_name);
    fflush(stdout);

    n = 10000;          /* default # of rectangles */
    if (myid == 0)
    startwtime = MPI_Wtime();

    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

    h   = 1.0 / (double) n;
    sum = 0.0;
    /* A slightly better approach starts from large i and works back */
    for (i = myid + 1; i <= n; i += numprocs)
    {
    x = h * ((double)i - 0.5);
    sum += f(x);
    }
    mypi = h * sum;

    MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

    if (myid == 0) {
    endwtime = MPI_Wtime();
    printf("pi is approximately %.16f, Error is %.16f\n",
           pi, fabs(pi - PI25DT));
    printf("wall clock time = %f\n", endwtime-startwtime);         
    fflush(stdout);
    }

    MPI_Finalize();
    return 0;
}

运行这个MPI程序的方法:

本地机上运行时须要指定进程数量 [number]

$ mpiexec -n <number> ./examples/cpi

指定[number]=4时运行结果以下:

$ mpiexec -n 4  ./examples/cpi
Process 0 of 4 is on nianjunzou-NX581
Process 1 of 4 is on nianjunzou-NX581
Process 2 of 4 is on nianjunzou-NX581
Process 3 of 4 is on nianjunzou-NX581
pi is approximately 3.1415926544231239, Error is 0.0000000008333307
wall clock time = 0.000320

多机上运行时除了进程数量n之外还须要写machinefile

$ mpiexec -f machinefile -n <number> ./examples/cpi

machinefile的形式以下,’host1’, ‘host2’, ‘host3’ and ‘host4’是你想运行的全部机器的各自hostname。’:2’, ‘:4’, ‘:1’描述了各个机器上想运行的进程数量,该数量默认为1。

host1
host2:2
host3:4   # Random comments
host4:1

更多的运行命令请访问:
http://wiki.mpich.org/mpich/index.php/Using_the_Hydra_Process_Manager

相关文章
相关标签/搜索