XFELReader: per-frame mask and photon conversion without a library call or a branch per pixel
On a deflated Cheetah CXI with a per-frame mask (AGIPD 1M), half of the reader's time after the inflate went to turning the stored frame into the image: the mask plane widened into a zeroed uint32 copy before being tested, MarkBad testing it with a branch per pixel through a reference the store could alias, and the photon conversion calling lroundf and branching on the mask, the finiteness and the clamp. Now the mask is tested as stored (uint16 or uint32), and both loops are written so that the compiler vectorises them: std::lround spelled out exactly (truncate, the exact fraction, a half or more away from zero) and the selects done as arithmetic (Pick). Plain C++, no intrinsics. Bit-identical: FNV hash of every image handed out (2000 frames) plus the pixel mask, before and after, on a CXI with a CrystFEL geometry (with and without a gain), a panel-table HDF5 (with and without a gain) and a bitshuffle/LZ4 int16 set: all identical. Reader alone, one thread: 14.1 -> 7.0 ms per frame. rugnux on the 14,445-frame CXI run (16 workers, A/B back to back): geometry refinement sample 11.4 -> 8.9 s, image pass 27.6 -> 21.1 s, whole run 67.4 -> 58.2 s; geometry and refinement log lines identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
This commit is contained in:
1 parent
077fad9379
commit
d345deaecd
3 files changed
+61
-30
No files matched your search
@@ -61,8 +61,11 @@ struct Mask {
|
||||
|
||||
bool operator==(const Mask &) const = default;
|
||||
|
||||
[[nodiscard]] bool Bad(uint32_t v) const {
|
||||
return (bad != 0 && (v & bad) != 0) || (good != 0 && (v & good) != good) || (nonzero_good && v == 0);
|
||||
// Whether a pixel the mask reads as v is bad, given the mask's good, bad and nonzero_good (passed rather than
|
||||
// read from the mask, so that a loop over the pixels keeps them in registers). Each term is false where its
|
||||
// field is unset; bitwise, so that the loop has no branch.
|
||||
[[nodiscard]] static bool Bad(uint32_t v, uint32_t good, uint32_t bad, bool nonzero_good) {
|
||||
return ((v & bad) != 0) | ((v & good) != good) | (nonzero_good & (v == 0));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+54
-27
@@ -61,6 +61,13 @@ void Accumulate(HDF5ReadOnlyFile &file, const std::string &path, const std::vect
|
||||
mean.Add(v);
|
||||
}
|
||||
|
||||
// b where use_b, otherwise a - as arithmetic rather than a branch, which the compiler would move the work
|
||||
// computing a into, and then not vectorise the loop over the pixels.
|
||||
int32_t Pick(bool use_b, int32_t a, int32_t b) {
|
||||
const int32_t all = -static_cast<int32_t>(use_b);
|
||||
return (a & ~all) | (b & all);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
JFJochXFELReader::JFJochXFELReader() = default;
|
||||
@@ -551,7 +558,7 @@ void JFJochXFELReader::Place() {
|
||||
v = mask_file.ReadVector<uint32_t>(m.data, {0, 0}, {frame_height_, frame_width_});
|
||||
} else
|
||||
v = FileAt(0).ReadVector<uint32_t>(m.data, {0, 0}, {frame_height_, frame_width_});
|
||||
MarkBad(m, v, masked_by_file);
|
||||
MarkBad(m, v.data(), masked_by_file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -753,7 +760,8 @@ void JFJochXFELReader::Place() {
|
||||
SetStartMessage(dataset_);
|
||||
}
|
||||
|
||||
void JFJochXFELReader::MarkBad(const xfel::Mask &m, const std::vector<uint32_t> &v, std::vector<uint8_t> &bad) const {
|
||||
template <class T>
|
||||
void JFJochXFELReader::MarkBad(const xfel::Mask &m, const T *v, std::vector<uint8_t> &bad) const {
|
||||
int64_t min_fs = 0, max_fs = static_cast<int64_t>(frame_width_) - 1;
|
||||
int64_t min_ss = 0, max_ss = static_cast<int64_t>(frame_height_) - 1;
|
||||
for (const auto &p: geom_.panels)
|
||||
@@ -761,12 +769,18 @@ void JFJochXFELReader::MarkBad(const xfel::Mask &m, const std::vector<uint32_t>
|
||||
min_fs = p.min_fs; max_fs = p.max_fs;
|
||||
min_ss = p.min_ss; max_ss = p.max_ss;
|
||||
}
|
||||
for (int64_t ss = min_ss; ss <= max_ss; ss++)
|
||||
for (int64_t fs = min_fs; fs <= max_fs; fs++) {
|
||||
const size_t i = static_cast<size_t>(ss) * frame_width_ + static_cast<size_t>(fs);
|
||||
if (m.Bad(v[i]))
|
||||
bad[i] = 1;
|
||||
}
|
||||
// The mask's bits in locals and plain pointers, a row at a time: a byte stored through bad could otherwise
|
||||
// be m's or bad's own, every pixel would read them again, and the loop would not be vectorised.
|
||||
const uint32_t good = m.good, bad_bits = m.bad;
|
||||
const bool nonzero_good = m.nonzero_good;
|
||||
const size_t width = max_fs - min_fs + 1;
|
||||
for (int64_t ss = min_ss; ss <= max_ss; ss++) {
|
||||
const size_t first = static_cast<size_t>(ss) * frame_width_ + min_fs;
|
||||
const T *in = v + first;
|
||||
uint8_t *out = bad.data() + first;
|
||||
for (size_t i = 0; i < width; i++)
|
||||
out[i] |= xfel::Mask::Bad(in[i], good, bad_bits, nonzero_good);
|
||||
}
|
||||
}
|
||||
|
||||
void JFJochXFELReader::ApplyGain(float adu_per_photon) {
|
||||
@@ -890,8 +904,15 @@ void JFJochXFELReader::PlaceInto(const In *in, const std::vector<uint8_t> &maske
|
||||
buffer.resize(width_ * height_ * sizeof(Out));
|
||||
auto out = reinterpret_cast<Out *>(buffer.data());
|
||||
if (blocks_.empty()) { // the stored frame, as stored
|
||||
for (size_t i = 0; i < width_ * height_; i++)
|
||||
out[i] = !masked.empty() && masked[i] ? err : static_cast<Out>(convert(in[i]));
|
||||
const size_t n = width_ * height_;
|
||||
if (masked.empty())
|
||||
for (size_t i = 0; i < n; i++)
|
||||
out[i] = static_cast<Out>(convert(in[i]));
|
||||
else {
|
||||
const uint8_t *m = masked.data();
|
||||
for (size_t i = 0; i < n; i++)
|
||||
out[i] = static_cast<Out>(Pick(m[i] != 0, convert(in[i]), err));
|
||||
}
|
||||
return;
|
||||
}
|
||||
std::fill(out, out + width_ * height_, Out{0}); // between the panels
|
||||
@@ -934,33 +955,40 @@ CompressedImage JFJochXFELReader::DecodeInto(int64_t image_number, Buffer &buffe
|
||||
const auto &m = frame_masks_[i];
|
||||
const auto &mask_layout = frame_mask_layouts_[i];
|
||||
const Frame mask{frame.file, ReplaceEvent(m.data, frame.group), frame.index, frame.group};
|
||||
std::vector<uint32_t> v(frame_width_ * frame_height_);
|
||||
if (mask_layout.plain_type) {
|
||||
thread_local std::vector<uint8_t> mask_stored;
|
||||
ReadStored(mask, mask_layout, mask_stored, false);
|
||||
if (mask_layout.stored_size == 2) {
|
||||
const auto *p = reinterpret_cast<const uint16_t *>(mask_stored.data());
|
||||
std::copy(p, p + v.size(), v.begin());
|
||||
} else
|
||||
std::memcpy(v.data(), mask_stored.data(), v.size() * sizeof(uint32_t));
|
||||
if (mask_layout.stored_size == 2)
|
||||
MarkBad(m, reinterpret_cast<const uint16_t *>(mask_stored.data()), masked);
|
||||
else
|
||||
MarkBad(m, reinterpret_cast<const uint32_t *>(mask_stored.data()), masked);
|
||||
} else {
|
||||
std::unique_lock ul(hdf5_mutex);
|
||||
v = frame.index >= 0
|
||||
? FileAt(frame.file).ReadVector<uint32_t>(mask.dataset, {static_cast<hsize_t>(frame.index), 0, 0},
|
||||
{1, frame_height_, frame_width_})
|
||||
: FileAt(frame.file).ReadVector<uint32_t>(mask.dataset, {0, 0}, {frame_height_, frame_width_});
|
||||
std::vector<uint32_t> v;
|
||||
{
|
||||
std::unique_lock ul(hdf5_mutex);
|
||||
v = frame.index >= 0
|
||||
? FileAt(frame.file).ReadVector<uint32_t>(mask.dataset, {static_cast<hsize_t>(frame.index), 0, 0},
|
||||
{1, frame_height_, frame_width_})
|
||||
: FileAt(frame.file).ReadVector<uint32_t>(mask.dataset, {0, 0}, {frame_height_, frame_width_});
|
||||
}
|
||||
MarkBad(m, v.data(), masked);
|
||||
}
|
||||
MarkBad(m, v, masked);
|
||||
}
|
||||
}
|
||||
|
||||
const float gain = adu_per_photon_;
|
||||
// A float that is no number (NaN, inf) is no measurement; anything past int32 is clamped to it.
|
||||
// Written without a branch or a library call, so that the loop over the pixels is vectorised. The clamp
|
||||
// keeps the sign of an infinity; std::lround is spelled out: t is c truncated, c - t is exact, and a
|
||||
// fraction of a half or more rounds away from zero.
|
||||
const auto photons = [gain](auto v) {
|
||||
const float x = static_cast<float>(v) / gain;
|
||||
if (!std::isfinite(x))
|
||||
return INT32_MIN;
|
||||
return static_cast<int32_t>(std::lround(std::clamp(x, -2147483520.0f, 2147483520.0f)));
|
||||
constexpr float limit = 2147483520.0f;
|
||||
const float c = std::fabs(x) < limit ? x : std::copysign(limit, x);
|
||||
const auto t = static_cast<int32_t>(c);
|
||||
const float fraction = c - static_cast<float>(t);
|
||||
const int32_t rounded = t + (fraction >= 0.5f) - (fraction <= -0.5f);
|
||||
return Pick(!std::isfinite(x), rounded, INT32_MIN);
|
||||
};
|
||||
const auto as_is = [](auto v) { return v; };
|
||||
const auto image = [&](CompressedImageMode mode) {
|
||||
@@ -971,8 +999,7 @@ CompressedImage JFJochXFELReader::DecodeInto(int64_t image_number, Buffer &buffe
|
||||
const auto integers = [&](auto to_int32) {
|
||||
const auto convert = [to_int32](auto v) -> int32_t {
|
||||
if constexpr (std::is_signed_v<decltype(v)>)
|
||||
if (v == std::numeric_limits<decltype(v)>::min())
|
||||
return INT32_MIN;
|
||||
return Pick(v == std::numeric_limits<decltype(v)>::min(), to_int32(v), INT32_MIN);
|
||||
return to_int32(v);
|
||||
};
|
||||
const uint8_t *p = stored.data();
|
||||
|
||||
@@ -77,7 +77,8 @@ class JFJochXFELReader : public JFJochReader {
|
||||
// Masks stored with one plane per frame (a Cheetah CXI's per-shot mask), applied as each frame is read.
|
||||
std::vector<xfel::Mask> frame_masks_;
|
||||
// Marks in bad every stored pixel the mask m, read as v, leaves out (on its panel, where it has one).
|
||||
void MarkBad(const xfel::Mask &m, const std::vector<uint32_t> &v, std::vector<uint8_t> &bad) const;
|
||||
template <class T>
|
||||
void MarkBad(const xfel::Mask &m, const T *v, std::vector<uint8_t> &bad) const;
|
||||
|
||||
// Where every frame is stored as one chunk of a plain little-endian number type - deflated (and
|
||||
// optionally byte-shuffled), as Cheetah writes it, or bitshuffle/LZ4 (or zstd), as Jungfraujoch does -
|
||||
|
||||
Reference in new issue
Block a user