并行程序设计模型 历安毛子代枚大学 XIDIAN UNIVERSITY 计算圆周率的样本程序 ▣消息传递(Message Passing) ■MPP,COW的自然模型 ■特点 >多线程、异步、多地址空间、显式同步、显式数据映射和负载分 配、显式通信 #define N 1000000 main({ double local,pi,w; long i,processid,numprocess; A:w=1/N; MPI Init(&argc,&argv); 关键字 MPP Massively Parallel Processor;COW,Cluster Of Workstation 6
并行程序设计模型 计算圆周率的样本程序 消息传递(Message Passing) MPP, COW的自然模型 特点 ➢ 多线程、异步、多地址空间、显式同步、显式数据映射和负载分 配、显式通信 6 #define N 1000000 main(){ double local, pi, w; long i, processid, numprocess; A: w=1/N; MPI_Init(&argc, &argv); 关键字 注:MPP, Massively Parallel Processor; COW, Cluster Of Workstation
并行程序设计模型 历忠毛子代枚大学 XIDIAN UNIVERSITY →计算圆周率的样本程序 ▣消息传递(Message Passing,续) //get the current process id MPI Comm rank(MPI COMM WORLD,&processid); //get the number of processes MPI_Comm_size(MPI_COMM_WORLD,numprocess); B:for(i=processid;i<N;i=i+numprocess)M/注意观察processid P:local =(i+0.5)*w; Q:local 4.0/(1.0+local*local); } C:MPI_Reduce(&local,&pi,1,MPI_Double,MPI_SUM,0, MPI COMM WORLD); D:if(processid ==0)printf("pi is %f n",pi*w); 关键字 MPI Finalize(); 7
并行程序设计模型 计算圆周率的样本程序 消息传递(Message Passing,续) //get the current process id MPI_Comm_rank(MPI_COMM_WORLD, &processid); //get the number of processes MPI_Comm_size(MPI_COMM_WORLD, & numprocess); B: for (i=processid; i<N; i= i+numprocess){ //注意观察processid P: local = (i+0.5)*w; Q: local = 4.0/(1.0+local*local); } C: MPI_Reduce(&local, &pi, 1, MPI_Double, MPI_SUM, 0, MPI_COMM_WORLD); D: if(processid == 0) printf(“pi is %f\n”,pi*w); MPI_Finalize(); } 关键字 7