简体   繁体   English

找出创建多少线程是有意义的

[英]Finding out how many threads make sense to create

I have some calculation task on a large amount of the data - so it can be quite easily to parallel.我有一些关于大量数据的计算任务——所以它可以很容易地并行。 Next question is how many threads does it make sense to create.下一个问题是创建多少个线程才有意义。 Of course I can measure time for different number of thread on my machine, but what if a program will be run on different machines, so I can't really make manual measurement.当然我可以测量我机器上不同线程数的时间,但是如果一个程序将在不同的机器上运行怎么办,所以我不能真正进行手动测量。 Is just get number of threads from std::thread::hardware_concurrency() good enough, or there are some other ways?仅从std::thread::hardware_concurrency()获取线程数是否足够好,或者还有其他一些方法?

That function ( std::thread::hardware_concurrency() ) will give you the total core count, including hyperthreading. function ( std::thread::hardware_concurrency() ) 将为您提供核心总数,包括超线程。

If your program does intensive number crunching I would say using only physical cores and setting processor affinity is the best choice.如果您的程序进行密集的数字运算,我会说仅使用物理内核并设置处理器亲和力是最佳选择。

You can know the current processor topology with hwloc library which works in most platforms.您可以使用适用于大多数平台的hwloc库了解当前处理器拓扑。

You may find an comprehensible explanation (though a bit old) here .你可能会在这里找到一个易于理解的解释(虽然有点旧)。

If there is lot of I/O then you may run two threads for processor to allow one to process data while other is waiting for input, or one extra thread without affinity so it can take processor time while others are waiting for I/O, but this is a very rough estimation: better measure in your machine.如果有很多 I/O,那么您可以为处理器运行两个线程以允许一个线程处理数据,而另一个线程正在等待输入,或者一个没有亲和力的额外线程,这样它可能会占用处理器时间而其他线程正在等待 I/O,但这是一个非常粗略的估计:更好地测量您的机器。

If you can test in other processors, you may have different strategy for each processor.如果您可以在其他处理器中进行测试,您可能对每个处理器有不同的策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM