我想用MPI_Neighbor_alltoall在网格中的节点之间交换weatherdata结构。我的程序编译并运行,但交换的数据不正确。
我的weatherdata结构
worker()
我的创建购物车代码段
typedef struct s_weatherdata{
// coordinates
float lon;
float lat;
// main
int pressure;
int humidity;
int temp;
// wind
int speed;
int deg;
}weatherdata;
创建MPI weatherdata struct
// Create Dimensions
MPI_Dims_create(size, ndims, dims);
// Create Cartesian
MPI_Cart_create(mycomm, ndims, dims, periods, 1, &cart_comm);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int len;
MPI_Get_processor_name(processor_name, &len);
// Get coordinates
MPI_Cart_coords(cart_comm, rank, ndims, coord);
我所需要的只是'速度'和' deg'来自weatherdata结构。 我的输出看起来像:
输出
// create a type for weatherdata
const int nitems = 7;
int blocklengths[7] = {1,1,1,1,1,1,1};
MPI_Datatype types[7] = {MPI_FLOAT, MPI_FLOAT, MPI_INT, MPI_INT, MPI_INT, MPI_INT, MPI_INT};
MPI_Datatype tmp_mpi_weatherdata, mpi_weatherdata;
MPI_Aint offsets[7];
MPI_Aint lb, extent;
offsets[0] = offsetof(weatherdata, lon);
offsets[1] = offsetof(weatherdata, lat);
offsets[2] = offsetof(weatherdata, pressure);
offsets[3] = offsetof(weatherdata, humidity);
offsets[4] = offsetof(weatherdata, temp);
offsets[5] = offsetof(weatherdata, speed);
offsets[6] = offsetof(weatherdata, deg);
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &tmp_mpi_weatherdata);
MPI_Type_get_extent(tmp_mpi_weatherdata, &lb, &extent);
MPI_Type_create_resized(tmp_mpi_weatherdata, lb, extent, &mpi_weatherdata);
MPI_Type_commit(&mpi_weatherdata);
weatherdata **recvdata = malloc(4*sizeof(weatherdata *));
weatherdata *data0 = malloc(4*(sizeof(weatherdata)));
weatherdata *data1 = data0+sizeof(weatherdata);
weatherdata *data2 = data1+sizeof(weatherdata);
weatherdata *data3 = data2+sizeof(weatherdata);
recvdata[0]=data0;
recvdata[1]=data1;
recvdata[2]=data2;
recvdata[3]=data3;
MPI_Neighbor_alltoall(my_data , 1, mpi_weatherdata, data0, 1, mpi_weatherdata, cart_comm);
printf("data0: %i %i\n", recvdata[0]->speed, recvdata[0]->deg);
printf("data1: %i %i\n", recvdata[1]->speed, recvdata[1]->deg);
printf("data2: %i %i\n", recvdata[2]->speed, recvdata[2]->deg);
printf("data3: %i %i\n", recvdata[3]->speed, recvdata[3]->deg);
答案 0 :(得分:2)
如果您想使用MPI_Neighbor_alltoall
,则代替
weatherdata **recvdata = malloc(4*sizeof(weatherdata *));
你应该
weatherdata recvdata[4];
如果您需要使用此类间接,则应调查MPI_Neighbor_alltoallw
。
如果您仍需要一些帮助,请发布mcve以便获得帮助。