MPI_Bcast广播操作的通信开销测试

29 篇文章 1 订阅

 

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(int argc, char **argv)
{
	int i, myid, other, numprocs;
	double start, end;
	MPI_Status status;
	
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &myid);
	MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
	int N = 65535;
	int *buf = (int *)malloc(sizeof(int) * (N + 1));
	if (buf == NULL) printf("Not enouth memory from %d!\n", myid);
	
	if(myid == 0) 
		for(i = 0; i < N; i ++ ) buf[i] = 1;
	else 
		memset(buf, 0, sizeof(buf));
	if(myid == 0) start = MPI_Wtime();
	MPI_Bcast(buf, N, MPI_INT, 0, MPI_COMM_WORLD);
	if(myid == 0) { 
		end = MPI_Wtime();
		printf("Elapsed time: %f\n", end - start);
	}
		
	if(myid == 0) 
                for(i = 0; i < N; i ++ ) buf[i] = 1;
        else 
                memset(buf, 0, sizeof(buf));
	if(myid == 0) {
		start = MPI_Wtime();
		for (i = 1; i < numprocs; i++) {
			MPI_Send(buf, N, MPI_INT, i, 99, MPI_COMM_WORLD);
		}
		end = MPI_Wtime();
		printf("Elapsed time: %f\n", end - start);
	}
	else 
		MPI_Recv(buf, N, MPI_INT, 0, 99, MPI_COMM_WORLD, &status);
	free(buf);
	buf = NULL;
	//printf("process %d freeing memory.../n",myid);
	
	MPI_Finalize();
}

result:

[root@c0108 zlt]# mpicc comm.c -o comm
[root@c0108 zlt]# mpiexec -n 8 ./comm
Elapsed time: 0.073060
Elapsed time: 0.158653


 

 

从结果上看,MPI的广播操作应该是经过优化的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值