问题描述
现代的多核处理器真的在进行并行处理吗?
例如,以Intel的核心i7处理器为例.其中一些具有#of Cores: 4
和#of Threads: 8
(摘自Intel的规格页面).如果我编写具有多个执行线程的程序(例如,用Java或C语言编写),是否会同时真正处理它们?我的老师说:多核处理器并非总是如此",但并没有提供太多细节.
Are the modern multi-core processors really doing parallel processing?
Like, take for example, Intel's core i7 processors. some of them has #of Cores: 4
and #of Threads: 8
(taken from Intel's specifications pages). If I to write a program (say in Java or C) that has multiple threads of execution, will they really be processed concurrently? My instructor said that "it is not always the case with multi-core processors", but didn't give too much details.
为什么英特尔必须同时指定#of核心和#of线程?线程不只是一个描述与程序相关的抽象的术语的术语,而不是真正的硬件核心"吗? (每个线程都在不同的内核上运行").
And why do Intel have to specify both #of Cores and #of Threads? Isn't thread just a term that describe a program-related abstraction, unlike "cores" which are actual hardware? ("Every thread runs on different core").
推荐答案
是的,现代多核处理器确实在适当的任务上进行了并行处理,并且提供了适当的软件编写以允许这样做.我亲自测试了具有可配置线程数的多线程软件,并观察吞吐量与线程数成正比,直到该数量等于内核数为止:在四核处理器上,一个线程以一定数量完成了一项任务时间,四个线程在相同的时间内完成了四个任务,吞吐量提高了三倍.除了内核数量之外,其他线程没有进一步增加,而实际上却略有减少:八个线程上的八个任务完成时间比四个线程上的四个任务多两倍.仅使用四个线程并使它们每个都连续处理两个任务会更快.
Yes, modern multicore processors are really doing parallel processing, on appropriate tasks and provided the software is properly written to permit it. I have personally tested multithreaded software with a configurable number of threads, and watched the throughput increase proportional to the number of threads until that number was equal to the number of cores: on a four core processor, one thread completed one task in a certain amount of time, and four threads completed four tasks in the same amount of time, a quadrupling of throughput. With additional threads beyond the number of cores, there was no further increase, and in fact a slight decrease: eight tasks on eight threads took more than twice the amount of time to complete than did four tasks on four threads. It was faster just to use four threads and have them each process two tasks in a row.
但是,也许这就是您的教授所指的,仅具有多个线程并不能保证您将利用多个内核.例如,线程可能受到共享资源争用的限制,也就是说,它们都需要访问一块内存.他们还可能争用共享的I/O资源,例如磁盘访问或网络访问.在某些情况下,一次运行多个线程可能不会改善性能,并且实际上可能会降低总吞吐量.
However, and this may be what your professor is referring to, just having multiple threads does not guarantee that you will be taking advantage of multiple cores. For example, the threads may be limited by contention on a shared resource - say, a piece of memory they all needed to access. They could also be contending for a shared I/O resource, like disk access or network access. In some of these cases, running more than one thread at a time may not improve things, and may actually decrease total throughput.
最后,由于英特尔的超线程"体系结构,线程数"和内核数"的数量有所不同,据说该体系结构允许每个内核一次运行两个线程.以我的经验,超线程在现实世界中不是很有用:例如,在上述测试中,内核是超线程的Intel内核,但是当我运行的线程数等于物理线程数时,仍会出现峰值吞吐量.内核,当我运行的线程数量等于英特尔声称的线程容量时,该内核就会掉线.我发现最好注意内核数量,而忽略英特尔的线程数量"主张.
Finally, the "number of threads" and "number of cores" numbers are different because of Intel's "hyperthreading" architecture, which supposedly allows each core to run two threads at a time. In my experience, hyperthreading is not very useful in the real world: for example, in the test I described above, the cores were hyperthreaded Intel cores, but the peak throughput still occurred when I ran a number of threads equal to the number of physical cores, and dropped when I ran a number of threads equal to Intel's claimed thread capacity. I find it best to pay attention to the number of cores and ignore Intel's "number of threads" claim.
这篇关于多线程-多核处理器真的在进行并行处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!