【发布时间】:2011-09-21 06:56:30
【问题描述】:
MPI_Gatherv 是这样一个 MPI 接口:
int MPI_Gatherv(
void* sendbuf,
int sendcount,
MPI_Datatype sendtype,
void* recvbuf,
int *recvcounts,
int *displs,
MPI_Datatype recvtype,
int root,
MPI_Comm comm)
“recvcounts”的类型是“int *”,这样我们可以分别设置每个进程要接收的项目数;但是我发现这是不可能的:
当recvcounts[i]
当recvcounts[i] > sendcount时,程序会crash,错误信息是这样的:
Fatal error in PMPI_Gatherv: Message truncated, error stack:
PMPI_Gatherv(386).....: MPI_Gatherv failed(sbuf=0012FD34, scount=2, MPI_CHAR, rbuf=0012FCC8, rcnts=0012FB30, displs=0012F998, MPI_CHAR, root=0, MPI_COMM_WORLD) failed
MPIR_Gatherv_impl(199):
MPIR_Gatherv(103).....:
MPIR_Localcopy(332)...: Message truncated; 2 bytes received but buffer size is 1
所以这意味着根必须从每个进程接收固定数量的项目并且参数recvcount没有意义?还是我理解错了?
这是我的代码:
#include <mpi.h>
#include <iostream>
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int n, id;
MPI_Comm_size(MPI_COMM_WORLD, &n);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
char x[100], y[100];
memset(x, '0' + id, sizeof(x));
memset(y, '%', sizeof(y));
int cnts[100], offs[100] = {0};
for (int i = 0; i < n; i++)
{
cnts[i] = i + 1;
if (i > 0)
{
offs[i] = offs[i - 1] + cnts[i - 1];
}
}
MPI_Gatherv(x, 1, MPI_CHAR, y, cnts, offs, MPI_CHAR, 0, MPI_COMM_WORLD); // receive only 1 item from each process
//MPI_Gatherv(x, 2, MPI_CHAR, y, cnts, offs, MPI_CHAR, 0, MPI_COMM_WORLD); // crash
if (id == 0)
{
printf("Gatherv:\n");
for (int i = 0; i < 100; i++)
{
printf("%c ", y[i]);
}
printf("\n");
}
MPI_Finalize();
return 0;
}
【问题讨论】: