! (C) Copyright 2005- ECMWF.
! (C) Copyright 2013- Meteo-France.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
! In applying this licence, ECMWF does not waive the privileges and immunities
! granted to it by virtue of its status as an intergovernmental organisation
! nor does it submit to any jurisdiction.
!

MODULE MPL_ALLGATHERV_MOD

!**** MPL_ALLGATHERV Send data to all processes

!     Purpose.
!     --------
!     Send a message to all processes from a buffer.
!     The data may be REAL*4, REAL*8,or INTEGER, one dimensional array
!                     REAL*4,or REAL*8, two dimensional array
!                  or INTEGER scalar

!**   Interface.
!     ----------
!        CALL MPL_ALLGATHERV

!        Input required arguments :
!        -------------------------
!           PSENDBUF -  buffer containing message
!                       (can be type REAL*4, REAL*8 or INTEGER)
!           PRECVBUF -  buffer containing message
!                       (can be type REAL*4, REAL*8 or INTEGER)
!           KRECVCOUNTS-number of elements received from each process

!        Input optional arguments :
!        -------------------------
!           KCOMM    -  Communicator number if different from MPI_COMM_WORLD
!                       or from that established as the default
!                       by an MPL communicator routine
!           KMP_TYPE -  buffering type (see MPL_BUFFER_METHOD)
!                       overrides value provided to MPL_BUFFER_METHOD
!           KRECVDISPL -displacements in PRECVBUF at which to place
!                       the incoming data
!           CDSTRING -  Character string for ABORT messages
!                       used when KERROR is not provided

!        Output required arguments :
!        -------------------------
!           none

!        Output optional arguments :
!        -------------------------
!           KREQUEST -  Communication request
!                       required when buffering type is non-blocking
!           KERROR   -  return error code.     If not supplied,
!                       MPL_ALLGATHERV aborts when an error is detected.
!     Author.
!     -------
!        D.Dent, M.Hamrud     ECMWF

!     Modifications.
!     --------------
!        Original:   2000-11-23
!        J.Hague:    2004-12-15 : Threadsafe
!        M.Hamrud:   2014-10-22 : Add nonblocking option
!        F.Vana:     2015-03-05 : Support for single precision
!        P.Gillies:  2018-05-30 : Add KSENDCOUNT argument, needed for zero length sends

!     ------------------------------------------------------------------

USE EC_PARKIND, ONLY : JPRD, JPIM ,JPRM
USE OML_MOD,    ONLY : OML_MY_THREAD

USE MPL_MPI,                  ONLY : MPI_REQUEST, MPI_COMM, MPI_REAL4, MPI_REAL8, MPI_INTEGER
USE MPL_DATA_MODULE,          ONLY : MPL_NUMPROC, MPL_COMM_OML, MPL_METHOD, MPL_UNIT, MPL_OUTPUT, &
 &                                   JP_BLOCKING_STANDARD, JP_BLOCKING_BUFFERED, &
 &                                   JP_NON_BLOCKING_STANDARD, JP_NON_BLOCKING_BUFFERED
USE MPL_STATS_MOD,            ONLY : MPL_RECVSTATS, MPL_SENDSTATS
USE YOMMPLSTATS,              ONLY : LMPLSTATS
USE MPL_MESSAGE_MOD,          ONLY : MPL_MESSAGE
USE MPL_DISPLS_CONTAINER_MOD, ONLY : YDDISPLS_LIST

IMPLICIT NONE

PRIVATE

LOGICAL   :: LLABORT=.TRUE.
LOGICAL   :: LLBARRIER

INTERFACE MPL_ALLGATHERV
   MODULE PROCEDURE MPL_ALLGATHERV_REAL8,MPL_ALLGATHERV_REAL4,&
      MPL_ALLGATHERV_INT,  MPL_ALLGATHERV_INT_SCALAR
END INTERFACE

PUBLIC MPL_ALLGATHERV

CONTAINS


