NUMA aware cache aligned memory allocation
If you're just looking to get the alignment functionality around a NUMA allocator, you can easily build your own.
The idea is to call the unaligned malloc()
with a little bit more space. Then return the first aligned address. To be able to free it, you need to store the base address at a known location.
Here's an example. Just substitute the names with whatever is appropriate:
pint // An unsigned integer that is large enough to store a pointer.
NUMA_malloc // The NUMA malloc function
NUMA_free // The NUMA free function
void* my_NUMA_malloc(size_t bytes,size_t align, /* NUMA parameters */ ){
// The NUMA malloc function
void *ptr = numa_malloc(
(size_t)(bytes + align + sizeof(pint)),
/* NUMA parameters */
);
if (ptr == NULL)
return NULL;
// Get aligned return address
pint *ret = (pint*)((((pint)ptr + sizeof(pint)) & ~(pint)(align - 1)) + align);
// Save the free pointer
ret[-1] = (pint)ptr;
return ret;
}
void my_NUMA_free(void *ptr){
if (ptr == NULL)
return;
// Get the free pointer
ptr = (void*)(((pint*)ptr)[-1]);
// The NUMA free function
numa_free(ptr);
}
To when you use this, you need to call my_NUMA_free
for anything allocated with my_NUMA_malloc
.
linux kernel module memory layout under NUMA
It depends on the OS. If you are using Linux a "First Touch" policy will be applied. This means all the data of you program will be allocated on a node close to the one containing the CPU on which your program is running.
Logically your program will see a unique shared memory. In a lower lever the OS may allocate the data on different NUMA node causing some performance issues.
It depends on you if you want to write a NUMA-aware code to avoid NUMA latency and contention. Although some Linux distribution are already implementing some NUMA balancing features. (e.g. Automatic NUMA Balancing)
Related Topics
Understanding Glibc Malloc Trimming
Linux Set End of File (Shrink, Truncate, Cut Out Some Data @ End)
How to Join 2 CSV Files with a Shell Script
Find Is Returning "Find: .: Permission Denied", But I Am Not Searching In
Parsing Result of Diff in Shell Script
In Bash Tee Is Making Function Variables Local, How to Escape This
Tail a Log into an Excerpt in Real Time
Bash Ip If Then Else Statement
How to Upgrade a Package in Linux That Was Built from Source
Find Ip Address of My System for a Particular Interface with Shell Script (Bash)
Ssh-Add from Bash Script and Automate Passphrase Entry
Can 'Vim' Open a Large File in Read Only Mode as Fast as 'Less'
Where Do Char Device Appear After Cdev_Add() Registers Successfully with Major on 117.
Docker: Permission Denied to Local MySQL Volume
Why Does The Stack Have to Be Page Aligned