Files
leonarski_f 2a5a47268f Merge branch 'rc188-fixedslots' into rc188-int
# Conflicts:
#	receiver/StillsGPUAnalysis.h
2026-11-19 15:37:12 +01:00

170 lines
7.9 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <array>
#include <condition_variable>
#include <exception>
#include <future>
#include <latch>
#include <map>
#include <memory>
#include <mutex>
#include <vector>
#include "StillsPrePass.h"
#include "RugnuxLiveStatus.h"
#include "../common/DiffractionExperiment.h"
#include "../common/JFJochException.h"
#include "../common/AzimuthalIntegrationMapping.h"
#include "../common/AzimuthalIntegrationProfile.h"
#include "../common/Logger.h"
#include "../common/ThreadSafeFIFO.h"
#include "../image_analysis/IndexAndRefine.h"
#include "../rugnux/StillsLiveMerge.h"
#include "../rugnux/ModelStep.h"
struct StillsSetup {
DiffractionExperiment experiment;
PixelMask mask;
std::unique_ptr<AzimuthalIntegrationMapping> mapping;
};
// What StillsGPUAnalysis needs from its owner (JFJochReceiverRugnuxStills).
class StillsSink {
public:
virtual ~StillsSink() = default;
// The pre-pass is over, no frame is analysed before this returns: the owner sends the start message.
virtual void StillsReady(const StillsSetup &setup) = 0;
// A frame is analysed; called from the analysis threads.
virtual void SendAnalysedImage(DataMessage &message, const AzimuthalIntegrationProfile &profile) = 0;
virtual SpotFindingSettings AnalysisSettings() = 0;
// The setup after the pre-pass failed, StillsReady included.
virtual void StillsSetupFailed(const JFJochException &error) = 0;
};
// Serial frames analysed on the GPU at the geometry of a pre-pass (fixed from there on), and merged beside the analysis
// The pre-pass waits for its first `prepass_images` images in the image buffer, one to a slot, in at most half of
// the `slots`: a collection they do not fit is refused (InputParameterInvalid), before anything is started
void CheckStillsPrePassFits(int64_t slots, int64_t slot_size, int64_t prepass_images);
class StillsGPUAnalysis {
public:
constexpr static std::array<double, 1> CHECKPOINTS = {0.5};
constexpr static int MERGE_THREADS = 8;
// The merge after the last frame scales and post-refines every crystal over the whole run (StillsLiveMerge::Final):
// the cold merge's result at the cold merge's cost after the last frame, instead of the fixed-cost merge of the
// crystals as they were prepared against the checkpoints. Prototype: an owner decision against the live budget.
constexpr static bool FINAL_FULL_SCALING = true;
// `start`: the collection's start message, as it would be sent without the pre-pass.
// `max_held`: the first images held for the pre-pass, at most (the receiver fits it to the image buffer).
StillsGPUAnalysis(const DiffractionExperiment &experiment, const PixelMask &mask, const StartMessage &start,
IndexerThreadPool *indexing_thread_pool, int64_t nthreads, int64_t max_held, Logger &logger,
StillsSink &sink, RugnuxLiveStatus &status, bool merge = true,
std::shared_ptr<const RugnuxModel> model = nullptr, double deadline_s = 0,
bool refine_solvent = false);
~StillsGPUAnalysis();
void Hold(const std::shared_ptr<HeldImage> &image);
// The frames held are all the pre-pass gets (the end of the collection): it starts on them
void StartPrePass();
// No frame comes after this: every frame is analysed before it returns. Throws if the setup after the pre-pass failed.
void Finish();
// After Finish, once everything is written; without it the outcome is reported when the analysis goes
void ReportOutcome();
// POST /cancel: the model step stops where it is, as at the deadline, and the frames are still analysed and merged.
void Cut(const std::string &reason);
// After Finish.
[[nodiscard]] std::optional<UnitCell> GetConsensusUnitCell() const;
[[nodiscard]] float GetBeamX_pxl() const;
[[nodiscard]] float GetBeamY_pxl() const;
[[nodiscard]] float GetDetectorDistance_mm() const;
// The merge of the collection, where one was made.
[[nodiscard]] const std::optional<StillsMergeOutput> &GetMerged() const;
[[nodiscard]] const DiffractionExperiment &GetMergeExperiment() const;
// The model step on the merge of the collection, where one was made.
[[nodiscard]] const std::optional<ModelStepResult> &GetModel() const;
// The cell, geometry and merged data, in the model's frame where a model was placed
void FillEndMessage(EndMessage &message) const;
private:
const DiffractionExperiment experiment;
const PixelMask &mask;
const StartMessage start;
IndexerThreadPool *indexing_thread_pool;
Logger &logger;
StillsSink &sink;
RugnuxLiveStatus &status;
const bool merge_frames;
std::mutex live_mutex;
RugnuxLiveStills live;
std::chrono::steady_clock::time_point ready_time;
void Publish();
std::optional<std::pair<RugnuxLiveState, std::string>> outcome; // kept by Finish for ReportOutcome
StillsSetup setup;
std::unique_ptr<IndexAndRefine> indexer;
std::mutex prepass_mutex;
std::condition_variable prepass_cv;
enum class PrePassState {Holding, Closing, Over}; // Closing: what is held is all there will be
PrePassState prepass_state = PrePassState::Holding;
std::map<int64_t, std::shared_ptr<HeldImage>> prepass_images;
std::latch prepass_done{1};
std::exception_ptr setup_error; // set before prepass_done
std::mutex error_mutex;
std::string analysis_error; // the first, where an analysis thread failed
void AnalysisError(const std::string &text);
const int64_t hold_limit; // the frames the pre-pass holds and fits on, at most
ThreadSafeFIFO<std::shared_ptr<HeldImage>> queue;
std::future<void> prepass_future;
std::vector<std::future<void>> analysis_futures;
// The frames analysed, by number, for the merge (-1 after the last); it takes their outcomes from the indexer.
ThreadSafeFIFO<int64_t> merge_queue;
std::unique_ptr<StillsLiveMerge> merge; // after the indexer: its reflections are in the indexer's arena
DiffractionExperiment merge_experiment;
std::optional<StillsMergeOutput> merged;
std::future<void> merge_future;
// Placed against each merge, mapped and refined on a thread of its own; Finish waits until deadline_s, then cuts it
const std::shared_ptr<const RugnuxModel> model;
const double deadline_s;
const bool refine_solvent; // the model step's refinement builds the solvent
std::string model_left_out; // why the model is left out; set by the merge thread before any step
struct ModelJob {
std::vector<MergedReflection> merged;
std::vector<float> shells; // the merge statistics' shell bounds
std::string title;
bool final = false;
bool cut = false;
};
std::mutex model_mutex;
std::condition_variable model_cv;
std::optional<ModelJob> model_next;
std::atomic<bool> step_cut{false}; // the step running is cut
std::atomic<int64_t> images_done{0}; // sent, analysed or not
bool model_stop = false;
bool model_cut = false; // the steps from here on are cut too
bool model_over = false; // the step on the merge of the collection has ended: nothing is left to cut
std::optional<ModelStepResult> model_placed; // the last step that placed the model, for the next to start from
std::optional<ModelStepResult> model_final;
std::future<void> model_future;
// The step on `out`, the merge of `title` (its reflections moved out, but for the final one).
void SubmitModel(StillsMergeOutput &out, const std::string &title, bool final);
void StopModel(bool cut);
void ModelThread();
void PublishModel(const ModelStepResult &result, const std::string &title);
void PrePassThread();
void AnalysisThread();
void MergeThread();
void PublishMerge(const StillsMergeOutput &out, const std::string &title);
};