SUBROUTINE MPL_ALLGATHERV_REAL4(PSENDBUF,PRECVBUF,KRECVCOUNTS,KSENDCOUNT,KRECVDISPL, &
& KMP_TYPE,KCOMM,KERROR,KREQUEST,CDSTRING)

#ifdef USE_8_BYTE_WORDS
USE MPI4TO8, ONLY : &
   MPI_COMM_SIZE => MPI_COMM_SIZE8, MPI_ALLGATHERV => MPI_ALLGATHERV8
#endif

IMPLICIT NONE
REAL(KIND=JPRM)            :: PSENDBUF(:)
REAL(KIND=JPRM)            :: PRECVBUF(:)
INTEGER(KIND=JPIM),INTENT(IN) :: KRECVCOUNTS(:)
INTEGER(KIND=JPIM),INTENT(IN),OPTIONAL :: KSENDCOUNT
INTEGER(KIND=JPIM),INTENT(IN),OPTIONAL :: KRECVDISPL(:),KCOMM,KMP_TYPE
INTEGER(KIND=JPIM),INTENT(OUT),OPTIONAL :: KERROR,KREQUEST
CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: CDSTRING
TARGET :: KRECVDISPL

TYPE(MPI_REQUEST)  :: IREQUEST_LOCAL
INTEGER(KIND=JPIM) :: IR,ISENDCOUNT,IRECVCOUNT
INTEGER(KIND=JPIM) :: IMP_TYPE
INTEGER(KIND=JPIM) :: IERROR,IPL_NUMPROC
TYPE(MPI_COMM)     :: ICOMM
INTEGER(KIND=JPIM) :: ITID
INTEGER(KIND=JPIM), TARGET, ALLOCATABLE :: IRECVDISPL(:)
INTEGER(KIND=JPIM), POINTER :: IRECVDISPL_PT(:)
LOGICAL :: LKRECVCOUNTS = .TRUE. ! .TRUE. if KRECVCOUNTS is present (used in _int_scalar version)
INTEGER(KIND=JPIM) :: IRECVCOUNTS(1) ! needed for _int_scalar version preamble

ITID = OML_MY_THREAD()
IF(PRESENT(KSENDCOUNT)) THEN
   ISENDCOUNT = KSENDCOUNT
ELSE
   ISENDCOUNT = SIZE(PSENDBUF)
   ISENDCOUNT = MAX(0,ISENDCOUNT)  ! Bug? on IBM
ENDIF
IRECVCOUNT = SIZE(PRECVBUF)

!--------- Preamble repeated for threadsafe--------------
#include "mpl_allgatherv_preamble.i90"
!--------- End of Preamble --------------


IF(IMP_TYPE == JP_BLOCKING_STANDARD .OR. IMP_TYPE == JP_BLOCKING_BUFFERED) THEN
 CALL MPI_ALLGATHERV(PSENDBUF,ISENDCOUNT,MPI_REAL4,PRECVBUF,&
   & KRECVCOUNTS,IRECVDISPL_PT,MPI_REAL4,ICOMM,IERROR)
ELSEIF(IMP_TYPE == JP_NON_BLOCKING_STANDARD .OR. IMP_TYPE == JP_NON_BLOCKING_BUFFERED) THEN
 CALL MPI_IALLGATHERV(PSENDBUF,ISENDCOUNT,MPI_REAL4,PRECVBUF,&
   & KRECVCOUNTS,IRECVDISPL_PT,MPI_REAL4,ICOMM,IREQUEST_LOCAL,IERROR)
 KREQUEST=IREQUEST_LOCAL%MPI_VAL
 IF(.NOT. PRESENT(KRECVDISPL)) THEN
    CALL YDDISPLS_LIST%APPEND(KREQ=KREQUEST,NO_NEW_NODE=.TRUE.)
 ENDIF
