sPyNNaker neural_modelling 7.1.1
Loading...
Searching...
No Matches
matrix_generator_static.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
22#include <stdbool.h>
23#include <debug.h>
27#include <utils.h>
28
30typedef struct {
34 uint32_t fixed_fixed_data[];
36
40#define SYNAPSE_WEIGHT_SHIFT 16
41
45#define SYNAPSE_WEIGHT_MASK 0xFFFF
46
48typedef struct matrix_generator_static_data {
49 union {
51 uint32_t *synaptic_matrix;
53 uint32_t synaptic_matrix_offset;
54 };
55 union {
57 uint32_t *delayed_synaptic_matrix;
59 uint32_t delayed_matrix_offset;
60 };
66 uint32_t synapse_type;
72 uint32_t max_stage;
76 uint32_t delay_bits;
78 uint32_t n_pre_neurons;
82
89static void setup_rows(uint32_t *matrix, uint32_t n_rows, uint32_t max_row_n_words) {
90 for (uint32_t i = 0; i < n_rows; i++) {
91 static_row_t *row = get_row(matrix, max_row_n_words, i);
92 log_debug("Setting up row %u at 0x%08x with %u max words", i, row, max_row_n_words);
93 row->plastic_plastic_size = 0;
94 row->fixed_plastic_size = 0;
95 row->fixed_fixed_size = 0;
96 }
97}
98
110static uint32_t build_static_word(
111 uint16_t weight, uint16_t delay, uint32_t type,
112 uint16_t post_index, uint32_t synapse_type_bits,
113 uint32_t synapse_index_bits, uint32_t delay_bits) {
114 uint32_t synapse_index_mask = (1 << synapse_index_bits) - 1;
115 uint32_t synapse_type_mask = (1 << synapse_type_bits) - 1;
116 uint32_t synapse_delay_mask = (1 << delay_bits) - 1;
117
118 uint32_t wrd = post_index & synapse_index_mask;
119 wrd |= (type & synapse_type_mask) << synapse_index_bits;
120 wrd |= (delay & synapse_delay_mask) <<
122 wrd |= (weight & SYNAPSE_WEIGHT_MASK) << SYNAPSE_WEIGHT_SHIFT;
123 return wrd;
124}
125
134 void *synaptic_matrix) {
136 *region = &sdram_data[1];
137 matrix_genetator_static_data_t *data = spin1_malloc(
139 *data = *sdram_data;
140
141 // Offsets are in words
142 uint32_t *syn_mat = synaptic_matrix;
143 if (data->synaptic_matrix_offset != 0xFFFFFFFF) {
144 data->synaptic_matrix = &(syn_mat[data->synaptic_matrix_offset]);
145 setup_rows(data->synaptic_matrix, data->n_pre_neurons,
146 data->max_row_n_words);
147 } else {
148 data->synaptic_matrix = NULL;
149 }
150 if (data->delayed_matrix_offset != 0xFFFFFFFF) {
151 data->delayed_synaptic_matrix = &(syn_mat[data->delayed_matrix_offset]);
152 setup_rows(data->delayed_synaptic_matrix,
153 data->n_pre_neurons * (data->max_stage - 1),
155 } else {
156 data->delayed_synaptic_matrix = NULL;
157 }
158
159 return data;
160}
161
166static void matrix_generator_static_free(void *generator) {
167 sark_free(generator);
168}
169
181static bool matrix_generator_static_write_synapse(void *generator,
182 uint32_t pre_index, uint16_t post_index, accum weight, uint16_t delay,
183 unsigned long accum weight_scale) {
184 matrix_genetator_static_data_t *data = generator;
185 struct delay_value delay_and_stage = get_delay(delay, data->max_stage,
186 data->max_delay_per_stage);
187 static_row_t *row;
188 uint32_t pos;
189 if (delay_and_stage.stage == 0) {
190 row = get_row(data->synaptic_matrix, data->max_row_n_words, pre_index);
191 pos = row->fixed_fixed_size;
192 if (pos >= data->max_row_n_words) {
193 log_warning("Row %u at 0x%08x of matrix 0x%08x is already full (%u of %u)",
194 pre_index, row, data->synaptic_matrix, pos, data->max_row_n_words);
195 return false;
196 }
197 } else {
198 row = get_delay_row(data->delayed_synaptic_matrix,
199 data->max_delayed_row_n_words, pre_index, delay_and_stage.stage,
201 pos = row->fixed_fixed_size;
202 if (pos >= data->max_delayed_row_n_words) {
203 log_warning("Row %u, stage %u at 0x%08x of delayed matrix 0x%08x"
204 "is already full (%u of %u)",
205 pre_index, delay_and_stage.stage, row,
206 data->delayed_synaptic_matrix, pos, data->max_delayed_row_n_words);
207 return false;
208 }
209 }
210
211 uint16_t scaled_weight = rescale_weight(weight, weight_scale);
212
213 row->fixed_fixed_size = pos + 1;
214 row->fixed_fixed_data[pos] = build_static_word(scaled_weight, delay_and_stage.delay,
215 data->synapse_type, post_index, data->synapse_type_bits,
216 data->synapse_index_bits, data->delay_bits);
217 return true;
218}
void log_warning(const char *message,...)
void log_debug(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_delay_mask
The mask to get the synaptic delay from a "synapse".
Definition local_only.c:71
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.
uint32_t synapse_index_bits
The number of bits needed to represent the synapse neuron id.
uint32_t max_delayed_row_n_words
The maximum number of words (excluding headers) on a delayed row.
static bool matrix_generator_static_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_pre_neurons_per_core
The number of pre-synaptic neurons per core.
static void matrix_generator_static_free(void *generator)
How to free any data for the static synaptic matrix generator.
uint32_t max_stage
The maximum delay stage, including 0 for no delay stage.
uint32_t fixed_fixed_size
the fixed-fixed size within a row
uint32_t fixed_plastic_size
the fixed-plastic size within a row
uint32_t plastic_plastic_size
the plastic-plastic size within a row
#define SYNAPSE_WEIGHT_MASK
The mask of a weight before shifting.
uint32_t delay_bits
The number of bits needed to represent the maximum delay per stage.
#define SYNAPSE_WEIGHT_SHIFT
The shift of the weight within a synaptic word.
uint32_t fixed_fixed_data[]
the fixed-fixed data within a row
static uint32_t build_static_word(uint16_t weight, uint16_t delay, uint32_t type, uint16_t post_index, uint32_t synapse_type_bits, uint32_t synapse_index_bits, uint32_t delay_bits)
Build a static synaptic word from components.
static void * matrix_generator_static_initialize(void **region, void *synaptic_matrix)
How to initialise the static synaptic matrix generator.
uint32_t n_pre_neurons
The number of pre-synaptic neurons.
uint32_t max_row_n_words
The maximum number of words (excluding headers) on a row.
uint32_t max_delay_per_stage
The maximum delay per delay stage in time steps.
uint32_t synapse_type_bits
The number of bits needed to represent the synapse type.
static void setup_rows(uint32_t *matrix, uint32_t n_rows, uint32_t max_row_n_words)
Set up the rows so that they are ready for writing to.
The stored data used to generate rows.
The layout of a purely static row of a synaptic matrix.
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