FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
roibin_split_stage.h
Go to the documentation of this file.
1#pragma once
2
96
97#include "stage/stage.h"
98#include "fzm_format.h"
99#include "backend/types.h"
100#include <array>
101#include <cstdint>
102#include <cstring>
103#include <stdexcept>
104#include <string>
105#include <unordered_map>
106#include <vector>
107
108namespace fz {
109
111struct RoiPeak {
112 uint32_t z;
113 uint16_t x;
114 uint16_t y;
115};
116static_assert(sizeof(RoiPeak) == 8, "RoiPeak must be 8 bytes to match the .roi format");
117
123template <typename TData>
124class ROIBinSplitStage : public Stage {
125public:
126 ROIBinSplitStage() = default;
127 ~ROIBinSplitStage() override = default;
128
129 // ── Configuration ───────────────────────────────────────────────────────
130
133 void setPeaksFile(const std::string& path);
134
136 void setPeaks(const std::vector<RoiPeak>& peaks);
137
139 void setRoiHalfWidth(uint32_t hw) { half_width_ = hw; }
140 uint32_t getRoiHalfWidth() const { return half_width_; }
141
144 void setBinFactor(uint32_t b) {
145 if (b == 0) throw std::runtime_error("ROIBinSplit: bin_factor must be >= 1");
146 bin_ = b;
147 }
148 uint32_t getBinFactor() const { return bin_; }
149
160 void setDims(const std::array<size_t, 3>& dims) override {
161 if (dims_from_header_ || dims[0] == 0) return;
162 dims_ = dims;
163 }
164 void setDims(size_t x, size_t y = 1, size_t z = 1) {
165 setDims(std::array<size_t, 3>{x, y, z});
166 }
167
168 size_t getNumPeaks() const { return peaks_.size(); }
169 size_t getBoxArea() const { return size_t(2 * half_width_ + 1) * (2 * half_width_ + 1); }
170 size_t getBgNx() const { return (dims_[0] + bin_ - 1) / bin_; }
171 size_t getBgNy() const { return (dims_[1] + bin_ - 1) / bin_; }
172 size_t getBgCount() const { return getBgNx() * getBgNy() * dims_[2]; }
173 size_t getRoiCount() const { return peaks_.size() * getBoxArea(); }
174
178 double getRoiOverlapFraction() const { return overlap_frac_; }
179
180 // ── Stage control ───────────────────────────────────────────────────────
181 void setInverse(bool inv) override { is_inverse_ = inv; }
182 bool isInverse() const override { return is_inverse_; }
183
184 // ── Port model ──────────────────────────────────────────────────────────
185 size_t getNumInputs() const override { return is_inverse_ ? 3 : 1; }
186 size_t getNumOutputs() const override { return is_inverse_ ? 1 : 3; }
187
188 std::vector<std::string> getOutputNames() const override {
189 return is_inverse_ ? std::vector<std::string>{"output"}
190 : std::vector<std::string>{"roi", "bg", "peaks"};
191 }
192
193 std::string getName() const override { return "ROIBinSplit"; }
194
195 uint16_t getStageTypeId() const override {
196 return static_cast<uint16_t>(StageType::ROIBIN_SPLIT);
197 }
198
199 uint8_t getOutputDataType(size_t output_index) const override {
200 if (is_inverse_) return elemType();
201 switch (output_index) {
202 case 0: return elemType(); // roi
203 case 1: return elemType(); // bg
204 default: return static_cast<uint8_t>(DataType::UINT8); // peaks
205 }
206 }
207 uint8_t getInputDataType(size_t input_index) const override {
208 if (!is_inverse_) return elemType();
209 switch (input_index) {
210 case 0: return elemType();
211 case 1: return elemType();
212 default: return static_cast<uint8_t>(DataType::UINT8);
213 }
214 }
215
216 // ── Execution ───────────────────────────────────────────────────────────
218 fz::stream_t stream,
219 MemoryPool* pool,
220 const std::vector<void*>& inputs,
221 const std::vector<void*>& outputs,
222 const std::vector<size_t>& sizes
223 ) override;
224
227 void onFinalize(size_t estimated_inlen, MemoryPool* pool) override;
228
229 size_t estimateDeviceFootprintBytes(size_t /*inlen*/) const override {
230 return peaks_.size() * sizeof(RoiPeak);
231 }
232
233 // ── Size estimation ─────────────────────────────────────────────────────
234 std::vector<size_t> estimateOutputSizes(
235 const std::vector<size_t>& input_sizes
236 ) const override {
237 if (is_inverse_) {
238 return {dims_[0] * dims_[1] * dims_[2] * sizeof(TData)};
239 }
240 (void)input_sizes;
241 return {getRoiCount() * sizeof(TData),
242 getBgCount() * sizeof(TData),
243 peaks_.size() * sizeof(RoiPeak)};
244 }
245
246 std::unordered_map<std::string, size_t>
247 getActualOutputSizesByName() const override {
248 if (is_inverse_)
249 return {{"output", dims_[0] * dims_[1] * dims_[2] * sizeof(TData)}};
250 return {{"roi", getRoiCount() * sizeof(TData)},
251 {"bg", getBgCount() * sizeof(TData)},
252 {"peaks", peaks_.size() * sizeof(RoiPeak)}};
253 }
254
255 size_t getActualOutputSize(int index) const override {
256 if (is_inverse_) return (index == 0)
257 ? dims_[0] * dims_[1] * dims_[2] * sizeof(TData) : 0;
258 switch (index) {
259 case 0: return getRoiCount() * sizeof(TData);
260 case 1: return getBgCount() * sizeof(TData);
261 case 2: return peaks_.size() * sizeof(RoiPeak);
262 default: return 0;
263 }
264 }
265
266 std::vector<std::string> getRunNotes() const override;
267
268 // ── Serialization ───────────────────────────────────────────────────────
269 size_t serializeHeader(size_t, uint8_t* buf, size_t max_size) const override {
270 if (max_size < kHeaderSize) return 0;
271 size_t off = 0;
272 auto put32 = [&](uint32_t v) { std::memcpy(buf + off, &v, 4); off += 4; };
273 auto put16 = [&](uint16_t v) { std::memcpy(buf + off, &v, 2); off += 2; };
274 put32(static_cast<uint32_t>(dims_[0]));
275 put32(static_cast<uint32_t>(dims_[1]));
276 put32(static_cast<uint32_t>(dims_[2]));
277 put32(static_cast<uint32_t>(peaks_.size()));
278 put16(static_cast<uint16_t>(half_width_));
279 put16(static_cast<uint16_t>(bin_));
280 buf[off++] = elemType();
281 buf[off++] = 0; // reserved
282 return off;
283 }
284
285 void deserializeHeader(const uint8_t* buf, size_t size) override {
286 if (size < kHeaderSize) return;
287 size_t off = 0;
288 auto get32 = [&]() { uint32_t v; std::memcpy(&v, buf + off, 4); off += 4; return v; };
289 auto get16 = [&]() { uint16_t v; std::memcpy(&v, buf + off, 2); off += 2; return v; };
290 dims_[0] = get32(); dims_[1] = get32(); dims_[2] = get32();
291 dims_from_header_ = true;
292 const uint32_t npeaks = get32();
293 half_width_ = get16();
294 bin_ = get16();
295 // The peak *values* arrive on the `peaks` input port, not in this header;
296 // only the count is needed here so the sizes line up before execute().
297 peaks_.assign(npeaks, RoiPeak{0, 0, 0});
298 }
299
300 size_t getMaxHeaderSize(size_t) const override { return kHeaderSize; }
301
302 void saveState() override {
303 saved_dims_ = dims_; saved_hw_ = half_width_;
304 saved_bin_ = bin_; saved_npeaks_ = peaks_.size();
305 state_saved_ = true;
306 }
307
312 void restoreState() override {
313 if (!state_saved_) return;
314 dims_ = saved_dims_; half_width_ = saved_hw_; bin_ = saved_bin_;
315 if (peaks_.size() != saved_npeaks_) peaks_.resize(saved_npeaks_);
316 }
317
318private:
319 // 4 x uint32 (dims, npeaks) + 2 x uint16 (hw, bin) + dtype + reserved.
320 static constexpr size_t kHeaderSize = 22;
321
322 static constexpr uint8_t elemType() {
323 return static_cast<uint8_t>(sizeof(TData) == 4 ? DataType::FLOAT32
324 : DataType::FLOAT64);
325 }
326
328 void computeOverlapFraction();
329
330 bool is_inverse_ = false;
331 bool dims_from_header_ = false;
332 bool state_saved_ = false;
333 std::array<size_t, 3> dims_ = {0, 0, 1};
334 uint32_t half_width_ = 4;
335 uint32_t bin_ = 1;
336 double overlap_frac_ = 0.0;
337
338 std::vector<RoiPeak> peaks_;
339 RoiPeak* d_peaks_ = nullptr;
340
341 std::array<size_t, 3> saved_dims_ = {0, 0, 1};
342 uint32_t saved_hw_ = 4;
343 uint32_t saved_bin_ = 1;
344 size_t saved_npeaks_ = 0;
345};
346
347extern template class ROIBinSplitStage<float>;
348extern template class ROIBinSplitStage<double>;
349
350} // namespace fz
Definition mempool.h:82
Definition roibin_split_stage.h:124
size_t getActualOutputSize(int index) const override
Definition roibin_split_stage.h:255
std::vector< std::string > getOutputNames() const override
Definition roibin_split_stage.h:188
size_t serializeHeader(size_t, uint8_t *buf, size_t max_size) const override
Definition roibin_split_stage.h:269
void onFinalize(size_t estimated_inlen, MemoryPool *pool) override
void execute(fz::stream_t stream, MemoryPool *pool, const std::vector< void * > &inputs, const std::vector< void * > &outputs, const std::vector< size_t > &sizes) override
void restoreState() override
Definition roibin_split_stage.h:312
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
Definition roibin_split_stage.h:247
void setBinFactor(uint32_t b)
Definition roibin_split_stage.h:144
void setPeaks(const std::vector< RoiPeak > &peaks)
Supply peaks directly (used by tests).
void setInverse(bool inv) override
Definition roibin_split_stage.h:181
void setDims(const std::array< size_t, 3 > &dims) override
Definition roibin_split_stage.h:160
double getRoiOverlapFraction() const
Definition roibin_split_stage.h:178
uint8_t getInputDataType(size_t input_index) const override
Definition roibin_split_stage.h:207
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition roibin_split_stage.h:234
size_t getMaxHeaderSize(size_t) const override
Definition roibin_split_stage.h:300
uint16_t getStageTypeId() const override
Definition roibin_split_stage.h:195
std::vector< std::string > getRunNotes() const override
std::string getName() const override
Definition roibin_split_stage.h:193
void setPeaksFile(const std::string &path)
void setRoiHalfWidth(uint32_t hw)
ROI box half-width in pixels; the box is (2*hw+1)^2. Default 4 → 9x9.
Definition roibin_split_stage.h:139
uint8_t getOutputDataType(size_t output_index) const override
Definition roibin_split_stage.h:199
size_t estimateDeviceFootprintBytes(size_t) const override
Definition roibin_split_stage.h:229
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition roibin_split_stage.h:285
void saveState() override
Definition roibin_split_stage.h:302
Definition stage.h:31
FZM binary file format definitions — structs, enums, and helpers.
Definition dag.h:24
@ ROIBIN_SPLIT
Region-of-interest / binned-background split (ROIBIN-style dual-error-bound branching).
Definition fzm_format.h:121
Base class interface for all compression stages.
One Bragg-peak record, matching the on-disk .roi layout exactly (8 bytes).
Definition roibin_split_stage.h:111
uint16_t x
fast-axis pixel coordinate
Definition roibin_split_stage.h:113
uint16_t y
slow-axis pixel coordinate
Definition roibin_split_stage.h:114
uint32_t z
frame index (slowest axis)
Definition roibin_split_stage.h:112
Backend-neutral GPU type aliases.