NEST main@f146f75
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
56
66{
67
68public:
69 // Destructor needs to be declared virtual to avoid undefined
70 // behavior, avoid possible memory leak and needs to be defined to
71 // avoid linker error, see, e.g., Meyers, S. (2005) p40ff
72 virtual ~ConnectorBase() {};
73
77 virtual synindex get_syn_id() const = 0;
78
82 virtual size_t size() const = 0;
83
87 virtual void get_synapse_status( const size_t tid, const size_t lcid, Dictionary& dict ) const = 0;
88
92 virtual void set_synapse_status( const size_t tid, const Dictionary& dict, ConnectorModel& cm ) = 0;
93
98 virtual void get_connection( const size_t source_node_id,
99 const size_t target_node_id,
100 const size_t tid,
101 const size_t lcid,
102 const long synapse_label,
103 std::deque< ConnectionID >& conns ) const = 0;
104
112 virtual void get_connection_with_specified_targets( const size_t source_node_id,
113 const NodeCollectionPTR& target,
114 const size_t tid,
115 const size_t lcid,
116 const long synapse_label,
117 std::deque< ConnectionID >& conns ) const = 0;
118
123 virtual void get_all_connections( const size_t source_node_id,
124 const size_t target_node_id,
125 const size_t tid,
126 const long synapse_label,
127 std::deque< ConnectionID >& conns ) const = 0;
128
132 virtual void
133 get_source_lcids( const size_t tid, const size_t target_node_id, std::vector< size_t >& source_lcids ) const = 0;
134
138 virtual void get_target_node_ids( const size_t tid,
139 const size_t start_lcid,
140 const std::string& post_synaptic_element,
141 std::vector< size_t >& target_node_ids ) const = 0;
142
146 virtual size_t get_target_node_id( const size_t tid, const unsigned int lcid ) const = 0;
147
151 virtual void send_to_all( const size_t tid, const std::vector< ConnectorModel* >& cm, Event& e ) = 0;
152
157 virtual size_t send( const size_t tid, const size_t lcid, const std::vector< ConnectorModel* >& cm, Event& e ) = 0;
158
171 virtual void correct_weight_stdp_ax_delay( const size_t tid,
173 const double t_spike_critical_interval_end,
174 const double t_post_spike ) = 0;
175
176 virtual void
177 send_weight_event( const size_t tid, const unsigned int lcid, Event& e, const CommonSynapseProperties& cp ) = 0;
178
182 virtual void trigger_update_weight( const long vt_node_id,
183 const size_t tid,
184 const std::vector< spikecounter >& dopa_spikes,
185 const double t_trig,
186 const std::vector< ConnectorModel* >& cm ) = 0;
187
192
196 virtual void set_source_has_more_targets( const size_t lcid, const bool has_more_targets ) = 0;
197
202 virtual size_t find_first_target( const size_t tid, const size_t start_lcid, const size_t target_node_id ) const = 0;
203
210 virtual size_t find_enabled_connection( const size_t tid,
211 const size_t syn_id,
212 const size_t source_node_id,
213 const size_t target_node_id,
214 const SourceTable& source_table ) const = 0;
215
219 virtual void disable_connection( const size_t lcid ) = 0;
220
224 virtual void remove_disabled_connections( const size_t first_disabled_index ) = 0;
225};
226
230template < typename ConnectionT >
232{
233private:
236
245
250 void register_axonal_delay_correction_required_( const ConnectionT& c );
251
252public:
253 explicit Connector( const synindex syn_id )
254 : syn_id_( syn_id )
256 {
257 }
258
259 ~Connector() override
260 {
261 C_.clear();
262 }
263
265 get_syn_id() const override
266 {
267 return syn_id_;
268 }
269
270 size_t
271 size() const override
272 {
273 return C_.size();
274 }
275
276 void
277 get_synapse_status( const size_t tid, const size_t lcid, Dictionary& dict ) const override
278 {
279 assert( lcid < C_.size() );
280
281 C_[ lcid ].get_status( dict );
282
283 // get target node ID here, where tid is available
284 // necessary for hpc synapses using TargetIdentifierIndex
285 dict[ names::target ] = static_cast< long >( C_[ lcid ].get_target( tid )->get_node_id() );
286 }
287
288 void
289 set_synapse_status( const size_t lcid, const Dictionary& dict, ConnectorModel& cm ) override
290 {
291 assert( lcid < C_.size() );
292
293 C_[ lcid ].set_status( dict, static_cast< GenericConnectorModel< ConnectionT >& >( cm ) );
294
295 // the delays may have changed
297 }
298
299 void
300 push_back( const ConnectionT& c )
301 {
303 C_.push_back( c );
304 }
305
306 void
307 push_back( ConnectionT&& c )
308 {
310 C_.push_back( std::move( c ) );
311 }
312
313 void
314 get_connection( const size_t source_node_id,
315 const size_t target_node_id,
316 const size_t tid,
317 const size_t lcid,
318 const long synapse_label,
319 std::deque< ConnectionID >& conns ) const override
320 {
321 if ( not C_[ lcid ].is_disabled() )
322 {
323 if ( synapse_label == UNLABELED_CONNECTION or C_[ lcid ].get_label() == synapse_label )
324 {
325 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
326 if ( target_node_id == 0 or target_node_id == current_target_node_id )
327 {
328 conns.push_back( ConnectionID( source_node_id, current_target_node_id, tid, syn_id_, lcid ) );
329 }
330 }
331 }
332 }
333
334 void
335 get_connection_with_specified_targets( const size_t source_node_id,
336 const NodeCollectionPTR& target,
337 const size_t tid,
338 const size_t lcid,
339 const long synapse_label,
340 std::deque< ConnectionID >& conns ) const override
341 {
342 if ( not C_[ lcid ].is_disabled() )
343 {
344 if ( synapse_label == UNLABELED_CONNECTION or C_[ lcid ].get_label() == synapse_label )
345 {
346 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
347 if ( target->contains( current_target_node_id ) )
348 {
349 conns.push_back( ConnectionID( source_node_id, current_target_node_id, tid, syn_id_, lcid ) );
350 }
351 }
352 }
353 }
354
355 void
356 get_all_connections( const size_t source_node_id,
357 const size_t target_node_id,
358 const size_t tid,
359 const long synapse_label,
360 std::deque< ConnectionID >& conns ) const override
361 {
362 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
363 {
364 get_connection( source_node_id, target_node_id, tid, lcid, synapse_label, conns );
365 }
366 }
367
368 void
369 get_source_lcids( const size_t tid, const size_t target_node_id, std::vector< size_t >& source_lcids ) const override
370 {
371 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
372 {
373 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
374 if ( current_target_node_id == target_node_id and not C_[ lcid ].is_disabled() )
375 {
376 source_lcids.push_back( lcid );
377 }
378 }
379 }
380
381 void
382 get_target_node_ids( const size_t tid,
383 const size_t start_lcid,
384 const std::string& post_synaptic_element,
385 std::vector< size_t >& target_node_ids ) const override
386 {
387 size_t lcid = start_lcid;
388 while ( true )
389 {
390 if ( C_[ lcid ].get_target( tid )->get_synaptic_elements( post_synaptic_element ) != 0.0
391 and not C_[ lcid ].is_disabled() )
392 {
393 target_node_ids.push_back( C_[ lcid ].get_target( tid )->get_node_id() );
394 }
395
396 if ( not C_[ lcid ].source_has_more_targets() )
397 {
398 break;
399 }
400
401 ++lcid;
402 }
403 }
404
405 size_t
406 get_target_node_id( const size_t tid, const unsigned int lcid ) const override
407 {
408 return C_[ lcid ].get_target( tid )->get_node_id();
409 }
410
411 void
412 send_to_all( const size_t tid, const std::vector< ConnectorModel* >& cm, Event& e ) override
413 {
414 if ( not ConnectionT::supports_flush_event and e.is_flush_event() )
415 {
416 return;
417 }
418
419 auto const& cp = static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties();
420
421 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
422 {
423 e.set_port( lcid );
424 assert( not C_[ lcid ].is_disabled() );
425 C_[ lcid ].send( e, tid, cp );
426 }
427 }
428
429 size_t
430 send( const size_t tid, const size_t lcid, const std::vector< ConnectorModel* >& cm, Event& e ) override
431 {
432 if ( not ConnectionT::supports_flush_event and e.is_flush_event() )
433 {
434 return 0;
435 }
436
437 typename ConnectionT::CommonPropertiesType const& cp =
438 static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties();
439
440 size_t lcid_offset = 0;
441
442 while ( true )
443 {
444 assert( lcid + lcid_offset < C_.size() );
445 ConnectionT& conn = C_[ lcid + lcid_offset ];
446
447 e.set_port( lcid + lcid_offset );
448 if ( not conn.is_disabled() )
449 {
450 if constexpr ( ConnectionT::requires_axonal_delay_correction )
451 {
453 {
454 // non-local sender -> receiver retrieves ID of sender Node from SourceTable based on tid, syn_id, lcid
455 // only if needed, as this is computationally costly
456 e.set_sender_node_id_info( tid, syn_id_, lcid + lcid_offset );
457 }
458 }
459 // Some synapses, e.g., bernoulli_synapse, may not send an event after all
460 const bool event_sent = conn.send( e, tid, cp );
461 if ( event_sent )
462 {
463 send_weight_event( tid, lcid + lcid_offset, e, cp );
464 }
465 }
466 if ( not conn.source_has_more_targets() )
467 {
468 break;
469 }
470 ++lcid_offset;
471 }
472
473 return 1 + lcid_offset; // event was delivered to at least one target
474 }
475
476 void correct_weight_stdp_ax_delay( const size_t tid,
478 const double t_spike_critical_interval_end,
479 const double t_post_spike ) override;
480
481 // Implemented in connector_base_impl.h
482 void
483 send_weight_event( const size_t tid, const unsigned int lcid, Event& e, const CommonSynapseProperties& cp ) override;
484
485 void
486 trigger_update_weight( const long vt_node_id,
487 const size_t tid,
488 const std::vector< spikecounter >& dopa_spikes,
489 const double t_trig,
490 const std::vector< ConnectorModel* >& cm ) override
491 {
492 for ( size_t i = 0; i < C_.size(); ++i )
493 {
494 if ( static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )
495 ->get_common_properties()
496 .get_vt_node_id()
497 == vt_node_id )
498 {
499 C_[ i ].trigger_update_weight( tid,
500 dopa_spikes,
501 t_trig,
502 static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties() );
503 }
504 }
505 }
506
507 void
509 {
510 sort( sources, C_ );
511 }
512
513 void
514 set_source_has_more_targets( const size_t lcid, const bool has_more_targets ) override
515 {
516 C_[ lcid ].set_source_has_more_targets( has_more_targets );
517 }
518
519 size_t
520 find_first_target( const size_t tid, const size_t start_lcid, const size_t target_node_id ) const override
521 {
522 // TODO: Once #3544 is merged, activate this assertion. It is currently
523 // commented out to avoid circular inclusions.
524 // assert( kernel().connection_manager.use_compressed_spikes() );
525
526 size_t lcid = start_lcid;
527 while ( true )
528 {
529 if ( C_[ lcid ].get_target( tid )->get_node_id() == target_node_id and not C_[ lcid ].is_disabled() )
530 {
531 return lcid;
532 }
533
534 if ( not C_[ lcid ].source_has_more_targets() )
535 {
536 return invalid_index;
537 }
538
539 ++lcid;
540 }
541 }
542
543 size_t
544 find_enabled_connection( const size_t tid,
545 const size_t syn_id,
546 const size_t source_node_id,
547 const size_t target_node_id,
548 const SourceTable& source_table ) const override
549 {
550 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
551 {
552 if ( source_table.get_node_id( tid, syn_id, lcid ) == source_node_id
553 and C_[ lcid ].get_target( tid )->get_node_id() == target_node_id and not C_[ lcid ].is_disabled() )
554 {
555 return lcid;
556 }
557 }
558
559 return invalid_index;
560 }
561
562 void
563 disable_connection( const size_t lcid ) override
564 {
565 assert( not C_[ lcid ].is_disabled() );
566 C_[ lcid ].disable();
567 }
568
569 void
570 remove_disabled_connections( const size_t first_disabled_index ) override
571 {
572 assert( C_[ first_disabled_index ].is_disabled() );
573 C_.erase( C_.begin() + first_disabled_index, C_.end() );
574 }
575};
576
577} // of namespace nest
578
579#endif
Container with a vector-of-vectors structure.
Definition block_vector.h:156
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:66
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:72
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 correct_weight_stdp_ax_delay(const size_t tid, CorrectionEntrySTDPAxDelay &entry, const double t_spike_critical_interval_end, const double t_post_spike)=0
On occurrence of a post-synaptic spike, correct the weight update of a previous pre-synaptic spike wh...
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
void correct_weight_stdp_ax_delay(const size_t tid, CorrectionEntrySTDPAxDelay &entry, const double t_spike_critical_interval_end, const double t_post_spike) override
On occurrence of a post-synaptic spike, correct the weight update of a previous pre-synaptic spike wh...
Definition connector_base_impl.h:82
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:412
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:289
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:544
void push_back(const ConnectionT &c)
Definition connector_base.h:300
bool axonal_delay_correction_required_
Whether any connection held here can ever require a retrospective correction.
Definition connector_base.h:244
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:335
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:265
void disable_connection(const size_t lcid) override
Disable the transfer of events through the connection at position lcid.
Definition connector_base.h:563
void remove_disabled_connections(const size_t first_disabled_index) override
Remove disabled connections from the connector.
Definition connector_base.h:570
const synindex syn_id_
Definition connector_base.h:235
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:486
size_t size() const override
Return the number of connections in this Connector.
Definition connector_base.h:271
Connector(const synindex syn_id)
Definition connector_base.h:253
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:430
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:382
void push_back(ConnectionT &&c)
Definition connector_base.h:307
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:356
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:520
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:314
void send_weight_event(const size_t tid, const unsigned int lcid, Event &e, const CommonSynapseProperties &cp) override
Definition connector_base_impl.h:54
BlockVector< ConnectionT > C_
Definition connector_base.h:234
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:406
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:277
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:514
~Connector() override
Definition connector_base.h:259
void sort_connections(BlockVector< Source > &sources) override
Sort connections according to source node IDs.
Definition connector_base.h:508
void register_axonal_delay_correction_required_(const ConnectionT &c)
Fold a newly added or modified connection into axonal_delay_correction_required_ and the kernel-wide ...
Definition connector_base_impl.h:40
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:369
Information on a received pre-synaptic spike which is buffered by an AxonalDelayArchivingNode,...
Definition axonal_delay_archiving_node.h:46
Encapsulate information sent between nodes.
Definition event.h:103
Definition connector_model.h:156
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
std::shared_ptr< NodeCollection > NodeCollectionPTR
Definition node_collection.h:50
static const long UNLABELED_CONNECTION
Connections are unlabeled by default.
Definition connection_label.h:38
size_t synindex
For enumerations of synapse types.
Definition nest_types.h:121
Declarations for base class Node.