ChunelFeng / CGraph

【A common used C++ DAG framework】 一个通用的、无三方依赖的、跨平台的、收录于awesome-cpp的、基于流图的并行计算框架。欢迎star & fork & 交流
http://www.chunel.cn
MIT License
1.7k stars 317 forks source link

taskflow 对比测例 #386

Open ChunelFeng opened 3 months ago

ChunelFeng commented 3 months ago
#include <taskflow/taskflow.hpp>  // the only include you need
class CStatus {
    int code = 0;
};

std::atomic<unsigned int> g_test_cnt = {0};

void demo1() {
    tf::Executor executor(8);
    tf::Taskflow taskflow("simple");
    // 并行的32路
    g_test_cnt = 0;
    for (int i = 0; i < 32; i++) {
       auto x = taskflow.emplace([] {
           g_test_cnt++;
           return CStatus();
        });
    }
    auto start_ts_ = std::chrono::high_resolution_clock::now();
    for(int i = 0; i < 500000; i++) {
        executor.run(taskflow).wait();
    }
    std::chrono::duration<double, std::milli> span = std::chrono::high_resolution_clock::now() - start_ts_;
    printf("----> [taskflow] time cost is : [%0.2lf] ms \n", span.count());
}

void demo2() {
    // 串行32个
    tf::Executor executor(1);
    tf::Taskflow taskflow;
    const int length = 32;
    std::vector<tf::Task> tasks(length);

    for (auto i = 0; i < length; ++i) {
        tasks[i] = taskflow.emplace([&] { g_test_cnt++; });
    }

    g_test_cnt = 0;
    taskflow.linearize(tasks);
    auto start_ts_ = std::chrono::high_resolution_clock::now();
    for(int i = 0; i < 1000000; i++) {
        executor.run(taskflow).wait();
    }
    std::chrono::duration<double, std::milli> span = std::chrono::high_resolution_clock::now() - start_ts_;
    printf("----> [taskflow] time cost is : [%0.2lf] ms \n",
           span.count());
}

void demo3() {
    // 简单dag图
    tf::Taskflow taskflow;
    g_test_cnt = 0;
    auto [A, B1, B2, C1, C2, D] = taskflow.emplace(
            // []() { return std::this_thread::sleep_for(std::chrono::milliseconds(1)); },
            []() { g_test_cnt++; return CStatus(); },
            []() { g_test_cnt++; return CStatus(); },
            []() { g_test_cnt++; return CStatus(); },
            []() { g_test_cnt++; return CStatus(); },
            []() { g_test_cnt++; return CStatus(); },
            []() { g_test_cnt++; return CStatus(); }
    );
    A.precede(B1, C1);
    B1.precede(B2);
    C1.precede(C2);
    D.succeed(B2, C2);
    // execute the workflow
    tf::Executor executor(2);
    auto start_ts_ = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 1000000; i++) {
        executor.run(taskflow).wait();
    }
    std::chrono::duration<double, std::milli> span = std::chrono::high_resolution_clock::now() - start_ts_;
    printf("----> [taskflow] time cost is : [%0.2lf] ms \n",
           span.count());
}

int main(){
    for (int i = 0; i < 5; i++) {
        //demo1();
        demo2();
         //demo3();
        std::cout << g_test_cnt << std::endl;
    }
    return 0;
}
ChunelFeng commented 1 month ago

void demo4() {
    tf::Taskflow taskflow;
    g_test_cnt = 0;

    auto [A1,A2,A3,A4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });
    auto [B1,B2, B3,B4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });
    auto [C1,C2,C3,C4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });
    auto [D1,D2,D3,D4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });

    B1.succeed(A1, A2, A3, A4);
    B2.succeed(A1, A2, A3, A4);
    B3.succeed(A1, A2, A3, A4);
    B4.succeed(A1, A2, A3, A4);

    C1.succeed(B1, B2, B3, B4);
    C2.succeed(B1, B2, B3, B4);
    C3.succeed(B1, B2, B3, B4);
    C4.succeed(B1, B2, B3, B4);

    D1.succeed(C1, C2, C3, C4);
    D2.succeed(C1, C2, C3, C4);
    D3.succeed(C1, C2, C3, C4);
    D4.succeed(C1, C2, C3, C4);

    tf::Executor executor(4);
    auto start_ts_ = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 100000; i++) {
        executor.run(taskflow).wait();
    }
    std::chrono::duration<double, std::milli> span = std::chrono::high_resolution_clock::now() - start_ts_;
    printf("----> [taskflow] time cost is : [%0.2lf] ms \n",
           span.count());
}