mpi-cos(x) 的积分

博客展示了代码实现,并配有结果截图,虽未给出具体代码和截图内容,但体现了代码实践与成果呈现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 实现代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mpi.h"
#define PI 3.141593
int main(int argc,char** argv){
	int rank,size;
	double a,b;
	a=0;
	b=PI/2;
	int p=size-1;
	int n=10;
	double h;
	h=(b-a)/p/n;
	
	MPI_Comm comm= MPI_COMM_WORLD;
	MPI_Status status;
	MPI_Init(&argc,&argv);
	MPI_Comm_size(comm,&size);
	MPI_Comm_rank(comm,&rank);

	if(rank== size-1){
		int j=0;
		double fin=0;
		double tem;
		for(j=0;j<size-1;j++){
			MPI_Recv(&tem,1,MPI_DOUBLE,j,99,comm,MPI_STATUS_IGNORE);
			fin=fin+tem;
			//printf("%d:%lf.\n",j,tem);
		}
		printf("cos(%f,%f):%f\n",a,b,fin);
	}
	else{
		int j=0;
		double part=0;
		for(j=0;j<n;j++){
			part= part+ cos(a+(rank*n+j)*h+h/2)*h;
		}
		MPI_Send(&part,1,MPI_DOUBLE,size-1,99,comm);
	}
	MPI_Finalize();
	return 0;
} 

结果截图:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值