phono3py-gpu 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- phono3py_gpu/__init__.py +19 -0
- phono3py_gpu/_wte.py +521 -0
- phono3py_gpu/backend.py +65 -0
- phono3py_gpu/batching.py +211 -0
- phono3py_gpu/collision.py +432 -0
- phono3py_gpu/eigensolver.py +222 -0
- phono3py_gpu/factory.py +353 -0
- phono3py_gpu/fourier.py +474 -0
- phono3py_gpu/harmonic.py +258 -0
- phono3py_gpu/initialization.py +50 -0
- phono3py_gpu/integration.py +183 -0
- phono3py_gpu/interaction.py +270 -0
- phono3py_gpu/isotope.py +116 -0
- phono3py_gpu/kappa.py +39 -0
- phono3py_gpu/kernels/collision.cu +11 -0
- phono3py_gpu/kernels/degeneracy.cu +36 -0
- phono3py_gpu/kernels/interaction.cu +89 -0
- phono3py_gpu/kernels/isotope.cu +28 -0
- phono3py_gpu/kernels/scattering.cu +85 -0
- phono3py_gpu/kernels/tetrahedron.cu +801 -0
- phono3py_gpu/multigpu.py +347 -0
- phono3py_gpu/scattering.py +449 -0
- phono3py_gpu/velocity.py +175 -0
- phono3py_gpu-1.0.0.dist-info/METADATA +151 -0
- phono3py_gpu-1.0.0.dist-info/RECORD +32 -0
- phono3py_gpu-1.0.0.dist-info/WHEEL +5 -0
- phono3py_gpu-1.0.0.dist-info/entry_points.txt +2 -0
- phono3py_gpu-1.0.0.dist-info/licenses/LICENSE +26 -0
- phono3py_gpu-1.0.0.dist-info/licenses/LICENSE.phono3py +26 -0
- phono3py_gpu-1.0.0.dist-info/licenses/LICENSE.phonopy +26 -0
- phono3py_gpu-1.0.0.dist-info/licenses/LICENSE.wte +28 -0
- phono3py_gpu-1.0.0.dist-info/top_level.txt +1 -0
phono3py_gpu/batching.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"""Experimental interleaved q batches for three-phonon scattering.
|
|
2
|
+
|
|
3
|
+
Integer symmetry metadata is prepared sequentially on the host. Each CUDA
|
|
4
|
+
interaction batch contains triplets from several initial q points.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
from phono3py.conductivity.grid_point_data import ScatteringResult
|
|
9
|
+
from phono3py.conductivity.lbte_collision_solver import LBTECollisionResult
|
|
10
|
+
|
|
11
|
+
from phono3py_gpu.backend import cupy, host, kernel
|
|
12
|
+
from phono3py_gpu.integration import sigma_weights
|
|
13
|
+
from phono3py_gpu.scattering import (
|
|
14
|
+
add_collision_rows,
|
|
15
|
+
average_initial,
|
|
16
|
+
collision_layout,
|
|
17
|
+
linewidth_detail,
|
|
18
|
+
reduce_collision,
|
|
19
|
+
sigma_groups,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def compute_q_batch(solver, grid_points, collision=False, *, last_batch=True):
|
|
24
|
+
"""Compute a bounded group of q points without per-q device synchronization.
|
|
25
|
+
|
|
26
|
+
Full/read/averaged pp and detailed file output use the original solver path.
|
|
27
|
+
This routine returns the same per-q result objects as the upstream solvers.
|
|
28
|
+
"""
|
|
29
|
+
cp = cupy()
|
|
30
|
+
engine, pp = solver.engine, solver._pp
|
|
31
|
+
retain_pp = collision and last_batch and solver.retain_pp
|
|
32
|
+
if engine.streamed:
|
|
33
|
+
# Large triplets already fill one launch with a single band tile.
|
|
34
|
+
answers = []
|
|
35
|
+
if collision:
|
|
36
|
+
original_retain_pp = solver.retain_pp
|
|
37
|
+
try:
|
|
38
|
+
for index, gp in enumerate(grid_points):
|
|
39
|
+
if collision:
|
|
40
|
+
solver.retain_pp = retain_pp and index == len(grid_points) - 1
|
|
41
|
+
result = solver.compute(int(gp))
|
|
42
|
+
tp, w = pp.get_triplets_at_q()[:2]
|
|
43
|
+
solver.q_batch_stats["triplet_batches"] += len(tp) * len(solver._sigmas)
|
|
44
|
+
solver.q_batch_stats["max_q_per_launch"] = 1
|
|
45
|
+
if collision:
|
|
46
|
+
answers.append(result)
|
|
47
|
+
else:
|
|
48
|
+
answers.append(
|
|
49
|
+
dict(
|
|
50
|
+
result=result,
|
|
51
|
+
gamma_N=solver.gamma_N,
|
|
52
|
+
gamma_U=solver.gamma_U,
|
|
53
|
+
triplets=tp,
|
|
54
|
+
weights=w,
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
finally:
|
|
58
|
+
if collision:
|
|
59
|
+
solver.retain_pp = original_retain_pp
|
|
60
|
+
return answers
|
|
61
|
+
nq, nb0 = len(grid_points), len(pp.band_indices)
|
|
62
|
+
ns, nt = len(solver._sigmas), len(solver._temperatures)
|
|
63
|
+
triplets, multiplicities, maps, swaps = [], [], [], []
|
|
64
|
+
offset = 0
|
|
65
|
+
if collision:
|
|
66
|
+
targets, rotations, rowshape, nc = collision_layout(solver)
|
|
67
|
+
for gp in grid_points:
|
|
68
|
+
if collision:
|
|
69
|
+
solver._collision.set_grid_point(int(gp))
|
|
70
|
+
gp2tp, _, swapped = solver._collision._get_gp2tp_map()
|
|
71
|
+
maps.append(gp2tp[targets] + offset)
|
|
72
|
+
swaps.append(swapped[targets])
|
|
73
|
+
else:
|
|
74
|
+
engine.set_grid_point(int(gp))
|
|
75
|
+
tp, w = pp.get_triplets_at_q()[:2]
|
|
76
|
+
triplets.append(tp)
|
|
77
|
+
multiplicities.append(w)
|
|
78
|
+
offset += len(tp)
|
|
79
|
+
|
|
80
|
+
counts = [len(tp) for tp in triplets]
|
|
81
|
+
q_triplets, q_weights = triplets, multiplicities
|
|
82
|
+
# Round-robin order ensures large single-q triplet lists also overlap in
|
|
83
|
+
# the same kernel, instead of mixing only the tail of neighbouring lists.
|
|
84
|
+
order = np.argsort(np.concatenate([np.arange(n) for n in counts]), kind="stable")
|
|
85
|
+
owner = np.repeat(np.arange(nq), counts)[order]
|
|
86
|
+
triplets = np.concatenate(triplets)[order]
|
|
87
|
+
multiplicities = cp.asarray(np.concatenate(multiplicities)[order])
|
|
88
|
+
owners = cp.asarray(owner)
|
|
89
|
+
normal = cp.asarray(
|
|
90
|
+
np.all(pp.bz_grid.addresses[triplets].sum(axis=1) == 0, axis=1), dtype=cp.int8
|
|
91
|
+
)
|
|
92
|
+
channels = 3 if not collision and solver._is_N_U else 1
|
|
93
|
+
gamma = cp.zeros((ns, nt, channels, nq, nb0))
|
|
94
|
+
bands = cp.asarray(pp.band_indices)
|
|
95
|
+
if collision:
|
|
96
|
+
inverse = np.argsort(order)
|
|
97
|
+
mapped = cp.asarray(inverse[np.asarray(maps)])
|
|
98
|
+
swapped = cp.asarray(np.asarray(swaps))
|
|
99
|
+
rotations = cp.asarray(rotations)
|
|
100
|
+
rows = np.empty((ns, nt, nq, *rowshape))
|
|
101
|
+
|
|
102
|
+
row_bytes = nt * nq * int(np.prod(rowshape)) * 8 if collision else 0
|
|
103
|
+
for group in sigma_groups(engine, solver._sigmas, row_bytes=row_bytes):
|
|
104
|
+
if collision:
|
|
105
|
+
row = cp.zeros((len(group), nt, nq, *rowshape))
|
|
106
|
+
saved, saved_zero = [], []
|
|
107
|
+
for start in range(0, len(triplets), engine.batch_size):
|
|
108
|
+
end = min(start + engine.batch_size, len(triplets))
|
|
109
|
+
selected = triplets[start:end]
|
|
110
|
+
tp = cp.asarray(selected)
|
|
111
|
+
frequencies = engine.frequencies[tp[:, 0]][:, bands]
|
|
112
|
+
integrations, zero = sigma_weights(
|
|
113
|
+
pp,
|
|
114
|
+
selected,
|
|
115
|
+
engine.frequencies,
|
|
116
|
+
frequencies,
|
|
117
|
+
[solver._sigmas[j] for j in group],
|
|
118
|
+
sigma_cutoff=solver._sigma_cutoff,
|
|
119
|
+
collision=collision,
|
|
120
|
+
vertices=engine.tetrahedron.vertices(selected)
|
|
121
|
+
if any(solver._sigmas[j] is None for j in group)
|
|
122
|
+
else None,
|
|
123
|
+
)
|
|
124
|
+
shared = engine.compute(selected, zero)
|
|
125
|
+
masked = pp.constant_averaged_interaction is None and not pp.symmetrize_fc3q
|
|
126
|
+
for j, (g, zero) in zip(group, integrations):
|
|
127
|
+
strength = (
|
|
128
|
+
cp.where(zero, 0, shared) if masked and len(group) > 1 else shared
|
|
129
|
+
)
|
|
130
|
+
stats = solver.q_batch_stats
|
|
131
|
+
stats["triplet_batches"] += 1
|
|
132
|
+
stats["max_q_per_launch"] = max(
|
|
133
|
+
stats["max_q_per_launch"], len(np.unique(owner[start:end]))
|
|
134
|
+
)
|
|
135
|
+
for k, temperature in enumerate(solver._temperatures):
|
|
136
|
+
values = linewidth_detail(
|
|
137
|
+
engine,
|
|
138
|
+
selected,
|
|
139
|
+
strength,
|
|
140
|
+
g,
|
|
141
|
+
temperature,
|
|
142
|
+
solver._occupations[k],
|
|
143
|
+
).sum(axis=(2, 3))
|
|
144
|
+
kernel("scattering.cu", "sum_by_q")(
|
|
145
|
+
((channels * nq * nb0 + 127) // 128,),
|
|
146
|
+
(128,),
|
|
147
|
+
(
|
|
148
|
+
gamma[j, k],
|
|
149
|
+
values,
|
|
150
|
+
multiplicities[start:end],
|
|
151
|
+
owners[start:end],
|
|
152
|
+
normal[start:end],
|
|
153
|
+
np.int32(len(selected)),
|
|
154
|
+
np.int32(nb0),
|
|
155
|
+
np.int32(nq),
|
|
156
|
+
np.int32(channels),
|
|
157
|
+
),
|
|
158
|
+
)
|
|
159
|
+
if not collision or temperature <= 0:
|
|
160
|
+
continue
|
|
161
|
+
reduced = reduce_collision(
|
|
162
|
+
strength,
|
|
163
|
+
g,
|
|
164
|
+
solver._inv_sinh[k],
|
|
165
|
+
tp,
|
|
166
|
+
solver._collision.unit_conversion_factor,
|
|
167
|
+
)
|
|
168
|
+
add_collision_rows(
|
|
169
|
+
row[j - group.start, k],
|
|
170
|
+
reduced,
|
|
171
|
+
mapped,
|
|
172
|
+
swapped,
|
|
173
|
+
rotations,
|
|
174
|
+
start=start,
|
|
175
|
+
num_cartesian=nc,
|
|
176
|
+
num_q=nq,
|
|
177
|
+
)
|
|
178
|
+
if retain_pp and j == ns - 1:
|
|
179
|
+
# Preserve the upstream final-q pp state without retaining
|
|
180
|
+
# every q's full interaction tensor on the host.
|
|
181
|
+
last = np.flatnonzero(owner[start:end] == nq - 1)
|
|
182
|
+
if len(last):
|
|
183
|
+
ids = cp.asarray(last)
|
|
184
|
+
saved.append(host(strength[ids]))
|
|
185
|
+
saved_zero.append(host(zero[ids]))
|
|
186
|
+
del integrations, shared, strength
|
|
187
|
+
if collision:
|
|
188
|
+
rows[group.start : group.stop] = host(row)
|
|
189
|
+
del row
|
|
190
|
+
if retain_pp:
|
|
191
|
+
pp.set_interaction_strength(
|
|
192
|
+
np.concatenate(saved), g_zero=np.concatenate(saved_zero)
|
|
193
|
+
)
|
|
194
|
+
for i, gp in enumerate(grid_points):
|
|
195
|
+
average_initial(gamma[:, :, :, i], pp, int(gp))
|
|
196
|
+
gamma = host(gamma)
|
|
197
|
+
if collision:
|
|
198
|
+
return [
|
|
199
|
+
LBTECollisionResult(gamma=gamma[:, :, 0, i], collision_row=rows[:, :, i])
|
|
200
|
+
for i in range(nq)
|
|
201
|
+
]
|
|
202
|
+
return [
|
|
203
|
+
dict(
|
|
204
|
+
result=ScatteringResult(gamma=gamma[:, :, 0, i]),
|
|
205
|
+
gamma_N=gamma[:, :, 1, i] if channels == 3 else None,
|
|
206
|
+
gamma_U=gamma[:, :, 2, i] if channels == 3 else None,
|
|
207
|
+
triplets=q_triplets[i],
|
|
208
|
+
weights=q_weights[i],
|
|
209
|
+
)
|
|
210
|
+
for i in range(nq)
|
|
211
|
+
]
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
"""GPU dense LBTE solve with upstream assembly and result containers."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from phono3py.conductivity.collision_matrix_kernel import (
|
|
7
|
+
IrreducibleCollisionMatrixKernel,
|
|
8
|
+
ReducibleCollisionMatrixKernel,
|
|
9
|
+
)
|
|
10
|
+
from phono3py.phonon.degeneracy import average_over_degenerate_sets
|
|
11
|
+
from phonopy.physical_units import get_physical_units
|
|
12
|
+
|
|
13
|
+
from phono3py_gpu.backend import cupy, eigh, host, kernel, report_devices
|
|
14
|
+
from phono3py_gpu.integration import average_axis
|
|
15
|
+
|
|
16
|
+
INPLACE_EIGH_BYTES = 256 * 2**20
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _upload_collision(matrix):
|
|
20
|
+
"""Upload directly into column-major storage with bounded staging memory."""
|
|
21
|
+
cp = cupy()
|
|
22
|
+
n = len(matrix)
|
|
23
|
+
vectors = cp.empty((n, n), dtype=cp.float64, order="F")
|
|
24
|
+
for start in range(0, n, 256):
|
|
25
|
+
vectors[start : start + 256] = cp.asarray(matrix[start : start + 256])
|
|
26
|
+
return vectors
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def eigh_collision(matrix):
|
|
30
|
+
"""Diagonalize FP64 input, overwriting column-major device input in place."""
|
|
31
|
+
from cupy_backends.cuda.libs import cublas, cusolver
|
|
32
|
+
|
|
33
|
+
cp = cupy()
|
|
34
|
+
n = len(matrix)
|
|
35
|
+
vectors = (
|
|
36
|
+
cp.asfortranarray(matrix)
|
|
37
|
+
if isinstance(matrix, cp.ndarray)
|
|
38
|
+
else _upload_collision(matrix)
|
|
39
|
+
)
|
|
40
|
+
values = cp.empty(n, dtype=cp.float64)
|
|
41
|
+
info = cp.empty((), dtype=cp.int32)
|
|
42
|
+
params = cusolver.createParams()
|
|
43
|
+
dtype = cp.cuda.runtime.CUDA_R_64F
|
|
44
|
+
try:
|
|
45
|
+
args = (
|
|
46
|
+
cp.cuda.Device().cusolver_handle,
|
|
47
|
+
params,
|
|
48
|
+
cusolver.CUSOLVER_EIG_MODE_VECTOR,
|
|
49
|
+
cublas.CUBLAS_FILL_MODE_LOWER,
|
|
50
|
+
n,
|
|
51
|
+
dtype,
|
|
52
|
+
vectors.data.ptr,
|
|
53
|
+
n,
|
|
54
|
+
dtype,
|
|
55
|
+
values.data.ptr,
|
|
56
|
+
dtype,
|
|
57
|
+
)
|
|
58
|
+
device_bytes, host_bytes = cusolver.xsyevd_bufferSize(*args)
|
|
59
|
+
cp.get_default_memory_pool().free_all_blocks()
|
|
60
|
+
work = cp.empty(device_bytes, dtype=cp.uint8)
|
|
61
|
+
cpu_work = np.empty(host_bytes, dtype=np.uint8)
|
|
62
|
+
cusolver.xsyevd(
|
|
63
|
+
*args,
|
|
64
|
+
work.data.ptr,
|
|
65
|
+
device_bytes,
|
|
66
|
+
cpu_work.ctypes.data,
|
|
67
|
+
host_bytes,
|
|
68
|
+
info.data.ptr,
|
|
69
|
+
)
|
|
70
|
+
status = int(info.get())
|
|
71
|
+
if status:
|
|
72
|
+
raise np.linalg.LinAlgError(f"cuSOLVER Xsyevd failed with info={status}.")
|
|
73
|
+
finally:
|
|
74
|
+
cusolver.destroyParams(params)
|
|
75
|
+
return values, vectors
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class GPUCollisionMixin:
|
|
79
|
+
"""Device algebra shared by the irreducible and full-mesh kernels."""
|
|
80
|
+
|
|
81
|
+
def __init__(self, *args, wte=False, **kwargs):
|
|
82
|
+
super().__init__(*args, **kwargs)
|
|
83
|
+
self._transport = "WTE-LBTE" if wte else "BTE-LBTE"
|
|
84
|
+
mode = os.environ.get("PHONO3PY_GPU_LBTE_SOLVER", "single")
|
|
85
|
+
if mode not in ("single", "mg"):
|
|
86
|
+
raise ValueError("PHONO3PY_GPU_LBTE_SOLVER must be single or mg.")
|
|
87
|
+
self._distributed = mode == "mg"
|
|
88
|
+
self._distributed_factor = None
|
|
89
|
+
self.eigensolver_stats = []
|
|
90
|
+
|
|
91
|
+
def _get_colmat_solver(self):
|
|
92
|
+
# LAPACKE availability does not constrain the device implementation.
|
|
93
|
+
if self._pinv_solver in (1, 2, 6):
|
|
94
|
+
return self._pinv_solver
|
|
95
|
+
return super()._get_colmat_solver()
|
|
96
|
+
|
|
97
|
+
def _prepare_collision_matrix(self):
|
|
98
|
+
if self._distributed:
|
|
99
|
+
return super()._prepare_collision_matrix()
|
|
100
|
+
if isinstance(self, IrreducibleCollisionMatrixKernel):
|
|
101
|
+
return self._get_ir_weights()
|
|
102
|
+
self._rotation_map = None
|
|
103
|
+
if self._kappa_settings.is_kappa_star:
|
|
104
|
+
self._rotation_map = self._get_rotation_map()
|
|
105
|
+
self._expand_local_values(self._ir_gr_grid_points, self._rotation_map)
|
|
106
|
+
if self._log_level:
|
|
107
|
+
print(
|
|
108
|
+
"- RTA values below are computed with the collision term "
|
|
109
|
+
"approximated by the\n"
|
|
110
|
+
" diagonal elements of the collision matrix, not by the "
|
|
111
|
+
"imaginary part of the\n"
|
|
112
|
+
" self-energy."
|
|
113
|
+
)
|
|
114
|
+
return self._get_collision_weights()
|
|
115
|
+
|
|
116
|
+
def _device_collision_matrix(self, s, t, weights):
|
|
117
|
+
"""Prepare one sigma/temperature and retain its matrix for the solve."""
|
|
118
|
+
cp = cupy()
|
|
119
|
+
n = self._get_symmetrization_size()
|
|
120
|
+
source = self._collision_matrix[s, t].reshape(n, n)
|
|
121
|
+
flat = cp.asarray(source)
|
|
122
|
+
matrix = flat.reshape(self._collision_matrix.shape[2:])
|
|
123
|
+
ir = isinstance(self, IrreducibleCollisionMatrixKernel)
|
|
124
|
+
if not ir and self._rotation_map is not None:
|
|
125
|
+
self._expand_device_collisions(matrix, self._rotation_map)
|
|
126
|
+
groups = list(
|
|
127
|
+
self._iter_ir_degenerate_ids() if ir else self._iter_mesh_degenerate_ids()
|
|
128
|
+
)
|
|
129
|
+
for i, ids in groups:
|
|
130
|
+
average_axis(matrix[i], ids, 0)
|
|
131
|
+
for i, ids in groups:
|
|
132
|
+
target = matrix[:, :, :, i] if ir else matrix[:, :, i]
|
|
133
|
+
average_axis(target, ids, 3 if ir else 2)
|
|
134
|
+
# Add the diagonal after averaging so degenerate blocks keep their rates.
|
|
135
|
+
bands = cp.arange(matrix.shape[1])
|
|
136
|
+
if ir:
|
|
137
|
+
diagonals = np.zeros((matrix.shape[0], matrix.shape[1], 3, 3))
|
|
138
|
+
for i, diagonal, rotation in self._iter_ir_diagonal_entries(s, t):
|
|
139
|
+
diagonals[i] += diagonal[:, None, None] * rotation
|
|
140
|
+
# Preserve the host summation order and upload all diagonal blocks once.
|
|
141
|
+
points = cp.arange(matrix.shape[0])[:, None]
|
|
142
|
+
matrix[points, bands, :, points, bands, :] += cp.asarray(diagonals)
|
|
143
|
+
w = cp.asarray(weights)
|
|
144
|
+
matrix *= (
|
|
145
|
+
w[:, None, None, None, None, None] * w[None, None, None, :, None, None]
|
|
146
|
+
)
|
|
147
|
+
else:
|
|
148
|
+
for i, ids in enumerate(self._gr_degenerate_ids):
|
|
149
|
+
diagonal = average_over_degenerate_sets(
|
|
150
|
+
self._get_main_diagonal(i, s, t), ids, 0
|
|
151
|
+
)
|
|
152
|
+
matrix[i, bands, i, bands] += cp.asarray(diagonal)
|
|
153
|
+
kernel("collision.cu", "symmetrize_collision")(
|
|
154
|
+
((flat.size + 255) // 256,), (256,), (flat, np.int32(n))
|
|
155
|
+
)
|
|
156
|
+
# A symmetric matrix's transpose provides column-major storage without a copy.
|
|
157
|
+
return flat.T if flat.nbytes >= INPLACE_EIGH_BYTES else flat
|
|
158
|
+
|
|
159
|
+
def _average_collision_matrix_by_degeneracy(self):
|
|
160
|
+
cp = cupy()
|
|
161
|
+
ir = isinstance(self, IrreducibleCollisionMatrixKernel)
|
|
162
|
+
groups = list(
|
|
163
|
+
self._iter_ir_degenerate_ids() if ir else self._iter_mesh_degenerate_ids()
|
|
164
|
+
)
|
|
165
|
+
for s, t in self._iter_sigma_temp():
|
|
166
|
+
if self._distributed:
|
|
167
|
+
matrix = self._collision_matrix[s, t]
|
|
168
|
+
for i, ids in groups:
|
|
169
|
+
row = cp.asarray(matrix[i])
|
|
170
|
+
average_axis(row, ids, 0)
|
|
171
|
+
matrix[i] = host(row)
|
|
172
|
+
for i, ids in groups:
|
|
173
|
+
target = matrix[:, :, :, i] if ir else matrix[:, :, i]
|
|
174
|
+
column = cp.asarray(target)
|
|
175
|
+
average_axis(column, ids, 3 if ir else 2)
|
|
176
|
+
target[:] = host(column)
|
|
177
|
+
continue
|
|
178
|
+
matrix = cp.asarray(self._collision_matrix[s, t])
|
|
179
|
+
for i, ids in groups:
|
|
180
|
+
average_axis(matrix[i], ids, 0)
|
|
181
|
+
for i, ids in groups:
|
|
182
|
+
if ir:
|
|
183
|
+
average_axis(matrix[:, :, :, i], ids, 3)
|
|
184
|
+
else:
|
|
185
|
+
average_axis(matrix[:, :, i], ids, 2)
|
|
186
|
+
self._collision_matrix[s, t] = host(matrix)
|
|
187
|
+
|
|
188
|
+
def _symmetrize_collision_matrix(self):
|
|
189
|
+
cp = cupy()
|
|
190
|
+
n = self._get_symmetrization_size()
|
|
191
|
+
for s, t in self._iter_sigma_temp():
|
|
192
|
+
if self._distributed:
|
|
193
|
+
matrix = self._collision_matrix[s, t].reshape(n, n)
|
|
194
|
+
for start in range(0, n, 256):
|
|
195
|
+
stop = min(start + 256, n)
|
|
196
|
+
row = cp.asarray(matrix[start:stop])
|
|
197
|
+
column = cp.asarray(matrix[:, start:stop]).T
|
|
198
|
+
mean = host((row + column) / 2)
|
|
199
|
+
matrix[start:stop] = mean
|
|
200
|
+
matrix[:, start:stop] = mean.T
|
|
201
|
+
continue
|
|
202
|
+
matrix = cp.asarray(self._collision_matrix[s, t].reshape(n, n))
|
|
203
|
+
self._collision_matrix[s, t] = host((matrix + matrix.T) / 2).reshape(
|
|
204
|
+
self._collision_matrix.shape[2:]
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
def _iter_kappa_at_sigmas(self, weights):
|
|
208
|
+
cp = cupy()
|
|
209
|
+
n = self._get_symmetrization_size()
|
|
210
|
+
solver = self._get_colmat_solver()
|
|
211
|
+
report = self._log_level
|
|
212
|
+
for s, t in self._iter_sigma_temp():
|
|
213
|
+
if not self._distributed:
|
|
214
|
+
matrix = self._device_collision_matrix(s, t, weights)
|
|
215
|
+
if self._kappa_settings.temperatures[t] <= 0:
|
|
216
|
+
if not self._distributed:
|
|
217
|
+
self._collision_matrix[s, t] = host(matrix).reshape(
|
|
218
|
+
self._collision_matrix.shape[2:]
|
|
219
|
+
)
|
|
220
|
+
del matrix
|
|
221
|
+
continue
|
|
222
|
+
if report:
|
|
223
|
+
from phono3py_gpu.multigpu import devices
|
|
224
|
+
|
|
225
|
+
ids = devices() if self._distributed else [cp.cuda.runtime.getDevice()]
|
|
226
|
+
note = " (cuSOLVERMg)" if self._distributed else ""
|
|
227
|
+
report_devices(f"{self._transport} global solve", ids, note)
|
|
228
|
+
report = False
|
|
229
|
+
if not self._distributed and isinstance(
|
|
230
|
+
self, ReducibleCollisionMatrixKernel
|
|
231
|
+
):
|
|
232
|
+
# Reducible RTA reads the prepared diagonal, not the raw gamma.
|
|
233
|
+
target = self._collision_matrix[s, t].reshape(n, n)
|
|
234
|
+
target.flat[:: n + 1] = host(matrix.diagonal())
|
|
235
|
+
self._set_kappa_RTA_at_step(s, t, weights)
|
|
236
|
+
if self._distributed:
|
|
237
|
+
self._solve_distributed_step(s, t, solver, weights)
|
|
238
|
+
yield s, t
|
|
239
|
+
continue
|
|
240
|
+
if solver == 7:
|
|
241
|
+
self._gpu_inverse = cp.linalg.pinv(matrix, rcond=1e-15)
|
|
242
|
+
stored = self._gpu_inverse
|
|
243
|
+
else:
|
|
244
|
+
if matrix.nbytes >= INPLACE_EIGH_BYTES:
|
|
245
|
+
self._gpu_eigenvalues, self._gpu_eigenvectors = eigh_collision(
|
|
246
|
+
matrix
|
|
247
|
+
)
|
|
248
|
+
else:
|
|
249
|
+
self._gpu_eigenvalues, self._gpu_eigenvectors = eigh(matrix)
|
|
250
|
+
self._collision_eigenvalues[s, t] = host(self._gpu_eigenvalues)
|
|
251
|
+
stored = (
|
|
252
|
+
self._gpu_eigenvectors.T
|
|
253
|
+
if solver in (1, 2, 4, 5)
|
|
254
|
+
else self._gpu_eigenvectors
|
|
255
|
+
)
|
|
256
|
+
del matrix
|
|
257
|
+
self._collision_matrix[s, t] = host(stored).reshape(
|
|
258
|
+
self._collision_matrix.shape[2:]
|
|
259
|
+
)
|
|
260
|
+
self._set_kappa_at_step(s, t, weights)
|
|
261
|
+
num_mesh = int(np.prod(self._kappa_settings.mesh_numbers))
|
|
262
|
+
self._kappa[s, t] /= num_mesh
|
|
263
|
+
self._kappa_RTA[s, t] /= num_mesh
|
|
264
|
+
del stored
|
|
265
|
+
self._gpu_eigenvectors = self._gpu_eigenvalues = self._gpu_inverse = None
|
|
266
|
+
yield s, t
|
|
267
|
+
|
|
268
|
+
def _solve_distributed_step(self, sigma, temperature, solver, weights):
|
|
269
|
+
from phono3py_gpu.eigensolver import DistributedEigh
|
|
270
|
+
from phono3py_gpu.multigpu import devices
|
|
271
|
+
|
|
272
|
+
n = self._get_symmetrization_size()
|
|
273
|
+
matrix = self._collision_matrix[sigma, temperature].reshape(n, n)
|
|
274
|
+
factor = DistributedEigh(matrix, devices())
|
|
275
|
+
self._distributed_factor = factor
|
|
276
|
+
self.eigensolver_stats.append(
|
|
277
|
+
dict(
|
|
278
|
+
devices=factor.devices,
|
|
279
|
+
dimension=n,
|
|
280
|
+
workspace_bytes_per_device=factor.workspace_bytes_per_device,
|
|
281
|
+
)
|
|
282
|
+
)
|
|
283
|
+
try:
|
|
284
|
+
if solver == 7:
|
|
285
|
+
inverse = factor.inverse_values(1e-15 * np.max(np.abs(factor.values)))
|
|
286
|
+
factor.write_inverse(matrix, inverse)
|
|
287
|
+
else:
|
|
288
|
+
inverse = factor.inverse_values(self._pinv_cutoff, self._pinv_method)
|
|
289
|
+
self._collision_eigenvalues[sigma, temperature] = factor.values
|
|
290
|
+
factor.write_vectors(matrix, transpose=solver in (1, 2, 4, 5))
|
|
291
|
+
self._distributed_inverse_values = inverse
|
|
292
|
+
self._set_kappa_at_step(sigma, temperature, weights)
|
|
293
|
+
num_mesh = int(np.prod(self._kappa_settings.mesh_numbers))
|
|
294
|
+
self._kappa[sigma, temperature] /= num_mesh
|
|
295
|
+
self._kappa_RTA[sigma, temperature] /= num_mesh
|
|
296
|
+
finally:
|
|
297
|
+
self._distributed_factor = None
|
|
298
|
+
factor.close()
|
|
299
|
+
|
|
300
|
+
def _solve_Y(self, solver, v, X, i_sigma, i_temp):
|
|
301
|
+
cp = cupy()
|
|
302
|
+
ir = isinstance(self, IrreducibleCollisionMatrixKernel)
|
|
303
|
+
if self._distributed_factor is not None:
|
|
304
|
+
factor = self._distributed_factor
|
|
305
|
+
y = factor.solve(X.ravel() if ir else X, self._distributed_inverse_values)
|
|
306
|
+
if solver == 6:
|
|
307
|
+
n = self._get_symmetrization_size()
|
|
308
|
+
factor.write_inverse(
|
|
309
|
+
self._collision_matrix[i_sigma, i_temp].reshape(n, n),
|
|
310
|
+
self._distributed_inverse_values,
|
|
311
|
+
)
|
|
312
|
+
return y.reshape(-1, 3)
|
|
313
|
+
x = cp.asarray(X.ravel() if ir else X)
|
|
314
|
+
if solver == 7:
|
|
315
|
+
y = self._gpu_inverse @ x
|
|
316
|
+
else:
|
|
317
|
+
w, v = self._gpu_eigenvalues, self._gpu_eigenvectors
|
|
318
|
+
valid = (cp.abs(w) if self._pinv_method == 0 else w) > self._pinv_cutoff
|
|
319
|
+
inverse = cp.where(valid, 1 / cp.where(valid, w, 1), 0)
|
|
320
|
+
projection = v.T @ x
|
|
321
|
+
y = v @ (projection * (inverse if ir else inverse[:, None]))
|
|
322
|
+
if solver == 6:
|
|
323
|
+
pinv = (v * inverse) @ v.T
|
|
324
|
+
self._collision_matrix[i_sigma, i_temp] = host(pinv).reshape(
|
|
325
|
+
self._collision_matrix.shape[2:]
|
|
326
|
+
)
|
|
327
|
+
return host(y.reshape(-1, 3))
|
|
328
|
+
|
|
329
|
+
def _get_X(self, i_temp, weights):
|
|
330
|
+
cp = cupy()
|
|
331
|
+
t = self._kappa_settings.temperatures[i_temp]
|
|
332
|
+
f = cp.asarray(self._get_X_frequencies())
|
|
333
|
+
u = get_physical_units()
|
|
334
|
+
if t <= 0:
|
|
335
|
+
return np.zeros_like(self._gv.reshape(-1, 3))
|
|
336
|
+
valid = f > self._kappa_settings.cutoff_frequency
|
|
337
|
+
x = cp.where(valid, f, 1) * u.THzToEv / (2 * u.KB * t)
|
|
338
|
+
scale = cp.where(valid, f * u.THzToEv / (cp.sinh(x) * 4 * u.KB * t * t), 0)
|
|
339
|
+
return host(
|
|
340
|
+
(
|
|
341
|
+
cp.asarray(self._gv)
|
|
342
|
+
* scale[..., None]
|
|
343
|
+
* cp.asarray(weights)[:, None, None]
|
|
344
|
+
).reshape(-1, 3)
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
def _set_mode_kappa(self, mode_kappa, X, Y, num_grid_points, i_sigma, i_temp):
|
|
348
|
+
cp = cupy()
|
|
349
|
+
nb = self._frequencies.shape[1]
|
|
350
|
+
x, y = (cp.asarray(a).reshape(num_grid_points, nb, 3) for a in (X, Y))
|
|
351
|
+
tensor = cp.zeros((num_grid_points, nb, 3, 3))
|
|
352
|
+
for rotation in self._rotations_cartesian:
|
|
353
|
+
r = cp.asarray(rotation)
|
|
354
|
+
vx, vy = x @ r.T, y @ r.T
|
|
355
|
+
tensor += vx[..., :, None] * vy[..., None, :]
|
|
356
|
+
tensor += tensor.swapaxes(-1, -2).copy()
|
|
357
|
+
pairs = ((0, 0), (1, 1), (2, 2), (1, 2), (0, 2), (0, 1))
|
|
358
|
+
result = cp.stack([tensor[..., a, b] for a, b in pairs], axis=-1)
|
|
359
|
+
gamma_index = self._get_gamma_index()
|
|
360
|
+
if gamma_index is not None:
|
|
361
|
+
result[gamma_index, :3] = 0
|
|
362
|
+
t = self._kappa_settings.temperatures[i_temp]
|
|
363
|
+
result *= (
|
|
364
|
+
self._kappa_settings.conversion_factor * get_physical_units().KB * t * t
|
|
365
|
+
)
|
|
366
|
+
mode_kappa[i_sigma, i_temp] = host(result)
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
class GPUIrreducibleCollisionKernel(
|
|
370
|
+
GPUCollisionMixin, IrreducibleCollisionMatrixKernel
|
|
371
|
+
):
|
|
372
|
+
def _get_collision_weights(self):
|
|
373
|
+
cp = cupy()
|
|
374
|
+
weights = self._get_ir_weights()
|
|
375
|
+
w = cp.asarray(weights)
|
|
376
|
+
for s, t in self._iter_sigma_temp():
|
|
377
|
+
if self._distributed:
|
|
378
|
+
for i in range(len(weights)):
|
|
379
|
+
row = cp.asarray(self._collision_matrix[s, t, i])
|
|
380
|
+
row *= w[i] * w[None, None, :, None, None]
|
|
381
|
+
self._collision_matrix[s, t, i] = host(row)
|
|
382
|
+
continue
|
|
383
|
+
matrix = cp.asarray(self._collision_matrix[s, t])
|
|
384
|
+
matrix *= (
|
|
385
|
+
w[:, None, None, None, None, None] * w[None, None, None, :, None, None]
|
|
386
|
+
)
|
|
387
|
+
self._collision_matrix[s, t] = host(matrix)
|
|
388
|
+
return weights
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
class GPUReducibleCollisionKernel(GPUCollisionMixin, ReducibleCollisionMatrixKernel):
|
|
392
|
+
def _expand_device_collisions(self, matrix, rot_grid_points):
|
|
393
|
+
rotations = cupy().asarray(rot_grid_points)
|
|
394
|
+
for gp in self._ir_gr_grid_points:
|
|
395
|
+
multiplicity = np.count_nonzero(rot_grid_points[:, gp] == gp)
|
|
396
|
+
source = matrix[gp].copy() / multiplicity
|
|
397
|
+
matrix[gp] = 0
|
|
398
|
+
for r, mapping in enumerate(rot_grid_points):
|
|
399
|
+
matrix[mapping[gp]][:, rotations[r], :] += source
|
|
400
|
+
|
|
401
|
+
def _expand_collisions(self, ir_gr_grid_points, rot_grid_points):
|
|
402
|
+
"""Expand collision rows on device using upstream integer rotation maps."""
|
|
403
|
+
cp = cupy()
|
|
404
|
+
rotations = cp.asarray(rot_grid_points)
|
|
405
|
+
for s, t in self._iter_sigma_temp():
|
|
406
|
+
if self._distributed:
|
|
407
|
+
matrix = self._collision_matrix[s, t]
|
|
408
|
+
for gp in ir_gr_grid_points:
|
|
409
|
+
multiplicity = np.count_nonzero(rot_grid_points[:, gp] == gp)
|
|
410
|
+
source = cp.asarray(matrix[gp]) / multiplicity
|
|
411
|
+
matrix[gp] = 0
|
|
412
|
+
for r, mapping in enumerate(rot_grid_points):
|
|
413
|
+
target = int(mapping[gp])
|
|
414
|
+
row = cp.asarray(matrix[target])
|
|
415
|
+
row[:, rotations[r], :] += source
|
|
416
|
+
matrix[target] = host(row)
|
|
417
|
+
continue
|
|
418
|
+
matrix = cp.asarray(self._collision_matrix[s, t])
|
|
419
|
+
for gp in ir_gr_grid_points:
|
|
420
|
+
multiplicity = np.count_nonzero(rot_grid_points[:, gp] == gp)
|
|
421
|
+
source = matrix[gp].copy() / multiplicity
|
|
422
|
+
matrix[gp] = 0
|
|
423
|
+
for r, mapping in enumerate(rot_grid_points):
|
|
424
|
+
# Each mapping is a permutation, so this scatter has no races.
|
|
425
|
+
matrix[mapping[gp]][:, rotations[r], :] += source
|
|
426
|
+
self._collision_matrix[s, t] = host(matrix)
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def create_collision_matrix_kernel(*, rot_grid_points=None, **kwargs):
|
|
430
|
+
if rot_grid_points is None:
|
|
431
|
+
return GPUReducibleCollisionKernel(**kwargs)
|
|
432
|
+
return GPUIrreducibleCollisionKernel(rot_grid_points=rot_grid_points, **kwargs)
|