Last active
November 2, 2017 08:46
-
-
Save eiichiroi/a6b57ac60ce79fe44eb2b77e4d8eb683 to your computer and use it in GitHub Desktop.
行列積のループ交換法の簡易ベンチマーク
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <random> | |
#include <cassert> | |
#include <cstdlib> | |
#include <sys/time.h> | |
struct Host { | |
Host(int num) | |
: num(num), A(num*num), B(num*num), C(num*num) { | |
Initialize(); | |
} | |
void Initialize() { | |
std::random_device seed_gen; | |
std::mt19937 engine(seed_gen()); | |
std::uniform_real_distribution<float> dist(-1.0, 1.0); | |
InitializeMatrix(A, engine, dist); | |
InitializeMatrix(B, engine, dist); | |
} | |
void InitializeMatrix(std::vector<float>& m, | |
std::mt19937& engine, | |
std::uniform_real_distribution<float>& dist) { | |
for (int i = 0; i < num; ++i) { | |
for (int j = 0; j < num; ++j) { | |
m[i*num + j] = dist(engine); | |
} | |
} | |
} | |
int num; | |
std::vector<float> A, B, C; | |
}; | |
double to_double(const timeval& t) { | |
return t.tv_sec + t.tv_usec * 1e-6; | |
} | |
double timeval_diff(const timeval& start, const timeval& end) { | |
return to_double(end) - to_double(start); | |
} | |
#define MMROW(X, dj, i, j) ((X)[(i)*(dj)+(j)]) | |
double MultiplyMatrixCLoopRow(Host* hst) { | |
const int num = hst->num; | |
struct timeval st, et; | |
// C Loop (row) 部分抜粋 | |
// hst->A , hst->B , hst->C ともに num×num×sizeof(float) の メモリ領域 | |
gettimeofday(&st,NULL); | |
for(int i=0;i<num;i++) | |
{ | |
for(int j=0;j<num;j++) | |
{ | |
float cc = 0.0; | |
for(int k=0;k<num;k++) | |
{ | |
cc += MMROW(hst->A,num,i,k) + MMROW(hst->B,num,k,j); | |
} | |
MMROW(hst->C,num,i,j) += cc; | |
} | |
} | |
gettimeofday(&et,NULL); | |
return timeval_diff(st, et); | |
} | |
double MultiplyMatrixCLoopSwap(Host* hst) { | |
const int num = hst->num; | |
struct timeval st, et; | |
// C Loop (row) 部分抜粋 | |
// hst->A , hst->B , hst->C ともに num×num×sizeof(float) の メモリ領域 | |
gettimeofday(&st,NULL); | |
for(int i=0;i<num;i++) | |
{ | |
for(int j=0;j<num;j++) | |
{ | |
MMROW(hst->C,num,i,j) = 0; | |
} | |
for(int k=0;k<num;k++) | |
{ | |
for(int j=0;j<num;j++) | |
{ | |
MMROW(hst->C,num,i,j) += MMROW(hst->A,num,i,k) + MMROW(hst->B,num,k,j); | |
} | |
} | |
} | |
gettimeofday(&et,NULL); | |
return timeval_diff(st, et); | |
} | |
#undef MMROW | |
int main(int argc, char* argv[]) { | |
for (int num = 100; num <= 2000; num += 100) { | |
double elapsed_times[2]; | |
{ | |
Host hst(num); | |
elapsed_times[0] = MultiplyMatrixCLoopRow(&hst); | |
} | |
{ | |
Host hst(num); | |
elapsed_times[1] = MultiplyMatrixCLoopSwap(&hst); | |
} | |
std::cout << num << "\t" | |
<< std::fixed << std::setprecision(5) | |
<< elapsed_times[0] << "\t" | |
<< elapsed_times[1] << std::endl; | |
} | |
return 0; | |
} |
Author
eiichiroi
commented
Nov 2, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment