eigen_diag.F90 8.12 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! 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.
!--------------------------------------------------------------------------------

7 8 9
MODULE m_eigen_diag
  USE m_juDFT
! the parameters are set to negative values to indicate that a particular solver is not compiled
10
    IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
11
    PRIVATE
12
#ifdef CPP_ELPA
13 14 15 16 17 18 19 20 21 22 23 24 25
  INTEGER,PARAMETER:: diag_elpa=1
#else
  INTEGER,PARAMETER:: diag_elpa=-1
#endif
#ifdef CPP_ELEMENTAL
  INTEGER,PARAMETER:: diag_elemental=2
#else
  INTEGER,PARAMETER:: diag_elemental=-2
#endif
#ifdef CPP_SCALAPACK
  INTEGER,PARAMETER:: diag_scalapack=3
#else
  INTEGER,PARAMETER:: diag_scalapack=-3
26 27 28 29 30
#endif
#ifdef CPP_MAGMA
  INTEGER,PARAMETER:: diag_magma=6
#else
  INTEGER,PARAMETER:: diag_magma=-6
31
#endif
Daniel Wortmann's avatar
Daniel Wortmann committed
32
#ifdef CPP_CHASE
33
  INTEGER,PARAMETER:: diag_chase=7
Daniel Wortmann's avatar
Daniel Wortmann committed
34 35 36 37 38 39 40 41 42 43
#else
  INTEGER,PARAMETER:: diag_chase=-7
#endif
#ifdef CPP_GPU
  INTEGER,PARAMETER:: diag_cusolver=8
#else
  INTEGER,PARAMETER:: diag_cusolver=-8
#endif
  
  INTEGER,PARAMETER:: diag_lapack=4
Daniel Wortmann's avatar
Daniel Wortmann committed
44
  INTEGER,PARAMETER:: diag_debugout=99
Daniel Wortmann's avatar
Daniel Wortmann committed
45
  PUBLIC eigen_diag,parallel_solver_available
46
CONTAINS
Daniel Wortmann's avatar
Daniel Wortmann committed
47 48 49 50 51

  LOGICAL FUNCTION parallel_solver_available()
    parallel_solver_available=any((/diag_elpa,diag_elemental,diag_scalapack/)>0)
  END FUNCTION parallel_solver_available

52
  SUBROUTINE eigen_diag(mpi,hmat,smat,ikpt,jsp,iter,ne,eig,ev)
Daniel Wortmann's avatar
Daniel Wortmann committed
53
    USE m_lapack_diag
54
    USE m_magma
55
    USE m_elpa
Daniel Wortmann's avatar
Daniel Wortmann committed
56
    USE m_scalapack
57
    USE m_elemental
58 59
    USE m_chase_diag
    USE m_types_mpimat
Daniel Wortmann's avatar
Daniel Wortmann committed
60
    USE m_types_gpumat
61
    USE m_matrix_copy
Daniel Wortmann's avatar
Daniel Wortmann committed
62
    USE m_cusolver_diag
63
    IMPLICIT NONE
64
#ifdef CPP_MPI
65 66
    include 'mpif.h'
#endif
67
    TYPE(t_mpi),               INTENT(IN)    :: mpi
68 69 70 71
    CLASS(t_mat),              INTENT(INOUT) :: smat,hmat
    CLASS(t_mat), ALLOCATABLE, INTENT(OUT)   :: ev
    INTEGER,                   INTENT(IN)    :: ikpt
    INTEGER,                   INTENT(IN)    :: jsp
72
    INTEGER,                   INTENT(IN)    :: iter
73 74
    INTEGER,                   INTENT(INOUT) :: ne
    REAL,                      INTENT(OUT)   :: eig(:)
75

76
    !Locals  
77
    LOGICAL :: parallel
78 79
    !For check-mode
    TYPE(t_mat) :: s_store,h_store
80
    
81
         
Daniel Wortmann's avatar
Daniel Wortmann committed
82 83 84 85 86 87
    SELECT TYPE(smat)
    CLASS IS (t_mpimat)
       parallel=.TRUE.
    CLASS default
       parallel=.FALSE.
    END SELECT
88 89

    !Create a copy of the matrix if in test mode
90
    IF (TRIM(judft_string_for_argument("-diag"))=="test") THEN