ENDIF
IF(LMPLSTATS) THEN
  CALL MPL_SENDSTATS(ISENDCOUNT,MPI_REAL4%MPI_VAL)
  CALL MPL_RECVSTATS(SUM(KRECVCOUNTS),MPI_REAL4%MPI_VAL)
ENDIF

IF(MPL_OUTPUT > 1 )THEN
   WRITE(MPL_UNIT,'(A,5I8)') ' MPL_ALLGATHERV ',ISENDCOUNT,IRECVCOUNT,ICOMM
ENDIF
IF(PRESENT(KERROR)) THEN
   KERROR=IERROR
ELSE
  IF(IERROR /= 0 ) CALL MPL_MESSAGE(CDMESSAGE='MPL_ALLGATHERV',&
   & CDSTRING=CDSTRING,KERROR=IERROR,LDABORT=LLABORT)
ENDIF

END SUBROUTINE MPL_ALLGATHERV_REAL4

SUBROUTINE MPL_ALLGATHERV_REAL8(PSENDBUF,PRECVBUF,KRECVCOUNTS,KSENDCOUNT,KRECVDISPL, &
& KMP_TYPE,KCOMM,KERROR,KREQUEST,CDSTRING)

#ifdef USE_8_BYTE_WORDS
USE MPI4TO8, ONLY : &
   MPI_COMM_SIZE => MPI_COMM_SIZE8, MPI_ALLGATHERV => MPI_ALLGATHERV8
#endif

IMPLICIT NONE
REAL(KIND=JPRD)            :: PSENDBUF(:)
REAL(KIND=JPRD)            :: PRECVBUF(:)
INTEGER(KIND=JPIM),INTENT(IN) :: KRECVCOUNTS(:)
INTEGER(KIND=JPIM),INTENT(IN),OPTIONAL :: KSENDCOUNT
INTEGER(KIND=JPIM),INTENT(IN),OPTIONAL :: KRECVDISPL(:),KCOMM,KMP_TYPE
INTEGER(KIND=JPIM),INTENT(OUT),OPTIONAL :: KERROR,KREQUEST
CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: CDSTRING
TARGET :: KRECVDISPL

TYPE(MPI_REQUEST)  :: IREQUEST_LOCAL
INTEGER(KIND=JPIM) :: IR,ISENDCOUNT,IRECVCOUNT
INTEGER(KIND=JPIM) :: IMP_TYPE
INTEGER(KIND=JPIM) :: IERROR,IPL_NUMPROC
TYPE(MPI_COMM)     :: ICOMM
INTEGER(KIND=JPIM) :: ITID,J
INTEGER(KIND=JPIM),ALLOCATABLE,TARGET :: IRECVDISPL(:)
INTEGER(KIND=JPIM), POINTER :: IRECVDISPL_PT(:)
LOGICAL :: LKRECVCOUNTS = .TRUE. ! .TRUE. if KRECVCOUNTS is present (used in _int_scalar version)
INTEGER(KIND=JPIM) :: IRECVCOUNTS(1) ! needed for _int_scalar version preamble

ITID = OML_MY_THREAD()
IF(PRESENT(KSENDCOUNT)) THEN
   ISENDCOUNT = KSENDCOUNT
ELSE
   ISENDCOUNT = SIZE(PSENDBUF)
   ISENDCOUNT = MAX(0,ISENDCOUNT)  ! Bug? on IBM
ENDIF
IRECVCOUNT = SIZE(PRECVBUF)

!--------- Preamble repeated for threadsafe--------------
#include "mpl_allgatherv_preamble.i90"
!!--------- End of Preamble --------------

IERROR=0

IF(IMP_TYPE == JP_BLOCKING_STANDARD .OR. IMP_TYPE == JP_BLOCKING_BUFFERED) THEN
  CALL MPI_ALLGATHERV(PSENDBUF,ISENDCOUNT,MPI_REAL8,PRECVBUF,&
   & KRECVCOUNTS,IRECVDISPL_PT,MPI_REAL8,ICOMM,IERROR)
