Build Packages / Create release (push) Successful in 7s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 4m56s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 9m10s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 13m51s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 16m56s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 20m59s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 21m25s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 21m17s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 14m14s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m59s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 25m48s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 15m52s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 19m33s
Build Packages / Generate python client (push) Successful in 29s
Build Packages / Build documentation (push) Successful in 1m19s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 20m49s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 25m31s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 25m24s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 26m5s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 22m19s
Build Packages / Unit tests (push) Failing after 2h28m24s
Build Packages / Validation Battery (push) Successful in 40m33s
* Rugnux: Processes a collection live while jfjoch_broker writes it (`rugnux <master.h5> <broker URL>`). * Rugnux: Processing starts on the first images and confirms its choices once all are in, so analysis can begin during collection. * Rugnux: Faster processing, especially on servers with several GPUs and CPU sockets and on computers without a GPU (`--no-gpu`, macOS). * Rugnux: Uses much less GPU memory, so large data sets stay on the GPU of smaller cards, and a data set too large for the card is processed on the CPU instead. * Rugnux: On macOS, Bragg integration runs on the Apple GPU. * Rugnux: Better handling of twinning and pseudo-symmetry, with the twin hypothesis written as `<prefix>_twin_<SG>.mtz`, and a wrong `--model` no longer confirms the data or decides the hand. * Rugnux: `--model` accepts predicted (AlphaFold) models and falls back to molecular replacement when the model is not placed as a rigid body (experimental `--mr` forces it), settling space group, hand and twinning and writing a different answer as `<prefix>_mr.mtz`. * Rugnux: Molecular replacement is faster and settles the space group in more cases with several copies in the asymmetric unit. * Rugnux: Deposition-ready mmCIF, a richer report, and numbered steps on the terminal with the full log in `<prefix>_log.txt`; `_P1.mtz`, `--estimate-beam-center` and `--beam-center-search` are removed. * Rugnux: The beam-stop shadow is found correctly on tilted detectors. * Rugnux: Stills files that record a stationary goniometer axis get the default stills geometry refinement. * jfjoch_viewer: "Analyze dataset" uses the same defaults as a bare `rugnux <file>`, and shows the results of processed CBF, marCCD and SMV sweeps. * jfjoch_viewer: The Files panel refreshes on F5 and every few seconds, and the plot type and other selections stay put during live collection. * jfjoch_writer: Memory stays bounded and old data files are closed efficiently on long runs, any failure ends the run in an error state the broker is told about, and the master file appears only after its data files; the ZeroMQ pusher logs when a stalled writer makes it drop images. * Fixes: The TCP image pusher no longer blocks the broker's receiver, GPU out-of-memory failures stop with a clear message, and the XDS plugin masks module and chip edges. --------- Co-authored-by: Filip Leonarski <jungfrau@filips-mac-mini.home> Reviewed-on: #85 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
147 lines
4.6 KiB
C++
147 lines
4.6 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "ReceiveRingBuffer.h"
|
|
|
|
#include <algorithm>
|
|
#include <optional>
|
|
|
|
struct ReceiveRingBuffer::State {
|
|
// Uninitialised on purpose: a ring that is never filled should not cost its capacity in
|
|
// resident memory.
|
|
std::unique_ptr<uint8_t[]> memory;
|
|
const size_t capacity;
|
|
|
|
std::mutex m;
|
|
std::condition_variable cv;
|
|
|
|
// Live regions in arrival order. Only the oldest can give back space, so a region released out of
|
|
// turn is marked and waits.
|
|
struct Slot {
|
|
size_t offset;
|
|
size_t size;
|
|
bool released = false;
|
|
};
|
|
std::deque<Slot> slots;
|
|
// Next byte to hand out. The ring is empty when slots is empty; the oldest live byte is then
|
|
// slots.front().offset.
|
|
size_t tail = 0;
|
|
|
|
size_t utilization = 0;
|
|
size_t max_utilization = 0;
|
|
bool stopped = false;
|
|
|
|
explicit State(size_t in_capacity)
|
|
: memory(in_capacity > 0 ? std::make_unique_for_overwrite<uint8_t[]>(in_capacity) : nullptr),
|
|
capacity(in_capacity) {}
|
|
|
|
// Where a region of `size` bytes would start, or nothing if it does not fit right now. Called
|
|
// under the mutex.
|
|
std::optional<size_t> FindSpace(size_t size) const {
|
|
if (slots.empty())
|
|
return 0;
|
|
const size_t head = slots.front().offset;
|
|
// tail has come round to head: treated as full. (After a wrap the piece between the old tail
|
|
// and the end is free, but it is behind the live data and not worth tracking.)
|
|
if (tail == head)
|
|
return std::nullopt;
|
|
if (tail > head) {
|
|
// Live data is [head, tail): free space is behind it to the end, or before it at the start.
|
|
if (capacity - tail >= size)
|
|
return tail;
|
|
if (head >= size)
|
|
return 0;
|
|
return std::nullopt;
|
|
}
|
|
// Wrapped: live data is [head, capacity) + [0, tail), free space is [tail, head).
|
|
if (head - tail >= size)
|
|
return tail;
|
|
return std::nullopt;
|
|
}
|
|
|
|
void Release(size_t offset) {
|
|
std::unique_lock ul(m);
|
|
for (auto &s: slots) {
|
|
if (s.offset == offset) {
|
|
s.released = true;
|
|
break;
|
|
}
|
|
}
|
|
while (!slots.empty() && slots.front().released) {
|
|
utilization -= slots.front().size;
|
|
slots.pop_front();
|
|
}
|
|
if (slots.empty())
|
|
tail = 0;
|
|
cv.notify_all();
|
|
}
|
|
|
|
void ReleaseHeap(size_t size) {
|
|
std::unique_lock ul(m);
|
|
utilization -= size;
|
|
}
|
|
};
|
|
|
|
ReceiveRingBuffer::ReceiveRingBuffer(size_t capacity) : state(std::make_shared<State>(capacity)) {}
|
|
|
|
std::shared_ptr<ReceiveBufferRegion> ReceiveRingBuffer::Allocate(size_t size) {
|
|
// Nothing to hold, and a zero-length slot would share its offset with the next real one.
|
|
if (size == 0)
|
|
return std::make_shared<ReceiveBufferRegion>();
|
|
|
|
std::unique_lock ul(state->m);
|
|
|
|
if (size > state->capacity) {
|
|
// Heap fallback: counted, not bounded.
|
|
state->utilization += size;
|
|
state->max_utilization = std::max(state->max_utilization, state->utilization);
|
|
auto *region = new ReceiveBufferRegion{new uint8_t[size], size};
|
|
return std::shared_ptr<ReceiveBufferRegion>(region, [s = state](ReceiveBufferRegion *r) {
|
|
delete[] r->data;
|
|
s->ReleaseHeap(r->size);
|
|
delete r;
|
|
});
|
|
}
|
|
|
|
std::optional<size_t> offset;
|
|
state->cv.wait(ul, [&] {
|
|
if (state->stopped)
|
|
return true;
|
|
offset = state->FindSpace(size);
|
|
return offset.has_value();
|
|
});
|
|
if (!offset)
|
|
return nullptr;
|
|
|
|
state->slots.push_back({*offset, size});
|
|
state->tail = *offset + size;
|
|
state->utilization += size;
|
|
state->max_utilization = std::max(state->max_utilization, state->utilization);
|
|
|
|
auto *region = new ReceiveBufferRegion{state->memory.get() + *offset, size};
|
|
return std::shared_ptr<ReceiveBufferRegion>(region, [s = state, off = *offset](ReceiveBufferRegion *r) {
|
|
s->Release(off);
|
|
delete r;
|
|
});
|
|
}
|
|
|
|
void ReceiveRingBuffer::Stop() {
|
|
std::unique_lock ul(state->m);
|
|
state->stopped = true;
|
|
state->cv.notify_all();
|
|
}
|
|
|
|
size_t ReceiveRingBuffer::GetCapacity() const {
|
|
return state->capacity;
|
|
}
|
|
|
|
size_t ReceiveRingBuffer::GetUtilization() const {
|
|
std::unique_lock ul(state->m);
|
|
return state->utilization;
|
|
}
|
|
|
|
size_t ReceiveRingBuffer::GetMaxUtilization() const {
|
|
std::unique_lock ul(state->m);
|
|
return state->max_utilization;
|
|
}
|