matrix5


//http://www.linuxidc.com/Linux/2012-08/67662.htm
#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 \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;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值