NEST  2.6.0,not_revisioned_source_dir@0
scheduler.h
Go to the documentation of this file.
1 /*
2  * scheduler.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 SCHEDULER_H
24 #define SCHEDULER_H
25 #include <queue>
26 #include <vector>
27 #include <iostream>
28 #include <iomanip>
29 #include <limits>
30 #include <fstream>
31 #include <sys/time.h>
32 
33 #include "nest.h"
34 #include "nest_time.h"
35 #include "nodelist.h"
36 #include "event.h"
37 #include "event_priority.h"
38 #include "randomgen.h"
39 #include "lockptr.h"
40 #include "communicator.h"
41 
42 namespace nest
43 {
44 
45  using std::priority_queue;
46  using std::vector;
48 
49  class Network;
50 
66  class Scheduler {
67 
68  public: // Public methods
69 
70  Scheduler(Network &);
71  virtual ~Scheduler();
72 
78  void reset();
79 
84  void clear_pending_spikes();
85 
94  void simulate(Time const&);
95 
97  void resume();
98 
102  void prepare_simulation();
103 
107  void finalize_simulation();
108 
109  void terminate();
110 
128  void send_remote(thread p, SpikeEvent&, const long_t lag = 0);
129 
148  void send_offgrid_remote(thread p, SpikeEvent&, const long_t lag = 0);
149 
151 
158  thread get_num_threads() const;
159 
165  void set_num_threads(thread n_threads);
166 
173  thread get_num_processes() const;
174 
177 
190  void set_num_rec_processes(int nrp, bool called_by_reset=false);
191 
195  void increment_n_gsd();
196 
200  index get_n_gsd();
201 
205  bool is_local_node(Node*) const;
206 
210  bool is_local_vp(thread) const;
211 
220  thread suggest_vp(index gid) const;
221 
230  thread suggest_rec_vp(index gid) const;
231 
232  thread vp_to_thread(thread vp) const;
233 
234  thread thread_to_vp(thread t) const;
235 
240 
246  thread get_process_id(thread vp) const;
247 
253  bool get_simulated() const;
254 
258  void set_off_grid_communication(bool off_grid_spiking);
259 
264  bool get_off_grid_communication() const;
265 
269  Time const& get_slice_origin() const;
270 
275 
281  Time const get_time() const;
282 
287  void update();
288 
289  void set_network_(Network*);
290 
291  void set_status(const DictionaryDatum&);
292  void get_status(DictionaryDatum &) const;
293 
297  librandom::RngPtr get_rng(const thread) const;
301  librandom::RngPtr get_grng() const;
302 
306  static
308 
313  static
315 
319  static
320  delay get_min_delay();
321 
325  static
327 
332  size_t get_slice() const;
333 
337  void calibrate_clock();
338 
343 
344  private:
345 
349  void init_();
350 
354  void finalize_();
355 
356  void advance_time_();
357 
358  void print_progress_();
359 
365  void prepare_nodes();
366 
371  void prepare_node_(Node *);
372 
376  void finalize_nodes();
377 
381  void compute_moduli_();
382 
383  void init_moduli_();
384 
385  void create_rngs_(const bool ctor_call = false);
386  void create_grng_(const bool ctor_call = false);
387 
394  static
395  void update_delay_extrema_();
396 
398  bool simulating_;
400 
402 
405 
407 
409  volatile index exit_counter_;
410 
411  vector<vector<Node*> > nodes_vec_;
413 
420 
421  timeval t_slice_begin_;
422  timeval t_slice_end_;
423  long t_real_;
424 
425  bool terminate_;
426  bool simulated_;
428  bool print_time_;
429 
430  std::vector<long_t> rng_seeds_;
432 
439  static
441 
447  static
449 
455  static
457 
466  static
467  vector<delay> moduli_;
468 
479  static
480  vector<delay> slice_moduli_;
481 
486  vector<librandom::RngPtr> rng_;
492 
500  std::vector<std::vector<std::vector<uint_t> > > spike_register_;
501 
509  std::vector<std::vector<std::vector<OffGridSpike> > >
511 
517  std::vector<uint_t> local_grid_spikes_;
518 
524  std::vector<uint_t> global_grid_spikes_;
525 
531  std::vector<OffGridSpike> local_offgrid_spikes_;
532 
538  std::vector<OffGridSpike> global_offgrid_spikes_;
539 
544  std::vector<int> displacements_;
545 
546 
551  static
553 
564 
565 
571  void update_nodes_vec_();
572 
578  void collocate_buffers_();
579 
583  void gather_events_();
584 
594  void deliver_events_(thread t);
595  };
596 
597  inline
599  {
600  return clock_;
601  }
602 
603  inline
605  {
606  return clock_ - Time::step(min_delay_);
607  }
608 
609  inline
610  Time const Scheduler::get_time() const
611  {
613  return clock_ + Time::step(from_step_);
614  }
615 
616  inline
618  {
619  return n_threads_;
620  }
621 
622  inline
624  {
626  }
627 
628  inline
630  {
631  return n_rec_procs_;
632  }
633 
634  inline
636  {
637  return n_sim_procs_;
638  }
639 
640  inline
642  {
643  ++n_gsd_;
644  }
645 
646  inline
648  {
649  return n_gsd_;
650  }
651 
652  inline
654  {
655  if (vp >= static_cast<thread>(n_sim_procs_ * n_threads_)) // vp belongs to recording VPs
656  {
657  return (vp-n_sim_procs_*n_threads_) % n_rec_procs_ + n_sim_procs_;
658  }
659  else // vp belongs to simulating VPs
660  {
661  return vp % n_sim_procs_;
662  }
663  }
664 
665  inline
667  {
668  return is_local_vp(n->get_vp());
669  }
670 
671  inline
673  {
674  return get_process_id(vp) == Communicator::get_rank();
675  }
676 
677  inline
679  {
680  return gid % (n_sim_procs_*n_threads_);
681  }
682 
683  inline
685  {
687  }
688 
689  inline
691  {
692  if (vp >= static_cast<thread>(n_sim_procs_ * n_threads_))
693  {
694  return (vp + n_sim_procs_*(1-n_threads_) - Communicator::get_rank() ) / n_rec_procs_;
695  }
696  else
697  {
698  return vp / n_sim_procs_;
699  }
700  }
701 
702  inline
704  {
705  if (Communicator::get_rank() >= static_cast<int>(n_sim_procs_)) // Rank is a recording process
706  {
708  }
709  else // Rank is a simulating process
710  {
711  return t * n_sim_procs_ + Communicator::get_rank();
712  }
713  }
714 
715  inline
717  {
718  return simulated_;
719  }
720 
721  inline
722  void Scheduler::set_off_grid_communication(bool off_grid_spiking)
723  {
724  off_grid_spiking_ = off_grid_spiking;
725  }
726 
727  inline
729  {
730  return off_grid_spiking_;
731  }
732 
733  inline
735  {
736  assert(thrd < static_cast<thread>(rng_.size()));
737  return rng_[thrd];
738  }
739 
740  inline
742  {
743  return grng_;
744  }
745 
746  inline
748  {
749  clock_.calibrate();
750  }
751 
752  inline
754  {
755  // Frozen nodes are initialized and calibrated, so that they
756  // have ring buffers and can accept incoming spikes.
757  n->init_buffers();
758  n->calibrate();
759  }
760 
761  inline
763  {
764  return nodes_vec_[t][thread_local_id];
765  }
766 
767  inline
769  {
770  // Put the spike in a buffer for the remote machines
771  for (int_t i = 0; i < e.get_multiplicity(); ++i)
773  }
774 
775  inline
777  {
778  // Put the spike in a buffer for the remote machines
779  OffGridSpike ogs(e.get_sender().get_gid(), e.get_offset());
780  for (int_t i = 0; i < e.get_multiplicity(); ++i)
781  offgrid_spike_register_[t][lag].push_back(ogs);
782  }
783 
784  inline
786  {
787  // Note, here d may be 0, since bin 0 represents the "current" time
788  // when all evens due are read out.
789  assert(static_cast<vector<delay>::size_type>(d) < moduli_.size());
790 
791  return moduli_[d];
792  }
793 
794  inline
796  {
797  // Note, here d may be 0, since bin 0 represents the "current" time
798  // when all evens due are read out.
799  assert(static_cast<vector<delay>::size_type>(d) < slice_moduli_.size());
800 
801  return slice_moduli_[d];
802  }
803 
804  inline
806  {
807  return min_delay_;
808  }
809 
810  inline
812  {
813  return max_delay_;
814  }
815 
816  inline
817  size_t Scheduler::get_slice() const
818  {
819  return slice_;
820  }
821 
822  inline
824  {
825  terminate_=true;
826  }
827 
828 }
829 
830 #endif //SCHEDULER_H
size_t index
Unsigned long type for enumerations.
Definition: nest.h:109
void print_progress_()
delay from_step_
update clock_+from_step<=T
Definition: scheduler.h:418
int int_t
Integer number with at least 16 bit.
Definition: nest.h:95
void increment_n_gsd()
Increment total number of global spike detectors by 1.
Definition: scheduler.h:641
static const delay comm_marker_
Marker Value to be put between the data fields from different time steps during communication.
Definition: scheduler.h:552
std::vector< uint_t > local_grid_spikes_
Buffer containing the gids of local neurons that spiked in the last min_delay_ interval.
Definition: scheduler.h:517
static vector< delay > slice_moduli_
Table of pre-computed slice-based modulos.
Definition: scheduler.h:480
volatile index exit_counter_
Counter for exit barrier.
Definition: scheduler.h:409
long_t grng_seed_
The seed of the global RNG, not neccessarily describing the state of the GRNG.
Definition: scheduler.h:431
void prepare_node_(Node *)
Initialized buffers, register in list of nodes to update/finalize.
Definition: scheduler.h:753
void reset()
Bring scheduler back to its initial state.
Definition: scheduler.cpp:109
delay to_do_
number of pending cycles.
Definition: scheduler.h:416
index get_n_gsd()
Get total number of global spike detectors.
Definition: scheduler.h:647
index get_gid() const
Return global Network ID.
Definition: node.h:753
thread get_num_processes() const
Return the number of processes used during simulation.
Definition: scheduler.h:623
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
void set_num_rec_processes(int nrp, bool called_by_reset=false)
Set number of recording processes, switches NEST to global spike detection mode.
thread vp_to_thread(thread vp) const
Definition: scheduler.h:690
std::vector< OffGridSpike > global_offgrid_spikes_
Buffer containing the gids and offsets for all neurons that fired off-grid spikes in the last min_del...
Definition: scheduler.h:538
Time const get_time() const
Precise time of simulation.
Definition: scheduler.h:610
void send_offgrid_remote(thread p, SpikeEvent &, const long_t lag=0)
Add global id of event sender to the spike_register.
Definition: scheduler.h:776
void set_num_threads(thread n_threads)
Set the number of threads by setting the internal variable n_threads_, the corresponding value in the...
thread get_num_threads() const
Return the number of threads used during simulation.
Definition: scheduler.h:617
std::vector< std::vector< std::vector< uint_t > > > spike_register_
Register for gids of neurons that spiked.
Definition: scheduler.h:500
void init_()
Initialize the scheduler by initializing the buffers.
Definition: scheduler.cpp:129
size_t get_slice() const
Get slice number.
Definition: scheduler.h:817
index n_gsd_
Total number of global spike detectors, used for distributing them over recording processes...
Definition: scheduler.h:406
void prepare_nodes()
Prepare nodes for simulation and register nodes in node_list.
Definition: scheduler.cpp:586
void terminate()
Definition: scheduler.h:823
void deliver_events_(thread t)
Read all event buffers for thread t and send the corresponding Events to the Nodes that are targeted...
static delay get_slice_modulo(delay d)
Index to slice-based buffer.
Definition: scheduler.h:795
void clear_pending_spikes()
Clear all pending spikes, but do not otherwise manipulate scheduler.
Definition: scheduler.cpp:124
index n_threads_
Number of threads per process.
Definition: scheduler.h:401
void update()
Update all non-frozen nodes.
Definition: scheduler.cpp:485
Scheduler(Network &)
Definition: scheduler.cpp:79
static void update_delay_extrema_()
Update delay extrema to current values.
Definition: scheduler.cpp:230
virtual void calibrate()=0
Re-calculate dependent parameters of the node.
static Network * net_
Pointer to network object.
Definition: scheduler.h:440
Node & get_sender() const
Return reference to sending Node.
Definition: event.h:732
static delay get_min_delay()
Return minimal connection delay.
Definition: scheduler.h:805
Time clock_
Network clock, updated once per slice.
Definition: scheduler.h:414
void finalize_nodes()
Invoke finalize() on nodes registered for finalization.
bool is_local_node(Node *) const
Return true if the node on the local machine, false if not.
Definition: scheduler.h:666
long t_real_
Accumunated wall-clock time spent simulating (in us)
Definition: scheduler.h:423
void advance_time_()
std::vector< OffGridSpike > local_offgrid_spikes_
Buffer containing the gids and offsets for local neurons that fired off-grid spikes in the last min_d...
Definition: scheduler.h:531
assert(pNet!=0)
void create_rngs_(const bool ctor_call=false)
Time const & get_slice_origin() const
Time at beginning of current slice.
Definition: scheduler.h:598
double_t get_offset() const
Return the creation time offset of the Event.
Definition: event.h:787
index nodes_vec_network_size_
Network size when nodes_vec_ was last updated.
Definition: scheduler.h:412
void calibrate()
Definition: nest_time.h:369
std::vector< int > displacements_
Buffer containing the starting positions for the spikes from each process within the global_(off)grid...
Definition: scheduler.h:544
thread get_num_rec_processes() const
Definition: scheduler.h:629
bool print_time_
Indicates whether time should be printed during simulations (or not)
Definition: scheduler.h:428
bool force_singlethreading_
Definition: scheduler.h:399
int_t get_multiplicity() const
Definition: event.h:352
bool simulating_
true if simulation in progress
Definition: scheduler.h:398
virtual ~Scheduler()
Definition: scheduler.cpp:104
Definition: nest_time.h:130
bool get_off_grid_communication() const
return current communication style.
Definition: scheduler.h:728
unsigned short targetindex
Unsigned short type for compact target representation.
Definition: nest.h:123
void set_network_(Network *)
thread get_process_id(thread vp) const
Return the process id for a given virtual process.
Definition: scheduler.h:653
void init_buffers()
Initialize buffers of a node.
Definition: node.cpp:73
void simulate(Time const &)
Simulate for the given time .
Definition: scheduler.cpp:292
static delay max_delay_
Value of the largest delay in the network.
Definition: scheduler.h:456
thread suggest_vp(index gid) const
Return a thread number for a given global node id.
Definition: scheduler.h:678
vector< vector< Node * > > nodes_vec_
Nodelists for nodes for each thread.
Definition: scheduler.h:411
void init_moduli_()
Definition: scheduler.cpp:173
static vector< delay > moduli_
Table of pre-computed modulos.
Definition: scheduler.h:467
void ensure_valid_thread_local_ids()
Ensure that all nodes in the network have valid thread-local IDs.
Definition: scheduler.h:342
librandom::RngPtr get_grng() const
Return pointer to global random number generator.
Definition: scheduler.h:741
librandom::RngPtr grng_
Global random number generator.
Definition: scheduler.h:491
const Name vp("vp")
Node parameter.
Definition: nest_names.h:341
Main administrative interface to the network.
Definition: network.h:135
thread thread_to_vp(thread t) const
Definition: scheduler.h:703
void set_off_grid_communication(bool off_grid_spiking)
set communication style to off_grid (true) or on_grid
Definition: scheduler.h:722
bool is_local_vp(thread) const
Return true if the thread is on the local machine, false if not.
Definition: scheduler.h:672
const Name thread_local_id("thread_local_id")
Thead-local ID of node, see Kunkel et al 2014, Sec 3.3.2.
Definition: nest_names.h:310
volatile index entry_counter_
Counter for entry barrier.
Definition: scheduler.h:408
void configure_spike_buffers_()
Resize spike_register and comm_buffer to correct dimensions.
Definition: scheduler.cpp:252
thread get_global_thread_id(thread lt) const
Return the global thread id of a local thread.
static int get_num_processes()
Definition: communicator.h:421
thread get_vp() const
Retrieve the number of the virtual process to which the node is assigned.
Definition: node.h:837
friend struct step
Definition: nest_time.h:171
delay slice_
current update slice
Definition: scheduler.h:415
bool terminate_
Terminate on signal or error.
Definition: scheduler.h:425
librandom::RngPtr get_rng(const thread) const
Return pointer to random number generator of the specified thread.
Definition: scheduler.h:734
void update_nodes_vec_()
Create up-to-date vector of local nodes, nodes_vec_.
index n_sim_procs_
MPI processes used for simulation.
Definition: scheduler.h:404
Combined storage of GID and offset information for off-grid spikes.
Definition: communicator.h:75
Time get_previous_slice_origin() const
Time at beginning of previous slice.
Definition: scheduler.h:604
timeval t_slice_begin_
Wall-clock time at the begin of a time slice.
Definition: scheduler.h:421
ad push_back(new StringDatum("("+modulepath+"/pynest-init.sli) run"))
void create_grng_(const bool ctor_call=false)
static delay get_max_delay()
Return maximal connection delay.
Definition: scheduler.h:811
void collocate_buffers_()
Rearrange the spike_register into a 2-dim structure.
index n_rec_procs_
MPI processes dedicated for recording devices.
Definition: scheduler.h:403
void calibrate_clock()
Calibrate clock after resolution change.
Definition: scheduler.h:747
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
long_t delay
Delay of a connection.
Definition: nest.h:178
Default types used by the NEST kernel.
static delay get_modulo(delay d)
Return (T+d) mod max_delay.
Definition: scheduler.h:785
Event for spike information.
Definition: event.h:320
std::vector< long_t > rng_seeds_
The seeds of the local RNGs. These do not neccessarily describe the state of the RNGs.
Definition: scheduler.h:430
static delay min_delay_
Value of the smallest delay in the network.
Definition: scheduler.h:448
bool simulated_
indicates whether the network has already been simulated for some time
Definition: scheduler.h:426
void gather_events_()
Collocate buffers and exchange events with other MPI processes.
Base class for all NEST network objects.
Definition: node.h:96
void send_remote(thread p, SpikeEvent &, const long_t lag=0)
Add global id of event sender to the spike_register.
Definition: scheduler.h:768
Communicator::OffGridSpike OffGridSpike
Definition: scheduler.h:47
bool off_grid_spiking_
indicates whether spikes are not constrained to the grid
Definition: scheduler.h:427
bool get_simulated() const
Return true, if the network has already been simulated for some time.
Definition: scheduler.h:716
timeval t_slice_end_
Wall-clock time at the end of time slice.
Definition: scheduler.h:422
std::vector< uint_t > global_grid_spikes_
Buffer containing the gids of all neurons that spiked in the last min_delay_ interval.
Definition: scheduler.h:524
Schedule update of Nodes and Events during simulation.
Definition: scheduler.h:66
static int get_rank()
Definition: communicator.h:416
void get_status(DictionaryDatum &) const
void finalize_()
Finalize the scheduler by freeing the buffers and destoying the mutexes.
Definition: scheduler.cpp:162
const Name p("p")
current release probability (Tsodyks2_connection)
Definition: nest_names.h:218
delay to_step_
update clock_+from_step<=T
Definition: scheduler.h:419
bool initialized_
Definition: scheduler.h:397
void finalize_simulation()
Cleanup after the simulation.
Definition: scheduler.cpp:411
int_t thread
Thread index type.
Definition: nest.h:133
long long_t
Integer number with at least 32 bit.
Definition: nest.h:96
vector< librandom::RngPtr > rng_
Vector of random number generators for threads.
Definition: scheduler.h:486
const double e
Definition: numerics.cpp:62
void resume()
Resume simulation after an interrupt.
Definition: scheduler.cpp:430
void compute_moduli_()
Re-compute table of fixed modulos, including slice-based.
Definition: scheduler.cpp:208
thread get_num_sim_processes() const
Definition: scheduler.h:635
Node * thread_lid_to_node(thread t, targetindex thread_local_id) const
Definition: scheduler.h:762
delay to_do_total_
number of requested cycles in current simulation.
Definition: scheduler.h:417
void prepare_simulation()
All steps that must be done before a simulation.
Definition: scheduler.cpp:365
std::vector< std::vector< std::vector< OffGridSpike > > > offgrid_spike_register_
Register for off-grid spikes.
Definition: scheduler.h:510
void set_status(const DictionaryDatum &)
thread suggest_rec_vp(index gid) const
Return a thread number for a given global recording node id.
Definition: scheduler.h:684