1

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?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
syaz
  • 2,659
  • 6
  • 36
  • 44
  • When you tell that "it isn't working", what do you mean: personnaly, I suspect you mean the output from diferent threads is mixing up? Is that what you mean? – yves Baumes Apr 02 '09 at 11:32

3 Answers3

6

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.

Edric
  • 23,676
  • 2
  • 38
  • 40
  • Thanks a bunch, Edric! I had pretty much your exact solution to my own version of this...but I didn't have the call to barrier() so things were still getting out of whack.. Much appreciated. –  Jan 29 '11 at 10:17
  • That's fairly unreliable: https://stackoverflow.com/questions/5305061/ordering-output-in-mpi – MechEng Sep 11 '21 at 14:18
3

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

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

sharptooth
  • 167,383
  • 100
  • 513
  • 979