diff --git a/image_analysis/geom_refinement/BeamCenterFromBackground.cpp b/image_analysis/geom_refinement/BeamCenterFromBackground.cpp index 7bc73c348..82e87ef86 100644 --- a/image_analysis/geom_refinement/BeamCenterFromBackground.cpp +++ b/image_analysis/geom_refinement/BeamCenterFromBackground.cpp @@ -5,8 +5,10 @@ #include #include +#include #include "../../common/JFJochMath.h" +#include "../../common/ParallelFor.h" namespace { @@ -36,6 +38,14 @@ constexpr int CLIP_ROUNDS = 2; constexpr float DAMPING = 0.5f; constexpr float CONVERGED_PXL = 0.02f; +// The step is compared with the one before it, and a walk whose step reverses twice running has +// crossed its fixed point rather than walked toward it: with DAMPING = 0.5 that happens where the +// local gain exceeds 2, which is a period-2 limit cycle the walk will not leave. Instrumented, a +// sweep that spent 96 of its 100 iterations in one oscillated by 0.08 px about a centre it knew to +// 1.7 px. Travel does not reverse - a fit walking 339 px kept its step direction through all 22 of +// its travelling iterations - so this ends the dithering without shortening the budget below. +constexpr int REVERSALS_AT_THE_FIXED_POINT = 2; + // A travel budget, not a convergence criterion. The shift a sector's regression can report is // bounded by the width of the features it reads - a sector whose profile has moved a long way is // not g + d*g' for any d, and the least-squares projection onto g' returns far less than the true @@ -43,9 +53,15 @@ constexpr float CONVERGED_PXL = 0.02f; // however far it still has to go. Ten iterations therefore cap the fit at about two hundred // pixels of travel and a centre further out than that is left part way there, still walking, with // the per-iteration precision reported as though it had arrived. The count is set to cross a -// detector instead; a centre that is already close still leaves on CONVERGED_PXL after a handful. +// detector instead; a centre that is already close leaves on CONVERGED_PXL, or on the reversal +// test above, after a handful. constexpr int MAX_ITERATIONS = 100; +// The two passes over the pixels are split into this many row blocks, each accumulating into cells +// of its own, and the blocks are folded in block order. The split is a property of the image and +// not of the machine, so the sums are grouped the same way however many threads ran them. +constexpr int BLOCKS = 64; + // Below these the fit has not seen enough of the detector to be believed at all. constexpr int MIN_USABLE_SECTORS = SECTORS * 3 / 5; constexpr int MIN_USABLE_RADIAL_BINS = 15; @@ -60,7 +76,9 @@ float median_of(std::vector &v) { std::optional FindBeamCenterFromBackground(const DiffractionExperiment &experiment, const PixelMask &mask, - const std::vector &mean) { + const std::vector &mean, size_t nthreads) { + if (nthreads == 0) + nthreads = std::max(1u, std::thread::hardware_concurrency()); const auto W = static_cast(experiment.GetXPixelsNumConv()); const auto H = static_cast(experiment.GetYPixelsNumConv()); const size_t n_pixels = static_cast(W) * H; @@ -92,50 +110,79 @@ FindBeamCenterFromBackground(const DiffractionExperiment &experiment, const Pixe std::vector profile(RADIAL_BINS), d_profile(RADIAL_BINS), clip_limit(n_cells); std::vector radial_ok(RADIAL_BINS); + // One set of cells per block, allocated once and reused by every iteration. + std::vector block_row(BLOCKS + 1); + for (int b = 0; b <= BLOCKS; b++) + block_row[b] = static_cast(static_cast(b) * H / BLOCKS); + std::vector block_sum(static_cast(BLOCKS) * n_cells); + std::vector block_sum_sq(static_cast(BLOCKS) * n_cells); + std::vector block_jx(static_cast(BLOCKS) * n_cells); + std::vector block_jy(static_cast(BLOCKS) * n_cells); + std::vector block_count(static_cast(BLOCKS) * n_cells); + float step_x = 0.0f, step_y = 0.0f, sigma_x = 0.0f, sigma_y = 0.0f; + float previous_x = 0.0f, previous_y = 0.0f; + int reversals = 0; for (int iteration = 0; iteration < MAX_ITERATIONS; iteration++) { - std::fill(sum.begin(), sum.end(), 0.0); - std::fill(sum_sq.begin(), sum_sq.end(), 0.0); - std::fill(sum_jx.begin(), sum_jx.end(), 0.0); - std::fill(sum_jy.begin(), sum_jy.end(), 0.0); - std::fill(count.begin(), count.end(), 0); + ParallelFor(BLOCKS, nthreads, [&](int b) { + double *b_sum = block_sum.data() + static_cast(b) * n_cells; + double *b_sum_sq = block_sum_sq.data() + static_cast(b) * n_cells; + double *b_jx = block_jx.data() + static_cast(b) * n_cells; + double *b_jy = block_jy.data() + static_cast(b) * n_cells; + int32_t *b_count = block_count.data() + static_cast(b) * n_cells; + std::fill(b_sum, b_sum + n_cells, 0.0); + std::fill(b_sum_sq, b_sum_sq + n_cells, 0.0); + std::fill(b_jx, b_jx + n_cells, 0.0); + std::fill(b_jy, b_jy + n_cells, 0.0); + std::fill(b_count, b_count + n_cells, 0); + for (int y = block_row[b]; y < block_row[b + 1]; y++) { + for (int x = 0; x < W; x++) { + const size_t i = static_cast(y) * W + x; + cell_of[i] = -1; + if (pixel_mask[i] != 0 || !std::isfinite(mean[i])) + continue; + const float u = (x - beam_x) * pixel_size; + const float v = (y - beam_y) * pixel_size; + const float lx = rot[0] * u + rot[1] * v + rot[2] * distance; + const float ly = rot[3] * u + rot[4] * v + rot[5] * distance; + const float lz = rot[6] * u + rot[7] * v + rot[8] * distance; + const float rho = std::sqrt(lx * lx + ly * ly); + const float two_theta = std::atan2(rho, lz); + if (two_theta < tt_lo || two_theta >= tt_hi || rho == 0.0f) + continue; - for (int y = 0; y < H; y++) { - for (int x = 0; x < W; x++) { - const size_t i = static_cast(y) * W + x; - cell_of[i] = -1; - if (pixel_mask[i] != 0 || !std::isfinite(mean[i])) - continue; - const float u = (x - beam_x) * pixel_size; - const float v = (y - beam_y) * pixel_size; - const float lx = rot[0] * u + rot[1] * v + rot[2] * distance; - const float ly = rot[3] * u + rot[4] * v + rot[5] * distance; - const float lz = rot[6] * u + rot[7] * v + rot[8] * distance; - const float rho = std::sqrt(lx * lx + ly * ly); - const float two_theta = std::atan2(rho, lz); - if (two_theta < tt_lo || two_theta >= tt_hi || rho == 0.0f) - continue; + const float phi = std::atan2(ly, lx); + // Both bins are clamped: a pixel one float ulp below the top of the band divides + // to exactly RADIAL_BINS, which is one cell past the end of every accumulator. + const int r_bin = std::clamp(static_cast((two_theta - tt_lo) / d_tt), 0, RADIAL_BINS - 1); + const int s_bin = std::clamp(static_cast((phi + PI) / (2 * PI) * SECTORS), 0, SECTORS - 1); + const int cell = r_bin * SECTORS + s_bin; - const float phi = std::atan2(ly, lx); - // Both bins are clamped: a pixel one float ulp below the top of the band divides - // to exactly RADIAL_BINS, which is one cell past the end of every accumulator. - const int r_bin = std::clamp(static_cast((two_theta - tt_lo) / d_tt), 0, RADIAL_BINS - 1); - const int s_bin = std::clamp(static_cast((phi + PI) / (2 * PI) * SECTORS), 0, SECTORS - 1); - const int cell = r_bin * SECTORS + s_bin; - - // d(2theta)/d(beam), through the lab coordinate: the detector coordinate depends - // on the centre only as (x - beam_x), so moving the centre is moving the pixel. - const float denominator = rho * rho + lz * lz; - const float g_x = lz * lx / (rho * denominator); - const float g_y = lz * ly / (rho * denominator); - const float g_z = -rho / denominator; - cell_of[i] = cell; - count[cell]++; - sum[cell] += mean[i]; - sum_sq[cell] += static_cast(mean[i]) * mean[i]; - sum_jx[cell] += -pixel_size * (g_x * rot[0] + g_y * rot[3] + g_z * rot[6]); - sum_jy[cell] += -pixel_size * (g_x * rot[1] + g_y * rot[4] + g_z * rot[7]); + // d(2theta)/d(beam), through the lab coordinate: the detector coordinate depends + // on the centre only as (x - beam_x), so moving the centre is moving the pixel. + const float denominator = rho * rho + lz * lz; + const float g_x = lz * lx / (rho * denominator); + const float g_y = lz * ly / (rho * denominator); + const float g_z = -rho / denominator; + cell_of[i] = cell; + b_count[cell]++; + b_sum[cell] += mean[i]; + b_sum_sq[cell] += static_cast(mean[i]) * mean[i]; + b_jx[cell] += -pixel_size * (g_x * rot[0] + g_y * rot[3] + g_z * rot[6]); + b_jy[cell] += -pixel_size * (g_x * rot[1] + g_y * rot[4] + g_z * rot[7]); + } } + }); + for (int c = 0; c < n_cells; c++) { + double s = 0, ss = 0, jx = 0, jy = 0; + int32_t n = 0; + for (int b = 0; b < BLOCKS; b++) { + const size_t k = static_cast(b) * n_cells + c; + s += block_sum[k]; ss += block_sum_sq[k]; + jx += block_jx[k]; jy += block_jy[k]; + n += block_count[k]; + } + sum[c] = s; sum_sq[c] = ss; sum_jx[c] = jx; sum_jy[c] = jy; count[c] = n; } count_all = count; // the Jacobian sums belong to the unclipped pixel set @@ -147,16 +194,32 @@ FindBeamCenterFromBackground(const DiffractionExperiment &experiment, const Pixe const double variance = std::max(sum_sq[c] / count[c] - m * m, 0.0); clip_limit[c] = static_cast(m + CLIP_SIGMA * std::sqrt(variance)); } - std::fill(sum.begin(), sum.end(), 0.0); - std::fill(sum_sq.begin(), sum_sq.end(), 0.0); - std::fill(count.begin(), count.end(), 0); - for (size_t i = 0; i < n_pixels; i++) { - const int32_t c = cell_of[i]; - if (c < 0 || clip_limit[c] < 0.0f || mean[i] > clip_limit[c]) - continue; - count[c]++; - sum[c] += mean[i]; - sum_sq[c] += static_cast(mean[i]) * mean[i]; + ParallelFor(BLOCKS, nthreads, [&](int b) { + double *b_sum = block_sum.data() + static_cast(b) * n_cells; + double *b_sum_sq = block_sum_sq.data() + static_cast(b) * n_cells; + int32_t *b_count = block_count.data() + static_cast(b) * n_cells; + std::fill(b_sum, b_sum + n_cells, 0.0); + std::fill(b_sum_sq, b_sum_sq + n_cells, 0.0); + std::fill(b_count, b_count + n_cells, 0); + const size_t lo = static_cast(block_row[b]) * W; + const size_t hi = static_cast(block_row[b + 1]) * W; + for (size_t i = lo; i < hi; i++) { + const int32_t c = cell_of[i]; + if (c < 0 || clip_limit[c] < 0.0f || mean[i] > clip_limit[c]) + continue; + b_count[c]++; + b_sum[c] += mean[i]; + b_sum_sq[c] += static_cast(mean[i]) * mean[i]; + } + }); + for (int c = 0; c < n_cells; c++) { + double s = 0, ss = 0; + int32_t n = 0; + for (int b = 0; b < BLOCKS; b++) { + const size_t k = static_cast(b) * n_cells + c; + s += block_sum[k]; ss += block_sum_sq[k]; n += block_count[k]; + } + sum[c] = s; sum_sq[c] = ss; count[c] = n; } } @@ -269,17 +332,22 @@ FindBeamCenterFromBackground(const DiffractionExperiment &experiment, const Pixe sigma_x = static_cast(std::sqrt(c22 / det * chi2)); sigma_y = static_cast(std::sqrt(c11 / det * chi2)); + reversals = (step_x * previous_x + step_y * previous_y < 0.0f) ? reversals + 1 : 0; + previous_x = step_x; + previous_y = step_y; + beam_x += DAMPING * step_x; beam_y += DAMPING * step_y; - if (std::hypot(step_x, step_y) < CONVERGED_PXL) + if (std::hypot(step_x, step_y) < CONVERGED_PXL || reversals >= REVERSALS_AT_THE_FIXED_POINT) break; } // A fit that leaves on the iteration cap has not converged - it was still walking when it ran // out - and the precision of its last step is not what it knows the centre to. The step it // still wanted to take is a floor under what is left, so report that instead: it turns a - // confidently wrong answer into one the caller's sigma gate refuses. A converged fit stops on - // CONVERGED_PXL, well under any sigma worth reporting, so this never touches it. + // confidently wrong answer into one the caller's sigma gate refuses. A fit that stopped on + // CONVERGED_PXL or at its fixed point takes a step far under any sigma worth reporting, so + // this never touches those. return BeamCenterEstimate{beam_x, beam_y, std::max({sigma_x, sigma_y, std::hypot(step_x, step_y)})}; } diff --git a/image_analysis/geom_refinement/BeamCenterFromBackground.h b/image_analysis/geom_refinement/BeamCenterFromBackground.h index 07ed43cb3..7d9451818 100644 --- a/image_analysis/geom_refinement/BeamCenterFromBackground.h +++ b/image_analysis/geom_refinement/BeamCenterFromBackground.h @@ -32,6 +32,8 @@ struct BeamCenterEstimate { // follows the noise in g' instead, and it does so confidently. // // `mean` is a per-pixel projection over a few tens of frames, NAN where no frame contributed. +// nthreads = 0 asks for all hardware threads. The pixels are split into a fixed number of row +// blocks whatever that count is, so the answer does not depend on it. std::optional FindBeamCenterFromBackground(const DiffractionExperiment &experiment, const PixelMask &mask, - const std::vector &mean); + const std::vector &mean, size_t nthreads = 0); diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index 64a249ad8..e0b91a704 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -1141,7 +1141,7 @@ void Rugnux::PreScan(int start_image, int images_to_process, int frame_count, Ru // by then the centre has been post-refined and the file's value is no longer what is in it. if (want_shadow && config_.beam_center_check) { background_center_ = FindBeamCenterFromBackground(experiment_, pixel_mask_, - finder.GetMeanProjection()); + finder.GetMeanProjection(), config_.nthreads); measured_beam_center_ = background_center_; const float need = BeamCenterNeed_pxl(experiment_); if (!background_center_) { @@ -1309,7 +1309,8 @@ void Rugnux::PreScan(int start_image, int images_to_process, int frame_count, Ru // same geometry, same projection - so this asks for it again only where it was not run. estimate = background_center_ ? background_center_ - : FindBeamCenterFromBackground(experiment_, pixel_mask_, finder.GetMeanProjection()); + : FindBeamCenterFromBackground(experiment_, pixel_mask_, + finder.GetMeanProjection(), config_.nthreads); } if (!estimate) { logger.Info("Beam centre: not measurable, keeping ({:.2f},{:.2f})", diff --git a/tests/BeamCenterFromBackgroundTest.cpp b/tests/BeamCenterFromBackgroundTest.cpp index 47d338529..55129a853 100644 --- a/tests/BeamCenterFromBackgroundTest.cpp +++ b/tests/BeamCenterFromBackgroundTest.cpp @@ -164,3 +164,21 @@ TEST_CASE("BeamCenterFromBackground_RecoversACentreFarFromTheDetectorMiddle", "[ CHECK(estimate->beam_x_pxl == Catch::Approx(geom_true.GetBeamX_pxl()).margin(2.0)); CHECK(estimate->beam_y_pxl == Catch::Approx(geom_true.GetBeamY_pxl()).margin(2.0)); } + +// The fit is accumulated over row blocks so that a machine with more cores gives the same answer as +// one with fewer: the blocks are a property of the image and the fold runs over them in order. +TEST_CASE("BeamCenterFromBackground_DoesNotDependOnTheThreadCount", "[BeamCenter]") { + DiffractionExperiment x = TestExperiment(); + PixelMask pixel_mask(x); + + const DiffractionGeometry geom_true = OffsetBy(x.GetDiffractionGeometry(), 3.0f, -2.5f); + const auto projection = SynthesiseProjection(x, pixel_mask, geom_true, 60.0f, 1.0f); + const auto one = FindBeamCenterFromBackground(x, pixel_mask, projection, 1); + const auto many = FindBeamCenterFromBackground(x, pixel_mask, projection, 8); + + REQUIRE(one.has_value()); + REQUIRE(many.has_value()); + CHECK(one->beam_x_pxl == many->beam_x_pxl); + CHECK(one->beam_y_pxl == many->beam_y_pxl); + CHECK(one->sigma_pxl == many->sigma_pxl); +}