Given a struct Pixel
and its equivalent MPI_Type mpiPixel
i create an array of pixels and write it to a file. Everything runs correctly except, the output in the file ends in some sort of a bit pattern (being interpreted as integers). The file is outputted in binary thus to view it was written correctly i use hexdump -v -e '7/4 "%10d "' -e '"\n"' pixelsx
THE CODE
struct Pixel
{
int red; int green; int blue;
Pixel() {}
Pixel(int r, int g, int b)
{
red = r; green = g; blue = b;
}
};
int main(int argc, char **argv)
{
int rank, size;
MPI_File file;
MPI_Offset offset;
MPI_Status status;
MPI_Datatype mpiPixel;
Pixel pixels[10];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* fill local data */
for (int i = 0; i < 10; i++)
pixels[i] = Pixel(i, i, i);
int blockcounts[1];
MPI_Aint offsets[1];
MPI_Datatype oldtypes[1];
//Pixel Description {starting pos, element count, element type}
offsets[0] = 0;
blockcounts[0] = 3;
oldtypes[0] = MPI_INT;
/* Now define structured type and commit it */
MPI_Type_struct(1, blockcounts, offsets, oldtypes, &mpiPixel);
MPI_Type_commit(&mpiPixel);
/* open the file, and set the view */
MPI_File_open(MPI_COMM_WORLD, "pixels",
MPI_MODE_CREATE | MPI_MODE_WRONLY,
MPI_INFO_NULL, &file);
MPI_File_seek(file, 0, MPI_SEEK_SET);
MPI_File_set_view(file, 0, MPI_CHAR, mpiPixel, "native", MPI_INFO_NULL);
MPI_File_write_all(file, pixels, 10, mpiPixel, &status);
MPI_File_close(&file);
MPI_Finalize();
return 0;
}
THE OUTPUT
0 0 0 1 1 1 2
2 2 3 3 3 4 4
4 5 5 5 6 6 6
7 7 7 8 8 8 9
9 9 4228656 0 0 0 4228656
0 0 0 -1795965243 32585
The line which shouldn't be there (if i'm not mistaken) is
4228656 0 0 0 4228656 0 0 0 -1795965243 32585
Why is the latter printed in the file. It is a memory assignment issue (the array?)?
P.s. The code is run with only one process. The reason is that i first need to get the write function working. Then ill add the offset for the other processes