Question

Given this struct:

struct mpi_energy_data { 
    int rank;  
    time_t from;
    time_t to; 
    char hostname[HOST_NAME_MAX]; 
};

I'm attempting to build a derived MPI type. I later use this in a gather operation, but all output in the receiving array is garbage except that which is sent from rank 0.

MPI_Datatype time_interval_mpi;
MPI_Datatype type[4] = { MPI_INT, MPI_CHAR, MPI_INT, MPI_INT };
int blocklen[4] = { 1,HOST_NAME_MAX, 1, 1 };

MPI_Aint offsets[4];
offsets[0] = offsetof(struct mpi_energy_data, rank);
offsets[1] = offsetof(struct mpi_energy_data, hostname);
offsets[2] = offsetof(struct mpi_energy_data, from);
offsets[3] = offsetof(struct mpi_energy_data, to);
MPI_Type_create_struct(4, blocklen, offsets, type, &time_interval_mpi);
MPI_Type_commit(&time_interval_mpi);

Here's the gather. I aim to gather 1 of the structs from each process to the root process. Anyone see anything wrong?

   struct mpi_energy_data *data = NULL;
   if (rank == 0) {
        data = malloc(sizeof(*data) * size);
   }
   struct mpi_energy_data ldata;
   ldata.rank = rank;
   sprintf(ldata.hostname, "example.com");
   gethostname(ldata.hostname, HOST_NAME_MAX);
   ldata.from = (int) ti.from;
   ldata.to = (int) ti.to;
   printf("%d sending %s %d %d\n", ldata.rank, ldata.hostname, (int)ldata.to, (int)ldata.from);
   MPI_Gather(&ldata, 1, time_interval_mpi,
   data, 1, time_interval_mpi, 0, MPI_COMM_WORLD);
   MPI_Type_free(&time_interval_mpi);
Was it helpful?

Solution

I fixed it. My creation of the derived type was incorrect. Here is the working version:

MPI_Datatype time_interval_mpi;
MPI_Datatype type[3] = { MPI_INT, MPI_LONG, MPI_CHAR };
int blocklen[3] = { 1, 2, HOST_NAME_MAX };

MPI_Aint offsets[3];
offsets[0] = offsetof(struct mpi_energy_data, rank);
offsets[1] = offsetof(struct mpi_energy_data, from);
offsets[2] = offsetof(struct mpi_energy_data, hostname);
MPI_Type_create_struct(3, blocklen, offsets, type, &time_interval_mpi);
MPI_Type_commit(&time_interval_mpi);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top