Demo_00, Step 03: A little more MPI

Step 03: Processes can do different things

For Step 03, we'll change this slightly by adding a conditional:

#include <stdio.h>
#include <mpi.h>

int main(int argc, char** argv){
        int rank;
	
	MPI_Init(&argc, &argv);
	
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	
        if(rank == 0){
	    printf("Hello, world! I'm rank %d\n", rank);
        }
        else{
	    printf("Hmm...\n");
        }

	MPI_Finalize();
}

The effect of this is that now only rank zero will reach the original output line, while the other ranks will pass to the other 'Hmm...' output. Load Step 03's code into the work directory (manually or using the 'copy' function), then build and run it. The output will be something like:

Hmm...
Hello, world! I'm rank 0
Hmm...
Hmm...

The importance of this example is that it shows a very common technique for constructing MPI programs: one rank (rank 0, usually) performs some operations while the others do something else.