ELSEIF(IMP_TYPE == JP_NON_BLOCKING_STANDARD .OR. IMP_TYPE == JP_NON_BLOCKING_BUFFERED) THEN
  CALL MPI_IALLGATHERV(PSENDBUF,ISENDCOUNT,MPI_REAL8,PRECVBUF,&
   & KRECVCOUNTS,IRECVDISPL_PT,MPI_REAL8,ICOMM,IREQUEST_LOCAL,IERROR)
  KREQUEST=IREQUEST_LOCAL%MPI_VAL
  IF(.NOT. PRESENT(KRECVDISPL)) THEN
    CALL YDDISPLS_LIST%APPEND(KREQ=KREQUEST,NO_NEW_NODE=.TRUE.)
  ENDIF
ENDIF

IF(LMPLSTATS) THEN
  CALL MPL_SENDSTATS(ISENDCOUNT,MPI_REAL8%MPI_VAL)
  CALL MPL_RECVSTATS(SUM(KRECVCOUNTS),MPI_REAL8%MPI_VAL)
ENDIF

IF(MPL_OUTPUT > 1 )THEN
   WRITE(MPL_UNIT,'(A,5I8)') ' MPL_ALLGATHERV ',ISENDCOUNT,IRECVCOUNT,ICOMM
ENDIF
IF(PRESENT(KERROR)) THEN
   KERROR=IERROR
ELSE
  IF(IERROR /= 0 ) CALL MPL_MESSAGE(CDMESSAGE='MPL_ALLGATHERV',CDSTRING=CDSTRING,KERROR=IERROR,&
   & LDABORT=LLABORT)
ENDIF

END SUBROUTINE MPL_ALLGATHERV_REAL8

SUBROUTINE MPL_ALLGATHERV_INT(KSENDBUF,KRECVBUF,KRECVCOUNTS,KSENDCOUNT,KRECVDISPL, &
& KMP_TYPE,KCOMM,KERROR,KREQUEST,CDSTRING)

#ifdef USE_8_BYTE_WORDS
USE MPI4TO8, ONLY : &
   MPI_COMM_SIZE => MPI_COMM_SIZE8, MPI_ALLGATHERV => MPI_ALLGATHERV8
#endif

IMPLICIT NONE
INTEGER(KIND=JPIM)         :: KSENDBUF(:)
INTEGER(KIND=JPIM)         :: KRECVBUF(:)
INTEGER(KIND=JPIM),INTENT(IN) :: KRECVCOUNTS(:)
INTEGER(KIND=JPIM),INTENT(IN), OPTIONAL :: KSENDCOUNT
INTEGER(KIND=JPIM),INTENT(IN),OPTIONAL :: KRECVDISPL(:),KCOMM,KMP_TYPE
INTEGER(KIND=JPIM),INTENT(OUT),OPTIONAL :: KERROR,KREQUEST
CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: CDSTRING
TARGET :: KRECVDISPL

TYPE(MPI_REQUEST)  :: IREQUEST_LOCAL
INTEGER(KIND=JPIM) :: IR,ISENDCOUNT,IRECVCOUNT
INTEGER(KIND=JPIM) :: IMP_TYPE
INTEGER(KIND=JPIM) :: IERROR,IPL_NUMPROC
TYPE(MPI_COMM)     :: ICOMM
INTEGER(KIND=JPIM) :: ITID
INTEGER(KIND=JPIM),ALLOCATABLE,TARGET :: IRECVDISPL(:)
INTEGER(KIND=JPIM), POINTER :: IRECVDISPL_PT(:)
LOGICAL :: LKRECVCOUNTS = .TRUE. ! .TRUE. if KRECVCOUNTS is present (used in _int_scalar version)
INTEGER(KIND=JPIM) :: IRECVCOUNTS(1) ! needed for _int_scalar version preamble

ITID = OML_MY_THREAD()
IF(PRESENT(KSENDCOUNT)) THEN
   ISENDCOUNT = KSENDCOUNT
