【发布时间】:2018-12-25 16:03:56
【问题描述】:
考虑以下 c++ 代码:
#include "threadpool.hpp"
#include <chrono>
#include <list>
#include <iostream>
#include <cmath>
int loop_size;
void process(int num) {
double x = 0;
double sum = 0;
for(int i = 0; i < loop_size; ++i) {
x += 0.0001;
sum += sin(x) / cos(x) + cos(x) * cos(x);
}
}
int main(int argc, char* argv[]) {
if(argc < 3) {
std::cerr << argv[0] << " [thread_pool_size] [threads] [sleep_time]" << std::endl;
exit(0);
}
thread_pool* pool = nullptr;
int th_count = std::atoi(argv[1]);
if(th_count != 0) {
pool = new thread_pool(th_count);
}
loop_size = std::stoi(argv[3]);
int max = std::stoi(argv[2]);
auto then = std::chrono::steady_clock::now();
std::list<std::thread> ths;
if(th_count == 0) {
for(int i = 0; i < max; ++i) {
ths.emplace_back(&process, i);
}
for(std::thread& t : ths) {
t.join();
}
} else {
for(int i = 0; i < max; ++i) {
pool->enqueue(std::bind(&process, i));
}
delete pool;
}
int diff = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - then).count();
std::cerr << "Time: " << diff << '\n';
return 0;
}
而"threadpool.hpp" 是this github repo 的修改版,可以使用here
我在我的机器 (Corei7-6700) 和 88 核服务器 (2x Xeon E5-2696 v4) 上编译了上述代码。结果我无法解释。
这就是我运行代码的方式:
tp <threadpool size> <number of threads> <iterations>
同样的代码在更快的机器上运行得更慢!我的本地机器上有 8 个核心,远程服务器上有 88 个核心,这些是结果:(最后两列表示每台机器上的平均完成时间(以毫秒为单位)
+============+=========+============+=============+====================+
| Threadpool | Threads | Iterations | Corei7-6700 | 2x Xeon E5-2696 v4 |
+============+=========+============+=============+====================+
| 100 | 100000 | 1000 | 1300 | 6000 |
+------------+---------+------------+-------------+--------------------+
| 1000 | 100000 | 1000 | 1400 | 5000 |
+------------+---------+------------+-------------+--------------------+
| 10000 | 100000 | 1000 | 1470 | 3400 |
+------------+---------+------------+-------------+--------------------+
似乎拥有更多内核会使代码运行速度变慢。所以我将服务器 (taskset) 上的 CPU 亲和性降低到 8 个内核并再次运行代码:
taskset 0-7 tp <threadpool size> <number of threads> <iterations>
这是新数据:
+============+=========+============+=============+====================+
| Threadpool | Threads | Iterations | Corei7-6700 | 2x Xeon E5-2696 v4 |
+============+=========+============+=============+====================+
| 100 | 100000 | 1000 | 1300 | 900 |
+------------+---------+------------+-------------+--------------------+
| 1000 | 100000 | 1000 | 1400 | 1000 |
+------------+---------+------------+-------------+--------------------+
| 10000 | 100000 | 1000 | 1470 | 1070 |
+------------+---------+------------+-------------+--------------------+
我在 32 核 Xeon 和 22 核旧 Xeon 机器上测试了相同的代码,模式相似:内核越少,多线程代码运行速度越快。但为什么呢?
重要提示:这是为了解决我原来的问题:
Why having more and faster cores makes my multithreaded software slower?
注意事项:
- 所有机器上的操作系统和编译器都相同:debian 9.0 amd64 running kernel 4.0.9-3, 6.3.0 20170516
- 没有额外的flasg,默认优化:
g++ ./threadpool.cpp -o ./tp -lpthread
【问题讨论】:
-
未优化程序的测速有何意义?
-
您的“进程”似乎是编译器可以完全优化掉的代码。
-
哦,2x Xeon 将是 NUMA,这意味着您将在 QPI 上处理该互斥体。看看如果你
numactl -m 0 -N 0(或类似的)将你的线程限制在单个物理套接字并且它是附加内存会发生什么 -
是的,理想情况下线程数与内核数相似,否则您只是在调度程序中花费额外的时间而没有任何好处。
-
Relevant discussion @Useless 点
标签: c++ multithreading scheduling