-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_MLS.py
More file actions
298 lines (257 loc) · 10.6 KB
/
Copy pathrun_MLS.py
File metadata and controls
298 lines (257 loc) · 10.6 KB
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
"""Evaluate MLS to obtain macroscale corrections.
This step reads the ``*_tasks.npz`` bundles prepared by
``3_update_metamodel`` and solves them in parallel using MPI. The predicted
corrections ``dQx.npy``, ``dQy.npy``, ``dP.npy`` and ``Fst.npy`` are written
to ``--output_dir`` for the subsequent macroscale solve.
Key command line options:
``--k_neighbors`` and ``--chunk_size`` control MLS workload.
``--output_dir`` points to input bundles and is where outputs are saved.
"""
import os
from utils.cli import parse_common_args
import time
from concurrent.futures import as_completed
from CONFIGPenalty import MLS_THETA, MLS_DEGREE
import numpy as np
from mpi4py import MPI
from mpi4py.futures import MPIPoolExecutor
from scipy.spatial import cKDTree
rank = MPI.COMM_WORLD.Get_rank()
size = MPI.COMM_WORLD.Get_size()
root = rank == 0
if root:
print(
f"[root] Master rank {rank} initialised with {size - 1} worker(s).", flush=True
)
# Global training data cached on each worker
G_MAT: np.ndarray | None = None # (Ntrain, Npoly)
G_Y: np.ndarray | None = None # (Ntrain,)
G_THETA: float | None = None
TRANSIENT_MODE = False
# --------------------------------------------------------------------------------------
# Worker initialiser - run only once on pool creation
# --------------------------------------------------------------------------------------
def init_worker():
"""Executed once on each worker when the pool starts to avoid memory overflow later"""
global G_MAT, G_Y, G_THETA
G_MAT = None
G_Y = None
G_THETA = None
print(
f"[worker-init] Rank {MPI.COMM_WORLD.Get_rank()} initialised globals",
flush=True,
)
# --------------------------------------------------------------------------------------
# Helpers executed by workers
# --------------------------------------------------------------------------------------
def update_globals(Y: np.ndarray, Mat: np.ndarray, theta: float):
"""Replace the global training data on a worker rank."""
global G_MAT, G_Y, G_THETA
G_MAT = Mat # view, not copy (already local on the worker)
G_Y = Y
G_THETA = float(theta)
return MPI.COMM_WORLD.Get_rank()
def _solve_one(idx: np.ndarray, dist: np.ndarray, w_thresh: float = 1e-1):
"""Weighted least squares for one query; executed inside the batch loop."""
wght = np.exp(-G_THETA * dist**2)
w_max = wght.max()
if w_max < 1e-15:
return np.zeros(G_MAT.shape[1])
mask = wght >= w_thresh * w_max
if np.count_nonzero(mask) < G_MAT.shape[1]:
mask = np.ones_like(mask, dtype=bool)
Mat_red = G_MAT[idx][mask]
Y_red = G_Y[idx][mask]
w_red = wght[mask]
Matw = Mat_red * w_red[:, None]
Pw = Y_red * w_red
alpha, *_ = np.linalg.lstsq(Matw, Pw, rcond=None)
return alpha
def batch_worker(
i_q_batch: np.ndarray,
idx_batch: np.ndarray,
dist_batch: np.ndarray,
w_thresh: float = 1e-3,
):
"""Executes on a worker: solves MLS for a *batch* of query indices."""
out = []
for local_idx, i_q in enumerate(i_q_batch):
alpha = _solve_one(idx_batch[local_idx], dist_batch[local_idx], w_thresh)
out.append((i_q, alpha))
return out, MPI.COMM_WORLD.Get_rank()
# --------------------------------------------------------------------------------------
# Controller-side helpers
# --------------------------------------------------------------------------------------
def ensure_all_workers_update(
pool: MPIPoolExecutor, Y: np.ndarray, Mat: np.ndarray, theta: float
):
"""Guarantee that *every* worker rank runs `update_globals`."""
futures = [pool.submit(update_globals, Y, Mat, theta) for _ in range(size - 1)]
for fut in as_completed(futures):
fut.result() # re-raise errors immediately
if root:
print("[root] All workers updated their training data", flush=True)
# --------------------------------------------------------------------------------------
# Other helper functions
# --------------------------------------------------------------------------------------
def process_prediction(
pool: MPIPoolExecutor,
tasks: np.ndarray,
Mati: np.ndarray,
Xi: np.ndarray,
Ymin: float,
Yrng: float,
output_filename: str,
output_dir: str,
theta: float,
k_neighbors: int,
chunk_size: int = 64,
):
"""Dispatches MLS solves to the worker pool and writes final prediction.
*Executed **only on the root rank***. Workers sit in the pool.
"""
Ni, Nt = len(tasks), Mati.shape[1]
print(
f"[root] process_prediction: {output_filename} with {Ni} query points",
flush=True,
)
placeholder = np.empty((Nt, Ni), dtype=float)
completed, rank_counts = 0, {}
# --- extract training arrays once (they are identical for every task) ----------
_, X_train, Y_train, Mat_train, _ = tasks[0]
# ---------- distribute training data to workers --------------------------------
ensure_all_workers_update(pool, Y_train, Mat_train, theta)
# ---------- KD-tree & neighbour search -----------------------------------------
tree = cKDTree(X_train)
dist_all, idx_all = tree.query(Xi, k=k_neighbors, workers=-1)
print("[root] KD-tree neighbour search done", flush=True)
# -----------------------------------------------------------------------------
# Fire off batches
# -----------------------------------------------------------------------------
submit_t0 = time.time()
futures = []
for start in range(0, Ni, chunk_size):
sli = slice(start, min(start + chunk_size, Ni))
i_q_batch = np.arange(start, min(start + chunk_size, Ni))
futures.append(
pool.submit(batch_worker, i_q_batch, idx_all[sli], dist_all[sli])
)
print(
f"[root] Submitted {len(futures)} batch tasks in {time.time() - submit_t0:.2f}s",
flush=True,
)
# collect results
for fut in as_completed(futures):
results, wrk = fut.result()
rank_counts[wrk] = rank_counts.get(wrk, 0) + len(results)
for i_q, alpha in results:
placeholder[:, i_q] = alpha
completed += len(results)
if completed % 500 == 0:
print(
f"[root] Completed {completed}/{Ni} rank_counts={rank_counts}",
flush=True,
)
# ----------------------------- gather & write output -------------------------
Mi = Mati * placeholder.T # (Ni, Nt)
Yi = Mi.sum(axis=1) * Yrng + Ymin
print(f"Minimum value of Yi: {np.min(Yi)}", flush=True)
np.save(os.path.join(output_dir, output_filename), Yi)
print(f"[root] Saved {output_filename} - shape {Yi.shape}", flush=True)
def main():
args = parse_common_args("Run MLS", MLS=True)
output_dir = os.path.join(args.output_dir)
os.makedirs(output_dir, exist_ok=True)
global TRANSIENT_MODE
TRANSIENT_MODE = args.transient
print(f'k_neighbors: {args.k_neighbors}')
t0 = time.time()
# --------------------------------------------------------------------------
# Load *.npz bundles per variable - identical structure for each.
# --------------------------------------------------------------------------
def load(name):
path = os.path.join(args.output_dir, f"{name}_tasks.npz")
data = np.load(path, allow_pickle=True)
feature_idx = None
feature_names = None
if "feature_idx" in data.files:
feature_idx = data["feature_idx"].tolist()
if "feature_names" in data.files:
feature_names = data["feature_names"].tolist()
return (
data["tasks"],
data["Mati"],
data["Xi"],
data["Ymin"].item(),
data["Yrng"].item(),
feature_idx,
feature_names,
)
dQx = load("dQx")
dQy = load("dQy")
dP = load("dP")
taustx = load("taustx")
tausty = load("tausty")
pmax = load("pmax")
pmin = load("pmin")
hmax = load("hmax")
hmin = load("hmin")
if root:
Ni = len(dQx[0])
Nt = dQx[1].shape[1]
print(f"[root] Loaded {Ni} query points, {Nt} polynomial terms.", flush=True)
# --------------------------------------------------------------------------
# Root rank drives the pool; workers do *not* enter this block.
# --------------------------------------------------------------------------
if root:
with MPIPoolExecutor(initializer=init_worker) as pool:
for idx, (name, pack) in enumerate(
zip(
(
"dQx",
"dQy",
"dP",
"taustx",
"tausty",
"pmax",
"pmin",
"hmax",
"hmin",
),
(dQx, dQy, dP, taustx, tausty, pmax, pmin, hmax, hmin),
)
):
tasks, Mati, Xi, Ymin, Yrng, feature_idx, feature_names = pack
if feature_idx is not None and feature_names is not None:
selected = [feature_names[i] for i in feature_idx]
print(
f"[root] ==== {name} features: {selected}",
flush=True,
)
print(
f"[root] ==== {name} === (theta={MLS_THETA[idx]}, degree={MLS_DEGREE[idx]})",
flush=True,
)
t_var = time.time()
process_prediction(
pool,
tasks,
Mati,
Xi,
Ymin,
Yrng,
f"{name}.npy",
args.output_dir,
MLS_THETA[idx],
k_neighbors=args.k_neighbors,
chunk_size=args.chunk_size,
)
print(f"[root] {name} done in {time.time() - t_var:.2f}s", flush=True)
print(f"[root] MLS evaluations finished in {time.time() - t0:.2f}s", flush=True)
else:
# Worker ranks idle here; the MPIPool server loop is already running inside
# mpi4py.futures runtime.
while True:
time.sleep(3600) # effectively wait forever; Ctrl+C / MPI abort ends run
if __name__ == "__main__":
main()