Arunkumar C R wrote:

Hi,

A fortran 90 code having MPI enabled subroutine is written. The subroutine part is given below,

program abc
        ..................                   !usual statements
       open(20, file='sum.20', action='write')
       open(30, file='sum.40', action='write')
       n2= 100;    nstep=50
       do step=1, nstep
              n1 = step
sum2 = (n2 - n1 + 1) * (2*n1 + (n2 - n1 )) / 2 !from arithmetic progression
              call routine
              write(20, *) step, sum1, sum2
        end do
end program abc


subroutine routine
use dat !module 'dat' with common variables for both program & subroutine
        use mpi
        implicit none
        integer::ivar, istart, iend, sumt, i

        if(step.eq.1) call mpi_init(ierr)
        call mpi_comm_rank(mpi_comm_world, irank, ierr)
        call mpi_comm_size(mpi_comm_world,    np, ierr)
        ivar  = (n2 - n1)  /  np + 1
        istart= min(irank  * ivar + 1,   n2 + 1)
        iend  = min(istart + ivar - 1,   n2)
        sum1  = 0
        do i=istart, iend
                sum1= sum1 + i
        end do

call mpi_reduce(sum1, sumt, 1, mpi_integer, mpi_sum, 0, mpi_comm_world, ierr)
        sum1  = sumt
        if(irank.eq.0) then
              write(30, *) step, sum1, sum2
        end if
        if(step.eq.nstep) call mpi_finalize(ierr)
end subroutine routine

The current problem is that once the subroutine is called the data written to sum.30 and sum.20 are not matching. If there's no mistake with the calculation part, how shall it be possible to get the same data in both the files. I could
see some of the 'sum1' values in sum.20 are not correct.

First, can you confirm that sum.30 is correct? You should be able to judge each output file independently, and not simply compare the two to each other.

One of the problems with sum.20 is that it is being (over?)written by multiple processes. When you launch the (multi-process MPI) job with mpirun, you start multiple copies of the executable. So, multiple processes are opening the files and writing to sum.20. You need a statement in the main program like the "if(irank.eq.0)" conditional in the subroutine.

I don't know what errors, exactly, you're seeing. So, I don't know if that addresses all of your problems. But this is certainly one of them.

Reply via email to