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

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

问题描述

假设我有一个处理 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天全站免登陆