I want to create a C++11 thread which I want it to run on my first core. I find that pthread_setaffinity_np and sched_setaffinity can change the CPU affinity of a thread and migrate it to the specified CPU. However this affinity specification changes after the thread has run.

我想创建一个C ++ 11线程,我希望它在我的第一个核心上运行。我发现pthread_setaffinity_np和sched_setaffinity可以改变线程的CPU亲和性并将其迁移到指定的CPU。但是,在线程运行后,此关联性规范会发生更改。

How can I create a C++11 thread with specific CPU affinity (a cpu_set_t object)?

如何创建具有特定CPU关联性的C ++ 11线程(cpu_set_t对象)?

If it is impossible to specify the affinity when initializing a C++11 thread, how can I do it with pthread_t in C?

如果在初始化C ++ 11线程时无法指定关联,那么如何在C中使用pthread_t呢?

My environment is G++ on Ubuntu. A piece of code is appreciated.

我的环境是Ubuntu上的G ++。一段代码表示赞赏。

3 个解决方案

#1


18

I am sorry to be the "myth buster" here, but setting thread affinity has great importance, and it grows in importance over time as the systems we all use become more and more NUMA (Non-Uniform Memory Architecture) by nature. Even a trivial dual socket server these days has RAM connected separately to each socket, and the difference in access to memory from a socket to its own RAM to that of the neighboring processor socket (remote RAM) is substantial. In the near future, processors are hitting the market in which the internal set of cores is NUMA in itself (separate memory controllers for separate groups of cores, etc). There is no need for me to repeat the work of others here, just look for "NUMA and thread affinity" online - and you can learn from years of experience of other engineers.

我很抱歉在这里成为“神话破坏者”,但设置线程亲和力非常重要,随着时间的推移它变得越来越重要,因为我们所使用的系统本质上变得越来越多的NUMA(非统一内存架构)。如今,即使是一个简单的双插槽服务器也会将RAM单独连接到每个插槽,并且从插槽到其自己的RAM访问存储器与相邻处理器插槽(远程RAM)的存储器的差异很大。在不久的将来,处理器正在进入市场,其中内部核心集合本身就是NUMA(用于单独核心组的独立存储器控制器等)。我不需要在这里重复其他人的工作,只需在线查找“NUMA和线程亲和力” - 您可以从其他工程师的多年经验中学习。

Not setting thread affinity is effectively equal to "hoping" that the OS scheduler will handle thread affinity correctly. Let me explain: You have a system with some NUMA nodes (processing and memory domains). You start a thread, and the thread does some stuff with memory, e.g. malloc some memory and then process etc. Modern OS (at least Linux, others probably too) do a good job thus far, the memory is, by default, allocated (if available) from the same domain of the CPU where the thread is running. Come time, the time-sharing OS (all modern OS) will put the thread to sleep. When the thread is put back into running state, it may be made runnable on any of the cores in the system (as you did not set an affinity mask to it), and the larger your system is, the higher the chance it will be "woken up" on a CPU which is remote from the memory it previously allocated or used. Now, all your memory accesses would be remote (not sure what this means to your application performance? read more about remote memory access on NUMA systems online)

不设置线程亲和性实际上等于“希望”OS调度程序将正确处理线程关联。让我解释一下:你有一个带有一些NUMA节点(处理和内存域)的系统。你启动一个线程,线程用内存做一些事情,例如malloc一些内存然后进行处理等现代操作系统(至少Linux,其他可能也是)到目前为止做得很好,默认情况下,内存是从运行线程的CPU的同一域分配(如果可用) 。来吧,分时操作系统(所有现代操作系统)都会让线程进入休眠状态。当线程重新进入运行状态时,它可以在系统中的任何核心上运行(因为你没有为它设置一个亲和力掩码),系统越大,它的可能性就越高。在远离先前分配或使用的内存的CPU上“唤醒”。现在,您的所有内存访问都是远程的(不确定这对您的应用程序性能意味着什么?了解有关NUMA系统上的远程内存访问的更多信息)

So, to summarize, affinity setting interfaces are VERY important when running code on systems that have more-than-trivial architecture -- which is rapidly becoming "any system" these days. Some thread runtime environments/libs allow for control of this at runtime without any specific programming (see OpenMP, for example in Intel's implementation of KMP_AFFINITY environment variable) - and it would be the right thing for C++11 implementers to include similar mechanisms in their runtime libs and language options (and until then, if your code is aimed for use on servers, I strongly recommend that you implement affinity control in your code)

因此,总而言之,在具有更重要的体系结构的系统上运行代码时,亲和设置接口非常重要 - 这些日子正迅速成为“任何系统”。一些线程运行时环境/库允许在运行时控制它而无需任何特定的编程(参见OpenMP,例如在英特尔的KMP_AFFINITY环境变量的实现中) - 对于C ++ 11实现者来说,包含类似的机制是正确的。他们的运行时库和语言选项(在此之前,如果您的代码旨在用于服务器,我强烈建议您在代码中实现关联控制)

更多相关文章

  1. Python爬虫系列(三)多线程爬取斗图网站(皮皮虾,我们上车)
  2. 转载和积累系列 - Nodejs HTTP多线程
  3. 从json获取最大插槽并应用于控制器
  4. python 的多线程 调用input后 别的线程也不运行了
  5. python-多线程3-生产者消费者
  6. 《Python核心编程》18.多线程编程(三)
  7. Python笔记(十一):多线程
  8. 从子线程终止主线程
  9. Python学习笔记18:Python多线程编程

随机推荐

  1. 系统安装经历
  2. 简单搭建syslog-ng server记录log
  3. 美丽新世界:linux 下的惬意生活
  4. Linux记录-HDFS副本机制
  5. Linux curl 命令模拟 POST/GET 请求
  6. 检查进程是否仍在运行
  7. LTP--linux稳定性测试 linux性能测试 ltp
  8. Linux Shell编程参考大全
  9. 高级Linux工程师常用软件清单
  10. linux系统更改目录和文件的权限总结