gpt4 book ai didi

c - 为什么 MPI_Gather 会出现缓冲区错误?

转载 作者:行者123 更新时间:2023-11-30 16:35:59 26 4
gpt4 key购买 nike

所以我想做的是将输入字符串“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 作为发送和接收数据类型,因此 sendcountrecvcount所有等级上必须相同。

MPI_Gatherv() 就是您正在寻找的内容。

最后但并非最不重要的一点是,在调用 printf() 之前,不要忘记以 NULL 终止 b

关于c - 为什么 MPI_Gather 会出现缓冲区错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48643732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com