MPI is designed so that the same executable is started on a number of machines, none of which may be the one on which the mpirun program is called. Furthermore, MPI jobs are typically put into a queue, and the actual job may run hours or days later.
As a consequence, MPI jobs are not meant to be used in an interactive mode, but all input should be provided ...
Input from the console may work on process zero, if that runs locally, but will certainly not work on other ranks. However, I wouldn't even do interactive input on process zero.
if [ $PMI_RANK -eq 0 ] ; then
( echo 5 ; echo 1 ; echo foo ) | $*
Put that in a script and start that with mpirun:
mpirun myscript.sh myprogram
I think the main use of it is abstraction and the advantages that come with it:
Applying a function to each element of an array comes up in many situations. Wrapping this pattern into a function raises the level of abstraction: you do not need to think in loops, your intent is probably clearer. If you are familiar with Python, comprehensions do this, as ...