sPyNNaker neural_modelling  development
neuron_impl_stoc_exp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 The University of Manchester
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
19 
20 #ifndef _NEURON_IMPL_STOC_EXP_
21 #define _NEURON_IMPL_STOC_EXP_
22 
24 #include <spin1_api.h>
25 #include <debug.h>
26 #include <random.h>
27 #include <stdfix-full-iso.h>
28 #include <common/maths-util.h>
29 
30 #define V_RECORDING_INDEX 0
31 #define EX_INPUT_INDEX 1
32 #define IN_INPUT_INDEX 2
33 #define PROB_INDEX 3
34 #define N_RECORDED_VARS 4
35 
36 #define SPIKE_RECORDING_BITFIELD 0
37 #define N_BITFIELD_VARS 1
38 
40 
43 
44 #include "stoc_exp_common.h"
45 
47 typedef struct neuron_params_t {
48 
51 
54 
57 
59  uint32_t refract_init;
60 
62  mars_kiss64_seed_t random_seed;
64 
65 
67 typedef struct neuron_impl_t {
68 
71 
74 
76  uint32_t t_refract;
77 
79  uint32_t refract_timer;
80 
82  mars_kiss64_seed_t random_seed;
83 
87 
90 
91 static bool neuron_impl_initialise(uint32_t n_neurons) {
92  // Allocate DTCM for neuron array
93  neuron_array = spin1_malloc(n_neurons * sizeof(neuron_impl_t));
94  if (neuron_array == NULL) {
95  log_error("Unable to allocate neuron array - Out of DTCM");
96  return false;
97  }
98 
99  return true;
100 }
101 
102 static inline void neuron_model_initialise(
104  UREAL ts = params->time_step;
105  state->tau_recip = ulrbits((
106  bitsuk(ukdivuk(UREAL_CONST(1.0), params->tau_ms)) & 0xFFFF) << 16);
107  state->bias = params->bias;
108  state->t_refract = stoc_exp_ceil_accum(ukdivuk(params->tau_ms, ts));
109  state->refract_timer = params->refract_init;
110  spin1_memcpy(state->random_seed, params->random_seed, sizeof(mars_kiss64_seed_t));
111  validate_mars_kiss64_seed(state->random_seed);
112 
113  // Reset the inputs
114  state->inputs[0] = ZERO;
115  state->inputs[1] = ZERO;
116 }
117 
118 static inline void neuron_model_save_state(neuron_impl_t *state, neuron_params_t *params) {
119  params->refract_init = state->refract_timer;
120  spin1_memcpy(params->random_seed, state->random_seed, sizeof(mars_kiss64_seed_t));
121 }
122 
123 static void neuron_impl_load_neuron_parameters(
124  address_t address, uint32_t next, uint32_t n_neurons,
125  address_t save_initial_state) {
126 
127  neuron_params_t *params = (neuron_params_t *) &address[next];
128  for (uint32_t i = 0; i < n_neurons; i++) {
130  }
131 
132  // If we are to save the initial state, copy the whole of the parameters
133  // to the initial state
134  if (save_initial_state) {
135  spin1_memcpy(save_initial_state, address,
136  n_neurons * sizeof(neuron_params_t));
137  }
138 }
139 
140 static void neuron_impl_store_neuron_parameters(
141  address_t address, uint32_t next, uint32_t n_neurons) {
142  neuron_params_t *params = (neuron_params_t *) &address[next];
143  for (uint32_t i = 0; i < n_neurons; i++) {
145  }
146 }
147 
148 static void neuron_impl_add_inputs(
149  index_t synapse_type_index, index_t neuron_index,
150  input_t weights_this_timestep) {
151  // Get the neuron itself
152  neuron_impl_t *neuron = &neuron_array[neuron_index];
153 
154  // Do something to store the inputs for the next state update
155  neuron->inputs[synapse_type_index] += weights_this_timestep;
156 }
157 
158 // Update done when in refractory
159 static inline void do_refrac_update(uint32_t timer_count, uint32_t time,
160  uint32_t neuron_index, neuron_impl_t *neuron) {
161  neuron->refract_timer -= 1;
162 
163  // Record things
164  neuron_recording_record_int32(PROB_INDEX, neuron_index, 0);
165  neuron_recording_record_accum(V_RECORDING_INDEX, neuron_index, ZERO);
166  neuron_recording_record_accum(EX_INPUT_INDEX, neuron_index, neuron->inputs[0]);
167  neuron_recording_record_accum(IN_INPUT_INDEX, neuron_index, neuron->inputs[1]);
168 
169  // Reset the inputs
170  neuron->inputs[0] = ZERO;
171  neuron->inputs[1] = ZERO;
172 
173  // Send a spike
175  send_spike(timer_count, time, neuron_index);
176 }
177 
178 static inline void do_non_refrac_update(uint32_t timer_count, uint32_t time,
179  uint32_t neuron_index, neuron_impl_t *neuron) {
180  // Work out the membrane voltage
181  REAL v_membrane = (neuron->bias + neuron->inputs[0]) - neuron->inputs[1];
182 
183  // Record things
184  neuron_recording_record_accum(V_RECORDING_INDEX, neuron_index, v_membrane);
185  neuron_recording_record_accum(EX_INPUT_INDEX, neuron_index, neuron->inputs[0]);
186  neuron_recording_record_accum(IN_INPUT_INDEX, neuron_index, neuron->inputs[1]);
187 
188  // Reset the inputs
189  neuron->inputs[0] = ZERO;
190  neuron->inputs[1] = ZERO;
191 
192  // Work out the probability
193  uint32_t prob = get_probability(neuron->tau_recip, v_membrane);
194 
195  // Record the probability
196  neuron_recording_record_int32(PROB_INDEX, neuron_index, (int32_t) prob);
197 
198  // Get a random number
199  uint32_t random = mars_kiss64_seed(neuron->random_seed);
200 
201  // If the random number is less than the probability value, spike
202  if (random < prob) {
203  neuron->refract_timer = neuron->t_refract - 1;
205  send_spike(timer_count, time, neuron_index);
206  }
207 }
208 
209 static void neuron_impl_do_timestep_update(
210  uint32_t timer_count, uint32_t time, uint32_t n_neurons) {
211  for (uint32_t neuron_index = 0; neuron_index < n_neurons; neuron_index++) {
212  // Get the neuron itself
213  neuron_impl_t *neuron = &neuron_array[neuron_index];
214 
215  // If in refractory, count down and spike!
216  if (neuron->refract_timer > 0) {
217  do_refrac_update(timer_count, time, neuron_index, neuron);
218  } else {
219  do_non_refrac_update(timer_count, time, neuron_index, neuron);
220  }
221  }
222 }
223 
224 #if LOG_LEVEL >= LOG_DEBUG
225 static void neuron_impl_print_inputs(uint32_t n_neurons) {
226  log_debug("-------------------------------------\n");
227  for (index_t i = 0; i < n_neurons; i++) {
228  neuron_impl_t *neuron = &neuron_array[i];
229  log_debug("inputs: %k %k", neuron->inputs[0], neuron->inputs[1]);
230  }
231  log_debug("-------------------------------------\n");
232 }
233 
234 static void neuron_impl_print_synapse_parameters(uint32_t n_neurons) {
235  // there aren't any accessible
236  use(n_neurons);
237 }
238 
239 static const char *neuron_impl_get_synapse_type_char(uint32_t synapse_type) {
240  if (synapse_type == 0) {
241  return 'E';
242  } else if (synapse_type == 1) {
243  return 'I';
244  }
245  return 'U';
246 }
247 #endif // LOG_LEVEL >= LOG_DEBUG
248 
249 
250 #endif // _NEURON_IMPL_STOC_EXP_
General API of a current source implementation.
Implement all current sources.
static uint32_t time
Simulation time.
maths-util.h - first created 7/10/2013 version 0.1
unsigned accum UREAL
Type used for "unsigned real" numbers.
Definition: maths-util.h:94
accum REAL
Type used for "real" numbers.
Definition: maths-util.h:91
unsigned long fract UFRACT
Type used for "unsigned fractional" numbers.
Definition: maths-util.h:100
#define UREAL_CONST(x)
Define a constant of type UREAL.
Definition: maths-util.h:108
static UREAL ukdivuk(UREAL a, UREAL b)
Divides an unsigned accum by another unsigned accum.
Definition: maths-util.h:242
#define ZERO
A REAL 0.0.
Definition: maths-util.h:123
REAL input_t
The type of an input.
static uint32_t n_neurons
The number of neurons on the core.
Definition: neuron.c:45
General API of a neuron implementation.
void neuron_impl_print_synapse_parameters(uint32_t n_neurons)
Print the synapse parameters of the neurons.
void neuron_impl_print_inputs(uint32_t n_neurons)
Print the inputs to the neurons.
const char * neuron_impl_get_synapse_type_char(uint32_t synapse_type)
Get the synapse type character for a synapse type.
@ SPIKE_RECORDING_BITFIELD
Spike event recording index.
static neuron_impl_t * neuron_array
Array of neuron states.
UFRACT tau_recip
The reciprocal of the tau value.
uint32_t t_refract
The refractory timer countdown value.
input_t inputs[2]
The inputs to add in the next timestep.
mars_kiss64_seed_t random_seed
The random state.
REAL bias
The bias value.
uint32_t refract_timer
The refractory timer.
definition of neuron state
static void neuron_model_initialise(neuron_t *state, neuron_params_t *params, uint32_t n_steps_per_timestep)
initialise the structure from the parameters
static void neuron_model_save_state(neuron_t *state, neuron_params_t *params)
save parameters and state back to SDRAM for reading by host and recovery on restart
REAL bias
The bias value.
mars_kiss64_seed_t random_seed
Random seed to use.
UREAL tau_ms
The tau value of the neuron.
UREAL time_step
The timestep of the neuron being used.
uint32_t refract_init
The initial refractory timer.
definition of neuron parameters
Recording of the state of a neuron (spiking, voltage, etc.)
static void neuron_recording_record_accum(uint32_t var_index, uint32_t neuron_index, accum value)
stores a recording of an accum variable only; this is faster than neuron_recording_record_value for t...
static void neuron_recording_record_bit(uint32_t var_index, uint32_t neuron_index)
stores a recording of a set bit; this is the only way to set a bit in a bitfield; neuron_recording_re...
static void neuron_recording_record_int32(uint32_t var_index, uint32_t neuron_index, int32_t value)
stores a recording of an int32_t variable only; this is faster than neuron_recording_record_value for...
Stochastic common code.
static uint32_t get_probability(UREAL tau, REAL p)
Calculates the probability as a uint32_t from 0 to 0xFFFFFFFF (which is 1)
static stdp_params params
Configuration parameters.