Debian Codegraph @

src/mbd_scalapack.f90

raw
1 ! This Source Code Form is subject to the terms of the Mozilla Public
2 ! License, v. 2.0. If a copy of the MPL was not distributed with this
3 ! file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 module mbd_scalapack
5
6 use mbd_constants
7 use mbd_lapack, only: mode
8 use mbd_blacs, only: blacs_desc_t
9 use mbd_utils, only: exception_t, tostr, clock_t
10
11 implicit none
12
13 private
14 public :: pmmul, pinvh, peigh, peigvalsh
15
16 interface pmmul
17 module procedure pmmul_real
18 module procedure pmmul_complex
19 end interface
20
21 interface pinvh
22 module procedure pinvh_real
23 end interface
24
25 interface peigh
26 module procedure peigh_real
27 module procedure peigh_complex
28 end interface
29
30 interface peigvalsh
31 module procedure peigvalsh_real
32 module procedure peigvalsh_complex
33 end interface
34
35 interface
36 ! The following interfaces were taken straight from the ScaLAPACK codebase,
37 ! replacing COMPLEX*16 for COMPLEX(dp)
38 SUBROUTINE PDSYEV(JOBZ, UPLO, N, A, IA, JA, DESCA, W, Z, IZ, JZ, DESCZ, WORK, LWORK, INFO)
39 CHARACTER JOBZ, UPLO
40 INTEGER IA, INFO, IZ, JA, JZ, LWORK, N
41 INTEGER DESCA(*), DESCZ(*)
42 DOUBLE PRECISION A(*), W(*), WORK(*), Z(*)
43 END
44 SUBROUTINE PZHEEV(JOBZ, UPLO, N, A, IA, JA, DESCA, W, Z, IZ, JZ, DESCZ, WORK, &
45 LWORK, RWORK, LRWORK, INFO)
46 import :: dp
47 CHARACTER JOBZ, UPLO
48 INTEGER IA, INFO, IZ, JA, JZ, LRWORK, LWORK, N
49 INTEGER DESCA(*), DESCZ(*)
50 DOUBLE PRECISION RWORK(*), W(*)
51 COMPLEX(dp) A(*), WORK(*), Z(*)
52 END
53 SUBROUTINE PDGETRF(M, N, A, IA, JA, DESCA, IPIV, INFO)
54 INTEGER IA, INFO, JA, M, N
55 INTEGER DESCA(*), IPIV(*)
56 DOUBLE PRECISION A(*)
57 END
58 SUBROUTINE PDGETRI(N, A, IA, JA, DESCA, IPIV, WORK, LWORK, IWORK, LIWORK, INFO)
59 INTEGER IA, INFO, JA, LIWORK, LWORK, N
60 INTEGER DESCA(*), IPIV(*), IWORK(*)
61 DOUBLE PRECISION A(*), WORK(*)
62 END
63
64 ! The following interfaces were written by hand based on https://www.ibm.com/docs/
65 subroutine PDGEMM(transa, transb, m, n, k, alpha, a, ia, ja, desc_a, b, &
66 ib, jb, desc_b, beta, c, ic, jc, desc_c)
67 character :: transa, transb
68 integer :: m, n, k, ia, ja, desc_a(*), ib, jb, desc_b(*), ic, jc, desc_c(*)
69 double precision :: alpha, a(*), b(*), beta, c(*)
70 end
71 subroutine PZGEMM(transa, transb, m, n, k, alpha, a, ia, ja, desc_a, b, &
72 ib, jb, desc_b, beta, c, ic, jc, desc_c)
73 import :: dp
74 character :: transa, transb
75 integer :: m, n, k, ia, ja, desc_a(*), ib, jb, desc_b(*), ic, jc, desc_c(*)
76 complex(dp) :: alpha, a(*), b(*), beta, c(*)
77 end
78 end interface
79
80 contains
81
82 subroutine pinvh_real(A, blacs, exc, src, clock)
83 real(dp), intent(inout) :: A(:, :)
84 type(blacs_desc_t), intent(in) :: blacs
85 type(exception_t), intent(out), optional :: exc
86 real(dp), intent(in), optional :: src(:, :)
87 type(clock_t), intent(inout), optional :: clock
88
89 integer, allocatable :: i_pivot(:), iwork_arr(:)
90 real(dp), allocatable :: work_arr(:)
91 integer :: n, n_work_arr, error_flag, n_iwork_arr(1)
92 real(dp) :: n_work_arr_optim(1)
93
94 n = 3 * blacs%n_atoms
95 if (n == 0) return
96 if (present(src)) A = src
97 allocate (i_pivot(n))
98 if (present(clock)) call clock%clock(16)
99 call PDGETRF(n, n, A, 1, 1, blacs%desc, i_pivot, error_flag)
100 if (present(clock)) call clock%clock(-16)
101 if (error_flag /= 0) then
102 if (present(exc)) then
103 exc%code = MBD_EXC_LINALG
104 exc%origin = 'PDGETRF'
105 exc%msg = 'Failed with code '//trim(tostr(error_flag))
106 end if
107 return
108 end if
109 call PDGETRI( &
110 n, A, 1, 1, blacs%desc, i_pivot, &
111 n_work_arr_optim, -1, n_iwork_arr, -1, error_flag &
112 )
113 n_work_arr = nint(n_work_arr_optim(1))
114 allocate (work_arr(n_work_arr), iwork_arr(n_iwork_arr(1)))
115 if (present(clock)) call clock%clock(17)
116 call PDGETRI( &
117 n, A, 1, 1, blacs%desc, i_pivot, &
118 work_arr, n_work_arr, iwork_arr, n_iwork_arr(1), error_flag &
119 )
120 if (present(clock)) call clock%clock(-17)
121 if (error_flag /= 0) then
122 if (present(exc)) then
123 exc%code = MBD_EXC_LINALG
124 exc%origin = 'PDSYTRI'
125 exc%msg = 'Failed with code '//trim(tostr(error_flag))
126 end if
127 return
128 end if
129 end subroutine
130
131 function pmmul_real(A, blacsA, B, blacsB, transA, transB, blacsC) result(C)
132 real(dp), intent(in) :: A(:, :), B(:, :)
133 type(blacs_desc_t), intent(in) :: blacsA, blacsB, blacsC
134 character, intent(in), optional :: transA, transB
135 real(dp) :: C(size(A, 1), size(B, 2))
136
137 character :: transA_, transB_
138 integer :: n
139
140 transA_ = 'N'
141 transB_ = 'N'
142 if (present(transA)) transA_ = transA
143 if (present(transB)) transB_ = transB
144 n = 3 * blacsA%n_atoms
145 call PDGEMM( &
146 transA_, transB_, n, n, n, 1d0, A, 1, 1, blacsA%desc, &
147 B, 1, 1, blacsB%desc, 0d0, C, 1, 1, blacsC%desc &
148 )
149 end function
150
151 function pmmul_complex(A, blacsA, B, blacsB, transA, transB, blacsC) result(C)
152 complex(dp), intent(in) :: A(:, :), B(:, :)
153 type(blacs_desc_t), intent(in) :: blacsA, blacsB, blacsC
154 character, intent(in), optional :: transA, transB
155 complex(dp) :: C(size(A, 1), size(B, 2))
156
157 character :: transA_, transB_
158 integer :: n
159
160 transA_ = 'N'
161 transB_ = 'N'
162 if (present(transA)) transA_ = transA
163 if (present(transB)) transB_ = transB
164 n = 3 * blacsA%n_atoms
165 call PZGEMM( &
166 transA_, transB_, n, n, n, (1d0, 0d0), A, 1, 1, blacsA%desc, &
167 B, 1, 1, blacsB%desc, (0d0, 0d0), C, 1, 1, blacsC%desc &
168 )
169 end function
170
171 subroutine peigh_real(A, blacs, eigs, exc, src, vals_only, clock)
172 real(dp), intent(inout) :: A(:, :)
173 type(blacs_desc_t), intent(in) :: blacs
174 real(dp), intent(out) :: eigs(:)
175 type(exception_t), intent(out), optional :: exc
176 real(dp), intent(in), optional :: src(:, :)
177 logical, intent(in), optional :: vals_only
178 type(clock_t), intent(inout), optional :: clock
179
180 real(dp), allocatable :: work_arr(:), vectors(:, :)
181 real(dp) :: n_work_arr(1)
182 integer :: error_flag, n
183
184 n = 3 * blacs%n_atoms
185 if (present(src)) A = src
186 if (mode(vals_only) == 'V') then
187 allocate (vectors(size(A, 1), size(A, 2)))
188 else
189 allocate (vectors(1, 1))
190 end if
191 call PDSYEV( &
192 mode(vals_only), 'U', n, A, 1, 1, blacs%desc, eigs, vectors, &
193 1, 1, blacs%desc, n_work_arr, -1, error_flag &
194 )
195 allocate (work_arr(nint(n_work_arr(1))))
196 if (present(clock)) call clock%clock(13)
197 call PDSYEV( &
198 mode(vals_only), 'U', n, A, 1, 1, blacs%desc, eigs, vectors, &
199 1, 1, blacs%desc, work_arr(1), size(work_arr), error_flag &
200 )
201 if (present(clock)) call clock%clock(-13)
202 if (error_flag /= 0) then
203 if (present(exc)) then
204 exc%code = MBD_EXC_LINALG
205 exc%origin = 'PDSYEV'
206 exc%msg = 'Failed with code '//trim(tostr(error_flag))
207 end if
208 return
209 end if
210 if (mode(vals_only) == 'V') A = vectors
211 end subroutine
212
213 subroutine peigh_complex(A, blacs, eigs, exc, src, vals_only, clock)
214 complex(dp), intent(inout) :: A(:, :)
215 type(blacs_desc_t), intent(in) :: blacs
216 real(dp), intent(out) :: eigs(:)
217 type(exception_t), intent(out), optional :: exc
218 complex(dp), intent(in), optional :: src(:, :)
219 logical, intent(in), optional :: vals_only
220 type(clock_t), intent(inout), optional :: clock
221
222 complex(dp), allocatable :: work_arr(:), vectors(:, :)
223 integer :: n_work_arr, n_rwork_arr
224 real(dp), allocatable :: rwork_arr(:)
225 integer :: error_flag, n
226
227 n = 3 * blacs%n_atoms
228 if (present(src)) A = src
229 if (mode(vals_only) == 'V') then
230 allocate (vectors(size(A, 1), size(A, 2)))
231 else
232 allocate (vectors(1, 1))
233 end if
234 allocate (work_arr(1), rwork_arr(1))
235 call PZHEEV( &
236 mode(vals_only), 'U', n, A, 1, 1, blacs%desc, eigs, vectors, &
237 1, 1, blacs%desc, work_arr, -1, rwork_arr, -1, error_flag &
238 )
239 n_work_arr = nint(dble(work_arr(1)))
240 n_rwork_arr = nint(rwork_arr(1))
241 deallocate (work_arr, rwork_arr)
242 if (mode(vals_only) == 'N') then
243 n_rwork_arr = max(2 * n, n_rwork_arr)
244 else
245 n_rwork_arr = max(4 * n - 2, n_rwork_arr)
246 end if
247 allocate (work_arr(n_work_arr), source=(0d0, 0d0))
248 allocate (rwork_arr(n_rwork_arr), source=0d0)
249 if (present(clock)) call clock%clock(13)
250 call PZHEEV( &
251 mode(vals_only), 'U', n, A, 1, 1, blacs%desc, eigs, vectors, &
252 1, 1, blacs%desc, work_arr, n_work_arr, rwork_arr, n_rwork_arr, &
253 error_flag &
254 )
255 if (present(clock)) call clock%clock(-13)
256 if (error_flag /= 0) then
257 if (present(exc)) then
258 exc%code = MBD_EXC_LINALG
259 exc%origin = 'PZHEEV'
260 exc%msg = 'Failed with code '//trim(tostr(error_flag))
261 end if
262 return
263 end if
264 if (mode(vals_only) == 'V') A = vectors
265 end subroutine
266
267 function peigvalsh_real(A, blacs, exc, destroy, clock) result(eigs)
268 real(dp), target, intent(in) :: A(:, :)
269 type(blacs_desc_t), intent(in) :: blacs
270 type(exception_t), intent(out), optional :: exc
271 logical, intent(in), optional :: destroy
272 real(dp) :: eigs(3 * blacs%n_atoms)
273 type(clock_t), intent(inout), optional :: clock
274
275 real(dp), allocatable, target :: A_work(:, :)
276 real(dp), pointer :: A_p(:, :)
277
278 nullify (A_p)
279 if (present(destroy)) then
280 if (destroy) then
281 A_p => A
282 end if
283 end if
284 if (.not. associated(A_p)) then
285 allocate (A_work(size(A, 1), size(A, 1)), source=A)
286 A_p => A_work
287 end if
288 call peigh_real(A_p, blacs, eigs, exc, vals_only=.true., clock=clock)
289 end function
290
291 function peigvalsh_complex(A, blacs, exc, destroy, clock) result(eigs)
292 complex(dp), target, intent(in) :: A(:, :)
293 type(blacs_desc_t), intent(in) :: blacs
294 type(exception_t), intent(out), optional :: exc
295 logical, intent(in), optional :: destroy
296 real(dp) :: eigs(3 * blacs%n_atoms)
297 type(clock_t), intent(inout), optional :: clock
298
299 complex(dp), allocatable, target :: A_work(:, :)
300 complex(dp), pointer :: A_p(:, :)
301
302 nullify (A_p)
303 if (present(destroy)) then
304 if (destroy) then
305 A_p => A
306 end if
307 end if
308 if (.not. associated(A_p)) then
309 allocate (A_work(size(A, 1), size(A, 1)), source=A)
310 A_p => A_work
311 end if
312 call peigh_complex(A_p, blacs, eigs, exc, vals_only=.true.)
313 end function
314
315 end module