DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
tpglibs::ProcessorInternalStateBufferManager< T > Class Template Reference

Manages the internal state storage buffers for a processor. More...

#include <ProcessorInternalStateBufferManager.hpp>

Collaboration diagram for tpglibs::ProcessorInternalStateBufferManager< T >:
[legend]

Public Types

using signal_t = T
 Signal type to use. Generally __m256i or std::array<int16_t, 16>;.

Public Member Functions

 ProcessorInternalStateBufferManager ()
 Constructor.
 ~ProcessorInternalStateBufferManager ()
 Destructor.
void write_to_active_buffer ()
 Write to the active buffer.
ProcessorMetricArray< signal_t > switch_buffer_and_read ()
 Read from the inactive buffer.
ProcessorMetricArray< std::array< int16_t, 16 > > switch_buffer_and_read_casted ()
 Read from the inactive buffer and cast to std::array<int16_t, 16>.
void configure_from_registry (ProcessorInternalStateNameRegistry< signal_t > *registry)
 Configure and allocate correct buffer storage given the configuration string.
void clear ()
 clear all buffers and deallocate memory.
void clear ()
ProcessorMetricArray< std::array< int16_t, 16 > > switch_buffer_and_read_casted ()
ProcessorMetricArray< std::array< int16_t, 16 > > switch_buffer_and_read_casted ()

Protected Member Functions

void allocate_buffers (size_t buffer_size)
 Allocate the correct size for the double buffer read and write buffers.
void allocate_cast_buffers (size_t)
 Allocate the correct size for the double buffer read and write buffers for the casted data.
void switch_active_buffer ()
 Switch the active buffer.
void allocate_cast_buffers (size_t n)

Private Attributes

std::vector< std::shared_ptr< signal_t > > m_internal_state_item_ptrs
 The vector of pointers to the internal state items.
ProcessorMetricArray< signal_t > m_store_buffers [2] {}
 The double buffers for storing the internal state data.
ProcessorMetricArray< std::array< int16_t, 16 > > m_cast_store_buffers [2] {}
 The double buffers for storing the internal state data casted to std::array<int16_t, 16>.
std::atomic< ProcessorMetricArray< std::array< int16_t, 16 > > * > m_cast_active_buffer { &m_cast_store_buffers[0] }
 The active buffer for the casted data.
std::atomic< ProcessorMetricArray< signal_t > * > m_write_buffer = &m_store_buffers[0]
 The write buffer pointer (buffer writer currently uses).
std::atomic< ProcessorMetricArray< signal_t > * > m_read_buffer = &m_store_buffers[0]
 The read buffer pointer (buffer reader currently uses).
std::atomic< uint16_t > m_write_seq { 0 }
 The sequence number for writes (odd=writing, even=complete).
std::atomic< uint16_t > m_last_read_seq { 0 }
 The last sequence number that was read.
size_t m_buffer_size
 size of each buffer.

Detailed Description

template<typename T>
class tpglibs::ProcessorInternalStateBufferManager< T >

Manages the internal state storage buffers for a processor.

Definition at line 32 of file ProcessorInternalStateBufferManager.hpp.

Member Typedef Documentation

◆ signal_t

template<typename T>
using tpglibs::ProcessorInternalStateBufferManager< T >::signal_t = T

Signal type to use. Generally __m256i or std::array<int16_t, 16>;.

Definition at line 36 of file ProcessorInternalStateBufferManager.hpp.

Constructor & Destructor Documentation

◆ ProcessorInternalStateBufferManager()

template<typename T>
tpglibs::ProcessorInternalStateBufferManager< T >::ProcessorInternalStateBufferManager ( )

Constructor.

Definition at line 120 of file ProcessorInternalStateBufferManager.hpp.

121{
122}

◆ ~ProcessorInternalStateBufferManager()

Destructor.

Definition at line 125 of file ProcessorInternalStateBufferManager.hpp.

126{
127 clear();
128}
void clear()
clear all buffers and deallocate memory.

Member Function Documentation

◆ allocate_buffers()

template<typename T>
void tpglibs::ProcessorInternalStateBufferManager< T >::allocate_buffers ( size_t buffer_size)
protected

Allocate the correct size for the double buffer read and write buffers.

Parameters
buffer_sizeThe size of the buffer.

Definition at line 151 of file ProcessorInternalStateBufferManager.hpp.

152{
153 for (auto& buf : m_store_buffers) {
154 buf.m_size = buffer_size;
155 buf.m_data = static_cast<T*>(_mm_malloc(buf.m_size * sizeof(T), alignof(T)));
156 }
157}
Manages the internal state storage buffers for a processor.
ProcessorMetricArray< signal_t > m_store_buffers[2]
The double buffers for storing the internal state data.

◆ allocate_cast_buffers() [1/2]

void tpglibs::ProcessorInternalStateBufferManager< __m256i >::allocate_cast_buffers ( size_t n)
inlineprotected

Definition at line 257 of file ProcessorInternalStateBufferManager.hpp.

