MPI 笛卡尔拓扑:MPI_Neighbor_alltoall 发送错误数据

Posted

技术标签:

【中文标题】MPI 笛卡尔拓扑:MPI_Neighbor_alltoall 发送错误数据【英文标题】:MPI Cartesian Topology: MPI_Neighbor_alltoall sends wrong data 【发布时间】:2017-12-17 02:30:54 【问题描述】:

我想用 MPI_Neighbor_alltoall 在网格中的节点之间交换天气数据结构。我的程序编译运行了,但是交换的数据不正确。

我的天气数据结构

typedef struct s_weatherdata
        // coordinates
        float lon;
        float lat;
        // main
        int pressure;
        int humidity;
        int temp;
        // wind
        int speed;
        int deg;
weatherdata;

我的创建购物车 sn-p

    // 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);

创建 MPI 天气数据结构

    // 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);

我只需要来自 weatherdata 结构的“速度”和“度”。 我的输出看起来像:

输出

mpicc -o prog_weather weather.c tools_weather.c jsmn/libjsmn.a -lm
mpirun -np 80 prog_weather 5.0 47.0 14.0 55.0 1
dims: [9] [8]
Process rank: 40, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][0]
data0: 64 0
data1: 0 0
data2: 32660 1
data3: 0 29807328
Process rank: 41, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][1]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 42, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][2]
data0: 64 0
data1: 0 0
data2: 32606 1
data3: 0 32248544
Process rank: 43, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][3]
data0: 64 0
data1: 0 0
data2: 32741 1
data3: 0 8250080
Process rank: 44, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][4]
data0: 64 0
data1: 0 0
data2: 32643 1
data3: 0 14066400
Process rank: 45, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][5]
data0: 64 0
data1: 0 0
data2: 32646 1
data3: 0 31564512
Process rank: 46, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][6]
data0: 64 0
data1: 32583 28214128
data2: 0 25712112
data3: 0 0
Process rank: 47, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [5][7]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 48, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][0]
data0: 64 0
data1: 32712 1938112176
data2: 0 25453888
data3: 0 25409520
Process rank: 49, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][1]
data0: 64 0
data1: 32739 172736176
data2: 0 12830016
data3: 0 12785648
Process rank: 50, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][2]
data0: 64 0
data1: 32557 36845344
data2: 32557 1
data3: 0 33977056
Process rank: 51, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][3]
data0: 64 0
data1: 32722 -1205793104
data2: 0 37106912
data3: 0 37062544
Process rank: 52, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][4]
data0: 64 0
data1: 32745 1562607280
data2: 0 35345632
data3: 0 35301264
Process rank: 53, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][5]
data0: 64 0
data1: 32547 -746242384
data2: 0 29332800
data3: 0 29288432
Process rank: 54, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][6]
data0: 64 0
data1: 32763 1657695920
data2: 0 18199776
data3: 0 18155408
Process rank: 55, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [6][7]
data0: 64 0
data1: 32675 99397296
data2: 0 14841152
data3: 0 14796784
Process rank: 56, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][0]
data0: 64 0
data1: 32549 434941616
data2: 0 40248640
data3: 0 40204272
Process rank: 57, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][1]
data0: 64 0
data1: 32676 28069552
data2: 0 34342256
data3: 0 34297888
Process rank: 58, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][2]
data0: 64 0
data1: 32620 -1602105680
data2: 0 37168352
data3: 0 37123984
Process rank: 59, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][3]
data0: 64 0
data1: 32563 17697648
data2: 32563 1
data3: 0 14828256
Process rank: 60, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][4]
data0: 64 0
data1: 32643 19327824
data2: 32643 1
data3: 0 16458464
Process rank: 61, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][5]
data0: 64 0
data1: 32668 25882288
data2: 0 39998784
data3: 0 39954416
Process rank: 62, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][6]
data0: 64 0
data1: 32542 29244144
data2: 32543 1
data3: 0 26374880
Process rank: 63, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [7][7]
data0: 64 0
data1: 32621 478269104
data2: 0 28558656
data3: 0 28514288
Process rank: 64, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][0]
data0: 64 0
data1: 0 33
data2: 0 17102144
data3: 0 17057776
Process rank: 65, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][1]
data0: 64 0
data1: 0 33
data2: 0 20526400
data3: 0 20482032
Process rank: 66, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][2]
data0: 64 0
data1: 32618 22001504
data2: 0 19498480
data3: 0 0
Process rank: 67, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][3]
data0: 64 0
data1: 32633 27649920
data2: 0 25146896
data3: 0 0
Process rank: 68, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][4]
data0: 64 0
data1: 32624 33851264
data2: 0 31348208
data3: 0 0
Process rank: 69, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][5]
data0: 64 0
data1: 0 33
data2: 32756 -1639239568
data3: 0 0
Process rank: 70, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][6]
data0: 64 0
data1: 0 33
data2: 32558 1921785968
data3: 0 0
Process rank: 71, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [8][7]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 37767920
Process rank: 0, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][0]
lb: 0
extent: 28
offsets: 0
offsets: 4
offsets: 8
offsets: 12
offsets: 16
offsets: 20
offsets: 24
data0: 0 0
data1: 32553 13450944
data2: 32553 1
data3: 0 10580704
Process rank: 1, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][1]
data0: 0 0
data1: 0 0
data2: 32684 1
data3: 0 30442208
Process rank: 2, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][2]
data0: 0 0
data1: 0 0
data2: 32631 1
data3: 0 7869152
Process rank: 3, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][3]
data0: 0 0
data1: 0 0
data2: 32594 1
data3: 0 24773344
Process rank: 4, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][4]
data0: 0 0
data1: 0 0
data2: 32514 1
data3: 0 16143072
Process rank: 5, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][5]
data0: 0 0
data1: 0 0
data2: 32584 1
data3: 0 17621728
Process rank: 6, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][6]
data0: 0 0
data1: 0 0
data2: 32632 1
data3: 0 40121056
Process rank: 7, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [0][7]
data0: 0 0
data1: 0 0
data2: 32550 1
data3: 0 39789280
Process rank: 8, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][0]
data0: 64 0
data1: 0 0
data2: 32669 1
data3: 0 38523616
Process rank: 9, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][1]
data0: 64 0
data1: 0 0
data2: 32654 1
data3: 0 11518688
Process rank: 10, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][2]
data0: 64 0
data1: 0 0
data2: 32666 1
data3: 0 21218016
Process rank: 11, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][3]
data0: 64 0
data1: 0 0
data2: 32607 1
data3: 0 18789088
Process rank: 12, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][4]
data0: 64 0
data1: 0 0
data2: 32626 1
data3: 0 25527008
Process rank: 13, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][5]
data0: 64 0
data1: 0 0
data2: 32634 1
data3: 0 17969888
Process rank: 14, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][6]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 15, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [1][7]
data0: 64 0
data1: 0 0
data2: 32509 1
data3: 0 34927328
Process rank: 16, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][0]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 17, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][1]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 18, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][2]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 19, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][3]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 20, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][4]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 21, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][5]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 22, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][6]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 23, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [2][7]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 24, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][0]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 25, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][1]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 26, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][2]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 27, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][3]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 28, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][4]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 29, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][5]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 30, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][6]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 31, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [3][7]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 32, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][0]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 33, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][1]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 34, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][2]
data0: 64 0
data1: 0 0
data2: 32617 1
data3: 0 28934880
Process rank: 35, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][3]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 36, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][4]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 37, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][5]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 38, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][6]
data0: 64 0
data1: 0 0
data2: 0 0
data3: 0 0
Process rank: 39, Total Processes in Comm: 72, Process Name: HP-250-G5-Notebook-PC, Coordinates [4][7]
data0: 64 0
data1: 0 0
data2: 32678 1
data3: 0 39498464

