Chinaunix首页 | 论坛 | 博客
  • 博客访问: 44524
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-05 10:35
文章分类

全部博文(20)

文章存档

2016年(16)

2015年(4)

我的朋友

分类: 高性能计算

2015-12-26 19:33:56

用MPI求解梯形积分(上)


版本一:(固定a,b,n值,使用MPI6个基本函数)


#include
#include


double f(double x){
 return x*x;
}


double Trap(double left_side,double right_side,int local_count,double h){
int i;
double estimate,temp;
double left1,right1;
left1=f(left_side);
right1=f(right_side);
estimate=(right1+left1)/2.0;
for(i=1;i
temp=left_side+i*h;
temp=f(temp);
estimate+=temp;
}
estimate=estimate*h;
return estimate;
}


int main(int argc,char *argv[]){
int n=1024,local_n;
int rank,size;
double a=0.0,b=3.0,h;
double local_a,local_b;
double local_sum,total_sum;
int q;
h=(b-a)/n;


MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);


local_n=n/size;
local_a=a+rank*local_n*h;
local_b=local_a+local_n*h;


        local_sum=Trap(local_a,local_b,local_n,h);
if(rank!=0){
        MPI_Send(&local_sum,1,MPI_DOUBLE,0,99,MPI_COMM_WORLD);
}else{
        total_sum=local_sum;
        for(q=1;q
        MPI_Recv(&local_sum,1,MPI_DOUBLE,q,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        total_sum+=local_sum;
}
}


if(rank==0){
        printf("with a=%f b=%f n=%d ,the total_sum=%f\n",a,b,n,total_sum);
}


MPI_Finalize();

}




版本二:(可从键盘键入a,b,n,使用MPI6个基本函数)
#include
#include


