GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
inputValue.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <functional>
5 #include <map>
6 #include <memory>
7 #include <string>
8 #include <vector>
9 
10 // Simulator includes
11 #include "networkClient.h"
12 
13 // Forward declarations
14 namespace pugi
15 {
16  class xml_node;
17 }
18 
19 //----------------------------------------------------------------------------
20 // SpineMLSimulator::InputValue
21 //----------------------------------------------------------------------------
22 namespace SpineMLSimulator
23 {
24 namespace InputValue
25 {
26 class Base
27 {
28 public:
29  virtual ~Base(){}
30 
31  //------------------------------------------------------------------------
32  // Declared virtuals
33  //------------------------------------------------------------------------
34  virtual void update(double dt, unsigned long long timestep,
35  std::function<void(unsigned int, double)> applyValueFunc) = 0;
36 
37 protected:
38  Base(unsigned int numNeurons, const pugi::xml_node &node);
39 
40  //------------------------------------------------------------------------
41  // Protected API
42  //------------------------------------------------------------------------
43  const std::vector<unsigned int> &getTargetIndices() const{ return m_TargetIndices; }
44  unsigned int getNumNeurons() const{ return m_NumNeurons; }
45 
46 private:
47  //------------------------------------------------------------------------
48  // Members
49  //------------------------------------------------------------------------
50  unsigned int m_NumNeurons;
51  std::vector<unsigned int> m_TargetIndices;
52 };
53 
54 //----------------------------------------------------------------------------
55 // SpineMLSimulator::InputValue::ScalarBase
56 //----------------------------------------------------------------------------
57 class ScalarBase : public Base
58 {
59 protected:
60  ScalarBase(unsigned int numNeurons, const pugi::xml_node &node) : Base(numNeurons, node){}
61 
62  //----------------------------------------------------------------------------
63  // Protected API
64  //----------------------------------------------------------------------------
65  void applyScalar(double value,
66  std::function<void(unsigned int, double)> applyValueFunc) const;
67 };
68 
69 //----------------------------------------------------------------------------
70 // SpineMLSimulator::InputValue::Constant
71 //----------------------------------------------------------------------------
72 class Constant : public ScalarBase
73 {
74 public:
75  Constant(double dt, unsigned int numNeurons, const pugi::xml_node &node);
76 
77  //------------------------------------------------------------------------
78  // InputValue virtuals
79  //------------------------------------------------------------------------
80  virtual void update(double dt, unsigned long long timestep,
81  std::function<void(unsigned int, double)> applyValueFunc) override;
82 private:
83  //------------------------------------------------------------------------
84  // Members
85  //------------------------------------------------------------------------
86  // Value to apply to targetted neurons throughout simulation
87  double m_Value;
88 };
89 
90 //----------------------------------------------------------------------------
91 // SpineMLSimulator::InputValue::ConstantArray
92 //----------------------------------------------------------------------------
93 class ConstantArray : public Base
94 {
95 public:
96  ConstantArray(double dt, unsigned int numNeurons, const pugi::xml_node &node);
97 
98  //------------------------------------------------------------------------
99  // InputValue virtuals
100  //------------------------------------------------------------------------
101  virtual void update(double dt, unsigned long long timestep,
102  std::function<void(unsigned int, double)> applyValueFunc) override;
103 private:
104  //------------------------------------------------------------------------
105  // Members
106  //------------------------------------------------------------------------
107  // Values to apply to targetted neurons throughout simulation
108  std::vector<double> m_Values;
109 };
110 
111 //----------------------------------------------------------------------------
112 // SpineMLSimulator::InputValue::TimeVarying
113 //----------------------------------------------------------------------------
114 class TimeVarying : public ScalarBase
115 {
116 public:
117  TimeVarying(double dt, unsigned int numNeurons, const pugi::xml_node &node);
118 
119  //------------------------------------------------------------------------
120  // InputValue virtuals
121  //------------------------------------------------------------------------
122  virtual void update(double dt, unsigned long long timestep,
123  std::function<void(unsigned int, double)> applyValueFunc) override;
124 private:
125  //----------------------------------------------------------------------------
126  // Members
127  //----------------------------------------------------------------------------
128  // Values to apply to all neurons at certain timesteps
129  std::map<unsigned int, double> m_TimeValues;
130 };
131 
132 //----------------------------------------------------------------------------
133 // SpineMLSimulator::InputValue::TimeVaryingArray
134 //----------------------------------------------------------------------------
135 class TimeVaryingArray : public Base
136 {
137 public:
138  TimeVaryingArray(double dt, unsigned int numNeurons, const pugi::xml_node &node);
139 
140  //------------------------------------------------------------------------
141  // InputValue virtuals
142  //------------------------------------------------------------------------
143  virtual void update(double dt, unsigned long long timestep,
144  std::function<void(unsigned int, double)> applyValueFunc) override;
145 
146 private:
147  //------------------------------------------------------------------------
148  // Typedefines
149  //------------------------------------------------------------------------
150  typedef std::vector<std::pair<unsigned int, double>> NeuronValueVec;
151 
152  //------------------------------------------------------------------------
153  // Members
154  //------------------------------------------------------------------------
155  // Vector of neurons and values to apply at certain timesteps
156  std::map<unsigned int, NeuronValueVec> m_TimeArrays;
157 };
158 
159 //----------------------------------------------------------------------------
160 // SpineMLSimulator::InputValue::External
161 //----------------------------------------------------------------------------
162 class External : public Base
163 {
164 public:
165  External(double dt, unsigned int numNeurons, const pugi::xml_node &node);
166 
167  //------------------------------------------------------------------------
168  // InputValue virtuals
169  //------------------------------------------------------------------------
170  virtual void update(double dt, unsigned long long timestep,
171  std::function<void(unsigned int, double)> applyValueFunc) final;
172 
173  //------------------------------------------------------------------------
174  // Public API
175  //------------------------------------------------------------------------
176  std::vector<double>::iterator getBufferBegin(){ return m_Buffer.begin(); }
177  std::vector<double>::iterator getBufferEnd(){ return m_Buffer.end(); }
178 
179 protected:
180  //------------------------------------------------------------------------
181  // Declared virtuals
182  //------------------------------------------------------------------------
183  virtual void updateInternal(){}
184 
185  //------------------------------------------------------------------------
186  // Protected API
187  //------------------------------------------------------------------------
188  unsigned int getSize() const
189  {
190  return getTargetIndices().empty() ? getNumNeurons() : (unsigned int)getTargetIndices().size();
191  }
192 
193  std::vector<double> &getBuffer(){ return m_Buffer; }
194 
195 private:
196  //------------------------------------------------------------------------
197  // Members
198  //------------------------------------------------------------------------
199  std::vector<double> m_Buffer;
200 
201  // How many GeNN timesteps do we wait before updating
202  unsigned int m_IntervalTimesteps;
203 
204  // Count down to next time we update
205  unsigned int m_CurrentIntervalTimesteps;
206 };
207 
208 //----------------------------------------------------------------------------
209 // SpineMLSimulator::InputValue::ExternalNetwork
210 //----------------------------------------------------------------------------
211 class ExternalNetwork : public External
212 {
213 public:
214  ExternalNetwork(double dt, unsigned int numNeurons, const pugi::xml_node &node);
215 
216 protected:
217  //------------------------------------------------------------------------
218  // External virtuals
219  //------------------------------------------------------------------------
220  virtual void updateInternal() override;
221 
222 private:
223  //------------------------------------------------------------------------
224  // Members
225  //------------------------------------------------------------------------
226  NetworkClient m_Client;
227 };
228 
229 //----------------------------------------------------------------------------
230 // Functions
231 //----------------------------------------------------------------------------
232 std::unique_ptr<Base> create(double dt, unsigned int numNeurons, const pugi::xml_node &node,
233  std::map<std::string, InputValue::External*> &externalInputs);
234 
235 } // namespace InputValue
236 } // namespace SpineMLSimulator
Definition: networkClient.h:27
Definition: inputValue.h:72
ScalarBase(unsigned int numNeurons, const pugi::xml_node &node)
Definition: inputValue.h:60
Definition: inputValue.h:26
std::vector< double > & getBuffer()
Definition: inputValue.h:193
std::unique_ptr< Base > create(double dt, unsigned int numNeurons, const pugi::xml_node &node, std::map< std::string, InputValue::External *> &externalInputs)
Definition: inputValue.cc:334
Definition: inputValue.h:114
const std::vector< unsigned int > & getTargetIndices() const
Definition: inputValue.h:43
unsigned int getNumNeurons() const
Definition: inputValue.h:44
Definition: connectors.h:25
std::vector< double >::iterator getBufferEnd()
Definition: inputValue.h:177
virtual ~Base()
Definition: inputValue.h:29
Definition: connectors.h:12
Definition: inputValue.h:162
unsigned int getSize() const
Definition: inputValue.h:188
Definition: inputValue.h:57
std::vector< double >::iterator getBufferBegin()
Definition: inputValue.h:176
virtual void updateInternal()
Definition: inputValue.h:183