选择最佳线程数用于并行处理数据 [英] Choosing optimal number of Threads for parallel processing of data

查看:444
本文介绍了选择最佳线程数用于并行处理数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个处理100万句话的任务。

Let's say I have a task with processing 1 million sentences.

对于每个句子,我都需要对它做一些事情,无论处理它们的具体顺序如何,它都会产生。

For each sentence, I need to do something with it, and it makes no matter what particular order they are processed in.

在我的Java程序中,我有一组从我的主要工作块中分割出来的一组未来,它用一个可调用来定义要在一大块句子上完成的工作单元,我正在寻找一种优化方法我分配用于处理大块句子的线程数,然后重新组合每个线程的所有结果。

In my Java program I have a set of futures partitioned from my main chunk of work with a callable that defines the unit of work to be done on a chunk of sentences, and I'm looking for a way to optimize the number of threads I allocate to work through the big block of sentences, and later recombine all the results of each thread.

在看到收益递减之前,我可以使用的最大线程数是多少?

What would be the maximum number of threads I could use that would give me optimal performance in terms of speed before I saw diminishing returns?

此外,是什么原因导致逻辑分配的线程越多,即能够一次完成的线程更多?

Also, what causes the logic that the more threads allocated, ie more being able to be done at once, to be incorrect?

推荐答案

在实践中,很难找到最佳线程数,甚至每次运行时这个数字都会有所不同该程序。因此,理论上,最佳线程数将是您机器上的核心的数量。如果您的核心是超线程(如英特尔所说),它可以在每个核心上运行2个线程。然后,在这种情况下,最佳线程数是您机器上核心数量的两倍。

In practice, it can be difficult to find the optimal number of threads and even that number will likely vary each time you run the program. So, theoretically, the optimal number of threads will be the number of cores you have on your machine. If your cores are "hyper threaded" (as Intel calls it) it can run 2 threads on each core. Then, in that case, the optimal number of threads is double the number of cores on your machine.

Also, what causes the logic that the more threads allocated, i.e. 
more being able to be done at once, to be incorrect?

分配更多线程导致同时完成更多工作的原因是错误的,因为只有1(如果核心是超线程,则可以在2个线程上运行,也可以在每个核心上一次运行。

The reason that as more threads are allocated leads to more work being done concurrently is false because only 1 (or 2 threads if the cores are "hyper threaded") can run at a single time on each core.

假设我有一个不是超线程的四核机器。在这种情况下,我可以同时运行多达4个线程。所以,我的最大吞吐量应该用4个线程来实现。假如我尝试在同一设置上运行8个线程。在这种情况下,内核会来回调度这些线程(通过上下文切换),并且阻塞一个线程以便让另一个线程运行。所以,最多可以一次运行4个线程的工作。

So assume I have a quad core machine that is not hyper threaded. In that case, i can run up to 4 threads concurrently. So, my maximum throughput should be achieved with 4 threads. Say if I try to run 8 threads on the same setup. In this case, the kernel would schedule these threads back and forth (by way of a context switch), and would block one thread in order to let another thread run. So, at most, the work of 4 threads can be run at a single time.

有关这方面的更多信息,查找上下文切换将非常有帮助使用Linux内核。这将为您提供有关此主题的所有信息。

For more information on this, it would be extremely helpful to look up "context switch" with a Linux kernel. That will provide you with all the information you ever wanted on this subject.

另外,请注意称为用户级线程和内核级别线程的线程之间存在差异。如果您进一步研究这个主题,这是一个重要的区别,但它超出了这个问题的范围。

Also, note that there is a difference between threads called "user level threads" and "kernel level threads". This is an important distinction if you research this topic further, but it is outside the scope of this question.

这篇关于选择最佳线程数用于并行处理数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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