m. *_* bs 6 c++ performance multithreading c++11 stdasync
我认为使用多线程处理简单和繁重的工作(例如矩阵计算)比使用单线程更好,所以我测试了以下代码:
int main()
{
constexpr int N = 100000;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> ini(0.0, 10.0);
// single-thread
{
std::vector<int> vec(N);
for(int i = 0; i < N; ++i)
{
vec[i] = ini(mt);
}
auto start = std::chrono::system_clock::now();
for(int i = 0; i < N; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
auto end = std::chrono::system_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "single : " << dur << " ms."<< std::endl;
}
// multi-threading (Th is the number of threads)
for(int Th : {1, 2, 4, 8, 16})
{
std::vector<int> vec(N);
for(int i = 0; i < N; ++i)
{
vec[i] = ini(mt);
}
auto start = std::chrono::system_clock::now();
std::vector<std::future<void>> fut(Th);
for(int t = 0; t < Th; ++t)
{
fut[t] = std::async(std::launch::async, [t, &vec, &N, &Th]{
for(int i = t*N / Th; i < (t + 1)*N / Th; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
}
for(int t = 0; t < Th; ++t)
{
fut[t].get();
}
auto end = std::chrono::system_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Th = " << Th << " : " << dur << " ms." << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行环境:
OS : Windows 10 64-bit
Build-system : Visual Studio Community 2015
CPU : Core i5 4210U
Run Code Online (Sandbox Code Playgroud)
在调试模式下构建此程序时,结果如我所料:
single : 146 ms.
Th = 1 : 140 ms.
Th = 2 : 71 ms.
Th = 4 : 64 ms.
Th = 8 : 61 ms.
Th = 16 : 68 ms.
Run Code Online (Sandbox Code Playgroud)
这表示不使用std :: async的代码与使用单线程的代码具有相同的性能,当使用4或8个线程时,我可以获得很好的性能.
但是,在发布模式下,我得到了不同的结果(N:100000 - > 100000000):
single : 54 ms.
Th = 1 : 443 ms.
Th = 2 : 285 ms.
Th = 4 : 205 ms.
Th = 8 : 206 ms.
Th = 16 : 221 ms.
Run Code Online (Sandbox Code Playgroud)
我想知道这个结果.仅针对后半代码,多线程只比单一代码具有更好的性能.但最快的是前半部分代码,它们不使用std :: async.我知道多线程的优化和开销对性能有很大影响.然而,
更新:我试图研究矢量化.我启用了/Qvec-report:1选项并获得了这样的事实:
//vectorized (when N is large)
for(int i = 0; i < N; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
//not vectorized
auto lambda = [&vec, &N]{
for(int i = 0; i < N; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
};
lambda();
//not vectorized
std::vector<std::future<void>> fut(Th);
for(int t = 0; t < Th; ++t)
{
fut[t] = std::async(std::launch::async, [t, &vec, &N, Th]{
for(int i = t*N / Th; i < (t + 1)*N / Th; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
}
Run Code Online (Sandbox Code Playgroud)
和运行时间:
single (with vectorization) : 47 ms.
single (without vectorization) : 70 ms.
Run Code Online (Sandbox Code Playgroud)
可以肯定的是,for-loop在多线程版本中没有矢量化.但是,由于任何其他原因,该版本还需要很多时间.
更新2:我在lambda(A类到B类)中重写了for循环:
//Type A (the previous one)
fut[t] = std::async(std::launch::async, [t, &vec, &N, Th]{
for(int i = t*N / Th; i < (t + 1)*N / Th; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
//Type B (the new one)
fut[t] = std::async(std::launch::async, [t, &vec, &N, Th]{
int nb = t * N / Th;
int ne = (t + 1) * N / Th;
for(int i = nb; i < ne; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
Run Code Online (Sandbox Code Playgroud)
B型效果很好.结果 :
single (vectorized) : 44 ms.
single (invectorized) : 77 ms.
--
Th = 1 (Type A) : 435 ms.
Th = 2 (Type A) : 278 ms.
Th = 4 (Type A) : 219 ms.
Th = 8 (Type A) : 212 ms.
--
Th = 1 (Type B) : 112 ms.
Th = 2 (Type B) : 74 ms.
Th = 4 (Type B) : 60 ms.
Th = 8 (Type B) : 61 ms.
Run Code Online (Sandbox Code Playgroud)
类型B的结果是可以理解的(多线程代码比单线程的矢量化代码运行得更快,并且没有矢量化代码那么快).另一方面,类型A似乎等同于类型B(仅使用临时变量),但这些显示了不同的性能.可以认为这两种类型可以生成不同的汇编代码.
更新3:我可能会发现一个减缓多线程for循环的因素.它是在条件下的分裂for.这是单线程测试:
//ver 1 (ordinary)
fut[t] = std::async(std::launch::async, [&vec, &N]{
for(int i = 0; i < N; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
//ver 2 (introducing a futile variable Q)
int Q = 1;
fut[t] = std::async(std::launch::async, [&vec, &N, Q]{
for(int i = 0; i < N / Q; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
//ver 3 (using a temporary variable)
int Q = 1;
fut[t] = std::async(std::launch::async, [&vec, &N, Q]{
int end = N / Q;
for(int i = 0; i < end; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
//ver 4 (using a raw value)
fut[t] = std::async(std::launch::async, [&vec]{
for(int i = 0; i < 100000000; ++i)
{
vec[i] = 2 * vec[i] + 3 * vec[i] - vec[i];
}
});
Run Code Online (Sandbox Code Playgroud)
和运行时间:
ver 1 : 132 ms.
ver 2 : 391 ms.
ver 3 : 47 ms.
ver 4 : 43 ms.
Run Code Online (Sandbox Code Playgroud)
第3版和第4版得到了很好的优化,而版本1并没有那么多,因为我认为编译器无法捕获N作为不变量,尽管N是constexpr.由于同样的原因,我认为ver 2非常慢.编译器不明白N和Q不会变化.因此,条件i < N / Q需要繁重的汇编代码,这会减慢for循环.
当您运行单线程时,您的单线程会vec在缓存中,因为您刚刚从 mt 创建了它。而且它会在缓存中保持良好的流式传输,因为它是所有缓存级别的唯一用户。
我认为这里没有进行太多矢量化,否则您会得到更短的时间。不过,我可能是错的,因为内存带宽是这里的关键。你看过asm吗?
任何其他线程都必须获取内存。对于您的情况来说,这本身并不是一个大问题,因为它是单个 cpu,因此 L3 是共享的,并且数据集无论如何都大于 L3。
但是,多个线程争夺 L3 是很糟糕的。我认为这是这里的主要因素。
您运行了太多线程。您应该运行与核心一样多的线程,以减少上下文切换和缓存垃圾的成本。
当 2 个硬件线程在管道(不是这里的情况)、BP(这里不是情况)和缓存利用率(这里的强情况 -> 参见#1)中有足够的“漏洞”时,HT 是有益的。
事实上,我很惊讶 >2 个线程的性能并没有下降太多——现在的 cpu 太棒了!
线程启动和任期时间不太可预测。如果您想要更多的可预测性,请不断运行线程并使用一些廉价的信号来启动它们并通知它们已完成。
编辑:具体问题的答案
这个过程只是向量的计算,那么在单线程代码中而不是在多线程代码中可以优化什么呢?
这里没有太多代码需要优化......您可以分解长循环以启用循环展开:
C = 16; // try other C values?
for(int i=nb; i<ne; i+=C) {
for(int j=0; j<C; j++)
vec[i+j] = ...; // that's === vec[i] <<= 2;
}
// need to do the remainder....
Run Code Online (Sandbox Code Playgroud)
如果编译器没有进行矢量化,您可以手动进行矢量化。先看组装。
该程序不包含任何有关互斥或原子等的内容,并且可能不会发生数据冲突。我认为多线程的开销相对较小。
真的。除了线程可以在自己的时间启动之外。特别是在 Windows 上,尤其是在 Windows 上,并且有很多 Windows 系统时。
不使用 std::async 的代码中的 CPU 利用率小于多线程代码中的 CPU 利用率。使用大部分CPU是否高效?
您总是希望在更短的时间内使用更多的 cpu%。我不确定你看到了什么,因为这里没有 IO。