Loading...
Searching...
No Matches
GradientHwy.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5// Highway SIMD-accelerated inner kernel for
6// Gradient::calculateGradientBetweenMovingAtoms().
7//
8// Vectorizes the innermost (fixed n, over m) accumulation that dominates the
9// gradient-enhanced covariance assembly (the compute-bound hot path):
10// r_diff = invr1 - invr2[m]
11// D1row[m] += c1 * r_diff
12// D12row[m] += c1 * deriv2[m]
13// D2row[m] += (c * (-r_diff)) * deriv2[m]
14// with c = -4 * lengtScale2_rec and c1 = c * deriv_x1. Pairtype fields store
15// the negative of the same contribution in the active pairtype block.
16
17#ifndef GPR_AUXILIARY_GRADIENT_HWY_H_
18#define GPR_AUXILIARY_GRADIENT_HWY_H_
19
20#ifdef USE_HIGHWAY
21
22#include <hwy/highway.h>
23
24#include <cstdint>
25
26namespace gpr {
27namespace hwy_kernels {
28
29// Accumulate the D1/D2/D12 contributions for one x1 row (fixed n) across all
30// m in [0, count). Pointers are the contiguous (RowMajor) row starts.
31HWY_ATTR void accumulate_gradient_blocks(double* HWY_RESTRICT d1_row,
32 double* HWY_RESTRICT d2_row,
33 double* HWY_RESTRICT d12_row,
34 const double* HWY_RESTRICT invr2,
35 const double* HWY_RESTRICT deriv2,
36 double c, double deriv_x1,
37 double invr1, int64_t count);
38
39// Batched: the whole n1 x n2 block in one call (d*_data = block bases, row
40// stride d_cols; invr1_arr/deriv_x1_arr = per-row precomputes) so the per-row
41// cross-TU dispatch/call overhead is amortized. Identical arithmetic to the
42// per-row variant.
43HWY_ATTR void accumulate_gradient_blocks_batched(
44 double* HWY_RESTRICT d1_data, double* HWY_RESTRICT d2_data,
45 double* HWY_RESTRICT d12_data, int64_t d_cols, int64_t n1,
46 const double* HWY_RESTRICT invr1_arr,
47 const double* HWY_RESTRICT deriv_x1_arr, const double* HWY_RESTRICT invr2,
48 const double* HWY_RESTRICT deriv2, double c, int64_t n2);
49
50// Batched aggregate + pairtype variant. Each non-null aggregate pointer gets
51// D += deriv, and each non-null pairtype pointer gets D_pt -= deriv over the
52// same n1 x n2 block and row stride.
53HWY_ATTR void accumulate_gradient_blocks_batched_pairtype(
54 double* HWY_RESTRICT d1_data, double* HWY_RESTRICT d2_data,
55 double* HWY_RESTRICT d12_data, double* HWY_RESTRICT d1pt_data,
56 double* HWY_RESTRICT d2pt_data, double* HWY_RESTRICT d12pt_data,
57 int64_t d_cols, int64_t n1, const double* HWY_RESTRICT invr1_arr,
58 const double* HWY_RESTRICT deriv_x1_arr, const double* HWY_RESTRICT invr2,
59 const double* HWY_RESTRICT deriv2, double c, int64_t n2);
60
61// Vectorized inverse-distance precompute over a contiguous batch:
62// invr[i] = 1 / sqrt(r2[i])
63// deriv[i] = num[i] * invr[i]^3
64// The expensive sqrt/reciprocal is done SIMD-wide; bit-identical to the scalar
65// 1.0/std::sqrt + cube (hardware vsqrtpd rounds like std::sqrt).
66HWY_ATTR void recip_sqrt_cube(double* HWY_RESTRICT invr,
67 double* HWY_RESTRICT deriv,
68 const double* HWY_RESTRICT r2,
69 const double* HWY_RESTRICT num, int64_t count);
70
71} // namespace hwy_kernels
72} // namespace gpr
73
74#endif // USE_HIGHWAY
75#endif // GPR_AUXILIARY_GRADIENT_HWY_H_
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16