The results are not printed unless I include this "print done irank" penultimate line.
Also, despite seeing that all ranks reach the print statement, the program hangs, as if they have not all reached MPI_FINALIZE.

Can anyone help me? Why does it do this?

I also had many times where the program would crash if I didn't include a print statement in a loop. I've been doing Fortran programming for a while, and this is my nightmare debugging scenario since I've never been able to figure out why the simple printing of statements magically fixes the program, and I usually end up having to go back to a serial solution, which is really slow.

If anyone might be able to help me, I would be really really grateful!!