(git:3d5d2e5)
Loading...
Searching...
No Matches
cp_fm_cusolver_api.F
Go to the documentation of this file.
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! **************************************************************************************************
13 USE iso_c_binding, ONLY: c_double,&
14 c_double_complex,&
15 c_int,&
16 c_ptr
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
30
31CONTAINS
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 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 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 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 eigenvalues = 0.0_dp
98 mark_used(n)
99 mark_used(nmo)
100 mark_used(eigenvalues_buffer)
101 mark_used(context)
102 cpabort("CP2K compiled without the cuSOLVERMp library.")
103#endif
104
105 CALL timestop(handle)
106 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 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 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 eigenvalues = 0.0_dp
172 mark_used(n)
173 mark_used(context)
174 cpabort("CP2K compiled without the cuSOLVERMp library.")
175#endif
176
177 CALL timestop(handle)
178 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 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 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 eigenvalues = 0.0_dp
245 mark_used(n)
246 mark_used(context)
247 cpabort("CP2K compiled without the cuSOLVERMp library.")
248#endif
249
250 CALL timestop(handle)
251 END SUBROUTINE cp_cfm_general_cusolver
252
253END MODULE cp_fm_cusolver_api
methods related to the blacs parallel environment
Represents a complex full matrix distributed on many processors.
Wrapper for cuSOLVERMp.
subroutine, public cp_fm_general_cusolver(amatrix, bmatrix, eigenvectors, eigenvalues)
Driver routine to solve generalized eigenvalue problem A*x = lambda*B*x with cuSOLVERMp.
subroutine, public cp_cfm_general_cusolver(amatrix, bmatrix, eigenvectors, eigenvalues)
Driver routine to solve generalized complex eigenvalue problem A*x = lambda*B*x with cuSOLVERMp.
subroutine, public cp_fm_diag_cusolver(matrix, eigenvectors, eigenvalues)
Driver routine to diagonalize a FM matrix with the cuSOLVERMp library.
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
represent a full matrix