sPyNNaker neural_modelling 7.1.1
Loading...
Searching...
No Matches
matrix_generator_neuromodulation.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
29typedef 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 is_reward;
40 uint32_t synapse_type;
42
44typedef struct {
46 uint32_t synapse_type:30;
47 uint32_t is_reward:1;
48 uint32_t is_neuromodulation:1;
50
52typedef struct {
55 uint32_t fixed_plastic_data[];
57
58// This is the amount to scale neuromodulation weights by to put them in STDP
59// fixed point format
60static const unsigned long accum WEIGHT_SCALE = 2048.0ulk;
61
71static row_nm_plastic_t *get_nm_row(uint32_t *synaptic_matrix, uint32_t max_row_n_words,
72 uint32_t pre_index) {
73 uint32_t idx = pre_index * (max_row_n_words + N_HEADER_WORDS);
74 return (row_nm_plastic_t *) &synaptic_matrix[idx];
75}
76
83 return (row_nm_fixed_t *) &plastic_row[1];
84}
85
92static void setup_nm_rows(uint32_t *matrix, uint32_t n_rows, uint32_t max_row_n_words,
93 uint32_t is_reward, uint32_t synapse_type) {
94 // Set all the header half-words to 0 and set all the sizes
95 for (uint32_t i = 0; i < n_rows; i++) {
96 row_nm_plastic_t *row = get_nm_row(matrix, max_row_n_words, i);
97 row->plastic_plastic_size = 1;
98 row->is_neuromodulation = 1;
99 row->is_reward = is_reward;
100 row->synapse_type = synapse_type;
101 row_nm_fixed_t *fixed = get_nm_fixed_row(row);
102 fixed->fixed_fixed_size = 0;
103 fixed->fixed_plastic_size = 0;
104 }
105}
106
107
116 void *synaptic_matrix) {
117 // Allocate memory for the parameters
119 spin1_malloc(sizeof(matrix_generator_neuromodulation));
120
121 // Copy the parameters in
123 *conf = *params_sdram;
124 *region = &params_sdram[1];
125
126 // Offsets are in words
127 uint32_t *syn_mat = synaptic_matrix;
128 conf->synaptic_matrix = &(syn_mat[conf->synaptic_matrix_offset]);
129 setup_nm_rows(conf->synaptic_matrix, conf->n_pre_neurons, conf->max_row_n_words,
130 conf->is_reward, conf->synapse_type);
131
132 return conf;
133}
134
140 sark_free(generator);
141}
142
143static bool matrix_generator_neuromodulation_write_synapse(void *generator,
144 uint32_t pre_index, uint16_t post_index, accum weight,
145 UNUSED uint16_t delay, UNUSED unsigned long accum weight_scale) {
146 matrix_generator_neuromodulation *conf = generator;
147 row_nm_plastic_t *plastic_row = get_nm_row(conf->synaptic_matrix,
148 conf->max_row_n_words, pre_index);
149 row_nm_fixed_t *fixed_row = get_nm_fixed_row(plastic_row);
150 uint32_t pos = fixed_row->fixed_plastic_size;
151 if (pos >= conf->max_row_n_synapses) {
152 log_warning("Row %u at 0x%08x, 0x%08x of matrix 0x%08x is already full (%u of %u)",
153 pre_index, plastic_row, fixed_row, conf->synaptic_matrix, pos,
154 conf->max_row_n_synapses);
155 return false;
156 }
157 uint16_t scaled_weight = rescale_weight(weight, WEIGHT_SCALE);
158 fixed_row->fixed_plastic_size = pos + 1;
159 fixed_row->fixed_plastic_data[pos] = (scaled_weight << 16) | post_index;
160 return true;
161}
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.
Common functions for matrix generation.
#define N_HEADER_WORDS
The number of header words per row.
static row_nm_plastic_t * get_nm_row(uint32_t *synaptic_matrix, uint32_t max_row_n_words, uint32_t pre_index)
Get a synaptic row for a given neuron.
uint32_t synapse_type
the synapse type of the row
uint32_t fixed_fixed_size
the fixed-fixed size within the fixed region
static row_nm_fixed_t * get_nm_fixed_row(row_nm_plastic_t *plastic_row)
Get the fixed part of a row that comes after the plastic part.
uint32_t fixed_plastic_data[]
the fixed-plastic data within the fixed region
uint32_t plastic_plastic_size
the plastic-plastic size within the row
uint32_t fixed_plastic_size
the fixed-plastic size within the fixed region
void * matrix_generator_neuromodulation_initialize(void **region, void *synaptic_matrix)
Initialise the Neuromodulation synaptic matrix generator.
static void setup_nm_rows(uint32_t *matrix, uint32_t n_rows, uint32_t max_row_n_words, uint32_t is_reward, uint32_t synapse_type)
Set up the rows so that they are ready for writing to.
uint32_t is_reward
is the row reward
void matrix_generator_neuromodulation_free(void *generator)
Free any data for the STDP synaptic matrix generator.
uint32_t is_neuromodulation
is the row neuromodulation
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