91 92 93 94 95 96 97 98 99 100 101 102
       SELECT TYPE(hmat)
        CLASS IS (t_mpimat)
          CALL s_store%init(hmat%l_real,hmat%global_size1,hmat%global_size2)
          CALL h_store%init(hmat%l_real,hmat%global_size1,hmat%global_size2)
       CLASS default
          CALL s_store%init(hmat%l_real,hmat%matsize1,hmat%matsize2)
          CALL h_store%init(hmat%l_real,hmat%matsize1,hmat%matsize2)
       END SELECT
       CALL matrix_copy(smat,s_store)
       CALL matrix_copy(hmat,h_store)
    END IF
       
Daniel Wortmann's avatar
Daniel Wortmann committed
103 104 105 106
    CALL timestart("Diagonalization")
    !Select the solver
    SELECT CASE (priv_select_solver(parallel))
    CASE (diag_elpa)
Uliana Alekseeva's avatar
Uliana Alekseeva committed
107
       CALL elpa_diag(hmat,smat,ne,eig,ev)
Daniel Wortmann's avatar
Daniel Wortmann committed
108 109 110 111 112 113
    CASE (diag_elemental)
       !CALL ELEMENTAL(hmat,smat,ne,eig,ev)
    CASE (diag_scalapack)
       CALL scalapack(hmat,smat,ne,eig,ev)
    CASE (diag_magma)
       !CALL magma_diag(hmat,smat,ne,eig,ev)
Daniel Wortmann's avatar
Daniel Wortmann committed
114 115
    CASE (diag_cusolver)
       CALL cusolver_diag(hmat,smat,ne,eig,ev)
Daniel Wortmann's avatar
Daniel Wortmann committed
116 117
    CASE (diag_lapack)
       CALL lapack_diag(hmat,smat,ne,eig,ev)
118
    CASE (diag_chase)
119
       CALL chase_diag(hmat,smat,ikpt,jsp,iter,ne,eig,ev)
Daniel Wortmann's avatar
Daniel Wortmann committed
120 121 122
    CASE (diag_debugout)
       CALL priv_debug_out(smat,hmat)
    END SELECT
123 124

    !Create test the solution
125
    IF (TRIM(judft_string_for_argument("-diag"))=="test") THEN
126 127 128
       CALL priv_test_solution(mpi,ne,s_store,h_store,eig,ev)
       CALL judft_error("Diagonalization tested")
    END IF
Daniel Wortmann's avatar
Daniel Wortmann committed
129
    CALL timestop("Diagonalization")
130 131 132
    !
  END SUBROUTINE eigen_diag

Daniel Wortmann's avatar
Daniel Wortmann committed
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
  SUBROUTINE priv_test_solution(mpi,ne,s_store,h_store,eig1,ev)
    USE m_types
    USE m_lapack_diag
    USE m_matrix_copy
    IMPLICIT NONE
    TYPE(t_mpi),INTENT(in):: mpi
    INTEGER,INTENT(INOUT) :: ne
    TYPE(t_mat)           :: s_store,h_store
    REAL,INTENT(in)       :: eig1(:)
    CLASS(t_mat)          :: ev

    
    REAL,ALLOCATABLE::eig2(:)
    TYPE(t_mat)     ::ev1
    CLASS(t_mat),ALLOCATABLE    ::ev2
    INTEGER         :: i,irank
    

    ALLOCATE(eig2(ne))
    CALL ev1%init(s_store%l_real,s_store%matsize1,s_store%matsize2)
    CALL matrix_copy(ev,ev1)
    
    IF (mpi%irank==0) THEN
       CALL lapack_diag(h_store,s_store,ne,eig2,ev2)

       OPEN(99,file="diag.compare")
       WRITE(99,*) "Eigenvalues"
       DO i=1,ne
          WRITE(99,*) i,eig1(i),eig2(i)
       ENDDO
       WRITE(99,*) "Eigenvectors"
       DO i=1,ne
          IF (ev1%l_real) THEN
             WRITE(99,"(i0,20(1x,f10.5))") i,ev1%data_r(1:10,i)
             WRITE(99,"(i0,20(1x,f10.5))") i,ev2%data_r(1:10,i)
          ELSE
             WRITE(99,"(i0,20(1x,f10.5))") i,ev1%data_c(1:10,i)
             WRITE(99,"(i0,20(1x,f10.5))") i,ev2%data_c(1:10,i)
          END IF
       ENDDO
       CLOSE(99)
    END IF
  END SUBROUTINE priv_test_solution
  
Daniel Wortmann's avatar
Daniel Wortmann committed
178
  SUBROUTINE priv_debug_out(smat,hmat)
