-
Notifications
You must be signed in to change notification settings - Fork 1
/
pexsi_interface.F
482 lines (444 loc) · 21.5 KB
/
pexsi_interface.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Interface to the PEXSI library, providing wrappers for all PEXSI
!> routines that are called inside CP2K. Requires PEXSI version 0.10.x.
!> \par History
!> 2014.12 created [Patrick Seewald]
!> \author Patrick Seewald
! **************************************************************************************************
MODULE pexsi_interface
#if defined(__LIBPEXSI)
USE f_ppexsi_interface, ONLY: f_ppexsi_dft_driver, &
f_ppexsi_load_real_hs_matrix, &
f_ppexsi_options, &
f_ppexsi_plan_finalize, &
f_ppexsi_plan_initialize, &
f_ppexsi_retrieve_real_dft_matrix, &
f_ppexsi_set_default_options
#endif
#if defined(__HAS_IEEE_EXCEPTIONS)
USE ieee_exceptions, ONLY: ieee_get_halting_mode, &
ieee_set_halting_mode, &
ieee_all
#endif
USE kinds, ONLY: int_8, &
real_8
USE ISO_C_BINDING, ONLY: C_INTPTR_T
USE message_passing, ONLY: mp_comm_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'pexsi_interface'
PUBLIC :: cp_pexsi_options, cp_pexsi_plan_initialize, &
cp_pexsi_load_real_hs_matrix, cp_pexsi_dft_driver, &
cp_pexsi_retrieve_real_dft_matrix, cp_pexsi_plan_finalize, &
cp_pexsi_set_options, cp_pexsi_get_options, cp_pexsi_set_default_options
TYPE cp_pexsi_options
PRIVATE
#if defined(__LIBPEXSI)
TYPE(f_ppexsi_options) :: options
#else
INTEGER :: unused = -1
#endif
END TYPE cp_pexsi_options
CONTAINS
! **************************************************************************************************
!> \brief Set PEXSI internal options
!> \param pexsi_options ...
!> \param temperature ...
!> \param gap ...
!> \param deltaE ...
!> \param numPole ...
!> \param isInertiaCount ...
!> \param maxPEXSIIter ...
!> \param muMin0 ...
!> \param muMax0 ...
!> \param mu0 ...
!> \param muInertiaTolerance ...
!> \param muInertiaExpansion ...
!> \param muPEXSISafeGuard ...
!> \param numElectronPEXSITolerance ...
!> \param matrixType ...
!> \param isSymbolicFactorize ...
!> \param ordering ...
!> \param rowOrdering ...
!> \param npSymbFact ...
!> \param verbosity ...
! **************************************************************************************************
SUBROUTINE cp_pexsi_set_options(pexsi_options, temperature, gap, deltaE, numPole, &
isInertiaCount, maxPEXSIIter, muMin0, muMax0, mu0, &
muInertiaTolerance, muInertiaExpansion, &
muPEXSISafeGuard, numElectronPEXSITolerance, &
matrixType, isSymbolicFactorize, ordering, rowOrdering, &
npSymbFact, verbosity)
TYPE(cp_pexsi_options), INTENT(INOUT) :: pexsi_options
REAL(KIND=real_8), INTENT(IN), OPTIONAL :: temperature, gap, deltaE
INTEGER, INTENT(IN), OPTIONAL :: numPole, isInertiaCount, &
maxPEXSIIter
REAL(KIND=real_8), INTENT(IN), OPTIONAL :: muMin0, muMax0, mu0, &
muInertiaTolerance, muInertiaExpansion, muPEXSISafeGuard, &
numElectronPEXSITolerance
INTEGER, INTENT(IN), OPTIONAL :: matrixType, &
isSymbolicFactorize, &
ordering, rowOrdering, npSymbFact, &
verbosity
#if defined(__LIBPEXSI)
IF (PRESENT(temperature)) pexsi_options%options%temperature = temperature
IF (PRESENT(gap)) pexsi_options%options%gap = gap
IF (PRESENT(deltaE)) pexsi_options%options%deltaE = deltaE
IF (PRESENT(numPole)) pexsi_options%options%numPole = numPole
IF (PRESENT(isInertiaCount)) pexsi_options%options%isInertiaCount = isInertiaCount
IF (PRESENT(maxPEXSIIter)) pexsi_options%options%maxPEXSIIter = maxPEXSIIter
IF (PRESENT(muMin0)) pexsi_options%options%muMin0 = muMin0
IF (PRESENT(muMax0)) pexsi_options%options%muMax0 = muMax0
IF (PRESENT(mu0)) pexsi_options%options%mu0 = mu0
IF (PRESENT(muInertiaTolerance)) &
pexsi_options%options%muInertiaTolerance = muInertiaTolerance
IF (PRESENT(muInertiaExpansion)) &
pexsi_options%options%muInertiaExpansion = muInertiaExpansion
IF (PRESENT(muPEXSISafeGuard)) &
pexsi_options%options%muPEXSISafeGuard = muPEXSISafeGuard
IF (PRESENT(numElectronPEXSITolerance)) &
pexsi_options%options%numElectronPEXSITolerance = numElectronPEXSITolerance
IF (PRESENT(matrixType)) pexsi_options%options%matrixType = matrixType
IF (PRESENT(isSymbolicFactorize)) &
pexsi_options%options%isSymbolicFactorize = isSymbolicFactorize
IF (PRESENT(ordering)) pexsi_options%options%ordering = ordering
IF (PRESENT(rowOrdering)) pexsi_options%options%rowOrdering = rowOrdering
IF (PRESENT(npSymbFact)) pexsi_options%options%npSymbFact = npSymbFact
IF (PRESENT(verbosity)) pexsi_options%options%verbosity = verbosity
#else
MARK_USED(pexsi_options)
MARK_USED(temperature)
MARK_USED(gap)
MARK_USED(deltaE)
MARK_USED(numPole)
MARK_USED(isInertiaCount)
MARK_USED(maxPEXSIIter)
MARK_USED(muMin0)
MARK_USED(muMax0)
MARK_USED(mu0)
MARK_USED(muInertiaTolerance)
MARK_USED(muInertiaExpansion)
MARK_USED(muPEXSISafeGuard)
MARK_USED(numElectronPEXSITolerance)
MARK_USED(matrixType)
MARK_USED(isSymbolicFactorize)
MARK_USED(ordering)
MARK_USED(rowOrdering)
MARK_USED(npSymbFact)
MARK_USED(verbosity)
CPABORT("Requires linking to the PEXSI library.")
#endif
! Additional PEXSI parameters and their defaults not made available here
! because CP2K should always use PEXSI's defaults:
! isConstructCommPattern (=?, pexsi does not even use it)
! symmetric (=1)
! transpose (=0)
END SUBROUTINE cp_pexsi_set_options
! **************************************************************************************************
!> \brief Access PEXSI internal options
!> \param pexsi_options ...
!> \param temperature ...
!> \param gap ...
!> \param deltaE ...
!> \param numPole ...
!> \param isInertiaCount ...
!> \param maxPEXSIIter ...
!> \param muMin0 ...
!> \param muMax0 ...
!> \param mu0 ...
!> \param muInertiaTolerance ...
!> \param muInertiaExpansion ...
!> \param muPEXSISafeGuard ...
!> \param numElectronPEXSITolerance ...
!> \param matrixType ...
!> \param isSymbolicFactorize ...
!> \param ordering ...
!> \param rowOrdering ...
!> \param npSymbFact ...
!> \param verbosity ...
! **************************************************************************************************
SUBROUTINE cp_pexsi_get_options(pexsi_options, temperature, gap, deltaE, numPole, &
isInertiaCount, maxPEXSIIter, muMin0, muMax0, mu0, &
muInertiaTolerance, muInertiaExpansion, &
muPEXSISafeGuard, numElectronPEXSITolerance, &
matrixType, isSymbolicFactorize, ordering, rowOrdering, &
npSymbFact, verbosity)
TYPE(cp_pexsi_options), INTENT(IN) :: pexsi_options
REAL(KIND=real_8), INTENT(OUT), OPTIONAL :: temperature, gap, deltaE
INTEGER, INTENT(OUT), OPTIONAL :: numPole, isInertiaCount, &
maxPEXSIIter
REAL(KIND=real_8), INTENT(OUT), OPTIONAL :: muMin0, muMax0, mu0, &
muInertiaTolerance, muInertiaExpansion, muPEXSISafeGuard, &
numElectronPEXSITolerance
INTEGER, INTENT(OUT), OPTIONAL :: matrixType, &
isSymbolicFactorize, &
ordering, rowOrdering, npSymbFact, &
verbosity
#if defined(__LIBPEXSI)
IF (PRESENT(temperature)) temperature = pexsi_options%options%temperature
IF (PRESENT(gap)) gap = pexsi_options%options%gap
IF (PRESENT(deltaE)) deltaE = pexsi_options%options%deltaE
IF (PRESENT(numPole)) numPole = pexsi_options%options%numPole
IF (PRESENT(isInertiaCount)) isInertiaCount = pexsi_options%options%isInertiaCount
IF (PRESENT(maxPEXSIIter)) maxPEXSIIter = pexsi_options%options%maxPEXSIIter
IF (PRESENT(muMin0)) muMin0 = pexsi_options%options%muMin0
IF (PRESENT(muMax0)) muMax0 = pexsi_options%options%muMax0
IF (PRESENT(mu0)) mu0 = pexsi_options%options%mu0
IF (PRESENT(muInertiaTolerance)) &
muInertiaTolerance = pexsi_options%options%muInertiaTolerance
IF (PRESENT(muInertiaExpansion)) &
muInertiaExpansion = pexsi_options%options%muInertiaExpansion
IF (PRESENT(muPEXSISafeGuard)) &
muPEXSISafeGuard = pexsi_options%options%muPEXSISafeGuard
IF (PRESENT(numElectronPEXSITolerance)) &
numElectronPEXSITolerance = pexsi_options%options%numElectronPEXSITolerance
IF (PRESENT(matrixType)) matrixType = pexsi_options%options%matrixType
IF (PRESENT(isSymbolicFactorize)) &
isSymbolicFactorize = pexsi_options%options%isSymbolicFactorize
IF (PRESENT(ordering)) ordering = pexsi_options%options%ordering
IF (PRESENT(rowOrdering)) rowOrdering = pexsi_options%options%rowOrdering
IF (PRESENT(npSymbFact)) npSymbFact = pexsi_options%options%npSymbFact
IF (PRESENT(verbosity)) verbosity = pexsi_options%options%verbosity
#else
MARK_USED(pexsi_options)
! assign intent-out arguments to silence compiler warnings
IF (PRESENT(temperature)) temperature = 0.0_real_8
IF (PRESENT(gap)) gap = 0.0_real_8
IF (PRESENT(deltaE)) deltaE = 0.0_real_8
IF (PRESENT(numPole)) numPole = -1
IF (PRESENT(isInertiaCount)) isInertiaCount = -1
IF (PRESENT(maxPEXSIIter)) maxPEXSIIter = -1
IF (PRESENT(muMin0)) muMin0 = 0.0_real_8
IF (PRESENT(muMax0)) muMax0 = 0.0_real_8
IF (PRESENT(mu0)) mu0 = 0.0_real_8
IF (PRESENT(muInertiaTolerance)) muInertiaTolerance = 0.0_real_8
IF (PRESENT(muInertiaExpansion)) muInertiaExpansion = 0.0_real_8
IF (PRESENT(muPEXSISafeGuard)) muPEXSISafeGuard = 0.0_real_8
IF (PRESENT(numElectronPEXSITolerance)) numElectronPEXSITolerance = 0.0_real_8
IF (PRESENT(matrixType)) matrixType = -1
IF (PRESENT(isSymbolicFactorize)) isSymbolicFactorize = -1
IF (PRESENT(ordering)) ordering = -1
IF (PRESENT(rowOrdering)) rowOrdering = -1
IF (PRESENT(npSymbFact)) npSymbFact = -1
IF (PRESENT(verbosity)) verbosity = -1
CPABORT("Requires linking to the PEXSI library.")
#endif
END SUBROUTINE cp_pexsi_get_options
! **************************************************************************************************
!> \brief ...
!> \param pexsi_options ...
! **************************************************************************************************
SUBROUTINE cp_pexsi_set_default_options(pexsi_options)
TYPE(cp_pexsi_options), INTENT(OUT) :: pexsi_options
#if defined(__LIBPEXSI)
CALL f_ppexsi_set_default_options(pexsi_options%options)
#else
CPABORT("Requires linking to the PEXSI library.")
#endif
END SUBROUTINE cp_pexsi_set_default_options
! **************************************************************************************************
!> \brief ...
!> \param comm ...
!> \param numProcRow ...
!> \param numProcCol ...
!> \param outputFileIndex ...
!> \return ...
! **************************************************************************************************
FUNCTION cp_pexsi_plan_initialize(comm, numProcRow, numProcCol, outputFileIndex)
TYPE(mp_comm_type), INTENT(IN) :: comm
INTEGER, INTENT(IN) :: numProcRow, numProcCol, &
outputFileIndex
INTEGER(KIND=C_INTPTR_T) :: cp_pexsi_plan_initialize
#if defined(__LIBPEXSI)
CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_pexsi_plan_initialize'
INTEGER :: info, handle
CALL timeset(routineN, handle)
cp_pexsi_plan_initialize = f_ppexsi_plan_initialize(comm%get_handle(), numProcRow, &
numProcCol, outputFileIndex, info)
IF (info .NE. 0) &
CPABORT("Pexsi returned an error. Consider logPEXSI0 for details.")
CALL timestop(handle)
#else
MARK_USED(comm)
MARK_USED(numProcRow)
MARK_USED(numProcCol)
MARK_USED(outputFileIndex)
cp_pexsi_plan_initialize = 0
CPABORT("Requires linking to the PEXSI library.")
#endif
END FUNCTION cp_pexsi_plan_initialize
! **************************************************************************************************
!> \brief ...
!> \param plan ...
!> \param pexsi_options ...
!> \param nrows ...
!> \param nnz ...
!> \param nnzLocal ...
!> \param numColLocal ...
!> \param colptrLocal ...
!> \param rowindLocal ...
!> \param HnzvalLocal ...
!> \param isSIdentity ...
!> \param SnzvalLocal ...
! **************************************************************************************************
SUBROUTINE cp_pexsi_load_real_hs_matrix(plan, pexsi_options, nrows, nnz, &
nnzLocal, numColLocal, colptrLocal, &
rowindLocal, HnzvalLocal, isSIdentity, &
SnzvalLocal)
INTEGER(KIND=C_INTPTR_T), INTENT(IN) :: plan
TYPE(cp_pexsi_options), INTENT(IN) :: pexsi_options
INTEGER, INTENT(IN) :: nrows, nnz, nnzLocal, &
numColLocal, colptrLocal(*), &
rowindLocal(*)
REAL(KIND=real_8), INTENT(IN) :: HnzvalLocal(*)
INTEGER, INTENT(IN) :: isSIdentity
REAL(KIND=real_8), INTENT(IN) :: SnzvalLocal(*)
#if defined(__LIBPEXSI)
CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_pexsi_load_real_symmetric_hs_matrix'
INTEGER :: handle, info
CALL timeset(routineN, handle)
CALL f_ppexsi_load_real_hs_matrix(plan, pexsi_options%options, nrows, nnz, nnzLocal, &
numColLocal, colptrLocal, rowindLocal, &
HnzvalLocal, isSIdentity, SnzvalLocal, info)
IF (info .NE. 0) &
CPABORT("Pexsi returned an error. Consider logPEXSI0 for details.")
CALL timestop(handle)
#else
MARK_USED(plan)
MARK_USED(pexsi_options)
MARK_USED(nrows)
MARK_USED(nnz)
MARK_USED(nnzLocal)
MARK_USED(numColLocal)
MARK_USED(isSIdentity)
CPABORT("Requires linking to the PEXSI library.")
! MARK_USED macro does not work on assumed shape variables
IF (.FALSE.) THEN; DO
IF (colptrLocal(1) > rowindLocal(1) .OR. HnzvalLocal(1) > SnzvalLocal(1)) EXIT
END DO; END IF
#endif
END SUBROUTINE cp_pexsi_load_real_hs_matrix
! **************************************************************************************************
!> \brief ...
!> \param plan ...
!> \param pexsi_options ...
!> \param numElectronExact ...
!> \param muPEXSI ...
!> \param numElectronPEXSI ...
!> \param muMinInertia ...
!> \param muMaxInertia ...
!> \param numTotalInertiaIter ...
!> \param numTotalPEXSIIter ...
! **************************************************************************************************
SUBROUTINE cp_pexsi_dft_driver(plan, pexsi_options, numElectronExact, muPEXSI, &
numElectronPEXSI, muMinInertia, muMaxInertia, &
numTotalInertiaIter, numTotalPEXSIIter)
INTEGER(KIND=C_INTPTR_T), INTENT(IN) :: plan
TYPE(cp_pexsi_options), INTENT(IN) :: pexsi_options
REAL(KIND=real_8), INTENT(IN) :: numElectronExact
REAL(KIND=real_8), INTENT(out) :: muPEXSI, numElectronPEXSI, &
muMinInertia, muMaxInertia
INTEGER, INTENT(out) :: numTotalInertiaIter, &
numTotalPEXSIIter
#if defined(__LIBPEXSI)
CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_pexsi_dft_driver'
INTEGER :: handle, info
#if defined(__HAS_IEEE_EXCEPTIONS)
LOGICAL, DIMENSION(5) :: halt
#endif
CALL timeset(routineN, handle)
! Unfortuntatelly, some PEXSI kernels raise IEEE754 exceptions.
! Therefore, we disable floating point traps temporarily.
#if defined(__HAS_IEEE_EXCEPTIONS)
CALL ieee_get_halting_mode(IEEE_ALL, halt)
CALL ieee_set_halting_mode(IEEE_ALL, .FALSE.)
#endif
CALL f_ppexsi_dft_driver(plan, pexsi_options%options, numElectronExact, muPEXSI, &
numElectronPEXSI, muMinInertia, muMaxInertia, &
numTotalInertiaIter, numTotalPEXSIIter, info)
#if defined(__HAS_IEEE_EXCEPTIONS)
CALL ieee_set_halting_mode(IEEE_ALL, halt)
#endif
IF (info .NE. 0) &
CPABORT("Pexsi returned an error. Consider logPEXSI0 for details.")
CALL timestop(handle)
#else
MARK_USED(plan)
MARK_USED(numelectronexact)
MARK_USED(pexsi_options)
! assign intent-out arguments to silence compiler warnings
muPEXSI = 0.0_real_8
numElectronPEXSI = 0.0_real_8
muMinInertia = 0.0_real_8
muMaxInertia = 0.0_real_8
numTotalInertiaIter = -1
numTotalPEXSIIter = -1
CPABORT("Requires linking to the PEXSI library.")
#endif
END SUBROUTINE cp_pexsi_dft_driver
! **************************************************************************************************
!> \brief ...
!> \param plan ...
!> \param DMnzvalLocal ...
!> \param EDMnzvalLocal ...
!> \param FDMnzvalLocal ...
!> \param totalEnergyH ...
!> \param totalEnergyS ...
!> \param totalFreeEnergy ...
! **************************************************************************************************
SUBROUTINE cp_pexsi_retrieve_real_dft_matrix(plan, DMnzvalLocal, EDMnzvalLocal, &
FDMnzvalLocal, totalEnergyH, &
totalEnergyS, totalFreeEnergy)
INTEGER(KIND=C_INTPTR_T), INTENT(IN) :: plan
REAL(KIND=real_8), INTENT(out) :: DMnzvalLocal(*), EDMnzvalLocal(*), &
FDMnzvalLocal(*), totalEnergyH, totalEnergyS, &
totalFreeEnergy
#if defined(__LIBPEXSI)
CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_pexsi_retrieve_real_symmetric_dft_matrix'
INTEGER :: handle, info
CALL timeset(routineN, handle)
CALL f_ppexsi_retrieve_real_dft_matrix(plan, DMnzvalLocal, EDMnzvalLocal, &
FDMnzvalLocal, totalEnergyH, &
totalEnergyS, totalFreeEnergy, info)
IF (info .NE. 0) &
CPABORT("Pexsi returned an error. Consider logPEXSI0 for details.")
CALL timestop(handle)
#else
MARK_USED(plan)
! assign intent-out arguments to silence compiler warnings
DMnzvalLocal(1) = 0.0_real_8
EDMnzvalLocal(1) = 0.0_real_8
FDMnzvalLocal(1) = 0.0_real_8
totalEnergyH = 0.0_real_8
totalEnergyS = 0.0_real_8
totalFreeEnergy = 0.0_real_8
CPABORT("Requires linking to the PEXSI library.")
#endif
END SUBROUTINE cp_pexsi_retrieve_real_dft_matrix
! **************************************************************************************************
!> \brief ...
!> \param plan ...
! **************************************************************************************************
SUBROUTINE cp_pexsi_plan_finalize(plan)
INTEGER(KIND=C_INTPTR_T), INTENT(IN) :: plan
#if defined(__LIBPEXSI)
CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_pexsi_plan_finalize'
INTEGER :: info, handle
CALL timeset(routineN, handle)
CALL f_ppexsi_plan_finalize(plan, info)
IF (info .NE. 0) &
CPABORT("Pexsi returned an error. Consider logPEXSI0 for details.")
CALL timestop(handle)
#else
MARK_USED(plan)
CPABORT("Requires linking to the PEXSI library.")
#endif
END SUBROUTINE
END MODULE pexsi_interface