NUMA感知的高速缓存对齐的内存分配 [英] NUMA aware cache aligned memory allocation
问题描述
在linux系统中,pthreads库为我们提供了用于对齐缓存以防止错误共享的函数(posix_memalign).要选择架构的特定NUMA节点,我们可以使用libnuma库.我想要的是同时需要两者的东西.我将某些线程绑定到某些处理器,并且我想为来自相应NUMA节点的每个线程分配本地数据结构,以减少这些线程的内存操作延迟.我该怎么办?
In linux systems, pthreads library provides us a function (posix_memalign) for cache alignment to prevent false sharing. And to choose a specific NUMA node of the arhitecture we can use libnuma library. What I want is something needing both two. I bind certain threads to some certain processors and I want allocate local data structures for each thread from the corresponding NUMA node in order to decrease delay in memory operations for the threads. How can I do this?
推荐答案
如果您只是希望获得有关NUMA分配器的对齐功能,则可以轻松构建自己的对齐功能.
If you're just looking to get the alignment functionality around a NUMA allocator, you can easily build your own.
这个想法是用更多的空间来调用未对齐的malloc()
.然后返回第一个对齐的地址.为了释放它,您需要将基址存储在已知位置.
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);
}
使用此功能时,您需要调用my_NUMA_free
来分配使用my_NUMA_malloc
分配的任何内容.
To when you use this, you need to call my_NUMA_free
for anything allocated with my_NUMA_malloc
.
这篇关于NUMA感知的高速缓存对齐的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!