Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

stdout - Ordering Output in MPI

in a simple MPI program I have used a column wise division of a large matrix. How can I order the output so that each matrix appears next to the other ordered ? I have tried this simple code the effect is quite different from the wanted:

for(int i=0;i<10;i++)
{
    for(int k=0;k<numprocs;k++)
    {
        if (my_id==k){
            for(int j=1;j<10;j++)
                printf("%d",data[i][j]);
        }
        MPI_Barrier(com);
    }
    if(my_id==0)
        printf("
");
}

Seems that each process has his own stdout and so is impossible to have ordered lines output without sending all the data to one master which will print out. Is my guess true ? Or what I'm doing wrong ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You guessed right. The MPI standard does not specify how stdout from different nodes should be collected for printing at the originating process. It is often the case that when multiple processes are doing prints the output will get merged in an unspecified way. fflush doesn't help.

If you want the output ordered in a certain way, the most portable method would be to send the data to the master process for printing.

For example, in pseudocode:

if (rank == 0) {
    print_col(0);
    for (i = 1; i < comm_size; i++) {
        MPI_Recv(buffer, .... i, ...);
        print_col(i);
    }
} else {
    MPI_Send(data, ..., 0, ...);
}

Another method which can sometimes work would be to use barries to lock step processes so that each process prints in turn. This of course depends on the MPI Implementation and how it handles stdout.

for(i = 0; i < comm_size; i++) {
    MPI_Barrier(MPI_COMM_WORLD);
    if (i == rank) {
         printf(...);
    }
}

Of course, in production code where the data is too large to print sensibly anyway, data is eventually combine by having each process writing to a separate file and merged separately, or using MPI I/O (defined in the MPI2 standards) to coordinate parallel writes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...