关于MPI多个并行进程循环 到第二个循环只剩进程号为0的进程

zxtm5 2018-05-27 05:28:57
我想通过多个非0进程进行计算 然后把结果传给主进程(编号为0) 然后由主进程确定最大值再进一步进行绘制操作
然后整个过程是个循环的过程 即这些进程多次重复操作(实际上我是对一个图像上的多个像素点循环操作,即进程循环对多个像素点操作),但是我发现到第二个循环的时候就只有编号为0的进程进入 其他进程不止所踪

我为了保持进程的同步 通过设置 MPI_Barrier(MPI_COMM_WORLD); 使进程同步 ,为了简化代码 我把那些计算部分简化 同时设置一些输出 看看程序的执行情况
具体代码如下:

struct pp{ //定义了一个结构体

int a;
float b;

};

int main(int nArgc, char *papszArgv[])
{

int cp, np;
MPI_Init(&nArgc, &papszArgv);//并行初始化
MPI_Comm_rank(MPI_COMM_WORLD, &cp);//获取进程编号
MPI_Comm_size(MPI_COMM_WORLD, &np);//获取进程数量

float t1 = 0,t2=0,t3=0; //计算总时间
float ts1 = 0,ts2 = 0,tsz=0; //计算传递时间
char* temp = (char *) malloc(2);
t1 = MPI_Wtime();

/*基于像元的图像处理*/

int raster_n =0;
float pixles_weight1;
int tag =0;
int tag2=0;
int tag3=0;

for(;raster_n < 3 && tag==0;raster_n++) //我在假设只有3个像素点
{
//MPI_Status status;
pp pixles_weight[100];
float pw[100];
int N_sample=5;
p=1;

MPI_Barrier(MPI_COMM_WORLD); //为了让第二次循环的时候进程可以同步 结果发现没有用
cout<<"谁开始啦"<<cp<<endl;

if(np>5 ) //我为了找出问题 运行时候只让进程总数为6
{

if(cp<=5 && cp>0 )
{

pixles_weight[cp-1].a=cp-1;
pixles_weight[cp-1].b =cp;
//发送给主进程
MPI_Send(&pixles_weight[cp-1],1,MPI_FLOAT,0,1,MPI_COMM_WORLD);

cout<<"Q1"<<cp-1<<endl;
tag++;
tag2++;
}
}

if (np==1)
{

pw[0] = 0;
pw[1] =1;
pw[2] = 2;
pw[3] = 3;
pw[4] = 4;
tag2=5;
}

MPI_Status status;

if(cp>=0 && cp<=np-1){

cout<<"等待2:"<<cp<<endl;
MPI_Barrier(MPI_COMM_WORLD); //再次让所有都同步

}

if(cp==0 && np>1 && tag>0){ //0进程接收信息
for(int j=0;j<5 ;j++){


ts1 = MPI_Wtime();
MPI_Recv(&pixles_weight[j],1,MPI_FLOAT,MPI_ANY_SOURCE,1,MPI_COMM_WORLD,&status);
pw[pixles_weight[j].a]=pixles_weight[j].b;

cout<<"receive:"<<pixles_weight[j].a<<endl;
ts2=MPI_Wtime();
tsz = tsz+ts2-ts1;


if(j==4)
{
tag=0;
}

}
}

if(cp>=0 && cp<=np-1){

cout<<"等待3:"<<cp<<endl;
MPI_Barrier(MPI_COMM_WORLD);

}

float max_weight=0;
int sample_x = 10;


if(cp==0 ) //0进程开始计算

{
if(tag==0){

cout<<"开始画咯"<<endl;
for(int i = 0;i<5;i++)
{
if(pw[i]>max_weight)
{
max_weight = pw[i];
sample_x = i;
}
}

cout<<"sample:"<<sample_x<<endl;


}

}
cout<<"等待4:"<<cp<<endl;
MPI_Barrier(MPI_COMM_WORLD); //再次让他们同步

cout<<cp<<endl;

}


// printf("bands = %d\n",Bands);

t2 = MPI_Wtime();
printf("consume time = %f,send time = %f",t2-t1,tsz);

MPI_Finalize();
return 0;

}

结果如下图:也就是到第二个循环0进程等不到其他进程 一直卡死在这儿了




所以我要崩溃了 到底为什么呀 恳请各位大神指点一二 我才刚刚接触不到一周 还是渣渣 跪求
...全文
1148 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-05-28
  • 打赏
  • 举报
回复
《Windows核心编程》
赵4老师 2018-05-28
  • 打赏
  • 举报
回复
Multiple Threads in the User Interface http://msdn.microsoft.com/zh-cn/library/ms810439.aspx

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