title: standard output in Fortran MPI code
body: <p>I have a parallel fortran code in which I want only the rank=0 process to be able to write to stdout, but I don't want to have to litter the code with:</p>
  
  <pre><code>if(rank==0) write(*,*) ...
  </code></pre>
  
  <p>so I was wondering if doing something like the following would be a good idea, or whether there is a better way?</p>
  
  <pre><code>program test
  
    use mpi
  
    implicit none
  
    integer :: ierr
    integer :: nproc
    integer :: rank
  
    integer :: stdout
  
    call mpi_init(ierr)
    call mpi_comm_rank(mpi_comm_world, rank, ierr)
    call mpi_comm_size(mpi_comm_world, nproc, ierr)
  
    select case(rank)
    case(0)
       stdout = 6
    case default
       stdout = 7
       open(unit=stdout, file='/dev/null')
    end select
  
    write(stdout,*) "Hello from rank=", rank
  
    call mpi_finalize(ierr)
  
  end program test
  </code></pre>
  
  <p>This gives:</p>
  
  <pre><code>$ mpirun -n 10 ./a.out
  Hello from rank=           0
  </code></pre>
  
  <p>Thanks for any advice!</p>