mpi程序实例

求Π值

#include "stdio.h"
#include "mpi.h"
#include<math.h>
#include "stdlib.h"
double f(double);
double f(double x) {
	return (4.0 / (1.0 + x * x));
}
int main(int argc, char** argv)
{
	int done = 0, n, myid, numprocs, i;
	double PI25DT = 3.141592653589792128462643;
	double mypi, pi, h, sum, x;
	double startwtime = 0.0, endwtime;
	int nameelen;
	char processer_name[MPI_MAX_PROCESSOR_NAME];
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &myid);
	MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
	MPI_Get_processor_name(processer_name, &nameelen);
	fprintf(stdout, "process %d of %d on %s\n", myid, numprocs, processer_name);
	n = 0;
	if (myid == 0) {
		printf("please give N=");
		scanf_s("%d", &n);
		startwtime = MPI_Wtime();
	}
	MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
	h = 1.0 / (double)n;
	sum = 0.0;
	for (i = myid + 1; i <= n; i += numprocs) {
		x = h * ((double)i - 0.5);
		sum += f(x);
		mypi = h * sum;
		MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
		if (myid == 0) {
			printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - PI25DT));
			endwtime = MPI_Wtime();
			printf("wall clock time = %f\n", endwtime - startwtime);
			fflush(stdout);
		}
	}
	MPI_Finalize();
	return 0;
}

进程间阻塞通信

#include "stdio.h"
#include "mpi.h"
#include<string.h>
#include<math.h>
#include<cstdlib>
#include "stdlib.h"
int main(int argc, char** argv)
{
	int N, K, myid;
	int sendid, dest;
	char* data = (char*)malloc(25 * sizeof(char));
	int i = 8, n;
	char* temp = (char*)malloc(2 * sizeof(char));
	*temp = 'i';
	strcpy_s(data, 7, "hello!");
	n = strlen(data);
	data[n] = *temp;
	data[n + 1] = 0;
	K = (int)argv[1];
	MPI_Status status;
	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &N);
	MPI_Comm_rank(MPI_COMM_WORLD, &myid);
	dest = (myid + K) % N;
	MPI_Send(data, 1, MPI_INT, dest, 100, MPI_COMM_WORLD);
	MPI_Recv(data, 1, MPI_INT, MPI_ANY_SOURCE, 100, MPI_COMM_WORLD, &status);
	printf("进程%d 发送消息%s 给进程%d ", (int)data[18], data, myid);
	MPI_Finalize();
	return 0;
}

进程间非阻塞通信

#include "stdio.h"
#include "mpi.h"
#include<string.h>
#include<math.h>
#include<cstdlib>
#include "stdlib.h"
int main(int argc, char** argv)
{
	int rank, size;
	MPI_Request r1, r2;
	MPI_Status s;
	int buf[10000], buf2[10000], count, tag1, tag2;
	count = 10000;
	tag1 = 100;
	tag2 = 1000;
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	if (rank == 0) {
		MPI_Isend(buf, count, MPI_INT, 1, tag1, MPI_COMM_WORLD, &r1);
		MPI_Isend(buf2, count, MPI_INT, 1, tag2, MPI_COMM_WORLD, &r2);
		MPI_Wait(&r1, &s);
		MPI_Wait(&r2, &s);
	}
	else
		if (rank == 1) {
			MPI_Irecv(buf2, count, MPI_INT, 0, tag2, MPI_COMM_WORLD, &r2);
			MPI_Irecv(buf, count, MPI_INT, 0, tag1, MPI_COMM_WORLD, &r1);
			MPI_Wait(&r2, &s);
			if (s.MPI_TAG != tag2) {
				printf("error in receive order\n");
			}
			MPI_Wait(&r1, &s);
		}
	MPI_Barrier(MPI_COMM_WORLD);
	if (rank == 0) {
		printf("test completed\n");
	}
	MPI_Finalize();
	return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值