# Sending, receiving result in deadlock

## Sending, receiving result in deadlock

I'm trying to solve a simple problem with MPI but when I comment all the
PRINT lines the program gets into a deadlock, when I uncomment one of
them then the programm proceeds smoothly!!!! I can't understand what is
wrong with this? I'm using blocking send and receive so time difference
that print makes should not has any effect on the overal performance!
The
idea of IF is that odd processes first send and evens recieve then vice
versa, so there should not be any deadlock even if there don't be any
buffer on the way!
What is wrong with my algorithm that makes it break?

``` integer,parameter   ::IM=100,JM=100
REAL,ALLOCATABLE  ::T(:,:),TF(:,:)
..
CALL MPI_COMM_RANK(MPI_COMM_WORLD,RNK,IERR)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD,SIZ,IERR)
..
prv = rnk-1
nxt = rnk+1
LIM = INT(IM/SIZ)
IF (rnk==0) THEN
ALLOCATE(TF(IM,JM))
prv = MPI_PROC_NULL
ELSEIF(rnk==siz-1) THEN
NXT = MPI_PROC_NULL
LIM = LIM+MOD(IM,SIZ)
END IF
ALLOCATE(T(LIM+2,JM+2))

IF (MOD(RNK,2)==0) THEN
CALL MPI_SEND(T(2,:),JM+2,MPI_REAL,PRV,10,MPI_COMM_WORLD,IERR)
!PRINT*,RNK,'SENT'
CALL MPI_RECV(T(1,:),JM+2,MPI_REAL,PRV,20,MPI_COMM_WORLD,STAT,IERR)
!PRINT*,RNK,'RECIEVED'
ELSE
CALL MPI_RECV(T(LIM+2,:),JM+2,MPI_REAL,NXT,10,MPI_COMM_WORLD,STAT,IERR)
PRINT*,RNK,'RECIEVED'
CALL MPI_SEND(T(LIM+1,:),JM+2,MPI_REAL,NXT,20,MPI_COMM_WORLD,IERR)
!PRINT*,RNK,'SENT'
END IF ```
3 posts / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Anishtain,

Your code (a bit modified) below works fine for number of processes from 2 to 16 on Intel64.
Do you define all variables? SIZ may be a real variable.

implicit none
include 'mpif.h'

integer,parameter :: IM=100,JM=100
REAL,ALLOCATABLE :: T(:,:),TF(:,:)
integer RNK,IERR,SIZ, prv, nxt, LIM
integer STAT(MPI_STATUS_SIZE)

CALL MPI_INIT(IERR)

CALL MPI_COMM_RANK(MPI_COMM_WORLD,RNK,IERR)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD,SIZ,IERR)

prv = rnk-1
nxt = rnk+1

LIM = INT(IM/SIZ)

IF (rnk==0) THEN
ALLOCATE(TF(IM,JM))
prv = MPI_PROC_NULL

ELSEIF(rnk==siz-1) THEN
NXT = MPI_PROC_NULL
LIM = LIM+MOD(IM,SIZ)

END IF

ALLOCATE(T(LIM+2,JM+2))

IF (MOD(RNK,2)==0) THEN
CALL MPI_SEND(T(2,:),JM+2,MPI_REAL,PRV,10,MPI_COMM_WORLD,IERR)
!PRINT*,RNK,'SENT'
CALL MPI_RECV(T(1,:),JM+2,MPI_REAL,PRV,20,MPI_COMM_WORLD,STAT,IERR)
!PRINT*,RNK,'RECIEVED'
ELSE
CALL MPI_RECV(T(LIM+2,:),JM+2,MPI_REAL,NXT,10,MPI_COMM_WORLD,STAT,IERR)
!PRINT*,RNK,'RECIEVED'
CALL MPI_SEND(T(LIM+1,:),JM+2,MPI_REAL,NXT,20,MPI_COMM_WORLD,IERR)
!PRINT*,RNK,'SENT'
END IF

IF (rnk==0) DEALLOCATE(TF)

DEALLOCATE(T)

call MPI_FINALIZE(IERR)

print *, rnk, " - done"

END PROGRAM

Regards!
Dmitry

Thanks Dmitry, the problem was with the optimization as I found by someone's advice. I just added the -O0 and it works. siz was integer btw.