-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathconnection_manager.cpp
1431 lines (1261 loc) · 44.6 KB
/
connection_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* connection_manager.cpp
*
* This file is part of NEST.
*
* Copyright (C) 2004 The NEST Initiative
*
* NEST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* NEST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NEST. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "connection_manager.h"
// Generated includes:
#include "config.h"
// C++ includes:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <limits>
#include <set>
#include <vector>
// Includes from libnestutil:
#include "compose.hpp"
#include "logging.h"
// Includes from nestkernel:
#include "clopath_archiving_node.h"
#include "conn_builder.h"
#include "conn_builder_factory.h"
#include "connection_label.h"
#include "connector_base.h"
#include "connector_model.h"
#include "delay_checker.h"
#include "exceptions.h"
#include "kernel_manager.h"
#include "mpi_manager_impl.h"
#include "nest_names.h"
#include "node.h"
#include "target_table_devices_impl.h"
#include "vp_manager_impl.h"
// Includes from sli:
#include "dictutils.h"
#include "sliexceptions.h"
#include "token.h"
#include "tokenutils.h"
nest::ConnectionManager::ConnectionManager()
: connruledict_( new Dictionary() )
, connbuilder_factories_()
, min_delay_( 1 )
, max_delay_( 1 )
, keep_source_table_( true )
, have_connections_changed_()
, sort_connections_by_source_( true )
, has_primary_connections_( false )
, check_primary_connections_()
, secondary_connections_exist_( false )
, check_secondary_connections_()
, stdp_eps_( 1.0e-6 )
{
}
nest::ConnectionManager::~ConnectionManager()
{
// Memory leak on purpose!
// The ConnectionManager is deleted, when the network is deleted, and
// this happens only, when main() is finished and we give the allocated memory
// back to the system anyway. Hence, why bother cleaning up our highly
// scattered connection infrastructure? They do not have any open files, which
// need to be closed or similar.
}
void
nest::ConnectionManager::initialize()
{
const thread num_threads = kernel().vp_manager.get_num_threads();
connections_.resize( num_threads );
secondary_recv_buffer_pos_.resize( num_threads );
sort_connections_by_source_ = true;
have_connections_changed_.initialize( num_threads, true );
check_primary_connections_.initialize( num_threads, false );
check_secondary_connections_.initialize( num_threads, false );
#pragma omp parallel
{
const thread tid = kernel().vp_manager.get_thread_id();
connections_[ tid ] = std::vector< ConnectorBase* >( kernel().model_manager.get_num_synapse_prototypes() );
secondary_recv_buffer_pos_[ tid ] = std::vector< std::vector< size_t > >();
} // of omp parallel
source_table_.initialize();
target_table_.initialize();
target_table_devices_.initialize();
std::vector< DelayChecker > tmp( kernel().vp_manager.get_num_threads() );
delay_checkers_.swap( tmp );
std::vector< std::vector< size_t > > tmp2( kernel().vp_manager.get_num_threads(), std::vector< size_t >() );
num_connections_.swap( tmp2 );
// The following line is executed by all processes, no need to communicate
// this change in delays.
min_delay_ = max_delay_ = 1;
}
void
nest::ConnectionManager::finalize()
{
source_table_.finalize();
target_table_.finalize();
target_table_devices_.finalize();
delete_connections_();
std::vector< std::vector< ConnectorBase* > >().swap( connections_ );
std::vector< std::vector< std::vector< size_t > > >().swap( secondary_recv_buffer_pos_ );
}
void
nest::ConnectionManager::set_status( const DictionaryDatum& d )
{
for ( size_t i = 0; i < delay_checkers_.size(); ++i )
{
delay_checkers_[ i ].set_status( d );
}
updateValue< bool >( d, names::keep_source_table, keep_source_table_ );
if ( not keep_source_table_ and kernel().sp_manager.is_structural_plasticity_enabled() )
{
throw KernelException(
"If structural plasticity is enabled, keep_source_table can not be set "
"to false." );
}
updateValue< bool >( d, names::sort_connections_by_source, sort_connections_by_source_ );
if ( not sort_connections_by_source_ and kernel().sp_manager.is_structural_plasticity_enabled() )
{
throw KernelException(
"If structural plasticity is enabled, sort_connections_by_source can not "
"be set to false." );
}
// Need to update the saved values if we have changed the delay bounds.
if ( d->known( names::min_delay ) or d->known( names::max_delay ) )
{
update_delay_extrema_();
}
}
nest::DelayChecker&
nest::ConnectionManager::get_delay_checker()
{
return delay_checkers_[ kernel().vp_manager.get_thread_id() ];
}
void
nest::ConnectionManager::get_status( DictionaryDatum& dict )
{
update_delay_extrema_();
def< double >( dict, names::min_delay, Time( Time::step( min_delay_ ) ).get_ms() );
def< double >( dict, names::max_delay, Time( Time::step( max_delay_ ) ).get_ms() );
const size_t n = get_num_connections();
def< long >( dict, names::num_connections, n );
def< bool >( dict, names::keep_source_table, keep_source_table_ );
def< bool >( dict, names::sort_connections_by_source, sort_connections_by_source_ );
}
DictionaryDatum
nest::ConnectionManager::get_synapse_status( const index source_node_id,
const index target_node_id,
const thread tid,
const synindex syn_id,
const index lcid ) const
{
kernel().model_manager.assert_valid_syn_id( syn_id );
DictionaryDatum dict( new Dictionary );
( *dict )[ names::source ] = source_node_id;
( *dict )[ names::synapse_model ] = LiteralDatum( kernel().model_manager.get_synapse_prototype( syn_id ).get_name() );
( *dict )[ names::target_thread ] = tid;
( *dict )[ names::synapse_id ] = syn_id;
( *dict )[ names::port ] = lcid;
const Node* source = kernel().node_manager.get_node_or_proxy( source_node_id, tid );
const Node* target = kernel().node_manager.get_node_or_proxy( target_node_id, tid );
// synapses from neurons to neurons and from neurons to globally
// receiving devices
if ( ( source->has_proxies() and target->has_proxies() and connections_[ tid ][ syn_id ] != NULL )
or ( ( source->has_proxies() and not target->has_proxies() and not target->local_receiver()
and connections_[ tid ][ syn_id ] != NULL ) ) )
{
connections_[ tid ][ syn_id ]->get_synapse_status( tid, lcid, dict );
}
else if ( source->has_proxies() and not target->has_proxies() and target->local_receiver() )
{
target_table_devices_.get_synapse_status_to_device( tid, source_node_id, syn_id, dict, lcid );
}
else if ( not source->has_proxies() )
{
const index ldid = source->get_local_device_id();
target_table_devices_.get_synapse_status_from_device( tid, ldid, syn_id, dict, lcid );
}
else
{
assert( false );
}
return dict;
}
void
nest::ConnectionManager::set_synapse_status( const index source_node_id,
const index target_node_id,
const thread tid,
const synindex syn_id,
const index lcid,
const DictionaryDatum& dict )
{
kernel().model_manager.assert_valid_syn_id( syn_id );
const Node* source = kernel().node_manager.get_node_or_proxy( source_node_id, tid );
const Node* target = kernel().node_manager.get_node_or_proxy( target_node_id, tid );
try
{
ConnectorModel& cm = kernel().model_manager.get_synapse_prototype( syn_id, tid );
// synapses from neurons to neurons and from neurons to globally
// receiving devices
if ( ( source->has_proxies() and target->has_proxies() and connections_[ tid ][ syn_id ] != NULL )
or ( ( source->has_proxies() and not target->has_proxies() and not target->local_receiver()
and connections_[ tid ][ syn_id ] != NULL ) ) )
{
connections_[ tid ][ syn_id ]->set_synapse_status( lcid, dict, cm );
}
else if ( source->has_proxies() and not target->has_proxies() and target->local_receiver() )
{
target_table_devices_.set_synapse_status_to_device( tid, source_node_id, syn_id, cm, dict, lcid );
}
else if ( not source->has_proxies() )
{
const index ldid = source->get_local_device_id();
target_table_devices_.set_synapse_status_from_device( tid, ldid, syn_id, cm, dict, lcid );
}
else
{
assert( false );
}
}
catch ( BadProperty& e )
{
throw BadProperty(
String::compose( "Setting status of '%1' connecting from node ID %2 to node ID %3 via port %4: %5",
kernel().model_manager.get_synapse_prototype( syn_id, tid ).get_name(),
source_node_id,
target_node_id,
lcid,
e.message() ) );
}
}
void
nest::ConnectionManager::delete_connections_()
{
#pragma omp parallel
{
const thread tid = kernel().vp_manager.get_thread_id();
for ( std::vector< ConnectorBase* >::iterator conn = connections_[ tid ].begin(); conn != connections_[ tid ].end();
++conn )
{
delete *conn;
}
} // end omp parallel
}
const nest::Time
nest::ConnectionManager::get_min_delay_time_() const
{
Time min_delay = Time::pos_inf();
std::vector< DelayChecker >::const_iterator it;
for ( it = delay_checkers_.begin(); it != delay_checkers_.end(); ++it )
{
min_delay = std::min( min_delay, it->get_min_delay() );
}
return min_delay;
}
const nest::Time
nest::ConnectionManager::get_max_delay_time_() const
{
Time max_delay = Time::get_resolution();
std::vector< DelayChecker >::const_iterator it;
for ( it = delay_checkers_.begin(); it != delay_checkers_.end(); ++it )
{
max_delay = std::max( max_delay, it->get_max_delay() );
}
return max_delay;
}
bool
nest::ConnectionManager::get_user_set_delay_extrema() const
{
bool user_set_delay_extrema = false;
std::vector< DelayChecker >::const_iterator it;
for ( it = delay_checkers_.begin(); it != delay_checkers_.end(); ++it )
{
user_set_delay_extrema |= it->get_user_set_delay_extrema();
}
return user_set_delay_extrema;
}
nest::ConnBuilder*
nest::ConnectionManager::get_conn_builder( const std::string& name,
NodeCollectionPTR sources,
NodeCollectionPTR targets,
const DictionaryDatum& conn_spec,
const DictionaryDatum& syn_spec )
{
const size_t rule_id = connruledict_->lookup( name );
return connbuilder_factories_.at( rule_id )->create( sources, targets, conn_spec, syn_spec );
}
void
nest::ConnectionManager::calibrate( const TimeConverter& tc )
{
for ( thread tid = 0; tid < kernel().vp_manager.get_num_threads(); ++tid )
{
delay_checkers_[ tid ].calibrate( tc );
}
}
void
nest::ConnectionManager::connect( NodeCollectionPTR sources,
NodeCollectionPTR targets,
const DictionaryDatum& conn_spec,
const DictionaryDatum& syn_spec )
{
conn_spec->clear_access_flags();
syn_spec->clear_access_flags();
if ( not conn_spec->known( names::rule ) )
{
throw BadProperty( "Connectivity spec must contain connectivity rule." );
}
const Name rule_name = static_cast< const std::string >( ( *conn_spec )[ names::rule ] );
if ( not connruledict_->known( rule_name ) )
{
throw BadProperty( String::compose( "Unknown connectivity rule: %1", rule_name ) );
}
const long rule_id = ( *connruledict_ )[ rule_name ];
ConnBuilder* cb = connbuilder_factories_.at( rule_id )->create( sources, targets, conn_spec, syn_spec );
assert( cb != 0 );
// at this point, all entries in conn_spec and syn_spec have been checked
ALL_ENTRIES_ACCESSED( *conn_spec, "Connect", "Unread dictionary entries in conn_spec: " );
ALL_ENTRIES_ACCESSED( *syn_spec, "Connect", "Unread dictionary entries in syn_spec: " );
cb->connect();
delete cb;
}
void
nest::ConnectionManager::connect( TokenArray sources, TokenArray targets, const DictionaryDatum& syn_spec )
{
// Get synapse id
size_t syn_id = 0;
auto synmodel = syn_spec->lookup( names::model );
if ( not synmodel.empty() )
{
std::string synmodel_name = getValue< std::string >( synmodel );
synmodel = kernel().model_manager.get_synapsedict()->lookup( synmodel_name );
if ( not synmodel.empty() )
{
syn_id = static_cast< size_t >( synmodel );
}
else
{
throw UnknownModelName( synmodel_name );
}
}
// Connect all sources to all targets
for ( auto&& source : sources )
{
auto source_node = kernel().node_manager.get_node_or_proxy( source );
for ( auto&& target : targets )
{
auto target_node = kernel().node_manager.get_node_or_proxy( target );
auto target_thread = target_node->get_thread();
connect_( *source_node, *target_node, source, target_thread, syn_id, syn_spec );
}
}
}
void
nest::ConnectionManager::update_delay_extrema_()
{
min_delay_ = get_min_delay_time_().get_steps();
max_delay_ = get_max_delay_time_().get_steps();
if ( not get_user_set_delay_extrema() )
{
// If no min/max_delay is set explicitly (SetKernelStatus), then the default
// delay used by the SPBuilders have to be respected for the min/max_delay.
min_delay_ = std::min( min_delay_, kernel().sp_manager.builder_min_delay() );
max_delay_ = std::max( max_delay_, kernel().sp_manager.builder_max_delay() );
}
if ( kernel().mpi_manager.get_num_processes() > 1 )
{
std::vector< delay > min_delays( kernel().mpi_manager.get_num_processes() );
min_delays[ kernel().mpi_manager.get_rank() ] = min_delay_;
kernel().mpi_manager.communicate( min_delays );
min_delay_ = *std::min_element( min_delays.begin(), min_delays.end() );
std::vector< delay > max_delays( kernel().mpi_manager.get_num_processes() );
max_delays[ kernel().mpi_manager.get_rank() ] = max_delay_;
kernel().mpi_manager.communicate( max_delays );
max_delay_ = *std::max_element( max_delays.begin(), max_delays.end() );
}
if ( min_delay_ == Time::pos_inf().get_steps() )
{
min_delay_ = Time::get_resolution().get_steps();
}
}
// node ID node thread syn_id dict delay weight
void
nest::ConnectionManager::connect( const index snode_id,
Node* target,
thread target_thread,
const synindex syn_id,
const DictionaryDatum& params,
const double delay,
const double weight )
{
kernel().model_manager.assert_valid_syn_id( syn_id );
set_have_connections_changed( target_thread );
Node* source = kernel().node_manager.get_node_or_proxy( snode_id, target_thread );
ConnectionType connection_type = connection_required( source, target, target_thread );
switch ( connection_type )
{
case CONNECT:
connect_( *source, *target, snode_id, target_thread, syn_id, params, delay, weight );
break;
case CONNECT_FROM_DEVICE:
connect_from_device_( *source, *target, target_thread, syn_id, params, delay, weight );
break;
case CONNECT_TO_DEVICE:
connect_to_device_( *source, *target, snode_id, target_thread, syn_id, params, delay, weight );
break;
case NO_CONNECTION:
return;
}
}
// node_id node_id dict syn_id
bool
nest::ConnectionManager::connect( const index snode_id,
const index tnode_id,
const DictionaryDatum& params,
const synindex syn_id )
{
kernel().model_manager.assert_valid_syn_id( syn_id );
const thread tid = kernel().vp_manager.get_thread_id();
set_have_connections_changed( tid );
if ( not kernel().node_manager.is_local_node_id( tnode_id ) )
{
return false;
}
Node* target = kernel().node_manager.get_node_or_proxy( tnode_id, tid );
const thread target_thread = target->get_thread();
Node* source = kernel().node_manager.get_node_or_proxy( snode_id, target_thread );
ConnectionType connection_type = connection_required( source, target, target_thread );
bool connected = true;
switch ( connection_type )
{
case CONNECT:
connect_( *source, *target, snode_id, target_thread, syn_id, params );
break;
case CONNECT_FROM_DEVICE:
connect_from_device_( *source, *target, target_thread, syn_id, params );
break;
case CONNECT_TO_DEVICE:
connect_to_device_( *source, *target, snode_id, target_thread, syn_id, params );
break;
case NO_CONNECTION:
connected = false;
break;
}
return connected;
}
void
nest::ConnectionManager::connect_( Node& s,
Node& r,
const index s_node_id,
const thread tid,
const synindex syn_id,
const DictionaryDatum& params,
const double delay,
const double weight )
{
const bool is_primary = kernel().model_manager.get_synapse_prototype( syn_id, tid ).is_primary();
if ( kernel().model_manager.connector_requires_clopath_archiving( syn_id )
and not dynamic_cast< Clopath_Archiving_Node* >( &r ) )
{
throw NotImplemented(
"This synapse model is not supported by the neuron model of at least one "
"connection." );
}
kernel()
.model_manager.get_synapse_prototype( syn_id, tid )
.add_connection( s, r, connections_[ tid ], syn_id, params, delay, weight );
source_table_.add_source( tid, syn_id, s_node_id, is_primary );
increase_connection_count( tid, syn_id );
// We do not check has_primary_connections_ and secondary_connections_exist_
// directly as this led to worse performance on the supercomputer Piz Daint.
if ( check_primary_connections_[ tid ].is_false() and is_primary )
{
#pragma omp atomic write
has_primary_connections_ = true;
check_primary_connections_[ tid ].set_true();
}
else if ( check_secondary_connections_[ tid ].is_false() and not is_primary )
{
#pragma omp atomic write
secondary_connections_exist_ = true;
check_secondary_connections_[ tid ].set_true();
}
}
void
nest::ConnectionManager::connect_to_device_( Node& s,
Node& r,
const index s_node_id,
const thread tid,
const synindex syn_id,
const DictionaryDatum& params,
const double delay,
const double weight )
{
// create entries in connection structure for connections to devices
target_table_devices_.add_connection_to_device( s, r, s_node_id, tid, syn_id, params, delay, weight );
increase_connection_count( tid, syn_id );
}
void
nest::ConnectionManager::connect_from_device_( Node& s,
Node& r,
const thread tid,
const synindex syn_id,
const DictionaryDatum& params,
const double delay,
const double weight )
{
// create entries in connections vector of devices
target_table_devices_.add_connection_from_device( s, r, tid, syn_id, params, delay, weight );
increase_connection_count( tid, syn_id );
}
void
nest::ConnectionManager::increase_connection_count( const thread tid, const synindex syn_id )
{
if ( num_connections_[ tid ].size() <= syn_id )
{
num_connections_[ tid ].resize( syn_id + 1 );
}
++num_connections_[ tid ][ syn_id ];
if ( num_connections_[ tid ][ syn_id ] >= MAX_LCID )
{
throw KernelException( String::compose(
"Too many connections: at most %1 connections supported per virtual "
"process and synapse model.",
MAX_LCID ) );
}
}
nest::index
nest::ConnectionManager::find_connection( const thread tid,
const synindex syn_id,
const index snode_id,
const index tnode_id )
{
// lcid will hold the position of the /first/ connection from node
// snode_id to any local node, or be invalid
index lcid = source_table_.find_first_source( tid, syn_id, snode_id );
if ( lcid == invalid_index )
{
return invalid_index;
}
// lcid will hold the position of the /first/ connection from node
// snode_id to node tnode_id, or be invalid
lcid = connections_[ tid ][ syn_id ]->find_first_target( tid, lcid, tnode_id );
if ( lcid != invalid_index )
{
return lcid;
}
return lcid;
}
void
nest::ConnectionManager::disconnect( const thread tid,
const synindex syn_id,
const index snode_id,
const index tnode_id )
{
set_have_connections_changed( tid );
assert( syn_id != invalid_synindex );
const index lcid = find_connection( tid, syn_id, snode_id, tnode_id );
if ( lcid == invalid_index ) // this function should only be called
// with a valid connection
{
throw InexistentConnection();
}
connections_[ tid ][ syn_id ]->disable_connection( lcid );
source_table_.disable_connection( tid, syn_id, lcid );
--num_connections_[ tid ][ syn_id ];
}
void
nest::ConnectionManager::trigger_update_weight( const long vt_id,
const std::vector< spikecounter >& dopa_spikes,
const double t_trig )
{
const thread tid = kernel().vp_manager.get_thread_id();
for ( std::vector< ConnectorBase* >::iterator it = connections_[ tid ].begin(); it != connections_[ tid ].end();
++it )
{
if ( *it != NULL )
{
( *it )->trigger_update_weight(
vt_id, tid, dopa_spikes, t_trig, kernel().model_manager.get_synapse_prototypes( tid ) );
}
}
}
size_t
nest::ConnectionManager::get_num_target_data( const thread tid ) const
{
size_t num_connections = 0;
for ( synindex syn_id = 0; syn_id < connections_[ tid ].size(); ++syn_id )
{
if ( connections_[ tid ][ syn_id ] != NULL )
{
num_connections += source_table_.num_unique_sources( tid, syn_id );
}
}
return num_connections;
}
size_t
nest::ConnectionManager::get_num_connections() const
{
size_t num_connections = 0;
for ( index t = 0; t < num_connections_.size(); ++t )
{
for ( index s = 0; s < num_connections_[ t ].size(); ++s )
{
num_connections += num_connections_[ t ][ s ];
}
}
return num_connections;
}
size_t
nest::ConnectionManager::get_num_connections( const synindex syn_id ) const
{
size_t num_connections = 0;
for ( index t = 0; t < num_connections_.size(); ++t )
{
if ( num_connections_[ t ].size() > syn_id )
{
num_connections += num_connections_[ t ][ syn_id ];
}
}
return num_connections;
}
ArrayDatum
nest::ConnectionManager::get_connections( const DictionaryDatum& params ) const
{
std::deque< ConnectionID > connectome;
const Token& source_t = params->lookup( names::source );
const Token& target_t = params->lookup( names::target );
const Token& syn_model_t = params->lookup( names::synapse_model );
NodeCollectionPTR source_a = NodeCollectionPTR( 0 );
NodeCollectionPTR target_a = NodeCollectionPTR( 0 );
long synapse_label = UNLABELED_CONNECTION;
updateValue< long >( params, names::synapse_label, synapse_label );
if ( not source_t.empty() )
{
source_a = getValue< NodeCollectionDatum >( source_t );
if ( not source_a->valid() )
{
throw KernelException( "GetConnection requires valid source NodeCollection." );
}
}
if ( not target_t.empty() )
{
target_a = getValue< NodeCollectionDatum >( target_t );
if ( not target_a->valid() )
{
throw KernelException( "GetConnection requires valid target NodeCollection." );
}
}
// If connections have changed, (re-)build presynaptic infrastructure,
// as this may involve sorting connections by source node IDs.
if ( have_connections_changed() )
{
if ( not kernel().simulation_manager.has_been_simulated() )
{
kernel().model_manager.create_secondary_events_prototypes();
}
#pragma omp parallel
{
const thread tid = kernel().vp_manager.get_thread_id();
kernel().simulation_manager.update_connection_infrastructure( tid );
}
}
size_t syn_id = 0;
// First we check, whether a synapse model is given.
// If not, we will iterate all.
if ( not syn_model_t.empty() )
{
Name synmodel_name = getValue< Name >( syn_model_t );
const Token synmodel = kernel().model_manager.get_synapsedict()->lookup( synmodel_name );
if ( not synmodel.empty() )
{
syn_id = static_cast< size_t >( synmodel );
}
else
{
throw UnknownModelName( synmodel_name.toString() );
}
get_connections( connectome, source_a, target_a, syn_id, synapse_label );
}
else
{
for ( syn_id = 0; syn_id < kernel().model_manager.get_num_synapse_prototypes(); ++syn_id )
{
get_connections( connectome, source_a, target_a, syn_id, synapse_label );
}
}
ArrayDatum result;
result.reserve( connectome.size() );
while ( not connectome.empty() )
{
result.push_back( ConnectionDatum( connectome.front() ) );
connectome.pop_front();
}
return result;
}
// Helper method which removes ConnectionIDs from input deque and
// appends them to output deque.
static inline std::deque< nest::ConnectionID >&
extend_connectome( std::deque< nest::ConnectionID >& out, std::deque< nest::ConnectionID >& in )
{
while ( not in.empty() )
{
out.push_back( in.front() );
in.pop_front();
}
return out;
}
void
nest::ConnectionManager::split_to_neuron_device_vectors_( const thread tid,
NodeCollectionPTR nodecollection,
std::vector< index >& neuron_node_ids,
std::vector< index >& device_node_ids ) const
{
NodeCollection::const_iterator t_id = nodecollection->begin();
for ( ; t_id < nodecollection->end(); ++t_id )
{
const index node_id = ( *t_id ).node_id;
const auto node = kernel().node_manager.get_node_or_proxy( node_id, tid );
// Normal neuron nodes have proxies. Globally receiving devices, e.g. volume transmitter, don't have a local
// receiver, but are connected in the same way as normal neuron nodes. Therefore they have to be treated as such
// here.
if ( node->has_proxies() or not node->local_receiver() )
{
neuron_node_ids.push_back( node_id );
}
else
{
device_node_ids.push_back( node_id );
}
}
}
void
nest::ConnectionManager::get_connections( std::deque< ConnectionID >& connectome,
NodeCollectionPTR source,
NodeCollectionPTR target,
synindex syn_id,
long synapse_label ) const
{
if ( is_source_table_cleared() )
{
throw KernelException(
"Invalid attempt to access connection information: source table was "
"cleared." );
}
const size_t num_connections = get_num_connections( syn_id );
if ( num_connections == 0 )
{
return;
}
if ( not source.get() and not target.get() )
{
#pragma omp parallel
{
thread tid = kernel().vp_manager.get_thread_id();
std::deque< ConnectionID > conns_in_thread;
ConnectorBase* connections = connections_[ tid ][ syn_id ];
if ( connections != NULL )
{
// Passing target_node_id = 0 ignores target_node_id while getting connections.
const size_t num_connections_in_thread = connections->size();
for ( index lcid = 0; lcid < num_connections_in_thread; ++lcid )
{
const index source_node_id = source_table_.get_node_id( tid, syn_id, lcid );
connections->get_connection( source_node_id, 0, tid, lcid, synapse_label, conns_in_thread );
}
}
target_table_devices_.get_connections( 0, 0, tid, syn_id, synapse_label, conns_in_thread );
if ( conns_in_thread.size() > 0 )
{
#pragma omp critical( get_connections )
{
extend_connectome( connectome, conns_in_thread );
}
}
} // of omp parallel
return;
} // if
else if ( not source.get() and target.get() )
{
#pragma omp parallel
{
thread tid = kernel().vp_manager.get_thread_id();
std::deque< ConnectionID > conns_in_thread;
// Split targets into neuron- and device-vectors.
std::vector< index > target_neuron_node_ids;
std::vector< index > target_device_node_ids;
split_to_neuron_device_vectors_( tid, target, target_neuron_node_ids, target_device_node_ids );
ConnectorBase* connections = connections_[ tid ][ syn_id ];
if ( connections != NULL )
{
const size_t num_connections_in_thread = connections->size();
for ( index lcid = 0; lcid < num_connections_in_thread; ++lcid )
{
const index source_node_id = source_table_.get_node_id( tid, syn_id, lcid );
connections->get_connection_with_specified_targets(
source_node_id, target_neuron_node_ids, tid, lcid, synapse_label, conns_in_thread );
}
for ( std::vector< index >::const_iterator t_node_id = target_neuron_node_ids.begin();
t_node_id != target_neuron_node_ids.end();
++t_node_id )
{
// target_table_devices_ contains connections both to and from
// devices. First we get connections from devices.
target_table_devices_.get_connections_from_devices_(
0, *t_node_id, tid, syn_id, synapse_label, conns_in_thread );
}
}
for ( std::vector< index >::const_iterator t_node_id = target_device_node_ids.begin();
t_node_id != target_device_node_ids.end();
++t_node_id )
{
// Then, we get connections to devices.
target_table_devices_.get_connections_to_devices_( 0, *t_node_id, tid, syn_id, synapse_label, conns_in_thread );
}
if ( conns_in_thread.size() > 0 )
{
#pragma omp critical( get_connections )
{
extend_connectome( connectome, conns_in_thread );
}
}
} // of omp parallel
return;
} // else if
else if ( source.get() )
{
#pragma omp parallel
{
thread tid = kernel().vp_manager.get_thread_id();
std::deque< ConnectionID > conns_in_thread;
// Split targets into neuron- and device-vectors.
std::vector< index > target_neuron_node_ids;
std::vector< index > target_device_node_ids;
if ( target.get() )
{
split_to_neuron_device_vectors_( tid, target, target_neuron_node_ids, target_device_node_ids );
}
const ConnectorBase* connections = connections_[ tid ][ syn_id ];
if ( connections != NULL )
{
const size_t num_connections_in_thread = connections->size();
for ( index lcid = 0; lcid < num_connections_in_thread; ++lcid )
{
const index source_node_id = source_table_.get_node_id( tid, syn_id, lcid );
if ( source->contains( source_node_id ) )
{
if ( not target.get() )
{
// Passing target_node_id = 0 ignores target_node_id while getting
// connections.
connections->get_connection( source_node_id, 0, tid, lcid, synapse_label, conns_in_thread );
}
else
{
connections->get_connection_with_specified_targets(
source_node_id, target_neuron_node_ids, tid, lcid, synapse_label, conns_in_thread );
}
}
}
}
NodeCollection::const_iterator s_id = source->begin();
for ( ; s_id < source->end(); ++s_id )