MPI并行计算与矩阵(2)
来源:互联网 发布:mac登录不上apple id 编辑:程序博客网 时间:2024/05/18 04:12
1The body of code
#include"mpi.h"
#include"stdio.h"
#include<stdlib.h>
const int rows = 3; //the rows of matrix
const int cols = 2; //the cols of matrix
int main(int argc, char **argv)
{
int rank,size,anstag;
int A[rows][cols],B[cols],C[rows];
int masterpro,buf[cols],ans,cnt;
double starttime,endtime;
double tmp,totaltime;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(size<2){
printf("Error:Too few processors!\n");
MPI_Abort(MPI_COMM_WORLD,99);
}
if (0==rank){
for(int i=0;i<cols;i++)
{
B[i]=i;
for(int j=0;j<rows;j++){
A[j][i]=i+j;
}
}
////output matrix A and vector B ///
printf("\t\tmarix A \n");
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("\t\t vector B\n");
for(int i=0;i<cols;i++)
printf("%d\t",B[i]);
printf("\n");
//bcast the B vector to all slave processors
MPI_Bcast(B,cols,MPI_INT,0,MPI_COMM_WORLD);
//partition the A matrix to all slave processors
for(int i=1;i<size;i++)
{
for(int k=i-1;k<rows;k+=size-1)
{
for(int j=0;j<cols;j++)
{
buf[j]=A[k][j];
}
MPI_Send(buf,cols,MPI_INT,i,k,MPI_COMM_WORLD);
}
}
}
else{
MPI_Bcast(B,cols,MPI_INT,0,MPI_COMM_WORLD);
//every processors receive the part of A matrix, and make Mul Operator with B vector
for(int k= rank-1;k<rows;k+=size-1){
MPI_Recv(buf,cols,MPI_INT,0,k,MPI_COMM_WORLD,&status);
ans=0;
for(int j=0;j<cols;j++)
{
ans=buf[j]*B[j];
}
MPI_Send(&ans,1,MPI_INT,0,k,MPI_COMM_WORLD);
}
}
if(0==rank){
//receive the result from all slave processor
printf("\t\toutput C Vector\n");
for(int i=0;i<rows;i++)
{
MPI_Recv(&ans,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
anstag = status.MPI_TAG;
C[anstag] =ans;
printf("%d\t",C[i]);
}
printf("\n");
}
MPI_Finalize();
return 0;
}
2 Question Part
1) How to understant this part of code? partition the A matrix to all slave processors
for(int i=1;i<size;i++)
{
for(int k=i-1;k<rows;k+=size-1)
{
for(int j=0;j<cols;j++)
{
buf[j]=A[k][j];
}
MPI_Send(buf,cols,MPI_INT,i,k,MPI_COMM_WORLD);
}
}
**************************************************************
A[rows][cols] was divided by each rows.
buf[cols].
MPI_Send(buf,cols,MPI_INT,i,k,MPI_COMM_WORLD);
i ----- the destination of this message
k------the tag of this message
the same processor (the same i),receive the message with diffent k, and each k has the same interval size-1.
So that the same processor will receive the same interval k, ie the equal interval cols of A matrix
2) after MPI_FInalize(), whether all the parreling enviroment stoped ?
MPI_Finalize() means that all the resource that applied by MPI_intial is set free, But it does mean after the MPI_Finalize(),
there is only one processor works!
This question, you can refer to the blog for details
http://bbs.csdn.net/topics/391038020
3 more informations
sometimes it works out like this, So what is wrong with the Code?
- MPI并行计算与矩阵(2)
- MPI并行计算与矩阵1
- 矩阵乘法的MPI并行计算
- 并行计算MPI(一):MPI入门
- MPI多进程并行计算矩阵乘法实现
- 并行计算MPI研究
- 并行计算mpi [ PI ]
- 并行计算 & HPC & MPI
- 并行处理 mpi矩阵乘法
- 并行计算MPI(二):MPI阻塞通信
- 并行计算MPI [ 简单例子 ]
- MPI实现并行计算统计数据
- Windows系统下搭建MPI(并行计算)环境
- Windows 7系统下搭建MPI(并行计算)环境
- Windows系统下搭建MPI(并行计算)环境<转>
- Windows系统下搭建MPI(并行计算)环境
- Windows 7系统下搭建MPI(并行计算)环境
- Windows系统下搭建MPI(并行计算)环境
- Kafka源码深度解析-序列7 -Consumer -coordinator协议与heartbeat实现原理
- HDU 1757 矩阵快速幂
- 用Python做投资-小试牛刀
- 丽兹行项目
- ConvertLUnicodeStringToNullTerminatedString
- MPI并行计算与矩阵(2)
- Java三线程循环有序打印ABC
- Fisher vector 学习。
- View、自定义属性
- Python执行带参数的外部程序
- MATLAB系统内置特殊的函数创建矩阵
- 本地通知的简单实现
- MySQL存储引擎--MyISAM与InnoDB区别
- Java多态