【问题讨论】:

MPI_Type_create_resized 调用是无用的,因为您只是使用MPI_Type_get_extent 为您提供的任何下限和范围。但这不是问题,因为在您的特定情况下没有填充(范围为28,等于最后一个字段的偏移量+sizeof int)。如图所示的输出很难阅读和理解 - 请至少显示等级的笛卡尔坐标。 我提出了一个全新的问题,描述了我程序的核心问题。 ***.com/questions/45091962/… 【参考方案1】:

如果你想使用MPI_Neighbor_alltoall,那么代替

weatherdata **recvdata = malloc(4*sizeof(weatherdata *));

你应该有

weatherdata recvdata[4];

如果您需要使用此类间接,则应改为调查MPI_Neighbor_alltoallw

如果您仍然需要一些帮助,请发帖 mcve 以便您获得帮助。

【讨论】:

recvdata 未在 all-to-all 调用中使用。它是涂料载体。使用data0,并且是连续的。 我看过 mcve,我会记住这一点,这真的很难理解。对不起,如果我做错了,但我提出了一个新问题,我描述了核心问题:***.com/questions/45091962/…

以上是关于MPI 笛卡尔拓扑:MPI_Neighbor_alltoall 发送错误数据的主要内容,如果未能解决你的问题,请参考以下文章

通过使用 MPI 创建 2d 拓扑的矩阵乘法

与不同大小数据类型的 MPI 通信

没有针对MPI_CART_CREATE的特定子例程

ubuntu安装openmpi && ns-3 mpi

OpenMPI 节点和网络拓扑

MPI_Isend/MPI_Irecv 在啥条件下等待其关联的完成调用 (MPI_Wait/MPI_Test) 开始数据传输?