如何使用 mpi 发送包含其他结构数组的结构

问题描述 投票:0回答:0

我有这些结构,我想将它们从主机发送到从机(假设我们有 2 个进程)

`struct Element
{
    int id;
    int n;
    int **matrix;
};

struct Manager
{
    double matching_value;
    int num_pictures;
    struct Element *pictures;
    int num_patterns;
    struct Element *patterns;
};

关于如何解决这个问题的任何建议?

我尝试创建 mpi 结构但没有成功

 // Define the MPI datatype for the Element struct
        MPI_Datatype MPI_Element;
        MPI_Type_contiguous(1, MPI_INT, &MPI_Element); // id
        MPI_Type_commit(&MPI_Element);
        MPI_Datatype MPI_ElementArray;
        MPI_Type_contiguous(2, MPI_INT, &MPI_ElementArray); // n and **matrix
        MPI_Type_create_resized(MPI_ElementArray, 0, sizeof(int *), &MPI_ElementArray);
        MPI_Type_commit(&MPI_ElementArray);
        MPI_Datatype MPI_ElementArray2;
        MPI_Type_contiguous(1, MPI_ElementArray, &MPI_ElementArray2); // *pictures or *patterns
        MPI_Type_commit(&MPI_ElementArray2);

        // Define the MPI datatype for the Manager struct
        int blocklengths[] = {1, 1, 1, 1}; // matching_value, num_pictures, pictures, num_patterns, patterns
        MPI_Aint displacements[] = {0, sizeof(double), sizeof(double) + sizeof(int), sizeof(double) + sizeof(int) + sizeof(struct Element *), sizeof(double) + sizeof(int) + sizeof(struct Element *) + sizeof(int)};
        MPI_Datatype types[] = {MPI_DOUBLE, MPI_INT, MPI_ElementArray2, MPI_INT, MPI_ElementArray2};
        MPI_Datatype MPI_Manager;
        MPI_Type_create_struct(5, blocklengths, displacements, types, &MPI_Manager);
        MPI_Type_commit(&MPI_Manager);
c mpi
© www.soinside.com 2019 - 2024. All rights reserved.