【发布时间】:2019-10-03 14:56:15
【问题描述】:
我创建了一个程序,它从 argv 获取参数并为每个线程创建一个线程,线程关联设置为参数的 int 值。
例如
./main 3 4
将创建两个线程,第一个将在第三个 cpu 上运行,第二个线程将使用第四个 cpu。
一个线程需要一秒钟才能完成(对数组 int[10000] 进行数学运算)
当我运行time ./main 1 2 时,我看到了预期的 1 秒实时
但是当我运行time ./main 1 3 时,我看到的是 2 秒而不是 1
我认为这与 numa 节点有关,但是
time ./main 1 4 导致 1 秒实时
经过更多测试,我发现只有 1 3 和 2 4 对花费的时间是预期的两倍。用户时间也增加了一倍。
$ time ./main 1 2
real 0m1.058s
user 0m2.100s
$ time ./main 1 3
real 0m2.019s
user 0m4.016s
$ time ./main 1 4
real 0m1.090s
user 0m2.152s
$ time ./main 2 4
real 0m2.014s
user 0m4.016s
$ time ./main 2 3
real 0m1.094s
user 0m2.156s
$ time ./main 3 4
real 0m1.170s
user 0m2.316s
我正在测试的代码。我跳过了set_affinity函数
void math_ops() {
size_t len = 14800; // with this number it takes around 1s to compute on my hardware
int* abc = new int[len+1];
memset(abc, 7, len);
for(int i = 1; i < len; i++) {
for(int j = 1; j < len; j++) {
abc[i] *= abc[j];
abc[j+1] -= abc[i-1];
abc[j-1] -= abc[i+1];
}
}
}
int main(int argc, char** argv) {
std::vector<std::thread> vec(argc);
int thread_num = argc - 1;
for (int i = 0; i < thread_num; i++) {
std::thread t(math_ops);
// sets thread affinity equal to the second parameter
set_affinity(t, atoi(argv[i+1]) - 1);
vec[i] = std::move(t);
}
for (int i = 0; i < thread_num; i++) {
vec[i].join();
}
return 0;
}
有谁知道为什么 cpu 对 1,3 和 2,4 的执行时间是原来的两倍?
【问题讨论】:
-
不相关,但每当您想到
new T[size]时,您应该改用vector<T>。此外,以下memset()调用看起来很可疑。 -
@UlrichEckhardt,谢谢,注意
-
@UlrichEckhardt 但这样做会使代码运行速度变慢两倍我已将
int*和memset替换为std::vector<int> abc(len, 7);我想我应该改用int abc[14800]; -
好吧,一个在
len字节上运行,另一个在len整数上运行。而且,只有一个会再次释放内存。 -
@UlrichEckhardt 对!谢谢
标签: c++ multithreading