0

The output of below fortran code

Program test
    Use mpi
    Implicit None
    Integer :: rank,num_process,ierr
    Real (8) :: a

    Call MPI_Init (ierr)
    Call MPI_Comm_rank (MPI_comm_world, rank, ierr)
    Call MPI_Comm_size (MPI_comm_world, num_process, ierr)

    Call random_number (a)
    Print *, 'rank = ', rank, 'a = ', a

    Call mpi_barrier (MPI_comm_world, ierr)
    Call random_seed ()
    Call random_number (a)
    Print *, 'rank = ', rank, 'a = ', a
    Call MPI_Finalize (ierr)
End

is something like

 rank =            1 a =   3.920868194323862E-007
 rank =            2 a =   3.920868194323862E-007
 rank =            3 a =   3.920868194323862E-007
 rank =            3 a =   0.937863842453073
 rank =            0 a =   3.920868194323862E-007
 rank =            2 a =   3.357612614239127E-002
 rank =            0 a =   0.928975653817377
 rank =            1 a =   0.481275889979884

Why the print result is not synchronized at the MPI_barrier?

The correct synchronized result should be something like this

 rank =            1 a =   3.920868194323862E-007
 rank =            2 a =   3.920868194323862E-007
 rank =            3 a =   3.920868194323862E-007
 rank =            0 a =   3.920868194323862E-007
 rank =            3 a =   0.937863842453073 
 rank =            2 a =   3.357612614239127E-002
 rank =            0 a =   0.928975653817377
 rank =            1 a =   0.481275889979884
user15964
  • 2,507
  • 2
  • 31
  • 57
  • It may be better to put the output as plain text in the question, rather than an image. Also, please explain exactly what you expect/hope to see. And, perhaps, as this is a question about buffering of standard output how you run the program. – francescalus Jul 21 '16 at 08:20
  • @francescalus I updated my post : ) – user15964 Jul 21 '16 at 08:23
  • IO goes to a buffer, and maybe this is causing some wrong ordering. You could try to add a `flush()` after the first print. – haraldkl Jul 21 '16 at 08:39
  • @haraldkl Hi, haraldkl. How to add it? I got an error – user15964 Jul 21 '16 at 08:43
  • See for example http://stackoverflow.com/q/1533717/577108, should be something like print *, 'something'; flush(6). Or, better use OUTPUT_UNIT from the iso_fortran_env module: https://gcc.gnu.org/onlinedocs/gfortran/ISO_005fFORTRAN_005fENV.html – haraldkl Jul 21 '16 at 08:52
  • @haraldkl I don't understand, I tried `call flush()`, didn't work. What is the unit for screen? – user15964 Jul 21 '16 at 08:58
  • `use, intrinsic :: iso_fortran_env, only : output_unit; ...; flush(output_unit)`. Note, not `call flush`. – francescalus Jul 21 '16 at 09:08
  • @francescalus Thank you. Ok, now the flush(output_unit) works. However, the output is still not synchronized. – user15964 Jul 21 '16 at 09:12
  • 1
    If you want deterministic output, you should output from a single rank only. – Hristo Iliev Jul 21 '16 at 11:25
  • 1
    @HristoIliev Thank you Hristo lliev. – user15964 Jul 21 '16 at 13:49

0 Answers0