MPI_Gatherv是MPI像這樣的接口:我發現自己無法理解MPI_Gatherv的參數「recvcounts」
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] < sendcount個,根進程將只接收sendcount個項目;
當recvcounts [I]> sendcount個,則程序將崩潰,錯誤消息是某事像這樣:
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;
}
我似乎發現了什麼,我誤會了:聲明「sendcount個= recvcount 「意思是,」sendcount = recvcounts [id]「對於每個過程都是正確的,不僅對於收集過程,我是對的嗎?對, – Sam
是的,沒錯。 –