MaxDegreeOfParallelism 决定最佳值

发布于 2024-10-25 18:41:29 字数 101 浏览 1 评论 0原文

简单的问题。

对于任何给定的算法,如何确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-11-01 18:41:29

我认为这取决于,如果您的所有任务都是“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.

裂开嘴轻声笑有多痛 2024-11-01 18:41:29

对于本地计算密集型进程,您应该尝试两个值;

  • 物理核心或处理器的数量
  • 虚拟核心的数量(物理,包括超线程)

根据我的经验,其中之一是最佳的。有时使用超线程会减慢速度,但通常会有所帮助。在 C# 中,使用 Environment.ProcessorCount 查找核心数量,包括超线程假核心。实际的物理核心更难以确定。检查其他问题。

对于必须等待资源(数据库查询、文件检索)的进程,扩展可以提供帮助。如果您必须等待某个进程 80% 的时间处于繁忙状态,那么经验法则是为其启动 5 个线程,这样任何时候都有一个线程处于繁忙状态。最大化每个流程在本地所需的 5x20%。

For local compute intensive processes you should try two values;

  • number of physical cores or processors
  • number of virtual cores (physical including hyperthreading)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文