SpiNNFrontEndCommon 7.4.2
Common support code for user-facing front end systems.
Loading...
Searching...
No Matches
routing_table.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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 __ROUTING_TABLE_H__
20#define __ROUTING_TABLE_H__
21
22//=============================================================================
23
25typedef struct key_mask_t {
27 uint32_t key;
28
30 uint32_t mask;
32
34typedef struct entry_t {
37
39 uint32_t route;
40
42 uint32_t source;
43} entry_t;
44
46typedef struct table_t {
48 uint32_t size;
49
52} table_t;
53
58
64entry_t* routing_table_get_entry(uint32_t entry_id_to_find) {
65 return &table->entries[entry_id_to_find];
66}
67
71 return table->size;
72}
73
78void routing_table_remove_from_size(int size_to_remove) {
79 table->size -= size_to_remove;
80}
81
85static inline void routing_table_put_entry(const entry_t* entry, int index) {
86 entry_t* e_ptr = routing_table_get_entry(index);
87 e_ptr->key_mask.key = entry->key_mask.key;
88 e_ptr->key_mask.mask = entry->key_mask.mask;
89 e_ptr->route = entry->route;
90 e_ptr->source = entry->source;
91}
92
96static inline void routing_table_copy_entry(int new_index, int old_index) {
97 entry_t* e_ptr = routing_table_get_entry(old_index);
98 routing_table_put_entry(e_ptr, new_index);
99}
100
104static inline void swap_entries(int a, int b) {
105 log_debug("swap %u %u", a, b);
107 log_debug("before %u %u %u %u",
108 temp.key_mask.key, temp.key_mask.mask, temp.route, temp.source);
110 routing_table_put_entry(&temp, b);
112 log_debug("before %u %u %u %u",
113 temp2.key_mask.key, temp2.key_mask.mask, temp2.route, temp2.source);
114}
115
116//=============================================================================
117//state for reduction in parameters being passed around
118
122static inline uint32_t key_mask_get_xs(key_mask_t km) {
123 return ~km.key & ~km.mask;
124}
125
129static inline unsigned int key_mask_count_xs(key_mask_t km) {
130 return __builtin_popcount(key_mask_get_xs(km));
131}
132
137static inline bool key_mask_intersect(key_mask_t a, key_mask_t b) {
138 return (a.key & b.mask) == (b.key & a.mask);
139}
140
147 uint32_t new_xs = ~(a.key ^ b.key);
148 key_mask_t c;
149 c.mask = a.mask & b.mask & new_xs;
150 c.key = (a.key | b.key) & c.mask;
151
152 return c;
153}
154
156#define RTR_MC_SET_FAILED 0
157
158
162typedef struct {
166 uint32_t app_id;
167
171
173 uint32_t table_size;
174
176 entry_t entries[];
177} header_t;
178
181void print_header(header_t *header) {
182 log_debug("app_id = %d", header->app_id);
183 log_debug("compress_as_much_as_possible = %d",
185 log_debug("table_size = %d", header->table_size);
186}
187
190static void read_table(header_t *header) {
191 table = MALLOC(
192 sizeof(uint32_t) + (sizeof(entry_t) * header->table_size));
193 if (table == NULL) {
194 log_error("failed to allocate memory for routing tables");
196 }
197 // Copy the size of the table
198 table->size = header->table_size;
199
200 // Copy in the routing table entries
202 sizeof(entry_t) * table->size);
203}
204
209bool load_routing_table(uint32_t app_id) {
210 // Try to allocate sufficient room for the routing table.
211 uint32_t entry_id = rtr_alloc_id(table->size, app_id);
212 if (entry_id == 0) {
213 log_error("Unable to allocate routing table of size %u\n", table->size);
214 return FALSE;
215 }
216
217 // Load entries into the table (provided the allocation succeeded).
218 // Note that although the allocation included the specified
219 // application ID we also need to include it as the most significant
220 // byte in the route (see `sark_hw.c`).
221 for (uint32_t i = 0; i < table->size; i++) {
222 entry_t entry = table->entries[i];
223 uint32_t route = entry.route | (app_id << 24);
224 uint success = rtr_mc_set(
225 entry_id + i, entry.key_mask.key, entry.key_mask.mask, route);
226 if (success == RTR_MC_SET_FAILED) {
227 log_warning("failed to set a router table entry at index %d",
228 entry_id + i);
229 }
230 }
231
232 // Indicate we were able to allocate routing table entries.
233 return TRUE;
234}
235
236#endif // __ROUTING_TABLE_H__
void log_error(const char *message,...)
This function logs errors. Errors usually indicate a serious fault in the program,...
void log_warning(const char *message,...)
This function logs warnings.
void log_debug(const char *message,...)
This function logs debugging messages. This level of message is normally not printed except when the ...
#define MALLOC
An easily-insertable name for the memory allocator.
@ EXIT_FAIL
We went wrong but we dont want to RTE.
void malloc_extras_terminate(uint result_code)
Stops execution with a result code.
uint32_t app_id
uint32_t table_size
Initial size of the routing table.
static void read_table(header_t *header)
Read a new copy of the routing table from SDRAM.
key_mask_t key_mask
Key and mask.
void print_header(header_t *header)
Print the header object for debug purposes.
uint32_t mask
Mask for the key_mask.
entry_t entries[]
Routing table entries.
static void swap_entries(int a, int b)
Swap a pair of entries at the given indices.
static uint32_t key_mask_get_xs(key_mask_t km)
Get a mask of the Xs in a key_mask.
#define RTR_MC_SET_FAILED
flag for if a rtr_mc_set() failure.
entry_t entries[]
Entries in the table.
uint32_t size
Number of entries in the table.
static unsigned int key_mask_count_xs(key_mask_t km)
Get a count of the Xs in a key_mask.
table_t * table
The table being manipulated.
static key_mask_t key_mask_merge(key_mask_t a, key_mask_t b)
Generate a new key-mask which is a combination of two other key_masks.
static void routing_table_copy_entry(int new_index, int old_index)
Copy an entry from one index to another.
uint32_t route
Routing direction.
uint32_t compress_as_much_as_possible
int routing_table_get_n_entries(void)
Get the number of entries in the routing table.
static bool key_mask_intersect(key_mask_t a, key_mask_t b)
Determine if two key_masks would match any of the same keys.
bool load_routing_table(uint32_t app_id)
Load a routing table to the router.
entry_t * routing_table_get_entry(uint32_t entry_id_to_find)
Gets a pointer to where this entry is stored.
static void routing_table_put_entry(const entry_t *entry, int index)
Write an entry to a specific index.
uint32_t key
Key for the key_mask.
void routing_table_remove_from_size(int size_to_remove)
updates table stores accordingly.
uint32_t source
Source of packets arriving at this entry.
Holds data for a routing table entry.
The header of the routing table information in the input data block.
Holds key and mask.
Holds a routing table description.
uint rtr_alloc_id(uint size, uint app_id)
uint rtr_mc_set(uint entry, uint key, uint mask, uint route)
#define NULL
void spin1_memcpy(void *dst, void const *src, uint len)
#define TRUE
#define FALSE
unsigned int uint