Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding MPI_Gather

Status
Not open for further replies.

emilio2510

Programmer
Feb 21, 2023
1
NZ
Hi there,

I’m relatively new to Fortran and to this community. I’m trying to write some code that performs some calculations on a multidimensional array using MPI. And for the life of me I can’t get MPI_Gather to work, and I’m sure it’s something very obvious that I’m missing here.

My code is:


[pre]program test

USE mpi
USE netcdf

IMPLICIT NONE

INTEGER,ALLOCATABLE,DIMENSION:),:,:) :: array, global_array
INTEGER :: j_start, j_end, i_start, i_end, J, I, t

! MPI
INTEGER :: mpirank, mpisize, mpierr

! Initialize MPI
call MPI_INIT(mpierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, mpisize, mpierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, mpirank, mpierr)

ALLOCATE(array(4,4,2))
ALLOCATE(global_array(4,4,2))

j_start = 1
j_end = 4
i_start = 1 + (mpirank*4/mpisize)
i_end = (mpirank + 1)*4/mpisize

DO t=1,2

DO J=j_start,j_end

DO I=i_start,i_end

array(J,I,t) = J+I+t

END DO

END DO ! J

END DO ! t

IF (mpirank==0) WRITE(*,*) 'before MPI_Gather...'
WRITE(*,*) mpirank, array(1,:,1)

CALL MPI_Barrier(MPI_COMM_WORLD, mpierr)

CALL MPI_Gather(array:),i_start:i_end,:), 4*2*2, MPI_INTEGER, &
global_array:),i_start:i_end,:), 4*2*2, MPI_INTEGER, &
0, MPI_COMM_WORLD, mpierr)

IF (mpirank == 0) THEN
WRITE(*,*) 'AFTER MPI_Gather...'
DO J=1,4
WRITE(*,*) (global_array(J,I,t), I=1,4,1)
END DO
END IF

DEALLOCATE(array)
DEALLOCATE(global_array)

! Finalise MPI
CALL MPI_FINALIZE(mpierr)

end program test[/pre]


And I get these results, which are obviously wrong:


[pre] before MPI_Gather...
1 0 0 5 6
0 3 4 0 0
AFTER MPI_Gather...
0 5289440 0 0
0 0 0 0
225 0 0 0
0 0 0 0[/pre]


I’m using Intel Fortran 2022, and I compile the program with mpiifort

Any help/comment/advice will be truly appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top