为什么并行矩阵乘法需要这么长时间?

发布于 2025-01-29 21:56:20 字数 3213 浏览 3 评论 0原文

我创建在并行计算一个复杂矩阵的情况下进行测试代码。

我正在计算CPU。

我观察到完成所有块大约需要3秒钟。

有人可以解释为什么需要这么长时间吗?

代码

utils.hpp

#pragma once

#include <chrono>
#include <armadillo>

namespace utils
{
    class watch : std::chrono::steady_clock {
        time_point start_ = now();
    public: auto elapsed_sec() const {return std::chrono::duration<double>(now() - start_).count();}
    };

    void op_herk(arma::cx_mat && A, arma::cx_mat & C)
    {
        using blas_int = int;
        using T = double;

        const char uplo = 'U';
        const char trans_A = 'N';
        const auto n = blas_int(C.n_cols);
        const auto k = blas_int(A.n_cols);
        const T local_alpha = T(1);
        const T local_beta  = T(0);
        const blas_int lda = n;

        arma::blas::herk<T>( &uplo, &trans_A, &n, &k, &local_alpha, A.mem, &lda, &local_beta, C.memptr(), &n);
        arma::herk_helper::inplace_conj_copy_upper_tri_to_lower_tri(C);
    }
}

threadpoll

#pragma once

#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/asio/thread_pool.hpp>

class ThreadPool {
public:
    explicit ThreadPool(size_t size = boost::thread::hardware_concurrency()) : threadPool(size)
    { }

    template<typename F>
    void addTask(F &&f)
    {
        boost::asio::post(threadPool, std::forward<F>(f));
    }
    void wait()
    {
        threadPool.wait();
    }

    ~ThreadPool()
    {
        threadPool.join();
    }
private:
    boost::asio::thread_pool threadPool;
};

main.cpp

#include <armadillo>
#include "Utils.h"
#include "ThreadPool.h"

int main() {
    ThreadPool threadPool;
    arma::cx_mat test (256, 30000 , arma::fill::randu);
    arma::vec averageTime(30, arma::fill::zeros);
    std::vector<arma::cx_mat > results(30);
    for(auto &it : results)
        it.set_size(256, 256);

    {
        for(int i = 0; i < 30; ++i)
        {
            threadPool.addTask([i = i, &results, &averageTime, test = test.submat(arma::span::all, arma::span(0, 20000)), _ = utils::watch() ]() {
                utils::op_herk(test, results[i]);
                arma::vec r = arma::sort(arma::eig_sym(results[i]), "descent");
                std::cout << _.elapsed_sec() << '\n';
                averageTime[i] = _.elapsed_sec();
            });
        }
        threadPool.wait();
        std::cout << "average " << arma::sum(averageTime)/averageTime.size() <<std::endl;
    }
    return 0;
}

参数: GCC 9.4 计算机:英特尔6个内核,12个线程; Armadillo 10.7.3 OpenBlas 0.3.17

cmake参数:set(cmake_cxx_flags_release“ $ {cmake_cxx_flags} -msse2 -o3 -mtune -mtune =本机=本地-flto”)

我的结果:

1.16084
1.16434
1.16571
1.16601
1.17055
1.17118
1.17382
1.17511
1.1767
1.17981
1.18254
1.18537
2.40071
2.40225
2.4025
2.40511
2.40545
2.40565
2.40583
2.40941
2.40972
2.40974
2.41172
2.41291
3.23446
3.23592
3.23734
3.23972
3.24305
3.24484
3.24728
average 2.14871

I create test code where I am computing in parallel one complex matrix.

I am computing on CPU.

I observed that it takes around 3 seconds to finish all the blocks.

Can someone explain why it takes so long time ?

Code

Utils.hpp

#pragma once

#include <chrono>
#include <armadillo>

namespace utils
{
    class watch : std::chrono::steady_clock {
        time_point start_ = now();
    public: auto elapsed_sec() const {return std::chrono::duration<double>(now() - start_).count();}
    };

    void op_herk(arma::cx_mat && A, arma::cx_mat & C)
    {
        using blas_int = int;
        using T = double;

        const char uplo = 'U';
        const char trans_A = 'N';
        const auto n = blas_int(C.n_cols);
        const auto k = blas_int(A.n_cols);
        const T local_alpha = T(1);
        const T local_beta  = T(0);
        const blas_int lda = n;

        arma::blas::herk<T>( &uplo, &trans_A, &n, &k, &local_alpha, A.mem, &lda, &local_beta, C.memptr(), &n);
        arma::herk_helper::inplace_conj_copy_upper_tri_to_lower_tri(C);
    }
}

ThreadPoll

#pragma once

#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/asio/thread_pool.hpp>

class ThreadPool {
public:
    explicit ThreadPool(size_t size = boost::thread::hardware_concurrency()) : threadPool(size)
    { }

    template<typename F>
    void addTask(F &&f)
    {
        boost::asio::post(threadPool, std::forward<F>(f));
    }
    void wait()
    {
        threadPool.wait();
    }

    ~ThreadPool()
    {
        threadPool.join();
    }
private:
    boost::asio::thread_pool threadPool;
};

main.cpp

#include <armadillo>
#include "Utils.h"
#include "ThreadPool.h"

int main() {
    ThreadPool threadPool;
    arma::cx_mat test (256, 30000 , arma::fill::randu);
    arma::vec averageTime(30, arma::fill::zeros);
    std::vector<arma::cx_mat > results(30);
    for(auto &it : results)
        it.set_size(256, 256);

    {
        for(int i = 0; i < 30; ++i)
        {
            threadPool.addTask([i = i, &results, &averageTime, test = test.submat(arma::span::all, arma::span(0, 20000)), _ = utils::watch() ]() {
                utils::op_herk(test, results[i]);
                arma::vec r = arma::sort(arma::eig_sym(results[i]), "descent");
                std::cout << _.elapsed_sec() << '\n';
                averageTime[i] = _.elapsed_sec();
            });
        }
        threadPool.wait();
        std::cout << "average " << arma::sum(averageTime)/averageTime.size() <<std::endl;
    }
    return 0;
}

Parameters :
gcc 9.4
computer : Intel 6 Cores , 12 threads;
armadillo 10.7.3
openblas 0.3.17

CMAKE parameters : set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -msse2 -O3 -mtune=native -flto")

My results :

1.16084
1.16434
1.16571
1.16601
1.17055
1.17118
1.17382
1.17511
1.1767
1.17981
1.18254
1.18537
2.40071
2.40225
2.4025
2.40511
2.40545
2.40565
2.40583
2.40941
2.40972
2.40974
2.41172
2.41291
3.23446
3.23592
3.23734
3.23972
3.24305
3.24484
3.24728
average 2.14871

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文