connection_updater.h
1 /*
2  * This file is part of SPORE.
3  *
4  * Copyright (C) 2016, the SPORE team (see AUTHORS).
5  *
6  * SPORE is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * SPORE is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with SPORE. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * For more information see: https://github.com/IGITUGraz/spore-nest-module
20  *
21  * File: connection_updater.h
22  * Author: Kappel
23  *
24  * Created on October 11, 2016, 7:09 PM
25  */
26 
27 #ifndef CONNECTION_UPDATER_H
28 #define CONNECTION_UPDATER_H
29 
30 #include <vector>
31 #include <set>
32 
33 #include "spore.h"
34 
35 #include "nest.h"
36 #include "event.h"
37 #include "node.h"
38 #include "nestmodule.h"
39 #include "dictdatum.h"
40 
41 #include "connection_manager_impl.h"
42 #include "connector_model_impl.h"
43 #include "kernel_manager.h"
44 
45 
46 namespace spore
47 {
48 
111 {
112 public:
115 
116  void init(nest::index cu_model_id);
117  void setup(long interval, long exceptable_latency);
118 
119  void register_connector(nest::ConnectorBase* new_conn, nest::ConnectorBase* old_conn, nest::index sender_gid,
120  nest::thread th, nest::synindex syn_id);
121 
122  void trigger_garbage_collector(nest::index target_gid, nest::index sender_gid,
123  nest::thread target_thread, nest::synindex syn_id);
124 
128  inline nest::delay get_interval() const
129  {
130  return interval_;
131  }
132 
136  inline nest::delay get_acceptable_latency() const
137  {
138  return acceptable_latency_;
139  }
140 
144  inline nest::delay get_max_latency() const
145  {
146  return interval_ + acceptable_latency_ + nest::kernel().connection_manager.get_min_delay();
147  }
148 
152  inline nest::Time get_origin() const
153  {
154  return nest::kernel().simulation_manager.get_slice_origin();
155  }
156 
160  inline nest::Time get_horizon() const
161  {
162  return get_origin() - nest::Time(nest::Time::step(interval_ + acceptable_latency_));
163  }
164 
168  inline bool is_valid() const
169  {
170  return (interval_ > 0) && (acceptable_latency_ >= 0) && (cu_id_ != nest::invalid_index);
171  }
172 
176  inline bool has_connections() const
177  {
178  return has_connections_;
179  }
180 
184  inline bool is_initialized() const
185  {
186  return is_initialized_;
187  }
188 
190 
191 private:
192  friend class ConnectionUpdater;
193 
198 
199  void execute_garbage_collector(nest::thread th);
200  void update(const nest::Time& time, nest::thread th);
201  void calibrate(nest::thread th);
202  void finalize(nest::thread th);
203  void prepare();
204  void reset();
205  void finalize();
206 
210  class ConnectionEntry
211  {
212  public:
213  ConnectionEntry( nest::ConnectorBase* connector, nest::Node* sender=0 )
214  : connector_(connector),
215  sender_(sender)
216  {
217  }
218 
219  ConnectionEntry( const ConnectionEntry& src )
220  : connector_(src.connector_),
221  sender_(src.sender_)
222  {
223  assert(sender_);
224  }
225 
226  bool operator==(const ConnectionEntry& conn) const
227  {
228  return (conn.connector_ == connector_);
229  }
230 
231  bool operator!=(const ConnectionEntry& conn) const
232  {
233  return (conn.connector_ != connector_);
234  }
235 
236  bool operator<=(const ConnectionEntry& conn) const
237  {
238  return (conn.connector_ <= connector_);
239  }
240 
241  bool operator>=(const ConnectionEntry& conn) const
242  {
243  return (conn.connector_ >= connector_);
244  }
245 
246  bool operator<(const ConnectionEntry& conn) const
247  {
248  return (conn.connector_ < connector_);
249  }
250 
251  bool operator>(const ConnectionEntry& conn) const
252  {
253  return (conn.connector_ > connector_);
254  }
255 
256  const ConnectionEntry& operator=(const ConnectionEntry& conn)
257  {
258  assert(sender_);
259  connector_ = conn.connector_;
260  sender_ = conn.sender_;
261  return *this;
262  }
263 
264  nest::ConnectorBase* get_connector() const
265  {
266  return connector_;
267  }
268 
269  nest::Node& get_sender() const
270  {
271  assert(sender_);
272  return *sender_;
273  }
274 
275  private:
276  nest::ConnectorBase* connector_;
277  nest::Node* sender_;
278  };
279 
283  class GarbageCollectorEntry
284  {
285  public:
286  GarbageCollectorEntry(nest::index target_gid, nest::index sender_gid, nest::synindex syn_id)
287  : target_gid_(target_gid),
288  sender_gid_(sender_gid),
289  syn_id_(syn_id)
290  {
291  }
292 
293  GarbageCollectorEntry(const GarbageCollectorEntry& src)
294  : target_gid_(src.target_gid_),
295  sender_gid_(src.sender_gid_),
296  syn_id_(src.syn_id_)
297  {
298  }
299 
300  const GarbageCollectorEntry& operator=(const GarbageCollectorEntry& src)
301  {
302  target_gid_ = src.target_gid_;
303  sender_gid_ = src.sender_gid_;
304  syn_id_ = src.syn_id_;
305  return *this;
306  }
307 
308  nest::index get_target_gid() const
309  {
310  return target_gid_;
311  }
312 
313  nest::index get_sender_gid() const
314  {
315  return sender_gid_;
316  }
317 
318  nest::synindex get_syn_id() const
319  {
320  return syn_id_;
321  }
322 
323  private:
324  nest::index target_gid_;
325  nest::index sender_gid_;
326  nest::synindex syn_id_;
327  };
328 
332  std::vector< std::set< ConnectionEntry > > connectors_;
333 
337  std::vector< std::set< nest::synindex > > used_models_;
338 
342  std::vector< std::vector< GarbageCollectorEntry > > garbage_pile_;
343 
344  long acceptable_latency_;
345  long interval_;
346  nest::index cu_model_id_;
347  nest::index cu_id_;
348  bool has_connections_;
349  bool is_initialized_;
350 
351  static ConnectionUpdateManager* instance_;
352 };
353 
357 class ConnectionUpdater : public nest::Node
358 {
359 public:
362  virtual ~ConnectionUpdater();
363 
364  virtual void update(nest::Time const& origin, const long from, const long to);
365  virtual void calibrate();
366  virtual void finalize();
367 
368  void get_status(DictionaryDatum&) const;
369  void set_status(const DictionaryDatum&);
370 
371  inline
372  bool has_proxies() const
373  {
374  return false;
375  }
376 
377  inline
378  bool one_node_per_process() const
379  {
380  return false;
381  }
382 
383 private:
384 
385  virtual void init_buffers_();
386  virtual void init_state_(const nest::Node& proto);
387 };
388 
397 class SynapseUpdateEvent : public nest::Event
398 {
399 public:
400  // Constructor.
401 
403  {
404  set_rport(-1);
405  };
406 
407  // SynapseUpdateEvent are dummy events. Trying to clone them will raise
408  // an exception!
409 
410  virtual Event* clone() const
411  {
412  throw nest::KernelException("Trying to call clone() of a SynapseUpdateEvent is illegal!");
413  return 0;
414  }
415 
416  // SynapseUpdateEvent are dummy events. Trying to send them will raise
417  // an exception!
418 
419  virtual void operator()()
420  {
421  throw nest::KernelException("Trying to send a SynapseUpdateEvent is illegal!");
422  }
423 };
424 
425 }
426 
427 #endif
~ConnectionUpdateManager()
Definition: connection_updater.cpp:52
bool is_initialized() const
Definition: connection_updater.h:184
Class that manages updating diligent connections.
Definition: connection_updater.h:110
Nest node to handle synapse updates on regular time grid.
Definition: connection_updater.h:357
void init(nest::index cu_model_id)
Definition: connection_updater.cpp:103
nest::delay get_acceptable_latency() const
Definition: connection_updater.h:136
void trigger_garbage_collector(nest::index target_gid, nest::index sender_gid, nest::thread target_thread, nest::synindex syn_id)
Definition: connection_updater.cpp:211
bool has_connections() const
Definition: connection_updater.h:176
nest::Time get_origin() const
Definition: connection_updater.h:152
ConnectionUpdateManager()
Definition: connection_updater.cpp:39
void register_connector(nest::ConnectorBase *new_conn, nest::ConnectorBase *old_conn, nest::index sender_gid, nest::thread th, nest::synindex syn_id)
Definition: connection_updater.cpp:160
nest::delay get_max_latency() const
Definition: connection_updater.h:144
nest::Time get_horizon() const
Definition: connection_updater.h:160
The dummy event class used to trigger synapse updates.
Definition: connection_updater.h:397
static ConnectionUpdateManager * instance()
Definition: connection_updater.cpp:312
nest::delay get_interval() const
Definition: connection_updater.h:128
void setup(long interval, long exceptable_latency)
Sets up the ConnectionUpdateManager with the given update interval and acceptable latency...
Definition: connection_updater.cpp:69
Global namespace holding all classes of the SPORE NEST module.
Definition: circular_buffer.h:31
bool is_valid() const
Definition: connection_updater.h:168