179 180 181
    USE m_types
    use m_judft
    IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
182
    CLASS(t_mat),INTENT(INOUT) :: hmat,smat
183
    !small subroutine that does only wite the matrix to a file
Daniel Wortmann's avatar
Daniel Wortmann committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    INTEGER:: i,ii,irank,ierr
    CHARACTER(len=20)::filename
#ifdef CPP_MPI
    INCLUDE 'mpif.h'
    CALL MPI_COMM_RANK(MPI_COMM_WORLD,irank,ierr)
#else
    irank=0
#endif
    WRITE(filename,"(a,i0)") "hmat",irank
    OPEN(999,file=TRIM(filename))
    WRITE(filename,"(a,i0)") "smat",irank
    OPEN(998,file=TRIM(filename))
    DO i=1,hmat%matsize2
       DO ii=1,hmat%matsize1
          IF (hmat%l_real) THEN
             WRITE(999,"(2i6,f15.6)") ii,i,hmat%data_r(ii,i)
             WRITE(998,"(2i6,f15.6)") ii,i,smat%data_r(ii,i)
          ELSE
             WRITE(999,"(2i6,2f15.6)") ii,i,hmat%data_c(ii,i)
             WRITE(998,"(2i6,2f15.6)") ii,i,smat%data_c(ii,i)
          ENDIF
205
       END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
206
    ENDDO
207 208
    CLOSE(999)
    CLOSE(998)
Daniel Wortmann's avatar
Daniel Wortmann committed
209 210 211
#ifdef CPP_MPI
    CALL MPI_BARRIER(MPI_COMM_WORLD,ierr)
#endif
212 213 214
    CALL judft_error("STOP in eigen_diag:debug_diag")
  END SUBROUTINE priv_debug_out

Daniel Wortmann's avatar
Daniel Wortmann committed
215
  FUNCTION priv_select_solver(parallel) RESULT(diag_solver)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    LOGICAL,INTENT(IN):: parallel
    INTEGER           :: diag_solver
    diag_solver=-99

    !Determine the default solver
    IF (parallel) THEN
#ifdef CPP_ELPA
       diag_solver=diag_elpa
#else
       diag_solver=diag_scalapack
#endif
    ELSE
       diag_solver=diag_lapack
    ENDIF

    !check if a special solver was requested
232 233 234 235 236 237
    IF (TRIM(juDFT_string_for_argument("-diag"))=="elpa")      diag_solver=diag_elpa
    IF (trim(juDFT_string_for_argument("-diag"))=="scalapack") diag_solver=diag_scalapack
    IF (trim(juDFT_string_for_argument("-diag"))=="elemental") diag_solver=diag_elemental
    IF (trim(juDFT_string_for_argument("-diag"))=="lapack")    diag_solver=diag_lapack
    IF (trim(juDFT_string_for_argument("-diag"))=="magma")     diag_solver=diag_magma
    IF (trim(juDFT_string_for_argument("-diag"))=="chase")     diag_solver=diag_chase
Daniel Wortmann's avatar
Daniel Wortmann committed
238
    IF (trim(juDFT_string_for_argument("-diag"))=="cusolver")  diag_solver=diag_cusolver
239
    IF (trim(juDFT_string_for_argument("-diag"))=="debugout")  diag_solver=diag_debugout
Daniel Wortmann's avatar
Daniel Wortmann committed
240
    
241
    !Check if solver is possible
Daniel Wortmann's avatar
Daniel Wortmann committed
242 243 244
    IF (diag_solver<0)  CALL juDFT_error("You selected a solver for the eigenvalue problem that is not available",hint="You most probably did not provide the appropriate libraries for compilation/linking")
    IF (ANY((/diag_lapack,diag_magma,diag_cusolver/)==diag_solver).AND.parallel) CALL judft_error("You selected an eigensolver that does not support distributed memory parallism",hint="Try scalapack,elpa or another supported solver for parallel matrices")
    IF (ANY((/diag_elpa,diag_elemental,diag_scalapack/)==diag_solver).AND..NOT.parallel) CALL judft_error("You selected an eigensolver for matrices that are memory distributed",hint="Try lapack, cusolver or another supported solver for non-distributed matrices")
245 246 247

  END FUNCTION priv_select_solver

Daniel Wortmann's avatar
Daniel Wortmann committed
248

Daniel Wortmann's avatar
Daniel Wortmann committed
249
 
250 251

END MODULE m_eigen_diag