setupMPI.F90 7.73 KB
Newer Older
Daniel Wortmann's avatar
Daniel Wortmann committed
1 2 3 4 5 6 7 8 9 10 11 12
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------

MODULE m_setupMPI
  use m_juDFT
  IMPLICIT NONE

CONTAINS
  SUBROUTINE setupMPI(nkpt,mpi)
13 14
!$  use omp_lib
    USE m_types  
15
    USE m_available_solvers,ONLY:parallel_solver_available
Daniel Wortmann's avatar
Daniel Wortmann committed
16 17 18
    INTEGER,INTENT(in)           :: nkpt
    TYPE(t_mpi),INTENT(inout)    :: mpi

19 20 21 22 23
    integer :: omp=-1

    !$ omp=omp_get_max_threads()
    if (mpi%irank==0) THEN
       !print INFO on parallelization
24
       WRITE(*,*) "--------------------------------------------------------"
25 26
#ifdef CPP_MPI
       write(*,*) "Number of MPI-tasks:  ",mpi%isize
27
       CALL add_usage_data("MPI-PE",mpi%isize)     
28 29
#else
       CALL add_usage_data("MPI-PE",1)     
30
#endif
31 32 33 34 35 36 37
       IF (omp==-1) THEN
          write(*,*) "No OpenMP version of FLEUR."
          CALL add_usage_data("OMP",0)
       ELSE
          WRITE(*,*) "Number of OMP-threads:",omp
          CALL add_usage_data("OMP",omp)
       ENDIF
38
    endif
Daniel Wortmann's avatar
Daniel Wortmann committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    IF (mpi%isize==1) THEN
       !give some info on available parallelisation
       CALL priv_dist_info(nkpt)
       mpi%n_start=1
       mpi%n_stride=1
       mpi%n_rank=0
       mpi%n_size=1
       mpi%n_groups=1
       mpi%sub_comm=mpi%mpi_comm
    END IF
#ifdef CPP_MPI
    !Distribute the work
    CALL priv_distribute_k(nkpt,mpi)

53
    !Now check if parallelization is possible
Daniel Wortmann's avatar
Daniel Wortmann committed
54 55 56 57 58 59
    IF (mpi%n_size>1.AND..NOT.parallel_solver_available()) &
         CALL juDFT_error("MPI parallelization failed",hint="You have to either compile FLEUR with a parallel diagonalization library (ELPA,SCALAPACK...) or you have to run such that the No of kpoints can be distributed on the PEs")       
#endif

    !generate the MPI communicators
    CALL priv_create_comm(nkpt,mpi)
60
    if (mpi%irank==0) WRITE(*,*) "--------------------------------------------------------"
Daniel Wortmann's avatar
Daniel Wortmann committed
61 62 63 64 65 66 67 68 69 70

  END SUBROUTINE setupMPI


  SUBROUTINE priv_distribute_k(nkpt,mpi)
    use m_types
    implicit none
    INTEGER,INTENT(in)      :: nkpt
    TYPE(t_mpi),INTENT(inout)    :: mpi

71
    !-------------------------------------------------------------------------------------------
Daniel Wortmann's avatar
Daniel Wortmann committed
72 73 74 75 76
    !
    ! Distribute the k-point / eigenvector  parallelisation so, that
    ! all pe's have aproximately equal load. Maximize for k-point 
    ! parallelisation. The naming conventions are as follows:
    !
77 78 79 80 81
    ! groups             1               2               3             4      (n_groups = 4) 
    !                 /     \         /     \          /   \         /   \
    ! k-points:      1       2       3       4       5       6      7     8     (nkpts = 8)
    !               /|\     /|\     /|\     /|\     /|\     /|\    /|\   /|\
    ! irank        0 1 2   3 4 5   1 2 3   4 5 6   0 1 2   3 4 5  1 2 3  4 5 6  (mpi%isize = 6)
Daniel Wortmann's avatar
Daniel Wortmann committed
82
    !
83
    ! n_rank       0 1 2   0 1 2   0 1 2   0 1 2   0 1 2   0 1 2  0 1 2  0 1 2  (mpi%n_size = 3)
Daniel Wortmann's avatar
Daniel Wortmann committed
84 85
    !
    !
86
    ! In the above example, 6 pe's should work on 8 k-points and distribute
Daniel Wortmann's avatar
Daniel Wortmann committed
87
    ! their load in a way, that 3 pe's work on each k-points, so 2 k-points
88 89
    ! are done in parellel (n_members=2) and each processor runs a loop over
    ! 4 k-points (mpi%n_groups = 4).
