简体   繁体   中英

MPI: Locking the stdout — 1 process at a time?

I want to print out the content of integer array of each processes. Problem is, it's all cluttered because of race condition.

What is the simplest solution? I don't want to debug. I want to show the contents because I'm doing sorting algorithm. So it's useful to show before and after sort.

I added this in lock.c:

#include <stdio.h>
static int lock=0;   //Don't use if timing execution
void capture(int rank) {
    while(lock!=0);
    lock = 1;
    printf("\nCaptured by %d\n", rank);
}
void release() {
    lock = 0;
}

And call capture() before printing stuff, then release() after printing. Yes, it's a semaphore-like hack. But it isn't working, any ideas?

Assuming you're using an MPI which collects all processes' STDOUTs, you can use MPI_Barrier() to enforce the ordering, like so:

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

Of course, this is inefficient, but it may be a little simpler than arranging to send all the information back to rank 0.

Assuming you mean the Message Passing Interface, the best approach is to write the output strings to a buffer and send them to the rank 0 process. This process should be used not for doing the real work but just for filtering and outputting the output strings.

Leave the other ranks to do the real work. I don't think your above solution will work because i is local to each rank.

You also don't want to use the messaging to stop the individual ranks (until they can output) since they will be unable to do their sorting work in the meantime.

Just have each rank 1 through N do it's work and send strings to rank 0. The strings could be of the form "NNN:SSSSSS" where N is the rank and S is the string. Rank 0 could then filter out specific messages or write different messages to different files, then combine them (sorted) once all the working ranks had shut down.

Each process has its own copy of the variable. So it's not shared and you can't use it for synchronization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM