sPyNNaker neural_modelling  7.4.2
param_generator.c
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 
21 #include "param_generator.h"
22 #include <spin1_api.h>
23 #include <debug.h>
24 #include "generator_types.h"
25 
33 
35 enum {
52 };
53 
57 typedef struct param_generator_info {
71 
76  const param_generator_info *type;
77  void *data;
78 };
79 
83 static const struct param_generator_info param_generators[] = {
84  {CONSTANT,
88  {UNIFORM,
92  {NORMAL,
104  {EXPONENTIAL,
112 };
113 
114 param_generator_t param_generator_init(uint32_t hash, void **in_region) {
115  // Look through the known generators
116  for (uint32_t i = 0; i < N_PARAM_GENERATORS; i++) {
117  const param_generator_info *type = &param_generators[i];
118 
119  // If the hash requested matches the hash of the generator, use it
120  if (hash == type->hash) {
121  // Prepare a space for the data
122  struct param_generator *generator =
123  spin1_malloc(sizeof(struct param_generator));
124  if (generator == NULL) {
125  log_error("Could not create generator");
126  return NULL;
127  }
128 
129  // Store the index
130  generator->type = type;
131 
132  // Initialise the generator and store the data
133  generator->data = type->initialize(in_region);
134  return generator;
135  }
136  }
137  log_error("Param generator with hash %u not found", hash);
138  return NULL;
139 }
140 
141 accum param_generator_generate(param_generator_t generator) {
142  return generator->type->generate(generator->data);
143 }
144 
145 void param_generator_free(param_generator_t generator) {
146  generator->type->free(generator->data);
147  sark_free(generator);
148 }
General types associated with generators.
void *() initialize_param_func(void **region)
How to initialise the param generator.
uint32_t generator_hash_t
The type of values used to indicate the subtype of generator to create. Must match the constants on t...
void() free_func(void *data)
How to free any data for the generator; all generator types use the same signature of free func.
accum() generate_param_func(void *generator)
How to generate values with a parameter generator.
@ NORMAL
A parameter that is a normally-distributed random variable.
@ CONSTANT
A parameter that is a constant.
@ UNIFORM
A parameter that is a uniformly-distributed random variable.
@ EXPONENTIAL
A parameter that is an exponentially-distributed random variable.
@ N_PARAM_GENERATORS
The number of known generators.
@ NORMAL_CLIPPED
A parameter that is a clipped-normally-distributed random variable.
@ NORMAL_CLIPPED_BOUNDARY
A parameter that is a clamped-normally-distributed random variable.
@ EXPONENTIAL_CLIPPED
A parameter that is a clipped-exponentially-distributed random variable.
free_func * free
Free any data for the generator.
initialize_param_func * initialize
Initialise the generator.
void param_generator_free(param_generator_t generator)
Finish with a parameter generator.
static const struct param_generator_info param_generators[]
An Array of known generators.
generate_param_func * generate
Generate values with a parameter generator.
param_generator_t param_generator_init(uint32_t hash, void **in_region)
Initialise a specific parameter generator.
generator_hash_t hash
The hash of the generator.
accum param_generator_generate(param_generator_t generator)
Generate value with a parameter generator.
The data for a parameter generator.
A "class" for parameter generators.
Interface for parameter generator.
Constant value parameter generator implementation.
static void * param_generator_constant_initialize(void **region)
How to initialise the constant parameter generator.
static accum param_generator_constant_generate(void *generator)
How to generate values with the constant parameter generator.
static void param_generator_constant_free(void *generator)
How to free any data for the constant parameter generator.
Exponentially distributed random parameter generator implementation.
static accum param_generator_exponential_generate(void *generator)
How to generate values with the exponential RNG parameter generator.
static void param_generator_exponential_free(void *generator)
How to free any data for the exponential RNG parameter generator.
static void * param_generator_exponential_initialize(void **region)
How to initialise the exponential RNG parameter generator.
Exponentially distributed random parameter generator implementation.
static accum param_generator_exponential_clipped_generate(void *generator)
How to generate values with the exponential RNG parameter generator.
static void param_generator_exponential_clipped_free(void *generator)
How to free any data for the exponential RNG parameter generator.
static void * param_generator_exponential_clipped_initialize(void **region)
How to initialise the exponential RNG parameter generator.
Normally distributed random parameter generator implementation.
static void param_generator_normal_free(void *generator)
How to free any data for the normal RNG parameter generator.
static accum param_generator_normal_generate(void *generator)
How to generate values with the normal RNG parameter generator.
static void * param_generator_normal_initialize(void **region)
How to initialise the normal RNG parameter generator.
Normally distributed random, redrawn if out of boundary, parameter generator implementation.
static accum param_generator_normal_clipped_generate(void *generator)
How to generate values with the clipped normal RNG parameter generator.
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.
Normally distributed random set to boundary parameter generator implementation.
static void * param_generator_normal_clipped_boundary_initialize(void **region)
How to initialise the clamped normal RNG parameter generator.
static void param_generator_normal_clipped_boundary_free(void *generator)
How to free any data for the clamped normal RNG parameter generator.
static accum param_generator_normal_clipped_boundary_generate(void *generator)
How to generate values with the clamped normal RNG parameter generator.
Uniformly distributed random set to boundary parameter generator implementation.
static accum param_generator_uniform_generate(void *generator)
How to generate values with the uniform RNG parameter generator.
static void param_generator_uniform_free(void *generator)
How to free any data for the uniform RNG parameter generator.
static void * param_generator_uniform_initialize(void **region)
How to initialise the uniform RNG parameter generator.