program reduction include 'mpif.h' integer numtasks, rank, count, source, ierr integer stat(MPI_STATUS_SIZE) real*4 sendbuf,recvbuf,sum C Fortran stores this array in column major order, so the C scatter will actually scatter columns, not rows. call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, numtasks, ierr) count=1 ! ! recv ranks from all the tasks ! if(rank.eq.0)then sum=0.0 do i=1,numtasks-1 call MPI_RECV(recvbuf,count,MPI_REAL,i,i, & MPI_COMM_WORLD,stat,ierr) sum=sum+recvbuf enddo else sendbuf= rank call MPI_SEND(sendbuf,count,MPI_REAL,0,rank, & MPI_COMM_WORLD,ierr) endif ! ! send the sum to all the tasks ! if(rank.eq.0)then do i=1,numtasks-1 call MPI_SEND(sum,count,MPI_REAL,i,i, & MPI_COMM_WORLD,ierr) enddo else call MPI_RECV(sum,count,MPI_REAL,0,rank, & MPI_COMM_WORLD,stat,ierr) endif print *, 'rank= ',rank,' Result: ',sum call MPI_FINALIZE(ierr) end