sPyNNaker neural_modelling  development
param_generator_normal_clipped.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 <stdfix.h>
23 #include <spin1_api.h>
24 #include <stdfix-full-iso.h>
25 #include <normal.h>
26 #include <synapse_expander/rng.h>
28 
32 #define MAX_REDRAWS 1000
33 
38  accum mu;
39  accum sigma;
40  accum low;
41  accum high;
42 };
43 
50 };
51 
59  // Allocate memory for the data
60  struct param_generator_normal_clipped *obj =
61  spin1_malloc(sizeof(struct param_generator_normal_clipped));
62  struct normal_clipped_params *params_sdram = *region;
63 
64  // Copy the parameters in
65  obj->params = *params_sdram;
66  *region = &params_sdram[1];
67 
68  log_debug("normal clipped mu = %k, sigma = %k, low = %k, high = %k",
69  obj->params.mu, obj->params.sigma, obj->params.low,
70  obj->params.high);
71  return obj;
72 }
73 
78 static void param_generator_normal_clipped_free(void *generator) {
79  sark_free(generator);
80 }
81 
87 static accum param_generator_normal_clipped_generate(void *generator) {
88  // For each index, generate a normally distributed random value, redrawing
89  // if outside the given range
90  struct param_generator_normal_clipped *obj = generator;
91  uint32_t n_draws = 0;
92  accum value = 0k;
93  do {
94  value = rng_normal(core_rng);
95  value = obj->params.mu + (value * obj->params.sigma);
96  n_draws++;
97  } while ((value < obj->params.low || value > obj->params.high)
98  && (n_draws < MAX_REDRAWS));
99  if (n_draws == MAX_REDRAWS) {
100  log_error("Maximum number of redraws (%u) exceeded on clipped normal "
101  "distribution with mu=%k, sigma=%k, low=%k, high=%k",
102  n_draws, obj->params.mu, obj->params.sigma, obj->params.low,
103  obj->params.high);
104  rt_error(RTE_SWERR);
105  }
106  return value;
107 }
General types associated with generators.
rng_t * core_rng
An RNG that is local to the current core.
static accum param_generator_normal_clipped_generate(void *generator)
How to generate values with the clipped normal RNG parameter generator.
#define MAX_REDRAWS
The maximum number of redraws performed before giving up.
static void * param_generator_normal_clipped_initialize(void **region)
How to initialise the clipped normal RNG parameter generator.
static void param_generator_normal_clipped_free(void *generator)
How to free any data for the clipped normal RNG parameter generator.
The parameters that can be copied in from SDRAM.
The data structure to be passed around for this generator. This includes the parameters and an RNG.
accum rng_normal(rng_t *rng)
Generate an normally-distributed random number.
Definition: rng.c:34
Random number generator interface.
region
spike source array region IDs in human readable form
static stdp_params params
Configuration parameters.