Daniel Wortmann's avatar
Daniel Wortmann committed
90 91 92 93 94
    ! n_rank and n_size are the equivalents of irank and isize. The former
    ! belong to the communicator SUB_COMM, the latter to MPI_COMM.
    !
    !          G.B. `99
    !
95
    !-------------------------------------------------------------------------------------------
Daniel Wortmann's avatar
Daniel Wortmann committed
96
    INTEGER:: n_members,n_size_min
97
    CHARACTER(len=1000)::txt
Daniel Wortmann's avatar
Daniel Wortmann committed
98 99

    n_members = MIN(nkpt,mpi%isize)
100 101
    IF (judft_was_argument("-n_min_size")) THEN
       txt=judft_string_for_argument("-n_min_size")
Daniel Wortmann's avatar
Daniel Wortmann committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
       READ(txt,*) n_size_min
       WRITE(*,*) "Trying to use ",n_size_min," PE per kpt"
       n_members = MIN(n_members , CEILING(REAL(mpi%isize)/n_size_min) ) 
    ENDIF
    DO  
       IF ((MOD(mpi%isize,n_members) == 0).AND.(MOD(nkpt,n_members) == 0) ) EXIT
       n_members = n_members - 1
    ENDDO
    mpi%n_groups = nkpt/n_members
    mpi%n_size   = mpi%isize/n_members
    mpi%n_stride = n_members
    IF (mpi%irank == 0) THEN
       WRITE(*,*) 'k-points in parallel: ',n_members
       WRITE(*,*) "pe's per k-point:     ",mpi%n_size
       WRITE(*,*) '# of k-point loops:   ',mpi%n_groups
    ENDIF
  END SUBROUTINE priv_distribute_k

  SUBROUTINE priv_create_comm(nkpt,mpi)
    use m_types
    implicit none
    INTEGER,INTENT(in)      :: nkpt
    TYPE(t_mpi),INTENT(inout)    :: mpi
#ifdef CPP_MPI
126 127 128
    INTEGER :: n_members,n,i,ierr,sub_group,world_group
    INTEGER :: i_mygroup(mpi%n_size)
    LOGICAL :: compact ! Deside how to distribute k-points
Daniel Wortmann's avatar
Daniel Wortmann committed
129

130
    compact = .true.
Daniel Wortmann's avatar
Daniel Wortmann committed
131
    n_members = nkpt/mpi%n_groups
132
    
Daniel Wortmann's avatar
Daniel Wortmann committed
133
    ! now, we make the groups
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
    
    
    IF (compact) THEN

        ! This will distribute sub ranks in a compact manner.
        ! For example, if nkpt = 8 and mpi%isize = 6:
        
        !  -----------------------------------
        ! |  0  |  1  |  2  |  3  |  4  |  5  |    mpi%irank
        !  -----------------------------------
        ! |  0  |  1  |  3  |  0  |  1  |  2  |    mpi%n_rank
        !  -----------------------------------
        ! |        1        |        2        |    k - points
        ! |        3        |        4        |
        ! |        5        |        6        |
        ! |        7        |        8        |
        !  -----------------------------------
    
        mpi%n_start = INT(mpi%irank/mpi%n_size) + 1
        i_mygroup(1) = (mpi%n_start-1) * mpi%n_size
        do i = 2, mpi%n_size
           i_mygroup(i) = i_mygroup(i-1) + 1
        enddo

    ELSE

        ! This will distribute sub ranks in a spread manner.
        ! For example, if nkpt = 8 and mpi%isize = 6:
    
        !  -----------------------------------
        ! |  0  |  1  |  2  |  3  |  4  |  5  |    mpi%irank
        !  -----------------------------------
        ! |  0  |  1  |  3  |  0  |  1  |  2  |    mpi%n_rank
        !  -----------------------------------
        ! |  1  |  2  |  1  |  2  |  1  |  2  |    k - points
        ! |  3  |  4  |  3  |  4  |  3  |  4  |
        ! |  5  |  6  |  5  |  6  |  5  |  6  |  
        ! |  7  |  8  |  7  |  8  |  7  |  8  |
        !  -----------------------------------

        mpi%n_start = MOD(mpi%irank,n_members) + 1
        !!      n_start = INT(irank/n_size) * n_size
        n = 0
        DO i = mpi%n_start,mpi%isize,n_members
        !!      DO i = n_start+1,n_start+n_size
           n = n+1
           i_mygroup(n) = i-1
        ENDDO

    ENDIF ! compact
Daniel Wortmann's avatar
Daniel Wortmann committed
184 185 186 187

    CALL MPI_COMM_GROUP (mpi%MPI_COMM,WORLD_GROUP,ierr)
    CALL MPI_GROUP_INCL (WORLD_GROUP,mpi%n_size,i_mygroup,SUB_GROUP,ierr)
    CALL MPI_COMM_CREATE (mpi%MPI_COMM,SUB_GROUP,mpi%SUB_COMM,ierr)
Daniel Wortmann's avatar
Daniel Wortmann committed
188
    !write (*,"(a,i0,100i4)") "MPI:",mpi%sub_comm,mpi%irank,mpi%n_groups,mpi%n_size,n,i_mygroup
Daniel Wortmann's avatar
Daniel Wortmann committed
189 190 191 192 193 194

    CALL MPI_COMM_RANK (mpi%SUB_COMM,mpi%n_rank,ierr)
#endif
  END SUBROUTINE priv_create_comm

  SUBROUTINE priv_dist_info(nkpt)
195
    USE m_available_solvers,ONLY:parallel_solver_available
Daniel Wortmann's avatar
Daniel Wortmann committed
196
    IMPLICIT NONE
197
    INTEGER,INTENT(in)           :: nkpt
Daniel Wortmann's avatar
Daniel Wortmann committed
198 199

    INTEGER:: n,k_only,pe_k_only(nkpt)
200
    
201
#ifdef CPP_MPI
Daniel Wortmann's avatar
Daniel Wortmann committed
202 203 204 205 206 207 208 209
    !Create a list of PE that will lead to k-point parallelization only
    k_only=0
    DO n=1,nkpt
       IF (MOD(nkpt,n)==0) THEN
          k_only=k_only+1
          pe_k_only(k_only)=n
       ENDIF
    END DO
210
    WRITE(*,*) "Most efficient MPI parallelization for:"
Daniel Wortmann's avatar
Daniel Wortmann committed
211 212 213
    WRITE(*,*) pe_k_only(:k_only)
    !check if eigenvalue parallelization is possible
    IF (parallel_solver_available()) WRITE(*,*) "Additional eigenvalue parallelization possible"
214
#endif
Daniel Wortmann's avatar
Daniel Wortmann committed
215 216 217 218 219 220 221
  END SUBROUTINE priv_dist_info



END MODULE m_setupMPI