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
//! Spatial-kernel design operators, split out of `matrix/mod.rs` by
//! concern (#1145). Re-exported from `matrix` so the public paths
//! `crate::matrix::{SpatialKernelEvaluator, ChunkedKernelDesignOperator}`
//! stay stable.
use super::*;
use crate::solver::gauge::Gauge;
pub trait SpatialKernelEvaluator: Send + Sync + 'static {
fn eval(&self, x: &[f64], c: &[f64]) -> f64;
}
impl<F> SpatialKernelEvaluator for F
where
F: Fn(&[f64], &[f64]) -> f64 + Send + Sync + 'static,
{
fn eval(&self, x: &[f64], c: &[f64]) -> f64 {
self(x, c)
}
}
impl<F> SpatialKernelEvaluator for Arc<F>
where
F: Fn(&[f64], &[f64]) -> f64 + Send + Sync + 'static + ?Sized,
{
fn eval(&self, x: &[f64], c: &[f64]) -> f64 {
self.as_ref()(x, c)
}
}
/// Chunked kernel design operator for spatial smooths (TPS, Matérn, Duchon).
///
/// Instead of storing a dense n × k matrix, evaluates K(data[i], center[j])
/// on-the-fly in row chunks. Memory usage is O(chunk_size × k) instead of O(n × k).
///
/// The optional `poly_basis` appends polynomial columns after the kernel columns
/// (e.g., linear polynomial for TPS identifiability).
///
/// The optional `kernel_gauge` restricts the kernel coefficient block through
/// a Gauge section, so the effective design is [K_reduced | poly] instead of
/// [K | poly].
pub struct ChunkedKernelDesignOperator<K: SpatialKernelEvaluator> {
/// Observation data points (n × d).
data: Arc<Array2<f64>>,
/// Radial basis centers (k × d).
centers: Arc<Array2<f64>>,
/// Kernel evaluator: (data_row, center_row) -> f64.
kernel: K,
/// Optional coefficient-space gauge applied to kernel columns.
kernel_gauge: Option<Arc<Gauge>>,
/// Optional polynomial basis columns (n × m) appended after kernel columns.
poly_basis: Option<Arc<Array2<f64>>>,
n: usize,
total_cols: usize,
/// One-time-materialized [K_eff | poly] block, populated on first hot use.
/// Only allocated when the dense block fits within the materialization budget;
/// reused across all PIRLS iterations and outer-seed evaluations.
materialized: OnceLock<Option<Arc<Array2<f64>>>>,
}
impl<K: SpatialKernelEvaluator> ChunkedKernelDesignOperator<K> {
pub fn new(
data: Arc<Array2<f64>>,
centers: Arc<Array2<f64>>,
kernel: K,
kernel_gauge: Option<Arc<Gauge>>,
poly_basis: Option<Arc<Array2<f64>>>,
) -> Result<Self, String> {
let n = data.nrows();
let k = centers.nrows();
if data.ncols() != centers.ncols() {
return Err(format!(
"ChunkedKernelDesignOperator: data dim {} != centers dim {}",
data.ncols(),
centers.ncols(),
));
}
if let Some(gauge) = kernel_gauge.as_ref()
&& gauge.raw_total() != k
{
return Err(format!(
"ChunkedKernelDesignOperator: kernel gauge raw width {} != centers rows {}",
gauge.raw_total(),
k,
));
}
if let Some(poly) = poly_basis.as_ref()
&& poly.nrows() != n
{
return Err(format!(
"ChunkedKernelDesignOperator: poly_basis rows {} != data rows {}",
poly.nrows(),
n,
));
}
let k_eff = kernel_gauge.as_ref().map_or(k, |g| g.reduced_total());
let poly_cols = poly_basis.as_ref().map_or(0, |p| p.ncols());
Ok(Self {
data: Arc::new(data.as_standard_layout().to_owned()),
centers: Arc::new(centers.as_standard_layout().to_owned()),
kernel,
kernel_gauge,
poly_basis,
n,
total_cols: k_eff + poly_cols,
materialized: OnceLock::new(),
})
}
/// Maximum bytes we are willing to spend on the one-shot materialized
/// [K_eff | poly] block. The lazy operator was originally selected because
/// the *initial* fit-time allocation budget was tight, but once PIRLS is
/// running we will pay the kernel-evaluation cost on every iteration unless
/// we cache the result. 1 GB is generous enough to cover large-scale
/// dense Duchon / TPS (n = 320k, p = 117 → ~300 MiB) while still rejecting
/// pathological dense kernels.
const MATERIALIZE_MAX_BYTES: usize = 1024 * 1024 * 1024;
/// Get-or-build the materialized [K_eff | poly] dense block. Returns
/// `None` when the block would exceed `MATERIALIZE_MAX_BYTES`; in that
/// case callers must fall back to row-chunked evaluation.
///
/// Implementation note: the build path runs `par_chunks_mut` inside
/// `kernel_chunk`, so we deliberately compute *outside* the
/// `OnceLock`. Using `get_or_init` would hold the lock across that
/// nested rayon work, and any sibling rayon workers that reach
/// `get_or_init` while the build is in flight would park on the
/// `OnceLock`'s OS mutex — starving the build's nested `par_iter`
/// and deadlocking the whole pool (every worker in
/// `pthread_mutex_wait`, init thread in `pthread_cond_wait`,
/// 0% CPU). See `feedback_oncelock_rayon_deadlock`. Computing
/// without the lock costs at most one redundant build per racing
/// caller — `OnceLock::set` discards losers; `get` after `set`
/// always observes the winning value regardless of who won.
fn materialized_combined(&self) -> Option<&Array2<f64>> {
if let Some(slot) = self.materialized.get() {
return slot.as_ref().map(|a| a.as_ref());
}
let bytes = self
.n
.checked_mul(self.total_cols)
.and_then(|cells| cells.checked_mul(std::mem::size_of::<f64>()));
let computed = match bytes {
Some(b) if b <= Self::MATERIALIZE_MAX_BYTES => {
Some(Arc::new(self.build_row_chunk_combined(0..self.n)))
}
_ => None,
};
if self.materialized.set(computed).is_err() {
return self
.materialized
.get()
.and_then(|opt| opt.as_ref().map(|a| a.as_ref()));
}
self.materialized
.get()
.and_then(|opt| opt.as_ref().map(|a| a.as_ref()))
}
/// Evaluate kernel block for a range of rows, then restrict it through the
/// coefficient Gauge when present.
///
/// This is not a matrix Kronecker product. The center rows are coordinate
/// arguments to `kernel.eval(data_row, center_row)`; each output entry is a
/// scalar kernel value before the optional column projection.
fn kernel_chunk(&self, rows: Range<usize>) -> Array2<f64> {
let chunk_n = rows.end - rows.start;
let k_raw = self.centers.nrows();
let dim = self.data.ncols();
let data = self
.data
.as_slice()
.expect("ChunkedKernelDesignOperator stores standard-layout data");
let centers = self
.centers
.as_slice()
.expect("ChunkedKernelDesignOperator stores standard-layout centers");
let kernel = &self.kernel;
let mut values = vec![0.0_f64; chunk_n * k_raw];
values
.par_chunks_mut(k_raw)
.enumerate()
.for_each(|(local, out_row)| {
let global = rows.start + local;
let x_start = global * dim;
let x = &data[x_start..x_start + dim];
for j in 0..k_raw {
let c_start = j * dim;
out_row[j] = kernel.eval(x, ¢ers[c_start..c_start + dim]);
}
});
let kernel_block = Array2::from_shape_vec((chunk_n, k_raw), values)
.expect("kernel chunk shape should match generated values");
if let Some(gauge) = self.kernel_gauge.as_ref() {
gauge.restrict_design(&kernel_block)
} else {
kernel_block
}
}
}
impl<K: SpatialKernelEvaluator> LinearOperator for ChunkedKernelDesignOperator<K> {
fn nrows(&self) -> usize {
self.n
}
fn ncols(&self) -> usize {
self.total_cols
}
fn apply(&self, vector: &Array1<f64>) -> Array1<f64> {
if let Some(combined) = self.materialized_combined() {
return fast_av(combined, vector);
}
let k_eff = self
.kernel_gauge
.as_ref()
.map_or(self.centers.nrows(), |g| g.reduced_total());
let v_kernel = vector.slice(s![..k_eff]);
let mut result = Array1::<f64>::zeros(self.n);
// Process in chunks to limit memory.
for start in (0..self.n).step_by(KERNEL_OPERATOR_ROW_CHUNK_SIZE) {
let end = (start + KERNEL_OPERATOR_ROW_CHUNK_SIZE).min(self.n);
let chunk = self.kernel_chunk(start..end);
let partial = fast_av(&chunk, &v_kernel);
result.slice_mut(s![start..end]).assign(&partial);
}
if let Some(poly) = self.poly_basis.as_ref() {
let v_poly = vector.slice(s![k_eff..]);
let poly_part = fast_av(poly, &v_poly);
result += &poly_part;
}
result
}
fn apply_transpose(&self, vector: &Array1<f64>) -> Array1<f64> {
if let Some(combined) = self.materialized_combined() {
return fast_atv(combined, vector);
}
let k_eff = self
.kernel_gauge
.as_ref()
.map_or(self.centers.nrows(), |g| g.reduced_total());
let mut result = Array1::<f64>::zeros(self.total_cols);
// Kernel part: chunked accumulation of K^T v.
for start in (0..self.n).step_by(KERNEL_OPERATOR_ROW_CHUNK_SIZE) {
let end = (start + KERNEL_OPERATOR_ROW_CHUNK_SIZE).min(self.n);
let chunk = self.kernel_chunk(start..end);
let v_slice = vector.slice(s![start..end]);
let partial = fast_atv(&chunk, &v_slice);
result.slice_mut(s![..k_eff]).scaled_add(1.0, &partial);
}
// Poly part.
if let Some(poly) = self.poly_basis.as_ref() {
let poly_part = fast_atv(poly, vector);
result.slice_mut(s![k_eff..]).assign(&poly_part);
}
result
}
fn diag_xtw_x(&self, weights: &Array1<f64>) -> Result<Array2<f64>, String> {
let p = self.total_cols;
// [STAGE] kernel-xtwx: prefer the one-shot materialized [K_eff | poly]
// block + faer streaming GEMM. This is the BLAS-3 path that beats
// the per-iteration kernel rebuild by an order of magnitude on dense
// Duchon / TPS designs.
if let Some(combined) = self.materialized_combined() {
let mut xtwx = Array2::<f64>::zeros((p, p));
stream_weighted_crossprod_into(
combined,
weights,
&mut xtwx,
CrossprodStructure::Full,
CrossprodAccum::Replace,
effective_global_parallelism(),
);
return Ok(xtwx);
}
// Fallback: design too large to materialize. Run row chunks in
// parallel, each thread folding into its own p×p accumulator and
// performing one BLAS-3 GEMM (Xc^T·(W·Xc)) per chunk.
let n = self.n;
if n == 0 || p == 0 {
return Ok(Array2::<f64>::zeros((p, p)));
}
let chunk_starts: Vec<usize> = (0..n).step_by(KERNEL_OPERATOR_ROW_CHUNK_SIZE).collect();
let xtwx = chunk_starts
.into_par_iter()
.fold(
|| Array2::<f64>::zeros((p, p)),
|mut acc, start| {
let end = (start + KERNEL_OPERATOR_ROW_CHUNK_SIZE).min(n);
let chunk = self.row_chunk_combined(start..end);
let mut wchunk = chunk.clone();
for local in 0..(end - start) {
let wi = weights[start + local];
wchunk.row_mut(local).mapv_inplace(|v| v * wi);
}
let chunk_view = FaerArrayView::new(&chunk);
let wchunk_view = FaerArrayView::new(&wchunk);
let mut acc_view = array2_to_matmut(&mut acc);
matmul(
acc_view.as_mut(),
Accum::Add,
chunk_view.as_ref().transpose(),
wchunk_view.as_ref(),
1.0,
Par::Seq,
);
acc
},
)
.reduce(
|| Array2::<f64>::zeros((p, p)),
|mut a, b| {
a += &b;
a
},
);
Ok(xtwx)
}
}
impl<K: SpatialKernelEvaluator> ChunkedKernelDesignOperator<K> {
/// Combined row chunk: [kernel_chunk | poly_chunk]. Reuses the cached
/// materialization when available to avoid recomputing kernel evaluations.
pub(crate) fn row_chunk_combined(&self, rows: Range<usize>) -> Array2<f64> {
if let Some(combined) = self.materialized_combined() {
return combined.slice(s![rows, ..]).to_owned();
}
self.build_row_chunk_combined(rows)
}
/// Build a combined row chunk from scratch, bypassing the cache. Used by
/// `row_chunk_combined` on a cache miss and by `materialized_combined`'s
/// initializer (which must avoid re-entering the OnceLock).
fn build_row_chunk_combined(&self, rows: Range<usize>) -> Array2<f64> {
let chunk_n = rows.end - rows.start;
let k_eff = self
.kernel_gauge
.as_ref()
.map_or(self.centers.nrows(), |g| g.reduced_total());
let kernel = self.kernel_chunk(rows.clone());
let poly_cols = self.poly_basis.as_ref().map_or(0, |p| p.ncols());
let mut combined = Array2::<f64>::zeros((chunk_n, k_eff + poly_cols));
combined.slice_mut(s![.., ..k_eff]).assign(&kernel);
if let Some(poly) = self.poly_basis.as_ref() {
combined
.slice_mut(s![.., k_eff..])
.assign(&poly.slice(s![rows, ..]));
}
combined
}
}
impl<K: SpatialKernelEvaluator> DenseDesignOperator for ChunkedKernelDesignOperator<K> {
/// Expose the cached [K_eff | poly] materialization so cross-block paths
/// can use the Dense × Dense BLAS-3 fast path instead of falling back to
/// chunked scalar accumulation.
fn as_dense_ref(&self) -> Option<&Array2<f64>> {
self.materialized_combined()
}
fn row_chunk_into(
&self,
rows: Range<usize>,
mut out: ArrayViewMut2<'_, f64>,
) -> Result<(), MatrixMaterializationError> {
if out.nrows() != rows.end - rows.start || out.ncols() != self.total_cols {
return Err(MatrixMaterializationError::MissingRowChunk {
context: "ChunkedKernelDesignOperator::row_chunk_into shape mismatch",
});
}
if let Some(combined) = self.materialized_combined() {
out.assign(&combined.slice(s![rows, ..]));
} else {
out.assign(&self.row_chunk_combined(rows));
}
Ok(())
}
fn to_dense(&self) -> Array2<f64> {
if let Some(combined) = self.materialized_combined() {
return combined.clone();
}
self.row_chunk_combined(0..self.n)
}
}