diff --git a/common/ImageBuffer.cpp b/common/ImageBuffer.cpp index fff84beb2..98b0101f4 100644 --- a/common/ImageBuffer.cpp +++ b/common/ImageBuffer.cpp @@ -320,6 +320,10 @@ void ImageBuffer::SaveStartMessage(const std::vector &msg) { start_message = msg; } +size_t ImageBuffer::GetBufferSize() const { + return buffer_size; +} + ImageBufferStatus ImageBuffer::GetStatus() const { ImageBufferStatus ret; { diff --git a/common/ImageBuffer.h b/common/ImageBuffer.h index 9c0be3795..d0a92c463 100644 --- a/common/ImageBuffer.h +++ b/common/ImageBuffer.h @@ -81,6 +81,7 @@ public: void ReleaseSlot(uint32_t location); int64_t GetAvailSlots() const; + size_t GetBufferSize() const; bool CheckIfBufferReturned(std::chrono::microseconds timeout); bool Finalize(std::chrono::microseconds timeout); diff --git a/receiver/JFJochReceiverRugnux.cpp b/receiver/JFJochReceiverRugnux.cpp index e4337f7c4..35a7b22a9 100644 --- a/receiver/JFJochReceiverRugnux.cpp +++ b/receiver/JFJochReceiverRugnux.cpp @@ -12,6 +12,7 @@ #include "spdlog/fmt/fmt.h" #include "JFJochReceiverLite.h" +#include "../common/HostMemory.h" #include "../common/ParallelFor.h" #include "../frame_serialize/CBORStream2Deserializer.h" #include "../frame_serialize/CBORStream2Serializer.h" @@ -22,6 +23,20 @@ using namespace std::chrono_literals; namespace { + // Kept for the rest of the broker - the receiver, the pushers, the preview - when the host memory + // rugnux plans against is decided. + constexpr size_t RUGNUX_HOST_MEMORY_MARGIN = 16ULL * 1024 * 1024 * 1024; + + // The host memory rugnux plans against (HostMemory.h) is the machine's budget less what the broker + // already holds: the image buffer, which has to keep the whole collection and so is most of the + // machine, and a margin. Never 0, which would take the whole machine's budget: at worst what could + // run side by side runs one part after the other. + size_t RugnuxHostBudget(const ImageBuffer &buffer) { + const size_t budget = DefaultHostMemoryBudget(); + const size_t taken = buffer.GetBufferSize() + RUGNUX_HOST_MEMORY_MARGIN; + return budget > taken ? budget - taken : 1; + } + int64_t SteadyNowNs() { return std::chrono::duration_cast( std::chrono::steady_clock::now().time_since_epoch()).count(); @@ -118,6 +133,7 @@ JFJochReceiverRugnux::JFJochReceiverRugnux(const DiffractionExperiment &in_exper reader(in_image_buffer), forward_nthreads(JFJochReceiverLite::NumberOfDataAnalysisThreads(in_forward_nthreads, in_experiment)), rugnux_nthreads(in_rugnux_nthreads), + host_budget(RugnuxHostBudget(in_image_buffer)), model(std::move(in_model)), deadline_s(in_deadline_s), live_status(in_live_status), @@ -127,8 +143,9 @@ JFJochReceiverRugnux::JFJochReceiverRugnux(const DiffractionExperiment &in_exper // priority. Made here, at the broker's own priority, before the rugnux thread lowers its own - so // the pool is lowered for the analysis only where it can be raised back (see RugnuxThread). parallel_detail::WorkerPool::Instance(); - logger.Info("Starting {} forward threads; rugnux runs on {} threads while the images arrive", - forward_nthreads, rugnux_nthreads); + logger.Info("Starting {} forward threads; rugnux runs on {} threads while the images arrive, " + "planning against {:.1f} GB of host memory", forward_nthreads, rugnux_nthreads, + static_cast(host_budget) / 1e9); for (int i = 0; i < forward_nthreads; i++) forward_futures.emplace_back(std::async(std::launch::async, &JFJochReceiverRugnux::ForwardThread, this, i)); measurement = std::async(std::launch::async, &JFJochReceiverRugnux::MeasurementThread, this); @@ -285,7 +302,8 @@ void JFJochReceiverRugnux::RugnuxThread() { } try { const auto dataset = reader.GetDataset(); - const RugnuxLiveSetup setup = MakeRugnuxLiveSetup(*dataset, rugnux_nthreads, model); + RugnuxLiveSetup setup = MakeRugnuxLiveSetup(*dataset, rugnux_nthreads, model); + setup.config.host_memory_budget_bytes = host_budget; LiveObserver observer(plots, scan_result, *az_int_mapping, live_status, logger); ProcessResult result; { @@ -329,6 +347,7 @@ void JFJochReceiverRugnux::ProbeThread() { return; // a sweep this short is decided on soon enough by the run itself // A quarter of the threads: it runs beside the main run, and each of its workers holds GPU memory too. RugnuxLiveSetup setup = MakeRugnuxLiveSetup(*dataset, std::max(2, rugnux_nthreads / 4), nullptr); + setup.config.host_memory_budget_bytes = host_budget; // the same as the main run's: one pool for both setup.config.end_image = static_cast(frames); setup.config.rotation_postrefine_geometry = false; setup.config.run_scaling = false; diff --git a/receiver/JFJochReceiverRugnux.h b/receiver/JFJochReceiverRugnux.h index a8259d009..9ee16d2c1 100644 --- a/receiver/JFJochReceiverRugnux.h +++ b/receiver/JFJochReceiverRugnux.h @@ -29,6 +29,7 @@ class JFJochReceiverRugnux : public JFJochReceiver { const int64_t forward_nthreads; const int rugnux_nthreads; + const size_t host_budget; // what the main run and the probe plan their host memory against const std::shared_ptr model; const double deadline_s; // after the last image, the analysis is cut (0 = never) RugnuxLiveStatus &live_status;