!-----------------------------------------------------------------------------------------------------------------------------------
!...................................................................................................................................!:.:.:
!.......................... File: mpi_02_Sendrecv.f90        .......................................................................!:.:.:
!===================================================================================================================================
!-----------------------------------------------------------------------------------------------------------------------------------
!
! Compile using the commands:
! mpifort mpi_02_Sendrecv.f90 -o m;mpirun -n 4 ./m
!
! If too many processes, then:   mpirun -n 32 --host $(hostname):32 ./m
!-----------------------------------------------------------------------------------------------------------------------------------
program     mpi_introduction

 use mpi
 implicit none
 integer, parameter :: dp = 8
 
 integer  :: ierr, nprocs, myrank, tag
 integer  :: send_to, recv_from
 real(dp) :: a(2), b(2) 
 integer  :: status(MPI_STATUS_SIZE)

 call mpi_init(ierr)
 call mpi_comm_size(MPI_COMM_WORLD,nprocs,ierr)
 call mpi_comm_rank(MPI_COMM_WORLD,myrank,ierr)

 ! Determine communication partners: Exchange with immediate neighbor
 send_to    = mod(myrank + 1         , nprocs)  
 recv_from  = mod(myrank - 1 + nprocs, nprocs) 
 tag        = 10
 
 a = [ myrank + 1.1_dp,myrank + 1.2_dp]
 b = 0.0_dp

 print '(A,I3,A,2F5.1)','00 Initial: Rank ',myrank,': A= ',a

 ! --- The MPI_SENDRECV Call ---
 ! This single call ensures the send and receive parts execute atomically, preventing deadlock.
 call mpi_sendrecv(                    &
      ! SEND    Arguments (Sending        my 'a' array)
      a, 2, MPI_REAL8, send_to  , tag, & 
      ! RECEIVE Arguments (Receiving into my 'b' array)
      b, 2, MPI_REAL8, recv_from, tag, &
      MPI_COMM_WORLD, status, ierr)

 print '(A,I3,A,2F5.1)','01 Final:   Rank ',myrank,': B= ',b
 
 call mpi_finalize(ierr)

end program mpi_introduction
!===================================================================================================================================
!-----------------------------------------------------------------------------------------------------------------------------------
!  Copyright by Konstantinos N. Anagnostopoulos, Physics Department, National Technical University of Athens, 2025
!  konstant@mail.ntua.gr, www.physics.ntua.gr/konstant
!  
!  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as 
!  published by the Free Software Foundation, version 3 of the License.
!  
!  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
!  
!  You should have received a copy of the GNU General Public Liense along with this program. If not, see http://www.gnu.org/licenses
!-----------------------------------------------------------------------------------------------------------------------------------
