DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
tpglibs::AVXFrugalPedestalSubtractProcessor Class Reference

AVX signal processor: Estimates the pedestal and subtracts. More...

#include <AVXFrugalPedestalSubtractProcessor.hpp>

Inheritance diagram for tpglibs::AVXFrugalPedestalSubtractProcessor:
[legend]
Collaboration diagram for tpglibs::AVXFrugalPedestalSubtractProcessor:
[legend]

Public Member Functions

 AVXFrugalPedestalSubtractProcessor ()
 Allocate and initialize dual buffers.
 
 ~AVXFrugalPedestalSubtractProcessor () noexcept
 Release buffer memory.
 
__m256i process (const __m256i &signal) override
 Estimate the pedestal using the given signal and subtract.
 
void configure (const nlohmann::json &config, const int16_t *plane_numbers) override
 Configure the accumulation limit according to plane number.
 
void save_metric_to_store_buffer () override
 Save metrics to store buffer.
 
virtual std::vector< std::string > get_metric_items () override
 returns the metrics being recorded and can be read by this processor
 
ProcessorMetricArray< __m256i > read_from_metric_store_buffer () override
 Read metrics from store buffer.
 
- Public Member Functions inherited from tpglibs::AVXProcessor
void save_metric_to_store_buffer () override
 Save metrics to store buffer; no-op for basic AVXProcessor.
 
ProcessorMetricArray< __m256i > read_from_metric_store_buffer () override
 Read metrics from store buffer; returns empty for basic AVXProcessor.
 
- Public Member Functions inherited from tpglibs::AbstractProcessor< __m256i >
virtual ~AbstractProcessor ()=default
 
void set_next_processor (std::shared_ptr< AbstractProcessor< __m256i > > next_processor)
 Setter for next processor.
 
virtual void attach_to_metric_collector (ProcessorMetricCollector< signal_type_t > &collector, size_t pipeline_id)
 Register this processor and next processor with the metric collector.
 

Protected Attributes

__m256i m_pedestal = _mm256_set1_epi16(0x4000)
 Vector of estimated pedestals for each channel.
 
__m256i m_accum = _mm256_setzero_si256()
 Vector of counts that a channel's signal was above or below m_pedestal.
 
int16_t m_accum_limit {10}
 Count limit before committing to a pedestal shift.
 
uint64_t m_sample_period {512}
 Adjustable period for storing metric to buffer, in terms of number of time process happens (sampling period)
 
uint64_t m_samples {0}
 
bool m_collect_metric_flag {false}
 

Private Attributes

ProcessorMetricArray< __m256i > m_metric_store_buffers [2] {}
 
std::atomic< ProcessorMetricArray< __m256i > * > m_active_buffer = &m_metric_store_buffers[0]
 
std::atomic< uint16_t > seq {0}
 

Additional Inherited Members

- Public Types inherited from tpglibs::AbstractProcessor< __m256i >
using signal_type_t
 Signal type to process on. General __m256i.
 

Detailed Description

AVX signal processor: Estimates the pedestal and subtracts.

Given a history of signals, this estimates the pedestal by shifting the current estimate when it is wrong in the same direction m_accum_limit times. For example, if the input signal is greater (less) than the estimated pedestal 10 (configurable) times in a row, then increment (decrement) the pedestal.

Definition at line 25 of file AVXFrugalPedestalSubtractProcessor.hpp.

Constructor & Destructor Documentation

◆ AVXFrugalPedestalSubtractProcessor()

tpglibs::AVXFrugalPedestalSubtractProcessor::AVXFrugalPedestalSubtractProcessor ( )

Allocate and initialize dual buffers.

Definition at line 17 of file AVXFrugalPedestalSubtractProcessor.cpp.

17 {
18 // Initialize the static arrays in metric store buffer
19 for (auto& buf : m_metric_store_buffers) {
20 buf.m_size = 2; // Storing m_pedetal and m_acuum for this type of processor
21 buf.m_data = static_cast<__m256i*>(
22 _mm_malloc(buf.m_size * sizeof(__m256i), alignof(__m256i))
23 );
24 }
25}

◆ ~AVXFrugalPedestalSubtractProcessor()

tpglibs::AVXFrugalPedestalSubtractProcessor::~AVXFrugalPedestalSubtractProcessor ( )
noexcept

Release buffer memory.

Definition at line 27 of file AVXFrugalPedestalSubtractProcessor.cpp.

