gpt4 book ai didi

multithreading - c++ std::async:在4核上比8核更快

转载 作者:行者123 更新时间:2023-12-03 12:51:53 25 4
gpt4 key购买 nike

我有16000个工作要执行。

每个工作都是独立的。没有共享内存,没有进程间通信,没有锁或互斥锁。

我正在使用Ubuntu 16.06。 c++ 11。英特尔®酷睿™i7-8550U CPU @ 1.80GHz×8

我使用std::async在内核之间拆分作业。

如果将作业分成8个(每个内核2000个),则计算时间为145。
如果将作业分成4个(每个内核4000个),则计算时间为60。

在两种情况下,reduce之后的输出相同。

如果我在计算期间监视CPU(仅使用htop),则会按预期发生(第一种情况下以100%使用8个内核,第二种情况下仅以100%使用4个内核)。

我很困惑,为什么4个内核的处理速度会比8个内核快得多。

最佳答案

i7-8550U具有4个核心和8个线程。

有什么区别?引用How-To Geek:

Hyper-threading was Intel’s first attempt to bring parallel computation to consumer PCs. It debuted on desktop CPUs with the Pentium 4 HT back in 2002. The Pentium 4’s of the day featured just a single CPU core, so it could really only perform one task at a time—even if it was able to switch between tasks quickly enough that it seemed like multitasking. Hyper-threading attempted to make up for that.

A single physical CPU core with hyper-threading appears as two logical CPUs to an operating system. The CPU is still a single CPU, so it’s a little bit of a cheat. While the operating system sees two CPUs for each core, the actual CPU hardware only has a single set of execution resources for each core. The CPU pretends it has more cores than it does, and it uses its own logic to speed up program execution. In other words, the operating system is tricked into seeing two CPUs for each actual CPU core.

Hyper-threading allows the two logical CPU cores to share physical execution resources. This can speed things up somewhat—if one virtual CPU is stalled and waiting, the other virtual CPU can borrow its execution resources. Hyper-threading can help speed your system up, but it’s nowhere near as good as having actual additional cores.



通过将工作分配到比可用的内核更多的内核,您将付出巨大的代价。

关于multithreading - c++ std::async:在4核上比8核更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47989178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com