MPI 加农算法

加农算法计算矩阵相乘 为了实现方便 只实现了矩阵自乘 而且 实际上各线程都可读取整个矩阵(略去输入和分配中的通信过程)

在各线程相互交换数据的过程中 一定要注意避免死锁

#include "mpi.h"
#include 
#include 
#include 

#define TAG 0

static inline void rtoij(int r,int w,int *i,int *j) {
	//rank to i,j
	*i=r/w;
	*j=r-(*i)*w;
}
static inline void ijtor(int i,int j,int *r,int w) {
	//i,j to rank
	*r=i*w+j;
}
static inline int aj(int i,int j,int w) {
	return (j+i)%w;
}
static inline int bi(int i,int j,int w) {
	//initial b distribute
	return aj(i,j,w);
}
int main(int argc,char *argv[]) {
	float m[][2]={{1,2},
			 {3,4}};//the matrix 
	//multiply by itself
	int self,size;
	MPI_Init(&argc,&argv);
	MPI_Comm_rank(MPI_COMM_WORLD,&self);
	MPI_Comm_size(MPI_COMM_WORLD,&size);
	MPI_Request r;
	MPI_Status s;
	int w=sqrt(size);
	int i,j,an,bn,ap,bp;
	rtoij(self,w,&i,&j);
	ijtor(i,(j+w-1)%w,&an,w);//next a process
	ijtor((i+w-1)%w,j,&bn,w);//next b process
	ijtor(i,(j+1)%w,&ap,w);//previous a process
	ijtor((i+1)%w,j,&bp,w);

	float res,a,b,tmp;
	//initialize data distribution
	a=m[i][aj(i,j,w)];
	b=m[bi(i,j,w)][j];
	res=a*b;

	for(int i=0;i


你可能感兴趣的:(做并行)