-
Notifications
You must be signed in to change notification settings - Fork 7
/
MatlabMatrixBenchmark0002.m
164 lines (113 loc) · 4.09 KB
/
MatlabMatrixBenchmark0002.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function [ mRunTime ] = MatlabMatrixBenchmark0002( operationMode, vTestIdx )
% ----------------------------------------------------------------------------------------------- %
% MATLAB Matrix Operations Benchmark - Test Suite 0002
% Reference:
% 1. C.
% Remarks:
% 1. Keep 'mX' "Read Only" within the functions to match Julia (Pass by Address).
% TODO:
% 1. A
% Release Notes:
% - 1.0.003 12/02/2017 Royi Avital
% * Ability to run only some of the tests.
% - 1.0.002 10/02/2017 Royi Avital
% * Added generation of 'mX' once outside the functions.
% - 1.0.001 09/02/2017 Royi Avital
% * Added 'MatrixExpRunTime()' and 'MatrixSqrtRunTime()'.
% * Added Quadratic Matrix Form Calculation 'MatrixQuadraticFormRunTime()'.
% * Added Univariate Quadratic Function Root to 'ElementWiseOperationsRunTime()'.
% * Updated 'MatrixGenerationRunTime()' to include Uniform Random Number Generation.
% * Fixed issue with 'CalcDistanceMatrixRunTime'.
% - 1.0.000 09/02/2017 Royi Avital
% * First release version.
% ----------------------------------------------------------------------------------------------- %
FALSE = 0;
TRUE = 1;
OFF = 0;
ON = 1;
RUN_TIME_DATA_FOLDER = 'RunTimeData';
RUN_TIME_FILE_NAME = 'RunTimeMatlab0002.csv';
OPERATION_MODE_PARTIAL = 1; %<! For Testing (Runs Fast)
OPERATION_MODE_FULL = 2;
cRunTimeFunctionsBase = {@MatrixExpRunTime, @MatrixSqrtRunTime, ...
@SvdRunTime, @EigRunTime, @CholDecRunTime, @MatInvRunTime};
cFunctionStringBase = {['Matrix Exponential'], ['Matrix Square Root'], ['SVD'], ...
['Eigen Decomposition'], ['Cholesky Decomposition'], ['Matrix Inversion']};
numTests = length(cRunTimeFunctionsBase);
if(operationMode == OPERATION_MODE_PARTIAL)
vMatrixSize = csvread('vMatrixSizePartial.csv');
numIterations = csvread('numIterationsPartial.csv');
elseif(operationMode == OPERATION_MODE_FULL)
vMatrixSize = csvread('vMatrixSizeFull.csv');
numIterations = csvread('numIterationsFull.csv');
end
cRunTimeFunctions = cRunTimeFunctionsBase(vTestIdx);
cFunctionString = cFunctionStringBase(vTestIdx);
mRunTime = zeros(length(vMatrixSize), length(cRunTimeFunctions), numIterations);
hTotelRunTimer = tic();
for ii = 1:length(vMatrixSize)
matrixSize = vMatrixSize(ii);
mX = randn(matrixSize, matrixSize);
disp(['Matrix Size - ', num2str(matrixSize)]);
for jj = 1:length(cRunTimeFunctions)
disp(['Processing ', num2str(cFunctionString{jj}), ' Matrix Size ', num2str(matrixSize)]);
for kk = 1:numIterations
[mA, mRunTime(ii, jj, kk)] = cRunTimeFunctions{jj}(matrixSize, mX);
end
disp(['Finished Processing ', num2str(cFunctionString{jj})]);
end
end
totalRunTime = toc(hTotelRunTimer);
mRunTime = median(mRunTime, 3);
disp(['Finished the Benchmark in ', num2str(totalRunTime), ' [Sec]']);
runTimeFilePath = fullfile(RUN_TIME_DATA_FOLDER, RUN_TIME_FILE_NAME);
mRunTimeBase = 0;
if(exist(runTimeFilePath, 'file'))
mRunTimeBase = csvread(runTimeFilePath);
end
if(any(size(mRunTimeBase) ~= [length(vMatrixSize), numTests]))
% Previous Data has incompatible dimensions
mRunTimeBase = zeros([length(vMatrixSize), numTests]);
end
mRunTimeBase(:, vTestIdx) = mRunTime;
if(operationMode == OPERATION_MODE_FULL)
csvwrite(runTimeFilePath, mRunTimeBase);
end
end
function [ mA, runTime ] = MatrixExpRunTime( matrixSize, mX )
tic();
mA = expm(mX);
runTime = toc();
end
function [ mA, runTime ] = MatrixSqrtRunTime( matrixSize, mX )
mY = mX.' * mX;
tic();
mA = sqrtm(mY);
runTime = toc();
end
function [ mA, runTime ] = SvdRunTime( matrixSize, mX )
tic();
[mU, mS, mV] = svd(mX);
runTime = toc();
mA = mU + mS + mV;
end
function [ mA, runTime ] = EigRunTime( matrixSize, mX )
tic();
[mD, mV] = eig(mX);
runTime = toc();
mA = mD + mV;
end
function [ mA, runTime ] = CholDecRunTime( matrixSize, mX )
mY = mX.' * mX;
tic();
mA = chol(mY);
runTime = toc();
end
function [ mA, runTime ] = MatInvRunTime( matrixSize, mX )
mY = mX.' * mX;
tic();
mA = inv(mY);
mB = pinv(mX);
runTime = toc();
mA = mA + mB;
end