Files
leonarski_fandClaude Opus 5.5 a609f6ba1c rugnux two-pass: the integration radius decided on the pre-pass frames, before the canonical pass
Where the measured spot width widened the integration radius, the canonical pass
integrated the whole sweep at the widened radius, and only then did the starvation
guard read how many background rings the neighbouring predictions crowd; over the
bound (1.13%) the pass was thrown away and run again at the fixed radius.

The geometry pre-pass now counts that same quantity at the widened radii without
integrating there (BraggIntegrationEngineCPU::CountNeighbourCrowding: the neighbour
mask and the ring pixels the run's mask leaves readable, no image) on 20 frames
spread over the pre-pass, with the standard error of the ratio over those frames.
Over the bound, the canonical pass integrates at the fixed radius from the outset.
A count within NEAR_TIE_SIGMA of the bound is a near tie in the part-B sense: the
canonical pass counts the widened radii again on 20 frames over the whole sweep,
and where that puts them under the bound the run is made again deciding late. A
run deciding late counts nothing early and decides as before. Under the bound the
canonical pass integrates at the widened radius and the measured guard stays as it
was, so a pre-pass count that is too low (its prediction holds fewer tails than
the canonical pass's, or it stood on another lattice class) costs only the pass
it cost before. No new threshold.

Battery against 46be3f647 (the 14 open sets that re-ran at the fixed radius, the
9 open/in-house sets closest under the bound, the ci tier, 2 private sets):
- p.hkl and MTZ data identical on all 51 open/in-house sets and on one private set.
- The 14 re-running sets: rugnux wall 534 -> 447 s (7qij -27, 4nwv -12, 7yzx -10,
  9fhc -9, 5src -6 s, on a shared machine). 8sqq and 8xtf count under the bound on
  the pre-pass and re-run as before. 4nwv (1.69 +- 0.28%) and one other set were
  near ties, re-checked on the whole sweep (1.72%, 1.61%), ending where they did.
- One private, densely crowded set: same space group and d_min, R_meas 28.6 ->
  39.3%. Its old re-run re-indexed starting from the spot budget the thrown-away
  pass had measured (152 of 1000). With that budget given (--max-spots 152), the
  new run gives 30.6%. The difference comes from that path, not from the radius.
- The count costs ~13 CPU-s on the heaviest set (65k predictions a frame). Over
  the 22 ci sets that count, pre-pass integration took 45.2 -> 45.8 s.
- A build forcing the near tie and its overturn on 4nwv re-checks on the whole
  sweep (1.80% against the engine's 1.81%) and makes the run again deciding late.
  It ends with the same p.hkl.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EizBKhTqrAYago9KJG3dA3
2026-10-11 13:01:23 +02:00

89 lines
4.6 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "BraggIntegrationEngine.h"
#include "../../common/PixelMask.h"
class CompressedImage;
// Plain-C++ reference/fallback engine: a faithful serial re-expression of BraggIntegrate2D (box
// sum) and ProfileIntegrate2D (Kabsch profile fit) reading the preprocessed int32 image. Also the
// numeric oracle the CUDA engine is checked against.
class BraggIntegrationEngineCPU : public BraggIntegrationEngine {
// Core integrator, templated on a pixel sampler so it reads either the preprocessed int32 buffer
// or a raw CompressedImage of any pixel type - both presented per-pixel in the INT32_MIN(masked)/
// INT32_MAX(saturated) convention - without ever materialising a second full-image copy.
// Full-frame scratch of RunImpl: the reflection mask and the signal-region owner map. A call writes
// only around its reflections, so these keep only the 16x16-pixel tiles written since the last
// Clear(): a frame-sized array was mostly never read, yet over a sweep every page of it got
// touched, in every worker. Reading a tile nothing wrote gives `empty`.
template <class T>
class TiledFrame {
static constexpr int TILE = 16;
int tiles_x;
T empty;
std::vector<int32_t> tile_start; // per tile: where it starts in `pixels`, -1 = not written
std::vector<int32_t> written; // the tiles written, for Clear()
std::vector<T> pixels;
public:
TiledFrame(int width, int height, T empty)
: tiles_x((width + TILE - 1) / TILE), empty(empty),
tile_start(static_cast<size_t>(tiles_x) * ((height + TILE - 1) / TILE), -1) {}
T Get(int x, int y) const {
const int32_t start = tile_start[(y / TILE) * tiles_x + x / TILE];
return start < 0 ? empty : pixels[start + (y % TILE) * TILE + x % TILE];
}
T &At(int x, int y) {
const int t = (y / TILE) * tiles_x + x / TILE;
if (tile_start[t] < 0) {
tile_start[t] = static_cast<int32_t>(pixels.size());
pixels.resize(pixels.size() + TILE * TILE, empty);
written.push_back(t);
}
return pixels[tile_start[t] + (y % TILE) * TILE + x % TILE];
}
void Clear() {
for (int t : written)
tile_start[t] = -1;
written.clear();
pixels.clear();
}
};
TiledFrame<uint8_t> refl_mask;
TiledFrame<uint32_t> owner;
// The run's pixel mask, packed 32 pixels to a word (PixelMask::GetPackedMask). An unreadable pixel
// it does not explain was unreadable on this frame only - an overload (see Reflection::overloaded).
std::vector<uint32_t> static_mask;
// Marks every predicted reflection's r2 region in refl_mask (MASK_FLUX / MASK_TAIL).
void MarkNeighbours(const std::vector<Reflection> &predicted, size_t npredicted);
template <class Sampler>
std::vector<Reflection> RunImpl(const Sampler &img, const std::vector<Reflection> &predicted,
size_t npredicted, int64_t image_number);
public:
BraggIntegrationEngineCPU(const DiffractionExperiment &experiment, const PixelMask &mask);
using BraggIntegrationEngine::Run; // keep the preprocessed-buffer overload visible
std::vector<Reflection> Run(const ImagePreprocessorBuffer &image,
const std::vector<Reflection> &predicted, size_t npredicted,
int64_t image_number) override;
// Only the neighbour count of these predicted reflections at this engine's stencil
// (BraggIntegrationCounts::predicted and bkg_starved_by_neighbour), reading no image: what a pass
// integrating at one radius measures of another. A ring pixel counts as readable unless the run's
// mask excludes it, so an overload on one frame is read as readable here.
void CountNeighbourCrowding(const std::vector<Reflection> &predicted, size_t npredicted);
// FPGA workflow: integrate straight off the assembled detector image, reading only the pixels
// inside each reflection disk (no whole-image conversion - the FPGA host cannot afford one at its
// frame rate). Masked pixels carry the type minimum and saturated the type maximum.
std::vector<Reflection> Run(const CompressedImage &image,
const std::vector<Reflection> &predicted, size_t npredicted,
int64_t image_number);
};