Snitch Runtime
Loading...
Searching...
No Matches
alloc_v2.h File Reference

Defines functions to dynamically allocate the cluster's L1 memory. More...

Go to the source code of this file.

Functions

snrt_allocator_tsnrt_l1_allocator_v2 ()
 Get a pointer to the L1 allocator.
 
void * snrt_l1_next_v2 ()
 Get the next pointer of the L1 allocator.
 
void * snrt_l1_next_aligned_hyperbank ()
 Get the next pointer of the L1 allocator, aligned to the hyperbank.
 
void snrt_l1_update_next_v2 (void *next)
 Override the L1 allocator next pointer.
 
static void snrt_l1_alloc_check_bounds ()
 Check if the allocation exceeds the allocator bounds and raise an exception if it does.
 
void * snrt_l1_alloc_cluster_local (size_t size, const size_t alignment=1)
 Allocate space for a variable in the cluster's L1 memory.
 
template<typename T >
T * snrt_l1_alloc_cluster_local (size_t count=1, const size_t alignment=alignof(T))
 
void * snrt_l1_alloc_compute_core_local (size_t size, const size_t alignment=1)
 Allocate space for N variables in the cluster's L1 memory.
 
void snrt_l1_init ()
 Initialize the L1 allocator.
 
snrt_allocator_tsnrt_l3_allocator_v2 ()
 Get a pointer to the L3 allocator.
 
void * snrt_l3_next_v2 ()
 Get the next pointer of the L3 allocator.
 
static void snrt_l3_alloc_check_bounds ()
 Check if the allocation exceeds the allocator bounds and raise an exception if it does.
 
void * snrt_l3_alloc_v2 (size_t size, const size_t alignment=1)
 Allocate space for a variable in L3 memory.
 
void snrt_l3_init ()
 Initialize the L3 allocator.
 
void * snrt_compute_core_local_ptr (void *ptr, uint32_t core_idx, size_t size)
 Get a pointer to the same variable allocated by another core.
 
void * snrt_remote_l1_ptr (void *ptr, uint32_t src_cluster_idx, uint32_t dst_cluster_idx)
 Get a pointer to the same offset in another cluster's L1 memory.
 

Variables

__thread snrt_allocator_t l1_allocator_v2
 
__thread snrt_allocator_t l3_allocator_v2
 

Detailed Description

Defines functions to dynamically allocate the cluster's L1 memory.

This file provides functions to dynamically allocate the cluster's L1 memory. It includes functions for allocating memory for cluster-local variables, compute core-local variables, and for manipulating pointers to variables allocated by different cores or clusters.

Function Documentation

◆ snrt_compute_core_local_ptr()

void * snrt_compute_core_local_ptr ( void * ptr,
uint32_t core_idx,
size_t size )
inline

Get a pointer to the same variable allocated by another core.

This function takes a pointer to a variable allocated using snrt_l1_alloc_compute_core_local(size_t, const size_t) and returns a pointer to the same variable allocated by another core, as specified by core_idx. The size argument should be the same used during allocation.

Parameters
ptrPointer to the variable allocated by the current core.
core_idxIndex of the core that allocated the variable.
sizeThe size of the variable.
Returns
Pointer to the same variable allocated by the specified core.
228 {
229 size_t offset = (core_idx - snrt_cluster_core_idx()) * size;
230 return (void *)((uintptr_t)ptr + offset);
231}

◆ snrt_l1_alloc_check_bounds()

static void snrt_l1_alloc_check_bounds ( )
inlinestatic

Check if the allocation exceeds the allocator bounds and raise an exception if it does.

57 {
58 if (snrt_l1_allocator_v2()->next > snrt_l1_allocator_v2()->end)
59 asm volatile("ecall \n");
60}
snrt_allocator_t * snrt_l1_allocator_v2()
Get a pointer to the L1 allocator.
Definition alloc_v2.h:26

◆ snrt_l1_alloc_cluster_local() [1/2]

