mingeselle.F90 8.48 KB
Newer Older
1 2 3
MODULE m_mingeselle
  USE m_juDFT
CONTAINS
4
  SUBROUTINE mingeselle(SUB_COMM,n_size,n_rank,nv, ahelp,l_real,aa_r,aa_c)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    !------------------------------------------------------------------+
    !                                                                  |
    ! Transfers the spin-down/spin-up part , upper triangle of the     |
    ! MT-hamiltonian from the help-array ``ahelp'' to the H-matrix.    |
    ! For eigenvector-parallelization this needs some communication    |
    ! between the nodes, since this part is created 'column-wise'      |
    ! but needed row-wise.                                             |
    !                                                                  |
    !     n_s(i): number of elements to send to pe #i                  |
    !     n_r(i): number of elements to receive from pe #i             |
    !     ns_tot,nr_tot : total number of elements to send/receive     |
    !     n2_start: pe that has first column of 2nd spin part          |
    !     cs_el,cr_el: send and receive elements                       |
    !     in_pos(n,i): where to put in the n'th element sent by pe #i  |
    !                                                                  |
    !------------------------------------------------------------------+
#include"./cpp_double.h"

    IMPLICIT NONE
    ! ..
    ! .. Scalar Arguments
    INTEGER, INTENT (IN) :: n_size,n_rank,SUB_COMM
    ! ..
    ! .. Array Arguments
29
    INTEGER, INTENT (IN)    :: nv(2)
30
    COMPLEX, INTENT (INOUT) :: ahelp(:)!(m_ahelp)
31
    LOGICAL, INTENT (IN)    :: l_real
32 33
    REAL,    INTENT (INOUT) :: aa_r(:)!(matsize)
    COMPLEX, INTENT (INOUT) :: aa_c(:)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    ! ..
    ! .. Local Scalars
    INTEGER ki,kj,ns_tot,nr_tot,n_p,n2_start,n_help
    INTEGER ns_max,nr_max,n_pos,np_s,np_r,nv_s,ii,i
    INTEGER inext,ifront,req_s,req_r
    ! ..
    ! .. Local Arrays
    INTEGER n_s(0:n_size-1),n_send(0:n_size-1)
    INTEGER n_r(0:n_size-1),n_recv(0:n_size-1),ierr(3)
    INTEGER, ALLOCATABLE :: in_pos(:,:)
    COMPLEX, ALLOCATABLE :: cs_el(:,:),cr_el(:),b_b(:),c_help(:,:)

    INCLUDE 'mpif.h'
    INTEGER stt(MPI_STATUS_SIZE)
    ! ..
    !
    ! kick out the diagonal elements of ahelp
    !
    i  = 0
    ii = 0
    DO ki =  n_rank+1, nv(1), n_size
       DO kj = 1,ki - 1
          i  =  i + 1
          ii = ii + 1
          ahelp(i) = ahelp(ii)
       END DO
       ii = ii + 1
    ENDDO
    !
    ! initialize
    !
    ns_tot = 0
    nr_tot = 0
    DO n_p = 0,n_size-1
       n_s(n_p) = 0
       n_r(n_p) = 0
    ENDDO
    !
    ! determine number of elements to send to other pe's
    !
    n2_start = MOD(nv(1),n_size) - 1
    DO ki = 1, nv(1)
       IF ( MOD(ki-1,n_size).EQ.n_rank ) THEN
          DO kj = 1, ki-1
             ns_tot = ns_tot + 1
             n_p = MOD((kj+n2_start),n_size)
             n_s(n_p) = n_s(n_p) + 1
          ENDDO
       ENDIF
    ENDDO
    !
    ! determine number of elements to receive from other pe's
    !
    DO ki = 1, nv(2)
       IF ( MOD(ki+nv(1)-1,n_size).EQ.n_rank ) THEN
          DO kj = ki+1, nv(2)
             nr_tot = nr_tot + 1
             n_p = MOD(kj-1,n_size)
             n_r(n_p) = n_r(n_p) + 1
          ENDDO
       ENDIF
    ENDDO
    !
    !      WRITE (*,*) ns_tot,(n_s(n_p),n_p=0,n_size-1)
    !      WRITE (*,*) nr_tot,(n_r(n_p),n_p=0,n_size-1)
    !
    ! determine the maximal number of s/r-counts and allocate s/r-arrays
    !
    ns_max = 0
    nr_max = 0
    DO n_p = 0,n_size-1
       ns_max = MAX(ns_max,n_s(n_p))
       nr_max = MAX(nr_max,n_r(n_p))
    ENDDO
    !      WRITE (*,*) ns_max ,nr_max  , n_size, n_rank
    ALLOCATE ( cs_el(ns_max,0:n_size-1),cr_el(nr_max), in_pos(nr_max,0:n_size-1) )
    !
    ! sort the elements of aahelp-array into the send-arrays
    !
    n_help = 0
    DO n_p = 0,n_size-1
       n_send(n_p) = 0
    ENDDO
    DO ki = 1, nv(1)
       IF ( MOD(ki-1,n_size).EQ.n_rank ) THEN
          DO kj = 1, ki-1
             n_help = n_help + 1
             n_p = MOD((kj+n2_start),n_size)
             n_send(n_p) = n_send(n_p) + 1
             cs_el(n_send(n_p),n_p) = ahelp(n_help)
          ENDDO
       ENDIF
    ENDDO
    IF (n_help/=ns_tot)  CALL juDFT_error("n_help.NE.ns_to         t",calledby ="mingeselle")
    DO n_p = 0,n_size-1
       IF (n_send(n_p)/=n_s(n_p))  CALL juDFT_error("n_send.NE.n_s" ,calledby ="mingeselle")
    ENDDO
    !
    ! resort send array: rows <-> columns
    !
    DO n_p = 0,n_size-1
       nv_s = NINT(SQRT(2.0*n_send(n_p))-0.5)
       ALLOCATE ( c_help(nv_s,nv_s) )

       n_help = 0
       DO ki = 1,nv_s
          DO kj = 1,ki
             n_help = n_help + 1
             c_help(ki,kj) = cs_el(n_help,n_p)
          ENDDO
       ENDDO

       n_help = 0
       DO kj = 1,nv_s
          DO ki = kj ,nv_s
             n_help = n_help + 1
             cs_el(n_help,n_p) = c_help(ki,kj)
          ENDDO
       ENDDO

       DEALLOCATE ( c_help )
    ENDDO
    !
    ! now we look where to put in the received elements
    !
    n_pos = 0
    DO n_p = 0,n_size-1
       n_recv(n_p) = 0
    ENDDO
    DO ki = 1, nv(1)+nv(2)
       IF ( MOD(ki-1,n_size).EQ.n_rank ) THEN
          DO kj = 1, ki
             n_pos = n_pos + 1 
             IF ( ki.GT.nv(1) ) THEN
                IF ((kj.GT.ki-nv(1)).AND.(kj.LE.nv(1))) THEN
                   n_p = MOD(kj-1,n_size)
                   n_recv(n_p) = n_recv(n_p) + 1
                   in_pos(n_recv(n_p),n_p) = n_pos
                ENDIF
             ENDIF
          ENDDO
       ENDIF
    ENDDO
    DO n_p = 0,n_size-1
       IF (n_recv(n_p)/=n_r(n_p))  CALL juDFT_error("n_recv.NE.n_s" ,calledby ="mingeselle")
    ENDDO
    !
    ! Mandaliet, mandaliet, min geselle kumme niet
    !
    ifront = ibefore(n_size,n_rank)
    inext  = iafter (n_size,n_rank)
    DO n_p = 0,n_size-1
       !
       ! determine pe's to send to and to receive from
       !
       np_s = MOD(inext +n_p,n_size)
       np_r = MOD(ifront-n_p,n_size)
       IF (np_r.LT.0) np_r = np_r + n_size
       !
       ! send section: local rows i with mod(i-1,np) = np_s will be sent to proc np_s
       !

       IF (np_s.NE.n_rank) THEN
          CALL MPI_ISEND(cs_el(1,np_s),n_send(np_s), CPP_MPI_COMPLEX,&
               np_s,n_rank,SUB_COMM,req_s,ierr)
          !          write (*,*) n_rank,'sends',n_send(np_s),'to',np_s
          !          write (*,'(i2,10f10.7)') n_rank,(real(cs_el(ki,np_s)),ki=1,10)
       ENDIF

       !
       ! receive section : local rows i  with mod(i-1,np) = np_r will be received from np_r
       ! ... skipped, if update matrix from local data:
       !
       IF (np_r.NE.n_rank) THEN
          CALL MPI_IRECV(cr_el,n_recv(np_r),CPP_MPI_COMPLEX, MPI_ANY_SOURCE,np_r,SUB_COMM,req_r,ierr)
          CALL MPI_WAIT(req_s,stt,ierr)
          CALL MPI_WAIT(req_r,stt,ierr)
          !          write (*,*) n_rank,'recvs',ierr,n_p,np_r
          !          write(*,*) n_rank,'receives',n_recv(np_r),'from',np_r
          !          write (*,'(i2,10f10.7)') n_rank,(real(cr_el(ki)),ki=1,10)
          !
          ! now update the matrix aa()
          !
