sPyNNaker neural_modelling  development
c_main_local_only.c
1 /*
2  * Copyright (c) 2021 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 
17 #include "c_main_neuron_common.h"
18 #include "c_main_common.h"
19 #include "profile_tags.h"
20 #include "local_only/local_only_impl.h"
21 #include "local_only.h"
22 #include "synapse_row.h"
23 
24 #define SYNAPSE_TYPE
25 
27 struct combined_provenance {
31  uint32_t max_backgrounds_queued;
34 };
35 
37 typedef enum callback_priorities {
38  MC = -1, DMA = 0, USER = 0, TIMER = 0, SDP = 1, BACKGROUND = 1
40 
42 enum regions {
43  SYSTEM_REGION,
44  PROVENANCE_DATA_REGION,
45  PROFILER_REGION,
51  LOCAL_ONLY_REGION,
52  LOCAL_ONLY_PARAMS_REGION,
55 };
56 
58 const struct common_regions COMMON_REGIONS = {
59  .system = SYSTEM_REGION,
60  .provenance = PROVENANCE_DATA_REGION,
61  .profiler = PROFILER_REGION,
62  .recording = RECORDING_REGION
63 };
64 
66 const struct common_priorities COMMON_PRIORITIES = {
67  .sdp = SDP,
68  .dma = DMA,
69  .timer = TIMER
70 };
71 
73 const struct neuron_regions NEURON_REGIONS = {
75  .neuron_params = NEURON_PARAMS_REGION,
76  .current_source_params = CURRENT_SOURCE_PARAMS_REGION,
77  .neuron_recording = NEURON_RECORDING_REGION,
78  .initial_values = INITIAL_VALUES_REGION
79 };
80 
82 // the timer tick callback returning the same value.
83 uint32_t time;
84 
86 static uint32_t timer_period;
87 
89 static uint32_t simulation_ticks = 0;
90 
92 static uint32_t infinite_run;
93 
95 static uint32_t recording_flags = 0;
96 
98 static uint32_t n_backgrounds_queued = 0;
99 
101 static uint32_t n_background_overloads = 0;
102 
104 static uint32_t max_backgrounds_queued = 0;
105 
107 static uint16_t *ring_buffers;
108 
110  UNUSED uint32_t time, UNUSED index_t neuron_index) {
111 }
112 
115 static void c_main_store_provenance_data(address_t provenance_region) {
116  struct combined_provenance *prov = (void *) provenance_region;
119  store_neuron_provenance(&prov->neuron_provenance);
120  local_only_store_provenance(&prov->local_only_provenance);
121 }
122 
124 void resume_callback(void) {
125 
126  // Reset recording
127  recording_reset();
128 
129  // try resuming neuron
130  if (!neuron_resume(time + 1)) {
131  log_error("failed to resume neuron.");
132  rt_error(RTE_SWERR);
133  }
134 }
135 
137 static inline void process_ring_buffers(void) {
138  uint32_t first_index = synapse_row_get_first_ring_buffer_index(
140  neuron_transfer(&ring_buffers[first_index]);
141 
142  // Print the neuron inputs.
143  #if LOG_LEVEL >= LOG_DEBUG
144  log_debug("Inputs");
146  #endif // LOG_LEVEL >= LOG_DEBUG
147 }
148 
153 void background_callback(uint timer_count, uint local_time) {
154  profiler_write_entry_disable_irq_fiq(PROFILER_ENTER | PROFILER_TIMER);
155 
156  log_debug("Timer tick %u \n", local_time);
157 
158  // Now do neuron time step update
160 
161  profiler_write_entry_disable_irq_fiq(PROFILER_EXIT | PROFILER_TIMER);
163 }
164 
169 void timer_callback(uint timer_count, UNUSED uint unused) {
170  // Disable interrupts to stop MC getting in the way of this bit
171  uint32_t state = spin1_int_disable();
172 
173  // Increment time step
174  time++;
175 
176  // Clear any outstanding spikes
178 
179  // Allow things to interrupt again
180  spin1_mode_restore(state);
181 
182  // Process ring buffers for the inputs from last time step
184 
185  /* if a fixed number of simulation ticks that were specified at startup
186  * then do reporting for finishing */
187  if (simulation_is_finished()) {
188 
189  // Enter pause and resume state to avoid another tick
190  simulation_handle_pause_resume(resume_callback);
191 
192  // Pause neuron processing
193  neuron_pause();
194 
195  // Pause common functions
196  common_pause(recording_flags);
197 
198  // Subtract 1 from the time so this tick gets done again on the next
199  // run
200  time--;
201 
202  simulation_ready_to_read();
203  return;
204  }
205 
206  // Push the rest to the background
207  if (!spin1_schedule_callback(background_callback, timer_count, time, BACKGROUND)) {
208  // We have failed to do this timer tick!
210  } else {
214  }
215  }
216 }
217 
221 static bool initialise(void) {
222  log_debug("Initialise: started");
223 
224  data_specification_metadata_t *ds_regions;
225  if (!initialise_common_regions(
228  COMMON_REGIONS, COMMON_PRIORITIES, &ds_regions)) {
229  return false;
230  }
231 
232  // Setup neurons
233  uint32_t n_rec_regions_used;
234  if (!initialise_neuron_regions(
235  ds_regions, NEURON_REGIONS, &n_rec_regions_used)) {
236  return false;
237  }
238 
240  data_specification_get_region(LOCAL_ONLY_REGION, ds_regions),
241  data_specification_get_region(LOCAL_ONLY_PARAMS_REGION, ds_regions),
242  n_rec_regions_used, &ring_buffers)) {
243  return false;
244  }
245 
246  // Set timer tick (in microseconds)
247  log_debug("setting timer tick callback for %d microseconds", timer_period);
248  spin1_set_timer_tick(timer_period);
249 
250  log_debug("Initialise: finished");
251  return true;
252 }
253 
255 void c_main(void) {
256 
257  // Start the time at "-1" so that the first tick will be 0
258  time = UINT32_MAX;
259 
260  // initialise the model
261  if (!initialise()) {
262  rt_error(RTE_API);
263  }
264 
265  simulation_run();
266 }
static void c_main_store_provenance_data(address_t provenance_region)
Callback to store provenance data (format: neuron_provenance).
Definition: c_main.c:122
const struct neuron_regions NEURON_REGIONS
From the regions, extract those that are neuron-specific.
Definition: c_main.c:74
callback_priorities
Identify the priorities for all tasks.
Definition: c_main.c:54
static void process_ring_buffers(void)
Process the ring buffers for the next time step.
Definition: c_main.c:150
void resume_callback(void)
the function to call when resuming a simulation
Definition: c_main.c:132
static uint32_t recording_flags
The recording flags indicating if anything is recording.
Definition: c_main.c:106
static weight_t * ring_buffers
The ring buffers to be used in the simulation.
Definition: c_main.c:118
void background_callback(uint timer_count, uint local_time)
Background activities called from timer.
Definition: c_main.c:165
const struct common_priorities COMMON_PRIORITIES
Identify the priorities of the common tasks.
Definition: c_main.c:67
static bool initialise(void)
Initialises the model by reading in the regions and checking recording data.
Definition: c_main.c:237
const struct common_regions COMMON_REGIONS
From the regions, extract those that are common.
Definition: c_main.c:59
static uint32_t simulation_ticks
Simulation speed.
@ TIMER
Call timer at 0 to keep it quick.
@ DMA
DMA is not actually used.
@ BACKGROUND
Background processing.
@ SDP
SDP handling is queued.
@ USER
Call user at 0 as well; will be behind timer.
static uint32_t infinite_run
True if we're running forever.
void c_main(void)
Entry point.
static uint32_t n_backgrounds_queued
The number of background tasks queued / running.
static uint32_t timer_period
Used for configuring the timer hardware.
static void timer_callback(uint timer_count, uint unused1)
Main timer callback.
static uint32_t max_backgrounds_queued
The maximum number of background tasks queued.
static uint32_t time
Simulation time.
static uint32_t n_background_overloads
The number of times the background couldn't be added.
uint32_t synapse_delay_mask
The mask to get the synaptic delay from a "synapse".
Definition: local_only.c:71
void local_only_clear_input(uint32_t time)
Clear the spikes for the last time step.
Definition: local_only.c:218
uint32_t synapse_type_index_bits
The number of bits used by the synapse type and post-neuron index.
Definition: local_only.c:74
static uint32_t local_time
The local time step counter.
Definition: local_only.c:68
void local_only_store_provenance(struct local_only_provenance *prov)
Store provenance gathered during run.
Definition: local_only.c:234
bool local_only_initialise(void *local_only_addr, void *local_only_params_addr, uint32_t n_rec_regions_used, uint16_t **ring_buffers_ptr)
Set up local-only processing of spikes.
Definition: local_only.c:164
Defines the "local-only" processing of spikes, that is, the processing of spikes without using transf...
@ PROFILER_TIMER
timer
Definition: profile_tags.h:23
void neuron_pause(void)
Perform steps needed before pausing a simulation.
Definition: neuron.c:184
void neuron_print_inputs(void)
Print the inputs to the neurons.
Definition: neuron.c:230
bool neuron_resume(uint32_t time)
Prepare to resume simulation of the neurons.
Definition: neuron.c:93
void neuron_transfer(weight_t *syns)
Add inputs to the neurons.
Definition: neuron.c:204
void neuron_do_timestep_update(timer_t time, uint timer_count)
executes all the updates to neural parameters when a given timer period has occurred.
Definition: neuron.c:190
@ NEURON_PARAMS_REGION
neuron parameters; 2
Definition: regions.h:28
@ CORE_PARAMS_REGION
core parameters; 1
Definition: regions.h:27
@ INITIAL_VALUES_REGION
initial neuron state; 16
Definition: regions.h:42
@ RECORDING_REGION
general recording data; 15
Definition: regions.h:41
@ NEURON_RECORDING_REGION
recording; 9
Definition: regions.h:35
@ NEURON_BUILDER_REGION
neuron building; 13
Definition: regions.h:39
@ CURRENT_SOURCE_PARAMS_REGION
current source parameters; 3
Definition: regions.h:29
@ MC
Multicast message reception is FIQ.
Tags for profiling of the spike source poisson.
The combined provenance from synapses and neurons.
Definition: c_main.c:43
uint32_t max_backgrounds_queued
Maximum backgrounds queued.
Definition: c_main.c:48
uint32_t n_background_queue_overloads
Background queue overloads.
Definition: c_main.c:50
The callback priorities used by all simulation cores.
Definition: c_main_common.h:46
uint32_t sdp
The SDP callback priority.
Definition: c_main_common.h:48
The identifiers of the regions used by all simulation cores.
Definition: c_main_common.h:34
uint32_t system
Data for general simulation setup.
Definition: c_main_common.h:36
The provenance information provided by neurons.
The region IDs used by the neuron processing.
uint32_t core_params
The core parameters.
void synapse_dynamics_process_post_synaptic_event(uint32_t time, index_t neuron_index)
Inform the synapses that the neuron fired.
implementation for handling the processing of synapse rows.
static index_t synapse_row_get_first_ring_buffer_index(uint32_t simulation_timestep, uint32_t synapse_type_index_bits, int32_t synapse_delay_mask)
Get the index of the first ring buffer for a given timestep.
Definition: synapse_row.h:285