template<typename T >
T * snrt_l1_alloc_cluster_local ( size_t count = 1,
const size_t alignment = alignof(T) )
inline
86 {
87 snrt_l1_allocator_v2()->next =
88 snrt_align_up(snrt_l1_allocator_v2()->next, alignment);
89 T *retval = (T *)snrt_l1_next_v2();
90 snrt_l1_allocator_v2()->next += count * sizeof(T);
92 return retval;
93}
static void snrt_l1_alloc_check_bounds()
Check if the allocation exceeds the allocator bounds and raise an exception if it does.
Definition alloc_v2.h:57
void * snrt_l1_next_v2()
Get the next pointer of the L1 allocator.
Definition alloc_v2.h:33

◆ snrt_l1_alloc_cluster_local() [2/2]

void * snrt_l1_alloc_cluster_local ( size_t size,
const size_t alignment = 1 )
inline

Allocate space for a variable in the cluster's L1 memory.

This function dynamically allocates space for a variable of size size in the cluster's L1 memory. The allocation is aligned to the specified alignment.

Parameters
sizeThe size of the variable to allocate.
alignmentThe alignment of the allocation. An alignment of 1 (byte) is equivalent to no alignment (in a byte-addressable system).
Returns
Pointer to the allocated variable.
75 {
76 snrt_l1_allocator_v2()->next =
77 snrt_align_up(snrt_l1_allocator_v2()->next, alignment);
78 void *retval = snrt_l1_next_v2();
79 snrt_l1_allocator_v2()->next += size;
81 return retval;
82}

◆ snrt_l1_alloc_compute_core_local()

void * snrt_l1_alloc_compute_core_local ( size_t size,
const size_t alignment = 1 )
inline

Allocate space for N variables in the cluster's L1 memory.

This function dynamically allocates space for N variables of size size in the cluster's L1 memory, where N is the number of compute cores in the cluster. The variables are allocated in a contiguous block of memory. The whole block is aligned to the specified alignment.

Parameters
sizeThe size of each variable to allocate.
alignmentThe alignment of the allocation.
Returns
Pointer to the allocated variable for each compute core. The return value for the DM core is undefined.
109 {
110 snrt_l1_allocator_v2()->next =
111 snrt_align_up(snrt_l1_allocator_v2()->next, alignment);
112 void *retval =
113 ((uint8_t *)snrt_l1_next_v2()) + size * snrt_cluster_core_idx();
114 snrt_l1_allocator_v2()->next += size * snrt_cluster_compute_core_num();
116 return retval;
117}

◆ snrt_l1_allocator_v2()

snrt_allocator_t * snrt_l1_allocator_v2 ( )
inline

Get a pointer to the L1 allocator.

Returns
Pointer to the L1 allocator.
26{ return &l1_allocator_v2; }

◆ snrt_l1_init()

void snrt_l1_init ( )
inline

Initialize the L1 allocator.

This function initializes the L1 allocator by calculating the end address of the heap and setting the base, end, and next pointers of the allocator.

Note
This function should be called before using any of the allocation functions.
128 {
129 // Calculate end address of the heap. The top of the TCDM address space is
130 // reserved for the cluster-local storage (CLS) and the stack of every
131 // core. We further provision a safety margin of 128B. The rest of the
132 // TCDM is reserved for the heap.
133 uint32_t heap_end_addr = snrt_cls_base_addr();
134 heap_end_addr -= (1 << SNRT_LOG2_STACK_SIZE) * snrt_cluster_core_num();
135 heap_end_addr -= 128;
136 // Initialize L1 allocator
137 uintptr_t l1_start_addr = (uintptr_t)(snrt_cluster()->tcdm.mem);
138 snrt_l1_allocator_v2()->base = snrt_align_up(l1_start_addr, MIN_CHUNK_SIZE);
139 snrt_l1_allocator_v2()->end = heap_end_addr;
141}

◆ snrt_l1_next_aligned_hyperbank()

void * snrt_l1_next_aligned_hyperbank ( )
inline

Get the next pointer of the L1 allocator, aligned to the hyperbank.

