Files

89 lines
4.0 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <atomic>
#include <future>
#include <latch>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <vector>
#include "FrameTransformation.h"
#include "ImageMetadata.h"
#include "../acquisition_device/AcquisitionDeviceGroup.h"
#include "../common/ADUHistogram.h"
#include "../common/ModuleSummation.h"
#include "../image_puller/ImagePuller.h"
#include "../jungfrau/JFCalibration.h"
// The images of a collection from the FPGA cards, as a detector stream, in image order. As JFJochReceiverFPGA: each
// thread takes the next image, waits for its modules in the cards' buffers, assembles them into an image of its own
// and compresses it; the image leaves as its compressed bytes, and the cards' buffers go back. The cards' buffers are
// the only window over the images in flight. No image is discarded here: an image waits for room in the queue to the
// receiver, holding the cards' buffers meanwhile, so the cards wait for the host. Images are lost only at the card.
class FPGAImagePuller : public ImagePuller {
public:
// Images compressed and waiting for the receiver
static constexpr size_t Queue = 128;
// A module this far behind the fastest one of its card is taken as missing from that frame. It must stay beyond
// AcquisitionCounters::ThresholdFramesLost: the device then returns that frame's buffer itself if it comes later.
static constexpr int64_t MissingAfterFrames = AcquisitionCounters::ThresholdFramesLost + 2;
private:
const DiffractionExperiment experiment;
Logger &logger;
AcquisitionDeviceGroup &devices;
// The cards' images making one image, summed here (DiffractionExperiment::GetHostSummation)
const int64_t host_summation;
uint64_t expected_packets_per_image = 0;
std::atomic<int64_t> next_to_take = 0;
// What is done, waiting for the images before it. No value: that image will not come.
std::mutex order_mutex;
std::map<int64_t, std::optional<ImagePullerOutput>> images_done;
int64_t next_image = 0;
std::vector<std::unique_ptr<ADUHistogram>> adu_histogram;
std::mutex error_mutex;
std::exception_ptr error;
std::atomic<bool> failed = false; // a failed card may never report finished: nothing waits for it then
std::atomic<bool> cancelled = false; // Cancel(): the receiver takes no more images, nothing waits for it
void SetError(std::exception_ptr e);
std::vector<std::future<void>> acquire_futures;
std::vector<std::future<void>> image_futures;
std::future<void> finish_future;
std::once_flag finish_once;
void AcquireThread(uint16_t data_stream, const PixelMask &mask, const JFCalibration *calibration, std::latch &ready);
void ImageThread();
void FinalizeMeasurement();
// False where the module is missing from the frame
bool WaitForFrame(uint16_t data_stream, uint16_t module_number, int64_t frame);
// Every card has finished, all of them before this frame
bool CardsFinishedBefore(int64_t frame) const;
void Emit(int64_t image_number, std::optional<ImagePullerOutput> output);
// Waits for room in the queue, as long as the receiver takes from it: after a cancel it no longer does
void Put(const ImagePullerOutput &output);
public:
// Arms the cards; when it returns the detector can be started.
FPGAImagePuller(const DiffractionExperiment &experiment, const PixelMask &mask, const JFCalibration *calibration,
AcquisitionDeviceGroup &devices, Logger &logger, int64_t image_threads);
~FPGAImagePuller() override;
void Disconnect() override {}
// The cards finish: what they have got still comes, then the end message.
void StopCards();
// As StopCards(), and the receiver takes no more images: nothing waits for it any more.
void Cancel();
// After the receiver is done: cancels, waits for the threads, throws what went wrong with the cards.
void Stop();
};