How To Allocate Shared Memory in Specific NUMA Node?

127 views Asked by At

I am trying to check the NUMA Effect on shared memory.

I want to allocate a shared memory(using shmget(), shmat()) in a specific NUMA node, for example in node 0. Other works will be done in another NUMA node, example in node 1. With this progress, I want to compare the access latency between local memory in same node and shared memory in another node.

However, I found out that using numa_alloc_..() doesn't work well for shared memory. So my approach was to use numa_set_preferred() instead of numa_alloc_..(). But I'm not sure this is right approach. Here is my code written in C. I tested in Ubuntu 18.04.

int shmid;
void *shm_ptr;

int current_cpu = sched_getcpu();
int local_node = numa_node_of_cpu(current_cpu);
int numa_node = local_node ? 0 : 1;

numa_set_preferred(numa_node);

 if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0)
{
    perror("shmget error");
    exit(1);
}

if ((shm_ptr = shmat(shmid, NULL, 0)) == (void *)-1)
{
    printf("shmat error\n");
    return;
}

/...do other works.../

numa_set_preferred(local_node);

Does this logic works well ? If not, is there other way to allocate a shared memory in a specific NUMA node ?

Sorry for my bad english by the way. Thank you.

0

There are 0 answers