【问题标题】:How transfer data in mpi? [duplicate]如何在mpi中传输数据? [复制]
【发布时间】:2015-09-15 23:51:02
【问题描述】:

据我所知,在 mpi 中,我们可以在计算机之间传输一些数组。我们可以转移其他东西而不是数组吗? 例如是否可以直接传输“set”数据?

【问题讨论】:

    标签: c++ set mpi


    【解决方案1】:

    是的,您可以使用 MPI 传输任意复杂的类型数据。所有通信操作都采用数据类型参数。这可能是一个基本的预定义数据类型,对应于 C/C++ 中的内置类型(例如,MPI_INT/intMPI_DOUBLE/double)或更复杂的用户定义类型。

    我最近posted 是一个完整的工作(纯 C)示例,其中 integer 值后跟 doubles 的数组在 MPI_Type_create_struct 的帮助下传输,可用于定义这样的用户定义的数据类型:

    #include <mpi.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        MPI_Init(&argc, &argv);
    
        int i, rank, tag = 1;
        MPI_Status status;
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    
        // Array of doubles plus element count
        typedef struct {
            int row;
            double elements[4];
        } My_array;
    
        // Derived datatype for an array of doubles plus element count
        MPI_Datatype MY_ARRAY_TYPE;
        const int nr_blocks = 2;
        int blocklengths[2] = {1, 4};
        MPI_Datatype oldtypes[2] = {MPI_INT, MPI_DOUBLE};
        MPI_Aint displacements[2];
        displacements[0] = offsetof(My_array, row);
        displacements[1] = offsetof(My_array, elements);
        MPI_Type_create_struct(nr_blocks, blocklengths, displacements,
                   oldtypes, &MY_ARRAY_TYPE);
        MPI_Type_commit(&MY_ARRAY_TYPE);
    
        if(rank == 0) {
            My_array array1  = {3, 3.1, 3.2, 3.3, 3.4};
            MPI_Send(&array1, 1, MY_ARRAY_TYPE, 1, tag, MPI_COMM_WORLD);
        }
        if(rank == 1) {
            My_array array2;
            MPI_Recv(&array2, 1, MY_ARRAY_TYPE, 0, tag, MPI_COMM_WORLD, &status);
            printf("Rank %d received elements of row %d:\n", rank, array2.row);
            for(i = 0; i < 4; i++)
                printf("\t%.1f\n", array2.elements[i]);
        }
        MPI_Type_free(&MY_ARRAY_TYPE);
        MPI_Finalize();
    }
    

    还有其他不太通用的派生数据类型构造函数更易于使用;您可以在this answer 中看到两个示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-18
      • 2011-03-03
      • 2013-01-13
      • 2014-04-23
      • 2014-03-09
      • 1970-01-01
      • 2014-09-02
      相关资源
      最近更新 更多