sPyNNaker neural_modelling  7.4.2
post_events_with_weight_change.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 
19 #ifndef _POST_EVENTS_H_
20 #define _POST_EVENTS_H_
21 
22 // Standard includes
23 #include <stdbool.h>
24 #include <stdint.h>
25 
26 // Include debug header for log_info etc
27 #include <debug.h>
28 #include <stddef.h>
29 
30 //---------------------------------------
31 // Macros
32 //---------------------------------------
34 #define MAX_EVENTS 16
35 
36 typedef struct update_post_trace_t {
37 
39  int16_t weight_change;
40 
42  uint16_t synapse_type;
43 
45  uint32_t pre_spike;
47 
48 //---------------------------------------
49 // Structures
50 //---------------------------------------
52 typedef struct {
54  uint32_t count;
58 
59 //---------------------------------------
60 // Inline functions
61 //---------------------------------------
62 
67  uint32_t n_neurons) {
68  post_event_history_t *history =
69  spin1_malloc(n_neurons * sizeof(post_event_history_t));
70  // Check allocations succeeded
71  if (history == NULL) {
72  log_error("Unable to allocate global STDP structures - Out of DTCM: Try "
73  "reducing the number of neurons per core to fix this problem ");
74  return NULL;
75  }
76 
77  // Loop through neurons and set count to 0
78  for (uint32_t n = 0; n < n_neurons; n++) {
79  history[n].count = 0;
80  for (uint32_t e = 0; e < MAX_EVENTS; e++) {
81  history[n].traces[e].synapse_type = 0;
82  history[n].traces[e].pre_spike = 0;
83  history[n].traces[e].weight_change = 0;
84  }
85  }
86 
87  return history;
88 }
89 
90 //---------------------------------------
95 static inline void post_events_add(
96  post_event_history_t *events, uint16_t weight_change,
97  uint32_t pre_spike, uint16_t synapse_type) {
98  if (events->count < MAX_EVENTS) {
99  // If there's still space, store time at current end
100  // and increment count minus 1
101  const uint32_t new_index = events->count++;
102  events->traces[new_index].weight_change = weight_change;
103  events->traces[new_index].pre_spike = pre_spike;
104  events->traces[new_index].synapse_type = synapse_type;
105  log_debug("Added pre spike %u with weight change %d to index %d", pre_spike,
106  weight_change, new_index);
107  } else {
108  log_debug("Events full, shuffling");
109  // Otherwise Shuffle down elements
110  for (uint32_t e = 1; e < MAX_EVENTS; e++) {
111  events->traces[e - 1] = events->traces[e];
112  }
113 
114  // Stick new time at end
115  events->traces[MAX_EVENTS - 1].weight_change = weight_change;
116  events->traces[MAX_EVENTS - 1].pre_spike = pre_spike;
117  events->traces[MAX_EVENTS - 1].synapse_type = synapse_type;
118  log_debug("Added pre spike %u with weight change %d to index %d", pre_spike,
119  weight_change, MAX_EVENTS - 1);
120  }
121 }
122 
123 static inline bool post_events_remove(post_event_history_t *events, uint32_t index) {
124  // Already gone? nothing to do!
125  if (index >= events->count) {
126  return false;
127  }
128  if (events->count > 1) {
129  // Swap the last one with the one to remove
130  events->traces[index] = events->traces[events->count - 1];
131  }
132  events->count--;
133  return events->count > 0;
134 }
135 
136 #endif // _POST_EVENTS_H_
static uint32_t n_neurons
The number of neurons on the core.
Definition: neuron.c:45
uint16_t synapse_type
The synapse type.
uint32_t count
Number of events stored.
static post_event_history_t * post_events_init_buffers(uint32_t n_neurons)
Initialise an array of post-synaptic event histories.
static void post_events_add(post_event_history_t *events, uint16_t weight_change, uint32_t pre_spike, uint16_t synapse_type)
Add a post-synaptic event to the history.
int16_t weight_change
The amount to change the weight by (positive or negative)
uint32_t pre_spike
The pre-spike to look out for in doing the update.
#define MAX_EVENTS
Maximum number of pre-synaptic events per post neuron.
post_trace_t traces[MAX_POST_SYNAPTIC_EVENTS]
Event traces.
Definition: post_events.h:45
Trace history of post-synaptic events.
Definition: post_events.h:39