NEST main@a355080
 
Loading...
Searching...
No Matches
connector_base.h
Go to the documentation of this file.
1/*
2 * connector_base.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 CONNECTOR_BASE_H
24#define CONNECTOR_BASE_H
25
26// Generated includes:
27#include "config.h"
28
29// C++ includes:
30#include <cstdlib>
31#include <vector>
32
33// Includes from libnestutil:
34#include "compose.hpp"
35#include "sort.h"
36#include "vector_util.h"
37
38// Includes from nestkernel:
40#include "connection_id.h"
41#include "connection_label.h"
42#include "connector_model.h"
43#include "event.h"
44#include "nest_names.h"
45#include "node.h"
46#include "node_collection.h"
47#include "source.h"
48#include "source_table.h"
49#include "spikecounter.h"
50
51
52namespace nest
53{
54
64{
65
66public:
67 // Destructor needs to be declared virtual to avoid undefined
68 // behavior, avoid possible memory leak and needs to be defined to
69 // avoid linker error, see, e.g., Meyers, S. (2005) p40ff
70 virtual ~ConnectorBase() {};
71
76 virtual synindex get_syn_id() const = 0;
77
81 virtual size_t size() const = 0;
82
87 virtual void get_synapse_status( const size_t tid, const size_t lcid, Dictionary& dict ) const = 0;
88
93 virtual void set_synapse_status( const size_t tid, const Dictionary& dict, ConnectorModel& cm ) = 0;
94
100 virtual void get_connection( const size_t source_node_id,
101 const size_t target_node_id,
102 const size_t tid,
103 const size_t lcid,
104 const long synapse_label,
105 std::deque< ConnectionID >& conns ) const = 0;
106
114 virtual void get_connection_with_specified_targets( const size_t source_node_id,
115 const NodeCollectionPTR& target,
116 const size_t tid,
117 const size_t lcid,
118 const long synapse_label,
119 std::deque< ConnectionID >& conns ) const = 0;
120
126 virtual void get_all_connections( const size_t source_node_id,
127 const size_t target_node_id,
128 const size_t tid,
129 const long synapse_label,
130 std::deque< ConnectionID >& conns ) const = 0;
131
136 virtual void
137 get_source_lcids( const size_t tid, const size_t target_node_id, std::vector< size_t >& source_lcids ) const = 0;
138
143 virtual void get_target_node_ids( const size_t tid,
144 const size_t start_lcid,
145 const std::string& post_synaptic_element,
146 std::vector< size_t >& target_node_ids ) const = 0;
147
151 virtual size_t get_target_node_id( const size_t tid, const unsigned int lcid ) const = 0;
152
156 virtual void send_to_all( const size_t tid, const std::vector< ConnectorModel* >& cm, Event& e ) = 0;
157
163 virtual size_t send( const size_t tid, const size_t lcid, const std::vector< ConnectorModel* >& cm, Event& e ) = 0;
164
165 virtual void
166 send_weight_event( const size_t tid, const unsigned int lcid, Event& e, const CommonSynapseProperties& cp ) = 0;
167
171 virtual void trigger_update_weight( const long vt_node_id,
172 const size_t tid,
173 const std::vector< spikecounter >& dopa_spikes,
174 const double t_trig,
175 const std::vector< ConnectorModel* >& cm ) = 0;
176
181
186 virtual void set_source_has_more_targets( const size_t lcid, const bool has_more_targets ) = 0;
187
193 virtual size_t find_first_target( const size_t tid, const size_t start_lcid, const size_t target_node_id ) const = 0;
194
201 virtual size_t find_enabled_connection( const size_t tid,
202 const size_t syn_id,
203 const size_t source_node_id,
204 const size_t target_node_id,
205 const SourceTable& source_table ) const = 0;
206
211 virtual void disable_connection( const size_t lcid ) = 0;
212
216 virtual void remove_disabled_connections( const size_t first_disabled_index ) = 0;
217};
218
222template < typename ConnectionT >
224{
225private:
228
229public:
230 explicit Connector( const synindex syn_id )
231 : syn_id_( syn_id )
232 {
233 }
234
235 ~Connector() override
236 {
237 C_.clear();
238 }
239
241 get_syn_id() const override
242 {
243 return syn_id_;
244 }
245
246 size_t
247 size() const override
248 {
249 return C_.size();
250 }
251
252 void
253 get_synapse_status( const size_t tid, const size_t lcid, Dictionary& dict ) const override
254 {
255 assert( lcid < C_.size() );
256
257 C_[ lcid ].get_status( dict );
258
259 // get target node ID here, where tid is available
260 // necessary for hpc synapses using TargetIdentifierIndex
261 dict[ names::target ] = static_cast< long >( C_[ lcid ].get_target( tid )->get_node_id() );
262 }
263
264 void
265 set_synapse_status( const size_t lcid, const Dictionary& dict, ConnectorModel& cm ) override
266 {
267 assert( lcid < C_.size() );
268
269 C_[ lcid ].set_status( dict, static_cast< GenericConnectorModel< ConnectionT >& >( cm ) );
270 }
271
272 void
273 push_back( const ConnectionT& c )
274 {
275 C_.push_back( c );
276 }
277
278 void
279 push_back( ConnectionT&& c )
280 {
281 C_.push_back( std::move( c ) );
282 }
283
284 void
285 get_connection( const size_t source_node_id,
286 const size_t target_node_id,
287 const size_t tid,
288 const size_t lcid,
289 const long synapse_label,
290 std::deque< ConnectionID >& conns ) const override
291 {
292 if ( not C_[ lcid ].is_disabled() )
293 {
294 if ( synapse_label == UNLABELED_CONNECTION or C_[ lcid ].get_label() == synapse_label )
295 {
296 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
297 if ( target_node_id == 0 or target_node_id == current_target_node_id )
298 {
299 conns.push_back( ConnectionID( source_node_id, current_target_node_id, tid, syn_id_, lcid ) );
300 }
301 }
302 }
303 }
304
305 void
306 get_connection_with_specified_targets( const size_t source_node_id,
307 const NodeCollectionPTR& target,
308 const size_t tid,
309 const size_t lcid,
310 const long synapse_label,
311 std::deque< ConnectionID >& conns ) const override
312 {
313 if ( not C_[ lcid ].is_disabled() )
314 {
315 if ( synapse_label == UNLABELED_CONNECTION or C_[ lcid ].get_label() == synapse_label )
316 {
317 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
318 if ( target->contains( current_target_node_id ) )
319 {
320 conns.push_back( ConnectionID( source_node_id, current_target_node_id, tid, syn_id_, lcid ) );
321 }
322 }
323 }
324 }
325
326 void
327 get_all_connections( const size_t source_node_id,
328 const size_t target_node_id,
329 const size_t tid,
330 const long synapse_label,
331 std::deque< ConnectionID >& conns ) const override
332 {
333 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
334 {
335 get_connection( source_node_id, target_node_id, tid, lcid, synapse_label, conns );
336 }
337 }
338
339 void
340 get_source_lcids( const size_t tid, const size_t target_node_id, std::vector< size_t >& source_lcids ) const override
341 {
342 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
343 {
344 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
345 if ( current_target_node_id == target_node_id and not C_[ lcid ].is_disabled() )
346 {
347 source_lcids.push_back( lcid );
348 }
349 }
350 }
351
352 void
353 get_target_node_ids( const size_t tid,
354 const size_t start_lcid,
355 const std::string& post_synaptic_element,
356 std::vector< size_t >& target_node_ids ) const override
357 {
358 size_t lcid = start_lcid;
359 while ( true )
360 {
361 if ( C_[ lcid ].get_target( tid )->get_synaptic_elements( post_synaptic_element ) != 0.0
362 and not C_[ lcid ].is_disabled() )
363 {
364 target_node_ids.push_back( C_[ lcid ].get_target( tid )->get_node_id() );
365 }
366
367 if ( not C_[ lcid ].source_has_more_targets() )
368 {
369 break;
370 }
371
372 ++lcid;
373 }
374 }
375
376 size_t
377 get_target_node_id( const size_t tid, const unsigned int lcid ) const override
378 {
379 return C_[ lcid ].get_target( tid )->get_node_id();
380 }
381
382 void
383 send_to_all( const size_t tid, const std::vector< ConnectorModel* >& cm, Event& e ) override
384 {
385 if ( not ConnectionT::supports_flush_event and e.is_flush_event() )
386 {
387 return;
388 }
389
390 auto const& cp = static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties();
391
392 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
393 {
394 e.set_port( lcid );
395 assert( not C_[ lcid ].is_disabled() );
396 C_[ lcid ].send( e, tid, cp );
397 }
398 }
399
400 size_t
401 send( const size_t tid, const size_t lcid, const std::vector< ConnectorModel* >& cm, Event& e ) override
402 {
403 if ( not ConnectionT::supports_flush_event and e.is_flush_event() )
404 {
405 return 0;
406 }
407
408 typename ConnectionT::CommonPropertiesType const& cp =
409 static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties();
410
411 size_t lcid_offset = 0;
412
413 while ( true )
414 {
415 assert( lcid + lcid_offset < C_.size() );
416 ConnectionT& conn = C_[ lcid + lcid_offset ];
417
418 e.set_port( lcid + lcid_offset );
419 if ( not conn.is_disabled() )
420 {
421 // Some synapses, e.g., bernoulli_synapse, may not send an event after all
422 const bool event_sent = conn.send( e, tid, cp );
423 if ( event_sent )
424 {
425 send_weight_event( tid, lcid + lcid_offset, e, cp );
426 }
427 }
428 if ( not conn.source_has_more_targets() )
429 {
430 break;
431 }
432 ++lcid_offset;
433 }
434
435 return 1 + lcid_offset; // event was delivered to at least one target
436 }
437
438 // Implemented in connector_base_impl.h
439 void
440 send_weight_event( const size_t tid, const unsigned int lcid, Event& e, const CommonSynapseProperties& cp ) override;
441
442 void
443 trigger_update_weight( const long vt_node_id,
444 const size_t tid,
445 const std::vector< spikecounter >& dopa_spikes,
446 const double t_trig,
447 const std::vector< ConnectorModel* >& cm ) override
448 {
449 for ( size_t i = 0; i < C_.size(); ++i )
450 {
451 if ( static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )
452 ->get_common_properties()
453 .get_vt_node_id()
454 == vt_node_id )
455 {
456 C_[ i ].trigger_update_weight( tid,
457 dopa_spikes,
458 t_trig,
459 static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties() );
460 }
461 }
462 }
463
464 void
466 {
467 sort( sources, C_ );
468 }
469
470 void
471 set_source_has_more_targets( const size_t lcid, const bool has_more_targets ) override
472 {
473 C_[ lcid ].set_source_has_more_targets( has_more_targets );
474 }
475
476 size_t
477 find_first_target( const size_t tid, const size_t start_lcid, const size_t target_node_id ) const override
478 {
479 // TODO: Once #3544 is merged, activate this assertion. It is currently
480 // commented out to avoid circular inclusions.
481 // assert( kernel().connection_manager.use_compressed_spikes() );
482
483 size_t lcid = start_lcid;
484 while ( true )
485 {
486 if ( C_[ lcid ].get_target( tid )->get_node_id() == target_node_id and not C_[ lcid ].is_disabled() )
487 {
488 return lcid;
489 }
490
491 if ( not C_[ lcid ].source_has_more_targets() )
492 {
493 return invalid_index;
494 }
495
496 ++lcid;
497 }
498 }
499
500 size_t
501 find_enabled_connection( const size_t tid,
502 const size_t syn_id,
503 const size_t source_node_id,
504 const size_t target_node_id,
505 const SourceTable& source_table ) const override
506 {
507 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
508 {
509 if ( source_table.get_node_id( tid, syn_id, lcid ) == source_node_id
510 and C_[ lcid ].get_target( tid )->get_node_id() == target_node_id and not C_[ lcid ].is_disabled() )
511 {
512 return lcid;
513 }
514 }
515
516 return invalid_index;
517 }
518
519 void
520 disable_connection( const size_t lcid ) override
521 {
522 assert( not C_[ lcid ].is_disabled() );
523 C_[ lcid ].disable();
524 }
525
526 void
527 remove_disabled_connections( const size_t first_disabled_index ) override
528 {
529 assert( C_[ first_disabled_index ].is_disabled() );
530 C_.erase( C_.begin() + first_disabled_index, C_.end() );
531 }
532};
533
534} // of namespace nest
535
536#endif
Container with a vector-of-vectors structure.
Definition block_vector.h:156
iterator begin()
Returns a read/write iterator that points to the first element in the BlockVector.
Definition block_vector.h:380
void clear()
Erases all the elements.
Definition block_vector.h:442
iterator end()
Returns a read/write iterator that points one past the last element in the BlockVector.
Definition block_vector.h:394
iterator erase(const_iterator, const_iterator)
Remove a range of elements.
Definition block_vector.h:473
size_t size() const
Returns the number of elements in the BlockVector.
Definition block_vector.h:456
void push_back(const value_type_ &value)
Add data to the end of the BlockVector.
Definition block_vector.h:408
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
Class containing the common properties for all connections of a certain type.
Definition common_synapse_properties.h:50
Definition connection_id.h:33
Base class to allow storing Connectors for different synapse types in vectors.
Definition connector_base.h:64
virtual void trigger_update_weight(const long vt_node_id, const size_t tid, const std::vector< spikecounter > &dopa_spikes, const double t_trig, const std::vector< ConnectorModel * > &cm)=0
Update weights of dopamine modulated STDP connections.
virtual void get_source_lcids(const size_t tid, const size_t target_node_id, std::vector< size_t > &source_lcids) const =0
For a given target_node_id add lcids of all connections with matching node ID of target to source_lci...
virtual void remove_disabled_connections(const size_t first_disabled_index)=0
Remove disabled connections from the connector.
virtual ~ConnectorBase()
Definition connector_base.h:70
virtual synindex get_syn_id() const =0
Return syn_id_ of the synapse type of this Connector (size_t in list of synapse prototypes).
virtual size_t find_enabled_connection(const size_t tid, const size_t syn_id, const size_t source_node_id, const size_t target_node_id, const SourceTable &source_table) const =0
Return lcid of first connection matching source and target node id and that is not disabled.
virtual void sort_connections(BlockVector< Source > &)=0
Sort connections according to source node IDs.
virtual void get_all_connections(const size_t source_node_id, const size_t target_node_id, const size_t tid, const long synapse_label, std::deque< ConnectionID > &conns) const =0
Add ConnectionIDs with given source_node_id to conns, looping over all lcids.
virtual void disable_connection(const size_t lcid)=0
Disable the transfer of events through the connection at position lcid.
virtual size_t send(const size_t tid, const size_t lcid, const std::vector< ConnectorModel * > &cm, Event &e)=0
Send the event e to the connection at position lcid.
virtual void get_connection(const size_t source_node_id, const size_t target_node_id, const size_t tid, const size_t lcid, const long synapse_label, std::deque< ConnectionID > &conns) const =0
Add ConnectionID with given source_node_id and lcid to conns.
virtual void send_weight_event(const size_t tid, const unsigned int lcid, Event &e, const CommonSynapseProperties &cp)=0
virtual void set_synapse_status(const size_t tid, const Dictionary &dict, ConnectorModel &cm)=0
Set status of the connection at position lcid according to the dictionary dict.
virtual size_t size() const =0
Return the number of connections in this Connector.
virtual void get_connection_with_specified_targets(const size_t source_node_id, const NodeCollectionPTR &target, const size_t tid, const size_t lcid, const long synapse_label, std::deque< ConnectionID > &conns) const =0
Add Connections with given source_node_id and lcid to conns.
virtual void get_synapse_status(const size_t tid, const size_t lcid, Dictionary &dict) const =0
Write status of the connection at position lcid to the dictionary dict.
virtual void send_to_all(const size_t tid, const std::vector< ConnectorModel * > &cm, Event &e)=0
Send the event e to all connections of this Connector.
virtual void get_target_node_ids(const size_t tid, const size_t start_lcid, const std::string &post_synaptic_element, std::vector< size_t > &target_node_ids) const =0
For a given start_lcid add node IDs of all targets that belong to the same source to target_node_ids.
virtual void set_source_has_more_targets(const size_t lcid, const bool has_more_targets)=0
Set a flag in the connection indicating whether the following connection belongs to the same source.
virtual size_t get_target_node_id(const size_t tid, const unsigned int lcid) const =0
For a given lcid return the node ID of the target of the connection.
virtual size_t find_first_target(const size_t tid, const size_t start_lcid, const size_t target_node_id) const =0
Return lcid of the first connection after start_lcid (inclusive) where the node_id of the target matc...
Definition connector_model.h:69
Homogeneous connector, contains synapses of one particular type (syn_id_).
Definition connector_base.h:224
void send_to_all(const size_t tid, const std::vector< ConnectorModel * > &cm, Event &e) override
Send the event e to all connections of this Connector.
Definition connector_base.h:383
void set_synapse_status(const size_t lcid, const Dictionary &dict, ConnectorModel &cm) override
Set status of the connection at position lcid according to the dictionary dict.
Definition connector_base.h:265
size_t find_enabled_connection(const size_t tid, const size_t syn_id, const size_t source_node_id, const size_t target_node_id, const SourceTable &source_table) const override
Return lcid of first connection matching source and target node id and that is not disabled.
Definition connector_base.h:501
void push_back(const ConnectionT &c)
Definition connector_base.h:273
void get_connection_with_specified_targets(const size_t source_node_id, const NodeCollectionPTR &target, const size_t tid, const size_t lcid, const long synapse_label, std::deque< ConnectionID > &conns) const override
Add Connections with given source_node_id and lcid to conns.
Definition connector_base.h:306
synindex get_syn_id() const override
Return syn_id_ of the synapse type of this Connector (size_t in list of synapse prototypes).
Definition connector_base.h:241
void disable_connection(const size_t lcid) override
Disable the transfer of events through the connection at position lcid.
Definition connector_base.h:520
void remove_disabled_connections(const size_t first_disabled_index) override
Remove disabled connections from the connector.
Definition connector_base.h:527
const synindex syn_id_
Definition connector_base.h:227
void trigger_update_weight(const long vt_node_id, const size_t tid, const std::vector< spikecounter > &dopa_spikes, const double t_trig, const std::vector< ConnectorModel * > &cm) override
Update weights of dopamine modulated STDP connections.
Definition connector_base.h:443
size_t size() const override
Return the number of connections in this Connector.
Definition connector_base.h:247
Connector(const synindex syn_id)
Definition connector_base.h:230
size_t send(const size_t tid, const size_t lcid, const std::vector< ConnectorModel * > &cm, Event &e) override
Send the event e to the connection at position lcid.
Definition connector_base.h:401
void get_target_node_ids(const size_t tid, const size_t start_lcid, const std::string &post_synaptic_element, std::vector< size_t > &target_node_ids) const override
For a given start_lcid add node IDs of all targets that belong to the same source to target_node_ids.
Definition connector_base.h:353
void push_back(ConnectionT &&c)
Definition connector_base.h:279
void get_all_connections(const size_t source_node_id, const size_t target_node_id, const size_t tid, const long synapse_label, std::deque< ConnectionID > &conns) const override
Add ConnectionIDs with given source_node_id to conns, looping over all lcids.
Definition connector_base.h:327
size_t find_first_target(const size_t tid, const size_t start_lcid, const size_t target_node_id) const override
Return lcid of the first connection after start_lcid (inclusive) where the node_id of the target matc...
Definition connector_base.h:477
void get_connection(const size_t source_node_id, const size_t target_node_id, const size_t tid, const size_t lcid, const long synapse_label, std::deque< ConnectionID > &conns) const override
Add ConnectionID with given source_node_id and lcid to conns.
Definition connector_base.h:285
void send_weight_event(const size_t tid, const unsigned int lcid, Event &e, const CommonSynapseProperties &cp) override
Definition connector_base_impl.h:39
BlockVector< ConnectionT > C_
Definition connector_base.h:226
size_t get_target_node_id(const size_t tid, const unsigned int lcid) const override
For a given lcid return the node ID of the target of the connection.
Definition connector_base.h:377
void get_synapse_status(const size_t tid, const size_t lcid, Dictionary &dict) const override
Write status of the connection at position lcid to the dictionary dict.
Definition connector_base.h:253
void set_source_has_more_targets(const size_t lcid, const bool has_more_targets) override
Set a flag in the connection indicating whether the following connection belongs to the same source.
Definition connector_base.h:471
~Connector() override
Definition connector_base.h:235
void sort_connections(BlockVector< Source > &sources) override
Sort connections according to source node IDs.
Definition connector_base.h:465
void get_source_lcids(const size_t tid, const size_t target_node_id, std::vector< size_t > &source_lcids) const override
For a given target_node_id add lcids of all connections with matching node ID of target to source_lci...
Definition connector_base.h:340
Encapsulate information sent between nodes.
Definition event.h:103
Definition connector_model.h:152
This data structure stores the node IDs of presynaptic neurons during postsynaptic connection creatio...
Definition source_table.h:104
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
const std::string target("target")
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
void sort(BlockVector< T1 > &vec_sort, BlockVector< T2 > &vec_perm)
Sorts two vectors according to elements in first vector.
Definition sort.h:177
static const long UNLABELED_CONNECTION
Connections are unlabeled by default.
Definition connection_label.h:38
std::shared_ptr< NodeCollection > NodeCollectionPTR
Definition node_collection.h:50
size_t synindex
For enumerations of synapse types.
Definition nest_types.h:115
Declarations for base class Node.