cygwin - Write log in stdout (MPI) -


i using mpi on windows cygwin. try use critical section write log one, not 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 @ single machine (emulation mode):
mpirun -v -np 2 ./bin/main.out
want dedicated space log per process, wrong?
(when wrote think not work correctly...)

this same problem asked here; there enough buffering going on @ various different layers there's no guarantee final output reflect order individual processes wrote, although in practice can work "small enough" outputs.

but if goal logfile, mpi-io provides mechanisms write file in such way - mpi_file_write_ordered, writes output in order of processors file. 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 proc %d\n", rank);      (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 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 , running gives:

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

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -