为什么并行矩阵乘法需要这么长时间?
我创建在并行计算一个复杂矩阵的情况下进行测试代码。
我正在计算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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论