MPI并行计算与矩阵1
来源:互联网 发布:人工智能需要哪些技术 编辑:程序博客网 时间:2024/05/18 05:31
#include<stdio.h>#include<mpi.h>#include<math.h>long n, i;double sum,pi,mypi,x,h;int group_size,my_rank;int main(argc,argv)int argc;char *argv[];{int group_size,my_rank;MPI_Status status;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);MPI_Comm_size(MPI_COMM_WORLD,&group_size);n=20;MPI_Bcast(&n,1,MPI_LONG,0,MPI_COMM_WORLD);h=1.0/(double)n;sum=0.0;for(i=my_rank+1;i<=n;i+=group_size){printf("i=%ld,processor=%d\n",i,my_rank);x=h*(i-0.5);sum=sum+4.0/(1.0+x*x);}mypi=h*sum;printf("the rank %ld,calculate mypi=%lf\n",my_rank,mypi);MPI_Reduce(&mypi,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);if(0==my_rank) printf("pi is :%lf\n",pi);fflush(stdout);MPI_Finalize();return 0;}
In order calculate pi
1)Every processor calculate part of pi
Then use MPI_Reduce() to sum all of each part
see the code above
2) If we remove the clause:if(0==my_rank), what will happen?
We see that each clause was run 4 times, and only when my_rank equal to 0, pi=3.14 otherwise it equals to 0.00!
any question?
0 0
- MPI并行计算与矩阵1
- MPI并行计算与矩阵(2)
- 矩阵乘法的MPI并行计算
- MPI多进程并行计算矩阵乘法实现
- 并行计算MPI研究
- 并行计算mpi [ PI ]
- 并行计算 & HPC & MPI
- 并行处理 mpi矩阵乘法
- 并行计算MPI(一):MPI入门
- 并行计算MPI(二):MPI阻塞通信
- 并行计算MPI [ 简单例子 ]
- MPI实现并行计算统计数据
- 并行计算MPI(三):计算π
- 矩阵乘的MPI并行程序
- 矩阵乘的MPI并行程序 MPI主从模式
- MPI并行计算环境的建立
- MPI编程中并行前缀和计算
- ubuntu下搭建MPI并行计算环境
- Netty详解一(理解Netty的设计理念)
- leetcode 2. Add Two Numbers python实现的理解,结点,单链表的应用,大数,进位的问题
- 【数论】Disgruntled Judge, NWERC 2008, UVa12169
- UVA 11292 The Dragon of Loowater
- mysql中创建数据库并授权
- MPI并行计算与矩阵1
- UVA 1421 Archery(二分+维护区间)
- 网站被攻击常见的几种方式
- codeforces 724B Batch Sort
- Java基础复习---字符串
- imread函数、namedWindow函数、imshow函数、imwrite函数
- c小知识
- MyEclipse 2016 CI 1 破解
- 【CF 702E】Analysis of Pathes in Functional Graph(倍增)