Question

I have a C++ program using MPI where I would like each process (up to 32) to write to a file. I am using a small, test data set consisting of 100 doubles distributed evenly across the processes. Here is how the output is formatted thus far:

  data_sink.Write(&d_p[i], 1, MPI::DOUBLE);
  data_sink.Write(&space, 1, MPI::CHAR);
  data_sink.Write(&r_p[j], 1, MPI::DOUBLE);
  data_sink.Write(&new_line, 1, MPI::CHAR);

What is the best way to format this output so that the result can be directly interpreted by GNUPlot?

Was it helpful?

Solution

I'm assuming here that data_sink is an MPI::File. Enough code to be able to reproduce your output would be helpful.

The first thing to know is that MPI-IO routines output in binary; outputting the space and the newline character after each value won't change that. But the other thing to know is that gnuplot can read binary files just fine. So here is a little MPI-IO code that outputs a sine wave, with each process getting 20 points of the whole domain:

#include <mpi.h>
#include <cmath>

int main(int argc, char **argv) {

    int rank, size;
    const int nperrank = 20;
    double *data = new double[2*nperrank];

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    int totnpts = nperrank * size;
    double dx = (2.*3.14159/totnpts);
    double left = rank * nperrank * dx;

    for (int i=0; i<nperrank; i++) {
        double x = left + dx*i;
        double sinx = sin(x);

        data[2*i] = x;
        data[2*i+1] = sinx;
    }

    MPI_File file;
    MPI_File_open(MPI_COMM_WORLD, "out.dat", MPI_MODE_WRONLY |  MPI_MODE_CREATE,
                    MPI_INFO_NULL, &file);

    MPI_Status status;
    MPI_File_write_at_all(file, 2*rank*nperrank*sizeof(double),
                            data, 2*nperrank, MPI_DOUBLE, &status);

    MPI_File_close(&file);

    MPI_Finalize();
    return 0;
}

And here's the output of running it and plotting:

$ mpicxx -o io io.cc
$ mpirun -np 4 ./io
$ gnuplot

    G N U P L O T
    Version 4.2 patchlevel 6
    [...]


Terminal type set to 'x11'
gnuplot> plot 'out.dat' binary format="%lf%lf" using 1:2 with linespoints
gnuplot>

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top