NEST main@caf0ae8
 
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 "source.h"
47#include "source_table.h"
48#include "spikecounter.h"
49
50
51namespace nest
52{
53
63{
64
65public:
66 // Destructor needs to be declared virtual to avoid undefined
67 // behavior, avoid possible memory leak and needs to be defined to
68 // avoid linker error, see, e.g., Meyers, S. (2005) p40ff
69 virtual ~ConnectorBase() {};
70
75 virtual synindex get_syn_id() const = 0;
76
80 virtual size_t size() const = 0;
81
86 virtual void get_synapse_status( const size_t tid, const size_t lcid, Dictionary& dict ) const = 0;
87
92 virtual void set_synapse_status( const size_t tid, const Dictionary& dict, ConnectorModel& cm ) = 0;
93
99 virtual void get_connection( const size_t source_node_id,
100 const size_t target_node_id,
101 const size_t tid,
102 const size_t lcid,
103 const long synapse_label,
104 std::deque< ConnectionID >& conns ) const = 0;
105
111 virtual void get_connection_with_specified_targets( const size_t source_node_id,
112 const std::vector< size_t >& target_neuron_node_ids,
113 const size_t tid,
114 const size_t lcid,
115 const long synapse_label,
116 std::deque< ConnectionID >& conns ) const = 0;
117
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
133 virtual void
134 get_source_lcids( const size_t tid, const size_t target_node_id, std::vector< size_t >& source_lcids ) const = 0;
135
140 virtual void get_target_node_ids( const size_t tid,
141 const size_t start_lcid,
142 const std::string& post_synaptic_element,
143 std::vector< size_t >& target_node_ids ) const = 0;
144
148 virtual size_t get_target_node_id( const size_t tid, const unsigned int lcid ) const = 0;
149
153 virtual void send_to_all( const size_t tid, const std::vector< ConnectorModel* >& cm, Event& e ) = 0;
154
160 virtual size_t send( const size_t tid, const size_t lcid, const std::vector< ConnectorModel* >& cm, Event& e ) = 0;
161
162 virtual void
163 send_weight_event( const size_t tid, const unsigned int lcid, Event& e, const CommonSynapseProperties& cp ) = 0;
164
168 virtual void trigger_update_weight( const long vt_node_id,
169 const size_t tid,
170 const std::vector< spikecounter >& dopa_spikes,
171 const double t_trig,
172 const std::vector< ConnectorModel* >& cm ) = 0;
173
178
183 virtual void set_source_has_more_targets( const size_t lcid, const bool has_more_targets ) = 0;
184
190 virtual size_t find_first_target( const size_t tid, const size_t start_lcid, const size_t target_node_id ) const = 0;
191
198 virtual size_t find_enabled_connection( const size_t tid,
199 const size_t syn_id,
200 const size_t source_node_id,
201 const size_t target_node_id,
202 const SourceTable& source_table ) const = 0;
203
208 virtual void disable_connection( const size_t lcid ) = 0;
209
213 virtual void remove_disabled_connections( const size_t first_disabled_index ) = 0;
214};
215
219template < typename ConnectionT >
221{
222private:
225
226public:
227 explicit Connector( const synindex syn_id )
228 : syn_id_( syn_id )
229 {
230 }
231
232 ~Connector() override
233 {
234 C_.clear();
235 }
236
238 get_syn_id() const override
239 {
240 return syn_id_;
241 }
242
243 size_t
244 size() const override
245 {
246 return C_.size();
247 }
248
249 void
250 get_synapse_status( const size_t tid, const size_t lcid, Dictionary& dict ) const override
251 {
252 assert( lcid < C_.size() );
253
254 C_[ lcid ].get_status( dict );
255
256 // get target node ID here, where tid is available
257 // necessary for hpc synapses using TargetIdentifierIndex
258 dict[ names::target ] = static_cast< long >( C_[ lcid ].get_target( tid )->get_node_id() );
259 }
260
261 void
262 set_synapse_status( const size_t lcid, const Dictionary& dict, ConnectorModel& cm ) override
263 {
264 assert( lcid < C_.size() );
265
266 C_[ lcid ].set_status( dict, static_cast< GenericConnectorModel< ConnectionT >& >( cm ) );
267 }
268
269 void
270 push_back( const ConnectionT& c )
271 {
272 C_.push_back( c );
273 }
274
275 void
276 push_back( ConnectionT&& c )
277 {
278 C_.push_back( std::move( c ) );
279 }
280
281 void
282 get_connection( const size_t source_node_id,
283 const size_t target_node_id,
284 const size_t tid,
285 const size_t lcid,
286 const long synapse_label,
287 std::deque< ConnectionID >& conns ) const override
288 {
289 if ( not C_[ lcid ].is_disabled() )
290 {
291 if ( synapse_label == UNLABELED_CONNECTION or C_[ lcid ].get_label() == synapse_label )
292 {
293 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
294 if ( current_target_node_id == target_node_id or target_node_id == 0 )
295 {
296 conns.push_back( ConnectionID( source_node_id, current_target_node_id, tid, syn_id_, lcid ) );
297 }
298 }
299 }
300 }
301
302 void
303 get_connection_with_specified_targets( const size_t source_node_id,
304 const std::vector< size_t >& target_neuron_node_ids,
305 const size_t tid,
306 const size_t lcid,
307 const long synapse_label,
308 std::deque< ConnectionID >& conns ) const override
309 {
310 if ( not C_[ lcid ].is_disabled() )
311 {
312 if ( synapse_label == UNLABELED_CONNECTION or C_[ lcid ].get_label() == synapse_label )
313 {
314 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
315 if ( std::find( target_neuron_node_ids.begin(), target_neuron_node_ids.end(), current_target_node_id )
316 != target_neuron_node_ids.end() )
317 {
318 conns.push_back( ConnectionID( source_node_id, current_target_node_id, tid, syn_id_, lcid ) );
319 }
320 }
321 }
322 }
323
324 void
325 get_all_connections( const size_t source_node_id,
326 const size_t target_node_id,
327 const size_t tid,
328 const long synapse_label,
329 std::deque< ConnectionID >& conns ) const override
330 {
331 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
332 {
333 get_connection( source_node_id, target_node_id, tid, lcid, synapse_label, conns );
334 }
335 }
336
337 void
338 get_source_lcids( const size_t tid, const size_t target_node_id, std::vector< size_t >& source_lcids ) const override
339 {
340 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
341 {
342 const size_t current_target_node_id = C_[ lcid ].get_target( tid )->get_node_id();
343 if ( current_target_node_id == target_node_id and not C_[ lcid ].is_disabled() )
344 {
345 source_lcids.push_back( lcid );
346 }
347 }
348 }
349
350 void
351 get_target_node_ids( const size_t tid,
352 const size_t start_lcid,
353 const std::string& post_synaptic_element,
354 std::vector< size_t >& target_node_ids ) const override
355 {
356 size_t lcid = start_lcid;
357 while ( true )
358 {
359 if ( C_[ lcid ].get_target( tid )->get_synaptic_elements( post_synaptic_element ) != 0.0
360 and not C_[ lcid ].is_disabled() )
361 {
362 target_node_ids.push_back( C_[ lcid ].get_target( tid )->get_node_id() );
363 }
364
365 if ( not C_[ lcid ].source_has_more_targets() )
366 {
367 break;
368 }
369
370 ++lcid;
371 }
372 }
373
374 size_t
375 get_target_node_id( const size_t tid, const unsigned int lcid ) const override
376 {
377 return C_[ lcid ].get_target( tid )->get_node_id();
378 }
379
380 void
381 send_to_all( const size_t tid, const std::vector< ConnectorModel* >& cm, Event& e ) override
382 {
383 if ( not ConnectionT::supports_flush_event and e.is_flush_event() )
384 {
385 return;
386 }
387
388 auto const& cp = static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties();
389
390 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
391 {
392 e.set_port( lcid );
393 assert( not C_[ lcid ].is_disabled() );
394 C_[ lcid ].send( e, tid, cp );
395 }
396 }
397
398 size_t
399 send( const size_t tid, const size_t lcid, const std::vector< ConnectorModel* >& cm, Event& e ) override
400 {
401 if ( not ConnectionT::supports_flush_event and e.is_flush_event() )
402 {
403 return 0;
404 }
405
406 typename ConnectionT::CommonPropertiesType const& cp =
407 static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties();
408
409 size_t lcid_offset = 0;
410
411 while ( true )
412 {
413 assert( lcid + lcid_offset < C_.size() );
414 ConnectionT& conn = C_[ lcid + lcid_offset ];
415
416 e.set_port( lcid + lcid_offset );
417 if ( not conn.is_disabled() )
418 {
419 // Some synapses, e.g., bernoulli_synapse, may not send an event after all
420 const bool event_sent = conn.send( e, tid, cp );
421 if ( event_sent )
422 {
423 send_weight_event( tid, lcid + lcid_offset, e, cp );
424 }
425 }
426 if ( not conn.source_has_more_targets() )
427 {
428 break;
429 }
430 ++lcid_offset;
431 }
432
433 return 1 + lcid_offset; // event was delivered to at least one target
434 }
435
436 // Implemented in connector_base_impl.h
437 void
438 send_weight_event( const size_t tid, const unsigned int lcid, Event& e, const CommonSynapseProperties& cp ) override;
439
440 void
441 trigger_update_weight( const long vt_node_id,
442 const size_t tid,
443 const std::vector< spikecounter >& dopa_spikes,
444 const double t_trig,
445 const std::vector< ConnectorModel* >& cm ) override
446 {
447 for ( size_t i = 0; i < C_.size(); ++i )
448 {
449 if ( static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )
450 ->get_common_properties()
451 .get_vt_node_id()
452 == vt_node_id )
453 {
454 C_[ i ].trigger_update_weight( tid,
455 dopa_spikes,
456 t_trig,
457 static_cast< GenericConnectorModel< ConnectionT >* >( cm[ syn_id_ ] )->get_common_properties() );
458 }
459 }
460 }
461
462 void
464 {
465 sort( sources, C_ );
466 }
467
468 void
469 set_source_has_more_targets( const size_t lcid, const bool has_more_targets ) override
470 {
471 C_[ lcid ].set_source_has_more_targets( has_more_targets );
472 }
473
474 size_t
475 find_first_target( const size_t tid, const size_t start_lcid, const size_t target_node_id ) const override
476 {
477 // TODO: Once #3544 is merged, activate this assertion. It is currently
478 // commented out to avoid circular inclusions.
479 // assert( kernel().connection_manager.use_compressed_spikes() );
480
481 size_t lcid = start_lcid;
482 while ( true )
483 {
484 if ( C_[ lcid ].get_target( tid )->get_node_id() == target_node_id and not C_[ lcid ].is_disabled() )
485 {
486 return lcid;
487 }
488
489 if ( not C_[ lcid ].source_has_more_targets() )
490 {
491 return invalid_index;
492 }
493
494 ++lcid;
495 }
496 }
497
498 size_t
499 find_enabled_connection( const size_t tid,
500 const size_t syn_id,
501 const size_t source_node_id,
502 const size_t target_node_id,
503 const SourceTable& source_table ) const override
504 {
505 for ( size_t lcid = 0; lcid < C_.size(); ++lcid )
506 {
507 if ( source_table.get_node_id( tid, syn_id, lcid ) == source_node_id
508 and C_[ lcid ].get_target( tid )->get_node_id() == target_node_id and not C_[ lcid ].is_disabled() )
509 {
510 return lcid;
511 }
512 }
513
514 return invalid_index;
515 }
516
517 void
518 disable_connection( const size_t lcid ) override
519 {
520 assert( not C_[ lcid ].is_disabled() );
521 C_[ lcid ].disable();
522 }
523
524 void
525 remove_disabled_connections( const size_t first_disabled_index ) override
526 {
527 assert( C_[ first_disabled_index ].is_disabled() );
528 C_.erase( C_.begin() + first_disabled_index, C_.end() );
529 }
530};
531
532} // of namespace nest
533
534#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:63
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 void get_connection_with_specified_targets(const size_t source_node_id, const std::vector< size_t > &target_neuron_node_ids, 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 ~ConnectorBase()
Definition connector_base.h:69
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_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:221
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:381
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:262
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:499
void push_back(const ConnectionT &c)
Definition connector_base.h:270
void get_connection_with_specified_targets(const size_t source_node_id, const std::vector< size_t > &target_neuron_node_ids, 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:303
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:238
void disable_connection(const size_t lcid) override
Disable the transfer of events through the connection at position lcid.
Definition connector_base.h:518
void remove_disabled_connections(const size_t first_disabled_index) override
Remove disabled connections from the connector.
Definition connector_base.h:525
const synindex syn_id_
Definition connector_base.h:224
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:441
size_t size() const override
Return the number of connections in this Connector.
Definition connector_base.h:244
Connector(const synindex syn_id)
Definition connector_base.h:227
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:399
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:351
void push_back(ConnectionT &&c)
Definition connector_base.h:276
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:325
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:475
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:282
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:223
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:375
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:250
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:469
~Connector() override
Definition connector_base.h:232
void sort_connections(BlockVector< Source > &sources) override
Sort connections according to source node IDs.
Definition connector_base.h:463
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:338
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
size_t synindex
For enumerations of synapse types.
Definition nest_types.h:115
Declarations for base class Node.