// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include "JFJochReceiverLite.h" #include "../common/CUDAWrapper.h" using namespace std::chrono_literals; int64_t JFJochReceiverLite::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(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(16, requested_thread_number); return requested_thread_number; } JFJochReceiverLite::JFJochReceiverLite(const DiffractionExperiment &in_experiment, const PixelMask &in_pixel_mask, ImagePuller &in_image_puller, ImagePusher &in_image_pusher, Logger &in_logger, int64_t forward_and_sum_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) : JFJochReceiverStream(in_experiment, in_pixel_mask, in_image_puller, in_image_pusher, in_logger, NumberOfThreads(forward_and_sum_nthreads, in_experiment), in_spot_finding_settings, in_current_status, in_plots, in_image_buffer, in_zmq_preview_socket, in_zmq_metadata_socket), // retain_outcomes=false: the receiver indexes only, there is no scaling or merging at the end indexer(experiment, indexing_thread_pool, false, /*real_time=*/true), analysis(nthreads) { StartThreads(); } JFJochReceiverLite::~JFJochReceiverLite() { cancelled = true; JoinThreads(); } void JFJochReceiverLite::ImageThread(uint32_t id) { logger.Debug("Thread {} started", id); try { pin_gpu(); } catch (const JFJochException &e) { logger.Warning("Error pinning GPU {}", e.what()); } WaitForImages(); try { // The analysis works on the assembled image; an image in raw module layout is only forwarded. if (experiment.IsGeometryTransformed()) analysis[id] = std::make_unique(experiment, *az_int_mapping, pixel_mask, indexer, /*enable_fused_adaptive_gpu=*/true); } catch (const JFJochException &e) { Cancel(e); return; } PollImages(id); logger.Debug("Thread {} finished", id); } void JFJochReceiverLite::AddToEndMessage(EndMessage &message) { JFJochReceiverStream::AddToEndMessage(message); AddIndexingToEndMessage(message, indexer); } void JFJochReceiverLite::OnImage(uint32_t id, DataMessage &data_msg, std::chrono::steady_clock::time_point received) { auto start = std::chrono::high_resolution_clock::now(); CountImage(data_msg); auto image_start_time = std::chrono::high_resolution_clock::now(); AzimuthalIntegrationProfile profile(*az_int_mapping); if (analysis[id]) { // Indexing only: integration is rugnux's (live or offline), not the Lite receiver's. SpotFindingSettings settings = GetSpotFindingSettings(); settings.quick_integration = false; analysis[id]->AnalyzeStreamImage(data_msg, profile, settings); } auto image_end_time = std::chrono::high_resolution_clock::now(); std::chrono::duration image_duration = image_end_time - image_start_time; data_msg.processing_time_s = image_duration.count(); StampImage(data_msg, image_puller.GetCurrentFifoUtilization()); AddToAverages(data_msg); plots.Add(data_msg, profile); scan_result.Add(data_msg); if (!serialmx_filter.ApplyFilter(data_msg)) ++images_skipped; else ForwardImage(data_msg, image_buffer.GetImageSlot()); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration duration_total = end - start; logger.Debug("Thread {} Image {:6d} processing {:8.04f} s analysis {:8.04f} s", id, data_msg.number, duration_total.count(), image_duration.count()); }