2

I am currently working on a NUMA system with two CPUs. The memory access time is not the same for the two CPUs.

I have used PTHREAD_SETAFFINITY_NP to assign threads to specific CPUs:

int stick_this_thread_to_core(int core_id) {
  int num_cores = sysconf(_SC_NPROCESSORS_ONLN);//number of processors
  if (core_id < 0 || core_id >= num_cores)
     return EINVAL;
  cpu_set_t mask;
  CPU_ZERO(&mask);
  CPU_SET(core_id, &mask);
  pthread_t current_thread = pthread_self();    
  return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &mask);
}

My question is, does it also restrict the memory allocation of the thread? For example, if I malloc in the thread running in CPU-1, will the memory be allocated in memories of CPU-1 only?

Vince
  • 14,470
  • 7
  • 39
  • 84
user3743384
  • 91
  • 1
  • 11

1 Answers1

2

I wouldn't count on it. In windows, you have to use special functions to allcate memory on a particular numa node. Linux is pretty similar. Look for a memory mapping function in linux that takes the numa node.

EDIT: numa_malloc

See this link

NUMA aware cache aligned memory allocation

Community
  • 1
  • 1
johnnycrash
  • 5,184
  • 5
  • 34
  • 58