Cypress  1.0
C++ Spiking Neural Network Simulation Framework
genn_models.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2019 Christoph Ostrau
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include <sharedLibraryModel.h>
22 #include <genn/genn/modelSpecInternal.h>
23 #include <genn/genn/weightUpdateModels.h>
24 
25 namespace GeNNModels {
26 template <typename T>
27 class SharedLibraryModel_ : public SharedLibraryModel<T> {
28 public:
29  using SharedLibraryModel<T>::getSymbol;
30 };
31 
32 //_______________________Connectivity Models____________________________________
33 
34 // Adapted from GeNN repository
36  : public InitSparseConnectivitySnippet::Base {
37 public:
38  DECLARE_SNIPPET(FixedNumberPostWithReplacementNoAutapse, 1);
39 
40  SET_ROW_BUILD_CODE(
41  "const scalar u = $(gennrand_uniform);\n"
42  "x += (1.0 - x) * (1.0 - pow(u, 1.0 / (scalar)($(rowLength) - c)));\n"
43  "const unsigned int postIdx = (unsigned int)(x * $(num_post));\n"
44  "if(postIdx != $(id_pre)){\n"
45  "if(postIdx < $(num_post)) {\n"
46  " $(addSynapse, postIdx);\n"
47  "}\n"
48  "else {\n"
49  " $(addSynapse, $(num_post) - 1);\n"
50  "}\n"
51  "c++;\n"
52  "if(c >= $(rowLength)) {\n"
53  " $(endRow);\n"
54  "}}\n");
55  SET_ROW_BUILD_STATE_VARS({{"x", "scalar", 0.0}, {"c", "unsigned int", 0}});
56 
57  SET_PARAM_NAMES({"rowLength"});
58 
59  SET_CALC_MAX_ROW_LENGTH_FUNC([](unsigned int, unsigned int,
60  const std::vector<double> &pars) {
61  return (unsigned int)pars[0];
62  });
63 };
65 
66 // Adapted from GeNN repository
68  : public InitSparseConnectivitySnippet::Base {
69 public:
70  DECLARE_SNIPPET(FixedNumberPostWithReplacement, 1);
71 
72  SET_ROW_BUILD_CODE(
73  "const scalar u = $(gennrand_uniform);\n"
74  "x += (1.0 - x) * (1.0 - pow(u, 1.0 / (scalar)($(rowLength) - c)));\n"
75  "const unsigned int postIdx = (unsigned int)(x * $(num_post));\n"
76  "if(postIdx < $(num_post)) {\n"
77  " $(addSynapse, postIdx);\n"
78  "}\n"
79  "else {\n"
80  " $(addSynapse, $(num_post) - 1);\n"
81  "}\n"
82  "c++;\n"
83  "if(c >= $(rowLength)) {\n"
84  " $(endRow);\n"
85  "}\n");
86  SET_ROW_BUILD_STATE_VARS({{"x", "scalar", 0.0}, {"c", "unsigned int", 0}});
87 
88  SET_PARAM_NAMES({"rowLength"});
89 
90  SET_CALC_MAX_ROW_LENGTH_FUNC([](unsigned int, unsigned int,
91  const std::vector<double> &pars) {
92  return (unsigned int)pars[0];
93  });
94 };
96 
97 //_______________________Weight Update Models___________________________________
102 /*
103 class TsodyksMarkramSynapse : public WeightUpdateModels::Base {
104 public:
105  DECLARE_WEIGHT_UPDATE_MODEL(TsodyksMarkramSynapse, 0, 9, 0, 0);
106 
107  SET_VARS({{"U", "scalar"}, // asymptotic value of probability of release
108  {"tauRec",
109  "scalar"}, // recovery time from synaptic depression [ms]
110  {"tauFacil", "scalar"}, // time constant for facilitation [ms]
111  {"tauPsc",
112  "scalar"}, // decay time constant of postsynaptic current [ms]
113  {"g", "scalar"}, // weight
114  {"u", "scalar"}, // 0
115  {"x", "scalar"}, // 1.0
116  {"y", "scalar"}, // 0.0
117  {"z", "scalar"}}); // 0.0
118 
119  SET_SIM_CODE(
120  "const scalar deltaST = $(t) - $(sT_pre);"
121  "$(z) *= exp(-deltaST / $(tauRec));"
122  "$(z) += $(y) * (exp(-deltaST / $(tauPsc)) - exp(-deltaST / "
123  "$(tauRec))) / (($(tauPsc) / $(tauRec)) - 1.0);"
124  "$(y) *= exp(-deltaST / $(tauPsc));"
125  "$(x) = 1.0 - $(y) - $(z);"
126  "$(u) *= exp(-deltaST / $(tauFacil));"
127  "$(u) += $(U) * (1.0 - $(u));"
128  "if ($(u) > $(U)) {"
129  " $(u) = $(U);"
130  "}\n"
131  "$(y) += $(x) * $(u);\n"
132  "$(addToInSyn, $(g) * $(x) * $(u));\n");
133 
134  SET_NEEDS_PRE_SPIKE_TIME(true);
135 };
136 IMPLEMENT_SNIPPET(TsodyksMarkramSynapse);*/ // TODO Broken!
137 
138 class SpikePairRuleAdditive : public WeightUpdateModels::Base {
139  DECLARE_WEIGHT_UPDATE_MODEL(SpikePairRuleAdditive, 6, 1, 1, 1);
140  SET_VARS({
141  {"g", "scalar", VarAccess::READ_WRITE} // 0 - Weight
142  });
143 
144  SET_PARAM_NAMES({
145  "Aplus", // 0 - Rate of potentiation
146  "Aminus", // 1 - Rate of depression
147  "Wmin", // 2 - Minimum weight
148  "Wmax", // 3 - Maximum weight
149  "tauPlus", // 4 - Potentiation time constant (ms)
150  "tauMinus", // 5 - Depression time constant (ms)
151  }
152  );
153 
154  SET_PRE_VARS({{"preTrace", "scalar"}}); // 0 - Internal, set to 0});
155  SET_POST_VARS({{"postTrace", "scalar"}}); // 0 - Internal, set to 0
156 
157  SET_SIM_CODE(
158  "$(addToInSyn, $(g));\n"
159  "scalar dt = $(t) - $(sT_post);\n"
160  "if (dt > 0){\n"
161  "const scalar update = $(Aminus) * $(postTrace) * exp(-dt / "
162  "$(tauMinus));\n"
163  "$(g) = fmin($(Wmax), fmax($(Wmin), $(g) - (($(Wmax) - $(Wmin)) * "
164  "update)));\n"
165  "}\n");
166 
167  SET_LEARN_POST_CODE(
168  "scalar dt = $(t) - $(sT_pre);\n"
169  "if (dt > 0){\n"
170  "const scalar update = $(Aplus) * $(preTrace) * exp(-dt / "
171  "$(tauPlus));\n"
172  "$(g) = fmin($(Wmax), fmax($(Wmin), $(g) + (($(Wmax) - $(Wmin)) * "
173  "update)));\n"
174  "}\n");
175 
176  SET_PRE_SPIKE_CODE(
177  "const scalar dt = $(t) - $(sT_pre);\n"
178  "$(preTrace) = $(preTrace) * exp(-dt / $(tauPlus)) + 1.0;\n")
179  SET_POST_SPIKE_CODE(
180  "const scalar dt = $(t) - $(sT_post);\n"
181  "$(postTrace) = $(postTrace) * exp(-dt / $(tauMinus)) + 1.0;\n");
182 
183  SET_NEEDS_PRE_SPIKE_TIME(true);
184  SET_NEEDS_POST_SPIKE_TIME(true);
185 };
187 
188 class SpikePairRuleMultiplicative : public WeightUpdateModels::Base {
189  DECLARE_WEIGHT_UPDATE_MODEL(SpikePairRuleMultiplicative, 6, 1, 1, 1);
190  SET_VARS({
191  {"g", "scalar"}, // 0 - Weight
192  });
193  SET_PRE_VARS({{"preTrace", "scalar"}}); // 0 - Internal, set to 0
194  SET_POST_VARS({{"postTrace", "scalar"}}); // 0 - Internal, set to 0
195 
196  SET_PARAM_NAMES({
197  "Aplus", // 0 - Rate of potentiation
198  "Aminus", // 1 - Rate of depression
199  "Wmin", // 2 - Minimum weight
200  "Wmax", // 3 - Maximum weight
201  "tauPlus", // 4 - Potentiation time constant (ms)
202  "tauMinus", // 5 - Depression time constant (ms)
203  }
204  );
205 
206  SET_SIM_CODE(
207  "$(addToInSyn, $(g));\n"
208  "scalar dt = $(t) - $(sT_post);\n"
209  "if (dt > 0){\n"
210  "const scalar update = $(Aminus) * $(postTrace) * exp(-dt / "
211  "$(tauMinus));\n"
212  "$(g) -= ($(g) - $(Wmin)) * update;\n"
213  "}\n");
214 
215  SET_LEARN_POST_CODE(
216  "scalar dt = $(t) - $(sT_pre);\n"
217  "if (dt > 0){\n"
218  "const scalar update = $(Aplus) * $(preTrace) * exp(-dt / "
219  "$(tauPlus));\n"
220  "$(g) += ($(Wmax) - $(g)) * update;\n"
221  "}\n");
222 
223  SET_PRE_SPIKE_CODE(
224  "const scalar dt = $(t) - $(sT_pre);\n"
225  "$(preTrace) = $(preTrace) * exp(-dt / $(tauPlus)) + 1.0;\n")
226  SET_POST_SPIKE_CODE(
227  "const scalar dt = $(t) - $(sT_post);\n"
228  "$(postTrace) = $(postTrace) * exp(-dt / $(tauMinus)) + 1.0;\n");
229 
230  SET_NEEDS_PRE_SPIKE_TIME(true);
231  SET_NEEDS_POST_SPIKE_TIME(true);
232 };
234 
235 } // namespace GeNNModels
Definition: genn_models.hpp:188
Definition: genn_models.hpp:27
IMPLEMENT_SNIPPET(FixedNumberPostWithReplacementNoAutapse)
Definition: genn_models.hpp:67
Definition: genn_models.hpp:25
Definition: genn_models.hpp:138