Build Packages / Build documentation (push) Successful in 45s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m34s
Build Packages / Create release (push) Successful in 8s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 8m55s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m11s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m51s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m42s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 24m46s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 26m18s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 31m0s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 31m16s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 32m15s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 27m12s
Build Packages / Generate python client (push) Successful in 30s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 30m40s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 33m20s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 31m3s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 27m5s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 30m24s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 32m54s
Build Packages / Unit tests (push) Failing after 1h53m52s
Build Packages / Validation Battery (push) Successful in 46m47s
Comments say what the next line does or why; names follow the rest of the receiver: <thing>_mutex, thread bodies <What>Thread (FPGAImagePuller: AcquireThread, ModuleThread, CompressThread, FinalizeMeasurement), no single letters for objects. Code does not change. JFJochReceiverFPGA and what the style review's "leave alone" list names (deleters, exception_ptr, the condition variables, LiveModel) stay as they were. Moved here from the comments: Pre-pass size: of 100 frames of a collection indexed at 18%, 7 indexed at a distance 2.6% off and the geometry refinement did not run; of 500, 34 did and refined it to 0.4%. With the broker's default fixed threshold 3 points fewer frames index than with the adaptive one the analysis takes. Preflight sends FillMessage alone, measured on a JUNGFRAU 9M at 1.4 kB against 543 kB for the start message, and the whole of it rather than the fields the check reads today, so the writer sees the description it will be given at start. FFT indexers and cuFFT plans are kept for the life of the broker because every pass of every collection builds the same pools, and making a plan takes a process-wide lock and took over a second on a card busy with the online analysis. The host memory budget subtracts the image buffer, which has to keep the whole collection and so is most of the machine; at worst what could run side by side runs one part after the other. The receiver is released before the indexer pool is rebuilt because a fresh GPU indexer next to a stale receiver (after a failed start the receiver is stopped but not destroyed until the next Start) can fail to initialise. ReceiveRingBuffer: one image of a large detector is tens of megabytes, so a queue of a thousand of them is a quantity of memory nobody chose. Regions are handed out and reclaimed in arrival order; a region released early is only remembered, and republish holds at most its queue's worth, so that is as far as the reclaim can lag. A message larger than the whole ring goes on the heap, so an oversized START or END cannot wedge the receiver. Republish is capped at a share of the ring because it keeps the oldest unsent images and would otherwise pin everything received after them, running the writer at the republish rate. ZMQImagePuller: with a full ring the broker's pusher waits one second, then drops the image and sends without waiting for the rest of the run, so a ring too small for the writer's stalls costs images. TCPImagePuller: a full ring blocks the pusher's send; its BUSY heartbeats tell it apart from a dead writer, and it gives up only after a minute in which no image was written. Keep-alive detection is about 60 s, both sides. Stills model step: the step on a provisional merge still running when the next merge is made is cut, one not started is replaced; a molecular replacement or a refinement not started is left out at the deadline, a refinement running is cut short; each step starts from the last placement. Without a space group stated the frames are merged in the model's where its cell is the run's consensus, otherwise the model is left out. Rotation, stills and stills with a model end to end against the build before this work: MTZ data columns, both maps, placed and refined models and blobs are byte for byte the same; only the MTZ history's build stamp differs. Scan results are the same with times stripped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
275 lines
11 KiB
C++
275 lines
11 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "JFJochReceiverStream.h"
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
int64_t JFJochReceiverStream::NumberOfThreads(int64_t requested_thread_number,
|
|
const DiffractionExperiment &in_experiment) {
|
|
int64_t number_of_images = in_experiment.GetImageNum();
|
|
auto image_time = in_experiment.GetImageTime();
|
|
|
|
if (requested_thread_number <= 0)
|
|
return 1;
|
|
|
|
// For very small datasets no need to go multithreaded
|
|
if (number_of_images < 4)
|
|
return 1;
|
|
|
|
if (number_of_images < 16)
|
|
return std::min<int64_t>(2, requested_thread_number);
|
|
|
|
// For 100 Hz, there is no reason to go for a very large number of threads
|
|
if (number_of_images < 256 || (image_time >= 10ms))
|
|
return std::min<int64_t>(16, requested_thread_number);
|
|
|
|
return requested_thread_number;
|
|
}
|
|
|
|
void JFJochReceiverStream::ApplyStartMessage(DiffractionExperiment &experiment, const StartMessage &msg) {
|
|
// A PSI detector's images are what the experiment set the card up to produce: nothing to learn here.
|
|
if (experiment.GetDetectorType() != DetectorType::DECTRIS)
|
|
return;
|
|
|
|
if ((experiment.GetXPixelsNum() != msg.image_size_x)
|
|
|| (experiment.GetYPixelsNum() != msg.image_size_y))
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Mismatch in detector size");
|
|
if (fabs(experiment.GetPixelSize_mm() - msg.pixel_size_x * 1e3) > 1e-7)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Mismatch in pixel size");
|
|
|
|
experiment.Detector().Description(msg.detector_description);
|
|
experiment.Detector().SerialNumber(msg.detector_serial_number);
|
|
experiment.Detector().SensorMaterial(msg.sensor_material);
|
|
experiment.Detector().SensorThickness_um(msg.sensor_thickness * 1e6);
|
|
experiment.Detector().SaturationLimit(SaturationLimitFromValue(msg.saturation_value));
|
|
// The outgoing start message is rebuilt from the experiment, NXmx would record these as not applied
|
|
experiment.Detector().CountRateCorrectionApplied(msg.countrate_correction_enabled);
|
|
experiment.Detector().CountRateCorrectionLookupTable(msg.countrate_correction_lookup_table);
|
|
experiment.Detector().FlatfieldApplied(msg.flatfield_enabled);
|
|
experiment.Detector().VirtualPixelInterpolationApplied(msg.virtual_pixel_interpolation_enabled);
|
|
// The mask written with the data is ours, never uploaded to the detector
|
|
experiment.ApplyPixelMask(false);
|
|
// Images are forwarded byte-for-byte, so the stream decides both width and sign
|
|
experiment.Detector().BitDepthImage(msg.bit_depth_image);
|
|
experiment.PixelSigned(msg.pixel_signed);
|
|
}
|
|
|
|
JFJochReceiverStream::JFJochReceiverStream(const DiffractionExperiment &in_experiment,
|
|
const PixelMask &in_pixel_mask,
|
|
ImagePuller &in_image_puller,
|
|
ImagePusher &in_image_pusher,
|
|
Logger &in_logger,
|
|
int64_t requested_nthreads,
|
|
const SpotFindingSettings &in_spot_finding_settings,
|
|
JFJochReceiverCurrentStatus &in_current_status,
|
|
JFJochReceiverPlots &in_plots,
|
|
ImageBuffer &in_image_buffer,
|
|
ZMQPreviewSocket *in_zmq_preview_socket,
|
|
ZMQMetadataSocket *in_zmq_metadata_socket,
|
|
IndexerThreadPool *indexing_thread_pool)
|
|
: JFJochReceiver(in_experiment,
|
|
in_image_buffer,
|
|
in_image_pusher,
|
|
in_current_status,
|
|
in_plots,
|
|
in_spot_finding_settings,
|
|
in_logger,
|
|
in_pixel_mask,
|
|
in_zmq_preview_socket,
|
|
in_zmq_metadata_socket,
|
|
indexing_thread_pool),
|
|
image_puller(in_image_puller),
|
|
nthreads(NumberOfThreads(requested_nthreads, in_experiment)),
|
|
measurement_started(1),
|
|
images_started(nthreads) {
|
|
}
|
|
|
|
void JFJochReceiverStream::StartThreads() {
|
|
logger.Debug("Starting {} image threads", nthreads);
|
|
for (int i = 0; i < nthreads; i++)
|
|
image_futures.emplace_back(std::async(std::launch::async, &JFJochReceiverStream::ImageThread, this, i));
|
|
measurement = std::async(std::launch::async, &JFJochReceiverStream::MeasurementThread, this);
|
|
images_started.wait();
|
|
logger.Debug("Image threads ready");
|
|
}
|
|
|
|
void JFJochReceiverStream::JoinFuture(std::future<void> &future) {
|
|
try {
|
|
if (future.valid())
|
|
future.get();
|
|
} catch (const std::exception &e) {
|
|
logger.Error("Receiver teardown failed: {}", e.what());
|
|
} catch (...) {
|
|
logger.Error("Receiver teardown failed with unknown exception");
|
|
}
|
|
}
|
|
|
|
void JFJochReceiverStream::JoinThreads() {
|
|
JoinFuture(measurement);
|
|
// The measurement thread joins them, unless it left with an error
|
|
for (auto &f: image_futures)
|
|
JoinFuture(f);
|
|
}
|
|
|
|
void JFJochReceiverStream::MeasurementThread() {
|
|
try {
|
|
// Once the detector has reported it is done, a start message in flight may still land, then give up
|
|
std::optional<std::chrono::steady_clock::time_point> give_up_at;
|
|
auto msg = image_puller.PollImage();
|
|
|
|
while (!cancelled && (!msg.has_value() || !msg->cbor || !msg->cbor->start_message)) {
|
|
if (detector_finished && !give_up_at)
|
|
give_up_at = std::chrono::steady_clock::now() + StartMessageGrace;
|
|
if (give_up_at && (std::chrono::steady_clock::now() > *give_up_at)) {
|
|
logger.Error("Detector finished, but no start message arrived - abandoning measurement");
|
|
cancelled = true;
|
|
break;
|
|
}
|
|
msg = image_puller.PollImage();
|
|
}
|
|
|
|
if (cancelled) {
|
|
current_status.SetProgress({});
|
|
current_status.SetStatus(GetStatus());
|
|
measurement_started.count_down();
|
|
return; // just quit the function - no measurement, no results
|
|
}
|
|
|
|
ApplyStartMessage(experiment, msg->cbor->start_message.value());
|
|
image_buffer.StartMeasurement(experiment); // Only at this point we know bit-depth of the images
|
|
start_time = std::chrono::system_clock::now();
|
|
OnStart(msg->received);
|
|
measurement_started.count_down();
|
|
} catch (const JFJochException &e) {
|
|
logger.ErrorException(e);
|
|
Cancel(e);
|
|
measurement_started.count_down();
|
|
throw;
|
|
} catch (const std::exception &e) {
|
|
logger.Error("Cancelling data collection due to exception: {}", e.what());
|
|
cancelled = true;
|
|
measurement_started.count_down();
|
|
throw;
|
|
}
|
|
|
|
logger.Info("Receiving started");
|
|
|
|
for (auto &f: image_futures)
|
|
f.get();
|
|
|
|
logger.Debug("Image threads finished");
|
|
|
|
FinishCollection();
|
|
}
|
|
|
|
void JFJochReceiverStream::OnStart(std::chrono::steady_clock::time_point received) {
|
|
SendStartMessage();
|
|
}
|
|
|
|
void JFJochReceiverStream::ImageThread(uint32_t id) {
|
|
WaitForImages();
|
|
PollImages(id);
|
|
}
|
|
|
|
void JFJochReceiverStream::WaitForImages() {
|
|
images_started.count_down();
|
|
measurement_started.wait();
|
|
}
|
|
|
|
void JFJochReceiverStream::PollImages(uint32_t id) {
|
|
while (!cancelled && !end_message_received) {
|
|
try {
|
|
auto msg = image_puller.PollImage(std::chrono::milliseconds(5));
|
|
|
|
if (!msg.has_value() || !msg->cbor) {
|
|
// Message not received or not parsed
|
|
continue;
|
|
} else if (msg->cbor->end_message) {
|
|
end_adu_histogram = msg->cbor->end_message->adu_histogram;
|
|
end_message_received = true;
|
|
logger.Debug("Thread {} end message received", id);
|
|
} else if (msg->cbor->data_message) {
|
|
DataMessage message = msg->cbor->data_message.value();
|
|
OnImage(id, message, msg->received);
|
|
}
|
|
UpdateMaxDelay(image_puller.GetCurrentFifoUtilization());
|
|
current_status.SetProgress(GetProgress());
|
|
current_status.SetStatus(GetStatus());
|
|
} catch (const JFJochException &e) {
|
|
logger.ErrorException(e);
|
|
Cancel(e);
|
|
} catch (const std::exception &e) {
|
|
logger.Error("Cancelling data collection due to exception: {}", e.what());
|
|
cancelled = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void JFJochReceiverStream::CheckIfImagesSent() {
|
|
if (!image_buffer.CheckIfBufferReturned(SendFinalizeTimeout)) {
|
|
logger.Error("Send commands not finalized in {} seconds", SendFinalizeTimeout.count());
|
|
throw JFJochException(JFJochExceptionCategory::ZeroMQ, "Send commands not finalized in "
|
|
+ std::to_string(SendFinalizeTimeout.count()) + " seconds");
|
|
}
|
|
}
|
|
|
|
void JFJochReceiverStream::WriterFinalize() {
|
|
if (push_images_to_writer)
|
|
writer_error = image_pusher.Finalize();
|
|
}
|
|
|
|
void JFJochReceiverStream::FinishCollection() {
|
|
current_status.SetProgress(1.0);
|
|
current_status.SetStatus(GetStatus());
|
|
|
|
SendEndMessage();
|
|
CheckIfImagesSent();
|
|
logger.Debug("All images sent");
|
|
|
|
WriterFinalize();
|
|
logger.Debug("Writing process finalized");
|
|
|
|
end_time = std::chrono::system_clock::now();
|
|
|
|
current_status.SetProgress({});
|
|
current_status.SetStatus(GetStatus());
|
|
}
|
|
|
|
void JFJochReceiverStream::StopReceiver() {
|
|
if (measurement.valid()) {
|
|
measurement.get();
|
|
logger.Debug("Receiver stopped");
|
|
}
|
|
}
|
|
|
|
void JFJochReceiverStream::Cancel(bool silent) {
|
|
JFJochReceiver::Cancel(silent);
|
|
// A silent cancel is not a request to abort, it says the detector has finished. It bounds the wait for the start message.
|
|
detector_finished = true;
|
|
}
|
|
|
|
void JFJochReceiverStream::AddToEndMessage(EndMessage &message) {
|
|
if (!end_adu_histogram.empty())
|
|
message.adu_histogram = end_adu_histogram;
|
|
}
|
|
|
|
// With the card summing, an image is many frames.
|
|
int64_t JFJochReceiverStream::ExpectedImages() const {
|
|
return experiment.GetImageNum();
|
|
}
|
|
|
|
float JFJochReceiverStream::GetEfficiency() const {
|
|
if (ExpectedImages() == 0)
|
|
return 0;
|
|
return static_cast<float>(images_collected) / static_cast<float>(ExpectedImages());
|
|
}
|
|
|
|
float JFJochReceiverStream::GetProgress() const {
|
|
if (ExpectedImages() == 0)
|
|
return 0.0;
|
|
return static_cast<float>(max_image_number_received) / static_cast<float>(ExpectedImages());
|
|
}
|