简体   繁体   English

超过 6 个线程的性能下降

[英]Performance degrades for more than 6 Threads

I wrote a multi-threaded code in C++ on Ubuntu 19 based server.我在基于 Ubuntu 19 的服务器上的 C++ 中编写了多线程代码。 The server has 8 cores, 2 threads per core and 126 GB of available memory.该服务器有 8 个内核,每个内核 2 个线程和 126 GB 的可用 memory。 Each thread in C++ code will do some processing independently and then write to a single file. C++ 代码中的每个线程都会独立进行一些处理,然后写入单个文件。 As the file is a shared resource, it's been accessed through a mutex.由于该文件是共享资源,因此可以通过互斥锁访问它。 The processing done by each thread is computationally expensive and takes hours (8 hours in case of 6 threads) to execute before it writes the result to the shared file.每个线程完成的处理计算成本很高,并且在将结果写入共享文件之前需要数小时(如果是 6 个线程,则需要 8 小时)来执行。

If I create six threads, the execution is the fastest.如果我创建六个线程,执行是最快的。 If I create more than six threads it takes more time to execute.如果我创建超过六个线程,则需要更多时间来执行。 Here is the code, I used for creating threads, I used default parameters.这是我用于创建线程的代码,我使用了默认参数。 Is that causing some problem?这会引起一些问题吗?

pthread_create(&id[i],NULL,&myFunc, (void*)&param[i])==-1

Six Threads running of C++ code. C++ 代码的六个线程运行。 CPUs are fully utilized on which the thread is running运行线程的 CPU 被充分利用

Sixteen Threads running of C++ code. C++ 代码的十六个线程运行。 CPU is not fully utilized as the kernel process (red bars) is utilizing more percentage than the user threads (green bars). CPU 未充分利用,因为 kernel 进程(红色条)使用的百分比高于用户线程(绿色条)。 It is the worst-case scenario, but even if I run 7 threads the performance is degraded considerably and I can see some red bars这是最坏的情况,但即使我运行 7 个线程,性能也会大大降低,并且我可以看到一些红条

In another experiment, I created two processes containing 6 threads each and ran them simultaneously on the server.在另一个实验中,我创建了两个进程,每个进程包含 6 个线程,并在服务器上同时运行它们。 In total twelve threads were running within two processes.总共有 12 个线程在两个进程中运行。 It executes without any problem (No red bars).它执行没有任何问题(没有红条)。

To sum up, I am unable to understand, why more than 6 threads are causing trouble when there are resources available.总而言之,我无法理解,为什么在有可用资源的情况下超过 6 个线程会造成麻烦。 While on the other hand, two processes each containing six threads execute without any problem.而另一方面,每个包含六个线程的两个进程可以毫无问题地执行。

  • Having more threads than cores is counter-productive as you lose the CPU locality - ie the threads start constantly moving from one CPU to another CPU.线程数多于内核数会适得其反,因为您失去了 CPU 位置——即线程开始不断地从一个 CPU 移动到另一个 CPU。 Linux tries its best to conserve the locality and if the cores are available, you are sure to execute on the same one as you did before. Linux 尽最大努力保护局部性,如果内核可用,您一定会像以前一样在同一个内核上执行。 But if threads are constantly waiting one another, you lose this optimization.但是如果线程不断地互相等待,你就会失去这种优化。

  • CPU cores with 2 threads is a somewhat fraudulent marketing.具有 2 个线程的 CPU 内核是一种有点欺诈性的营销方式。 You still have only a fixed number of execution units and those 2 threads compete for them.您仍然只有固定数量的执行单元,并且这 2 个线程竞争它们。 In the very rare case where one thread is doing only add/multiply and another one is doing only load/store, you could probably get some benefit.在极少数情况下,一个线程只做加法/乘法而另一个线程只做加载/存储,你可能会得到一些好处。 But most of the time those 2 threads won't run faster then if they were waiting on each other.但是大多数情况下,如果它们彼此等待,这两个线程不会运行得更快。

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

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