ELSE
   ISENDCOUNT = SIZE(KSENDBUF)
   ISENDCOUNT = MAX(0,ISENDCOUNT)  ! Bug? on IBM
ENDIF
IRECVCOUNT = SIZE(KRECVBUF)

!--------- Preamble repeated for threadsafe--------------
#include "mpl_allgatherv_preamble.i90"
!--------- End of Preamble --------------

IERROR=0
IF(IMP_TYPE == JP_BLOCKING_STANDARD .OR. IMP_TYPE == JP_BLOCKING_BUFFERED) THEN
  CALL MPI_ALLGATHERV(KSENDBUF,ISENDCOUNT,MPI_INTEGER,KRECVBUF,&
   & KRECVCOUNTS,IRECVDISPL_PT,MPI_INTEGER,ICOMM,IERROR)
ELSEIF(IMP_TYPE == JP_NON_BLOCKING_STANDARD .OR. IMP_TYPE == JP_NON_BLOCKING_BUFFERED) THEN
   CALL MPI_IALLGATHERV(KSENDBUF,ISENDCOUNT,MPI_INTEGER,KRECVBUF,&
   & KRECVCOUNTS,IRECVDISPL_PT,MPI_INTEGER,ICOMM,IREQUEST_LOCAL,IERROR)
  KREQUEST=IREQUEST_LOCAL%MPI_VAL
  IF(.NOT. PRESENT(KRECVDISPL)) THEN
    CALL YDDISPLS_LIST%APPEND(KREQ=KREQUEST,NO_NEW_NODE=.TRUE.)
  ENDIF
ENDIF

IF(LMPLSTATS) THEN
  CALL MPL_SENDSTATS(ISENDCOUNT,MPI_INTEGER%MPI_VAL)
  CALL MPL_RECVSTATS(SUM(KRECVCOUNTS),MPI_INTEGER%MPI_VAL)
ENDIF

IF(MPL_OUTPUT > 1 )THEN
   WRITE(MPL_UNIT,'(A,5I8)') ' MPL_ALLGATHERV ',ISENDCOUNT,IRECVCOUNT,ICOMM
ENDIF
IF(PRESENT(KERROR)) THEN
   KERROR=IERROR
ELSE
  IF(IERROR /= 0 ) CALL MPL_MESSAGE(CDMESSAGE='MPL_ALLGATHERV',CDSTRING=CDSTRING,KERROR=IERROR,LDABORT=LLABORT)
ENDIF

END SUBROUTINE MPL_ALLGATHERV_INT

SUBROUTINE MPL_ALLGATHERV_INT_SCALAR(KSENDBUF,KRECVBUF,KRECVCOUNTS,KSENDCOUNT,KRECVDISPL, &
& KMP_TYPE,KCOMM,KERROR,KREQUEST,CDSTRING)

#ifdef USE_8_BYTE_WORDS
USE MPI4TO8, ONLY : &
   MPI_COMM_SIZE => MPI_COMM_SIZE8, MPI_ALLGATHERV => MPI_ALLGATHERV8
#endif

IMPLICIT NONE
INTEGER(KIND=JPIM)         :: KSENDBUF
INTEGER(KIND=JPIM)         :: KRECVBUF(:)
INTEGER(KIND=JPIM),INTENT(IN),OPTIONAL :: KRECVCOUNTS(:)
INTEGER(KIND=JPIM),INTENT(IN), OPTIONAL :: KSENDCOUNT
INTEGER(KIND=JPIM),INTENT(IN),OPTIONAL :: KRECVDISPL(:),KCOMM,KMP_TYPE
INTEGER(KIND=JPIM),INTENT(OUT),OPTIONAL :: KERROR,KREQUEST
CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: CDSTRING
TARGET :: KRECVDISPL

