NEST main@caf0ae8
 
Loading...
Searching...
No Matches
ring_buffer.h
Go to the documentation of this file.
1/*
2 * ring_buffer.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef RING_BUFFER_H
24#define RING_BUFFER_H
25
26// C++ includes:
27#include <array>
28#include <list>
29#include <vector>
30
31// Includes from nestkernel:
32#include "kernel_manager.h"
33#include "nest_time.h"
34#include "nest_types.h"
35
36namespace nest
37{
38
83{
84public:
85 RingBuffer();
86
93 void add_value( const long offs, const double );
94
101 void set_value( const long offs, const double );
102
109 double get_value( const long offs );
110
117 double get_value_wfr_update( const long offs );
118
124 void clear();
125
132 void resize();
133
137 size_t
138 size() const
139 {
140 return buffer_.size();
141 }
142
143private:
145 std::vector< double > buffer_;
146
154 size_t get_index_( const long d ) const;
155};
156
157inline void
158RingBuffer::add_value( const long offs, const double v )
159{
160 buffer_[ get_index_( offs ) ] += v;
161}
162
163inline void
164RingBuffer::set_value( const long offs, const double v )
165{
166 buffer_[ get_index_( offs ) ] = v;
167}
168
169inline double
170RingBuffer::get_value( const long offs )
171{
172 assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
173 assert( offs < kernel().connection_manager.get_min_delay() );
174
175 // offs == 0 is beginning of slice, but we have to
176 // take modulo into account when indexing
177 long idx = get_index_( offs );
178 double val = buffer_[ idx ];
179 buffer_[ idx ] = 0.0; // clear buffer after reading
180 return val;
181}
182
183inline double
185{
186 assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
187 assert( offs < kernel().connection_manager.get_min_delay() );
188
189 // offs == 0 is beginning of slice, but we have to
190 // take modulo into account when indexing
191 long idx = get_index_( offs );
192 double val = buffer_[ idx ];
193 return val;
194}
195
196inline size_t
197RingBuffer::get_index_( const long d ) const
198{
199 const long idx = kernel().event_delivery_manager.get_modulo( d );
200 assert( 0 <= idx );
201 assert( static_cast< size_t >( idx ) < buffer_.size() );
202 return idx;
203}
204
205
207{
208public:
209 MultRBuffer();
210
216 void add_value( const long offs, const double );
217
223 double get_value( const long offs );
224
228 void clear();
229
233 void resize();
234
238 size_t
239 size() const
240 {
241 return buffer_.size();
242 }
243
244private:
246 std::vector< double > buffer_;
247
255 size_t get_index_( const long d ) const;
256};
257
258inline void
259MultRBuffer::add_value( const long offs, const double v )
260{
261 assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
262 buffer_[ get_index_( offs ) ] *= v;
263}
264
265inline double
266MultRBuffer::get_value( const long offs )
267{
268 assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
269 assert( offs < kernel().connection_manager.get_min_delay() );
270
271 // offs == 0 is beginning of slice, but we have to
272 // take modulo into account when indexing
273 long idx = get_index_( offs );
274 double val = buffer_[ idx ];
275 buffer_[ idx ] = 0.0; // clear buffer after reading
276 return val;
277}
278
279inline size_t
280MultRBuffer::get_index_( const long d ) const
281{
282 const long idx = kernel().event_delivery_manager.get_modulo( d );
283 assert( 0 <= idx and static_cast< size_t >( idx ) < buffer_.size() );
284 return idx;
285}
286
287
289{
290public:
292
299 void append_value( const long offs, const double );
300
301 std::list< double >& get_list( const long offs );
302
307 void clear();
308
315 void resize();
316
320 size_t
321 size() const
322 {
323 return buffer_.size();
324 }
325
326private:
328 std::vector< std::list< double > > buffer_;
329
337 size_t get_index_( const long d ) const;
338};
339
340inline void
341ListRingBuffer::append_value( const long offs, const double v )
342{
343 buffer_[ get_index_( offs ) ].push_back( v );
344}
345
346inline std::list< double >&
347ListRingBuffer::get_list( const long offs )
348{
349 assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
350 assert( offs < kernel().connection_manager.get_min_delay() );
351
352 // offs == 0 is beginning of slice, but we have to
353 // take modulo into account when indexing
354 long idx = get_index_( offs );
355 return buffer_[ idx ];
356}
357
358inline size_t
359ListRingBuffer::get_index_( const long d ) const
360{
361 const long idx = kernel().event_delivery_manager.get_modulo( d );
362 assert( 0 <= idx );
363 assert( static_cast< size_t >( idx ) < buffer_.size() );
364 return idx;
365}
366
367
368template < unsigned int num_channels >
370{
371public:
373
374 void add_value( const size_t slot, const size_t channel, const double value );
375
376 const std::array< double, num_channels >& get_values_all_channels( const size_t slot ) const;
377 void reset_values_all_channels( const size_t slot );
378
379 void clear();
380
381 void resize();
382
383 size_t size() const;
384
385private:
392 std::vector< std::array< double, num_channels > > buffer_;
393};
394
395template < unsigned int num_channels >
396inline void
398{
399 assert( slot < buffer_.size() );
400 buffer_[ slot ].fill( 0.0 );
401}
402
403template < unsigned int num_channels >
404inline void
405MultiChannelInputBuffer< num_channels >::add_value( const size_t slot, const size_t channel, const double value )
406{
407 buffer_[ slot ][ channel ] += value;
408}
409
410template < unsigned int num_channels >
411inline const std::array< double, num_channels >&
413{
414 assert( slot < buffer_.size() );
415 return buffer_[ slot ];
416}
417
418template < unsigned int num_channels >
419inline size_t
421{
422 return buffer_.size();
423}
424
425} // namespace nest
426
427
428#endif /* #ifndef RING_BUFFER_H */
long get_modulo(long d)
Return (T+d) mod max_delay.
Definition event_delivery_manager.h:527
Definition ring_buffer.h:289
void resize()
Resize the buffer according to max_thread and max_delay.
Definition ring_buffer.cpp:81
size_t get_index_(const long d) const
Obtain buffer index.
Definition ring_buffer.h:359
void clear()
Initialize the buffer with empty lists.
Definition ring_buffer.cpp:91
std::vector< std::list< double > > buffer_
Buffered data.
Definition ring_buffer.h:328
size_t size() const
Returns buffer size, for memory measurement.
Definition ring_buffer.h:321
std::list< double > & get_list(const long offs)
Definition ring_buffer.h:347
void append_value(const long offs, const double)
Append a value to the ring buffer list.
Definition ring_buffer.h:341
ListRingBuffer()
Definition ring_buffer.cpp:75
Definition ring_buffer.h:207
std::vector< double > buffer_
Buffered data.
Definition ring_buffer.h:246
MultRBuffer()
Definition ring_buffer.cpp:52
void clear()
Initialize the buffer with noughts.
Definition ring_buffer.cpp:68
size_t size() const
Returns buffer size, for memory measurement.
Definition ring_buffer.h:239
void add_value(const long offs, const double)
Add a value to the ring buffer.
Definition ring_buffer.h:259
double get_value(const long offs)
Read one value from ring buffer.
Definition ring_buffer.h:266
void resize()
Resize the buffer according to max_thread and max_delay.
Definition ring_buffer.cpp:58
size_t get_index_(const long d) const
Obtain buffer index.
Definition ring_buffer.h:280
Definition ring_buffer.h:370
const std::array< double, num_channels > & get_values_all_channels(const size_t slot) const
Definition ring_buffer.h:412
MultiChannelInputBuffer()
Definition ring_buffer_impl.h:32
void reset_values_all_channels(const size_t slot)
Definition ring_buffer.h:397
size_t size() const
Definition ring_buffer.h:420
void resize()
Definition ring_buffer_impl.h:40
void add_value(const size_t slot, const size_t channel, const double value)
Definition ring_buffer.h:405
void clear()
Definition ring_buffer_impl.h:51
std::vector< std::array< double, num_channels > > buffer_
Buffered data stored in a vector of arrays of double values.
Definition ring_buffer.h:392
Buffer Layout.
Definition ring_buffer.h:83
size_t get_index_(const long d) const
Obtain buffer index.
Definition ring_buffer.h:197
RingBuffer()
Definition ring_buffer.cpp:28
void resize()
Resize the buffer according to max_thread and max_delay.
Definition ring_buffer.cpp:34
double get_value_wfr_update(const long offs)
Read one value from ring buffer without deleting it afterwards.
Definition ring_buffer.h:184
void set_value(const long offs, const double)
Set a ring buffer entry to a given value.
Definition ring_buffer.h:164
std::vector< double > buffer_
Buffered data.
Definition ring_buffer.h:145
void add_value(const long offs, const double)
Add a value to the ring buffer.
Definition ring_buffer.h:158
void clear()
Initialize the buffer with noughts.
Definition ring_buffer.cpp:44
double get_value(const long offs)
Read one value from ring buffer.
Definition ring_buffer.h:170
size_t size() const
Returns buffer size, for memory measurement.
Definition ring_buffer.h:138
EventDeliveryManager event_delivery_manager
Definition kernel_manager.h:241
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
KernelManager & kernel()
Definition kernel_manager.h:311