sPyNNaker neural_modelling 7.1.1
Loading...
Searching...
No Matches
matrix_generator_stdp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 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
23#include <stdbool.h>
24#include <spin1_api.h>
25#include <debug.h>
29#include <utils.h>
30
32typedef struct {
34 uint16_t plastic_plastic_data[];
36
38typedef struct {
41 uint16_t fixed_plastic_data[];
43
45typedef struct matrix_generator_stdp {
46 union {
48 uint32_t *synaptic_matrix;
50 uint32_t synaptic_matrix_offset;
51 };
52 union {
54 uint32_t *delayed_synaptic_matrix;
56 uint32_t delayed_matrix_offset;
57 };
67 uint32_t synapse_type;
73 uint32_t max_stage;
77 uint32_t delay_bits;
79 uint32_t n_pre_neurons;
89
99static uint32_t plastic_half_words(uint32_t n_half_words_per_pp_header,
100 uint32_t n_half_words_per_pp_synapse, uint32_t max_row_n_synapses) {
101 uint32_t n_half_words = n_half_words_per_pp_header
102 + (n_half_words_per_pp_synapse * max_row_n_synapses);
103 if (n_half_words & 0x1) {
104 n_half_words += 1;
105 }
106 return n_half_words;
107}
108
121 uint32_t n_half_words_per_pp_header, uint32_t n_half_words_per_pp_synapse,
122 uint32_t max_row_n_synapses) {
123 uint32_t idx_16 = plastic_half_words(n_half_words_per_pp_header,
124 n_half_words_per_pp_synapse, max_row_n_synapses);
125 return (row_fixed_t *) &(plastic_row->plastic_plastic_data[idx_16]);
126}
127
139static void setup_stdp_rows(uint32_t *matrix, uint32_t n_rows,
140 uint32_t n_half_words_per_pp_header, uint32_t n_half_words_per_pp_synapse,
141 uint32_t max_row_n_synapses, uint32_t max_row_n_words) {
142
143 // Set all the header half-words to 0 and set all the sizes
144 uint32_t plastic_words = plastic_half_words(n_half_words_per_pp_header,
145 n_half_words_per_pp_synapse, max_row_n_synapses) >> 1;
146 for (uint32_t i = 0; i < n_rows; i++) {
147 row_plastic_t *row = get_row(matrix, max_row_n_words, i);
148 // Use word writing for efficiency
149 uint32_t *data = (uint32_t *) &row->plastic_plastic_data[0];
150 for (uint32_t j = 0; j < plastic_words; j++) {
151 data[j] = 0;
152 }
153 row->plastic_plastic_size = plastic_words;
154 row_fixed_t *fixed = get_stdp_fixed_row(row, n_half_words_per_pp_header,
155 n_half_words_per_pp_synapse, max_row_n_synapses);
156 fixed->fixed_fixed_size = 0;
157 fixed->fixed_plastic_size = 0;
158 }
159}
160
161
173 uint16_t delay, uint32_t type,
174 uint32_t post_index, uint32_t synapse_type_bits,
175 uint32_t synapse_index_bits, uint32_t delay_bits) {
176 uint16_t synapse_index_mask = (1 << synapse_index_bits) - 1;
177 uint16_t synapse_type_mask = (1 << synapse_type_bits) - 1;
178 uint16_t delay_mask = (1 << delay_bits) - 1;
179
180 uint16_t wrd = post_index & synapse_index_mask;
181 wrd |= (type & synapse_type_mask) << synapse_index_bits;
182 wrd |= (delay & delay_mask) <<
184 // wrd |= (delay & SYNAPSE_DELAY_MASK) << synapse_type_bits;
185
186 return wrd;
187}
188
196void *matrix_generator_stdp_initialize(void **region, void *synaptic_matrix) {
197 // Allocate memory for the parameters
199 spin1_malloc(sizeof(matrix_generator_stdp_data_t));
200
201 // Copy the parameters in
202 matrix_generator_stdp_data_t *params_sdram = *region;
203 *obj = *params_sdram;
204 *region = &params_sdram[1];
205
206 // Offsets are in words
207 uint32_t *syn_mat = synaptic_matrix;
208 if (obj->synaptic_matrix_offset != 0xFFFFFFFF) {
209 obj->synaptic_matrix = &(syn_mat[obj->synaptic_matrix_offset]);
210 setup_stdp_rows(obj->synaptic_matrix, obj->n_pre_neurons,
213 obj->max_row_n_words);
214 } else {
215 obj->synaptic_matrix = NULL;
216 }
217
218 if (obj->delayed_matrix_offset != 0xFFFFFFFF) {
219 obj->delayed_synaptic_matrix = &(syn_mat[obj->delayed_matrix_offset]);
220 setup_stdp_rows(obj->delayed_synaptic_matrix,
221 obj->n_pre_neurons * (obj->max_stage - 1),
225 } else {
226 obj->delayed_synaptic_matrix = NULL;
227 }
228
229 return obj;
230}
231
236void matrix_generator_stdp_free(void *generator) {
237 sark_free(generator);
238}
239
250static bool matrix_generator_stdp_write_synapse(void *generator,
251 uint32_t pre_index, uint16_t post_index, accum weight, uint16_t delay,
252 unsigned long accum weight_scale) {
253 matrix_generator_stdp_data_t *data = generator;
254 struct delay_value delay_and_stage = get_delay(delay, data->max_stage,
255 data->max_delay_per_stage);
256 row_plastic_t *plastic_row;
257 row_fixed_t *fixed_row;
258 uint32_t pos;
259 if (delay_and_stage.stage == 0) {
260 plastic_row = get_row(data->synaptic_matrix, data->max_row_n_words,
261 pre_index);
262 fixed_row = get_stdp_fixed_row(plastic_row,
265 pos = fixed_row->fixed_plastic_size;
266 if (pos >= data->max_row_n_synapses) {
267 log_warning("Row %u at 0x%08x, 0x%08x of matrix 0x%08x is already full (%u of %u)",
268 pre_index, plastic_row, fixed_row, data->synaptic_matrix, pos,
269 data->max_row_n_synapses);
270 return false;
271 }
272 } else {
273 plastic_row = get_delay_row(data->delayed_synaptic_matrix,
274 data->max_delayed_row_n_words, pre_index, delay_and_stage.stage,
276 fixed_row = get_stdp_fixed_row(plastic_row,
279 pos = fixed_row->fixed_plastic_size;
280 if (pos >= data->max_delayed_row_n_synapses) {
281 log_warning("Row %u at 0x%08x, 0x%08x of matrix 0x%08x is already full (%u of %u)",
282 pre_index, plastic_row, fixed_row, data->synaptic_matrix, pos,
284 return false;
285 }
286 }
287
288 uint16_t scaled_weight = rescale_weight(weight, weight_scale);
289
290 fixed_row->fixed_plastic_size = pos + 1;
292 delay_and_stage.delay, data->synapse_type, post_index,
294 uint32_t plastic_pos = data->n_half_words_per_pp_row_header
295 + (data->n_half_words_per_pp_synapse * pos) + data->weight_half_word;
296 plastic_row->plastic_plastic_data[plastic_pos] = scaled_weight;
297 return true;
298}
void log_warning(const char *message,...)
Declarations for delay extensions.
General types associated with generators.
static uint16_t rescale_weight(accum weight, unsigned long accum weight_scale)
Rescales a weight to account for weight granularity and type-converts it.
uint32_t synapse_index_bits
The number of bits used by just the post-neuron index.
Definition local_only.c:77
Common functions for matrix generation.
static struct delay_value get_delay(uint16_t delay_value, uint32_t max_stage, uint32_t max_delay_per_stage)
Get a converted delay value and stage.
static void * get_delay_row(uint32_t *delayed_synaptic_matrix, uint32_t max_delayed_row_n_words, uint32_t pre_index, uint32_t delay_stage, uint32_t n_pre_neurons_per_core, uint32_t max_delay_stage, uint32_t n_pre_neurons)
Get a delayed synaptic row for a given neuron and delay stage.
static void * get_row(uint32_t *synaptic_matrix, uint32_t max_row_n_words, uint32_t pre_index)
Get a synaptic row for a given neuron.
A converted final delay value and delay stage.
uint32_t synapse_type
The matrix synapse type.
void * matrix_generator_stdp_initialize(void **region, void *synaptic_matrix)
Initialise the STDP synaptic matrix generator.
uint32_t max_stage
The maximum delay stage, including 0 for no delay stage.
uint16_t plastic_plastic_data[]
the plastic-plastic data within the row
static uint32_t plastic_half_words(uint32_t n_half_words_per_pp_header, uint32_t n_half_words_per_pp_synapse, uint32_t max_row_n_synapses)
Get the maximum number of plastic half-words in a row.
uint32_t n_pre_neurons
The number of pre-synaptic neurons.
uint32_t max_delayed_row_n_words
The maximum number of words on a delayed row.
uint32_t fixed_fixed_size
the fixed-fixed size within the fixed region
uint32_t max_row_n_synapses
The maximum number of synapses on a row.
uint16_t fixed_plastic_data[]
the fixed-plastic data within the fixed region
uint32_t delay_bits
The number of bits needed to represent the maximum delay per stage.
static void setup_stdp_rows(uint32_t *matrix, uint32_t n_rows, uint32_t n_half_words_per_pp_header, uint32_t n_half_words_per_pp_synapse, uint32_t max_row_n_synapses, uint32_t max_row_n_words)
Set up the rows so that they are ready for writing to.
static row_fixed_t * get_stdp_fixed_row(row_plastic_t *plastic_row, uint32_t n_half_words_per_pp_header, uint32_t n_half_words_per_pp_synapse, uint32_t max_row_n_synapses)
Get the fixed part of a row that comes after the plastic part. Note that this assumes the max row siz...
uint32_t n_half_words_per_pp_synapse
The number of half-words in each plastic-plastic synapse.
void matrix_generator_stdp_free(void *generator)
Free any data for the STDP synaptic matrix generator.
static bool matrix_generator_stdp_write_synapse(void *generator, uint32_t pre_index, uint16_t post_index, accum weight, uint16_t delay, unsigned long accum weight_scale)
How to write a synapse to a matrix.
uint32_t n_half_words_per_pp_row_header
The number of half-words in a plastic-plastic row header.
uint32_t plastic_plastic_size
the plastic-plastic size within the row
uint32_t max_row_n_words
The maximum number of words on a row.
uint32_t n_pre_neurons_per_core
The number of pre-synaptic neurons per core.
uint32_t weight_half_word
The index of the half-word that will contain the weight.
uint32_t fixed_plastic_size
the fixed-plastic size within the fixed region
uint32_t max_delay_per_stage
The maximum delay per delay stage in time steps.
static uint16_t build_fixed_plastic_half_word(uint16_t delay, uint32_t type, uint32_t post_index, uint32_t synapse_type_bits, uint32_t synapse_index_bits, uint32_t delay_bits)
Build a fixed-plastic half-word from its components.
uint32_t max_delayed_row_n_synapses
The maximum number of synapses on a delayed row.
uint32_t synapse_type_bits
The number of bits needed to represent the synapse type.
uint32_t synapse_index_bits
The number of bits needed to represent the synapse neuron id.
The layout of the fixed synapse region of the row; the fixed-fixed region is empty.
The layout of the initial plastic synapse part of the row.
void sark_free(void *ptr)
region
spike source array region IDs in human readable form
#define NULL
uint32_t synapse_index_mask
Mask to pick out the synapse index.
Definition synapses.c:69
uint32_t synapse_type_bits
Number of bits in the synapse type.
Definition synapses.c:71
uint32_t synapse_type_mask
Mask to pick out the synapse type.
Definition synapses.c:73