在创建线程时设置CPU亲和力 [英] Set CPU affinity when create a thread

查看:146
本文介绍了在创建线程时设置CPU亲和力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如何创建具有特定CPU亲和力的C ++ 11线程(cpu_set_t对象)?

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

如果在初始化C ++ 11线程时无法指定亲缘关系,如何在C中使用pthread_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?

我的环境是Ubuntu上的G ++.一段代码值得赞赏.

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

推荐答案

很抱歉在这里成为神话破坏者",但是设置线程亲和力非常重要,并且随着时间的流逝,随着我们所使用的系统的增加,它的重要性越来越高从本质上来说,它已成为越来越多的NUMA(非统一内存体系结构). 如今,即使是普通的双插槽服务器,也将RAM分别连接到每个插槽,并且从插槽到其自己的RAM的内存访问与相邻处理器插槽(远程RAM)的访问内存差别很大.在不久的将来,处理器进入市场,其内部内核本身就是NUMA(用于独立内核组的独立存储器控制器,等等). 我不需要在这里重复其他人的工作,只需在线查找"NUMA和线程关联性",您就可以从其他工程师的多年经验中学习.

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.

未设置线程亲和力实际上等于OS调度程序将正确处理线程亲和力的希望".让我解释: 您的系统具有一些NUMA节点(处理和内存域).您启动一个线程,该线程会用内存做一些事情,例如malloc分配一些内存,然后进行处理等.到目前为止,现代OS(至少是Linux,可能还有其他)也做得很好,默认情况下,内存是从运行线程的CPU的同一域分配的(如果可用). . 时间到了,分时操作系统(所有现代操作系统)将使线程进入睡眠状态.当线程重新进入运行状态时,它可能可以在系统的任何内核上运行(因为您没有为其设置亲和力掩码),并且系统越大,在远离先前分配或使用的内存的CPU上,它被唤醒"的机会越大.现在,您所有的内存访问都将是远程的(不确定这对您的应用程序性能意味着什么?请在线阅读有关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)

因此,总而言之,在具有不平凡的体系结构的系统上运行代码时,相似性设置界面非常重要-如今,这些体系结构正在迅速成为任何系统".一些线程运行时环境/库允许在运行时对此进行控制,而无需进行任何特定编程(例如,在Intel的KMP_AFFINITY环境变量的实现中,请参见OpenMP)-对于C ++ 11实现者,在其中包含类似的机制将是正确的选择它们的运行时库和语言选项(在此之前,如果您的代码旨在在服务器上使用,我强烈建议您在代码中实现相似性控制)

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)

这篇关于在创建线程时设置CPU亲和力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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