Loading...
Searching...
No Matches
Gradient.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/*
6 * Gradient.h
7 *
8 * Created on: 20 Oct 2020
9 * Author: Maxim Masterov
10 * Company: SURFSara
11 */
12
13#ifndef GPR_GRADIENT_H_
14#define GPR_GRADIENT_H_
15
16#include <vector>
17
21
22namespace aux {
23
28class Gradient {
29public:
31 virtual ~Gradient() { }
32
33 // Each pushed D1/D2/D12 block belongs to one requested coordinate.
34 // calculateDerivativesSameDim and calculateDerivativesDiffDim clear
35 // those matrices at the start of every coordinate, then add that
36 // coordinate's pairs. The pair-type matrices are not stored per
37 // coordinate: after a call that names several coordinates they hold
38 // the sum of those coordinates.
39
62 const gpr::Coord& x1, const gpr::Coord& x2,
63 const gpr::AtomsConfiguration& conf_info,
64 const gpr::FieldMatrixd& lengthScale, const gpr::FieldMatrixIdx& dims,
65 uint8_t calc_options,
66 gpr::Derivatives<std::vector<gpr::FieldMatrixd>>& derivatives,
67 gpr::Derivatives<std::vector<gpr::FieldMatrixd>>& derivatives_pt);
68
71 void setFarFieldRadius(double radius);
72 [[nodiscard]] double farFieldRadius() const
73 {
74 return far_field_radius_;
75 }
76
100 const gpr::Coord& x1, const gpr::Coord& x2,
101 const gpr::AtomsConfiguration& conf_info,
102 const gpr::FieldMatrixd& lengthScale, const gpr::FieldMatrixIdx& dims1,
103 const gpr::FieldMatrixIdx& dims2, const uint8_t calc_options,
104 const uint8_t direction_x2,
105 gpr::Derivatives<std::vector<gpr::FieldMatrixd>>& derivatives,
106 gpr::Derivatives<std::vector<gpr::FieldMatrixd>>& derivatives_pt);
107
108private:
109 double far_field_radius_ = 0.0;
110
125 // Inlined here so the gradient hot path (called billions of times per
126 // PET-MAD optimize via evaluateValueInDerivativeField below) can elide
127 // the cross-translation-unit function call. Cachegrind on synthetic
128 // n_obs=200 attributed 0.4% Ir + ~120M instructions to this trivial
129 // 4-multiply expression alone.
130 static inline double calculateDerivative(const double lengtScale2_rec,
131 const double val1,
132 const double val2)
133 {
134 return -4. * lengtScale2_rec * val1 * val2;
135 }
136
153 // Inlined here: cachegrind attributed 4.7% Ir to this function with
154 // 1.3 billion calls during a single-rank synthetic n_obs=200 train.
155 // Out-of-line in Gradient.cpp the compiler could not inline across the
156 // translation-unit boundary; bringing the body into the header lets
157 // every callsite (the inner (n,m) accumulation loop in
158 // calculateGradientBetweenMovingAtoms / MovingAndFrozen) elide the
159 // function-call overhead and constant-fold the calc_options branch.
160 static inline void evaluateValueInDerivativeField(
161 const gpr::Pair<Eigen::Index>& at, const uint8_t calc_options,
162 const uint8_t field_type, const double lengtScale2_rec,
163 const gpr::Index_t pt, const double val1, const double val2,
164 gpr::FieldMatrixd& D, std::vector<gpr::FieldMatrixd>& D_pt)
165 {
166 if (calc_options & field_type) {
167 const double deriv_ij =
168 calculateDerivative(lengtScale2_rec, val1, val2);
169 // += sums the atom pairs of the coordinate currently being
170 // assembled. The caller clears D before that coordinate.
171 D(at.first, at.second) += deriv_ij;
172 // The _pt field is allocated when the corresponding bit shifted
173 // four positions to the left is set in calc_options.
174 if (calc_options & (field_type << 4))
175 D_pt[pt](at.first, at.second) -= deriv_ij;
176 }
177 }
178
201 void calculateGradientBetweenMovingAtoms(
202 const gpr::Coord& x1, const gpr::Coord& x2,
203 const gpr::AtomsConfiguration& conf_info,
204 const gpr::FieldMatrixd& lengthScale, const gpr::Index_t direction_x1,
205 const gpr::Index_t direction_x2, const gpr::Indices2D& ind,
206 uint8_t calc_options, gpr::Derivatives<gpr::FieldMatrixd*> derivatives,
207 gpr::Derivatives<std::vector<gpr::FieldMatrixd>>& derivatives_pt,
208 std::vector<double>& invr2_scratch,
209 std::vector<double>& deriv2_scratch);
210
232 void calculateGradientBetweenMovingAndFrozenAtoms(
233 const gpr::Coord& x1, const gpr::Coord& x2,
234 const gpr::AtomsConfiguration& conf_info,
235 const gpr::FieldMatrixd& lengthScale, const gpr::Index_t direction_x1,
236 const gpr::Index_t direction_x2, const gpr::Indices2D& ind,
237 uint8_t calc_options, gpr::Derivatives<gpr::FieldMatrixd*> derivatives,
238 gpr::Derivatives<std::vector<gpr::FieldMatrixd>>& derivatives_pt,
239 std::vector<double>& invr2_scratch,
240 std::vector<double>& deriv2_scratch);
241
242 void accumulateFarFieldDerivatives(
243 const gpr::Coord& x1, const gpr::Coord& x2,
244 const gpr::AtomsConfiguration& conf_info,
245 const gpr::FieldMatrixd& lengthScale, gpr::Index_t moving,
246 gpr::Index_t direction_x1, gpr::Index_t direction_x2,
247 uint8_t calc_options, gpr::FieldMatrixd& D1, gpr::FieldMatrixd& D2,
249 gpr::Derivatives<std::vector<gpr::FieldMatrixd>>& derivatives_pt) const;
250
262 void resizeDerivativeFields(const gpr::Coord& x1, const gpr::Coord& x2,
263 const gpr::AtomsConfiguration& conf_info,
264 const uint8_t calc_options,
265 const uint8_t field_type, gpr::FieldMatrixd& D,
266 std::vector<gpr::FieldMatrixd>& D_pt);
267
276 void pushBackDerivativeField(const uint8_t calc_options,
277 const uint8_t field_type, gpr::FieldMatrixd& D,
278 std::vector<gpr::FieldMatrixd>& D_vec);
279};
280
281} /* namespace aux */
282
283#endif /* GPR_GRADIENT_H_ */
@ D1
Definition Enums.h:19
@ D12
Definition Enums.h:21
@ D2
Definition Enums.h:20
void setFarFieldRadius(double radius)
Definition Gradient.cpp:885
double farFieldRadius() const
Definition Gradient.h:72
virtual ~Gradient()
Definition Gradient.h:31
void calculateDerivativesDiffDim(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthScale, const gpr::FieldMatrixIdx &dims1, const gpr::FieldMatrixIdx &dims2, const uint8_t calc_options, const uint8_t direction_x2, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives_pt)
Evaluate gradient of covariance function, of which has been taken partial derivatives with respect to...
Definition Gradient.cpp:655
void calculateDerivativesSameDim(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthScale, const gpr::FieldMatrixIdx &dims, uint8_t calc_options, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives_pt)
Evaluate gradient of covariance function with respect to both input variables x and x2 (in same dimen...
Definition Gradient.cpp:555
Eigen::Matrix< Index_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixIdx
Definition Macros.h:29
FieldMatrixd Coord
Definition Macros.h:35
uint32_t Index_t
Definition Macros.h:17
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25
Stores configurations of all atoms in the system.
Definition Structures.h:832
Structure of directional derivatives.
Definition Structures.h:63
A simple pair with identical datatypes.
Definition Structures.h:44