100 public static int Eigen(
int nrCandidates,
int nrSpectra,
int topN,
int batchSize, Random r,
bool batched,
bool sparse,
bool useInt =
false)
103 var candidateValues =
new int[nrCandidates * 100];
104 var candidatesIdx =
new int[nrCandidates];
106 for (
int i = 0; i < candidateValues.Length; i += 100)
108 candidatesIdx[currentIdx] = i;
109 var tmpValues =
new int[100];
110 for (
int j = 0; j < tmpValues.Length; j++)
112 var val = r.Next(ENCODING_SIZE);
113 while (Array.Exists(tmpValues, x => x == val))
115 val = r.Next(ENCODING_SIZE);
119 Array.Sort(tmpValues);
120 for (
int j = 0; j < tmpValues.Length; j++)
122 candidateValues[i + j] = tmpValues[j];
125 if (currentIdx % 5000 == 0)
127 Console.WriteLine($
"Generated {currentIdx} candidates...");
132 var spectraValues =
new int[nrSpectra * 500];
133 var spectraIdx =
new int[nrSpectra];
135 for (
int i = 0; i < spectraValues.Length; i += 500)
137 spectraIdx[currentIdx] = i;
138 var tmpValues =
new int[500];
139 for (
int j = 0; j < tmpValues.Length; j++)
141 var val = r.Next(ENCODING_SIZE);
142 while (Array.Exists(tmpValues, x => x == val))
144 val = r.Next(ENCODING_SIZE);
148 Array.Sort(tmpValues);
149 for (
int j = 0; j < tmpValues.Length; j++)
151 spectraValues[i + j] = tmpValues[j];
157 var sw = Stopwatch.StartNew();
160 var cValuesLoc = GCHandle.Alloc(candidateValues, GCHandleType.Pinned);
161 var cIdxLoc = GCHandle.Alloc(candidatesIdx, GCHandleType.Pinned);
162 var sValuesLoc = GCHandle.Alloc(spectraValues, GCHandleType.Pinned);
163 var sIdxLoc = GCHandle.Alloc(spectraIdx, GCHandleType.Pinned);
164 var resultArray =
new int[spectraIdx.Length * topN];
168 IntPtr cValuesPtr = cValuesLoc.AddrOfPinnedObject();
169 IntPtr cIdxPtr = cIdxLoc.AddrOfPinnedObject();
170 IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
171 IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();
179 IntPtr result = findTopCandidatesBatchedInt(cValuesPtr, cIdxPtr,
181 candidateValues.Length, candidatesIdx.Length,
182 spectraValues.Length, spectraIdx.Length,
184 NORMALIZE, USE_GAUSSIAN,
188 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
190 memStat = releaseMemory(result);
194 IntPtr result = findTopCandidatesBatched2Int(cValuesPtr, cIdxPtr,
196 candidateValues.Length, candidatesIdx.Length,
197 spectraValues.Length, spectraIdx.Length,
199 NORMALIZE, USE_GAUSSIAN,
203 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
205 memStat = releaseMemory(result);
212 IntPtr result = findTopCandidatesInt(cValuesPtr, cIdxPtr,
214 candidateValues.Length, candidatesIdx.Length,
215 spectraValues.Length, spectraIdx.Length,
217 NORMALIZE, USE_GAUSSIAN,
220 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
222 memStat = releaseMemory(result);
226 IntPtr result = findTopCandidates2Int(cValuesPtr, cIdxPtr,
228 candidateValues.Length, candidatesIdx.Length,
229 spectraValues.Length, spectraIdx.Length,
231 NORMALIZE, USE_GAUSSIAN,
234 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
236 memStat = releaseMemory(result);
240 else if (!batched && sparse)
242 IntPtr result = findTopCandidates(cValuesPtr, cIdxPtr,
244 candidateValues.Length, candidatesIdx.Length,
245 spectraValues.Length, spectraIdx.Length,
247 NORMALIZE, USE_GAUSSIAN,
250 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
252 memStat = releaseMemory(result);
254 else if (!batched && !sparse)
256 IntPtr result = findTopCandidates2(cValuesPtr, cIdxPtr,
258 candidateValues.Length, candidatesIdx.Length,
259 spectraValues.Length, spectraIdx.Length,
261 NORMALIZE, USE_GAUSSIAN,
264 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
266 memStat = releaseMemory(result);
268 else if(batched && sparse)
270 IntPtr result = findTopCandidatesBatched(cValuesPtr, cIdxPtr,
272 candidateValues.Length, candidatesIdx.Length,
273 spectraValues.Length, spectraIdx.Length,
275 NORMALIZE, USE_GAUSSIAN,
279 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
281 memStat = releaseMemory(result);
283 else if(batched && !sparse)
285 IntPtr result = findTopCandidatesBatched2(cValuesPtr, cIdxPtr,
287 candidateValues.Length, candidatesIdx.Length,
288 spectraValues.Length, spectraIdx.Length,
290 NORMALIZE, USE_GAUSSIAN,
294 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
296 memStat = releaseMemory(result);
300 Console.WriteLine(
"Impossible case!");
306 Console.WriteLine(
"Something went wrong:");
307 Console.WriteLine(ex.ToString());
311 if (cValuesLoc.IsAllocated) { cValuesLoc.Free(); }
312 if (cIdxLoc.IsAllocated) { cIdxLoc.Free(); }
313 if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
314 if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
320 for (
int i = 0; i < topN; i++)
322 Console.WriteLine(resultArray[i]);
325 Console.WriteLine($
"MemStat: {memStat}");
326 var mode = batched ?
"(SpMM)" :
"(SpMV)";
327 Console.WriteLine($
"Time for candidate search {mode}:");
328 Console.WriteLine(sw.Elapsed.TotalSeconds.ToString());
332 GC.WaitForPendingFinalizers();