c - 在 C 中的 MPI 中如何创建结构的结构并将其发送到多个进程

标签 c struct mpi

所以我有这个结构

typedef struct{
     float x;
     float y;
     int centroid;
}point;

我也有这个结构

typedef struct{
     int csize;//the current size
     int tsize;//the total size
     point * data;//the data carried
}ArrayList;

这些第二个结构在 C 中形成了一个动态增长的数组(实现了动态增长的功能并且工作正常)。 我究竟如何创建一个结构并使用 C 中的 MPI 发送它? 我查看了其他帖子,例如 struct serialization in C and transfer over MPI和其他人,但我找不到解决问题的办法。

我会很感激一些帮助。

最好的, 盖多

编辑 - 可能的重复并不能解决我的问题,我的问题是关于一个包含指向动态增长的结构数组的指针的结构。重复的问题涉及在结构中包含一组 native 类型。

最佳答案

手动将结构数组序列化到缓冲区并发送缓冲区是一个糟糕的主意,因为它在发送端和接收端都引入了另一个内存副本。

发送 MPI 结构数组与发送任何其他对象的数组没有区别;你只需要创建一个结构类型 - 有 many examples hereelsewhere - 确保计算偏移量和大小,因为结构中可能插入了填充。然后传递一个数组:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "mpi.h"

typedef struct{
    float x;
    float y;
    int centroid;
} point;

typedef struct{
    int csize;//the current size
    int tsize;//the total size
    point * data;//the data carried
} ArrayList;


int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);

    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    assert( size >= 2 );
    const int npts_avail=20;
    point points[npts_avail];

    ArrayList list;
    list.data = points;

    if (rank == 0) {
        int npts_used=10;
        list.csize = npts_used;
        list.tsize = npts_avail;
        for (int i=0; i<list.csize; i++) {
            points[i].x = 1.*i;
            points[i].y = -2.*i;
            points[i].centroid = i;
        }
    }

    const int nfields=3;
    MPI_Aint disps[nfields];
    int blocklens[] = {1,1,1};
    MPI_Datatype types[] = {MPI_FLOAT, MPI_FLOAT, MPI_INT};

    disps[0] = offsetof( point, x );
    disps[1] = offsetof( point, y );
    disps[2] = offsetof( point, centroid );

    MPI_Datatype istruct, pstruct;
    MPI_Type_create_struct(nfields, blocklens, disps, types, &istruct );
    MPI_Type_create_resized( istruct, 0, (char *)&(points[1]) - (char *)(&points[0]), &pstruct );
    MPI_Type_commit(&pstruct);

    if (rank == 0) {
        MPI_Send( &(list.csize), 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
        MPI_Send( &(list.tsize), 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
        MPI_Send( list.data, list.csize, pstruct, 1, 0, MPI_COMM_WORLD);
    } else if (rank == 1) {
        MPI_Recv( &(list.csize), 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        MPI_Recv( &(list.tsize), 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        MPI_Recv( list.data, list.csize, pstruct, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    }

    if (rank == 1) {
        printf("Received: \n");
        for (int i=0; i<list.csize; i++) {
            printf(" (%f, %f): %d\n", points[i].x, points[i].y, points[i].centroid);
        }
    }

    MPI_Finalize();
}

运行得到预期的输出:

$ mpirun -np 2 ./structs
Received:
 (0.000000, -0.000000): 0
 (1.000000, -2.000000): 1
 (2.000000, -4.000000): 2
 (3.000000, -6.000000): 3
 (4.000000, -8.000000): 4
 (5.000000, -10.000000): 5
 (6.000000, -12.000000): 6
 (7.000000, -14.000000): 7
 (8.000000, -16.000000): 8
 (9.000000, -18.000000): 9

请注意,您也可以构建 ArrayList 的 MPI 结构,并使用它 - 除了每次重新发送(在发送端)时可能必须更改数据的位移,而在接收端它可以在您知道需要接收的数据量之前,您甚至可以下定决心。所以最好先发送大小(在两条消息中,就像我在这里发送的那样,或者更好的是,将其作为两个整数的一条消息发送),然后如图所示发送结构数组。

关于c - 在 C 中的 MPI 中如何创建结构的结构并将其发送到多个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26896686/

相关文章:

c - 将字符串返回到需要在屏幕上打印的 C 程序形式的 Bash 脚本

核心转储到超过 26 个字符的 realloc 文本 c

Matlab对象实例更新

C、Contiki rime,传输结构

c - MPI串口主要功能

c - 无符号算术期间高于 ULONG_MAX 的数字

c - 为什么ebx保存在调用gets的简单函数的栈帧中?

c++ - 如何使用 MPI_Type_create_subarray?

c# - sizeof() 结构未知。为什么?

python - 在 mpi4py 中接收腌制项目的非阻塞方式