|
之前看到国外某个学校的老师,为了让学生学习并行计算编程,苦于没有经费购买超级计算机,就去用64个树莓派做了一个并行计算机。样子还是挺好看的,总所周知树莓派是ARM11在架构和指令集上都决定了这个做超级计算机是不成熟的方案。
pcduino是cortex A8架构,用的是armv7指令集,主频是1GHz,用它来做并行计算机性价比是最高的。本人这里将实现过程列出如下:
1,安装gfortran 编译器
sudo apt-get install gfortran
2,下载安装mpi源码
mkdir /home/ubuntu/mpich2
cd ~/ mpich2
到http://www.mpich.org/downloads上去下载源码
wget http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs/1.4.1p1/mpich2-1.4.1p1.tar.gz
3,解压源码构建mpi安装目录
tar xvf mpich2-1.4.1p1.tar.gz
sudo mkdir /home/rpimpi/
sudo mkdir /home/rpimpi/mpich2-install
mkdir /home/ubuntu/mpich_build
cd /home/ubuntu/mpich_build
sudo /home/ubuntu/mpich2/mpich2-1.4.1p1/configure -prefix=/home/rpimpi/mpich2-install
这个要配置很长时间,大概20分钟,去pcduino的论坛看看吧,据说有个创意大赛。
4,编译安装mpi
sudo make && make install
这个差不多也要20分钟。
5,到第4步mpi的可执行程序已经生成了,需要设置一下环境变量
vim ~/.profile 在后面添加
PATH=”$PATH:/home/rpimpi/mpich2-install/bin”
source ~/.profile
可以用
$ which mpicc
$ which mpiexec
两个命令环境变量是不是设置正确。
6,新建一个目录来测试mpi
mkdir mpi_testing
cd mpi_testing
vim mach
在mach文件里面加上自己的IP
然后执行mpiexec -f mach –n 1 hostname
这时候会输出ubuntu
这里 -n 1 表示用1个线程去运行
下面用两个线程来运行mpi源码里面的example
mpiexec -f machi -n 2 ~/mpich_build/examples/cpi
这时候会输出
Process 0 of 2 is on pcduino2
Process 1 of 2 is on pcduino2
pi is approximately 3.1415926544231318, Error is 0.0000000008333387
wall clock time = 0.019410
说明在单个板子的上并行计算已经构建完成,下面会说明怎么用N个pcduino上面实现并行计算
很多的第一反应的是,这不是要在N多板子上都这样装一遍吗,这多麻烦啊,pcduino 531的系统可以很好的解决这个问题。具体的解决办法参见http://www.linksprite.com/cnblog/?p=1147
安装以上办法把现在装好的mpi的pcduino在两个板子复制一下,这样会得到三个都装了mpi的板子。
6,测试两个pcduino组成的并行计算机
在mach里面添加另外一个板子的ip
192.168.2.125
192.168.2.128
由于pcduino在出厂的时候机器名都是一样的,这个需要修改/etc/hostname
分别在里面添加自己的pcduino的名字,这里都用pcduino1,pcduino2…..来命令,一下是测试效果
ubuntu@pcduino2:~/mpi_testing$ mpiexec -f mach -n 2 ~/mpich_build/examples/cpi
Process 1 of 2 is on pcduino2
Process 0 of 2 is on pcduino1
pi is approximately 3.1415926544231318, Error is 0.0000000008333387
wall clock time = 0.003652
从时间上看两个pcduino相对于一个快多了。
7,熟悉mpi的人知道,mpi需要专门的编程方法,这里给出hello world,用三个pcduino测试。
当在测试两个pcduino的时候会遇到一个问题,在运行的时候会提示需要输入ssh的密码,如果每次都需要这样做,并行计算会因为这样的操作变的很慢的,这里给出解决办法,登陆ssh不需要密码,让两个pcduino之间产生信任关系。
首先在本地生产ssh的key
[user1@rh user1]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_rsa):
Created directory ‘/home/user1/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user1/.ssh/id_rsa.
Your public key has been saved in /home/user1/.ssh/id_rsa.pub.
The key fingerprint is:
e0:f0:3b:d3:0a:3d:da:42:01:6a:61:2f:6c:a0:c6:e7 [email protected]
然后将这个key分别装入到其他两个pcduino上面,命令如下
[user1@rh user1]$
cat ~/.ssh/id_rsa.pub | ssh [email protected] “mkdir .ssh;cat >> .ssh/authorized_keys”
cat ~/.ssh/id_rsa.pub | ssh [email protected] “mkdir .ssh;cat >> .ssh/authorized_keys”
这样在再次执行mpi就不需要输入密码了。
helloworld.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);
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;
}
可以看出编程方法跟一般的C语言编程方法有很大区别。
用mpicc -o helloworld helloworld.c编译一下
运行结果如下:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|