sPyNNaker neural_modelling  development
connection_generator_fixed_prob.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 <synapse_expander/rng.h>
24 
25 // Eclipse does *NOT* like this type!
26 typedef unsigned long fract probability_t;
27 
30  uint32_t allow_self_connections;
31  probability_t probability;
32 };
33 
39 struct fixed_prob {
41 };
42 
50  // Allocate memory for the data
51  struct fixed_prob *obj = spin1_malloc(sizeof(struct fixed_prob));
52 
53  // Copy the parameters in
54  struct fixed_prob_params *params_sdram = *region;
55  obj->params = *params_sdram;
56  *region = &params_sdram[1];
57 
58  log_debug("Fixed Probability Connector, allow self connections = %u, "
59  "probability = %k",
60  obj->params.allow_self_connections, (accum) obj->params.probability);
61  return obj;
62 }
63 
68 static void connection_generator_fixed_prob_free(void *generator) {
69  sark_free(generator);
70 }
71 
92  void *generator, uint32_t pre_lo, uint32_t pre_hi,
93  uint32_t post_lo, uint32_t post_hi, UNUSED uint32_t post_index,
94  uint32_t post_slice_start, uint32_t post_slice_count,
95  unsigned long accum weight_scale, accum timestep_per_delay,
96  param_generator_t weight_generator, param_generator_t delay_generator,
97  matrix_generator_t matrix_generator) {
98  struct fixed_prob *obj = generator;
99 
100  // Get the actual ranges to generate within
101  uint32_t post_start = max(post_slice_start, post_lo);
102  uint32_t post_end = min(post_slice_start + post_slice_count - 1, post_hi);
103 
104  for (uint32_t pre = pre_lo; pre <= pre_hi; pre++) {
105  for (uint32_t post = post_start; post <= post_end; post++) {
106  if (pre == post && !obj->params.allow_self_connections) {
107  continue;
108  }
109 
110  // Generate a random number
111  probability_t value = ulrbits(rng_generator(core_rng));
112 
113  // If less than our probability, generate a connection
114  if (value < obj->params.probability) {
115  uint32_t local_post = post - post_slice_start;
116  accum weight = param_generator_generate(weight_generator);
117  uint16_t delay = rescale_delay(
118  param_generator_generate(delay_generator), timestep_per_delay);
119  if (!matrix_generator_write_synapse(matrix_generator, pre, local_post,
120  weight, delay, weight_scale)) {
121  // Retry not useful here
122  log_warning("Could not add to matrix!");
123  }
124  }
125  }
126  }
127  return true;
128 }
static void * connection_generator_fixed_prob_initialise(void **region)
Initialise the fixed-probability connection generator.
static bool connection_generator_fixed_prob_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 fixed-probability connection generator.
static void connection_generator_fixed_prob_free(void *generator)
Free the fixed-probability connection generator.
The data structure to be passed around for this connector.
The parameters that can be copied in from SDRAM.
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.
rng_t * core_rng
An RNG that is local to the current core.
accum param_generator_generate(param_generator_t generator)
Generate value with a parameter generator.
uint32_t rng_generator(rng_t *rng)
Generate a uniformly-distributed random number.
Definition: rng.c:26
Random number generator interface.
region
spike source array region IDs in human readable form
static stdp_params params
Configuration parameters.