Cygwin 在标准输出中写入日志(MPI)

Cygwin 在标准输出中写入日志(MPI),cygwin,mpi,Cygwin,Mpi,我使用Cygwin在Windows上使用MPI。我尝试使用临界区来编写日志,但我不会这样做,因为我总是得到一个混合日志 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 < totalP

我使用Cygwin在Windows上使用MPI。我尝试使用临界区来编写日志,但我不会这样做,因为我总是得到一个混合日志

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);
}
setbuf(stdout,0);
整体过程;
MPI_Comm_大小(MPI_Comm_WORLD和totalProcess);
int processRank;
MPI_通信排名(MPI_通信世界和processRank);
int秩=0;
while(等级
我在单机上运行mpi(仿真模式):
mpirun-v-np2./bin/main.out

我需要每个进程的专用空间日志,我做错了什么?

(当我写它的时候,我认为它不能正常工作…

这是同样的问题;在不同的层上有足够的缓冲,因此不能保证最终输出将反映各个进程编写的顺序,尽管在实践中它可以用于“足够小”的输出

但是,如果目标类似于日志文件,那么MPI-IO为您提供了写入文件的机制,即按照处理器的顺序将输出写入文件。例如:

#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;
}
$ 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-----