!...................................................................................................................................!:.:.:
!.......................... File: mpi_09_Allreduce.f90       .......................................................................!:.:.:
!===================================================================================================================================
!-----------------------------------------------------------------------------------------------------------------------------------
!
! Compile using the commands:
! mpifort mpi_09_Allreduce.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, n=2
 integer             :: ierr, nprocs, myrank
 integer             :: nmat_loc,nmat, i
 real(dp)            :: x   ,x_sum
 real(dp)            :: a(n),a_sum(n)

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

 x =     (myrank+1) * 10.0_dp
 a = [ ( (myrank+1) * 10.0_dp + i , i = 1,n) ]

 print '(A,I2,10F6.1)','00     x= ',myrank,x
 print '(A,I2,10F6.1)','02     a= ',myrank,a

 call mpi_allreduce( &
      x,x_sum,       & !send+recv buffers
      1,MPI_REAL8,   & !count and type
      MPI_SUM,       & !operation on data
      MPI_COMM_WORLD,ierr)

 call mpi_allreduce( &
      a,a_sum,       & !send+recv buffers
      n,MPI_REAL8,   & !count and type
      MPI_SUM,       & !operation on data
      MPI_COMM_WORLD,ierr)

 
 print '(A,I2,10F6.1)','01 sum x= ',myrank,x_sum
 print '(A,I2,10F6.1)','03 sum a= ',myrank,a_sum
 

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