c++ std::transform - std::execution::par_unseq无效

vmdwslir  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(176)
#include <iostream>
#include <numeric>
#include <vector>
#include <cmath>
#include <execution>
#include <chrono>
using std::cout;
using std::endl;

static auto start_time = std::chrono::high_resolution_clock::now();
// Start the timer
void tick() {
    
    start_time = std::chrono::high_resolution_clock::now();
}

// Stop the timer and output the elapsed time in milliseconds
void tock() {
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
    std::cout << "Elapsed time: " << duration_ms << " ms" << std::endl;
}

double compute_p_n(double m, int n) {
    return 0.5 * (1 - std::pow(1 - 2 * m, n));
}

void do_work(auto pol)
{
    const double m = 1e-2;
    const int k = 3e8; 
    std::vector<double> input(k);
    std::vector<double> results(k);

    tick();
    std::iota(input.begin(), input.end(), 1); // initialize with 1, 2, 3, ..., k
    cout << "initializing with indices: ";
    tock(); 
    
    tick();
    std::transform(pol, input.begin(), input.end(), results.begin(),
                   [m](double n) { return compute_p_n(m, n); }); // compute p(n)
    cout << "transform: "; 
    tock();
    
}

int main() {
    
    cout << "hw concur: " << std::thread::hardware_concurrency() << endl;

    cout << "parallel: " << endl;
    do_work(std::execution::par_unseq);
    
    cout << endl << "sequential: " << endl;
    do_work(std::execution::seq);

    return 0;
}

当我运行这个时,std::execution::par_unseq没有什么区别;运行transform()需要18秒。
g计算概率cpp --std=c20 &&。/a.out
hw同意:16
平行:
使用索引初始化:经过时间:1389毫秒
变换:经过时间:5303毫秒
顺序:
使用索引初始化:经过时间:1366毫秒
变换:经过时间:6576毫秒

问题

我如何让它实际上使用我所有的核心?

ru9i0ody

ru9i0ody1#

问题是TBB
我需要安装libtbb-dev我需要将-ltbb传递给编译器。
现在我的结果是:

$ g++ compute_prob.cpp --std=c++20 -ltbb && ./a.out
hw concur: 16
parallel:
initializing with indices: Elapsed time: 1408 ms
transform: Elapsed time: 666 ms

sequential:
initializing with indices: Elapsed time: 1448 ms
transform: Elapsed time: 6156 ms

相关问题