17 public static int Benchmark(
int nrCandidates,
int nrSpectra,
int topN,
int batchSize, Random r)
20 var candidateValues =
new int[nrCandidates * 100];
21 var candidatesIdx =
new int[nrCandidates];
22 var csrRowoffsets =
new int[nrCandidates + 1];
23 var csrIdx =
new int[nrCandidates * 100];
25 for (
int i = 0; i < candidateValues.Length; i += 100)
27 candidatesIdx[currentIdx] = i;
28 csrRowoffsets[currentIdx] = i;
29 var tmpValues =
new int[100];
30 for (
int j = 0; j < tmpValues.Length; j++)
32 var val = r.Next(ENCODING_SIZE);
33 while (Array.Exists(tmpValues, x => x == val))
35 val = r.Next(ENCODING_SIZE);
39 Array.Sort(tmpValues);
40 for (
int j = 0; j < tmpValues.Length; j++)
42 candidateValues[i + j] = tmpValues[j];
43 csrIdx[i + j] = tmpValues[j];
46 if (currentIdx % 5000 == 0)
48 Console.WriteLine($
"Generated {currentIdx} candidates...");
52 csrRowoffsets[currentIdx++] = nrCandidates * 100;
55 var spectraValues =
new int[nrSpectra * 500];
56 var spectraIdx =
new int[nrSpectra];
58 for (
int i = 0; i < spectraValues.Length; i += 500)
60 spectraIdx[currentIdx] = i;
61 var tmpValues =
new int[500];
62 for (
int j = 0; j < tmpValues.Length; j++)
64 var val = r.Next(ENCODING_SIZE);
65 while (Array.Exists(tmpValues, x => x == val))
67 val = r.Next(ENCODING_SIZE);
71 Array.Sort(tmpValues);
72 for (
int j = 0; j < tmpValues.Length; j++)
74 spectraValues[i + j] = tmpValues[j];
80 var cValuesLoc = GCHandle.Alloc(candidateValues, GCHandleType.Pinned);
81 var cIdxLoc = GCHandle.Alloc(candidatesIdx, GCHandleType.Pinned);
82 var csrRowoffsetsLoc = GCHandle.Alloc(csrRowoffsets, GCHandleType.Pinned);
83 var csrIdxLoc = GCHandle.Alloc(csrIdx, GCHandleType.Pinned);
84 var sValuesLoc = GCHandle.Alloc(spectraValues, GCHandleType.Pinned);
85 var sIdxLoc = GCHandle.Alloc(spectraIdx, GCHandleType.Pinned);
86 var resultArrayEigen =
new int[spectraIdx.Length * topN];
87 var resultArrayEigenInt =
new int[spectraIdx.Length * topN];
88 var resultArrayEigen2 =
new int[spectraIdx.Length * topN];
89 var resultArrayEigen2Int =
new int[spectraIdx.Length * topN];
90 var resultArrayEigenB =
new int[spectraIdx.Length * topN];
91 var resultArrayEigenBInt =
new int[spectraIdx.Length * topN];
92 var resultArrayEigenB2 =
new int[spectraIdx.Length * topN];
93 var resultArrayEigenB2Int =
new int[spectraIdx.Length * topN];
94 var resultArrayCuda =
new int[spectraIdx.Length * topN];
95 var resultArrayCudaB =
new int[spectraIdx.Length * topN];
96 var resultArrayCudaB2 =
new int[spectraIdx.Length * topN];
100 IntPtr cValuesPtr = cValuesLoc.AddrOfPinnedObject();
101 IntPtr cIdxPtr = cIdxLoc.AddrOfPinnedObject();
102 IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
103 IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
104 IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
105 IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();
107 var sw1 = Stopwatch.StartNew();
109 IntPtr resultEigen = findTopCandidates(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
110 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
111 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);
113 Marshal.Copy(resultEigen, resultArrayEigen, 0, spectraIdx.Length * topN);
115 memStat = releaseMemory(resultEigen);
119 Console.WriteLine(
"Time for candidate search Eigen SpM*SpV:");
120 Console.WriteLine(sw1.Elapsed.TotalSeconds.ToString());
122 var sw1Int = Stopwatch.StartNew();
124 IntPtr resultEigenInt = findTopCandidatesInt(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
125 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
126 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);
128 Marshal.Copy(resultEigenInt, resultArrayEigenInt, 0, spectraIdx.Length * topN);
130 memStat = releaseMemory(resultEigenInt);
134 Console.WriteLine(
"Time for candidate search Eigen SpM*SpV (int):");
135 Console.WriteLine(sw1Int.Elapsed.TotalSeconds.ToString());
137 var sw2 = Stopwatch.StartNew();
139 IntPtr resultEigen2 = findTopCandidates2(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
140 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
141 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);
143 Marshal.Copy(resultEigen2, resultArrayEigen2, 0, spectraIdx.Length * topN);
145 memStat = releaseMemory(resultEigen2);
149 Console.WriteLine(
"Time for candidate search Eigen SpM*V:");
150 Console.WriteLine(sw2.Elapsed.TotalSeconds.ToString());
152 var sw2Int = Stopwatch.StartNew();
154 IntPtr resultEigen2Int = findTopCandidates2Int(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
155 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
156 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);
158 Marshal.Copy(resultEigen2Int, resultArrayEigen2Int, 0, spectraIdx.Length * topN);
160 memStat = releaseMemory(resultEigen2Int);
164 Console.WriteLine(
"Time for candidate search Eigen SpM*V (int):");
165 Console.WriteLine(sw2Int.Elapsed.TotalSeconds.ToString());
167 var sw3 = Stopwatch.StartNew();
169 IntPtr resultEigenB = findTopCandidatesBatched(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
170 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
171 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);
173 Marshal.Copy(resultEigenB, resultArrayEigenB, 0, spectraIdx.Length * topN);
175 memStat = releaseMemory(resultEigenB);
179 Console.WriteLine(
"Time for candidate search Eigen SpM*SpM:");
180 Console.WriteLine(sw3.Elapsed.TotalSeconds.ToString());
182 var sw3Int = Stopwatch.StartNew();
184 IntPtr resultEigenBInt = findTopCandidatesBatchedInt(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
185 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
186 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);
188 Marshal.Copy(resultEigenBInt, resultArrayEigenBInt, 0, spectraIdx.Length * topN);
190 memStat = releaseMemory(resultEigenBInt);
194 Console.WriteLine(
"Time for candidate search Eigen SpM*SpM (int):");
195 Console.WriteLine(sw3Int.Elapsed.TotalSeconds.ToString());
197 var sw4 = Stopwatch.StartNew();
199 IntPtr resultEigenB2 = findTopCandidatesBatched2(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
200 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
201 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);
203 Marshal.Copy(resultEigenB2, resultArrayEigenB2, 0, spectraIdx.Length * topN);
205 memStat = releaseMemory(resultEigenB2);
209 Console.WriteLine(
"Time for candidate search Eigen SpM*M:");
210 Console.WriteLine(sw4.Elapsed.TotalSeconds.ToString());
212 var sw4Int = Stopwatch.StartNew();
214 IntPtr resultEigenB2Int = findTopCandidatesBatched2Int(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
215 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
216 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);
218 Marshal.Copy(resultEigenB2Int, resultArrayEigenB2Int, 0, spectraIdx.Length * topN);
220 memStat = releaseMemory(resultEigenB2Int);
224 Console.WriteLine(
"Time for candidate search Eigen SpM*M (int):");
225 Console.WriteLine(sw4Int.Elapsed.TotalSeconds.ToString());
227 var sw5 = Stopwatch.StartNew();
229 IntPtr resultCuda = findTopCandidatesCuda(csrRowoffsetsPtr, csrIdxPtr,
231 csrRowoffsets.Length, csrIdx.Length,
232 spectraValues.Length, spectraIdx.Length,
233 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0);
235 Marshal.Copy(resultCuda, resultArrayCuda, 0, spectraIdx.Length * topN);
237 memStat = releaseMemoryCuda(resultCuda);
241 Console.WriteLine(
"Time for candidate search Cuda SpMV:");
242 Console.WriteLine(sw5.Elapsed.TotalSeconds.ToString());
244 var sw6 = Stopwatch.StartNew();
246 IntPtr resultCudaB2 = findTopCandidatesCudaBatched2(csrRowoffsetsPtr, csrIdxPtr,
248 csrRowoffsets.Length, csrIdx.Length,
249 spectraValues.Length, spectraIdx.Length,
250 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0);
252 Marshal.Copy(resultCudaB2, resultArrayCudaB2, 0, spectraIdx.Length * topN);
254 memStat = releaseMemoryCuda(resultCudaB2);
258 Console.WriteLine(
"Time for candidate search Cuda SpMM:");
259 Console.WriteLine(sw6.Elapsed.TotalSeconds.ToString());
261 var sw7 = Stopwatch.StartNew();
263 IntPtr resultCudaB = findTopCandidatesCudaBatched(csrRowoffsetsPtr, csrIdxPtr,
265 csrRowoffsets.Length, csrIdx.Length,
266 spectraValues.Length, spectraIdx.Length,
267 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0);
269 Marshal.Copy(resultCudaB, resultArrayCudaB, 0, spectraIdx.Length * topN);
271 memStat = releaseMemoryCuda(resultCudaB);
275 Console.WriteLine(
"Time for candidate search Cuda SpGEMM:");
276 Console.WriteLine(sw7.Elapsed.TotalSeconds.ToString());
280 Console.WriteLine(
"Something went wrong:");
281 Console.WriteLine(ex.ToString());
285 if (cValuesLoc.IsAllocated) { cValuesLoc.Free(); }
286 if (cIdxLoc.IsAllocated) { cIdxLoc.Free(); }
287 if (csrRowoffsetsLoc.IsAllocated) { csrRowoffsetsLoc.Free(); }
288 if (csrIdxLoc.IsAllocated) { csrIdxLoc.Free(); }
289 if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
290 if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
293 Console.WriteLine($
"Top 5 of the first spectrum:");
295 for (
int i = 0; i < 5; i++)
297 Console.WriteLine($
"eSpVf32: {resultArrayEigen[i]}");
298 Console.WriteLine($
"eSpVi32: {resultArrayEigenInt[i]}");
299 Console.WriteLine($
"eDnVf32: {resultArrayEigen2[i]}");
300 Console.WriteLine($
"eDnVi32: {resultArrayEigen2Int[i]}");
301 Console.WriteLine($
"eSpMf32: {resultArrayEigenB[i]}");
302 Console.WriteLine($
"eSpMi32: {resultArrayEigenBInt[i]}");
303 Console.WriteLine($
"eDnMf32: {resultArrayEigenB2[i]}");
304 Console.WriteLine($
"eDnMi32: {resultArrayEigenB2Int[i]}");
305 Console.WriteLine($
"cDnVf32: {resultArrayCuda[i]}");
306 Console.WriteLine($
"cSpMf32: {resultArrayCudaB[i]}");
307 Console.WriteLine($
"cDnMf32: {resultArrayCudaB2[i]}");
308 Console.WriteLine(
"-----");
311 Console.WriteLine($
"Top 5 of the last spectrum:");
313 for (
int i = spectraIdx.Length * topN - topN; i < spectraIdx.Length * topN - topN + 5; i++)
315 Console.WriteLine($
"eSpVf32: {resultArrayEigen[i]}");
316 Console.WriteLine($
"eSpVi32: {resultArrayEigenInt[i]}");
317 Console.WriteLine($
"eDnVf32: {resultArrayEigen2[i]}");
318 Console.WriteLine($
"eDnVi32: {resultArrayEigen2Int[i]}");
319 Console.WriteLine($
"eSpMf32: {resultArrayEigenB[i]}");
320 Console.WriteLine($
"eSpMi32: {resultArrayEigenBInt[i]}");
321 Console.WriteLine($
"eDnMf32: {resultArrayEigenB2[i]}");
322 Console.WriteLine($
"eDnMi32: {resultArrayEigenB2Int[i]}");
323 Console.WriteLine($
"cDnVf32: {resultArrayCuda[i]}");
324 Console.WriteLine($
"cSpMf32: {resultArrayCudaB[i]}");
325 Console.WriteLine($
"cDnMf32: {resultArrayCudaB2[i]}");
326 Console.WriteLine(
"-----");
329 Console.WriteLine($
"MemStat: {memStat}");
333 GC.WaitForPendingFinalizers();