MPI计算Pi

Broadcast

点对点

#include<stdio.h>
#include<math.h>
#include "mpi.h"
double f(double a)
{
    return (4.0 / (1.0 + a*a));
}
int main(int argc,char* argv[])
{
    int i,n=1000000000,myid,numprocs;
    double pi=0.0,h,a,respective=0.0,startTime,endTime;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
	MPI_Status status;
	h = 1.0/(double)n;
	startTime = MPI_Wtime();
    if(myid!=0)
    {	
       for(i=myid+1;i<=n;i+=(numprocs-1)){
		   a=h*((double)i-0.5);
		   respective+=f(a);
	   }
	    respective = h * respective;
		MPI_Send(&respective,1,MPI_DOUBLE,0,99,MPI_COMM_WORLD);
    }else{/*myid==0*/
	for(i=1;i<numprocs;i++){
		MPI_Recv(&respective,1,MPI_DOUBLE,i,99,MPI_COMM_WORLD,&status);
		pi+=respective;
	}
		 endTime = MPI_Wtime();
		 printf("\nPI is %f\nTime is : %f\n",pi,endTime - startTime);}
	
    MPI_Finalize();
    return 0;
}

reduce

#include<stdio.h>
#include<math.h>
#include "mpi.h"
double func(double a)
{
    return (4.0 / (1.0 + a*a));
}
int main(int argc,char* argv[])
{
    int n=1000000000,myid,numprocs;
    double pi,h,a,respective,startTime,endTime;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    if(myid==0)
    {
        startTime=MPI_Wtime();
    }
    h = 1.0/(double)n;
    respective = 0.0;
    for(int i=myid+1;i<=n;i+=numprocs)//线程数目
    {
        a = h * ((double)i - 0.5);
        respective += func(a);
    }
    respective = h * respective;
    MPI_Reduce(&respective,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
    if(myid==0)
    {
        endTime = MPI_Wtime();
        printf("\nPI is %f\nTime is : %f\n",pi,endTime - startTime);
    }
    MPI_Finalize();
    return 0;
}

gatherysk

static long num_steps = 100000000;
double step;
void main(int argc, char *argv[])
{
        double pi=0.0;
        step = 1.0 / (double)num_steps;
        double start = MPI_Wtime();
        MPI_Init(NULL, NULL);
        int id,size,i,j,k;
        MPI_Comm_rank (MPI_COMM_WORLD, &id);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
        double x,sum,pi_temp,pi_buffer[size];
        for (i = id,sum = 0.0;i <= num_steps;i = i+size) {
                x = (i - 0.5) * step;
                sum += 4.0 / (1.0 + x * x);
        }
        pi_temp = sum * step;
        MPI_Gather(&pi_temp, 1, MPI_DOUBLE, pi_buffer, 1, MPI_DOUBLE, 0, MPI_COM                                                                                       M_WORLD);
        if (id == 0){
                for (j=0;j<size;j++){
                        pi += pi_buffer[k];
                }
                printf("pi value is: %f\n", pi);
                double end = MPI_Wtime();
                printf("runtime is: %f\n", end - start);
                printf("process number is: %d\n", size);
        }
        MPI_Finalize();
}

broadcastysk

#include <mpi.h>
#include <stdio.h>
static long num_steps = 100000000;
double step;
void main(int argc, char *argv[])
{
        step = 1.0 / (double)num_steps;
        double start = MPI_Wtime();
        MPI_Init(NULL, NULL);
        int id,size,i,j,k;
        MPI_Comm_rank (MPI_COMM_WORLD, &id);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
        double x,sum,pi_temp[size],pi=0.0;
        for (i = id,sum = 0.0;i <= num_steps;i = i+size) {
                x = (i - 0.5) * step;
                sum += 4.0 / (1.0 + x * x);
        }
        pi_temp[id] = sum * step;
        for (j=1;j<size;j++){
                MPI_Bcast(&pi_temp[j], 1, MPI_DOUBLE, j, MPI_COMM_WORLD);
        }
        if (id == 0){
                for (k=0;k<size;k++){
                        pi += pi_temp[k];
                }
                printf("pi value is: %f\n", pi);
                double end = MPI_Wtime();
                printf("runtime is: %f\n", end - start);
                printf("process number is: %d\n", size);
        }
        MPI_Finalize();
}

reduceysk

#include <mpi.h>
#include <stdio.h>
static long num_steps = 100000000;
double step;
void main(int argc, char *argv[])
{
        double pi=0.0;
        step = 1.0 / (double)num_steps;
        double start = MPI_Wtime();
        MPI_Init(NULL, NULL);
        int id,size,i,j,k;
        MPI_Comm_rank (MPI_COMM_WORLD, &id);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
        double x,sum,pi_temp;
        for (i = id,sum = 0.0;i <= num_steps;i = i+size) {
                x = (i - 0.5) * step;
                sum += 4.0 / (1.0 + x * x);
        }
        pi_temp = sum * step;
        MPI_Reduce(&pi_temp, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
        if (id == 0){
                printf("pi value is: %f\n", pi);
                double end = MPI_Wtime();
                printf("runtime is: %f\n", end - start);
                printf("process number is: %d\n", size);
        }
        MPI_Finalize();
}

point2pointysk

#include <mpi.h>
#include <stdio.h>
static long num_steps = 100000000;
double step;
void main(int argc, char *argv[])
{
        double pi;
        step = 1.0 / (double)num_steps;
        double start = MPI_Wtime();
        MPI_Init(NULL, NULL);
        double x,sum,pi_temp;
        int id,size,i,j;
        MPI_Comm_rank (MPI_COMM_WORLD, &id);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
        for (i = id,sum = 0.0;i <= num_steps;i = i+size) {
                x = (i - 0.5) * step;
                sum += 4.0 / (1.0 + x * x);
        }
        pi_temp = sum * step;
        if (id != 0) {
                MPI_Send(&pi_temp, 1, MPI_DOUBLE, 0, id, MPI_COMM_WORLD);
        }
        else {
                pi = pi_temp;
                for (j=1;j<size;j++) {
                        MPI_Recv(&pi_temp, 1, MPI_DOUBLE, j, j, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                        pi += pi_temp;
                }
                printf("pi value is: %f\n", pi);
                double end = MPI_Wtime();
                printf("runtime is: %f\n", end - start);
                printf("process number is: %d\n", size);
        }
        MPI_Finalize();
}
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值