!...................................................................................................................................!:.:.:
!.......................... File: mpi_02_SendAndRecv.f90     .......................................................................!:.:.:
!===================================================================================================================================
!-----------------------------------------------------------------------------------------------------------------------------------
!
! Compile using the commands:
! mpifort mpi_02_SendAndRecv.f90 -o m;mpirun -n 4 ./m
!
!-----------------------------------------------------------------------------------------------------------------------------------
program     mpi_introduction

 use mpi
 implicit none
 integer, parameter :: dp = 8
 integer            :: ierr, nprocs, myrank
 integer            :: source,destination,tag,count
 real(dp)           :: x(2), y(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)

 source      = 0
 destination = nprocs - 1
 tag         = 100     ! any integer value you want

 if(myrank == source     ) then
  x = [-99.0,-98.0]
  call mpi_send(x , 2, MPI_REAL8,destination,tag,MPI_COMM_WORLD,ierr)
  print *,'proc ',myrank,': I sent to       ',destination,' x= ',x
 end if

 if(myrank == destination) then
  call mpi_recv(y , 2, MPI_REAL8,source     ,tag,MPI_COMM_WORLD,status,ierr)
  
  print *,'proc ',myrank,': I received from ',source,' y= ',y

  call mpi_get_count(status,MPI_REAL8,count,ierr)
  print *,'proc ',myrank,': message tag=    ',status(MPI_TAG),' source= ',status(MPI_SOURCE),' count= ',count

 end if

 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
!-----------------------------------------------------------------------------------------------------------------------------------
