Build the detector's lookup tables once, not once per worker
The image loop gives every worker its own analysis engine, so a run builds ninety-six of them. Each one derived, from scratch, tables that are the same in all of them: the byte-per-pixel mask, the resolution mask, the radial kernel, and the checksum that names the shared device tables. The checksum was the worst of it, because it is part of the cache KEY and so is computed before the lookup - a hit still hashed the whole table. On a 16 Mpx detector that is the bin table, the corrections and the mask, 126 MB an engine, about twelve gigabytes over a run, to answer a question whose answer had not changed. The header said it cost nothing measurable; a profile says otherwise, and says it is worst exactly during the ramp when the machine has nothing else to do. It cannot simply be remembered against the address, which is what it exists to catch: a buffer can be freed and another allocated where it was, and the cache would then hand back a device copy of something else. So the owner of the bytes computes it instead. The azimuthal mapping writes its two tables in its constructor and never again. The pixel mask re-derives its binary form and its checksum on every path that changes the mask, and all of those paths are now private to the class. The key therefore still describes the bytes as they are at the moment of the lookup. The resolution mask was two passes over every pixel - a float comparison into a vector<bool>, then a bit-by-bit repack - in each of the ninety-six. It is one pass now, writing the packed form directly, built once for the limits asked for and handed out as a shared pointer so a worker keeps the mask it was given. The radial kernel is cached on the six numbers it is derived from. Nothing computes a different value; only who computes it changes. Byte-identical merged output on a 16 Mpx set and on a small one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011n8riB6X59oRjkrSHzNPAU
This commit is contained in:
co-authored by
Claude Opus 5
parent
27020d27e9
commit
5ee0f22a61
@@ -5,8 +5,11 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
#include "../../common/JFJochMath.h" // PI (M_PI is not standard, and MSVC does not define it)
|
||||
|
||||
@@ -32,6 +35,24 @@ double parallax_var_px2(const std::string &material, double thickness_um, double
|
||||
return var / (pixel_um * pixel_um);
|
||||
}
|
||||
|
||||
// The radial-offset kernels below are a pure function of these six numbers, and one engine is built
|
||||
// per worker per pass - 96 of them on a two-pass run - so the table was built 96 times over from the
|
||||
// same inputs. Build it once and let the rest copy it; it is a few hundred floats. Two workers can
|
||||
// still race to build the same table, which costs nothing but the second build: the values are
|
||||
// identical, and emplace keeps whichever arrived first.
|
||||
struct RadialKernelKey {
|
||||
float r1_sq, r2, r3;
|
||||
int n_kern, k_off, k_len;
|
||||
|
||||
bool operator<(const RadialKernelKey &o) const {
|
||||
return std::tie(r1_sq, r2, r3, n_kern, k_off, k_len)
|
||||
< std::tie(o.r1_sq, o.r2, o.r3, o.n_kern, o.k_off, o.k_len);
|
||||
}
|
||||
};
|
||||
|
||||
std::mutex radial_kernel_mutex;
|
||||
std::map<RadialKernelKey, std::vector<float>> radial_kernel_cache;
|
||||
|
||||
} // namespace
|
||||
|
||||
BraggIntegrationEngine::BraggIntegrationEngine(const DiffractionExperiment &experiment)
|
||||
@@ -128,10 +149,19 @@ BraggIntegrationEngine::BraggIntegrationEngine(const DiffractionExperiment &expe
|
||||
// above grow_max.
|
||||
k_off = static_cast<int>(std::ceil(r3 + std::max<double>(grow_max, n_kern - 1))) + 1;
|
||||
k_len = 2 * k_off + 1;
|
||||
k_diff.clear();
|
||||
k_diff.reserve(static_cast<size_t>(n_kern) * k_len);
|
||||
for (int j = 0; j < n_kern; ++j)
|
||||
BuildRadialKernel(static_cast<float>(j));
|
||||
const RadialKernelKey kernel_key{r1_sq, r2, r3, n_kern, k_off, k_len};
|
||||
{
|
||||
const std::lock_guard lock(radial_kernel_mutex);
|
||||
if (const auto it = radial_kernel_cache.find(kernel_key); it != radial_kernel_cache.end())
|
||||
k_diff = it->second;
|
||||
}
|
||||
if (k_diff.empty()) {
|
||||
k_diff.reserve(static_cast<size_t>(n_kern) * k_len);
|
||||
for (int j = 0; j < n_kern; ++j)
|
||||
BuildRadialKernel(static_cast<float>(j));
|
||||
const std::lock_guard lock(radial_kernel_mutex);
|
||||
radial_kernel_cache.emplace(kernel_key, k_diff);
|
||||
}
|
||||
|
||||
polarization = experiment.GetPolarizationFactor();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user