Commit a264c13
Perf: speed up the LR kernel-to-potential integrands by ~45%
`PotHxcLR::cal_v_eff` dominates an LR run: 74% of the total for 07_LiF/pbe
(949 s of 1289 s over 541 calls). Roughly half of that was FFT; the rest was
serial element-wise work and allocator traffic. Five changes, no algorithmic
change:
1. OpenMP on the element-wise loops. Everything these loops call
(`grad_rho`, `grad_dot`, the Hartree kernel) was already parallel; the
integrands here were the only part still running on one thread.
2. Raw pointers instead of `.at()` (43 sites). The bounds check is a branch
per access, ~10 per grid point, and it blocks vectorization. The
outer-vector lookups (`drho_gs.at(0)`) are hoisted out of the loops, where
they keep their bounds check for free.
3. A scratch pool instead of per-call temporaries. `drho`, `gdot_terms` and
`vxc_tmp` were allocated and value-initialized on every call and then
overwritten before being read -- ~450 MB of pointless memset per call at a
200^3 grid, plus the page faults. `grad_dot` assigns rather than
accumulates, so even `vxc_tmp`'s zero-fill was dead. The pool is shared by
all instances (a run holds three `PotHxcLR`) so it does not multiply.
4. `add_v_hartree` replaces `H_Hartree_pw::v_hartree`, which (a) re-did the
forward FFT of rho^X that the GGA branch needs anyway -- 1 of the 10 FFTs
per call was pure duplication, (b) reduced a Hartree "energy" of the
transition density through `Parallel_Reduce::reduce_pool` every call, a
collective nobody reads that also clobbers the global
`H_Hartree_pw::hartree_energy`, and (c) returned a `matrix` by value, to
which `v_eff += 2 * (...)` added a second full-size temporary.
5. The nspin=2 singlet/triplet combinations `v2rho2_uu -+ v2rho2_ud` and
`2*vsigma_uu -+ vsigma_ud` are pre-contracted once per potential instead
of being rebuilt at every grid point of every call, which also replaces
two strided reads with one contiguous one. Costs 8-16 B/point, and only
for closed-shell nspin=2.
`PotGradXCLR::cal_v_eff` (the g^xc branch feeding the Z-vector RHS) gets 1-3
of the same treatment, for -20% to -28%. The shared pool matters more there:
a `PotGradXCLR` is constructed inside the loop over excited states, so
per-object buffers would never be reused at all.
Measured (single node, 16 threads), analytic gradients unchanged:
01_Si/lda 93 s -> 51 s (-45%) cal_v_eff 66 -> 45 ms/call
01_Si/pbe 229 s -> 125 s (-46%) cal_v_eff 356 -> 268 ms/call
02_Li2/lda 165 s -> 86 s (-48%) cal_v_eff 559 -> 278 ms/call
02_Li2/pbe 530 s -> 253 s (-52%) cal_v_eff 2651 -> 1553 ms/call
(The total also benefits from solving the Z-vector equation once instead of
three times, a separate fix; the per-call figures above isolate this commit.)
Excitation energies are identical to every printed digit; the nspin=1 cases
agree to 1e-14, and of 60 force components per nspin=2 case, 2-3 differ by
exactly one unit in the last printed digit (the Hartree prefactor is now
associated differently).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QMV7xFZ69hhc3HnVBHbD961 parent 073ab9b commit a264c13
4 files changed
Lines changed: 335 additions & 184 deletions
File tree
- source/source_lcao/module_lr
- Grad/xc
- potentials
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
11 | 29 | | |
12 | 30 | | |
13 | 31 | | |
| |||
41 | 59 | | |
42 | 60 | | |
43 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
44 | 68 | | |
45 | 69 | | |
46 | | - | |
| 70 | + | |
47 | 71 | | |
48 | 72 | | |
49 | 73 | | |
50 | 74 | | |
51 | | - | |
52 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
53 | 78 | | |
54 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
55 | 88 | | |
56 | 89 | | |
57 | 90 | | |
58 | 91 | | |
59 | 92 | | |
60 | 93 | | |
61 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
62 | 97 | | |
63 | 98 | | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
71 | 106 | | |
72 | | - | |
| 107 | + | |
73 | 108 | | |
74 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
75 | 113 | | |
76 | 114 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
82 | 120 | | |
83 | | - | |
| 121 | + | |
84 | 122 | | |
85 | 123 | | |
86 | 124 | | |
| |||
90 | 128 | | |
91 | 129 | | |
92 | 130 | | |
93 | | - | |
94 | 131 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
22 | 35 | | |
23 | 36 | | |
24 | 37 | | |
0 commit comments