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
113 lines
4.7 KiB
C++
113 lines
4.7 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <bit>
|
|
|
|
#include "../../common/JFJochException.h"
|
|
#include "ImageSpotFinder.h"
|
|
#include "StrongPixelSet.h"
|
|
|
|
ImageSpotFinder::ImageSpotFinder(int32_t width, int32_t height, bool host_bit_buffer)
|
|
: width(width),
|
|
height(height),
|
|
output_buffer(host_bit_buffer ? width * height / 32 + 1 : 0),
|
|
res_mask_bits(OutputSize(), 0) {
|
|
// Exclude the padding bits of the last word up front, so neither the host scan nor the GPU
|
|
// compaction needs a separate "is this bit still inside the image?" test.
|
|
const size_t npixel = static_cast<size_t>(width) * height;
|
|
if (npixel % 32 != 0)
|
|
res_mask_bits.back() = ~((1u << (npixel % 32)) - 1u);
|
|
}
|
|
|
|
size_t ImageSpotFinder::OutputSize() const {
|
|
return (width * height) / 32 + ((width * height % 32 != 0) ? 1 : 0);
|
|
}
|
|
|
|
size_t ImageSpotFinder::OutputByteSize() const {
|
|
return OutputSize() * sizeof(uint32_t);
|
|
}
|
|
|
|
void ImageSpotFinder::SetResolutionMask(const std::vector<bool> &mask) {
|
|
const size_t npixel = static_cast<size_t>(width) * height;
|
|
if (mask.size() != npixel)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"ImageSpotFinder::SetResolutionMask: mask size mismatch");
|
|
std::vector<uint32_t> packed(OutputSize(), 0);
|
|
for (size_t i = 0; i < npixel; i++)
|
|
if (mask[i])
|
|
packed[i / 32] |= 1u << (i % 32);
|
|
SetResolutionMaskBits(packed);
|
|
}
|
|
|
|
void ImageSpotFinder::SetResolutionMaskBits(const std::vector<uint32_t> &packed_mask) {
|
|
if (packed_mask.size() != OutputSize())
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"ImageSpotFinder::SetResolutionMaskBits: mask size mismatch");
|
|
res_mask_bits = packed_mask;
|
|
const size_t npixel = static_cast<size_t>(width) * height;
|
|
if (npixel % 32 != 0)
|
|
res_mask_bits.back() |= ~((1u << (npixel % 32)) - 1u);
|
|
}
|
|
|
|
const std::vector<float> &ImageSpotFinder::GetRingBackground() const {
|
|
static const std::vector<float> none;
|
|
return none;
|
|
}
|
|
|
|
void ImageSpotFinder::ExtractComponentsHost(const ImagePreprocessorBuffer &image,
|
|
const SpotFindingSettings &settings) {
|
|
// Collect the strong pixels first and read their values afterwards, instead of reading the image
|
|
// pixel by pixel: on the GPU that read is a device gather, which is what lets the preprocessed
|
|
// image stay on the device instead of being copied back in full for every frame.
|
|
strong_pixel.clear();
|
|
for (size_t i = 0; i < OutputSize(); i++) {
|
|
// The resolution mask is packed like the bit buffer, so a whole word of it is excluded at
|
|
// once instead of testing 32 bits one at a time.
|
|
uint32_t word = output_buffer[i] & ~res_mask_bits[i];
|
|
while (word != 0) {
|
|
strong_pixel.push_back(static_cast<uint32_t>(i * 32 + std::countr_zero(word)));
|
|
word &= word - 1;
|
|
}
|
|
}
|
|
|
|
components.clear();
|
|
// The connected-component search rejects a frame with this many strong pixels, so their values are
|
|
// of no use.
|
|
if (strong_pixel.size() >= UINT16_MAX)
|
|
return;
|
|
|
|
image.Gather(strong_pixel, strong_pixel_value);
|
|
|
|
StrongPixelSet pixel_set;
|
|
for (size_t i = 0; i < strong_pixel.size(); i++)
|
|
pixel_set.AddStrongPixel(strong_pixel[i] % width, strong_pixel[i] / width, strong_pixel_value[i]);
|
|
pixel_set.FindComponentsImage(settings, components);
|
|
}
|
|
|
|
const std::vector<DiffractionSpot> &ImageSpotFinder::ExtractComponents(const ImagePreprocessorBuffer &image,
|
|
const SpotFindingSettings &settings) {
|
|
ExtractComponentsHost(image, settings);
|
|
return components;
|
|
}
|
|
|
|
std::vector<DiffractionSpot> ImageSpotFinder::Filter(const std::vector<DiffractionSpot> &in,
|
|
const SpotFindingSettings &settings) {
|
|
std::vector<DiffractionSpot> out;
|
|
const int64_t min_pix = settings.min_pix_per_spot.value_or(2);
|
|
for (const auto &spot: in)
|
|
if (spot.PixelCount() >= min_pix)
|
|
out.push_back(spot);
|
|
return out;
|
|
}
|
|
|
|
std::vector<DiffractionSpot> ImageSpotFinder::ExtractSpots(const ImagePreprocessorBuffer &image,
|
|
const SpotFindingSettings &settings) {
|
|
return Filter(ExtractComponents(image, settings), settings);
|
|
}
|
|
|
|
std::vector<DiffractionSpot> ImageSpotFinder::Run(const ImagePreprocessorBuffer &image,
|
|
const SpotFindingSettings &settings) {
|
|
Detect(image, settings);
|
|
return ExtractSpots(image, settings);
|
|
}
|