!...................................................................................................................................!:.:.:
!.......................... File: mpi_06_Gather.f90          .......................................................................!:.:.:
!===================================================================================================================================
!-----------------------------------------------------------------------------------------------------------------------------------
!
! Compile using the commands:
! mpifort mpi_06_Gather.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 = [((myrank+1) * 10.0_dp + i, i = 1, nmat_loc)]

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

 call mpi_gather( &
      b,nmat_loc,MPI_REAL8,& !send nmat_loc to root
      a,nmat_loc,MPI_REAL8,& !receive nmat_loc elements from each process
      0,                   & !root process
      MPI_COMM_WORLD,ierr)

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