Files
Jungfraujoch/tests/ImageBufferTest.cpp
leonarski_f aecd48b9d3
Build Packages / Build documentation (push) Successful in 48s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 15m47s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 23m19s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 18m47s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m27s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 26m59s
Build Packages / Unit tests (push) Successful in 1h50m25s
Build Packages / Validation Battery (push) Successful in 46m38s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 32m52s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 32m32s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 28m34s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 29m19s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 30m11s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 33m58s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 31m26s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 34m4s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 32m17s
Build Packages / Generate python client (push) Successful in 32s
Build Packages / Create release (push) Successful in 9s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 31m50s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 19m38s
v1.0.0-rc.186 (#97)
* jfjoch_broker (experimental): Live Rugnux covers serial collections from DECTRIS detectors; a rotation collection too large for the image buffer falls back to standard per-image processing with a warning, and a stream that ends without its end message finishes the collection instead of hanging it.
* jfjoch_broker (experimental): Live Rugnux settings to build the solvent during refinement and to process a rotation collection as stills, a Clear action for the last result, a `_report.txt` for live rotation runs, R-free reported for the refined model, and ice-ring detection on by default for rotation as in the command line.
* jfjoch_broker (experimental): More complete and reproducible live serial merges, analysed for twinning, pseudo-translation and anisotropy, with merging statistics by resolution shell in the live status.
* jfjoch_writer: Live results with large models are written reliably; update jfjoch_writer together with jfjoch_broker.
* Rugnux: Several masters can be processed together with `--force-still`, including grid scans; more robust and faster merging of several sweeps; improved twinning, pseudo-translation, anisotropy, anomalous-signal and space-group decisions.
* Rugnux: Molecular replacement places the copies of a multi-copy solution together; the pseudo-translation warning quotes the Patterson peak and is not raised once a model is placed; the report advises on extending the rotation or reorienting the crystal where the coverage would gain from it.
* Web frontend: Light and dark modes, a phone layout, a hideable page list, a redesigned Rugnux live page with step-through of the Fo-Fc peaks in the structure view.
* jfjoch_viewer: A broker URL given on the command line follows the running collection.

Reviewed-on: #97
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-13 20:13:43 +01:00

409 lines
13 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include "../common/ImageBuffer.h"
#include "../common/ZeroCopyReturnValue.h"
TEST_CASE("ImageBuffer_Location size") {
ImageBuffer buf(2*1024*1024);
REQUIRE_THROWS(buf.StartMeasurement(4 * 1024 * 1024));
REQUIRE_NOTHROW(buf.StartMeasurement(1024 * 1024));
REQUIRE(buf.GetStatus().total_slots == 2);
REQUIRE(buf.GetStatus().available_slots == 2);
}
TEST_CASE("ImageBuffer") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4*experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
REQUIRE(buf_ctrl.GetAvailSlots() == 4);
ZeroCopyReturnValue *ret0 = buf_ctrl.GetImageSlot();
REQUIRE(ret0 != nullptr);
REQUIRE(buf_ctrl.GetAvailSlots() == 3);
ZeroCopyReturnValue *ret1 = buf_ctrl.GetImageSlot();
REQUIRE(ret1 != nullptr);
ZeroCopyReturnValue *ret2 = buf_ctrl.GetImageSlot();
REQUIRE(ret2 != nullptr);
ZeroCopyReturnValue *ret3 = buf_ctrl.GetImageSlot();
REQUIRE(ret3 != nullptr);
REQUIRE(buf_ctrl.GetAvailSlots() == 0);
ZeroCopyReturnValue *ret4 = buf_ctrl.GetImageSlot();
REQUIRE(ret4 == nullptr);
REQUIRE(buf_ctrl.GetStatus().preparation_slots == 4);
REQUIRE(buf_ctrl.GetStatus().sending_slots == 0);
REQUIRE(buf_ctrl.GetStatus().available_slots == 0);
ret0->ReadyToSend();
ret1->ReadyToSend();
REQUIRE(buf_ctrl.GetStatus().preparation_slots == 2);
REQUIRE(buf_ctrl.GetStatus().sending_slots == 2);
REQUIRE(buf_ctrl.GetStatus().available_slots == 0);
REQUIRE(buf_ctrl.GetSlotStatus().total_slots == 4);
REQUIRE(buf_ctrl.GetSlotStatus().preparation_slots == 2);
REQUIRE(buf_ctrl.GetSlotStatus().sending_slots == 2);
REQUIRE(buf_ctrl.GetSlotStatus().available_slots == 0);
ret2->ReadyToSend();
ret3->ReadyToSend();
REQUIRE(buf_ctrl.GetStatus().preparation_slots == 0);
REQUIRE(buf_ctrl.GetStatus().sending_slots == 4);
REQUIRE(buf_ctrl.GetStatus().available_slots == 0);
ret0->release();
ret1->release();
ret2->release();
ret3->release();
REQUIRE(buf_ctrl.GetStatus().available_slots == 4);
REQUIRE(buf_ctrl.GetAvailSlots() == 4);
REQUIRE(buf_ctrl.CheckIfBufferReturned(std::chrono::microseconds(1)));
}
TEST_CASE("ImageBuffer_Preview_Access") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4*experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
REQUIRE(buf_ctrl.GetAvailSlots() == 4);
ZeroCopyReturnValue *ret0 = buf_ctrl.GetImageSlot();
REQUIRE(ret0 != nullptr);
ret0->SetImageNumber(0);
ret0->release();
ZeroCopyReturnValue *ret1 = buf_ctrl.GetImageSlot();
REQUIRE(ret1 != nullptr);
ret1->SetImageNumber(1);
ret1->release();
ZeroCopyReturnValue *ret2 = buf_ctrl.GetImageSlot();
REQUIRE(ret2 != nullptr);
ret2->SetImageNumber(2);
ret2->SetImageSize(3456);
ret2->SetIndexed(true);
ret2->release();
ZeroCopyReturnValue *ret3 = buf_ctrl.GetImageSlot();
REQUIRE(ret3 != nullptr);
ret3->SetImageNumber(15);
ret3->SetImageSize(1234);
ret3->SetIndexed(false);
ret3->release();
REQUIRE(buf_ctrl.GetAvailSlots() == 4);
auto v = buf_ctrl.GetStatus().images_in_the_buffer;
REQUIRE(v.size() == 4);
REQUIRE(v[0] == 0);
REQUIRE(v[3] == 15);
std::vector<uint8_t> image_2, image_2_2, image_12, image_max, image_max_indexed;
REQUIRE(buf_ctrl.GetImage(image_2, 2));
REQUIRE(image_2.size() == 3456);
REQUIRE(!buf_ctrl.GetImage(image_12, 12));
REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1)));
// After finalize was run, there should be no preview
REQUIRE(!buf_ctrl.GetImage(image_2_2, 2));
}
TEST_CASE("ImageBuffer_Counter") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4*experiment.GetImageBufferLocationSize());
auto cnt_1 = buf_ctrl.GetStatus().current_counter;
buf_ctrl.StartMeasurement(experiment);
auto cnt_2 = buf_ctrl.GetStatus().current_counter;
ZeroCopyReturnValue *ret0 = buf_ctrl.GetImageSlot();
REQUIRE(ret0 != nullptr);
ret0->SetImageNumber(0);
ret0->ReadyToSend();
auto cnt_3 = buf_ctrl.GetStatus().current_counter;
ZeroCopyReturnValue *ret1 = buf_ctrl.GetImageSlot();
REQUIRE(ret1 != nullptr);
ret1->SetImageNumber(1);
ret1->ReadyToSend();
auto cnt_4 = buf_ctrl.GetStatus().current_counter;
ret0->release();
ret1->release();
REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1)));
auto cnt_5 = buf_ctrl.GetStatus().current_counter;
CHECK(cnt_1 != cnt_2);
CHECK(cnt_1 != cnt_3);
CHECK(cnt_1 != cnt_4);
CHECK(cnt_1 != cnt_5);
CHECK(cnt_2 != cnt_3);
CHECK(cnt_2 != cnt_4);
CHECK(cnt_2 != cnt_5);
CHECK(cnt_3 != cnt_4);
CHECK(cnt_3 != cnt_5);
CHECK(cnt_4 != cnt_5);
}
TEST_CASE("ImageBuffer_Preview_Access_GetImage_MaxImage") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4*experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
REQUIRE(buf_ctrl.GetAvailSlots() == 4);
ZeroCopyReturnValue *ret0 = buf_ctrl.GetImageSlot();
REQUIRE(ret0 != nullptr);
ret0->SetImageNumber(0);
ret0->release();
ZeroCopyReturnValue *ret1 = buf_ctrl.GetImageSlot();
REQUIRE(ret1 != nullptr);
ret1->SetImageNumber(1);
ret1->release();
ZeroCopyReturnValue *ret2 = buf_ctrl.GetImageSlot();
REQUIRE(ret2 != nullptr);
ret2->SetImageNumber(2);
ret2->SetImageSize(3456);
ret2->SetIndexed(true);
ret2->release();
ZeroCopyReturnValue *ret3 = buf_ctrl.GetImageSlot();
REQUIRE(ret3 != nullptr);
ret3->SetImageNumber(15);
ret3->SetImageSize(1234);
ret3->SetIndexed(false);
ret3->release();
std::vector<uint8_t> image_max, image_max_indexed;
REQUIRE(buf_ctrl.GetImage(image_max, ImageBuffer::MaxImage));
REQUIRE(image_max.size() == 1234);
REQUIRE(buf_ctrl.GetImage(image_max_indexed, ImageBuffer::MaxIndexedImage));
REQUIRE(image_max_indexed.size() == 3456);
REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1)));
}
TEST_CASE("ImageBuffer_Restart") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4*experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
ZeroCopyReturnValue *ret0 = buf_ctrl.GetImageSlot();
REQUIRE(ret0 != nullptr);
ret0->SetImageNumber(0);
ret0->release();
REQUIRE(buf_ctrl.GetStatus().images_in_the_buffer.size() == 1);
// Restart measurement
REQUIRE_NOTHROW(buf_ctrl.StartMeasurement(experiment));
REQUIRE(buf_ctrl.GetAvailSlots() == 4);
REQUIRE(buf_ctrl.GetStatus().images_in_the_buffer.empty());
ret0 = buf_ctrl.GetImageSlot();
REQUIRE(ret0 != nullptr);
ret0->SetImageNumber(0);
ret0->release();
REQUIRE_NOTHROW(buf_ctrl.StartMeasurement(experiment));
}
TEST_CASE("ImageBuffer_BuffersNotReturned") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4*experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
REQUIRE(buf_ctrl.GetAvailSlots() == 4);
ZeroCopyReturnValue *ret0 = buf_ctrl.GetImageSlot();
REQUIRE(ret0 != nullptr);
REQUIRE(buf_ctrl.GetAvailSlots() == 3);
REQUIRE(!buf_ctrl.CheckIfBufferReturned(std::chrono::microseconds(1)));
}
TEST_CASE("ImageBuffer_SlotReuse") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
// Only 1 slot so reuse is guaranteed
ImageBuffer buf_ctrl(experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
REQUIRE(buf_ctrl.GetStatus().total_slots == 1);
REQUIRE(buf_ctrl.GetAvailSlots() == 1);
// First cycle: acquire -> ReadyToSend -> release
ZeroCopyReturnValue *ret = buf_ctrl.GetImageSlot();
REQUIRE(ret != nullptr);
REQUIRE(buf_ctrl.GetAvailSlots() == 0);
ret->SetImageNumber(0);
ret->SetImageSize(100);
ret->SetIndexed(false);
ret->ReadyToSend();
REQUIRE(buf_ctrl.GetStatus().sending_slots == 1);
REQUIRE(buf_ctrl.GetStatus().preparation_slots == 0);
ret->release();
REQUIRE(buf_ctrl.GetAvailSlots() == 1);
REQUIRE(buf_ctrl.GetStatus().sending_slots == 0);
// Second cycle: reuse the same slot — this is where the bug manifested
ZeroCopyReturnValue *ret2 = buf_ctrl.GetImageSlot();
REQUIRE(ret2 != nullptr);
REQUIRE(buf_ctrl.GetAvailSlots() == 0);
REQUIRE(buf_ctrl.GetStatus().preparation_slots == 1);
ret2->SetImageNumber(1);
ret2->SetImageSize(200);
ret2->SetIndexed(true);
REQUIRE_NOTHROW(ret2->ReadyToSend());
REQUIRE(buf_ctrl.GetStatus().sending_slots == 1);
REQUIRE(buf_ctrl.GetStatus().preparation_slots == 0);
REQUIRE_NOTHROW(ret2->release());
REQUIRE(buf_ctrl.GetAvailSlots() == 1);
REQUIRE(buf_ctrl.GetStatus().sending_slots == 0);
// Third cycle: verify it keeps working
ZeroCopyReturnValue *ret3 = buf_ctrl.GetImageSlot();
REQUIRE(ret3 != nullptr);
ret3->SetImageNumber(2);
ret3->SetImageSize(300);
ret3->SetIndexed(false);
REQUIRE_NOTHROW(ret3->ReadyToSend());
REQUIRE_NOTHROW(ret3->release());
REQUIRE(buf_ctrl.GetAvailSlots() == 1);
REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1)));
}
TEST_CASE("ImageBuffer_SlotReuse_ReleaseFromInPreparation") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
// First cycle: acquire -> release directly (skipping explicit ReadyToSend)
ZeroCopyReturnValue *ret = buf_ctrl.GetImageSlot();
REQUIRE(ret != nullptr);
ret->SetImageNumber(0);
ret->SetImageSize(100);
ret->release();
REQUIRE(buf_ctrl.GetAvailSlots() == 1);
// Second cycle: reuse after release-from-InPreparation path
ZeroCopyReturnValue *ret2 = buf_ctrl.GetImageSlot();
REQUIRE(ret2 != nullptr);
ret2->SetImageNumber(1);
ret2->SetImageSize(200);
REQUIRE_NOTHROW(ret2->ReadyToSend());
REQUIRE_NOTHROW(ret2->release());
REQUIRE(buf_ctrl.GetAvailSlots() == 1);
REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1)));
}
TEST_CASE("ImageBuffer_DiscardSlot") {
// A slot given back with DiscardSlot is found no more; the copy of the same image in another slot is.
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4 * experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
ZeroCopyReturnValue *held = buf_ctrl.GetImageSlot();
REQUIRE(held != nullptr);
memset(held->GetImage(), 1, 100);
held->SetImageNumber(5);
held->SetImageSize(100);
held->ReadyToSend();
std::vector<uint8_t> v;
REQUIRE(buf_ctrl.GetImage(v, 5));
CHECK(v[0] == 1);
ZeroCopyReturnValue *analysed = buf_ctrl.GetImageSlot();
REQUIRE(analysed != nullptr);
memset(analysed->GetImage(), 2, 200);
analysed->SetImageNumber(5);
analysed->SetImageSize(200);
analysed->ReadyToSend();
auto counter = buf_ctrl.GetStatus().current_counter;
held->discard();
CHECK(buf_ctrl.GetStatus().current_counter != counter);
CHECK(buf_ctrl.GetStatus().images_in_the_buffer == std::vector<int64_t>{5});
CHECK(buf_ctrl.GetStatus().sending_slots == 1);
REQUIRE(buf_ctrl.GetImage(v, 5));
CHECK(v.size() == 200);
CHECK(v[0] == 2);
REQUIRE(buf_ctrl.GetImage(v, ImageBuffer::MaxImage));
CHECK(v.size() == 200);
analysed->release();
// From preparation, as a frame dropped before it was shown
ZeroCopyReturnValue *dropped = buf_ctrl.GetImageSlot();
REQUIRE(dropped != nullptr);
dropped->SetImageNumber(6);
dropped->discard();
CHECK(buf_ctrl.GetStatus().preparation_slots == 0);
CHECK(!buf_ctrl.GetImage(v, 6));
CHECK(buf_ctrl.GetAvailSlots() == 4);
REQUIRE(buf_ctrl.CheckIfBufferReturned(std::chrono::microseconds(1)));
// Every slot can be taken again
for (int i = 0; i < 4; i++) {
ZeroCopyReturnValue *ret = buf_ctrl.GetImageSlot();
REQUIRE(ret != nullptr);
ret->SetImageNumber(10 + i);
ret->release();
}
REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1)));
}
TEST_CASE("ImageBuffer_ResultsChanged") {
DiffractionExperiment experiment(DetJF(18,3,8,36));
ImageBuffer buf_ctrl(4 * experiment.GetImageBufferLocationSize());
buf_ctrl.StartMeasurement(experiment);
auto counter = buf_ctrl.GetStatus().current_counter;
buf_ctrl.ResultsChanged();
CHECK(buf_ctrl.GetStatus().current_counter != counter);
}