MaxDegreeOfParallelism 决定最佳值
简单的问题。
对于任何给定的算法,如何确定 MaxDegreeOfParallelism 的最佳值是多少?需要考虑哪些因素以及如何权衡?
Simple question.
How do you decide what the optimal value for MaxDegreeOfParallelism is for any given algorithm? What are the factors to consider and what are the trade-offs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这取决于,如果您的所有任务都是“CPU 限制”,那么它可能等于您机器中的 CPU 数量。不过,如果您的任务是“IO 密集型”,您可以开始增加数量。
当 CPU 必须从一个线程切换到另一个线程(上下文切换)时,会产生一定的成本,因此如果使用太多线程并且 CPU 一直在切换,则会降低性能。另一方面,如果您对该参数限制太多,并且操作是长时间的“IO 绑定”操作,并且 CPU 空闲很多时间等待这些任务完成......您并没有充分利用您的资源机器的资源(这就是多线程的含义)
我认为这取决于@Amdahls定律所指出的每种算法,并且没有可以遵循的主要经验法则,您必须对其进行规划和调整:D
干杯。
I think it depends, if all your tasks are "CPU bound" it would be probably equal to the number of CPUs in your machine. Nevertheless if you tasks are "IO bound" you can to start to increase the number.
When the CPU has to switch from one thread to other (context switch) it has a cost, so if you use too much threads and the CPU is switching all the time, you decrease the performance. In the other hand, if you limit that parameter too much, and the operations are long "IO bound" operations, and the CPU is idle a lot of time waiting for those tasks to complete... you are not doing the most with your machine's resources (and that is what multithreading is about)
I think it depends on each algorithm as @Amdahls's Law has pointed out, and there is not a master rule of thumb you can follow, you will have to plan it and tun it :D
Cheers.
对于本地计算密集型进程,您应该尝试两个值;
根据我的经验,其中之一是最佳的。有时使用超线程会减慢速度,但通常会有所帮助。在 C# 中,使用 Environment.ProcessorCount 查找核心数量,包括超线程假核心。实际的物理核心更难以确定。检查其他问题。
对于必须等待资源(数据库查询、文件检索)的进程,扩展可以提供帮助。如果您必须等待某个进程 80% 的时间处于繁忙状态,那么经验法则是为其启动 5 个线程,这样任何时候都有一个线程处于繁忙状态。最大化每个流程在本地所需的 5x20%。
For local compute intensive processes you should try two values;
One of these is optimal in my experience. Sometimes using hyperthreading slows down, usually it helps. In C# use Environment.ProcessorCount to find the number of cores including hyperthreading fake cores. Actual physical cores is more difficult to determine. Check other questions for this.
For processes that have to wait for resources (db queries, file retrieval) scaling up can help. If you have to wait 80% of the time a process is busy the rule of thumb is to start 5 threads for it so one thread will be busy at any time. Maximizing the 5x20% each process requires locally.