Makros
Makros

Reputation: 294

Write log in stdout (MPI)

I using MPI on Windows with Cygwin. I try to use critical section for write log some one, but what I would not do I always get a mixed log.

setbuf(stdout, 0);
int totalProcess;
MPI_Comm_size(MPI_COMM_WORLD, &totalProcess);
int processRank;
MPI_Comm_rank(MPI_COMM_WORLD, &processRank);
int rank = 0;
while (rank < totalProcess) {
   if (processRank == rank) {
       printf("-----%d-----\n", rank);
       printf("%s", logBuffer);
       printf("-----%d-----\n", rank);
       //fflush(stdout);
   }
   rank ++;
   MPI_Barrier(MPI_COMM_WORLD);
}

I run mpi at single machine (emulation mode):
mpirun -v -np 2 ./bin/main.out
I want dedicated space log per process, what I do wrong?
(When I wrote it I think it would not work correctly...)

Upvotes: 2

Views: 1072

Answers (1)

Jonathan Dursi
Jonathan Dursi

Reputation: 50937

This is the same problem asked about here; there is enough buffering going on at various different layers that there's no guarantee that the final output will reflect the order that the individual processes wrote, although in practice it can work for "small enough" outputs.

But if the goal is something like a logfile, MPI-IO provides mechanisms for you to write to a file in exactly such a way - MPI_File_write_ordered, which writes output in order of processors to the file. As an example:

#include <string.h>
#include <stdio.h>
#include "mpi.h"

int main(int argc, char** argv)
{
    int rank, size;
    MPI_File logfile;

    char mylogbuffer[1024];
    char line[128];

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


    MPI_File_open(MPI_COMM_WORLD, "logfile.txt", MPI_MODE_WRONLY | MPI_MODE_CREATE,
                   MPI_INFO_NULL, &logfile);

    /* write initial message */

    sprintf(mylogbuffer,"-----%d-----\n", rank);
    sprintf(line,"Message from proc %d\n", rank);

    for (int i=0; i<rank; i++)
        strcat(mylogbuffer, line);

    sprintf(line,"-----%d-----\n", rank);
    strcat(mylogbuffer, line);

    MPI_File_write_ordered(logfile, mylogbuffer, strlen(mylogbuffer), MPI_CHAR, MPI_STATUS_IGNORE);

    /* write another message */

    sprintf(mylogbuffer,"-----%d-----\nAll done\n-----%d-----\n", rank, rank);
    MPI_File_write_ordered(logfile, mylogbuffer, strlen(mylogbuffer), MPI_CHAR, MPI_STATUS_IGNORE);

    MPI_File_close(&logfile);

    MPI_Finalize();
    return 0;
}

Compiling and running gives:

$ mpicc -o log log.c -std=c99 
$ mpirun -np 5 ./log
$ cat logfile.txt    
-----0-----
-----0-----
-----1-----
Message from proc 1
-----1-----
-----2-----
Message from proc 2
Message from proc 2
-----2-----
-----3-----
Message from proc 3
Message from proc 3
Message from proc 3
-----3-----
-----4-----
Message from proc 4
Message from proc 4
Message from proc 4
Message from proc 4
-----4-----
-----0-----
All done
-----0-----
-----1-----
All done
-----1-----
-----2-----
All done
-----2-----
-----3-----
All done
-----3-----
-----4-----
All done
-----4-----

Upvotes: 2

Related Questions