MPI 并行解上三角矩阵表示的方程组

来源:互联网 发布:软件工程项目报告 编辑:程序博客网 时间:2024/05/16 19:36

最基本的回代法实现

#include "mpi.h"#include <stdio.h>#include <stdlib.h>#define ROOT 0#define TAG 0int 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 xint 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 copyMPI_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 \n",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;}

原创粉丝点击