为什么 MPI_Gather 会出现缓冲区错误?
Why does MPI_Gather give a buffer error?
所以我想做的是将输入字符串 "HELO" 打印为 "HEELLLOOOO"
到目前为止,我想出了这个代码
#include <mpi.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i=0,rank,size;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
char buf[20]="HELO",a[50],b[50];
//int len = strlen(buf);
MPI_Scatter(buf,1,MPI_CHAR,a,1,MPI_CHAR,0,MPI_COMM_WORLD);
while(i<rank)
{
a[i+1]=a[i];
i++;
}
MPI_Gather(a,rank+1,MPI_CHAR,b,rank+1,MPI_CHAR,0,MPI_COMM_WORLD);
if(rank==0)
{
printf("%s\n",b );
}
MPI_Finalize();
return 0;
}
当我尝试 运行 使用 4 个进程时,出现错误:
Fatal error in PMPI_Gather: Message truncated, error stack:
PMPI_Gather(904)........................:
MPI_Gather(sbuf=0x7fffa92bac30, scount=3, MPI_CHAR,
rbuf=0x7fffa92bac10, rcount=3, MPI_CHAR, root=0, MPI_COMM_WORLD)
failed MPIR_Gather_impl(726)...................:
MPIR_Gather(686)........................:
MPIR_Gather_intra(294)..................:
MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 3 and tag
3 truncated; 4 bytes received but buffer size is 3 Fatal error in
PMPI_Gather: Message truncated, error stack:
PMPI_Gather(904)........................:
MPI_Gather(sbuf=0x7ffd15c31980, scount=1, MPI_CHAR,
rbuf=0x7ffd15c31960, rcount=1, MPI_CHAR, root=0, MPI_COMM_WORLD)
failed MPIR_Gather_impl(726)...................:
MPIR_Gather(686)........................:
MPIR_Gather_intra(230)..................:
MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 1 and tag
3 truncated; 2 bytes received but buffer size is 1
MPIR_Gather_intra(230)..................:
MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 2 and tag
1073741827 truncated; 6 bytes received but buffer size is 2
有人能告诉我哪里错了吗?
此处不能使用MPI_Gather()
。
由于您使用 MPI_CHAR
作为发送和接收数据类型,因此 sendcount
和 recvcount
在 all 等级上必须相同。
MPI_Gatherv()
就是您要找的。
最后但同样重要的是,在调用 printf()
之前不要忘记 NULL 终止 b
所以我想做的是将输入字符串 "HELO" 打印为 "HEELLLOOOO" 到目前为止,我想出了这个代码
#include <mpi.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i=0,rank,size;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
char buf[20]="HELO",a[50],b[50];
//int len = strlen(buf);
MPI_Scatter(buf,1,MPI_CHAR,a,1,MPI_CHAR,0,MPI_COMM_WORLD);
while(i<rank)
{
a[i+1]=a[i];
i++;
}
MPI_Gather(a,rank+1,MPI_CHAR,b,rank+1,MPI_CHAR,0,MPI_COMM_WORLD);
if(rank==0)
{
printf("%s\n",b );
}
MPI_Finalize();
return 0;
}
当我尝试 运行 使用 4 个进程时,出现错误:
Fatal error in PMPI_Gather: Message truncated, error stack: PMPI_Gather(904)........................: MPI_Gather(sbuf=0x7fffa92bac30, scount=3, MPI_CHAR, rbuf=0x7fffa92bac10, rcount=3, MPI_CHAR, root=0, MPI_COMM_WORLD) failed MPIR_Gather_impl(726)...................: MPIR_Gather(686)........................: MPIR_Gather_intra(294)..................: MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 3 and tag 3 truncated; 4 bytes received but buffer size is 3 Fatal error in PMPI_Gather: Message truncated, error stack: PMPI_Gather(904)........................: MPI_Gather(sbuf=0x7ffd15c31980, scount=1, MPI_CHAR, rbuf=0x7ffd15c31960, rcount=1, MPI_CHAR, root=0, MPI_COMM_WORLD) failed MPIR_Gather_impl(726)...................: MPIR_Gather(686)........................: MPIR_Gather_intra(230)..................: MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 1 and tag 3 truncated; 2 bytes received but buffer size is 1 MPIR_Gather_intra(230)..................: MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 2 and tag 1073741827 truncated; 6 bytes received but buffer size is 2
有人能告诉我哪里错了吗?
此处不能使用MPI_Gather()
。
由于您使用 MPI_CHAR
作为发送和接收数据类型,因此 sendcount
和 recvcount
在 all 等级上必须相同。
MPI_Gatherv()
就是您要找的。
最后但同样重要的是,在调用 printf()
b