0

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

Aiden Strydom
  • 1,198
  • 2
  • 14
  • 43

1 Answers1

1

The MPI datatype does not correspond to the C++ structure at all. Your Pixel structure consists of three int elements. You register an MPI datatype for four float elements and use it to write the array of structures to the file. Since int and float happen to be of the same size on most 32- and 64-bit architectures and since no padding is added between the structure elements, MPI ends up reading (4*sizeof(float) - sizeof(Pixel))*10 = 40 bytes past the end of the pixels array. This shows up as 10 (40/4) additional random values in the file content.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • Thanks for pointing out that i posted an old rendition of the code... But alas i myself noticed it - changed it - and still the same results. Regardless ill fix the post. – Aiden Strydom Apr 24 '14 at 20:44