TYPE(MPI_REQUEST)  :: IREQUEST_LOCAL
INTEGER(KIND=JPIM) :: IR,ISENDCOUNT,IRECVCOUNT
INTEGER(KIND=JPIM) :: IMP_TYPE
INTEGER(KIND=JPIM) :: IRECVCOUNTS(MPL_NUMPROC)
INTEGER(KIND=JPIM) :: IERROR,IPL_NUMPROC
TYPE(MPI_COMM)     :: ICOMM
INTEGER(KIND=JPIM) :: ITID
INTEGER(KIND=JPIM),ALLOCATABLE,TARGET :: IRECVDISPL(:)
INTEGER(KIND=JPIM), POINTER :: IRECVDISPL_PT(:)
LOGICAL :: LKRECVCOUNTS ! .TRUE. if KRECVCOUNTS is present

ITID = OML_MY_THREAD()
IF(PRESENT(KSENDCOUNT)) THEN
   ISENDCOUNT = KSENDCOUNT
ELSE
   ISENDCOUNT = 1
ENDIF
IRECVCOUNT = SIZE(KRECVBUF)
IF(PRESENT(KRECVCOUNTS)) THEN
   IF(ANY(KRECVCOUNTS < 0) .OR. ANY(KRECVCOUNTS > 1)) THEN
      CALL MPL_MESSAGE(CDMESSAGE='MPL_ALLGATHERV_INT_SCALAR: KRECVCOUNTS contains <0 or >1 counts',LDABORT=LLABORT)
   ENDIF
   IRECVCOUNTS=KRECVCOUNTS
   LKRECVCOUNTS = .TRUE.
ELSE
   IRECVCOUNTS(:) = 1
   LKRECVCOUNTS = .FALSE.
ENDIF
!--------- Preamble repeated for threadsafe--------------
#include "mpl_allgatherv_preamble.i90"
!--------- End of Preamble --------------

IF(IMP_TYPE == JP_BLOCKING_STANDARD .OR. IMP_TYPE == JP_BLOCKING_BUFFERED) THEN
  CALL MPI_ALLGATHERV(KSENDBUF,ISENDCOUNT,MPI_INTEGER,KRECVBUF,IRECVCOUNTS,&
   &  IRECVDISPL_PT,MPI_INTEGER,ICOMM,IERROR)
ELSEIF(IMP_TYPE == JP_NON_BLOCKING_STANDARD .OR. IMP_TYPE == JP_NON_BLOCKING_BUFFERED) THEN
   CALL MPI_IALLGATHERV(KSENDBUF,ISENDCOUNT,MPI_INTEGER,KRECVBUF,IRECVCOUNTS,&
   &  IRECVDISPL_PT,MPI_INTEGER,ICOMM,IREQUEST_LOCAL,IERROR)
  KREQUEST=IREQUEST_LOCAL%MPI_VAL
  IF(.NOT. PRESENT(KRECVDISPL)) THEN
    CALL YDDISPLS_LIST%APPEND(KREQ=KREQUEST,NO_NEW_NODE=.TRUE.)
  ENDIF
ENDIF
IF(LMPLSTATS) THEN
  CALL MPL_SENDSTATS(ISENDCOUNT,MPI_INTEGER%MPI_VAL)
  CALL MPL_RECVSTATS(SUM(IRECVCOUNTS),MPI_INTEGER%MPI_VAL)
ENDIF

IF(MPL_OUTPUT > 1 )THEN
   WRITE(MPL_UNIT,'(A,5I8)') ' MPL_ALLGATHERV ',ISENDCOUNT,IRECVCOUNT,ICOMM
ENDIF
IF(PRESENT(KERROR)) THEN
   KERROR=IERROR
ELSE
  IF(IERROR /= 0 ) CALL MPL_MESSAGE(CDMESSAGE='MPL_ALLGATHERV',CDSTRING=CDSTRING,KERROR=IERROR,LDABORT=LLABORT)
ENDIF

END SUBROUTINE MPL_ALLGATHERV_INT_SCALAR

END MODULE MPL_ALLGATHERV_MOD
