S Grimminck
S Grimminck

Reputation: 528

returning MPI DataType to MPI Gather

I have the following MPI_DataType code

typedef struct resultset {
    int rank, totalProcessed, bracket1, bracket2, bracket3, bracket4;
    float bracket1percent, bracket2percent, bracket3percent, bracket4percent;
} resultset;

MPI_Datatype createResultType()
{
    // Set-up the arguments for the type constructor
    MPI_Datatype new_type;

    int count = 2;

    int blocklens[] = { 6, 4 };

    MPI_Aint indices[2];
    indices[0] = 0;
    MPI_Type_extent( MPI_FLOAT, &indices[1] );
    indices[1] *= 4;    // There are 4 float

    MPI_Datatype old_types[] = { MPI_INT, MPI_FLOAT };

    // Call the data type constructor
    MPI_Type_struct(count, blocklens, indices, old_types, &new_type);
    MPI_Type_commit(&new_type);

    return new_type;
}

I am attempting to use the following code to recieve the data from other processes. rank is an integer that defines what rank the process is.

MPI_Datatype resType = createResultType();
if(rank != 0){
    MPI_Gather(&results, sizeof(resultset), resType, NULL, 1, resType, 0, MPI_COMM_WORLD);
}
else {
    resultset* all_results = new resultset[numProcs];
    MPI_Gather(&results, sizeof(resultset), resType, all_results, sizeof(resultset), resType, 0, MPI_COMM_WORLD);
}

The issue is that I can't loop through the all_results, using this method:

for(int i = 0; i < numProcs; ++i){
    std::cout << all_results[i].rank << " processed " << all_results[i].totalProcessed <<std::endl;
}

I believe the issue lies with my datatype, but I am not sure. Any advice would be appreciated.

EDIT: I got the issue of looping through it, now I'm getting incomplete data. I changed sizeof(resultset) to 1 inside the gather function. Now I receive the correct data, EXCEPT the last 2 floats are -431602080 instead of the 0.0022234327654 that they should be.

MPI_Datatype resType = createResultType();
if(rank != 0){
    MPI_Gather(&results, 1, resType, NULL, 1, resType, 0, MPI_COMM_WORLD);
}
else {
    resultset* all_results = new resultset[numProcs];
    MPI_Gather(&results, 1, resType, all_results, 1, resType, 0, MPI_COMM_WORLD);
}

Upvotes: 3

Views: 198

Answers (1)

Adam Boyce
Adam Boyce

Reputation: 98

Float needs to be "6"

int blocklens[] = { 6, 4 };

to

int blocklens[] = { 6, 6 };

Upvotes: 1

Related Questions