217 218 219 220 221
          IF (l_real) THEN
             aa_r(in_pos(:n_recv(np_r),np_r)) = aa_r(in_pos(:n_recv(np_r),np_r)) + cr_el(:n_recv(np_r))
          ELSE
             aa_c(in_pos(:n_recv(np_r),np_r)) = aa_c(in_pos(:n_recv(np_r),np_r)) + cr_el(:n_recv(np_r))
          ENDIF
222
       ELSE
223 224 225 226 227
          IF (l_real) THEN
             aa_r(in_pos(:n_recv(np_r),np_r)) = aa_r(in_pos(:n_recv(np_r),np_r)) + cs_el(:n_recv(np_r),np_s)
          ELSE
             aa_c(in_pos(:n_recv(np_r),np_r)) = aa_c(in_pos(:n_recv(np_r),np_r)) + cs_el(:n_recv(np_r),np_s)
          ENDIF
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
       ENDIF
       !         CALL MPI_BARRIER(SUB_COMM,ierr)
    ENDDO

    DEALLOCATE (cs_el,cr_el,in_pos)

  END SUBROUTINE mingeselle
  !
  !-------------------------------------------------------------
  !
  INTEGER FUNCTION ibefore(np, p)
    !
    ! Determine (in a ring structure) which is the front process
    !
    IMPLICIT NONE
    INTEGER, INTENT (IN) :: np  !  number of processes
    INTEGER, INTENT (IN) :: p   !  current processes

    IF ( p > 0 ) THEN
       ibefore = p-1
    ELSE
       ibefore = np-1
    ENDIF

  END FUNCTION ibefore
  !
  !-------------------------------------------------------------
  !
  INTEGER FUNCTION iafter(np, p)
    !
    ! Determine (in a ring structure) which is the next process
    !
    IMPLICIT NONE
    INTEGER, INTENT (IN) :: np  !  number of processes
    INTEGER, INTENT (IN) :: p   !  current processes

    IF ( p < np-1 ) THEN
       iafter = p+1
    ELSE
       iafter = 0
    ENDIF

  END FUNCTION iafter
  !
  !-------------------------------------------------------------
  !
END MODULE m_mingeselle