2

MPI seems to automatically designate 0 as the master process. But I want to be able to designate another process (e.g. process with rank 10) to be the master process.

Also, the function scanf only works in the master process: other processes simply ignore it and go to the next statement. Why is this happening?

nbro
  • 15,395
  • 32
  • 113
  • 196
Madhu
  • 253
  • 1
  • 8
  • 21

2 Answers2

0

Yes and no.

You can create a separate communicator world subgroup with MPI_Comm_create or MPI_Comm_split.

As for scanf, I think, if you run them, say, on a different rank, it will output that rank process. Say, if you have a sperate terminal on that rank, but your terminal is most likely on rank 0, hence you only seeing the output there, but then your not running the program on the other ranks, so I don't think it will work.

To print from separate nodes you need to use sprintf, and store the result in memory, and then send it back to rank 0 to printf to your terminal.

nbro
  • 15,395
  • 32
  • 113
  • 196
pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • But how do other processes can printf correctly if my terminal is on 0? And is there a way to open a new terminal for one of the other processes? – Madhu Oct 10 '12 at 04:27
  • @user1464184 i think you would have to use `sprintf` to save the result to memory and then use `MPI_send` to send that back to rank 0 then print it out at rank 0, let me dig up an example – pyCthon Oct 10 '12 at 04:28
  • http://hamilton.nuigalway.ie/teaching/AOS/NINE/mpi-first-examples.html in example 4 that sprintf runs on every rank, but you can run it separately with a `if (rank = 3){ sprintf(....` – pyCthon Oct 10 '12 at 04:33
0

When mpiexec starts MPI processes, only one of them receives stdin. The rest will not have that descriptor open, so reading from it will just return an error. If you check the return value from scanf it will probably be EOF. Technically there could be an implementation that would copy the contents of stdin and forward to all processes, but I don't think any MPI does this.

stdout on the other hand is usually collected from all processes by the launcher, and printed on the terminal, but the order in which things get printed is next to impossible to control. The common solution is to collect the data at one rank manually and have it print everything, as pyCthon suggested.

Finally, OpenMPI's mpiexec can do the things you asked for: specify which rank will get stdin, and even launch certain ranks in a separate xterm. This might be useful for debugging, but I wouldn't rely on these features for core functionality: you might want to use your code with a different MPI implementation some day. Besides, say you are running on a large cluster - do you really want a thousand terminals open? :)

Greg Inozemtsev
  • 4,516
  • 23
  • 26