5. Dynamic memory management in target regions¶
Memory requests (malloc, calloc, etc.) within OpenMP target regions are satisfied by allocating portions from DSP heaps. The OpenMP implementation specifies fixed sizes and placements for private DSP heaps as well as the heap that is shared by all the DSPs. The DSP runtime provides additional APIs to initialize and manage heaps in order to afford the user more flexibility to control the size and location of heaps.
5.2. DSP Heap in Local Memory (L2SRAM)¶
The DSP runtime provides a simplistic API to initialize a heap in L2 SRAM and allocate from it. This heap is local to the core which initialized it.
5.2.1. Heap Initialization API¶
A heap can be initialized in L2 SRAM via the following API:
void __heap_init_l2(void *ptr, int size);
The storage associated with the heap must be start on a 64bit boundary. Unlike DDR and MSMC heaps, heaps initialized in L2 SRAM do not persist across target regions or kernels. Underlying storage for dsp heaps in local memory can be set up in one of the following ways:
5.2.1.1. Via the *local* map type in OpenMP¶
TI’s OpenMP implementation includes a TI-specific *local* map type that allows data to be allocated on a DSP’s L2 SRAM. This allocated buffer can be used to initialize the heap.
void l2_alloc_and_free(char *p, size_t bytes)
{
//p is actually just a dummy buffer. It will not be copied to the DSPs.
#pragma omp target map(to:bytes) map(local:p[0:bytes])
{
//p gets allocated in DSP L2 SRAMS at the start of the target region
char *p1;
__heap_init_l2(p, bytes);
p1 = (char *) __malloc_l2(bytes);
if (!p1)
printf("Error\n");
else
printf("L2SRAM heap pointer is 0x%08x\n", p1);
}
}
5.2.1.2. Via a DATA_SECTION pragma in DSP C code¶
/* Array is already aligned on a 64b boundary. No need for DATA_ALIGN */
#define L2_HEAP_SIZE (256)
#pragma DATA_SECTION(l2_heap, ".mem_l2")
char l2_heap[L2_HEAP_SIZE];
...
void foo()
{
__heap_init_l2 ((void *)l2_heap, L2_HEAP_SIZE);
...
... __malloc_l2(sizeof(double));
}
5.2.2. Dynamic Memory Management APIs¶
After the L2 heap is initialized by the DSP cores using the __heap_init_l2 call, the only API available is a malloc:
void *__malloc_l2 (size_t size); /* Pointer returned is aligned to 64 bit boundary */