DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
AVXFrugalPedestalSubtractProcessor.cpp
Go to the documentation of this file.
1
11#include <mm_malloc.h>
12
13namespace tpglibs {
14
15REGISTER_AVXPROCESSOR_CREATOR("AVXFrugalPedestalSubtractProcessor", AVXFrugalPedestalSubtractProcessor)
16
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}
26
28 for (auto buf : m_metric_store_buffers) {
29 _mm_free(buf.m_data);
30 }
31}
32
33void AVXFrugalPedestalSubtractProcessor::configure(const nlohmann::json& config, const int16_t* plane_numbers) {
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}
38
39__m256i AVXFrugalPedestalSubtractProcessor::process(const __m256i& signal) {
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}
72
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}
89
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}
108
110 if (!m_collect_metric_flag) return {};
111 return {"m_pedestal", "m_accum"};
112}
113
114} // namespace tpglibs
#define REGISTER_AVXPROCESSOR_CREATOR(processor_name, processor_class)
Factory registration macro.
AVX signal processor: Estimates the pedestal and subtracts.
void save_metric_to_store_buffer() override
Save metrics to store buffer.
uint64_t m_sample_period
Adjustable period for storing metric to buffer, in terms of number of time process happens (sampling ...
void configure(const nlohmann::json &config, const int16_t *plane_numbers) override
Configure the accumulation limit according to plane number.
__m256i process(const __m256i &signal) override
Estimate the pedestal using the given signal and subtract.
ProcessorMetricArray< __m256i > read_from_metric_store_buffer() override
Read metrics from store buffer.
__m256i m_pedestal
Vector of estimated pedestals for each channel.
std::atomic< ProcessorMetricArray< __m256i > * > m_active_buffer
virtual std::vector< std::string > get_metric_items() override
returns the metrics being recorded and can be read by this processor
int16_t m_accum_limit
Count limit before committing to a pedestal shift.
__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.
Dynamic array of processor metrics, templated on signal type.