Returns
The next pointer of the L1 allocator.
40 {
41 return snrt_align_up_hyperbank(snrt_l1_next_v2());
42}

◆ snrt_l1_next_v2()

void * snrt_l1_next_v2 ( )
inline

Get the next pointer of the L1 allocator.

Returns
The next pointer of the L1 allocator.
33{ return (void *)snrt_l1_allocator_v2()->next; }

◆ snrt_l1_update_next_v2()

void snrt_l1_update_next_v2 ( void * next)
inline

Override the L1 allocator next pointer.

Parameters
nextThe new value for the next pointer.
49 {
50 snrt_l1_allocator_v2()->next = (uint32_t)next;
51}

◆ snrt_l3_alloc_check_bounds()

static void snrt_l3_alloc_check_bounds ( )
inlinestatic

Check if the allocation exceeds the allocator bounds and raise an exception if it does.

167 {
168 if (snrt_l3_allocator_v2()->next >= snrt_l3_allocator_v2()->end)
169 asm volatile("ecall \n");
170}
snrt_allocator_t * snrt_l3_allocator_v2()
Get a pointer to the L3 allocator.
Definition alloc_v2.h:154

◆ snrt_l3_alloc_v2()

void * snrt_l3_alloc_v2 ( size_t size,
const size_t alignment = 1 )
inline

Allocate space for a variable in L3 memory.

This function dynamically allocates space for a variable of size size in L3 memory. The allocation is aligned to the specified alignment.

Parameters
sizeThe size of the variable to allocate.
alignmentThe alignment of the allocation. An alignment of 1 (byte) is equivalent to no alignment (in a byte-addressable system).
Returns
Pointer to the allocated variable.
184 {
185 snrt_l3_allocator_v2()->next =
186 snrt_align_up(snrt_l3_allocator_v2()->next, alignment);
187 void *retval = snrt_l3_next_v2();
188 snrt_l3_allocator_v2()->next += size;
190 return retval;
191}
void * snrt_l3_next_v2()
Get the next pointer of the L3 allocator.
Definition alloc_v2.h:161
static void snrt_l3_alloc_check_bounds()
Check if the allocation exceeds the allocator bounds and raise an exception if it does.
Definition alloc_v2.h:167

◆ snrt_l3_allocator_v2()

snrt_allocator_t * snrt_l3_allocator_v2 ( )
inline

Get a pointer to the L3 allocator.

Returns
Pointer to the L3 allocator.
154{ return &l3_allocator_v2; }

◆ snrt_l3_init()

void snrt_l3_init ( )
inline

Initialize the L3 allocator.

This function initializes the L3 allocator, starting at the _edram symbol. See linker script for definition of said symbol.

Note
This function should be called before using any of the allocation functions.
202 {
203 extern uint32_t _edram;
204 snrt_l3_allocator_v2()->base =
205 snrt_align_up((uint32_t)&_edram, MIN_CHUNK_SIZE);
206 snrt_l3_allocator_v2()->end = SNRT_L3_END_ADDR;
208}

◆ snrt_l3_next_v2()

void * snrt_l3_next_v2 ( )
inline

Get the next pointer of the L3 allocator.

Returns
The next pointer of the L3 allocator.
161{ return (void *)snrt_l3_allocator_v2()->next; }

◆ snrt_remote_l1_ptr()

void * snrt_remote_l1_ptr ( void * ptr,
uint32_t src_cluster_idx,
uint32_t dst_cluster_idx )
inline

Get a pointer to the same offset in another cluster's L1 memory.

This function takes a pointer to a variable in the calling (source) cluster's L1 memory and returns a pointer to the same offset in the target (destination) cluster's L1 memory.

Parameters
ptrPointer to the variable in the source cluster's L1 memory.
src_cluster_idxIndex of the source cluster.
dst_cluster_idxIndex of the destination cluster.
Returns
Pointer to the same offset in the destination cluster's L1 memory.
246 {
247 return (void *)((uintptr_t)ptr +
248 (dst_cluster_idx - src_cluster_idx) * SNRT_CLUSTER_OFFSET);
249}