【问题标题】:Thread Affinity also restrict memory allocation?Thread Affinity 也限制内存分配?
【发布时间】:2014-06-16 02:54:24
【问题描述】:

我目前正在使用具有两个 CPU 的 NUMA 系统。两个 CPU 的内存访问时间不一样。

我已经使用PTHREAD_SETAFFINITY_NP 将线程分配给特定的 CPU:

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);
}

我的问题是,它是否也限制了线程的内存分配?比如我malloc在CPU-1运行的线程中,内存会不会只分配在CPU-1的内存中?

【问题讨论】:

    标签: c multithreading memory memory-management


    【解决方案1】:

    我不会指望它。在 Windows 中,您必须使用特殊函数来分配特定 numa 节点上的内存。 Linux 非常相似。在 linux 中寻找一个采用 numa 节点的内存映射函数。

    编辑: numa_malloc

    查看此链接

    NUMA aware cache aligned memory allocation

    【讨论】:

      猜你喜欢
      • 2010-12-26
      • 2019-03-05
      • 2021-11-23
      • 2011-01-21
      • 2012-06-27
      • 2014-12-25
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      相关资源
      最近更新 更多