0

I am trying to make multiple processes write an integer buffer into a file at the same time using MPI parallel io. To achieve this goal I searched various websites:

And I tried to learn their teachings. To test them i created the following simple code in c++:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

#define BUFSIZE 100

using namespace std;

int main(int argc, char *argv[])
{
    int myrank, buf[BUFSIZE], rcode;
    MPI_File thefile;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    for (int i=0; i<BUFSIZE; i++)
        buf[i] = myrank * BUFSIZE + i;

    rcode = MPI_File_open(MPI_COMM_WORLD, "testfile", MPI_MODE_CREATE | MPI_MODE_RDWR,
                                MPI_INFO_NULL, &thefile);

    if(rcode != MPI_SUCCESS){
        cerr << "Did not open file" << endl;
        return EXIT_FAILURE;
    }

    rcode = MPI_File_set_view(thefile, myrank * (MPI_Offset)BUFSIZE * sizeof(int), MPI_INT, MPI_INT,
                                        "native", MPI_INFO_NULL);
    if(rcode != MPI_SUCCESS){
        cerr << "Problem setting process view" << endl;
        return EXIT_FAILURE;
    }


    MPI_File_write(thefile, buf, BUFSIZE, MPI_INT, MPI_STATUS_IGNORE);
    if(rcode != MPI_SUCCESS){
        cerr << "Problem writting file" << endl;
        return EXIT_FAILURE;
    }

    MPI_File_close(&thefile);
    MPI_Finalize();

    return EXIT_SUCCESS;
}

However, when I try to read the file with Kate, I get random garbage: a bunch of squares, rectangles, !$"%&!!/ symbols and no integers at all.

What I am doing wrong?

stefan
  • 10,215
  • 4
  • 49
  • 90
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 1
    Most probably you are writing raw integers to the file. It reads as garbage if you treat it as ascii text. Try using `od` to see the contents of the file. – Aneri Mar 24 '13 at 18:53
  • 2
    It's a command line utility. `od -t d4 /path/to/file` – Aneri Mar 24 '13 at 18:56
  • The program is correct as written; @Aneri is quite right, od shows that the output is just what you'd expect, but it's in binary format, which is how MPI-IO operates - think `fwrite()`, not `fprintf()`. – Jonathan Dursi Mar 24 '13 at 19:51

1 Answers1

2

You seem to have a text/binary data misconception.

The ints written by MPI_File_write(..., MPI_INT,...) will be written as binary data, not ASCII or Unicode text. "A bunch of squares, rectangles, @#$@#! symbols" reads to me as if you are opening the file with a text editor and looking for ASCII numbers.

If you want formatted output, you have to format it yourself. If you want a text editor to read your file, you need to write text, not ints.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Thx, but if I use write instead of write_all I have the same result! – Flame_Phoenix Mar 24 '13 at 18:55
  • 1
    "it means to write BUFSIZE bytes, not BUFSIZE ints" -- that's not correct. The signature is `MPI_File_write(MPI_File mpi_fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)`; it exactly does write `count` of whatever `datatype` is given. So yes, `MPI_File_write(... BUFSIZE, MPI_INT...)` would write BUFSIZE ints. – Jonathan Dursi Mar 24 '13 at 19:42
  • @JohathanDursi ah -- I missed that argument. Thanks! – Yakk - Adam Nevraumont Mar 24 '13 at 20:44
  • @FLAME_phoenix create strings of formatted characters and output them as bytes? It probably won't be trivial to make parallel, because the length of each datapoint will vary. Probably the right question is how to read raw `int` data in binary form. – Yakk - Adam Nevraumont Mar 25 '13 at 01:42