Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

standard output in Fortran MPI code

Tags:

fortran

mpi

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:

if(rank==0) write(*,*) ...

so I was wondering if doing something like the following would be a good idea, or whether there is a better way?

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

This gives:

$ mpirun -n 10 ./a.out
Hello from rank=           0

Thanks for any advice!

like image 691
astrofrog Avatar asked Apr 13 '11 12:04

astrofrog


People also ask

What is MPI in Fortran?

Message Passing Interface (MPI) is a standard used to allow different nodes on a cluster to communicate with each other. In this tutorial we will be using the Intel Fortran Compiler, GCC, IntelMPI, and OpenMPI to create a multiprocessor programs in Fortran.


2 Answers

There are two disadvantages to your solution:

  1. This "clever" solution actually obscures the code, since it lies: stdout isn't stdout any more. If someone reads the code he/she will think that all processes are writing to stdout, while in reality they aren't.
  2. If you want all processes to write to stdout at some point, what will you do then? Add more tricks?

If you really want to stick with this trick, please don't use "stdout" as a variable for the unit number, but e.g. "master" or anything that indicates you're not actually writing to stdout. Furthermore, you should be aware that the number 6 isn't always stdout. Fortran 2003 allows you to check the unit number of stdout, so you should use that if you can.

My advice would be to stay with the if(rank==0) statements. They are clearly indicating what happens in the code. If you use lots of similar i/o statements, you could write subroutines for writing only for rank 0 or for all processes. These can have meaningful names that indicate the intended usage.

like image 91
steabert Avatar answered Oct 27 '22 21:10

steabert


mpirun comes with the option to redirect stdout from each process into separate files. For example, -output-filename out would result in out.1.0, out.1.1, ... which you then can monitor using whatever way you like (I use tail -f). Next to if(rank.eq.0) this is the cleanest solution I think.

like image 27
elorenz Avatar answered Oct 27 '22 21:10

elorenz