-
Notifications
You must be signed in to change notification settings - Fork 7
/
JuliaMatrixBenchmark0001.jl
144 lines (109 loc) · 4.24 KB
/
JuliaMatrixBenchmark0001.jl
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
# ----------------------------------------------------------------------------------------------- #
# Julia Matrix Operations Benchmark - Test Suite 0001
# Reference:
# 1. C.
# Remarks:
# 1. W.
# TODO:
# 1. A
# Release Notes:
# - 2.0.001 14/10/2019 Royi Avital
# * Fixed missing `.` before `-` in `mE = exp.(-(mA .^ 2));`.
# - 2.0.000 13/10/2019 Royi Avital
# * Update for compatibility for Julia 1.2.
# - 1.0.004 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' and 'mY' once outside the functions.
# * Fixed issue with the Quadratic Form.
# * Optimized creation of scalars and vectors.
# - 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.
# ----------------------------------------------------------------------------------------------- #
function JuliaMatrixBenchmark0001( vTestIdx = [1, 2, 3, 4, 5, 6], vMatrixSize = [2, 5, 10, 20, 50, 100, 200, 300, 500, 750, 1000, 2000, 3000, 4000], numIterations = 7 )
cRunTimeFunctionsBase = [MatrixGenerationRunTime, MatrixAdditionRunTime, MatrixMultiplicationRunTime,
MatrixQuadraticFormRunTime, MatrixReductionsRunTime, ElementWiseOperationsRunTime];
cFunctionStringBase = ["Matrix Generation", "Matrix Addition", "Matrix Multiplication", "Matrix Quadratic Form",
"Matrix Reductions", "Element Wise Operations"];
numTests = length(cRunTimeFunctionsBase);
cRunTimeFunctions = cRunTimeFunctionsBase[vTestIdx];
cFunctionString = cFunctionStringBase[vTestIdx];
mRunTime = zeros(length(vMatrixSize), length(cRunTimeFunctions), numIterations);
startTime = time();
for ii = 1:length(vMatrixSize)
matrixSize = vMatrixSize[ii];
mX = randn(matrixSize, matrixSize);
mY = randn(matrixSize, matrixSize);
println("Matrix Size - $matrixSize");
for jj = 1:length(cRunTimeFunctions)
println("Processing $(cFunctionString[jj]) Matrix Size $matrixSize");
for kk = 1:numIterations
mA, mRunTime[ii, jj, kk] = cRunTimeFunctions[jj](matrixSize, mX, mY);
end
println("Finished Processing $(cFunctionString[jj])");
end
end
endTime = time();
totalRunTime = endTime - startTime;
mRunTime = median(mRunTime, dims = 3);
mRunTime = dropdims(mRunTime, dims = 3);
println("Finished the Benchmark in $totalRunTime [Sec]");
return mRunTime;
end
function MatrixGenerationRunTime( matrixSize, mX, mY )
runTime = @elapsed begin
mA = randn(matrixSize, matrixSize);
mB = rand(matrixSize, matrixSize);
end
mA = mA .+ mB;
return mA, runTime;
end
function MatrixAdditionRunTime( matrixSize, mX, mY )
scalarA = rand();
scalarB = rand();
runTime = @elapsed begin
mA = (scalarA .* mX) .+ (scalarB .* mY);
end
return mA, runTime;
end
function MatrixMultiplicationRunTime( matrixSize, mX, mY )
scalarA = rand();
scalarB = rand();
runTime = @elapsed begin
mA = (scalarA .+ mX) * (scalarB .+ mY);
end
return mA, runTime;
end
function MatrixQuadraticFormRunTime( matrixSize, mX, mY )
vX = randn(matrixSize);
vB = randn(matrixSize);
sacalrC = rand();
runTime = @elapsed begin
mA = ((mX * vX)' * (mX * vX)) .+ (vB' * vX) .+ sacalrC;
end
return mA, runTime;
end
function MatrixReductionsRunTime( matrixSize, mX, mY )
runTime = @elapsed begin
mA = sum(mX, dims = 1) .+ minimum(mY, dims = 2); #Broadcasting
end
return mA, runTime;
end
function ElementWiseOperationsRunTime( matrixSize, mX, mY )
mA = rand(matrixSize, matrixSize);
mB = 3 .+ rand(matrixSize, matrixSize);
mC = rand(matrixSize, matrixSize);
runTime = @elapsed begin
mD = abs.(mA) .+ sin.(mB);
mE = exp.(.-(mA .^ 2));
mF = (-mB .+ sqrt.((mB .^ 2) .- (4 .* mA .* mC))) ./ (2 .* mA);
end
mA = mD .+ mE .+ mF;
return mA, runTime;
end