I am trying to run this simple MPI Fortran 90 code on 4 processors. The problem is that I am not able to merge or synchronize the values of the elements of the array calculated by each processor. Here is the code:
PROGRAM TRY
USE MPI
integer status(mpi_status_size)
INTEGER I, J, K, II, IERR, MY_ID, NUM_PROCS, PSP
INTEGER , PARAMETER :: GRIDX =8
REAL , DIMENSION(gridx) :: PSI
psi=0
PRINT*, 'YASER'
call mpi_init(ierr)
call mpi_comm_rank(MPI_COMM_WORLD,my_id,ierr)
call mpi_comm_size(MPI_COMM_WORLD,num_procs,ierr)
DO I=1+MY_ID*GRIDX/NUM_PROCS, (MY_ID+1)*GRIDX/NUM_PROCS
PSI(I)=1.0
END DO
**IF (MY_ID .NE. 0) THEN
CALL mpi_send( PSI(1+MY_ID*GRIDX/NUM_PROCS:(MY_ID+1)*GRIDX/NUM_PROCS),GRIDX/NUM_PROCS,mpi_real, 0,10,mpi_comm_world,ierr)
END IF
IF (MY_ID .EQ. 0) THEN
DO II=1,NUM_PROCS-1
CALL mpi_recv(PSI(1+MY_ID*GRIDX/NUM_PROCS:(MY_ID+1)*GRIDX/NUM_PROCS),GRIDX/NUM_PROCS,mpi_real, &
II,10,mpi_comm_world,status,ierr)
END DO
END IF**
IF (MY_ID .EQ. 0) THEN
OPEN(PSP,FILE='TRYpsi.txt')
DO I=1, GRIDX
WRITE (PSP,*) PSI(I)
END DO
CLOSE(PSP)
END IF
CALL MPI_FINALIZE(IERR)
END PROGRAM TRY
However, although each processor does its job correctly and make 'psi' equal to one, the problem is with their communication because when I open the TRYpsi.txt, I see that while the elements of the array calculated by the 0th processor are all equal to one, the other elements are all equal to zero! This means that either the 0th processor has not received the information from the other processors or the other processors have not sent the information to the other processors.
Could you please help me with mpi_send and mpi_recv?