Pre-scan on the GPU: background beam-centre walk and beam-stop mask
The two pre-scan steps that were still CPU-bound in a GPU build now run where the projection already is. - FindBeamCenterFromBackground: the per-iteration binning pass and the two clip rounds run on the device (BeamCenterBackgroundGPU); the fit itself stays on the host. Each cell is summed in the host's order (pixel order within the host's row blocks, blocks in order), and the per-pixel cell/derivative formula is shared (BackgroundBand.h). The angles come from BackgroundAtan2 (IEEE ops only) instead of atan2f, and both translation units are compiled without FMA contraction, so host and device give the same bits: 0 of 6.5 M pixels in a different cell, identical walks on the three in-house rotation sets. With glibc/CUDA atan2f and default contraction ~30 pixels per 16 Mpx sweep changed cell and the fitted centre moved by up to 0.05 px. - ShadowFinder::GetMask: the whole mask (pooling, ring medians, components, morphology, hole fill, arm search) runs on the device from ShadowAccumulatorGPU's projection (ShadowMaskGPU), so the 360 MB projection no longer comes back; the mean projection is divided on the device too (same bits). The two small fits over rings and sectors (BlockedOutTo, HarmonicFit) are shared with the host path in ShadowFinderInternal.h. Integers, comparisons, sorts and components are exact; the polarization trig, the Poisson log and the arm-search azimuth are not, so a pixel at a threshold can differ. The one-time change against the previous CPU arithmetic (BackgroundAtan2, no contraction), measured on the myoglobin, cytochrome C and thaumatin rotation sets: ring centre moves 0.002-0.045 px (fit sigma 0.75-1.2 px), beam-centre capture 0.01-0.04 px; beam-stop mask differs on 31 / 144 / 53 pixels of 259k / 144k / 198k (25 of the myoglobin ones are GPU-vs-CPU arithmetic in the mask, the rest follow the centre); hot-pixel mask identical. Spot width, integration radii, bandwidth, beam-centre arbitration, indexing, space group, cell, resolution and the merged statistics table are identical; only the error model moves in its 4th digit. CPU build: the same centres and decisions. Timing (GPU, box at load 30-38): ring walk 0.54 -> 0.23-0.27 s, mask 1.24-1.44 -> 0.18-0.22 s, beam-centre capture walk 1.1-1.3 -> 0.31-0.35 s. Tests: ShadowFinder_DeviceMaskMatchesHost, BeamCenterFromBackground_DeviceMatchesHost (bit-exact), plus [ShadowFinder], [BeamCenter], [HotPixelFinder]. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K5K8jvPPbmCrbqnWkddTuB
This commit is contained in:
1 parent
24e36ae740
commit
f849e2d1be
15 files changed
+1468
-175
No files matched your search
@@ -471,3 +471,91 @@ TEST_CASE("ShadowFinder_ABrightRingIsNotABeamStop", "[ShadowFinder]") {
|
||||
// Anything much beyond the stop, its arm and their penumbra means the walk ran away.
|
||||
CHECK(std::count(mask.begin(), mask.end(), 1u) < 6000);
|
||||
}
|
||||
|
||||
#ifdef JFJOCH_USE_CUDA
|
||||
#include "../common/CUDAWrapper.h"
|
||||
#include "../compression/JFJochCompressor.h"
|
||||
|
||||
namespace {
|
||||
// The mask and the mean projection of the same frames, once from the host projection (the frames
|
||||
// handed over uncompressed) and once from the device's (handed over as bitshuffle+LZ4, which is
|
||||
// what sends them to the GPU).
|
||||
struct HostAndDevice {
|
||||
std::vector<uint32_t> host_mask, device_mask;
|
||||
std::vector<float> host_mean, device_mean;
|
||||
};
|
||||
|
||||
HostAndDevice MaskBothWays(const DiffractionExperiment &x, const std::vector<std::vector<int32_t>> &frames) {
|
||||
const PixelMask pixel_mask(x);
|
||||
HostAndDevice out;
|
||||
std::vector<uint8_t> buffer;
|
||||
{
|
||||
ShadowFinder finder(x, pixel_mask);
|
||||
for (const auto &frame : frames) {
|
||||
DataMessage msg{};
|
||||
msg.image = CompressedImage(frame, W, H);
|
||||
finder.AddImage(msg, buffer);
|
||||
}
|
||||
out.host_mask = finder.GetMask();
|
||||
out.host_mean = finder.GetMeanProjection();
|
||||
}
|
||||
{
|
||||
ShadowFinder finder(x, pixel_mask);
|
||||
JFJochBitShuffleCompressor compressor(CompressionAlgorithm::BSHUF_LZ4);
|
||||
std::vector<std::vector<uint8_t>> compressed;
|
||||
for (const auto &frame : frames) {
|
||||
compressed.push_back(compressor.Compress(frame));
|
||||
DataMessage msg{};
|
||||
msg.image = CompressedImage(compressed.back().data(), compressed.back().size(), W, H,
|
||||
CompressedImageMode::Int32, CompressionAlgorithm::BSHUF_LZ4);
|
||||
finder.AddImage(msg, buffer);
|
||||
}
|
||||
out.device_mask = finder.GetMask();
|
||||
out.device_mean = finder.GetMeanProjection();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
// The mask is made on the GPU wherever the projection is there. On scenes where no pixel sits within a
|
||||
// rounding of a threshold the two must agree to the pixel: every step but the polarization factor,
|
||||
// the Poisson test's logarithm and the arm search's azimuth is exact on both, and these scenes are
|
||||
// built so that none of the three decides anything at an edge.
|
||||
TEST_CASE("ShadowFinder_DeviceMaskMatchesHost", "[ShadowFinder]") {
|
||||
if (get_gpu_count() == 0)
|
||||
SKIP("no GPU");
|
||||
|
||||
SECTION("a beam stop with a reflection behind it") {
|
||||
std::vector<std::vector<int32_t>> frames;
|
||||
for (int f = 0; f < NFRAMES; f++)
|
||||
frames.push_back(Scene(false, f == 0));
|
||||
const auto r = MaskBothWays(TestExperiment(), frames);
|
||||
CHECK(std::count(r.host_mask.begin(), r.host_mask.end(), 1u) == 2612);
|
||||
CHECK(r.device_mask == r.host_mask);
|
||||
CHECK(std::memcmp(r.device_mean.data(), r.host_mean.data(), r.host_mean.size() * sizeof(float)) == 0);
|
||||
}
|
||||
|
||||
SECTION("an arm that lets part of the beam through, across a module gap") {
|
||||
constexpr int32_t BRIGHT = 50;
|
||||
constexpr int ARM_HALF_WIDE = 15, GAP_X0 = 200, GAP_X1 = 216, OPAQUE_FROM_X = 232;
|
||||
std::vector<std::vector<int32_t>> frames;
|
||||
for (int f = 0; f < NFRAMES; f++) {
|
||||
frames.emplace_back(static_cast<size_t>(W) * H, BRIGHT);
|
||||
auto &frame = frames.back();
|
||||
for (int y = 0; y < H; y++)
|
||||
for (int xi = 0; xi < W; xi++) {
|
||||
const int dx = xi - C, dy = y - C;
|
||||
if (dx * dx + dy * dy <= STOP_R * STOP_R)
|
||||
frame[I(xi, y)] = 0;
|
||||
else if (dx >= 0 && std::abs(dy) <= ARM_HALF_WIDE)
|
||||
frame[I(xi, y)] = xi >= OPAQUE_FROM_X ? 0 : BRIGHT * 6 / 10;
|
||||
if (xi >= GAP_X0 && xi <= GAP_X1)
|
||||
frame[I(xi, y)] = INT32_MIN;
|
||||
}
|
||||
}
|
||||
const auto r = MaskBothWays(TestExperiment(), frames);
|
||||
CHECK(std::count(r.host_mask.begin(), r.host_mask.end(), ShadowFinder::TRANSMITTING) > 0);
|
||||
CHECK(r.device_mask == r.host_mask);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user