Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Wrapper for cuSOLVERMp
10 : !> \author Ole Schuett
11 : ! **************************************************************************************************
12 : MODULE cp_fm_cusolver_api
13 : USE ISO_C_BINDING, ONLY: C_DOUBLE,&
14 : C_DOUBLE_COMPLEX,&
15 : C_INT,&
16 : C_PTR
17 : USE cp_blacs_env, ONLY: cp_blacs_env_type
18 : USE cp_cfm_types, ONLY: cp_cfm_type
19 : USE cp_fm_types, ONLY: cp_fm_type
20 : USE kinds, ONLY: dp
21 : #include "../base/base_uses.f90"
22 :
23 : IMPLICIT NONE
24 :
25 : PRIVATE
26 :
27 : PUBLIC :: cp_fm_diag_cusolver
28 : PUBLIC :: cp_cfm_general_cusolver
29 : PUBLIC :: cp_fm_general_cusolver
30 :
31 : CONTAINS
32 :
33 : ! **************************************************************************************************
34 : !> \brief Driver routine to diagonalize a FM matrix with the cuSOLVERMp library.
35 : !> \param matrix the matrix that is diagonalized
36 : !> \param eigenvectors eigenvectors of the input matrix
37 : !> \param eigenvalues eigenvalues of the input matrix
38 : !> \author Ole Schuett
39 : ! **************************************************************************************************
40 0 : SUBROUTINE cp_fm_diag_cusolver(matrix, eigenvectors, eigenvalues)
41 : TYPE(cp_fm_type), INTENT(IN) :: matrix, eigenvectors
42 : REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
43 :
44 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_diag_cusolver'
45 :
46 : INTEGER :: handle, n, nmo
47 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues_buffer
48 : TYPE(cp_blacs_env_type), POINTER :: context
49 : INTERFACE
50 : SUBROUTINE cp_fm_diag_cusolver_c(cusolver_context, fortran_comm, matrix_desc, &
51 : nprow, npcol, myprow, mypcol, &
52 : n, matrix, eigenvectors, eigenvalues) &
53 : BIND(C, name="cp_fm_diag_cusolver")
54 : IMPORT :: C_INT, C_DOUBLE, C_PTR
55 : TYPE(C_PTR), INTENT(INOUT) :: cusolver_context
56 : INTEGER(kind=C_INT), VALUE :: fortran_comm
57 : INTEGER(kind=C_INT), DIMENSION(*) :: matrix_desc
58 : INTEGER(kind=C_INT), VALUE :: nprow
59 : INTEGER(kind=C_INT), VALUE :: npcol
60 : INTEGER(kind=C_INT), VALUE :: myprow
61 : INTEGER(kind=C_INT), VALUE :: mypcol
62 : INTEGER(kind=C_INT), VALUE :: n
63 : REAL(kind=C_DOUBLE), DIMENSION(*) :: matrix
64 : REAL(kind=C_DOUBLE), DIMENSION(*) :: eigenvectors
65 : REAL(kind=C_DOUBLE), DIMENSION(*) :: eigenvalues
66 : END SUBROUTINE cp_fm_diag_cusolver_c
67 : END INTERFACE
68 :
69 0 : CALL timeset(routineN, handle)
70 :
71 : #if defined(__CUSOLVERMP)
72 : n = matrix%matrix_struct%nrow_global
73 : context => matrix%matrix_struct%context
74 :
75 : ! The passed eigenvalues array might be smaller than n.
76 : ALLOCATE (eigenvalues_buffer(n))
77 :
78 : CALL cp_fm_diag_cusolver_c( &
79 : cusolver_context=context%cusolver_context, &
80 : fortran_comm=matrix%matrix_struct%para_env%get_handle(), &
81 : matrix_desc=matrix%matrix_struct%descriptor, &
82 : nprow=context%num_pe(1), &
83 : npcol=context%num_pe(2), &
84 : myprow=context%mepos(1), &
85 : mypcol=context%mepos(2), &
86 : n=matrix%matrix_struct%nrow_global, &
87 : matrix=matrix%local_data, &
88 : eigenvectors=eigenvectors%local_data, &
89 : eigenvalues=eigenvalues_buffer)
90 :
91 : nmo = SIZE(eigenvalues)
92 : eigenvalues(1:nmo) = eigenvalues_buffer(1:nmo)
93 :
94 : #else
95 : MARK_USED(matrix)
96 : MARK_USED(eigenvectors)
97 0 : eigenvalues = 0.0_dp
98 : MARK_USED(n)
99 : MARK_USED(nmo)
100 : MARK_USED(eigenvalues_buffer)
101 : MARK_USED(context)
102 0 : CPABORT("CP2K compiled without the cuSOLVERMp library.")
103 : #endif
104 :
105 0 : CALL timestop(handle)
106 0 : END SUBROUTINE cp_fm_diag_cusolver
107 :
108 : ! **************************************************************************************************
109 : !> \brief Driver routine to solve generalized eigenvalue problem A*x = lambda*B*x with cuSOLVERMp.
110 : !> \param aMatrix the first matrix for the generalized eigenvalue problem
111 : !> \param bMatrix the second matrix for the generalized eigenvalue problem
112 : !> \param eigenvectors eigenvectors of the input matrix
113 : !> \param eigenvalues eigenvalues of the input matrix
114 : ! **************************************************************************************************
115 0 : SUBROUTINE cp_fm_general_cusolver(aMatrix, bMatrix, eigenvectors, eigenvalues)
116 : USE ISO_C_BINDING, ONLY: C_INT, C_DOUBLE
117 : TYPE(cp_fm_type), INTENT(IN) :: aMatrix, bMatrix, eigenvectors
118 : REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
119 :
120 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_general_cusolver'
121 :
122 : INTEGER(kind=C_INT) :: handle, n
123 : TYPE(cp_blacs_env_type), POINTER :: context
124 : INTERFACE
125 : SUBROUTINE cp_fm_general_cusolver_c(cusolver_context, fortran_comm, a_matrix_desc, b_matrix_desc, &
126 : nprow, npcol, myprow, mypcol, &
127 : n, nmo, aMatrix, bMatrix, eigenvectors, eigenvalues) &
128 : BIND(C, name="cp_fm_diag_cusolver_sygvd")
129 : IMPORT :: C_INT, C_DOUBLE, C_PTR
130 : TYPE(C_PTR), INTENT(INOUT) :: cusolver_context
131 : INTEGER(kind=C_INT), VALUE :: fortran_comm
132 : INTEGER(kind=C_INT), DIMENSION(*) :: a_matrix_desc, b_matrix_desc
133 : INTEGER(kind=C_INT), VALUE :: nprow
134 : INTEGER(kind=C_INT), VALUE :: npcol
135 : INTEGER(kind=C_INT), VALUE :: myprow
136 : INTEGER(kind=C_INT), VALUE :: mypcol
137 : INTEGER(kind=C_INT), VALUE :: n
138 : INTEGER(kind=C_INT), VALUE :: nmo
139 : REAL(kind=C_DOUBLE), DIMENSION(*) :: aMatrix
140 : REAL(kind=C_DOUBLE), DIMENSION(*) :: bMatrix
141 : REAL(kind=C_DOUBLE), DIMENSION(*) :: eigenvectors
142 : REAL(kind=C_DOUBLE), DIMENSION(*) :: eigenvalues
143 : END SUBROUTINE cp_fm_general_cusolver_c
144 : END INTERFACE
145 :
146 0 : CALL timeset(routineN, handle)
147 :
148 : #if defined(__CUSOLVERMP)
149 : n = INT(aMatrix%matrix_struct%nrow_global, C_INT)
150 : context => aMatrix%matrix_struct%context
151 :
152 : CALL cp_fm_general_cusolver_c( &
153 : cusolver_context=context%cusolver_context, &
154 : fortran_comm=INT(aMatrix%matrix_struct%para_env%get_handle(), C_INT), &
155 : a_matrix_desc=INT(aMatrix%matrix_struct%descriptor, C_INT), &
156 : b_matrix_desc=INT(bMatrix%matrix_struct%descriptor, C_INT), &
157 : nprow=INT(context%num_pe(1), C_INT), &
158 : npcol=INT(context%num_pe(2), C_INT), &
159 : myprow=INT(context%mepos(1), C_INT), &
160 : mypcol=INT(context%mepos(2), C_INT), &
161 : n=n, &
162 : nmo=INT(SIZE(eigenvalues), C_INT), &
163 : aMatrix=aMatrix%local_data, &
164 : bMatrix=bMatrix%local_data, &
165 : eigenvectors=eigenvectors%local_data, &
166 : eigenvalues=eigenvalues)
167 : #else
168 : MARK_USED(aMatrix)
169 : MARK_USED(bMatrix)
170 : MARK_USED(eigenvectors)
171 0 : eigenvalues = 0.0_dp
172 : MARK_USED(n)
173 : MARK_USED(context)
174 0 : CPABORT("CP2K compiled without the cuSOLVERMp library.")
175 : #endif
176 :
177 0 : CALL timestop(handle)
178 0 : END SUBROUTINE cp_fm_general_cusolver
179 :
180 : ! **************************************************************************************************
181 : !> \brief Driver routine to solve generalized complex eigenvalue problem A*x = lambda*B*x with
182 : !> cuSOLVERMp.
183 : !> \param aMatrix the first matrix for the generalized eigenvalue problem
184 : !> \param bMatrix the second matrix for the generalized eigenvalue problem
185 : !> \param eigenvectors eigenvectors of the input matrix
186 : !> \param eigenvalues eigenvalues of the input matrix
187 : ! **************************************************************************************************
188 0 : SUBROUTINE cp_cfm_general_cusolver(aMatrix, bMatrix, eigenvectors, eigenvalues)
189 : USE ISO_C_BINDING, ONLY: C_DOUBLE, C_DOUBLE_COMPLEX, C_INT
190 : TYPE(cp_cfm_type), INTENT(IN) :: aMatrix, bMatrix, eigenvectors
191 : REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
192 :
193 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_general_cusolver'
194 :
195 : INTEGER(kind=C_INT) :: handle, n
196 : TYPE(cp_blacs_env_type), POINTER :: context
197 : INTERFACE
198 : SUBROUTINE cp_cfm_general_cusolver_c(cusolver_context, fortran_comm, a_matrix_desc, b_matrix_desc, &
199 : nprow, npcol, myprow, mypcol, &
200 : n, nmo, aMatrix, bMatrix, eigenvectors, eigenvalues) &
201 : BIND(C, name="cp_cfm_diag_cusolver_hegvd")
202 : IMPORT :: C_DOUBLE, C_DOUBLE_COMPLEX, C_INT, C_PTR
203 : TYPE(C_PTR), INTENT(INOUT) :: cusolver_context
204 : INTEGER(kind=C_INT), VALUE :: fortran_comm
205 : INTEGER(kind=C_INT), DIMENSION(*) :: a_matrix_desc, b_matrix_desc
206 : INTEGER(kind=C_INT), VALUE :: nprow
207 : INTEGER(kind=C_INT), VALUE :: npcol
208 : INTEGER(kind=C_INT), VALUE :: myprow
209 : INTEGER(kind=C_INT), VALUE :: mypcol
210 : INTEGER(kind=C_INT), VALUE :: n
211 : INTEGER(kind=C_INT), VALUE :: nmo
212 : COMPLEX(kind=C_DOUBLE_COMPLEX), DIMENSION(*) :: aMatrix
213 : COMPLEX(kind=C_DOUBLE_COMPLEX), DIMENSION(*) :: bMatrix
214 : COMPLEX(kind=C_DOUBLE_COMPLEX), DIMENSION(*) :: eigenvectors
215 : REAL(kind=C_DOUBLE), DIMENSION(*) :: eigenvalues
216 : END SUBROUTINE cp_cfm_general_cusolver_c
217 : END INTERFACE
218 :
219 0 : CALL timeset(routineN, handle)
220 :
221 : #if defined(__CUSOLVERMP)
222 : n = INT(aMatrix%matrix_struct%nrow_global, C_INT)
223 : context => aMatrix%matrix_struct%context
224 :
225 : CALL cp_cfm_general_cusolver_c( &
226 : cusolver_context=context%cusolver_context, &
227 : fortran_comm=INT(aMatrix%matrix_struct%para_env%get_handle(), C_INT), &
228 : a_matrix_desc=INT(aMatrix%matrix_struct%descriptor, C_INT), &
229 : b_matrix_desc=INT(bMatrix%matrix_struct%descriptor, C_INT), &
230 : nprow=INT(context%num_pe(1), C_INT), &
231 : npcol=INT(context%num_pe(2), C_INT), &
232 : myprow=INT(context%mepos(1), C_INT), &
233 : mypcol=INT(context%mepos(2), C_INT), &
234 : n=n, &
235 : nmo=INT(SIZE(eigenvalues), C_INT), &
236 : aMatrix=aMatrix%local_data, &
237 : bMatrix=bMatrix%local_data, &
238 : eigenvectors=eigenvectors%local_data, &
239 : eigenvalues=eigenvalues)
240 : #else
241 : MARK_USED(aMatrix)
242 : MARK_USED(bMatrix)
243 : MARK_USED(eigenvectors)
244 0 : eigenvalues = 0.0_dp
245 : MARK_USED(n)
246 : MARK_USED(context)
247 0 : CPABORT("CP2K compiled without the cuSOLVERMp library.")
248 : #endif
249 :
250 0 : CALL timestop(handle)
251 0 : END SUBROUTINE cp_cfm_general_cusolver
252 :
253 : END MODULE cp_fm_cusolver_api
|