我试图在C++中并行化一个长时间运行的函数,并使用std :: async它只使用一个核心.
这不是函数的运行时间太小,因为我目前正在使用大约需要10分钟运行的测试数据.
根据我的逻辑,我创建NThreads值得期货(每个都占用循环的一部分而不是单个单元格,因此它是一个很好的长期运行的线程),每个都将调度异步任务.然后在创建它们之后,程序旋转锁定等待它们完成.但是它总是使用一个核心?!
这不是我看顶部并说它看起来大致像一个CPU,我的ZSH配置输出最后一个命令的CPU%,它总是正好 100%,从不高于
auto NThreads = 12;
auto BlockSize = (int)std::ceil((int)(NThreads / PathCountLength));
std::vector<std::future<std::vector<unsigned __int128>>> Futures;
for (auto I = 0; I < NThreads; ++I) {
std::cout << "HERE" << std::endl;
unsigned __int128 Min = I * BlockSize;
unsigned __int128 Max = I * BlockSize + BlockSize;
if (I == NThreads - 1)
Max = PathCountLength;
Futures.push_back(std::async(
[](unsigned __int128 WMin, unsigned __int128 Min, unsigned__int128 Max,
std::vector<unsigned __int128> ZeroChildren,
std::vector<unsigned __int128> OneChildren,
unsigned __int128 PathCountLength)
-> …Run Code Online (Sandbox Code Playgroud)