-1

As I know in mpi we can transfer some arrays between computers. can we transfer any thing else instead of arrays ? for example is it possible to transfer "set " data directly?

koala
  • 59
  • 8

1 Answers1

0

Yes, you can transfer arbitrarily complex typed data with MPI. All communication operations take a datatype argument. This may be a basic, predefined datatype corresponding to built-in types in C/C++ (e.g., MPI_INT/int or MPI_DOUBLE/double) or more complex user-defined types.

I just recently posted a complete working (plain C) example where an integer value followed by an array of doubles is transmitted with the help of MPI_Type_create_struct, which can be used to define such a user-defined datatype:

#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();
}

There are other, less general derived datatype constructors that are easier to use; you can see two examples in this answer.

Community
  • 1
  • 1
mort
  • 12,988
  • 14
  • 52
  • 97