Loading...
Searching...
No Matches
ForceNorm.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12// Rule: no MulAdd, NegMulAdd, or MulSub on a result that can reach an output.
13#include "eon/ForceNorm.h"
14
15#include <algorithm>
16#include <cmath>
17#include <cstddef>
18#include <limits>
19
20#ifdef WITH_HIGHWAY
21
22// foreach_target.h re-includes this file once per SIMD target. The path is
23// relative to the project include root.
24#undef HWY_TARGET_INCLUDE
25#define HWY_TARGET_INCLUDE "client/ForceNorm.cpp"
26#include <hwy/foreach_target.h>
27#include <hwy/highway.h>
28
29HWY_BEFORE_NAMESPACE();
30namespace eonc {
31namespace HWY_NAMESPACE {
32
33namespace hn = hwy::HWY_NAMESPACE;
34
35double MaxFreeAtomForceNorm(const double *HWY_RESTRICT forces,
36 const double *HWY_RESTRICT fixed, long nAtoms) {
37 if (forces == nullptr || nAtoms <= 0) {
38 return 0.0;
39 }
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);
45
46 size_t i = 0;
47 for (; i + lanes <= n; i += lanes) {
48 auto x = hn::Zero(d);
49 auto y = hn::Zero(d);
50 auto z = hn::Zero(d);
51 hn::LoadInterleaved3(d, forces + 3 * i, x, y, z);
52 // Mul then Add, not MulAdd. A contracted FMA rounds once; the scalar
53 // tail rounds each product and the sum, and both paths must agree.
54 const auto nrm =
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);
64 // hn::Max keeps or drops a NaN depending on the target (maxpd on x86,
65 // vmaxq on NEON), so a NaN is returned before it reaches the max.
66 if (!hn::AllFalse(d, hn::IsNaN(free))) {
67 return std::numeric_limits<double>::quiet_NaN();
68 }
69 vmax = hn::Max(vmax, free);
70 } else {
71 if (!hn::AllFalse(d, hn::IsNaN(nrm))) {
72 return std::numeric_limits<double>::quiet_NaN();
73 }
74 vmax = hn::Max(vmax, nrm);
75 }
76 }
77
78 const double tail = detail::maxFreeAtomForceNormScalar(
79 forces, fixed, static_cast<long>(i), nAtoms);
80 if (std::isnan(tail)) {
81 return tail;
82 }
83 return std::max(hn::ReduceMax(d, vmax), tail);
84}
85
86} // namespace HWY_NAMESPACE
87} // namespace eonc
88HWY_AFTER_NAMESPACE();
89
90#if HWY_ONCE
91namespace eonc {
92
93HWY_EXPORT(MaxFreeAtomForceNorm);
94
95double maxFreeAtomForceNorm(const double *forces, const double *fixed,
96 long nAtoms) {
97 return HWY_DYNAMIC_DISPATCH(MaxFreeAtomForceNorm)(forces, fixed, nAtoms);
98}
99
100} // namespace eonc
101#endif // HWY_ONCE
102
103#else // !WITH_HIGHWAY
104
105namespace eonc {
106
107double maxFreeAtomForceNorm(const double *forces, const double *fixed,
108 long nAtoms) {
109 return detail::maxFreeAtomForceNormScalar(forces, fixed, 0, nAtoms);
110}
111
112} // namespace eonc
113
114#endif // WITH_HIGHWAY
double maxFreeAtomForceNormScalar(const double *forces, const double *fixed, long begin, long nAtoms)
Scalar max of per-atom Euclidean norms on [begin, nAtoms).
Definition ForceNorm.h:25
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.