24#undef HWY_TARGET_INCLUDE
25#define HWY_TARGET_INCLUDE "client/ForceNorm.cpp"
26#include <hwy/foreach_target.h>
27#include <hwy/highway.h>
29HWY_BEFORE_NAMESPACE();
31namespace HWY_NAMESPACE {
33namespace hn = hwy::HWY_NAMESPACE;
35double MaxFreeAtomForceNorm(
const double *HWY_RESTRICT forces,
36 const double *HWY_RESTRICT fixed,
long nAtoms) {
37 if (forces ==
nullptr || nAtoms <= 0) {
40 const hn::ScalableTag<double> d;
41 const size_t lanes = hn::Lanes(d);
42 const size_t n =
static_cast<size_t>(nAtoms);
43 const auto half = hn::Set(d, 0.5);
44 auto vmax = hn::Zero(d);
47 for (; i + lanes <= n; i += lanes) {
51 hn::LoadInterleaved3(d, forces + 3 * i, x, y, z);
55 hn::Sqrt(hn::Add(hn::Add(hn::Mul(x, x), hn::Mul(y, y)), hn::Mul(z, z)));
56 if (fixed !=
nullptr) {
57 auto fx = hn::Zero(d);
58 auto fy = hn::Zero(d);
59 auto fz = hn::Zero(d);
60 hn::LoadInterleaved3(d, fixed + 3 * i, fx, fy, fz);
61 const auto allFixed = hn::And(
62 hn::Gt(fx, half), hn::And(hn::Gt(fy, half), hn::Gt(fz, half)));
63 const auto free = hn::IfThenElseZero(hn::Not(allFixed), nrm);
66 if (!hn::AllFalse(d, hn::IsNaN(free))) {
67 return std::numeric_limits<double>::quiet_NaN();
69 vmax = hn::Max(vmax, free);
71 if (!hn::AllFalse(d, hn::IsNaN(nrm))) {
72 return std::numeric_limits<double>::quiet_NaN();
74 vmax = hn::Max(vmax, nrm);
78 const double tail = detail::maxFreeAtomForceNormScalar(
79 forces, fixed,
static_cast<long>(i), nAtoms);
80 if (std::isnan(tail)) {
83 return std::max(hn::ReduceMax(d, vmax), tail);
93HWY_EXPORT(MaxFreeAtomForceNorm);
97 return HWY_DYNAMIC_DISPATCH(MaxFreeAtomForceNorm)(forces, fixed, nAtoms);
double maxFreeAtomForceNormScalar(const double *forces, const double *fixed, long begin, long nAtoms)
Scalar max of per-atom Euclidean norms on [begin, nAtoms).
RAII resource manager for the ARTn C library with global synchronization.
double maxFreeAtomForceNorm(const double *forces, const double *fixed, long nAtoms)
Max Euclidean norm over N x 3 row-major force rows.