易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
MPI 并行解上三角矩阵表示的方程组
MPI最基本的回代法实现
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define ROOT 0
#define TAG 0
int
main(
int
argc,
char
*argv[]) {
float
matrix[][4]={{2,3,4,5},
//the upper trangular matrix representing a equation set
{0,2,3,4},
{0,0,2,3}};
int
xcnt=3;
//cout of x
int
self,size,tag=0;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&self);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Request r;
MPI_Status s;
MPI_Datatype MPI_VEC;
MPI_Type_vector(xcnt+1,1,1,MPI_FLOAT,&MPI_VEC);
MPI_Type_commit(&MPI_VEC);
float
* equation=(
float
*)malloc((xcnt+1)*
sizeof
(
float
));
float
xs,xr;
if
(0==self) {
//send each process the correspond equation
//parellel send and copy
MPI_Issend(matrix[1],1,MPI_VEC,1,TAG,MPI_COMM_WORLD,&r);
for
(
int
i=0;i<=xcnt;++i) {
equation[i]=matrix[0][i];
}
MPI_Wait(&r,&s);
for
(
int
i=2;i<size;++i) {
MPI_Ssend(matrix[i],1,MPI_VEC,i,TAG,MPI_COMM_WORLD);
}
}
else
{
MPI_Recv(equation,1,MPI_VEC,ROOT,TAG,MPI_COMM_WORLD,&s);
}
for
(
int
i=xcnt-1;i>=0;--i) {
if
(i==self) {
xs=equation[xcnt]/equation[i];
printf(
"x%d = %f "
,self,xs);
MPI_Bcast(&xs,1,MPI_FLOAT,i,MPI_COMM_WORLD);
}
else
{
MPI_Bcast(&xs,1,MPI_FLOAT,i,MPI_COMM_WORLD);
if
(i>self) {
equation[xcnt]-=equation[i]*xs;
}
}
}
free(equation);
MPI_Finalize();
return
0;
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图