forked from nest/nest-simulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmusic_event_out_proxy.cpp
More file actions
229 lines (187 loc) · 5.79 KB
/
Copy pathmusic_event_out_proxy.cpp
File metadata and controls
229 lines (187 loc) · 5.79 KB
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
/*
* music_event_out_proxy.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 "music_event_out_proxy.h"
#ifdef HAVE_MUSIC
// C++ includes:
#include <numeric>
// Includes from sli:
#include "arraydatum.h"
#include "dict.h"
#include "dictutils.h"
#include "doubledatum.h"
#include "integerdatum.h"
// Includes from libnestutil:
#include "compose.hpp"
#include "logging.h"
// Includes from nestkernel:
#include "kernel_manager.h"
/* ----------------------------------------------------------------
* Default constructors defining default parameters and state
* ---------------------------------------------------------------- */
nest::music_event_out_proxy::Parameters_::Parameters_()
: port_name_( "event_out" )
{
}
nest::music_event_out_proxy::State_::State_()
: published_( false )
, port_width_( -1 )
{
}
/* ----------------------------------------------------------------
* Parameter extraction and manipulation functions
* ---------------------------------------------------------------- */
void
nest::music_event_out_proxy::Parameters_::get( DictionaryDatum& d ) const
{
( *d )[ names::port_name ] = port_name_;
}
void
nest::music_event_out_proxy::Parameters_::set( const DictionaryDatum& d, State_& s )
{
// TODO: This is not possible, as P_ does not know about get_name()
// if(d->known(names::port_name) and s.published_)
// throw MUSICPortAlreadyPublished(get_name(), P_.port_name_);
if ( not s.published_ )
{
updateValue< string >( d, names::port_name, port_name_ );
}
}
void
nest::music_event_out_proxy::State_::get( DictionaryDatum& d ) const
{
( *d )[ names::published ] = published_;
( *d )[ names::port_width ] = port_width_;
}
void
nest::music_event_out_proxy::State_::set( const DictionaryDatum&, const Parameters_& )
{
}
/* ----------------------------------------------------------------
* Default and copy constructor for node
* ---------------------------------------------------------------- */
nest::music_event_out_proxy::music_event_out_proxy()
: DeviceNode()
, P_()
, S_()
{
}
nest::music_event_out_proxy::music_event_out_proxy( const music_event_out_proxy& n )
: DeviceNode( n )
, P_( n.P_ )
, S_( n.S_ )
{
}
nest::music_event_out_proxy::~music_event_out_proxy()
{
if ( S_.published_ )
{
delete V_.MP_;
delete V_.music_perm_ind_;
}
}
void
nest::music_event_out_proxy::init_buffers_()
{
}
void
nest::music_event_out_proxy::pre_run_hook()
{
// only publish the output port once,
if ( not S_.published_ )
{
MUSIC::Setup* s = kernel().music_manager.get_music_setup();
if ( s == 0 )
{
throw MUSICSimulationHasRun( get_name() );
}
V_.MP_ = s->publishEventOutput( P_.port_name_ );
if ( not V_.MP_->isConnected() )
{
throw MUSICPortUnconnected( get_name(), P_.port_name_ );
}
if ( not V_.MP_->hasWidth() )
{
throw MUSICPortHasNoWidth( get_name(), P_.port_name_ );
}
S_.port_width_ = V_.MP_->width();
// check, if there are connections to receiver ports, which are
// beyond the width of the port
std::vector< MUSIC::GlobalIndex >::const_iterator it;
for ( it = V_.index_map_.begin(); it != V_.index_map_.end(); ++it )
{
if ( *it > S_.port_width_ )
{
throw UnknownReceptorType( *it, get_name() );
}
}
// The permutation index map, contains global_index[local_index]
V_.music_perm_ind_ = new MUSIC::PermutationIndex( &V_.index_map_.front(), V_.index_map_.size() );
// we identify channels by global indices within NEST
V_.MP_->map( V_.music_perm_ind_, MUSIC::Index::GLOBAL );
S_.published_ = true;
std::string msg = String::compose( "Mapping MUSIC output port '%1' with width=%2.", P_.port_name_, S_.port_width_ );
LOG( M_INFO, "MusicEventHandler::publish_port()", msg.c_str() );
}
}
void
nest::music_event_out_proxy::get_status( DictionaryDatum& d ) const
{
P_.get( d );
S_.get( d );
( *d )[ names::connection_count ] = V_.index_map_.size();
// make a copy, since MUSIC uses int instead of long int
std::vector< long >* pInd_map_long = new std::vector< long >( V_.index_map_.size() );
std::copy< std::vector< MUSIC::GlobalIndex >::const_iterator, std::vector< long >::iterator >(
V_.index_map_.begin(), V_.index_map_.end(), pInd_map_long->begin() );
( *d )[ names::index_map ] = IntVectorDatum( pInd_map_long );
}
void
nest::music_event_out_proxy::set_status( const DictionaryDatum& d )
{
Parameters_ ptmp = P_; // temporary copy in case of errors
ptmp.set( d, S_ ); // throws if BadProperty
State_ stmp = S_;
stmp.set( d, P_ ); // throws if BadProperty
// if we get here, temporaries contain consistent set of properties
P_ = ptmp;
S_ = stmp;
}
void
nest::music_event_out_proxy::handle( SpikeEvent& e )
{
assert( e.get_multiplicity() > 0 );
// propagate the spikes to MUSIC port
double time = e.get_stamp().get_ms() * 1e-3; // event time in seconds
long receiver_port = e.get_rport();
#ifdef _OPENMP
#pragma omp critical( insertevent )
{
#endif
for ( size_t i = 0; i < e.get_multiplicity(); ++i )
{
V_.MP_->insertEvent( time, MUSIC::GlobalIndex( receiver_port ) );
}
#ifdef _OPENMP
}
#endif
}
#endif