c++矩陣計算性能對比:Eigen和GPU解讀
更新時間:2022年12月15日 14:55:01 作者:guotianqing
這篇文章主要介紹了c++矩陣計算性能對比:Eigen和GPU解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
生成隨機矩陣
生成隨機矩陣有多種方式,直接了當(dāng)?shù)姆绞绞鞘褂蔑@式循環(huán)的方式為矩陣的每個元素賦隨機值。
#include <iostream>
#include <random>
using namespace std;
// 生成隨機數(shù)
double GenerateRandomRealValue()
{
? ? std::random_device rd;
? ? std::default_random_engine eng(rd());
? ? std::uniform_real_distribution<double> distr(1, 10);
? ? return distr(eng);
}
int main()
{
?? ??? ?// 3d矩陣
? ? double a[3][3];
? ? for (int i = 0; i < 3; ++i) {
? ? ? ? for (int j = 0; ?j < 3; ++j) {
? ? ? ? ? ? a[i][j] = GenerateRandomRealValue();
? ? ? ? }
? ? }
? ? return 0;
}另一種方式是使用Eigen庫,它提供了矩陣運算的庫。
生成隨機矩陣:
#include "Eigen/Dense"
#include <functional>
using namespace std;
using namespace Eigen;
MatrixXd Generate2DMatrixByEigen()
{
?? ??? ?// 直接使用內(nèi)置的Random,產(chǎn)生均勻分布隨機矩陣
? ? MatrixXd m = MatrixXd::Random(3,3);
? ??
? ? // 也可以調(diào)用自定義的隨機數(shù)生成函數(shù)填充數(shù)據(jù)
? ? // MatrixXd m = MatrixXd::Zero(3,3).unaryExpr(std::bind(GenerateRandomRealValue));
? ? return m;
}計算矩陣點積
使用顯式循環(huán)計算
直接上代碼:
void CalcMatrixDotForLoop(const vector<vector<double>>& a, const vector<vector<double>>& b)
{
? ? std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
? ? if (a[0].size() != b.size()) {
? ? ? ? cout << "error:" << a.size() << "," << b[0].size() << endl;
? ? ? ? return;
? ? }
? ? vector<vector<double>> c;
? ? vector<double> c_row(b[0].size());
? ? for (int i = 0; i < a.size(); ++i) {
? ? ? ? for (int j = 0; j < b[0].size(); ++j) {
? ? ? ? ? ? for (int k = 0; k < b.size(); ++k) {
? ? ? ? ? ? ? ? c_row[j] += a[i][k] * b[k][j];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? c.emplace_back(c_row);
? ? }
? ? std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
? ? std::chrono::duration<double, std::milli> time_span = t2 - t1;
? ? std::cout << "Loop takes " << time_span.count() << " ms\n";
? ? // cout << "matrix c:\n";
? ? // for (int i = 0; i < c.size(); ++i) {
? ? // ? ? for (int j = 0; j < c[0].size(); ++j) {
? ? // ? ? ? ? cout << c[i][j] << ",";
? ? // ? ? }
? ? // ? ? cout << endl;
? ? // }
}使用Eigen庫
代碼:
void ModeEigen(const int a_row, const int a_col, const int b_row, const int b_col)
{
? ? std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
? ? auto c = a * b;
? ? std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
? ? std::chrono::duration<double, std::milli> time_span = t2 - t1;
? ? std::cout << "Eigen takes " << time_span.count() << " ms\n";
? ? // cout << "matrix c:\n" << c << endl;
}使用GPU
代碼片斷:
auto t_begin = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now(); cudaMalloc((void**)&da,size); cudaMalloc((void**)&db,size); cudaMalloc((void**)&dc,size); t2 = std::chrono::high_resolution_clock::now(); time_span = t2 - t1; std::cout << "GPU malloc takes " << time_span.count() << " ms\n"; t1 = std::chrono::high_resolution_clock::now(); cudaMemcpy(da,a,size,cudaMemcpyHostToDevice); cudaMemcpy(db,b,size,cudaMemcpyHostToDevice); t2 = std::chrono::high_resolution_clock::now(); time_span = t2 - t1; std::cout << "cudaMemcpy takes " << time_span.count() << " ms\n"; t1 = std::chrono::high_resolution_clock::now(); dim3 dg(32,32); dim3 dbs((n+dg.x-1)/dg.x,(n+dg.y-1)/dg.y); mextix<<<dbs,dg>>>(da,db,dc,n); t2 = std::chrono::high_resolution_clock::now(); time_span = t2 - t1; std::cout << "gpu takes " << time_span.count() << " ms\n"; t1 = std::chrono::high_resolution_clock::now(); cudaMemcpy(c,dc,size,cudaMemcpyDeviceToHost); t2 = std::chrono::high_resolution_clock::now(); time_span = t2 - t1; std::cout << "cudaMemcpy back takes " << time_span.count() << " ms\n"; cudaFree(da); cudaFree(db); cudaFree(dc); auto t_end = std::chrono::high_resolution_clock::now(); time_span = t_end - t_begin; std::cout << "GPU total takes " << time_span.count() << " ms\n";
結(jié)果分析
經(jīng)過測試,得到以下結(jié)論:
- 對于CPU上矩陣運算來說,使用Eigen遠遠優(yōu)于顯式循環(huán)(我只使用了單線程,你當(dāng)然可以嘗試多線程,但程度復(fù)雜度會明顯上升)
- 對于小規(guī)模矩陣來說,Eigen庫要快于GPU(數(shù)據(jù)在host和device之間的拷貝消耗了大量的時間)
- 對于較大規(guī)模矩陣來說,GPU的優(yōu)勢才顯現(xiàn)出來(數(shù)據(jù)運算時間超過了拷貝耗時,運算量越大,GPU并行的優(yōu)勢也越明顯)
總之:
- 絕對避免使用顯式循環(huán),使用Eigen庫
- 對于一般的應(yīng)用來說,使用Eigen庫足夠應(yīng)付大多數(shù)場景,畢竟CPU機器要比GPU機器廉價且普遍
- 對于涉及大量的矩陣運算,包括機器學(xué)習(xí)等,GPU才是真正的用武之地
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
visual studio2019的安裝以及使用圖文步驟詳解
這篇文章主要介紹了visual studio2019的安裝以及使用圖文步驟詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
一文詳解C++子類函數(shù)為什么不能重載父類函數(shù)
這篇文章主要介紹了一文詳解C++子類函數(shù)為什么不能重載父類函數(shù),文章圍繞主題展開詳細的內(nèi)容戒殺,具有一定的參考價值,需要的朋友可以參考一下2022-09-09
動態(tài)數(shù)組C++實現(xiàn)方法(分享)
下面小編就為大家?guī)硪黄獎討B(tài)數(shù)組C++實現(xiàn)方法(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

