diff --git a/image_analysis/bragg_integration/BraggIntegrationEngineCPU.cpp b/image_analysis/bragg_integration/BraggIntegrationEngineCPU.cpp index 64a864868..edbe47b1b 100644 --- a/image_analysis/bragg_integration/BraggIntegrationEngineCPU.cpp +++ b/image_analysis/bragg_integration/BraggIntegrationEngineCPU.cpp @@ -44,7 +44,9 @@ struct ImageSampler { } // namespace BraggIntegrationEngineCPU::BraggIntegrationEngineCPU(const DiffractionExperiment &experiment) - : BraggIntegrationEngine(experiment) {} + : BraggIntegrationEngine(experiment), + refl_mask(static_cast(xpixel), static_cast(ypixel), 0), + owner(static_cast(xpixel), static_cast(ypixel), BRAGG_OWNER_NONE) {} template std::vector BraggIntegrationEngineCPU::RunImpl(const Sampler &img, @@ -71,17 +73,8 @@ std::vector BraggIntegrationEngineCPU::RunImpl(const Sampler &img, // signal is excluded from this reflection's r2..r3 background ring. The region is the INNER // stencil ellipse, taken in each neighbour's OWN frame - an elongated neighbour whose streak // is still marked as a disk would leak its tails into this reflection's ring. --- - // Back to empty what the previous call wrote (see the header). - for (const Rect &b : refl_mask_dirty) - for (int y = b.y0; y <= b.y1; ++y) - std::fill(refl_mask.begin() + y * W + b.x0, refl_mask.begin() + y * W + b.x1 + 1, 0); - refl_mask_dirty.clear(); - for (const Rect &b : owner_dirty) - for (int y = b.y0; y <= b.y1; ++y) - std::fill(owner.begin() + y * W + b.x0, owner.begin() + y * W + b.x1 + 1, BRAGG_OWNER_NONE); - owner_dirty.clear(); - - refl_mask.resize(npixel, 0); + refl_mask.Clear(); + owner.Clear(); for (size_t i = 0; i < npredicted; ++i) { const auto &r = predicted[i]; const BraggStencil st = MakeBraggStencil(r.predicted_x, r.predicted_y, stencil); @@ -89,12 +82,10 @@ std::vector BraggIntegrationEngineCPU::RunImpl(const Sampler &img, const int x1 = std::min(W - 1, static_cast(std::ceil(r.predicted_x + st.ex_in + 1.0f))); const int y0 = std::max(0, static_cast(std::floor(r.predicted_y - st.ey_in - 1.0f))); const int y1 = std::min(H - 1, static_cast(std::ceil(r.predicted_y + st.ey_in + 1.0f))); - if (x0 <= x1 && y0 <= y1) - refl_mask_dirty.push_back({x0, x1, y0, y1}); for (int y = y0; y <= y1; ++y) for (int x = x0; x <= x1; ++x) { const auto d = BraggStencilDistances(st, x - r.predicted_x, y - r.predicted_y); - if (d.inner < r2_sq) refl_mask[y * W + x] = 1; + if (d.inner < r2_sq) refl_mask.At(x, y) = 1; } } @@ -106,7 +97,6 @@ std::vector BraggIntegrationEngineCPU::RunImpl(const Sampler &img, const bool exclude = overlap == OverlapMode::Exclude && mode != IntegratorMode::BoxSum; const bool use_owner = overlap != OverlapMode::Off; if (use_owner) { - owner.resize(npixel, BRAGG_OWNER_NONE); const float claim_sq = claim * claim; for (size_t i = 0; i < npredicted; ++i) { const auto &r = predicted[i]; @@ -114,21 +104,19 @@ std::vector BraggIntegrationEngineCPU::RunImpl(const Sampler &img, const int x1 = std::min(W - 1, static_cast(std::ceil(r.predicted_x + claim))); const int y0 = std::max(0, static_cast(std::floor(r.predicted_y - claim))); const int y1 = std::min(H - 1, static_cast(std::ceil(r.predicted_y + claim))); - if (x0 <= x1 && y0 <= y1) - owner_dirty.push_back({x0, x1, y0, y1}); for (int y = y0; y <= y1; ++y) for (int x = x0; x <= x1; ++x) { const float dx = x - r.predicted_x, dy = y - r.predicted_y; const float d2 = dx * dx + dy * dy; if (d2 >= claim_sq) continue; - uint32_t &o = owner[y * W + x]; + uint32_t &o = owner.At(x, y); o = std::min(o, BraggOwnerKey(std::sqrt(d2), inv_claim, static_cast(i))); } } } auto clean = [&](int x, int y, size_t i) { return !use_owner - || BraggOwnedBy(owner[static_cast(y) * W + x], static_cast(i)); + || BraggOwnedBy(owner.Get(x, y), static_cast(i)); }; // --- Pass A: box-sum every reflection (rough I, background, centroid, strong flag). --- @@ -213,7 +201,7 @@ std::vector BraggIntegrationEngineCPU::RunImpl(const Sampler &img, y_sum += y; ++n_inner_valid; } else if (d.inner >= r2_sq && d.outer < r3_sq) { - if (refl_mask[y * W + x]) { ++n_bkg_neighbour; continue; } + if (refl_mask.Get(x, y)) { ++n_bkg_neighbour; continue; } if (!valid(px)) continue; bkg_sum += static_cast(px); if (bkg_trim_frac > 0.0) bkg_vals.push_back(px); @@ -267,7 +255,7 @@ std::vector BraggIntegrationEngineCPU::RunImpl(const Sampler &img, for (int x = x0; x <= x1; ++x) { const auto d = BraggStencilDistances(st, x - r.predicted_x, y - r.predicted_y); if (!(d.inner >= r2_sq && d.outer < r3_sq)) continue; - if (refl_mask[y * W + x]) continue; + if (refl_mask.Get(x, y)) continue; const int32_t px = img[y * W + x]; if (!valid(px)) continue; if (static_cast(px) <= thr) { diff --git a/image_analysis/bragg_integration/BraggIntegrationEngineCPU.h b/image_analysis/bragg_integration/BraggIntegrationEngineCPU.h index ed1861599..f125f391b 100644 --- a/image_analysis/bragg_integration/BraggIntegrationEngineCPU.h +++ b/image_analysis/bragg_integration/BraggIntegrationEngineCPU.h @@ -14,15 +14,44 @@ 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. Kept from - // call to call rather than allocated per image - at 18 Mpixel that was 90 MB, a fresh mmap and a - // page fault per 4 kB every call - and put back to their empty state one rectangle at a time: the - // rectangles a call writes are recorded, and the next call clears exactly those before it starts. - struct Rect { int x0, x1, y0, y1; }; - std::vector refl_mask; - std::vector owner; - std::vector refl_mask_dirty; - std::vector owner_dirty; + // 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 TiledFrame { + static constexpr int TILE = 16; + int tiles_x; + T empty; + std::vector tile_start; // per tile: where it starts in `pixels`, -1 = not written + std::vector written; // the tiles written, for Clear() + std::vector pixels; + public: + TiledFrame(int width, int height, T empty) + : tiles_x((width + TILE - 1) / TILE), empty(empty), + tile_start(static_cast(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(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 refl_mask; + TiledFrame owner; template std::vector RunImpl(const Sampler &img, const std::vector &predicted,