【发布时间】:2011-06-28 20:20:56
【问题描述】:
试图进行“光环/幽灵”行交换,但我被一个死锁难住了(在图片下方的代码 sn-p 中)。
要交换的“光环”行表示为深灰色线(在图片中)和hp[0] 和hp[M-1](在代码中)。
[不能发图片;声望不够。再次用文字说明:hp[0] 和 hp[M-1] 是“晕”行(即要交换的行),而 hp[1] 和 hp[M-2](以及中间的所有行)将被计算。]
为什么这个 sn-p(对 2 个进程有效)会与 3 个以上的进程死锁?
// in-between processes ("P1" and "P2" in the picture;
// one of "P1" and "P2" is of course missing in the case of 3 processes)
if (p > 0 && p < P-1)
{
MPI_Sendrecv(hp[M-2], N, MPI_DOUBLE, p+1, 0,
hp[0], N, MPI_DOUBLE, p-1, 0, MPI_COMM_WORLD, &s);
MPI_Sendrecv(hp[1], N, MPI_DOUBLE, p-1, 1,
hp[M-1], N, MPI_DOUBLE, p+1, 1, MPI_COMM_WORLD, &s);
}
// root process ("P0" in the picture)
else if (p == 0)
{
MPI_Sendrecv(hp[M-2], N, MPI_DOUBLE, p+1, 0,
hp[M-1], N, MPI_DOUBLE, p+1, 1, MPI_COMM_WORLD, &s);
}
// last process ("P3" in the picture)
else
{
MPI_Sendrecv(hp[1], N, MPI_DOUBLE, p-1, 1,
hp[0], N, MPI_DOUBLE, p-1, 0, MPI_COMM_WORLD, &s);
}
平台:带有DeinoMPI GUI 的 Windows XP 有一个按钮 "Show Messages" 其中"Interrupts the running job and prints the current state of the message queues"
嗯,这是一个“当前状态”的例子(当处于死锁状态时):
Rank 0 queues:
Posted receive queue:
rank=2, tag=1, context_id=1(Collective), count=0, dtype=MPI_BYTE
Rank 1 queues:
Posted receive queue:
rank=0, tag=0, context_id=MPI_COMM_WORLD, count=10, dtype=MPI_DOUBLE
Received but unmatched queue:
rank=2, tag=2, context_id=MPI_COMM_WORLD, length=80
rank=2, tag=2, context_id=MPI_COMM_WORLD, length=80
rank=0, tag=1, context_id=1(Collective), length=0
Rank 2 queues:
Posted receive queue:
rank=1, tag=1, context_id=MPI_COMM_WORLD, count=10, dtype=MPI_DOUBLE
为什么有MPI_BYTE 作为数据类型,1(Collective) 作为上下文?为什么Rank 0 在他的接收队列中有rank = 2?!
PS:如果我在问(并且遗漏)一些明显的东西,请原谅我,但我已经阅读了太多 SO 问题,唉,找不到解决方案。这么多,我知道 Jonathan Dursi、High Performance Mark 和 suszterpatt 三人组的 HPC。
更新(完整循环)
循环没有更多内容,所以我可以完整地发布它:它有一些评论MPI_Barriers,因为我随机尝试哪种组合会起作用(谈论“黑匣子”)。因此,除了那些MPI_Barriers(和循环前的MPI_Sccaterv)之外,没有任何其他通信正在进行。出于测试目的,我在循环之后的MPI_Gatherv 之前执行return 0;(因此这也应该没有死锁影响)。
while (1)
{
difference = 0.0;
//MPI_Barrier(MPI_COMM_WORLD);
// in-between processes ("P1" and "P2" in the picture;
// one of "P1" and "P2" is of course missing in the case of 3 processes)
if (p > 0 && p < P-1)
{
MPI_Sendrecv(hp[M-2], N, MPI_DOUBLE, p+1, 0,
hp[0], N, MPI_DOUBLE, p-1, 0, MPI_COMM_WORLD, &s);
MPI_Sendrecv(hp[1], N, MPI_DOUBLE, p-1, 1,
hp[M-1], N, MPI_DOUBLE, p+1, 1, MPI_COMM_WORLD, &s);
}
// root process ("P0" in the picture)
else if (p == 0)
{
MPI_Sendrecv(hp[M-2], N, MPI_DOUBLE, p+1, 0,
hp[M-1], N, MPI_DOUBLE, p+1, 1, MPI_COMM_WORLD, &s);
}
// last process ("P3" in the picture)
else
{
MPI_Sendrecv(hp[1], N, MPI_DOUBLE, p-1, 1,
hp[0], N, MPI_DOUBLE, p-1, 0, MPI_COMM_WORLD, &s);
}
//MPI_Barrier(MPI_COMM_WORLD);
// calculate "hpNEW" for each inner point
for (y = 1; y < M-1; ++y)
for (x = 1; x < N-1; ++x)
{
hpNEW[y][x] = (hp[y][x-1] + hp[y][x+1] + hp[y-1][x] + hp[y+1][x]) / 4.0;
if (fabs( hpNEW[y][x] - hp[y][x] ) > diff)
difference = fabs(hpNEW[y][x] - hp[y][x]);
}
if (difference < EPSILON)
break;
// transfer "hpNEW"'s calculated inner points to "hp" for next iteration
for (y = 1; y < M-1; ++y)
for (x = 1; x < N-1; ++x)
hp[y][x] = hpNEW[y][x];
} // while END
一个进程确实会首先break 退出循环......这会/可能导致死锁(以及其他我不知道的可能情况)? 如果是这样,如何防止它?
关于“奇怪”tags 的另一件事。我刚刚运行了上面的循环,所有MPI_Barriers 都被注释掉了......并得到了这个“奇怪”(有一个tag=4!)消息队列状态:
Rank 0 queues:
Posted receive queue:
rank=1, tag=4, context_id=1(Collective), count=30, dtype=MPI_DOUBLE
Received but unmatched queue:
rank=2, tag=1, context_id=1(Collective), length=0
Rank 1 queues:
Posted receive queue:
rank=0, tag=0, context_id=MPI_COMM_WORLD, count=10, dtype=MPI_DOUBLE
Received but unmatched queue:
rank=2, tag=1, context_id=MPI_COMM_WORLD, length=80
Rank 2 queues:
Posted receive queue:
rank=1, tag=1, context_id=1(Collective), count=0, dtype=MPI_BYTE
【问题讨论】:
标签: c mpi parallel-processing