sPyNNaker neural_modelling  7.4.2
connection_generator_one_to_one_offset.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 
23 
26  // Amount to add to the pre by to get the post
27  int32_t offset;
28  // Whether to wrap around the post values or just clip
29  uint32_t wrap;
30  // The group size to consider for the offset
31  uint32_t n_neurons_per_group;
32 
33 };
34 
35 
43  // Allocate the data structure for parameters
44  struct one_to_one_offset *params = spin1_malloc(sizeof(struct one_to_one_offset));
45  struct one_to_one_offset *params_sdram = *region;
46 
47  // Copy the parameters into the data structure
48  *params = *params_sdram;
49  *region = &params_sdram[1];
50 
51  log_debug("one_to_one_offset connector, one_to_one_offset = %u, wrap = %u, "
52  "n_neurons_per_group = %u",
53  params->offset, params->wrap, params->n_neurons_per_group);
54 
55  return params;
56 }
57 
62 static void connection_generator_one_to_one_offset_free(UNUSED void *generator) {
63  // Nothing to do
64 }
65 
86  void *generator, uint32_t pre_lo, uint32_t pre_hi,
87  uint32_t post_lo, uint32_t post_hi, UNUSED uint32_t post_index,
88  uint32_t post_slice_start, uint32_t post_slice_count,
89  unsigned long accum weight_scale, accum timestep_per_delay,
90  param_generator_t weight_generator, param_generator_t delay_generator,
91  matrix_generator_t matrix_generator) {
92 
93  struct one_to_one_offset *obj = generator;
94 
95  // Get the actual ranges to generate within
96  uint32_t post_start = max(post_slice_start, post_lo);
97  uint32_t post_end = min(post_slice_start + post_slice_count - 1, post_hi);
98 
99  // Work out where we are in the generation
100  // We need to connect each pre-neuron to each post-neuron in each group
101  // (but not to itself). We are currently generating a subset of the post
102  // neurons, so we need to work out which group we are in within that subset,
103  // and which is the first post-neuron in the group that we are generating
104  // for now.
105  uint32_t post_group;
106  uint32_t post_value;
107  div_mod(post_start, obj->n_neurons_per_group, &post_group, &post_value);
108 
109  // Work out where the pre-neurons start and end for the group that we are
110  // in at the start of the post-neurons.
111  uint32_t pre_start = pre_lo + post_group * obj->n_neurons_per_group;
112  uint32_t pre_end = min(pre_start + obj->n_neurons_per_group - 1, pre_hi);
113 
114  // Go through the post neurons in this slice
115  for (uint32_t post = post_start; post <= post_end; post++) {
116  uint32_t local_post = post - post_slice_start;
117 
118  // Find the pre that occurs after offset; as the offset is post from
119  // pre, we subtract it to get pre from post (note it might be negative already)
120  int32_t pre = post - obj->offset;
121  bool use = true;
122  if (pre < (int32_t) pre_start) {
123  if (obj->wrap) {
124  pre += obj->n_neurons_per_group;
125  } else {
126  use = false;
127  }
128  } else if (pre > (int32_t) pre_end) {
129  if (obj->wrap) {
130  pre -= obj->n_neurons_per_group;
131  } else {
132  use = false;
133  }
134  }
135 
136  if (use) {
137  accum weight = param_generator_generate(weight_generator);
138  uint16_t delay = rescale_delay(
139  param_generator_generate(delay_generator), timestep_per_delay);
141  local_post, weight, delay, weight_scale)) {
142  log_error("Matrix not sized correctly!");
143  return false;
144  }
145  }
146 
147  // Work out next loop iteration. If we have reached the end of a group
148  // of values, we need to move onto the next group.
149  post_value += 1;
150  if (post_value == obj->n_neurons_per_group) {
151  post_value = 0;
152  pre_start += obj->n_neurons_per_group;
153  pre_end = min(pre_start + obj->n_neurons_per_group - 1, pre_hi);
154  if (pre_start > pre_hi) {
155  break;
156  }
157  }
158  }
159 
160  return true;
161 }
static void * connection_generator_one_to_one_offset_initialise(void **region)
Initialise the one_to_one_offset connection generator.
static bool connection_generator_one_to_one_offset_generate(void *generator, uint32_t pre_lo, uint32_t pre_hi, uint32_t post_lo, uint32_t post_hi, uint32_t post_index, uint32_t post_slice_start, uint32_t post_slice_count, unsigned long accum weight_scale, accum timestep_per_delay, param_generator_t weight_generator, param_generator_t delay_generator, matrix_generator_t matrix_generator)
Generate connections with the one_to_one_offset connection generator.
static void connection_generator_one_to_one_offset_free(void *generator)
Free the one_to_one_offset connection generator.
The parameters to be passed around for this connector.
General types associated with generators.
static uint16_t rescale_delay(accum delay, accum timestep_per_delay)
Rescales a delay to account for timesteps and type-converts it.
bool matrix_generator_write_synapse(matrix_generator_t generator, uint32_t pre_index, uint16_t post_index, accum weight, uint16_t delay, unsigned long accum weight_scale)
Write a synapse with a matrix generator.
The data for a matrix generator.
accum param_generator_generate(param_generator_t generator)
Generate value with a parameter generator.
region
spike source array region IDs in human readable form
static stdp_params params
Configuration parameters.