* MPI并行程序设计 cpi.c #include mpi.h #include stdio.h #include math.h double f( double ); double f( double a ) { return (4.0 / (1.0 + a*a)); } */214 * MPI并行程序设计 cpi.c int main( int argc, char *argv[]) { int done = 0, n, myid, numprocs, i; double PI25DT = 3.141592653589793238462643; double mypi, pi, h, sum, x; double startwtime = 0.0, endwtime; int namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; MPI_Init(argc,argv); MPI_Comm_size(MPI_COMM_WORLD,numprocs); MPI_Comm_rank(MPI_COMM_WORLD,myid); MPI_Get_processor_name(processor_name,namelen); fprintf(stderr,Process %d on %s\n, myid, processor_name); */214 * MPI并行程序设计 cpi.c n = 0; while (!done) { if (myid == 0) { startwtime = MPI_Wtime(); } MPI_Bcast(n, 1, MPI_INT, 0, MPI_COMM_WORLD); */214 * MPI并行程序设计 cpi.c if (n == 0) done = 1; else { 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; */214 * MPI并行程序设计 cpi.c 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); } } } MPI_Finalize(); return 0; } */214 MPI性能分析与优化 MPI性能分析与优化 并行程序的时间消耗公式: 其中Ttotal、Tcomm和Tserial分别表示程序总用时、通信用时和串行部分用时 通信开销的测试 消息传递的开销一般由两部分组成 : 一部分是通信固有的延迟,这对每个消息来说可以看作是固定的 一部分是消息传递开销,与消息的数据量和网络带宽有关 * * MPI并行程序设计 Blocking Receive int MPI_Recv(void* buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status); OUT buf 接收缓冲区的起始地址 IN count 要接收信息的元素个数 IN datatype 接收信息的数据类型 IN
原创力文档

文档评论(0)