!...................................................................................................................................!:.:.:
!.......................... File: mpi_05_Scatter.f90         .......................................................................!:.:.:
!===================================================================================================================================
!-----------------------------------------------------------------------------------------------------------------------------------
!
! Compile using the commands:
! mpifort mpi_05_Scatter.f90 -o m;mpirun -n 4 ./m | sort
!
! 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
 integer             :: nmat_loc,nmat, i
 real(dp),allocatable :: a(:),b(:)

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

 nmat_loc = 2;
 nmat     = nmat_loc*nprocs

 allocate(a(nmat),b(nmat_loc))

 a = -99.0_dp ; b = -99.0_dp

 if(myrank == 0)then
  a = [(10.0_dp *i, i = 1,nmat)]
 end if

 print '(A,I2,20F6.1)','00 Before scatter: a= ',myrank,a
 print '(A,I2,20F6.1)','02 Before scatter: b= ',myrank,b

 call mpi_scatter( &
      a,nmat_loc,MPI_REAL8,& !send nmat_loc to EACH process
      b,nmat_loc,MPI_REAL8,& !receive: local b expects nmat_loc elements
      0,                   & !root process
      MPI_COMM_WORLD,ierr)

 print '(A,I2,20F6.1)','01 After  scatter: a= ',myrank,a
 print '(A,I2,20F6.1)','03 After  scatter: b= ',myrank,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
!-----------------------------------------------------------------------------------------------------------------------------------
