linux下MPI编译
1.下载安装首先进行解压文件openmpi-3.1.2.tar.gz。解压进入openmpi-3.1.2,./configure --help可以查看安装配置参数,这里直接./configure --prefix=/usr/local/openmpi-3.1.0,完成参数配置执行make完成编译执行make install 完成安装配置环境变量,执行vi /etc/profile打开配...
·
1.下载安装
- 官网下载文件openmpi-3.1.2.tar.gz(也可以使用编译好的库,下载解压后直接从第五步开始配置环境变量https://download.csdn.net/download/kingskynine/10732466)。
- 解压进入openmpi-3.1.2,
./configure --help
可以查看安装配置参数,这里直接./configure --prefix=/usr/local/openmpi-3.1.2
,完成参数配置 - 执行
make
完成编译 - 执行
make install
完成安装 - 配置环境变量,执行
vi /etc/profile
打开配置文件,在最下面将mpi的lib库和bin库加入到系统变量里export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/openmpi-3.1.2/lib export PATH=$PATH:/usr/local/openmpi-3.1.2/bin
,保存退出。 - 执行
source /etc/profile
重新加载一下动态库 - 执行
mpirun
,查看配置是否成功
2.测试demo
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "mpi.h"
//cJsonPathName模拟输入文件
void master(int iTotalSlave,char *cJsonPathName)
{
printf("start master\n");
int i;
printf("start send/\n");
for (i = 1;i<=iTotalSlave;i++)
{
MPI_Send(&i, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
printf("start recv/\n");
for (i = 1;i<=iTotalSlave;i++)
{
int nReturnNum;
MPI_Status statusOrder;
MPI_Recv(&nReturnNum, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &statusOrder);
printf("nReturnNum:%d\n",nReturnNum);
}
}
void slave(int iID,int iTotalSlave)
{
int nReceiveNum;
MPI_Status statusOrder;
MPI_Recv(&nReceiveNum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &statusOrder);
printf("hello world node%d\n",nReceiveNum);
nReceiveNum+=100;
MPI_Send(&nReceiveNum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
return;
}
int main(int argc, char *argv[])
{
if((argc == 2) && (0 == strcmp(argv[1], "-version")))
{
printf("version 2.0\n" );
return 0;
}
else if(argc < 2)
{
printf("input param\n");
return 1;
}
// 启动MPI
MPI_Init(&argc, &argv);
int iID,iTotalID;
MPI_Comm_rank(MPI_COMM_WORLD, &iID);
MPI_Comm_size(MPI_COMM_WORLD, &iTotalID);
int iTotalSlave=iTotalID-1;
(iID==0)?master(iTotalSlave,argv[1]):slave(iID, iTotalSlave);
MPI_Finalize();
return 0;
}
3.makefile编写
LIBTOOL = libtool
CC = $(LIBTOOL) --mode=compile --tag=CXX g++
LINK = $(LIBTOOL) --mode=link --tag=CXX g++ -Wl,-rpath,./lib
INCS = -I/usr/local/openmpi-3.1.2/include
LIBS = -L/usr/local/openmpi-3.1.2/lib -lmpi
CFLAGS = -Wall -g -O2 -std=c++11 -fPIC -Wl,-rpath,./lib
CPPS = $(wildcard ../src/*.cpp)
OBJS = $(patsubst %.cpp,%.lo,$(CPPS))
BIN = mpitest
all: $(BIN)
$(OBJS):%.lo: %.cpp
$(CC) -o $@ $(CFLAGS) $(INCS) -c $<
$(BIN): $(OBJS)
$(LINK) -o $@ $(CFLAGS) $^ $(LIBS)
clean:
$(LIBTOOL) --mode=clean --tag=CXX rm -f $(OBJS) $(BIN)
4.测试运行
[root@bogon Release]# mpirun --allow-run-as-root -n 4 mpitest 1
start master
start send/
start recv/
hello world node2
hello world node3
nReturnNum:101
nReturnNum:102
nReturnNum:103
hello world node1
更多推荐
已为社区贡献1条内容
所有评论(0)