Files
leonarski_f 4809c1622c
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m43s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 28m46s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 32m5s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 39m44s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m7s
Build Packages / Create release (push) Successful in 8s
Build Packages / Build documentation (push) Successful in 49s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m30s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 31m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m29s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m51s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 26m7s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 29m38s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m6s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 29m44s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 33m22s
Build Packages / Generate python client (push) Successful in 47s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 34m6s
Build Packages / Unit tests (push) Successful in 1h2m47s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 35m59s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 34m8s
Build Packages / Validation Battery (push) Successful in 38m31s
v1.0.0-rc.187 (#98)
* Rugnux: Refinement several times faster, one protocol on the CPU and the GPU, parts of it on the Apple GPU; its random draws are the same on every platform.
* Rugnux: Refinement follows common practice: the free set decides only the restraint weights and the displacement model (isotropic, TLS or anisotropic, by Hamilton's test), everything else is decided on the working set; lower R-free for placed and predicted models.
* Rugnux: Restraint dictionaries via `--restraints` (or among the inputs) and more built-in ligands; a model whose space group is a subgroup of the data's is refined in its own group; reflections plus a model select `--mode refine` on their own.
* Rugnux: `-C` fixes the crystal system on the axes as typed; `-S` alone fixes the space group only, the cell is still found and reduced; a space group or cell stored in the input file is ignored, with a line in the log.
* Rugnux: More reliable multi-sweep merging; `--mode scale` merges several stills runs as one; `_plot.txt` and the report name each input; a second crystal form is reported for stills given a cell.
* jfjoch_broker (experimental): Live Rugnux on FPGA detectors (JUNGFRAU) takes the same path as a DECTRIS stream, rotation and serial alike, model step included, with analysis threads per GPU; frames waiting for the analysis can be viewed; no host stage drops a frame - a source faster than the analysis is held at the card.
* jfjoch_broker: Summation of more than 256 frames per image is split between the card and the host (`summation_mode` is then `hybrid`), and live Rugnux runs on such collections; pedestal runs take a receiver of their own.
* jfjoch_broker (experimental): `stills_downsample` analyses 1 of every N serial images (chosen by a hash; the first 2000 all; the others written without analysis; reset to 1 by `/initialize`); `max_spot_count` left out means 250 for per-image indexing and up to 1000 for live stills; the live status gives the merge before the model work, the fitted resolution and the anomalous peak height of each unexplained blob.
* jfjoch_broker: Without a `pcie` entry the broker runs on a mock of the card instead of the HLS simulation; its spot, ROI and azimuthal results are zero then.
* Web frontend: Redesigned Rugnux live page; Vite 8, TypeScript 6, Mol* 5.13 and dependency security updates.
* Tools: `jfjoch_fpga_stills_test` measures live stills on the cards with a stored dataset; `jfjoch_fpga_test` loses the HLS-simulation option.
* Other: FFTW no longer fetched or linked (the CPU FFT indexer keeps the name `FFTW`); PCIe driver now GPL-2.0-or-later and third-party notices complete; several crash fixes.

Reviewed-on: #98
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-19 20:49:28 +01:00

269 lines
12 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "FPGAImagePuller.h"
#include <thread>
#include <utility>
FPGAImagePuller::FPGAImagePuller(const DiffractionExperiment &in_experiment, const PixelMask &mask,
const JFCalibration *calibration, AcquisitionDeviceGroup &in_devices,
Logger &in_logger, int64_t image_threads)
: ImagePuller(Queue + 2), // the images waiting, the start and the end message
experiment(in_experiment),
logger(in_logger),
devices(in_devices),
host_summation(in_experiment.GetHostSummation()) {
const int ndatastreams = experiment.GetDataStreamsNum();
if (devices.size() < ndatastreams)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Number of acquisition devices has to match data streams");
for (int m = 0; m < experiment.GetModulesNum(); m++)
adu_histogram.emplace_back(std::make_unique<ADUHistogram>());
for (int d = 0; d < ndatastreams; d++) {
devices[d].PrepareAction(experiment);
expected_packets_per_image += devices[d].Counters().GetExpectedPacketsPerImage() * host_summation;
}
if (experiment.GetDetectorType() != DetectorType::JUNGFRAU)
calibration = nullptr;
// The receiver's own start message is built from its experiment; this one only says the run has begun.
StartMessage start{};
experiment.FillMessage(start);
outside_fifo.Put({.cbor = std::make_shared<CBORStream2DeserializerOutput>(start),
.received = std::chrono::steady_clock::now()});
for (int i = 0; i < image_threads; i++)
image_futures.emplace_back(std::async(std::launch::async, &FPGAImagePuller::ImageThread, this));
std::latch ready(ndatastreams);
for (int d = 0; d < ndatastreams; d++)
acquire_futures.emplace_back(std::async(std::launch::async, &FPGAImagePuller::AcquireThread, this,
static_cast<uint16_t>(d), std::cref(mask), calibration,
std::ref(ready)));
ready.wait();
finish_future = std::async(std::launch::async, &FPGAImagePuller::FinalizeMeasurement, this);
if (failed) {
try {
Stop();
} catch (...) {
outside_fifo.Clear(); // the destructor does not run
throw;
}
}
}
FPGAImagePuller::~FPGAImagePuller() {
try {
Stop();
} catch (const std::exception &e) {
logger.Error("FPGA image puller teardown failed: {}", e.what());
}
}
void FPGAImagePuller::SetError(std::exception_ptr e) {
std::unique_lock ul(error_mutex);
if (!error)
error = e;
failed = true;
}
void FPGAImagePuller::StopCards() {
for (int d = 0; d < experiment.GetDataStreamsNum(); d++)
devices[d].Cancel();
}
void FPGAImagePuller::Cancel() {
cancelled = true;
StopCards();
}
// Cancels first: a receiver that stopped on an error takes no more images, which would hold the threads otherwise
void FPGAImagePuller::Stop() {
Cancel();
std::call_once(finish_once, [this] { finish_future.get(); });
std::unique_lock ul(error_mutex);
if (error)
std::rethrow_exception(std::exchange(error, nullptr));
}
void FPGAImagePuller::AcquireThread(uint16_t data_stream, const PixelMask &mask, const JFCalibration *calibration,
std::latch &ready) {
auto &device = devices[data_stream];
try {
// Only what the card needs for the pixels: its analysis results are not read.
if (calibration != nullptr)
device.InitializeCalibration(experiment, *calibration);
device.InitializePixelMask(experiment, mask);
device.StartAction(experiment);
} catch (...) {
SetError(std::current_exception());
StopCards(); // the image threads waiting on the other cards have to come out as well
ready.count_down();
return;
}
ready.count_down();
try {
device.WaitForActionComplete();
} catch (...) {
SetError(std::current_exception());
StopCards();
}
}
// Polled: a card that fails or finishes ends the wait. A frame given up on that comes later is below the device's
// slowest frame then (MissingAfterFrames), and the device returns its buffer itself.
bool FPGAImagePuller::WaitForFrame(uint16_t data_stream, uint16_t module_number, int64_t frame) {
const auto &counters = devices[data_stream].Counters();
// A frame is complete once the module is two further on, as in JFJochReceiverFPGA
while ((static_cast<int64_t>(counters.GetCurrFrameNumber(module_number)) < frame + 2)
&& !counters.IsAcquisitionFinished() && !failed
&& (static_cast<int64_t>(counters.GetFastestFrameNumber()) < frame + 2 + MissingAfterFrames))
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return counters.IsAnyPacketCollected(frame, module_number);
}
bool FPGAImagePuller::CardsFinishedBefore(int64_t frame) const {
for (int d = 0; d < experiment.GetDataStreamsNum(); d++) {
const auto &counters = devices[d].Counters();
if (!counters.IsAcquisitionFinished() || (static_cast<int64_t>(counters.GetFastestFrameNumber()) >= frame))
return false;
}
return true;
}
void FPGAImagePuller::ImageThread() {
// One image of its own, assembled and compressed here
FrameTransformation transformation(experiment);
// Summation on the host: the cards' images of one image added up, one module each, as in JFJochReceiverFPGA
std::vector<std::unique_ptr<ModuleSummation>> summation;
if (host_summation > 1)
for (int m = 0; m < experiment.GetModulesNum(); m++)
summation.emplace_back(std::make_unique<ModuleSummation>(experiment));
try {
for (int64_t image = next_to_take++; image < experiment.GetImageNum() && !failed; image = next_to_take++) {
// The cards are through and none got as far as this image (a cancel, or fewer frames than asked for):
// it will not come, and is not assembled from missing modules either - as JFJochReceiverFPGA
if (CardsFinishedBefore(image * host_summation)) {
Emit(image, std::nullopt);
continue;
}
ImageMetadata metadata(experiment);
auto take_module = [&](const DeviceOutput *output, int d, int m) {
metadata.Process(output);
adu_histogram[experiment.GetFirstModuleOfDataStream(d) + m]->Add(*output);
transformation.ProcessModule(output, d);
};
for (auto &s: summation)
s->Clear();
// The card's images of this image in their order, all modules of each before the next, as they come
for (int64_t frame = image * host_summation; frame < (image + 1) * host_summation; frame++) {
for (int d = 0; d < experiment.GetDataStreamsNum(); d++) {
auto &device = devices[d];
for (int m = 0; m < experiment.GetModulesNum(d); m++) {
const bool collected = WaitForFrame(d, m, frame);
if (!summation.empty()) {
auto &s = *summation[experiment.GetFirstModuleOfDataStream(d) + m];
if (collected)
s.AddFPGAOutput(*device.GetDeviceOutput(frame, m));
else
s.AddEmptyOutput();
} else if (collected)
take_module(device.GetDeviceOutput(frame, m), d, m);
else
transformation.FillNotCollectedModule(m, d);
if (collected)
device.FrameBufferRelease(frame, m);
}
}
}
// A module missing from any of the card's images is missing from the image
if (!summation.empty())
for (int d = 0; d < experiment.GetDataStreamsNum(); d++)
for (int m = 0; m < experiment.GetModulesNum(d); m++) {
const auto &s = *summation[experiment.GetFirstModuleOfDataStream(d) + m];
if (s.empty())
transformation.FillNotCollectedModule(m, d);
else
take_module(&s.GetOutput(), d, m);
}
DataMessage message{};
message.number = image;
message.original_number = image;
metadata.Export(message, expected_packets_per_image);
// No module of it came
if (message.image_collection_efficiency == 0.0f) {
Emit(image, std::nullopt);
continue;
}
// The card's spot finder and ROI map were not set up for this run; the receiver reports its own.
message.strong_pixel_count.reset();
message.roi.clear();
// The compressed image, copied once into a buffer of the output's own
const CompressedImage compressed = transformation.GetCompressedImage();
auto bytes = std::make_shared<std::vector<uint8_t>>(compressed.GetCompressed(),
compressed.GetCompressed() + compressed.GetCompressedSize());
message.image = CompressedImage(bytes->data(), bytes->size(), compressed.GetWidth(), compressed.GetHeight(),
compressed.GetMode(), compressed.GetCompressionAlgorithm());
ImagePullerOutput output;
output.cbor = std::make_shared<CBORStream2DeserializerOutput>(message);
output.raw = std::shared_ptr<ReceiveBufferRegion>(new ReceiveBufferRegion{bytes->data(), bytes->size()},
[bytes](ReceiveBufferRegion *region) { delete region; });
Emit(image, std::move(output));
}
} catch (...) {
SetError(std::current_exception());
StopCards();
}
}
// Whichever thread finishes the image next in line lets it, and those waiting behind it, out.
void FPGAImagePuller::Emit(int64_t image_number, std::optional<ImagePullerOutput> output) {
std::unique_lock ul(order_mutex);
images_done.emplace(image_number, std::move(output));
for (auto it = images_done.begin(); (it != images_done.end()) && (it->first == next_image);
it = images_done.erase(it), next_image++) {
if (it->second) {
it->second->received = std::chrono::steady_clock::now();
Put(*it->second);
}
}
}
void FPGAImagePuller::FinalizeMeasurement() {
for (auto &f: image_futures)
f.get();
StopCards();
for (auto &f: acquire_futures)
f.get();
// Only an error leaves images unfinished, holding back those behind them
{
std::unique_lock ul(order_mutex);
for (auto &[n, output]: images_done)
if (output)
Put(*output);
images_done.clear();
}
EndMessage end{};
for (size_t i = 0; i < adu_histogram.size(); i++)
end.adu_histogram["module" + std::to_string(i)] = adu_histogram[i]->GetHistogram();
Put({.cbor = std::make_shared<CBORStream2DeserializerOutput>(end), .received = std::chrono::steady_clock::now()});
}
void FPGAImagePuller::Put(const ImagePullerOutput &output) {
while (!outside_fifo.PutTimeout(output, std::chrono::milliseconds(100)) && !cancelled);
}