NEST main@caf0ae8
 
Loading...
Searching...
No Matches
source_table.h
Go to the documentation of this file.
1/*
2 * source_table.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 SOURCE_TABLE_H
24#define SOURCE_TABLE_H
25
26// C++ includes:
27#include <algorithm>
28#include <cassert>
29#include <iostream>
30#include <map>
31#include <set>
32#include <vector>
33
34// Includes from nestkernel:
35#include "mpi_manager.h"
36#include "nest_types.h"
38#include "source.h"
40#include "spike_data.h"
41
42// Includes from libnestutil
43#include "block_vector.h"
44#include "vector_util.h"
45
46namespace nest
47{
48
49class TargetData;
50
57{
58public:
59 CSDMapEntry( size_t source_index, size_t target_thread )
60 : source_index_( source_index )
61 , target_thread_( target_thread )
62 {
63 // MAX_LCID cannot be used since the value is used to mark invalid entries.
64 // MAX_TID is defined based on the NUM_BITS_TID field width and thus much smaller than invalid_thread and can be
65 // used.
66 assert( source_index < MAX_LCID );
67 assert( target_thread <= MAX_TID );
68 }
69
70 size_t
72 {
73 return source_index_;
74 }
75 size_t
77 {
78 return target_thread_;
79 }
80
81private:
84};
85
87using success_csdmapentry_size = StaticAssert< sizeof( CSDMapEntry ) == 8 >::success;
88
104{
105 friend class ConnectionManager;
106
107private:
111 std::vector< std::vector< BlockVector< Source > > > sources_;
112
117
119 std::vector< SourceTablePosition > current_positions_;
121 std::vector< SourceTablePosition > saved_positions_;
122
130
138 static const size_t min_deleted_elements_ = 1000000;
139
149 bool source_should_be_processed_( const size_t rank_start, const size_t rank_end, const Source& source ) const;
150
155 bool next_entry_has_same_source_( const SourceTablePosition& current_position, const Source& current_source ) const;
156
161 bool previous_entry_has_same_source_( const SourceTablePosition& current_position,
162 const Source& current_source ) const;
163
168 bool populate_target_data_fields_( const SourceTablePosition& current_position,
169 const Source& current_source,
170 const size_t source_rank,
171 TargetData& next_target_data ) const;
172
183 std::vector< std::vector< std::map< size_t, SpikeData > > > compressible_sources_;
184
195 std::vector< std::map< size_t, CSDMapEntry > > compressed_spike_data_map_;
196
197public:
198 SourceTable();
199 ~SourceTable();
200
204 void initialize();
205
209 void finalize();
210
214 void add_source( const size_t tid, const synindex syn_id, const size_t node_id, const bool is_primary );
215
219 void clear( const size_t tid );
220
224 bool is_cleared() const;
225
229 bool get_next_target_data( const size_t tid,
230 const size_t rank_start,
231 const size_t rank_end,
232 size_t& source_rank,
233 TargetData& next_target_data );
234
239 void reject_last_target_data( const size_t tid );
240
244 void save_entry_point( const size_t tid );
245
249 void restore_entry_point( const size_t tid );
250
254 void reset_entry_point( const size_t tid );
255
259 size_t get_node_id( const size_t tid, const synindex syn_id, const size_t lcid ) const;
260
265 std::vector< BlockVector< Source > >& get_thread_local_sources( const size_t tid );
266
272
277 void reset_processed_flags( const size_t tid );
278
282 void clean( const size_t tid );
283
288 void no_targets_to_process( const size_t tid );
289
296 std::map< size_t, size_t >& buffer_pos_of_source_node_id_syn_id_ );
297
306 size_t find_first_source( const size_t tid, const synindex syn_id, const size_t snode_id ) const;
307
311 void disable_connection( const size_t tid, const synindex syn_id, const size_t lcid );
312
316 size_t remove_disabled_sources( const size_t tid, const synindex syn_id );
317
322 void get_source_node_ids( const size_t tid,
323 const synindex syn_id,
324 const std::vector< size_t >& source_lcids,
325 std::vector< size_t >& sources );
326
335 size_t num_unique_sources( const size_t tid, const synindex syn_id ) const;
336
340 void resize_sources();
341
346 size_t pack_source_node_id_and_syn_id( const size_t source_node_id, const synindex syn_id ) const;
347
349
350 // creates maps of sources with more than one thread-local target
351 void collect_compressible_sources( const size_t tid );
352 // fills the compressed_spike_data structure in ConnectionManager
353 void fill_compressed_spike_data( std::vector< std::vector< std::vector< SpikeData > > >& compressed_spike_data );
354
356
357 void dump_sources() const;
358 void dump_compressible_sources() const;
360 const std::vector< std::vector< std::vector< SpikeData > > >& compressed_spike_data ) const;
361};
362
363inline void
364SourceTable::add_source( const size_t tid, const synindex syn_id, const size_t node_id, const bool is_primary )
365{
366 const Source src( node_id, is_primary );
367 sources_[ tid ][ syn_id ].push_back( src );
368}
369
370inline void
371SourceTable::clear( const size_t tid )
372{
373 for ( std::vector< BlockVector< Source > >::iterator it = sources_[ tid ].begin(); it != sources_[ tid ].end(); ++it )
374 {
375 it->clear();
376 }
377 sources_[ tid ].clear();
378 is_cleared_.set_true( tid );
379}
380
381inline void
383{
384 // The last target data returned by get_next_target_data() could not
385 // be inserted into MPI buffer due to overflow. We hence need to
386 // correct the processed flag of the last entry (see
387 // source_table.cpp)
388 assert( current_positions_[ tid ].lcid + 1
389 < static_cast< long >( sources_[ current_positions_[ tid ].tid ][ current_positions_[ tid ].syn_id ].size() ) );
390
391 sources_[ current_positions_[ tid ].tid ][ current_positions_[ tid ].syn_id ][ current_positions_[ tid ].lcid + 1 ]
392 .set_processed( false );
393}
394
395inline void
397{
398 if ( saved_entry_point_[ tid ].is_false() )
399 {
400 saved_positions_[ tid ].tid = current_positions_[ tid ].tid;
401 saved_positions_[ tid ].syn_id = current_positions_[ tid ].syn_id;
402
403 // if tid and syn_id are valid entries, also store valid entry for lcid
404 if ( current_positions_[ tid ].tid > -1 and current_positions_[ tid ].syn_id > -1 )
405 {
406 // either store current_position.lcid + 1, since this can
407 // contain non-processed entry (see reject_last_target_data()) or
408 // store maximal value for lcid.
409 saved_positions_[ tid ].lcid = std::min( current_positions_[ tid ].lcid + 1,
410 static_cast< long >(
411 sources_[ current_positions_[ tid ].tid ][ current_positions_[ tid ].syn_id ].size() - 1 ) );
412 }
413 else
414 {
415 assert( current_positions_[ tid ].lcid == -1 );
416 saved_positions_[ tid ].lcid = -1;
417 }
419 }
420}
421
422inline void
424{
425 current_positions_[ tid ] = saved_positions_[ tid ];
427}
428
429inline void
431{
432 // Since we read the source table backwards, we need to set saved
433 // values to the biggest possible value. These will be used to
434 // initialize current_positions_ correctly upon calling
435 // restore_entry_point. However, this can only be done if other
436 // values have valid values.
437 saved_positions_[ tid ].tid = sources_.size() - 1;
438 if ( saved_positions_[ tid ].tid > -1 )
439 {
440 saved_positions_[ tid ].syn_id = sources_[ saved_positions_[ tid ].tid ].size() - 1;
441 }
442 else
443 {
444 saved_positions_[ tid ].syn_id = -1;
445 }
446 if ( saved_positions_[ tid ].syn_id > -1 )
447 {
448 saved_positions_[ tid ].lcid = sources_[ saved_positions_[ tid ].tid ][ saved_positions_[ tid ].syn_id ].size() - 1;
449 }
450 else
451 {
452 saved_positions_[ tid ].lcid = -1;
453 }
454}
455
456inline void
458{
459 for ( std::vector< BlockVector< Source > >::iterator it = sources_[ tid ].begin(); it != sources_[ tid ].end(); ++it )
460 {
461 for ( BlockVector< Source >::iterator iit = it->begin(); iit != it->end(); ++iit )
462 {
463 iit->set_processed( false );
464 }
465 }
466}
467
468inline void
470{
471 current_positions_[ tid ].tid = -1;
472 current_positions_[ tid ].syn_id = -1;
473 current_positions_[ tid ].lcid = -1;
474}
475
476inline void
477SourceTable::disable_connection( const size_t tid, const synindex syn_id, const size_t lcid )
478{
479 // disabling a source changes its node ID to 2^62 -1
480 // source here
481 assert( not sources_[ tid ][ syn_id ][ lcid ].is_disabled() );
482 sources_[ tid ][ syn_id ][ lcid ].disable();
483}
484
485inline void
487 const synindex syn_id,
488 const std::vector< size_t >& source_lcids,
489 std::vector< size_t >& sources )
490{
491 for ( std::vector< size_t >::const_iterator cit = source_lcids.begin(); cit != source_lcids.end(); ++cit )
492 {
493 sources.push_back( sources_[ tid ][ syn_id ][ *cit ].get_node_id() );
494 }
495}
496
497inline size_t
498SourceTable::num_unique_sources( const size_t tid, const synindex syn_id ) const
499{
500 size_t n = 0;
501 size_t last_source = 0;
502 for ( BlockVector< Source >::const_iterator cit = sources_[ tid ][ syn_id ].begin();
503 cit != sources_[ tid ][ syn_id ].end();
504 ++cit )
505 {
506 if ( last_source != ( *cit ).get_node_id() )
507 {
508 last_source = ( *cit ).get_node_id();
509 ++n;
510 }
511 }
512 return n;
513}
514
515inline size_t
516SourceTable::pack_source_node_id_and_syn_id( const size_t source_node_id, const synindex syn_id ) const
517{
518 assert( source_node_id < 72057594037927936 );
519 assert( syn_id < invalid_synindex );
520 // syn_id is maximally 256, so shifting node ID by 8 bits and storing
521 // syn_id in the lowest 8 leads to a unique number
522 return ( source_node_id << 8 ) + syn_id;
523}
524
525inline void
527{
528 for ( auto& source_index_map : compressed_spike_data_map_ )
529 {
530 source_index_map.clear();
531 }
532}
533
534} // namespace nest
535
536#endif /* #ifndef SOURCE_TABLE_H */
Container with a vector-of-vectors structure.
Definition block_vector.h:156
iterator end()
Returns a read/write iterator that points one past the last element in the BlockVector.
Definition block_vector.h:394
Entry of compressed_spike_data_map_.
Definition source_table.h:57
size_t target_thread_
Definition source_table.h:83
CSDMapEntry(size_t source_index, size_t target_thread)
Definition source_table.h:59
size_t source_index_
Definition source_table.h:82
size_t get_source_index() const
Definition source_table.h:71
size_t get_target_thread() const
Definition source_table.h:76
Definition connection_manager.h:62
A thread-safe vector to keep track of the status across threads, for example during gather operations...
Definition per_thread_bool_indicator.h:104
void set_false(const size_t tid)
Definition per_thread_bool_indicator.h:121
void set_true(const size_t tid)
Definition per_thread_bool_indicator.h:111
This data structure stores the node IDs of presynaptic neurons during postsynaptic connection creatio...
Definition source_table.h:104
void clean(const size_t tid)
Removes all entries marked as processed.
Definition source_table.cpp:113
void dump_compressed_spike_data(const std::vector< std::vector< std::vector< SpikeData > > > &compressed_spike_data) const
Definition source_table.cpp:597
void resize_sources()
Resizes sources_ according to total number of threads and synapse types.
Definition source_table.cpp:307
void save_entry_point(const size_t tid)
Stores current_positions_ in saved_positions_.
Definition source_table.h:396
std::vector< std::map< size_t, CSDMapEntry > > compressed_spike_data_map_
A structure to temporarily store locations of "unpacked spikes" in the compressed_spike_data_ structu...
Definition source_table.h:195
size_t remove_disabled_sources(const size_t tid, const synindex syn_id)
Removes all entries from sources_ that are marked as disabled.
Definition source_table.cpp:199
void fill_compressed_spike_data(std::vector< std::vector< std::vector< SpikeData > > > &compressed_spike_data)
Definition source_table.cpp:546
void dump_compressible_sources() const
Definition source_table.cpp:527
void finalize()
Delete data structures.
Definition source_table.cpp:68
size_t get_node_id(const size_t tid, const synindex syn_id, const size_t lcid) const
Returns the node ID of the source at tid|syn_id|lcid.
Definition source_table.cpp:159
size_t num_unique_sources(const size_t tid, const synindex syn_id) const
Returns the number of unique node IDs for given thread id and synapse type in sources_....
Definition source_table.h:498
void disable_connection(const size_t tid, const synindex syn_id, const size_t lcid)
Marks entry in sources_ at given position as disabled.
Definition source_table.h:477
SourceTable()
Definition source_table.cpp:38
size_t pack_source_node_id_and_syn_id(const size_t source_node_id, const synindex syn_id) const
Encodes combination of node ID and synapse types as single long number.
Definition source_table.h:516
void resize_compressible_sources()
Definition source_table.cpp:465
void dump_sources() const
Definition source_table.cpp:506
void get_source_node_ids(const size_t tid, const synindex syn_id, const std::vector< size_t > &source_lcids, std::vector< size_t > &sources)
Returns node IDs for entries in sources_ for the given thread id, synapse type and local connections ...
Definition source_table.h:486
size_t find_first_source(const size_t tid, const synindex syn_id, const size_t snode_id) const
Finds the first non-disabled entry in sources_ at the given thread id and synapse type that has sende...
Definition source_table.cpp:169
std::vector< std::vector< std::map< size_t, SpikeData > > > compressible_sources_
A structure to temporarily hold information about all process local targets will be addressed by inco...
Definition source_table.h:183
void clear_compressed_spike_data_map()
Definition source_table.h:526
void restore_entry_point(const size_t tid)
Restores current_positions_ from saved_positions_.
Definition source_table.h:423
bool populate_target_data_fields_(const SourceTablePosition &current_position, const Source &current_source, const size_t source_rank, TargetData &next_target_data) const
Fills the fields of a TargetData during construction of * presynaptic connection infrastructure.
Definition source_table.cpp:352
void reset_processed_flags(const size_t tid)
Resets all processed flags.
Definition source_table.h:457
void reset_entry_point(const size_t tid)
Resets saved_positions_ to end of sources_.
Definition source_table.h:430
void clear(const size_t tid)
Clears sources_.
Definition source_table.h:371
std::vector< std::vector< BlockVector< Source > > > sources_
3D structure storing node IDs of presynaptic neurons.
Definition source_table.h:111
std::vector< SourceTablePosition > current_positions_
Needed during readout of sources_.
Definition source_table.h:119
void initialize()
Initialize data structure.
Definition source_table.cpp:47
bool previous_entry_has_same_source_(const SourceTablePosition &current_position, const Source &current_source) const
Returns true if the previous entry in the SourceTable has the same source gid.
Definition source_table.cpp:338
void compute_buffer_pos_for_unique_secondary_sources(const size_t tid, std::map< size_t, size_t > &buffer_pos_of_source_node_id_syn_id_)
Computes MPI buffer positions for unique combination of source node ID and synapse type across all th...
Definition source_table.cpp:234
std::vector< SourceTablePosition > saved_positions_
Needed during readout of sources_.
Definition source_table.h:121
bool is_cleared() const
Returns true if sources_ has been cleared.
Definition source_table.cpp:87
SourceTablePosition find_maximal_position() const
Determines maximal saved_positions_ after which it is safe to delete sources during clean().
Definition source_table.cpp:99
bool next_entry_has_same_source_(const SourceTablePosition &current_position, const Source &current_source) const
Returns true if the following entry in the SourceTable has the same source gid.
Definition source_table.cpp:325
static const size_t min_deleted_elements_
Minimal number of sources that need to be deleted per synapse type and thread before a reallocation o...
Definition source_table.h:138
void collect_compressible_sources(const size_t tid)
Definition source_table.cpp:476
void reject_last_target_data(const size_t tid)
Rejects the last target data, and resets the current_positions_ accordingly.
Definition source_table.h:382
PerThreadBoolIndicator saved_entry_point_
If we detect an overflow in one of the MPI buffer parts, we save our current position in sources_ to ...
Definition source_table.h:129
void add_source(const size_t tid, const synindex syn_id, const size_t node_id, const bool is_primary)
Adds a source to sources_.
Definition source_table.h:364
bool get_next_target_data(const size_t tid, const size_t rank_start, const size_t rank_end, size_t &source_rank, TargetData &next_target_data)
Returns the next target data, according to the current_positions_.
Definition source_table.cpp:397
PerThreadBoolIndicator is_cleared_
Whether the 3D structure has been deleted.
Definition source_table.h:116
std::vector< BlockVector< Source > > & get_thread_local_sources(const size_t tid)
Returns a reference to all sources local on thread; necessary for sorting.
Definition source_table.cpp:93
~SourceTable()
Definition source_table.cpp:42
void no_targets_to_process(const size_t tid)
Sets current_positions_ for this thread to minimal values so that these are not considered in find_ma...
Definition source_table.h:469
bool source_should_be_processed_(const size_t rank_start, const size_t rank_end, const Source &source) const
Returns whether this Source object should be considered when constructing MPI buffers for communicati...
Definition source_table.cpp:314
Stores the node ID of a presynaptic neuron and the number of local targets, along with a flag,...
Definition source.h:41
Used to communicate part of the connection infrastructure from post- to presynaptic side.
Definition target_data.h:175
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
constexpr uint8_t NUM_BITS_LCID
Definition nest_types.h:83
constexpr int64_t MAX_TID
Definition nest_types.h:95
constexpr synindex invalid_synindex
Definition nest_types.h:116
StaticAssert< sizeof(CSDMapEntry)==8 >::success success_csdmapentry_size
check legal size
Definition source_table.h:87
constexpr uint8_t NUM_BITS_TID
Definition nest_types.h:76
size_t synindex
For enumerations of synapse types.
Definition nest_types.h:115
constexpr uint64_t MAX_LCID
Definition nest_types.h:93
Three-tuple to store position in 3d vector of sources.
Definition source_table_position.h:40
Compile time assertions.
Definition static_assert.h:47