-
Notifications
You must be signed in to change notification settings - Fork 268
/
kernel.cs
228 lines (187 loc) · 6.45 KB
/
kernel.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/// <license>
/// This is a port of the SciMark2a Java Benchmark to C# by
/// Chris Re (cmr28@cornell.edu) and Werner Vogels (vogels@cs.cornell.edu)
///
/// For details on the original authors see http://math.nist.gov/scimark2
///
/// This software is likely to burn your processor, bitflip your memory chips
/// anihilate your screen and corrupt all your disks, so you it at your
/// own risk.
/// </license>
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace SciMark2
{
[BenchmarkCategory(Categories.Runtime, Categories.JIT, Categories.SciMark)]
public class kernel
{
double[] inputFFT;
double[][] inputSOR;
private double[] inputSparseMultX;
private double[] inputSparseMultY;
private double[] inputSparseMultVal;
private int[] inputSparseMultCol;
private int[] inputSparseMultRow;
private double[][] inputLU;
private int[] inputLUPivot;
private double[][] inputLUA;
[GlobalSetup(Target = nameof(benchFFT))]
public void SetupFFT()
{
Random R = new SciMark2.Random(Constants.RANDOM_SEED);
int N = Constants.FFT_SIZE;
inputFFT = RandomVector(2 * N, R);
}
[Benchmark]
public void benchFFT()
{
long Iterations = 20000;
innerFFT(inputFFT, Iterations);
}
private static void innerFFT(double[] x, long Iterations)
{
for (int i = 0; i < Iterations; i++)
{
FFT.transform(x); // forward transform
FFT.inverse(x); // backward transform
}
}
[GlobalSetup(Target = nameof(benchSOR))]
public void SetupSOR()
{
Random R = new SciMark2.Random(Constants.RANDOM_SEED);
int N = Constants.SOR_SIZE;
inputSOR = RandomMatrix(N, N, R);
}
[Benchmark]
public void benchSOR()
{
int Iterations = 20000;
SOR.execute(1.25, inputSOR, Iterations);
}
[Benchmark]
public void benchMonteCarlo()
{
int Iterations = 40000000;
MonteCarlo.integrate(Iterations);
}
[GlobalSetup(Target = nameof(benchSparseMult))]
public void SetupSparseMult()
{
Random R = new SciMark2.Random(Constants.RANDOM_SEED);
int N = Constants.SPARSE_SIZE_M;
int nz = Constants.SPARSE_SIZE_nz;
inputSparseMultX = RandomVector(N, R);
inputSparseMultY = new double[N];
int nr = nz / N; // average number of nonzeros per row
int anz = nr * N; // _actual_ number of nonzeros
inputSparseMultVal = RandomVector(anz, R);
inputSparseMultCol = new int[anz];
inputSparseMultRow = new int[N + 1];
inputSparseMultRow[0] = 0;
for (int r = 0; r < N; r++)
{
// initialize elements for row r
int rowr = inputSparseMultRow[r];
inputSparseMultRow[r + 1] = rowr + nr;
int step = r / nr;
if (step < 1)
step = 1;
// take at least unit steps
for (int i = 0; i < nr; i++)
inputSparseMultCol[rowr + i] = i * step;
}
}
[Benchmark]
public void benchSparseMult()
{
int Iterations = 100000;
SparseCompRow.matmult(inputSparseMultY, inputSparseMultVal, inputSparseMultRow, inputSparseMultCol, inputSparseMultX, Iterations);
}
[GlobalSetup(Target = nameof(benchmarkLU))]
public void SetupLU()
{
Random R = new SciMark2.Random(Constants.RANDOM_SEED);
int N = Constants.LU_SIZE;
inputLUA = RandomMatrix(N, N, R);
inputLU = new double[N][];
for (int i = 0; i < N; i++)
{
inputLU[i] = new double[N];
}
inputLUPivot = new int[N];
}
[Benchmark]
public void benchmarkLU()
{
int Iterations = 2000;
for (int i = 0; i < Iterations; i++)
{
CopyMatrix(inputLU, inputLUA);
LU.factor(inputLU, inputLUPivot);
}
}
private static void CopyMatrix(double[][] B, double[][] A)
{
int M = A.Length;
int N = A[0].Length;
int remainder = N & 3; // N mod 4;
for (int i = 0; i < M; i++)
{
double[] Bi = B[i];
double[] Ai = A[i];
for (int j = 0; j < remainder; j++)
Bi[j] = Ai[j];
for (int j = remainder; j < N; j += 4)
{
Bi[j] = Ai[j];
Bi[j + 1] = Ai[j + 1];
Bi[j + 2] = Ai[j + 2];
Bi[j + 3] = Ai[j + 3];
}
}
}
private static double[][] RandomMatrix(int M, int N, Random R)
{
double[][] A = new double[M][];
for (int i = 0; i < M; i++)
{
A[i] = new double[N];
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
A[i][j] = R.nextDouble();
return A;
}
private static double[] RandomVector(int N, Random R)
{
double[] A = new double[N];
for (int i = 0; i < N; i++)
A[i] = R.nextDouble();
return A;
}
private static double[] matvec(double[][] A, double[] x)
{
int N = x.Length;
double[] y = new double[N];
matvec(A, x, y);
return y;
}
private static void matvec(double[][] A, double[] x, double[] y)
{
int M = A.Length;
int N = A[0].Length;
for (int i = 0; i < M; i++)
{
double sum = 0.0;
double[] Ai = A[i];
for (int j = 0; j < N; j++)
sum += Ai[j] * x[j];
y[i] = sum;
}
}
}
}