每个内核的最佳线程数 [英] Optimal number of threads per core

查看:23
本文介绍了每个内核的最佳线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 4 核 CPU,我想在最短的时间内运行某个进程.该进程在理想情况下是可并行化的,因此我可以在无限数量的线程上运行它的块,并且每个线程花费相同的时间.

Let's say I have a 4-core CPU, and I want to run some process in the minimum amount of time. The process is ideally parallelizable, so I can run chunks of it on an infinite number of threads and each thread takes the same amount of time.

由于我有 4 个内核,因此我不希望通过运行比内核更多的线程来提高速度,因为单个内核只能在给定时刻运行单个线程.我对硬件不太了解,所以这只是一个猜测.

Since I have 4 cores, I don't expect any speedup by running more threads than cores, since a single core is only capable of running a single thread at a given moment. I don't know much about hardware, so this is only a guess.

在比内核更多的线程上运行可并行化进程是否有好处?换句话说,如果我使用 4000 个线程而不是 4 个线程运行它,我的进程会更快、更慢或在大约相同的时间内完成吗?

推荐答案

如果您的线程不执行 I/O、同步等操作,并且没有其他任何事情在运行,那么每个内核 1 个线程将为您提供最佳性能.然而,情况很可能并非如此.添加更多线程通常会有所帮助,但在某些时候之后,它们会导致一些性能下降.

If your threads don't do I/O, synchronization, etc., and there's nothing else running, 1 thread per core will get you the best performance. However that very likely not the case. Adding more threads usually helps, but after some point, they cause some performance degradation.

不久前,我正在一台 2 四核机器上进行性能测试,该机器在 Mono 上运行 ASP.NET 应用程序,负载相当不错.我们使用了最小和最大线程数,最后我们发现对于该特定配置中的特定应用程序,最佳吞吐量介于 36 到 40 个线程之间.超出这些界限的任何事情都表现得更糟.学过的知识?如果我是你,我会使用不同数量的线程进行测试,直到找到适合您的应用程序的正确数量.

Not long ago, I was doing performance testing on a 2 quad-core machine running an ASP.NET application on Mono under a pretty decent load. We played with the minimum and maximum number of threads and in the end we found out that for that particular application in that particular configuration the best throughput was somewhere between 36 and 40 threads. Anything outside those boundaries performed worse. Lesson learned? If I were you, I would test with different number of threads until you find the right number for your application.

可以肯定的是:4k 线程需要更长的时间.这是很多上下文切换.

One thing for sure: 4k threads will take longer. That's a lot of context switches.

这篇关于每个内核的最佳线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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