27 {
28 for (auto buf : m_metric_store_buffers) {
29 _mm_free(buf.m_data);
30 }
31}

Member Function Documentation

◆ configure()

void tpglibs::AVXFrugalPedestalSubtractProcessor::configure ( const nlohmann::json & config,
const int16_t * plane_numbers )
overridevirtual

Configure the accumulation limit according to plane number.

Parameters
configJSON config for the accumulation limits per plane.
plane_numbersArray of plane numbers. Gives the channels to apply the accumulation limit.

Implements tpglibs::AbstractProcessor< __m256i >.

Definition at line 33 of file AVXFrugalPedestalSubtractProcessor.cpp.

33 {
34 m_accum_limit = config["accum_limit"];
35 if (config.contains("metric_collect_time_sample_period")) m_sample_period = config["metric_collect_time_sample_period"];
36 if (config.contains("metric_collect_toggle_state")) m_collect_metric_flag = config["metric_collect_toggle_state"];
37}
uint64_t m_sample_period
Adjustable period for storing metric to buffer, in terms of number of time process happens (sampling ...
int16_t m_accum_limit
Count limit before committing to a pedestal shift.

◆ get_metric_items()

std::vector< std::string > tpglibs::AVXFrugalPedestalSubtractProcessor::get_metric_items ( )
overridevirtual

returns the metrics being recorded and can be read by this processor

Returns
a vector of two strings: m_accum and m_pedestal

Reimplemented from tpglibs::AbstractProcessor< __m256i >.

Definition at line 109 of file AVXFrugalPedestalSubtractProcessor.cpp.

109 {
110 if (!m_collect_metric_flag) return {};
111 return {"m_pedestal", "m_accum"};
112}

◆ process()

__m256i tpglibs::AVXFrugalPedestalSubtractProcessor::process ( const __m256i & signal)
overridevirtual

Estimate the pedestal using the given signal and subtract.

Parameters
signalA vector of channel signals.
Returns
The input signal minus the estimated pedestal.

Reimplemented from tpglibs::AVXProcessor.

Definition at line 39 of file AVXFrugalPedestalSubtractProcessor.cpp.

39 {
40 // save metric
41
43
44 // Find the channels that are above or below the pedestal.
45 __m256i is_gt = _mm256_cmpgt_epi16(signal, m_pedestal);
46 __m256i is_lt = _mm256_cmpgt_epi16(m_pedestal, signal);
47
48 // Update m_accum.
49 __m256i to_add = _mm256_setzero_si256(); // Assumes equal to pedestal.
50 to_add = _mm256_blendv_epi8(to_add, _mm256_set1_epi16(1), is_gt); // Set the above pedestal case.
51 to_add = _mm256_blendv_epi8(to_add, _mm256_set1_epi16(-1), is_lt); // Set the below pedestal case.
52
53 m_accum = _mm256_add_epi16(m_accum, to_add);
54
55 // Check the accum limit condition.
56 is_gt = _mm256_cmpgt_epi16(m_accum, _mm256_set1_epi16(m_accum_limit)); // m_accum > +limit.
57 is_lt = _mm256_cmpgt_epi16(_mm256_set1_epi16(-1*m_accum_limit), m_accum); // m_accum < -limit = -limit > m_accum.
58
59 to_add = _mm256_setzero_si256();
60 to_add = _mm256_blendv_epi8(to_add, _mm256_set1_epi16(1), is_gt);
61 to_add = _mm256_blendv_epi8(to_add, _mm256_set1_epi16(-1), is_lt);
62
63 // Update pedestal.
64 m_pedestal = _mm256_adds_epi16(m_pedestal, to_add);
65
66 // Reset too high/low m_accum channels.
67 __m256i need_reset = _mm256_or_si256(is_lt, is_gt);
68 m_accum = _mm256_blendv_epi8(m_accum, _mm256_setzero_si256(), need_reset);
69
70 return AVXProcessor::process(_mm256_sub_epi16(signal, m_pedestal));
71}
void save_metric_to_store_buffer() override
Save metrics to store buffer.
__m256i m_pedestal
Vector of estimated pedestals for each channel.
__m256i m_accum
Vector of counts that a channel's signal was above or below m_pedestal.
virtual __m256i process(const __m256i &signal) override
Simple signal pass-through on __m256i type.

◆ read_from_metric_store_buffer()

ProcessorMetricArray< __m256i > tpglibs::AVXFrugalPedestalSubtractProcessor::read_from_metric_store_buffer ( )
overridevirtual

Read metrics from store buffer.

Reimplemented from tpglibs::AbstractProcessor< __m256i >.

Definition at line 90 of file AVXFrugalPedestalSubtractProcessor.cpp.

90 {
91 // Wait until no write is in progress.
92 if (!m_collect_metric_flag) return {};
93 uint16_t start_seq;
94 do {
95 start_seq = seq.load(std::memory_order_acquire);
96 } while (start_seq & 1); // spin if writer is mid-write
97
98 // Swap the active buffer so writer will go to the other one.
99 auto current_active = m_active_buffer.load(std::memory_order_acquire);
100 auto new_active = (current_active == &m_metric_store_buffers[0])
103 m_active_buffer.store(new_active, std::memory_order_release);
104
105 // Now it's safe to read from the previous active buffer.
106 return *current_active;
107}
std::atomic< ProcessorMetricArray< __m256i > * > m_active_buffer

◆ save_metric_to_store_buffer()

void tpglibs::AVXFrugalPedestalSubtractProcessor::save_metric_to_store_buffer ( )
overridevirtual

Save metrics to store buffer.

Reimplemented from tpglibs::AbstractProcessor< __m256i >.

Definition at line 73 of file AVXFrugalPedestalSubtractProcessor.cpp.

73 {
74 // Store is expected to happen at a much higher frequency than read, for example,
75 // can be store after each processing
76 // Therefore, "save" should have no interrupt or wait
77
78 //set seq
79 seq.fetch_add(1, std::memory_order_release);
80 auto free_ptr = m_active_buffer.load(std::memory_order_acquire);
81 // write to free buffer
82
83 free_ptr->m_data[0] = m_pedestal;
84 free_ptr->m_data[1] = m_accum;
85
86 //set seq to indicate write end
87 seq.fetch_add(1, std::memory_order_release);
88}

Member Data Documentation

◆ m_accum

__m256i tpglibs::AVXFrugalPedestalSubtractProcessor::m_accum = _mm256_setzero_si256()
protected

Vector of counts that a channel's signal was above or below m_pedestal.

Definition at line 31 of file AVXFrugalPedestalSubtractProcessor.hpp.

◆ m_accum_limit

int16_t tpglibs::AVXFrugalPedestalSubtractProcessor::m_accum_limit {10}
protected

Count limit before committing to a pedestal shift.

Definition at line 34 of file AVXFrugalPedestalSubtractProcessor.hpp.

34{10};

◆ m_active_buffer

std::atomic<ProcessorMetricArray<__m256i>*> tpglibs::AVXFrugalPedestalSubtractProcessor::m_active_buffer = &m_metric_store_buffers[0]
private

Definition at line 44 of file AVXFrugalPedestalSubtractProcessor.hpp.

◆ m_collect_metric_flag

bool tpglibs::AVXFrugalPedestalSubtractProcessor::m_collect_metric_flag {false}
protected

Definition at line 39 of file AVXFrugalPedestalSubtractProcessor.hpp.

39{false};

◆ m_metric_store_buffers

ProcessorMetricArray<__m256i> tpglibs::AVXFrugalPedestalSubtractProcessor::m_metric_store_buffers[2] {}
private

Definition at line 42 of file AVXFrugalPedestalSubtractProcessor.hpp.

42{};

◆ m_pedestal

__m256i tpglibs::AVXFrugalPedestalSubtractProcessor::m_pedestal = _mm256_set1_epi16(0x4000)
protected

Vector of estimated pedestals for each channel.

Definition at line 28 of file AVXFrugalPedestalSubtractProcessor.hpp.

◆ m_sample_period

uint64_t tpglibs::AVXFrugalPedestalSubtractProcessor::m_sample_period {512}
protected

Adjustable period for storing metric to buffer, in terms of number of time process happens (sampling period)

Definition at line 37 of file AVXFrugalPedestalSubtractProcessor.hpp.

37{512};

◆ m_samples

uint64_t tpglibs::AVXFrugalPedestalSubtractProcessor::m_samples {0}
protected

Definition at line 38 of file AVXFrugalPedestalSubtractProcessor.hpp.

38{0};

◆ seq

std::atomic<uint16_t> tpglibs::AVXFrugalPedestalSubtractProcessor::seq {0}
private

Definition at line 46 of file AVXFrugalPedestalSubtractProcessor.hpp.

46{0};

The documentation for this class was generated from the following files: