Files
Jungfraujoch/image_analysis/image_preprocessing/ImagePreprocessorBufferGPU.cu
T
leonarski_fandClaude Opus 5 ac202a55a1 Spot finding: the strong-pixel limit follows the detector
An image with 65535 or more strong pixels was given up on and reported ZERO
spots - silently, no log line, indistinguishable from a frame that did not
diffract. 65535 is one pixel in 64 of the JUNGFRAU 4M the number was written
for; left fixed while the detectors grew it became one in 276 of an
18-megapixel EIGER, which a strongly diffracting crystal passes on its best
frames. On the strong rotation set just added to the battery it cost 767 of
1800 images: peakCountUnfiltered 0 and resolutionEstimate NaN across two
blocks of the sweep, the two where the crystal diffracts hardest. Make the bar
one pixel in 64 everywhere, and never below the value that stood here, so no
smaller detector loses ground. It lived in three places - the host extractor,
StrongPixelSet, and SpotExtractorGPU's buffer capacity - now one function.

The bar was there for a reason and raising it alone would not have been safe.
sparseccl walks a sliding window of the last two lines and tests every pixel in
it, which is quadratic in how many strong pixels a line pair holds: a handful
for the silicon-tracker hits upstream wrote it for, four thousand for a lit
detector line, and 76 seconds for a fully lit frame. But the pixels arrive in
raster order, so the window need not be walked at all - a pixel's earlier
8-neighbours are the one to its left and the at most three above it, which is
what the GPU extractor already finds by binary search. Keeping the previous
line's range and a forward-only cursor gives the same edge set and the same
unions in the same order, so the labels are identical, and the fully lit frame
now takes 0.16 s. Verified bit-identical on real frames, on fully dense frames,
across occupancy 1e-5 to 5e-2, and on 4000 randomised images including ones
with blank lines; SpotExtractorGPU's host-vs-device parity test passes
untouched.

ImagePreprocessorBufferGPU's gather staging was sized to the old constant, with
a comment tying it to the caller's give-up. Raising that give-up without it
would have run the gather off the end of the device buffer, so it follows the
same limit now.

Byte-identical .hkl on three battery crystals that never reach the bar. On the
strong set, with symmetry, cell and geometry pinned so only the spot list
moves: <I/sigma> better in every resolution shell, CC1/2 97.8 -> 98.5%,
R_meas 30.5 -> 28.4%, ISa 3.36 -> 3.58, indexing rate 0.772 -> 0.824.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FBumeJVx4oeXxiBRpkrE5H
2026-08-28 10:38:10 +02:00

46 lines
1.8 KiB
Plaintext

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "ImagePreprocessorBufferGPU.h"
__global__ void gather_kernel(const int32_t *__restrict__ image,
const uint32_t *__restrict__ npixel,
int32_t *__restrict__ values,
int count) {
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < count; i += blockDim.x * gridDim.x)
values[i] = image[npixel[i]];
}
ImagePreprocessorBufferGPU::ImagePreprocessorBufferGPU(size_t npixel, bool host_mirror)
: ImagePreprocessorBuffer(npixel, host_mirror),
gpu_image(npixel),
// A no-op when the mirror was not allocated: CudaRegisteredVector skips an empty vector.
buffer_reg(buffer),
max_gather(StrongPixelLimit(npixel)),
gpu_gather_index(max_gather),
gpu_gather_value(max_gather) {
}
int32_t *ImagePreprocessorBufferGPU::getGPUBuffer() {
return gpu_image;
}
const int32_t *ImagePreprocessorBufferGPU::getGPUBuffer() const {
return gpu_image;
}
void ImagePreprocessorBufferGPU::Gather(const std::vector<uint32_t> &npixel, std::vector<int32_t> &values) const {
values.resize(npixel.size());
if (npixel.empty())
return;
const int count = static_cast<int>(npixel.size());
cudaMemcpyAsync(gpu_gather_index.get(), npixel.data(), count * sizeof(uint32_t),
cudaMemcpyHostToDevice, gather_stream);
gather_kernel<<<(count + 255) / 256, 256, 0, gather_stream>>>(
gpu_image.get(), gpu_gather_index.get(), gpu_gather_value.get(), count);
cudaMemcpyAsync(values.data(), gpu_gather_value.get(), count * sizeof(int32_t),
cudaMemcpyDeviceToHost, gather_stream);
cudaStreamSynchronize(gather_stream);
}