258{
259 for (auto& buf : m_cast_store_buffers) {
260 buf.m_size = n;
261 buf.m_data = static_cast<std::array<int16_t, 16>*>(_mm_malloc(n * sizeof(std::array<int16_t, 16>), 32));
262 }
264}
ProcessorMetricArray< std::array< int16_t, 16 > > m_cast_store_buffers[2]
The double buffers for storing the internal state data casted to std::array<int16_t,...
std::atomic< ProcessorMetricArray< std::array< int16_t, 16 > > * > m_cast_active_buffer
The active buffer for the casted data.

◆ allocate_cast_buffers() [2/2]

template<typename T>
void tpglibs::ProcessorInternalStateBufferManager< T >::allocate_cast_buffers ( size_t )
inlineprotected

Allocate the correct size for the double buffer read and write buffers for the casted data.

Parameters
buffer_sizeThe size of the buffer.

Definition at line 84 of file ProcessorInternalStateBufferManager.hpp.

84{}; // Do nothing for the generic template

◆ clear() [1/2]

void tpglibs::ProcessorInternalStateBufferManager< __m256i >::clear ( )
inline

Definition at line 218 of file ProcessorInternalStateBufferManager.hpp.

219{
220 // free double buffer for temp values
221 for (auto& buf : m_store_buffers) {
222 _mm_free(buf.m_data);
223 }
224 // free double buffer for casted values
225 for (auto& buf : m_cast_store_buffers) {
226 _mm_free(buf.m_data);
227 }
228}

◆ clear() [2/2]

template<typename T>
void tpglibs::ProcessorInternalStateBufferManager< T >::clear ( )
inline

clear all buffers and deallocate memory.

Definition at line 66 of file ProcessorInternalStateBufferManager.hpp.

67 {
68 for (auto& buf : m_store_buffers) {
69 _mm_free(buf.m_data);
70 }
71 }

◆ configure_from_registry()

template<typename T>
void tpglibs::ProcessorInternalStateBufferManager< T >::configure_from_registry ( ProcessorInternalStateNameRegistry< signal_t > * registry)

Configure and allocate correct buffer storage given the configuration string.

Parameters
registryThe registry object of internal state names.

Definition at line 132 of file ProcessorInternalStateBufferManager.hpp.

133{
134 // obtain the number of internal state items
135 auto num_items = registry->get_number_of_requested_internal_states();
138 // Writer starts with buffer 0, reader starts with buffer 1 (they must be different!)
142 // reset sequence counters
145 // obtain the pointers to the internal state items
146 m_internal_state_item_ptrs = registry->get_all_requested_internal_state_item_ptrs();
147}
std::atomic< ProcessorMetricArray< signal_t > * > m_write_buffer
The write buffer pointer (buffer writer currently uses).
std::atomic< uint16_t > m_last_read_seq
The last sequence number that was read.
std::atomic< uint16_t > m_write_seq
The sequence number for writes (odd=writing, even=complete).
std::vector< std::shared_ptr< signal_t > > m_internal_state_item_ptrs
The vector of pointers to the internal state items.
void allocate_cast_buffers(size_t)
Allocate the correct size for the double buffer read and write buffers for the casted data.
std::atomic< ProcessorMetricArray< signal_t > * > m_read_buffer
The read buffer pointer (buffer reader currently uses).
void allocate_buffers(size_t buffer_size)
Allocate the correct size for the double buffer read and write buffers.

◆ switch_active_buffer()

template<typename T>
void tpglibs::ProcessorInternalStateBufferManager< T >::switch_active_buffer ( )
protected

Switch the active buffer.

◆ switch_buffer_and_read()

template<typename T>
ProcessorMetricArray< typename ProcessorInternalStateBufferManager< T >::signal_t > tpglibs::ProcessorInternalStateBufferManager< T >::switch_buffer_and_read ( )

Read from the inactive buffer.

Returns
The data read from the inactive buffer.

Definition at line 185 of file ProcessorInternalStateBufferManager.hpp.

186{
187 // Wait until no write is in progress
189 do {
191 } while (current_write_seq & 1); // spin if writer is mid-write (odd seq number)
192
193 // Check if there's new data since last read
195
197 // New data available - swap the buffers between reader and writer
200
201 // Swap: reader gets what writer just finished, writer gets what reader was using
204
205 // Update last read sequence
207 }
208
209 // Return data from read buffer
211 return *read_ptr;
212}

◆ switch_buffer_and_read_casted() [1/3]

ProcessorMetricArray< std::array< int16_t, 16 > > tpglibs::ProcessorInternalStateBufferManager< std::array< int16_t, 16 > >::switch_buffer_and_read_casted ( )
inline

Definition at line 269 of file ProcessorInternalStateBufferManager.hpp.

270{
271 // First, get the raw data using switch_buffer_and_read
273
274 // For std::array<int16_t, 16>, the cast is trivial - just return the data as-is
275 return raw_data;
276}
ProcessorMetricArray< signal_t > switch_buffer_and_read()
Read from the inactive buffer.

