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
+32
-10
@@ -3,13 +3,16 @@
|
||||
|
||||
#include "PixelMask.h"
|
||||
#include "RawToConvertedGeometry.h"
|
||||
#include "TableChecksum.h"
|
||||
#include "JFJochException.h"
|
||||
#include "JFJochCompressor.h"
|
||||
|
||||
PixelMask::PixelMask() = default;
|
||||
|
||||
PixelMask::PixelMask(size_t width, size_t height)
|
||||
: mask(width*height, 0) {}
|
||||
: mask(width*height, 0) {
|
||||
UpdateBinaryMask();
|
||||
}
|
||||
|
||||
PixelMask::PixelMask(const DiffractionExperiment &experiment)
|
||||
: PixelMask(experiment.GetXPixelsNumConv(),
|
||||
@@ -17,7 +20,9 @@ PixelMask::PixelMask(const DiffractionExperiment &experiment)
|
||||
CalcEdgePixels(experiment);
|
||||
}
|
||||
|
||||
PixelMask::PixelMask(const std::vector<uint32_t> &in_mask) : mask(in_mask) {}
|
||||
PixelMask::PixelMask(const std::vector<uint32_t> &in_mask) : mask(in_mask) {
|
||||
UpdateBinaryMask();
|
||||
}
|
||||
|
||||
uint32_t PixelMask::LoadMask(const std::vector<uint32_t> &input_mask, uint8_t bit) {
|
||||
uint32_t ret = 0;
|
||||
@@ -35,7 +40,7 @@ uint32_t PixelMask::LoadMask(const std::vector<uint32_t> &input_mask, uint8_t bi
|
||||
return ret;
|
||||
}
|
||||
|
||||
void PixelMask::UpdateRawMask(const DiffractionExperiment &experiment) {
|
||||
void PixelMask::UpdateDerived(const DiffractionExperiment &experiment) {
|
||||
switch (experiment.GetDetectorType()) {
|
||||
case DetectorType::JUNGFRAU:
|
||||
case DetectorType::EIGER:
|
||||
@@ -46,6 +51,14 @@ void PixelMask::UpdateRawMask(const DiffractionExperiment &experiment) {
|
||||
raw_mask.clear();
|
||||
break;
|
||||
}
|
||||
UpdateBinaryMask();
|
||||
}
|
||||
|
||||
void PixelMask::UpdateBinaryMask() {
|
||||
binary_mask.resize(mask.size());
|
||||
for (size_t i = 0; i < mask.size(); i++)
|
||||
binary_mask[i] = (mask[i] != 0);
|
||||
binary_mask_checksum = TableChecksum(binary_mask.data(), binary_mask.size());
|
||||
}
|
||||
|
||||
void PixelMask::CalcEdgePixels_i(const DiffractionExperiment &experiment) {
|
||||
@@ -96,7 +109,7 @@ void PixelMask::CalcEdgePixels_i(const DiffractionExperiment &experiment) {
|
||||
|
||||
void PixelMask::CalcEdgePixels(const DiffractionExperiment &experiment) {
|
||||
CalcEdgePixels_i(experiment);
|
||||
UpdateRawMask(experiment);
|
||||
UpdateDerived(experiment);
|
||||
}
|
||||
|
||||
const std::vector<uint32_t> &PixelMask::GetMaskRaw() const {
|
||||
@@ -110,6 +123,14 @@ const std::vector<uint32_t> &PixelMask::GetMask() const {
|
||||
return mask;
|
||||
}
|
||||
|
||||
const std::vector<uint8_t> &PixelMask::GetBinaryMask() const {
|
||||
return binary_mask;
|
||||
}
|
||||
|
||||
uint64_t PixelMask::GetBinaryMaskChecksum() const {
|
||||
return binary_mask_checksum;
|
||||
}
|
||||
|
||||
const std::vector<uint32_t> &PixelMask::GetMask(const DiffractionExperiment& experiment) const {
|
||||
if (experiment.IsGeometryTransformed())
|
||||
return GetMask();
|
||||
@@ -180,7 +201,7 @@ void PixelMask::LoadDetectorBadPixelMask(const DiffractionExperiment &experiment
|
||||
|
||||
CalcEdgePixels_i(experiment);
|
||||
|
||||
UpdateRawMask(experiment);
|
||||
UpdateDerived(experiment);
|
||||
}
|
||||
|
||||
PixelMaskStatistics PixelMask::GetStatistics() const {
|
||||
@@ -207,12 +228,12 @@ PixelMaskStatistics PixelMask::GetStatistics() const {
|
||||
void PixelMask::LoadUserMask(const DiffractionExperiment& experiment, const std::vector<uint32_t> &in_mask) {
|
||||
if (in_mask.size() == mask.size()) {
|
||||
LoadMask(in_mask, UserMaskedPixelBit);
|
||||
UpdateRawMask(experiment);
|
||||
UpdateDerived(experiment);
|
||||
} else if (in_mask.size() == experiment.GetModulesNum() * RAW_MODULE_SIZE) {
|
||||
std::vector<uint32_t> tmp(experiment.GetPixelsNumConv(), 0);
|
||||
RawToConvertedGeometry(experiment, tmp.data(), in_mask. data());
|
||||
LoadMask(tmp, UserMaskedPixelBit);
|
||||
UpdateRawMask(experiment);
|
||||
UpdateDerived(experiment);
|
||||
} else
|
||||
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
||||
"Size of input user mask invalid");
|
||||
@@ -223,13 +244,13 @@ void PixelMask::LoadBeamStopMask(const DiffractionExperiment& experiment, const
|
||||
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
||||
"Size of input beam stop mask invalid");
|
||||
LoadMask(in_mask, BeamStopPixelBit);
|
||||
UpdateRawMask(experiment);
|
||||
UpdateDerived(experiment);
|
||||
}
|
||||
|
||||
void PixelMask::ClearBeamStopMask(const DiffractionExperiment& experiment) {
|
||||
for (auto &i: mask)
|
||||
i &= ~(1u << BeamStopPixelBit);
|
||||
UpdateRawMask(experiment);
|
||||
UpdateDerived(experiment);
|
||||
}
|
||||
|
||||
void PixelMask::LoadUserMask(const DiffractionExperiment& experiment, const CompressedImage& image) {
|
||||
@@ -307,6 +328,7 @@ void PixelMask::LoadDECTRISBadPixelMask(const std::vector<uint32_t> &input_mask)
|
||||
}
|
||||
}
|
||||
raw_mask = {}; // For DECTRIS - there is no raw mask
|
||||
UpdateBinaryMask();
|
||||
}
|
||||
|
||||
void PixelMask::LoadDarkBadPixelMask(const DiffractionExperiment& experiment, const std::vector<uint32_t> &input_mask) {
|
||||
@@ -325,5 +347,5 @@ void PixelMask::LoadDarkBadPixelMask(const DiffractionExperiment& experiment, co
|
||||
mask[i] &= ~(1 << NoisyPixelBit);
|
||||
}
|
||||
}
|
||||
UpdateRawMask(experiment);
|
||||
UpdateDerived(experiment);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user