-
Notifications
You must be signed in to change notification settings - Fork 3
/
JuliaBenchSIMD.jl
258 lines (173 loc) · 6.09 KB
/
JuliaBenchSIMD.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
function JuliaBenchSIMD( operationMode )
allFunctions = [MatrixAddition, MatrixMultiplication, ElementWiseOperations]; # only SIMD functions to run
allFunctionsString = [ "Matrix Addition", "Matrix Multiplication", "Element Wise Operations"];
if (operationMode == 1) # partial benchmark
vMatrixSize = dropdims(readdlm(joinpath("Inputs","vMatrixSizePartial.csv"), ',',Int64), dims=1);
numIterations = dropdims(readdlm(joinpath("Inputs","numIterationsPartial.csv"), ',',Int64), dims=1);
elseif (operationMode == 2) # full benchmark
vMatrixSize = dropdims(readdlm(joinpath("Inputs","vMatrixSizeFull.csv"), ',',Int64), dims=1);
numIterations = dropdims(readdlm(joinpath("Inputs","numIterationsFull.csv"), ',',Int64), dims=1);
elseif (operationMode == 0) # Test benchmark
vMatrixSize = 2;
numIterations = 1;
end
numIterations = numIterations[1]; # It is 1x1 Array -> Scalar
mRunTime = zeros(length(vMatrixSize), length(allFunctions), numIterations);
tRunTime= Array{Any}(undef,length(allFunctions)+1,length(vMatrixSize)+1)# a table containing all the information
tRunTime[1,1]="FunctionName\\MatrixSize";
for ii = 1:length(vMatrixSize)
matrixSize = vMatrixSize[ii];
mX = randn(matrixSize, matrixSize);
mY = randn(matrixSize, matrixSize);
println("Matrix Size - $matrixSize");
jj=1;
for fun in allFunctions
println("Processing $(allFunctionsString[jj]) - MatrixSize= $matrixSize");
for kk = 1:numIterations;
benchIJK =@benchmark $fun($matrixSize, $mX, $mY)
# t =@benchmarkable $fun($matrixSize, $mX, $mY);
# tune!(t)
# run(t)
mRunTime[ii, jj, kk]=median(benchIJK).time/1e3;
# println("$(mRunTime[ii, jj, kk])")
end
tRunTime[jj+1,1]="$(allFunctionsString[jj])";
tRunTime[1,ii+1]="$matrixSize";
tRunTime[jj+1,ii+1]=mean(mRunTime[ii, jj,:]);
jj+=1;
end
end
return tRunTime, mRunTime;
end
#=
function MatrixGeneration( matrixSize, mX, mY )
mA = randn(matrixSize, matrixSize);
mB = rand(matrixSize, matrixSize);
return mA;
end
=#
function MatrixAddition( matrixSize, mX, mY )
scalarA = rand();
scalarB = rand();
# mA = (scalarA .* mX) .+ (scalarB .* mY);
mA = Array{Float64}(undef, matrixSize, matrixSize);
@simd for ii = 1:(matrixSize * matrixSize)
@inbounds mA[ii] = (scalarA * mX[ii]) + (scalarB * mY[ii]);
end
return mA;
end
function MatrixMultiplication( matrixSize, mX, mY )
scalarA = rand();
scalarB = rand();
# mA = (scalarA .+ mX) * (scalarB .+ mY);
mA = Array{Float64}(undef, matrixSize, matrixSize);
@simd for ii = 1:(matrixSize * matrixSize)
@inbounds mA[ii] = (scalarA + mX[ii]) * (scalarB + mY[ii]);
end
return mA;
end
#=
function MatrixQuadraticForm( matrixSize, mX, mY )
vX = randn(matrixSize);
vB = randn(matrixSize);
sacalrC = rand();
mA = (transpose(mX * vX) * (mX * vX)) .+ (transpose(vB) * vX) .+ sacalrC;
return mA;
end
function MatrixReductions( matrixSize, mX, mY )
mA = sum(mX, dims=1) .+ minimum(mY, dims=2); #Broadcasting
return mA;
end
=#
function ElementWiseOperations( matrixSize, mX, mY )
mA = rand(matrixSize, matrixSize);
mB = 3 .+ rand(matrixSize, matrixSize);
mC = rand(matrixSize, matrixSize);
# mD = abs.(mA) .+ sin.(mA);
mD = Array{Float64}(undef, matrixSize, matrixSize);
@simd for ii = 1:(matrixSize * matrixSize)
@inbounds mD[ii] = abs(mA[ii]) + sin(mA[ii]);
end
# mE = exp.(-(mA .^ 2));
mE = Array{Float64}(undef, matrixSize, matrixSize);
@simd for ii = 1:(matrixSize * matrixSize)
@inbounds mE[ii] = exp(- (mA[ii] * mA[ii]));
end
# mF = (-mB .+ sqrt.((mB .^ 2) .- (4 .* mA .* mC))) ./ (2 .* mA);
mF = Array{Float64}(undef, matrixSize, matrixSize);
@simd for ii = 1:(matrixSize * matrixSize)
@inbounds mF[ii] = (-mB[ii] + sqrt( (mB[ii] * mB[ii]) - (4 * mA[ii] * mC[ii]) )) ./ (2 * mA[ii]);
end
mA = mD .+ mE .+ mF;
return mA;
end
#=
function MatrixExp( matrixSize, mX, mY )
mA = exp(mX);
return mA;
end
function MatrixSqrt( matrixSize, mX, mY )
mY = transpose(mX) * mX;
mA = sqrt(mY);
return mA;
end
function Svd( matrixSize, mX, mY )
F = svd(mX, full = false); # F is SVD object
mU, mS, mV = F;
return mA=0;
end
function Eig( matrixSize, mX, mY )
F = eigen(mX); # F is eigen object
mD, mV = F;
return mA=0;
end
function CholDec( matrixSize, mX, mY )
mY = transpose(mX) * mX;
mA = cholesky(mY);
return mA;
end
function MatInv( matrixSize, mX, mY )
mY = transpose(mX) * mX;
mA = inv(mY);
mB = pinv(mX);
mA = mA .+ mB;
return mA;
end
function LinearSystem( matrixSize, mX, mY )
mB = randn(matrixSize, matrixSize);
vB = randn(matrixSize);
vA = mX \ vB;
mA = mX \ mB;
mA = mA .+ vA;
return mA;
end
function LeastSquares( matrixSize, mX, mY )
mB = randn(matrixSize, matrixSize);
vB = randn(matrixSize);
vA = (transpose(mX) * mX) \ (transpose(mX) * vB);
mA = (transpose(mX) * mX) \ (transpose(mX) * mB);
mA = mA .+ vA;
return mA;
end
function CalcDistanceMatrix( matrixSize, mX, mY )
mY = randn(matrixSize, matrixSize);
mA = transpose(sum(mX .^ 2, dims=1)) .- (2 .* transpose(mX) * mY) .+ sum(mY .^ 2, dims=1);
return mA;
end
function KMeans( matrixSize, mX, mY )
# Assuming Samples are slong Columns (Rows are features)
numClusters = Int64( max( round(matrixSize / 100), 1 ) ); # % max between 1 and round(...)
numIterations = 10;
# http://stackoverflow.com/questions/36047516/julia-generating-unique-random-integer-array
mA = mX[:, randperm(matrixSize)[1:numClusters]]; #<! Cluster Centroids
for ii = 1:numIterations
vMinDist, mClusterId = findmin( transpose(sum(mA .^ 2, dims=1)) .- (2 .* transpose(mA)* mX), dims=1); #<! Is there a `~` equivalent in Julia?
vClusterId = LinearIndices( dropdims(mClusterId, dims=1) ); # to be able to access it later
for jj = 1:numClusters
mA[:, jj] = mean( mX[:, vClusterId .== jj ], dims=2 );
end
end
mA = mA[:, 1] .+ transpose(mA[:, end]);
return mA;
end
=#