-
Notifications
You must be signed in to change notification settings - Fork 356
/
free_layer.h
382 lines (334 loc) · 11.7 KB
/
free_layer.h
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
/*
* free_layer.h
*
* 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/>.
*
*/
#ifndef FREE_LAYER_H
#define FREE_LAYER_H
// C++ includes:
#include <algorithm>
#include <limits>
#include <sstream>
// Includes from nestkernel:
#include "nest_names.h"
// Includes from sli:
#include "dictutils.h"
// Includes from spatial:
#include "layer.h"
#include "ntree_impl.h"
namespace nest
{
/**
* Layer with free positioning of neurons, positions specified by user.
*/
template < int D >
class FreeLayer : public Layer< D >
{
public:
Position< D > get_position( size_t sind ) const;
void set_status( const DictionaryDatum& );
void get_status( DictionaryDatum& ) const;
protected:
/**
* Communicate positions across MPI processes
* @param iter Insert iterator which will receive pairs of Position,node ID
* @param node_collection NodeCollection of the layer
*/
template < class Ins >
void communicate_positions_( Ins iter, NodeCollectionPTR node_collection );
void insert_global_positions_ntree_( Ntree< D, size_t >& tree, NodeCollectionPTR node_collection );
void insert_global_positions_vector_( std::vector< std::pair< Position< D >, size_t > >& vec,
NodeCollectionPTR node_collection );
/**
* Calculate the index in the position vector on this MPI process based on the local ID.
*
* @param lid local ID of the node
* @return index in the local position vector
*/
size_t lid_to_position_id_( size_t lid ) const;
//! Vector of positions.
std::vector< Position< D > > positions_;
size_t num_local_nodes_ = 0;
/**
* Class to be used when communicating positions across MPI processes.
*/
class NodePositionData
{
public:
size_t
get_node_id() const
{
return node_id_;
}
Position< D >
get_position() const
{
return Position< D >( pos_ );
}
bool
operator<( const NodePositionData& other ) const
{
return node_id_ < other.node_id_;
}
bool
operator==( const NodePositionData& other ) const
{
return node_id_ == other.node_id_;
}
private:
double node_id_;
double pos_[ D ];
};
};
template < int D >
void
FreeLayer< D >::set_status( const DictionaryDatum& d )
{
Layer< D >::set_status( d );
Position< D > max_point; // for each dimension, the largest value of the positions, aka upper right
for ( int d = 0; d < D; ++d )
{
this->lower_left_[ d ] = std::numeric_limits< double >::infinity();
max_point[ d ] = -std::numeric_limits< double >::infinity();
}
num_local_nodes_ = std::accumulate( this->node_collection_->begin(),
this->node_collection_->end(),
0,
[]( size_t a, NodeIDTriple b )
{
const auto node = kernel().node_manager.get_mpi_local_node_or_device_head( b.node_id );
return node->is_proxy() ? a : a + 1;
} );
// Read positions from dictionary
if ( d->known( names::positions ) )
{
const Token& tkn = d->lookup( names::positions );
if ( tkn.is_a< TokenArray >() )
{
TokenArray pos = getValue< TokenArray >( tkn );
positions_.clear();
positions_.reserve( num_local_nodes_ );
auto nc_it = this->node_collection_->begin();
for ( Token* it = pos.begin(); it != pos.end(); ++it, ++nc_it )
{
assert( nc_it != this->node_collection_->end() );
Position< D > point = getValue< std::vector< double > >( *it );
const auto node = kernel().node_manager.get_mpi_local_node_or_device_head( ( *nc_it ).node_id );
assert( node );
if ( not node->is_proxy() )
{
positions_.push_back( point );
}
// We do the calculation for lower_left_ and max_point even if we don't add the position, to keep the size of
// the layer consistent over processes.
for ( int d = 0; d < D; ++d )
{
if ( point[ d ] < this->lower_left_[ d ] )
{
this->lower_left_[ d ] = point[ d ];
}
if ( point[ d ] > max_point[ d ] )
{
max_point[ d ] = point[ d ];
}
}
}
assert( positions_.size() == num_local_nodes_ );
}
else if ( tkn.is_a< ParameterDatum >() )
{
auto pd = dynamic_cast< ParameterDatum* >( tkn.datum() );
auto pos = dynamic_cast< DimensionParameter* >( pd->get() );
positions_.clear();
positions_.reserve( num_local_nodes_ );
RngPtr rng = get_rank_synced_rng();
for ( auto nc_it = this->node_collection_->begin(); nc_it < this->node_collection_->end(); ++nc_it )
{
// We generate the position here, even if we do not store it, to do the same calculations of lower_left_ and
// max_point on all processes.
Position< D > point = pos->get_values( rng );
const auto node = kernel().node_manager.get_mpi_local_node_or_device_head( ( *nc_it ).node_id );
assert( node );
if ( not node->is_proxy() )
{
positions_.push_back( point );
}
// We do the calculation for lower_left_ and max_point even if we don't add the position, to keep the size of
// the layer consistent over processes.
for ( int d = 0; d < D; ++d )
{
if ( point[ d ] < this->lower_left_[ d ] )
{
this->lower_left_[ d ] = point[ d ];
}
if ( point[ d ] > max_point[ d ] )
{
max_point[ d ] = point[ d ];
}
}
}
assert( positions_.size() == num_local_nodes_ );
}
else
{
throw KernelException( "'positions' must be an array or a DimensionParameter." );
}
}
if ( d->known( names::extent ) )
{
this->extent_ = getValue< std::vector< double > >( d, names::extent );
Position< D > center = ( max_point + this->lower_left_ ) / 2;
auto lower_left_point = this->lower_left_; // save lower-left-most point
this->lower_left_ = center - this->extent_ / 2;
// check if all points are inside the specified layer extent
auto upper_right_limit = center + this->extent_ / 2;
for ( int d = 0; d < D; ++d )
{
if ( lower_left_point[ d ] < this->lower_left_[ d ] or max_point[ d ] > upper_right_limit[ d ] )
{
throw BadProperty( "Node position outside of layer" );
}
}
}
else
{
if ( this->node_collection_->size() <= 1 )
{
throw KernelException( "If only one node is created, 'extent' must be specified." );
}
const auto positional_extent = max_point - this->lower_left_;
const auto center = ( max_point + this->lower_left_ ) / 2;
for ( int d = 0; d < D; ++d )
{
// Set extent to be extent of the points, rounded up in each dimension.
this->extent_[ d ] = std::ceil( positional_extent[ d ] );
}
// Adjust lower_left relative to the rounded center with the rounded up extent.
this->lower_left_ = center - this->extent_ / 2;
}
}
template < int D >
void
FreeLayer< D >::get_status( DictionaryDatum& d ) const
{
Layer< D >::get_status( d );
TokenArray points;
for ( typename std::vector< Position< D > >::const_iterator it = positions_.begin(); it != positions_.end(); ++it )
{
points.push_back( it->getToken() );
}
def2< TokenArray, ArrayDatum >( d, names::positions, points );
}
template < int D >
Position< D >
FreeLayer< D >::get_position( size_t lid ) const
{
return positions_.at( lid_to_position_id_( lid ) );
}
template < int D >
template < class Ins >
void
FreeLayer< D >::communicate_positions_( Ins iter, NodeCollectionPTR node_collection )
{
// This array will be filled with node ID,pos_x,pos_y[,pos_z] for local nodes:
std::vector< double > local_node_id_pos;
// If the NodeCollection has proxies, nodes and positions are distributed over MPI processes,
// and we must iterate only the local nodes. If not, all nodes and positions are on all MPI processes.
// All models in a layer are the same, so if has_proxies() for the NodeCollection returns true, we
// know that all nodes in the NodeCollection have proxies. Likewise, if it returns false we know that
// no nodes have proxies.
NodeCollection::const_iterator nc_begin =
node_collection->has_proxies() ? node_collection->MPI_local_begin() : node_collection->begin();
NodeCollection::const_iterator nc_end = node_collection->end();
// Reserve capacity in the vector based on number of local nodes. If the NodeCollection is sliced,
// it may need less than the reserved capacity.
local_node_id_pos.reserve( ( D + 1 ) * num_local_nodes_ );
for ( NodeCollection::const_iterator nc_it = nc_begin; nc_it < nc_end; ++nc_it )
{
// Push node ID into array to communicate
local_node_id_pos.push_back( ( *nc_it ).node_id );
// Push coordinates one by one
const auto pos = get_position( ( *nc_it ).lid );
for ( int j = 0; j < D; ++j )
{
local_node_id_pos.push_back( pos[ j ] );
}
}
// This array will be filled with node ID,pos_x,pos_y[,pos_z] for global nodes:
std::vector< double > global_node_id_pos;
std::vector< int > displacements;
kernel().mpi_manager.communicate( local_node_id_pos, global_node_id_pos, displacements );
// To avoid copying the vector one extra time in order to sort, we
// sneakishly use reinterpret_cast
NodePositionData* pos_ptr;
NodePositionData* pos_end;
pos_ptr = reinterpret_cast< NodePositionData* >( &global_node_id_pos[ 0 ] );
pos_end = pos_ptr + global_node_id_pos.size() / ( D + 1 );
// Get rid of any multiple entries
std::sort( pos_ptr, pos_end );
pos_end = std::unique( pos_ptr, pos_end );
// Unpack node IDs and coordinates
for ( ; pos_ptr < pos_end; pos_ptr++ )
{
*iter++ = std::pair< Position< D >, size_t >( pos_ptr->get_position(), pos_ptr->get_node_id() );
}
}
template < int D >
void
FreeLayer< D >::insert_global_positions_ntree_( Ntree< D, size_t >& tree, NodeCollectionPTR node_collection )
{
communicate_positions_( std::inserter( tree, tree.end() ), node_collection );
}
// Helper function to compare node IDs used for sorting (Position,node ID) pairs
template < int D >
static bool
node_id_less( const std::pair< Position< D >, size_t >& a, const std::pair< Position< D >, size_t >& b )
{
return a.second < b.second;
}
template < int D >
void
FreeLayer< D >::insert_global_positions_vector_( std::vector< std::pair< Position< D >, size_t > >& vec,
NodeCollectionPTR node_collection )
{
communicate_positions_( std::back_inserter( vec ), node_collection );
// Sort vector to ensure consistent results
std::sort( vec.begin(), vec.end(), node_id_less< D > );
}
template < int D >
size_t
FreeLayer< D >::lid_to_position_id_( size_t lid ) const
{
// If the NodeCollection has proxies, nodes and positions are distributed over MPI processes,
// and we must iterate only the local nodes. If not, all nodes and positions are on all MPI processes.
// All models in a layer are the same, so if has_proxies() for the NodeCollection returns true, we
// know that all nodes in the NodeCollection have proxies. Likewise, if it returns false we know that
// no nodes have proxies.
if ( not this->node_collection_->has_proxies() )
{
return lid;
}
else
{
const auto num_procs = kernel().mpi_manager.get_num_processes();
return lid / num_procs;
}
}
} // namespace nest
#endif