# How to compute the mean 2D slice of a 3D set of data in MPI

I have a 3D set of data v(i,j,k), and I want to compute the mean 2D slice vmean(i,j) summing up the nz slices of the v(i,j,k) set along its third dimension. I wrote down this piece of FORTRAN90 code but it does not produce the correct results. Anyone can suggest me the solution? Thanks in advance

gbl_sum = 0.0
do j    = mystarty,myendy
do i = mystartx,myendy

lcl_sum = sum(v(i,j,mystartz:myendz))

call MPI_REDUCE(lcl_sum, gbl_sum(i,j), &
1, real, mpi_sum, root,mpi_comm_world,err)

gbl_sum(i,j) = lcl_sum

enddo
enddo

call mpi_BCAST(gbl_sum,gbl_sumSize,real,root,&
mpi_comm_world,err)

do j    = mystarty,myendy
do i = mystartx,myendy
vmean(i,j) = gbl_sum(i,j)/real(nz)
enddo
enddo

• The algorithm you need depends on how you split up your data across the MPI processes. You should describe how you do that. Jan 25 '20 at 20:43
• @spektr The data are split with a Cartesian-type topology, employing a Cartesian communicator Jan 26 '20 at 8:24