【发布时间】:2014-11-05 14:25:28
【问题描述】:
我按照here的例子添加了一些代码进行测试,但是当我添加新代码时出现了一些奇怪的结果。结果如下所示。它挂在这里,无法继续。
[0]: Before Bcast, buf is 777
[1]: Before Bcast, buf is 32767
[0]: After Bcast, buf is 777
从两个角度来看很奇怪:
code为
if(rank==i) if(i==0)时不会挂机whey
buffer[1]在广播前不为 0。
代码如下:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv) {
int rank;
int buf;
const int root=0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == root) {
buf = 777;
}
printf("[%d]: Before Bcast, buf is %d\n", rank, buf);
// newly added
for(int i=0; i<2; i++)
{
if(rank==i)
{
if(i==1)
MPI_Bcast(&buf, 1, MPI_INT, rank, MPI_COMM_WORLD);
}
}
// end newly added
/* everyone calls bcast, data is taken from root and ends up in everyone's buf */
MPI_Bcast(&buf, 1, MPI_INT, root, MPI_COMM_WORLD);
printf("[%d]: After Bcast, buf is %d\n", rank, buf);
MPI_Finalize();
return 0;
}
我真正想做的是实现功能: 每个处理器运行一部分处理,然后在得到结果后,广播它,其他处理器通过结合接收结果来更新自己的结果。
这里是主要部分代码(原代码来自libsvm)
struct decision_function
{
double *alpha;
double rho;
};
int gate_no = 2;
int p = 0;
int nr_class = 8;
for(int i=0;i<nr_class;i++)
{
for(int j=i+1;j<nr_class;j++)
{
if(((world_rank==i-2*gate_no*(i/(2*gate_no))) && (i%(2*gate_no) < gate_no))||((world_rank==2*gate_no*(i/(2*gate_no)+1)-i-1)&&(i%(2*gate_no) >= gate_no)))
{
// some process for generating f[p] here
....
MPI_Bcast(f[p].alpha, count[i]+count[j], MPI_DOUBLE, world_rank, MPI_COMM_WORLD);
}
++p;
}
}
但是这段代码不起作用,而且我遇到了一些错误。
Fatal error in PMPI_Bcast: Other MPI error, error stack:
PMPI_Bcast(1478)......................: MPI_Bcast(buf=0xcc7b40, count=2340, MPI_DOUBLE, root=1, MPI_COMM_WORLD) failed
MPIR_Bcast_impl(1321).................:
MPIR_Bcast_intra(1119)................:
MPIR_Bcast_scatter_ring_allgather(962):
MPIR_Bcast_binomial(154)..............: message sizes do not match across processes in the collective
【问题讨论】: