sPyNNaker neural_modelling  development
matrix_generator_weight_changer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 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 
29 typedef struct {
30  union {
32  uint32_t *synaptic_matrix;
34  uint32_t synaptic_matrix_offset;
35  };
36  uint32_t max_row_n_words;
37  uint32_t max_row_n_synapses;
38  uint32_t n_pre_neurons;
39  uint32_t synapse_type;
40  uint32_t synapse_type_bits;
41  uint32_t synapse_index_bits;
42  uint32_t row_offset;
44 
46 typedef struct {
48  uint32_t pre_spike: 31;
49  uint32_t is_update: 1;
51 
53 typedef struct {
54  uint32_t fixed_fixed_size;
55  uint32_t fixed_plastic_size;
56  int32_t fixed_plastic_data[];
58 
68 static row_changer_plastic_t *get_changer_row(uint32_t *synaptic_matrix,
69  uint32_t max_row_n_words, uint32_t pre_index) {
70  uint32_t idx = pre_index * (max_row_n_words + N_HEADER_WORDS);
71  return (row_changer_plastic_t *) &synaptic_matrix[idx];
72 }
73 
80  return (row_changer_fixed_t *) &plastic_row[1];
81 }
82 
89 static void setup_changer_rows(uint32_t *matrix, uint32_t n_rows,
90  uint32_t max_row_n_words, uint32_t row_offset) {
91  for (uint32_t i = 0; i < n_rows; i++) {
92  row_changer_plastic_t *row = get_changer_row(matrix, max_row_n_words, i);
93  row->plastic_plastic_size = 1;
94  row->pre_spike = i + row_offset;
95  row->is_update = 1;
97  fixed->fixed_fixed_size = 0;
98  fixed->fixed_plastic_size = 0;
99  }
100 }
101 
102 
111  void *synaptic_matrix) {
112  // Allocate memory for the parameters
114  spin1_malloc(sizeof(matrix_generator_weight_changer));
115 
116  // Copy the parameters in
117  matrix_generator_weight_changer *params_sdram = *region;
118  *conf = *params_sdram;
119  *region = &params_sdram[1];
120 
121  // Offsets are in words
122  uint32_t *syn_mat = synaptic_matrix;
123  conf->synaptic_matrix = &(syn_mat[conf->synaptic_matrix_offset]);
124  setup_changer_rows(conf->synaptic_matrix, conf->n_pre_neurons,
125  conf->max_row_n_words, conf->row_offset);
126 
127  return conf;
128 }
129 
134 void matrix_generator_changer_free(void *generator) {
135  sark_free(generator);
136 }
137 
138 static uint32_t build_changer_word(
139  uint32_t type, uint32_t post_index, uint32_t synapse_type_bits,
140  uint32_t synapse_index_bits, int16_t weight) {
141  uint32_t synapse_index_mask = (1 << synapse_index_bits) - 1;
142  uint32_t synapse_type_mask = (1 << synapse_type_bits) - 1;
143 
144  uint32_t wrd = post_index & synapse_index_mask;
145  wrd |= (type & synapse_type_mask) << synapse_index_bits;
146  // The weight position is fixed
147  wrd |= weight << 16;
148 
149  return wrd;
150 }
151 
152 static bool matrix_generator_changer_write_synapse(void *generator,
153  uint32_t pre_index, uint16_t post_index, accum weight,
154  UNUSED uint16_t delay, unsigned long accum weight_scale) {
155  matrix_generator_weight_changer *conf = generator;
156  row_changer_plastic_t *plastic_row = get_changer_row(conf->synaptic_matrix,
157  conf->max_row_n_words, pre_index);
158  row_changer_fixed_t *fixed_row = get_changer_fixed_row(plastic_row);
159  uint32_t pos = fixed_row->fixed_plastic_size;
160  if (pos >= conf->max_row_n_synapses) {
161  log_warning("Row %u at 0x%08x, 0x%08x of matrix 0x%08x is already full (%u of %u)",
162  pre_index, plastic_row, fixed_row, conf->synaptic_matrix, pos,
163  conf->max_row_n_synapses);
164  return false;
165  }
166  uint16_t scaled_weight = rescale_weight(weight, weight_scale);
167  int16_t signed_weight = (int16_t) scaled_weight;
168  if (weight < 0) {
169  signed_weight = -signed_weight;
170  }
171  fixed_row->fixed_plastic_size = pos + 1;
172  fixed_row->fixed_plastic_data[pos] = build_changer_word(conf->synapse_type,
173  post_index, conf->synapse_type_bits, conf->synapse_index_bits,
174  signed_weight);
175  return true;
176 }
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.
#define N_HEADER_WORDS
The number of header words per row.
static void setup_changer_rows(uint32_t *matrix, uint32_t n_rows, uint32_t max_row_n_words, uint32_t row_offset)
Set up the rows so that they are ready for writing to.
int32_t fixed_plastic_data[]
the fixed-plastic data within the fixed region
static row_changer_fixed_t * get_changer_fixed_row(row_changer_plastic_t *plastic_row)
Get the fixed part of a row that comes after the plastic part.
static row_changer_plastic_t * get_changer_row(uint32_t *synaptic_matrix, uint32_t max_row_n_words, uint32_t pre_index)
Get a synaptic row for a given neuron.
void * matrix_generator_changer_initialize(void **region, void *synaptic_matrix)
Initialise the Changer synaptic matrix generator.
uint32_t plastic_plastic_size
the plastic-plastic size within the row
void matrix_generator_changer_free(void *generator)
Free any data for the matrix generator.
uint32_t fixed_fixed_size
the fixed-fixed size within the fixed region
uint32_t fixed_plastic_size
the fixed-plastic size within the fixed region
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.
region
spike source array region IDs in human readable form
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