void Get_input(int size,int rank,float* a_p,float* b_p,int* n_p){


int dest;
if(rank==0){
        printf("Please Enter a,b,n:\n");
        scanf("%f%f%d",a_p,b_p,n_p);
for(dest=1;dest
        MPI_Send(a_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);
        MPI_Send(b_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);
        MPI_Send(n_p,1,MPI_INT,dest,99,MPI_COMM_WORLD);
}}
else{
        MPI_Recv(a_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(b_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(n_p,1,MPI_INT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);


}
}


float f(float x){
        return x*x;
}


float Trap(float left_side,float right_side,int local_count,float h){
int i;
float right,left;
float estimate,temp;
left=f(left_side);
right=f(right_side);
estimate=(left+right)/2.0;
for(i=1;i
temp=left_side+i*h;
temp=f(temp);
estimate+=temp;
}
estimate=estimate*h;
return estimate;
}


int main(int argc,char*argv[]){


int n,local_n;
int rank,size;
int q;
float a,b,h;
float local_a,local_b;
float local_sum,total_sum;


MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
Get_input(size,rank,&a,&b,&n);
h=(b-a)/n;
local_n=n/size;
local_a=a+rank*local_n*h;
local_b=local_a+local_n*h;
local_sum=Trap(local_a,local_b,local_n,h);
if(rank!=0){
        MPI_Send(&local_sum,1,MPI_FLOAT,0,99,MPI_COMM_WORLD);
}else{
        total_sum=local_sum;
        for(q=1;q
        MPI_Recv(&local_sum,1,MPI_FLOAT,q,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        total_sum+=local_sum;}
}


if(rank==0){
        printf("With a=%f b=%f n=%d Total_sum=%f\n",a,b,n,total_sum);
}


MPI_Finalize();


}




版本三:(使用MPI_Reduce函数)
#include
#include


void Get_input(int size,int rank,float* a_p,float* b_p,int* n_p ){
int dest;
if(rank==0){
        printf("Please Enter a,b,n:\n");
        scanf("%f%f%d",a_p,b_p,n_p);
        for(dest=1;dest
        MPI_Send(a_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);
        MPI_Send(b_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);
        MPI_Send(n_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);
}
}else{
        MPI_Recv(a_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(b_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(n_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}


}


float f(float x){
        return x*x;
}


float Trap(float left_side,float right_side,int local_count,float h){
int i;
float estimate,temp;
float left,right;


left=f(left_side);
right=f(right_side);
estimate=(left+right)/2.0;


for(i=1;i
temp=left_side+i*h;
temp=f(temp);
estimate+=temp;
}
estimate=estimate*h;
return estimate;
}


int main(int argc,char *argv[]){


int n,local_n;
int size,rank;
float a,b,h;
float local_a,local_b;
float local_sum,total_sum=0.0;


MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);


Get_input(size,rank,&a,&b,&n);


h=(b-a)/n;
local_n=n/size;
local_a=a+rank*local_n*h;
local_b=local_a+local_n*h;


local_sum=Trap(local_a,local_b,local_n,h);


MPI_Reduce(&local_sum,&total_sum,1,MPI_FLOAT,MPI_SUM,0,MPI_COMM_WORLD);
if(rank==0){
        printf("With a=%f b=%f n=%d Total_sum=%f\n",a,b,n,total_sum);
}
MPI_Finalize();
}


版本四:(使用MPI_Allreduce函数)
#include
#include


void Get_input(int size,int rank,float* a_p,float* b_p,int* n_p){
int dest;
if(rank==0){
        printf("Please Enter a,b,n:\n");
        scanf("%f%f%d",a_p,b_p,n_p);
        for(dest=1;dest
        MPI_Send(a_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);
        MPI_Send(b_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);
        MPI_Send(n_p,1,MPI_FLOAT,dest,99,MPI_COMM_WORLD);}
}else{
        MPI_Recv(a_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(b_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(n_p,1,MPI_FLOAT,0,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}


}
float f(float x){
return x*x;
}


float Trap(float left_side,float right_side,int local_count,float h){
int i;
float estimate,temp;
float right,left;


left=f(left_side);
right=f(right_side);
estimate=(left+right)/2.0;


for(i=1;i
temp=left_side+i*h;
temp=f(temp);
estimate+=temp;
}
estimate*=h;
return estimate;
}


int main(int argc,char* argv[]){
int n,local_n;
int size,rank;
float a,b,h;
float local_a,local_b;
float local_sum,total_sum;


MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);


Get_input(size,rank,&a,&b,&n);
h=(b-a)/n;
local_n=n/size;
local_a=a+rank*local_n*h;
local_b=local_a+local_n*h;
local_sum=Trap(local_a,local_b,local_n,h);


MPI_Allreduce(&local_sum,&total_sum,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);


if(rank==1){
        printf("With a=%f b=%f n=%d Total_sum=%f\n",a,b,n,total_sum);
}


MPI_Finalize();


}


版本:(使用MPI_Bcast函数)
#include
#include


void Get_input(int size,int rank,float* a_p,float* b_p,int* n_p){


if(rank==0){
        printf("Please Enter a,b,n:\n");
        scanf("%f%f%d",a_p,b_p,n_p);
}
MPI_Bcast(a_p,1,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Bcast(b_p,1,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Bcast(n_p,1,MPI_FLOAT,0,MPI_COMM_WORLD);


}


float f(float x){
return x*x;
}


float Trap(float left_side,float right_side,int local_count,float h){


int i;
float estimate,temp;
float right,left;
float local_sum,total_sum;


right=f(right_side);
left=f(left_side);
estimate=(right+left)/2.0;
for(i=1;i
temp=left_side+i*h;
temp=f(temp);
estimate+=temp;
}
estimate*=h;
return estimate;
}


int main(int argc,char*argv[]){
int n,local_n;
int size,rank;
float a,b,h;
float local_a,local_b;
float local_sum,total_sum;


MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
Get_input(size,rank,&a,&b,&n);
h=(b-a)/n;
local_n=n/size;
local_a=a+rank*local_n*h;
local_b=local_a+local_n*h;
local_sum=Trap(local_a,local_b,local_n,h);
MPI_Allreduce(&local_sum,&total_sum,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
if(rank==1){
        printf("With a=%f b=%f n=%d Total_sum=%f\n",a,b,n,total_sum);
}
MPI_Finalize();

}



版本六:(使用派生数据类型)

#include
#include


void Build_mpi_type(float* a_p,float* b_p,int* n_p,MPI_Datatype* input_mpi_t_p)
{
int array_of_blocklengths[3]={1,1,1};
MPI_Datatype array_of_types[3]={MPI_FLOAT,MPI_FLOAT,MPI_INT};
MPI_Aint a_addr,b_addr,n_addr;
MPI_Aint array_of_displacement[3]={0};
MPI_Get_address(a_p,&a_addr);
MPI_Get_address(b_p,&b_addr);
MPI_Get_address(n_p,&n_addr);
array_of_displacement[1]=b_addr-a_addr;
array_of_displacement[2]=n_addr-a_addr;
MPI_Type_create_struct(3,array_of_blocklengths,array_of_displacement,array_of_types,input_mpi_t_p);
MPI_Type_commit(input_mpi_t_p);
}


void Get_input(int size,int rank,float* a_p,float* b_p,int* n_p)
{
        MPI_Datatype input_mpi_t;
        Build_mpi_type(a_p,b_p,n_p,&input_mpi_t);
        if(rank==0){
printf("Please Enter a,b,n:\n");
scanf("%f%f%d",a_p,b_p,n_p);
}
MPI_Bcast(a_p,1,input_mpi_t,0,MPI_COMM_WORLD);
MPI_Type_free(&input_mpi_t);
}


float f(float x){
return x*x;
}


float Trap(float left_side,float right_side,int local_count,float h){
int i;
float right,left;
float estimate,temp;


left=f(left_side);
right=f(right_side);
estimate=(left+right)/2.0;
for(i=1;i
temp=left_side+i*h;
temp=f(temp);
estimate+=temp;
}
estimate*=h;
return estimate;
}


int main(int argc,char* argv[]){
        int rank,size;
        int n,local_n;
        float a,b,h;
        float local_a,local_b;
        float local_sum,total_sum;


MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
Get_input(size,rank,&a,&b,&n);
h=(b-a)/n;
local_n=n/size;
local_a=a+rank*local_n*h;
local_b=local_a+local_n*h;
local_sum=Trap(local_a,local_b,local_n,h);
MPI_Allreduce(&local_sum,&total_sum,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
if(rank==1){
        printf("With a=%f b=%f n=%d Total_sum=%f\n",a,b,n,total_sum);
}
MPI_Finalize();


}


待续。。。
阅读(1910) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~