NUMA感知的高速缓存对齐的内存分配 [英] NUMA aware cache aligned memory allocation

查看:349
本文介绍了NUMA感知的高速缓存对齐的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