centos中mpich的安装及使用

https://www.itdaan.com/blog/2017/04/29/4184b453ec6c.html

安装(腾讯云centos 6.5 64位)

  • yum list mpich* 查看有什么版本的mpich包,在此处有版本2的,所以装版本2的
  • yum install mpich2 mpich2-devel mpich2-doc
  • which mpicc 发现找不到该命令
  • find / -name "mpich" 然后会找到mpich的目录,可能会有好几个,有bin的那个就是我们要找的目录
  • cd ~
  • vi .bashrc
  • 在后面加上MPI_ROOT=/usr/lib64/mpich
  • export PATH=$MPI_ROOT/bin:$PATH 这里的目录视具体情况而定
  • source .bashrc 使之生效

使用(新建文件hello.c)

#include <mpi.h>
#include <stdio.h>
#include <math.h>
int main(int argc,char* argv[])
{
    int myid, numprocs;
    int namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];

    MPI_Init(&argc,&argv);/* 初始化并行环境 */
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);/* 当前进程的ID号 */
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);/* 进程的总數 */
    MPI_Get_processor_name(processor_name,&namelen);/* 当前处理器的名称 */

    fprintf(stderr,"Hello World! Process %d of %d on %s\n",
                  myid, numprocs, processor_name);

    MPI_Finalize();/* 结束并行环境 */
    return 0;
}

 

  • 1
mpicc -o hello hello.c
mpirun -np 4 ./helloc

centos7(自测成功)

yum -y mpich*

[root@localhost ~]# yum -y mpich*
Loaded plugins: fastestmirror
No such command: mpich*. Please use /usr/bin/yum --help
[root@localhost ~]# yum list mpich*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Installed Packages
mpich-3.0.x86_64                                                                                                                           3.0.4-10.el7                                                                                                                  @base
mpich-3.0-devel.x86_64                                                                                                                     3.0.4-10.el7                                                                                                                  @base
mpich-3.0-doc.noarch                                                                                                                       3.0.4-10.el7                                                                                                                  @base
Available Packages
mpich-3.0.i686                                                                                                                             3.0.4-10.el7                                                                                                                  base 
mpich-3.0-autoload.x86_64                                                                                                                  3.0.4-10.el7                                                                                                                  base 
mpich-3.0-devel.i686                                                                                                                       3.0.4-10.el7                                                                                                                  base 
mpich-3.2.i686                                                                                                                             3.2-2.el7                                                                                                                     base 
mpich-3.2.x86_64                                                                                                                           3.2-2.el7                                                                                                                     base 
mpich-3.2-autoload.x86_64                                                                                                                  3.2-2.el7                                                                                                                     base 
mpich-3.2-devel.i686                                                                                                                       3.2-2.el7                                                                                                                     base 
mpich-3.2-devel.x86_64                                                                                                                     3.2-2.el7                                                                                                                     base 
mpich-3.2-doc.noarch                                                                                                                       3.2-2.el7                                                                                                                     base

 

yum install mpich3 mpich3-devel mpich3-doc  #不可以,单独运行下面的
yum install mpich-3.0.x86_64 
yum install mpich-3.0-devel.x86_64
yum install mpich-3.0-doc.noarch

  

 


Linux下MPI并行编程环境搭建配置

本文永久链接地址:http://www.lifeba.org/arch/linux_mpi.html

MPI的全称是Message Passing Interface即标准消息传递界面,可以用于并行计算。MPI有多种实现版本,如MPICH, CHIMP以及OPENMPI。这里我们采用MPICH版本。

一、MPICH安装

下载:http://www.mpich.org/static/downloads/3.0.4/mpich-3.0.4.tar.gz

tar -xzvf soft/mpich-3.0.4.tar.gz
cd mpich-3.0.4/
./configure --prefix=/usr/local/mpich
make && make install

安装后加入环境变量/etc/profile,并执行 source /etc/profile

PATH=$PATH:/usr/local/mpich/bin
MANPATH=$MANPATH:/usr/local/mpich/man
export PATH MANPATH

二、单节点测试

复制源代码包下的examples目录到安装目录下

cp -r examples/ /usr/local/mpich

执行

mpirun -np 10 ./examples/cpi

输出结果如下:

Process 0 of 10 is on server150
Process 9 of 10 is on server150
Process 1 of 10 is on server150
Process 4 of 10 is on server150
Process 5 of 10 is on server150
Process 7 of 10 is on server150
Process 2 of 10 is on server150
Process 3 of 10 is on server150
Process 6 of 10 is on server150
Process 8 of 10 is on server150

pi is approximately 3.1415926544231256, Error is 0.0000000008333325
wall clock time = 0.020644

如果我们现在想编译文件: 在/home/houqingdong下执行:  mpicc -o hello  hello.c 这时候会提醒:-bash:mpicc  command not found 这是因为我们还没有配置路径

在命令行下输入: export PATH=/home/houqingdong/mpiexe/bin:$PATH  注意:这里仅仅是暂时的设置路径,在重启后效果会消失,如果想一劳永逸的配置,请google查询

看一下我们配置是否成功可以执行一下  echo $PATH 看一下输出结果中是否有我们的路径

  

三、集群配置

1、集群机器上面需要配置ssh登录权限。参考:Hadoop-0.21.0在linux分布式集群配置  中的ssh配置(密钥无密码登录)部分。

2、复制编译程序到其他机器上面

scp -r mpich server140:/usr/local/
scp -r mpich server151:/usr/local/
scp -r mpich server130:/usr/local/
scp -r mpich server143:/usr/local/

同时在每台机器上面相应加入环境变量中。

3、
在/usr/local/mpich 下新建servers文件,内容如下:

server150:2 #run 2 process
server140:2
server130:2
server143:2
server151:2

执行下面命令,并指定servers文件

mpiexec -n 10 -f servers ./examples/cpi

输出

Process 0 of 10 is on server150
Process 1 of 10 is on server150
Process 4 of 10 is on server140
Process 5 of 10 is on server140
Process 6 of 10 is on server143
Process 7 of 10 is on server143
Process 8 of 10 is on server130
Process 9 of 10 is on server130
Process 2 of 10 is on server151
Process 3 of 10 is on server151
pi is approximately 3.1415926544231256, Error is 0.0000000008333325
wall clock time = 0.018768

四、参考资料

 
 
posted @ 2021-03-03 18:00  古锁阳关  阅读(2150)  评论(0编辑  收藏  举报