◆ switch_buffer_and_read_casted() [2/3]

ProcessorMetricArray< std::array< int16_t, 16 > > tpglibs::ProcessorInternalStateBufferManager< __m256i >::switch_buffer_and_read_casted ( )
inline

Definition at line 233 of file ProcessorInternalStateBufferManager.hpp.

234{
235 // First, get the raw data using switch_buffer_and_read
237
239
240 // Cast each __m256i to std::array<int16_t, 16>
241 for (size_t i = 0; i < raw_data.m_size; ++i) {
242 // Cast and save to cast buffer
243 _mm256_store_si256(reinterpret_cast<__m256i*>(cast_free->m_data[i].data()), raw_data.m_data[i]);
244 }
245
246 // Flip the active cast buffer
247
250
251 return *cast_free;
252}

◆ switch_buffer_and_read_casted() [3/3]

template<typename T>
ProcessorMetricArray< std::array< int16_t, 16 > > tpglibs::ProcessorInternalStateBufferManager< T >::switch_buffer_and_read_casted ( )

Read from the inactive buffer and cast to std::array<int16_t, 16>.

◆ write_to_active_buffer()

template<typename T>
void tpglibs::ProcessorInternalStateBufferManager< T >::write_to_active_buffer ( )

Write to the active buffer.

Parameters
dataThe data to write.

Definition at line 161 of file ProcessorInternalStateBufferManager.hpp.

162{
163 // Increment seq to indicate write start (becomes odd)
165
167
168 // Write to write buffer
169 for (size_t i = 0; i < m_internal_state_item_ptrs.size(); i++) {
170 // Check for nullptr before dereferencing (handles invalid state names)
171 if (m_internal_state_item_ptrs[i] != nullptr) {
173 } else {
174 // If pointer is null, write a zeroed value
175 write_ptr->m_data[i] = T{};
176 }
177 }
178
179 // Increment seq to indicate write complete (becomes even)
181}

Member Data Documentation

◆ m_buffer_size

template<typename T>
size_t tpglibs::ProcessorInternalStateBufferManager< T >::m_buffer_size
private

size of each buffer.

Definition at line 115 of file ProcessorInternalStateBufferManager.hpp.

◆ m_cast_active_buffer

template<typename T>
std::atomic<ProcessorMetricArray<std::array<int16_t, 16> >*> tpglibs::ProcessorInternalStateBufferManager< T >::m_cast_active_buffer { &m_cast_store_buffers[0] }
private

The active buffer for the casted data.

Definition at line 100 of file ProcessorInternalStateBufferManager.hpp.

◆ m_cast_store_buffers

template<typename T>
ProcessorMetricArray<std::array<int16_t, 16> > tpglibs::ProcessorInternalStateBufferManager< T >::m_cast_store_buffers[2] {}
private

The double buffers for storing the internal state data casted to std::array<int16_t, 16>.

Definition at line 97 of file ProcessorInternalStateBufferManager.hpp.

◆ m_internal_state_item_ptrs

template<typename T>
std::vector<std::shared_ptr<signal_t> > tpglibs::ProcessorInternalStateBufferManager< T >::m_internal_state_item_ptrs
private

The vector of pointers to the internal state items.

Definition at line 91 of file ProcessorInternalStateBufferManager.hpp.

◆ m_last_read_seq

template<typename T>
std::atomic<uint16_t> tpglibs::ProcessorInternalStateBufferManager< T >::m_last_read_seq { 0 }
private

The last sequence number that was read.

Definition at line 112 of file ProcessorInternalStateBufferManager.hpp.

◆ m_read_buffer

template<typename T>
std::atomic<ProcessorMetricArray<signal_t>*> tpglibs::ProcessorInternalStateBufferManager< T >::m_read_buffer = &m_store_buffers[0]
private

The read buffer pointer (buffer reader currently uses).

Definition at line 106 of file ProcessorInternalStateBufferManager.hpp.

◆ m_store_buffers

template<typename T>
ProcessorMetricArray<signal_t> tpglibs::ProcessorInternalStateBufferManager< T >::m_store_buffers[2] {}
private

The double buffers for storing the internal state data.

Definition at line 94 of file ProcessorInternalStateBufferManager.hpp.

◆ m_write_buffer

template<typename T>
std::atomic<ProcessorMetricArray<signal_t>*> tpglibs::ProcessorInternalStateBufferManager< T >::m_write_buffer = &m_store_buffers[0]
private

The write buffer pointer (buffer writer currently uses).

Definition at line 103 of file ProcessorInternalStateBufferManager.hpp.

◆ m_write_seq

template<typename T>
std::atomic<uint16_t> tpglibs::ProcessorInternalStateBufferManager< T >::m_write_seq { 0 }
private

The sequence number for writes (odd=writing, even=complete).

Definition at line 109 of file ProcessorInternalStateBufferManager.hpp.


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