Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add padding to MPI structure

I have an array of C structure which I'd like to fill reading a file (in parallel, using set_view and so on)

typedef struct
{
    char   type;
    double value;
    double velocity;
} Cell;

My issue is that some file (type1) will only have type and value (in with case velocity has to be left to O, and in some other file (type2) I have both type, value and velocity

Therefore, when reading n blocks in my file, I'm either reading n x 9 bits (case1) or n x 17 bits( (case2) which I have to put in the buffer with the good alignment.

I started with a mpi_cell_aligned type

MPI_Datatype mpi_cell_aligned;
int          count[] = { 1,                    1,                     1                        };
MPI_Aint     displ[] = { offsetof(Cell, type), offsetof(Cell, value), offsetof(Cell, velocity) };
MPI_Datatype types[] = { MPI_CHAR,             MPI_DOUBLE,            MPI_DOUBLE               };
switch(type)
{
    case 1: MPI_Type_create_struct(2, count, displ, types, &mpi_cell_aligned); break;
    case 2: MPI_Type_create_struct(3, count, displ, types, &mpi_cell_aligned); break;
}
MPI_Type_commit(&mpi_cell_aligned);

And using MPI_Type_contiguous I also built a mpi_cell_packed type with represent 9/17 continuous bits (ui the format in the binary file).

My issue is for writing to my buffer, I'm trying to build a vector type that contains several mpi_cell_aligned. In case 2 it's easy as each type is next to the other, but in case 1 I have to account to a padding between my types which correspond to the length of 1 double.

Unfortunately, the stride given to MPI_Type_Vector must be measured in number of structures, and not in bytes. I the meantime I cannot just describe my vector using MPI_BYTE as my cell structure is not full (alignment padding between the char and the first double).

How can I build the corresponding MPI Datatype which would correctly represent an array of Cell in case 1?

like image 615
Amxx Avatar asked Feb 18 '26 19:02

Amxx


1 Answers

You have to modify the extent for the mpi type in case 1.

The extent of a type is a size used to know where to find the following element in send/recv/write/read operation.

The main function is MPI_Type_create_resized. In your case the extent of the mpi type in case 1 must be the same as the extent of the mpi type in case 2.

So you must do something like that :

/* Temporary type */
MPI_Type_create_struct(2, count, displ, types, &mpi_cell_aligned_temp);
/* Compute new extent */
MPI_Type_size(mpi_cell_aligned_temp,&size_double);
extent = offsetof(Cell, velocity)+size_double;
/* Create new type with new extent */
MPI_Type_create_resized(mpi_cell_aligned_temp,0, extent,&mpi_cell_aligned_temp);
like image 105
D. Lecas Avatar answered Feb 21 '26 14:02

D. Lecas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!