17 public static int Compare(
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 var sw1Int = Stopwatch.StartNew();
121 IntPtr resultEigenInt = findTopCandidatesInt(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
122 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
123 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);
125 Marshal.Copy(resultEigenInt, resultArrayEigenInt, 0, spectraIdx.Length * topN);
127 memStat = releaseMemory(resultEigenInt);
131 var sw2 = Stopwatch.StartNew();
133 IntPtr resultEigen2 = findTopCandidates2(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
134 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
135 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);
137 Marshal.Copy(resultEigen2, resultArrayEigen2, 0, spectraIdx.Length * topN);
139 memStat = releaseMemory(resultEigen2);
143 var sw2Int = Stopwatch.StartNew();
145 IntPtr resultEigen2Int = findTopCandidates2Int(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
146 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
147 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);
149 Marshal.Copy(resultEigen2Int, resultArrayEigen2Int, 0, spectraIdx.Length * topN);
151 memStat = releaseMemory(resultEigen2Int);
155 var sw3 = Stopwatch.StartNew();
157 IntPtr resultEigenB = findTopCandidatesBatched(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
158 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
159 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);
161 Marshal.Copy(resultEigenB, resultArrayEigenB, 0, spectraIdx.Length * topN);
163 memStat = releaseMemory(resultEigenB);
167 var sw3Int = Stopwatch.StartNew();
169 IntPtr resultEigenBInt = findTopCandidatesBatchedInt(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(resultEigenBInt, resultArrayEigenBInt, 0, spectraIdx.Length * topN);
175 memStat = releaseMemory(resultEigenBInt);
179 var sw4 = Stopwatch.StartNew();
181 IntPtr resultEigenB2 = findTopCandidatesBatched2(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
182 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
183 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);
185 Marshal.Copy(resultEigenB2, resultArrayEigenB2, 0, spectraIdx.Length * topN);
187 memStat = releaseMemory(resultEigenB2);
191 var sw4Int = Stopwatch.StartNew();
193 IntPtr resultEigenB2Int = findTopCandidatesBatched2Int(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
194 candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
195 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);
197 Marshal.Copy(resultEigenB2Int, resultArrayEigenB2Int, 0, spectraIdx.Length * topN);
199 memStat = releaseMemory(resultEigenB2Int);
203 var sw5 = Stopwatch.StartNew();
205 IntPtr resultCuda = findTopCandidatesCuda(csrRowoffsetsPtr, csrIdxPtr,
207 csrRowoffsets.Length, csrIdx.Length,
208 spectraValues.Length, spectraIdx.Length,
209 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, 0);
211 Marshal.Copy(resultCuda, resultArrayCuda, 0, spectraIdx.Length * topN);
213 memStat = releaseMemoryCuda(resultCuda);
217 var sw6 = Stopwatch.StartNew();
219 IntPtr resultCudaB = findTopCandidatesCudaBatched(csrRowoffsetsPtr, csrIdxPtr,
221 csrRowoffsets.Length, csrIdx.Length,
222 spectraValues.Length, spectraIdx.Length,
223 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0);
225 Marshal.Copy(resultCudaB, resultArrayCudaB, 0, spectraIdx.Length * topN);
227 memStat = releaseMemoryCuda(resultCudaB);
231 var sw7 = Stopwatch.StartNew();
233 IntPtr resultCudaB2 = findTopCandidatesCudaBatched2(csrRowoffsetsPtr, csrIdxPtr,
235 csrRowoffsets.Length, csrIdx.Length,
236 spectraValues.Length, spectraIdx.Length,
237 topN, (
float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0);
239 Marshal.Copy(resultCudaB2, resultArrayCudaB2, 0, spectraIdx.Length * topN);
241 memStat = releaseMemoryCuda(resultCudaB2);
245 Console.WriteLine(
"Time for candidate search Eigen SpM*SpV:");
246 Console.WriteLine(sw1.Elapsed.TotalSeconds.ToString());
247 Console.WriteLine(
"Time for candidate search Eigen SpM*SpV (int):");
248 Console.WriteLine(sw1Int.Elapsed.TotalSeconds.ToString());
249 Console.WriteLine(
"Time for candidate search Eigen SpM*V:");
250 Console.WriteLine(sw2.Elapsed.TotalSeconds.ToString());
251 Console.WriteLine(
"Time for candidate search Eigen SpM*V (int):");
252 Console.WriteLine(sw2Int.Elapsed.TotalSeconds.ToString());
253 Console.WriteLine(
"Time for candidate search Eigen SpM*SpM:");
254 Console.WriteLine(sw3.Elapsed.TotalSeconds.ToString());
255 Console.WriteLine(
"Time for candidate search Eigen SpM*SpM (int):");
256 Console.WriteLine(sw3Int.Elapsed.TotalSeconds.ToString());
257 Console.WriteLine(
"Time for candidate search Eigen SpM*M:");
258 Console.WriteLine(sw4.Elapsed.TotalSeconds.ToString());
259 Console.WriteLine(
"Time for candidate search Eigen SpM*M (int):");
260 Console.WriteLine(sw4Int.Elapsed.TotalSeconds.ToString());
261 Console.WriteLine(
"Time for candidate search Cuda SpMV:");
262 Console.WriteLine(sw5.Elapsed.TotalSeconds.ToString());
263 Console.WriteLine(
"Time for candidate search Cuda SpGEMM:");
264 Console.WriteLine(sw6.Elapsed.TotalSeconds.ToString());
265 Console.WriteLine(
"Time for candidate search Cuda SpMM:");
266 Console.WriteLine(sw7.Elapsed.TotalSeconds.ToString());
270 Console.WriteLine(
"Something went wrong:");
271 Console.WriteLine(ex.ToString());
275 if (cValuesLoc.IsAllocated) { cValuesLoc.Free(); }
276 if (cIdxLoc.IsAllocated) { cIdxLoc.Free(); }
277 if (csrRowoffsetsLoc.IsAllocated) { csrRowoffsetsLoc.Free(); }
278 if (csrIdxLoc.IsAllocated) { csrIdxLoc.Free(); }
279 if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
280 if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
283 Console.WriteLine($
"Top {topN} of the first spectrum:");
285 for (
int i = 0; i < topN; i++)
287 Console.WriteLine($
"eSpVf32: {resultArrayEigen[i]}");
288 Console.WriteLine($
"eSpVi32: {resultArrayEigenInt[i]}");
289 Console.WriteLine($
"eDnVf32: {resultArrayEigen2[i]}");
290 Console.WriteLine($
"eDnVi32: {resultArrayEigen2Int[i]}");
291 Console.WriteLine($
"eSpMf32: {resultArrayEigenB[i]}");
292 Console.WriteLine($
"eSpMi32: {resultArrayEigenBInt[i]}");
293 Console.WriteLine($
"eDnMf32: {resultArrayEigenB2[i]}");
294 Console.WriteLine($
"eDnMi32: {resultArrayEigenB2Int[i]}");
295 Console.WriteLine($
"cDnVf32: {resultArrayCuda[i]}");
296 Console.WriteLine($
"cSpMf32: {resultArrayCudaB[i]}");
297 Console.WriteLine($
"cDnMf32: {resultArrayCudaB2[i]}");
298 Console.WriteLine(
"-----");
301 Console.WriteLine($
"Top {topN} of the last spectrum:");
303 for (
int i = spectraIdx.Length * topN - topN; i < spectraIdx.Length * topN; i++)
305 Console.WriteLine($
"eSpVf32: {resultArrayEigen[i]}");
306 Console.WriteLine($
"eSpVi32: {resultArrayEigenInt[i]}");
307 Console.WriteLine($
"eDnVf32: {resultArrayEigen2[i]}");
308 Console.WriteLine($
"eDnVi32: {resultArrayEigen2Int[i]}");
309 Console.WriteLine($
"eSpMf32: {resultArrayEigenB[i]}");
310 Console.WriteLine($
"eSpMi32: {resultArrayEigenBInt[i]}");
311 Console.WriteLine($
"eDnMf32: {resultArrayEigenB2[i]}");
312 Console.WriteLine($
"eDnMi32: {resultArrayEigenB2Int[i]}");
313 Console.WriteLine($
"cDnVf32: {resultArrayCuda[i]}");
314 Console.WriteLine($
"cSpMf32: {resultArrayCudaB[i]}");
315 Console.WriteLine($
"cDnMf32: {resultArrayCudaB2[i]}");
316 Console.WriteLine(
"-----");
319 Console.WriteLine($
"MemStat: {memStat}");
323 GC.WaitForPendingFinalizers();