CandidateVectorSearch 1.7.2
Searching for peptide candidates using sparse matrix + matrix/vector multiplication.
Loading...
Searching...
No Matches
Eigen.cs
Go to the documentation of this file.
1using System.Diagnostics;
2using System.Runtime.InteropServices;
3
5{
6 public partial class DataLoader
7 {
8 const string dll = @"VectorSearch.dll";
9 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
10 private static extern IntPtr findTopCandidates(IntPtr cV, IntPtr cI,
11 IntPtr sV, IntPtr sI,
12 int cVL, int cIL,
13 int sVL, int sIL,
14 int n, float tolerance,
15 bool normalize, bool gaussianTol,
16 int cores, int verbose);
17
18 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
19 private static extern IntPtr findTopCandidatesInt(IntPtr cV, IntPtr cI,
20 IntPtr sV, IntPtr sI,
21 int cVL, int cIL,
22 int sVL, int sIL,
23 int n, float tolerance,
24 bool normalize, bool gaussianTol,
25 int cores, int verbose);
26
27 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
28 private static extern IntPtr findTopCandidatesBatched(IntPtr cV, IntPtr cI,
29 IntPtr sV, IntPtr sI,
30 int cVL, int cIL,
31 int sVL, int sIL,
32 int n, float tolerance,
33 bool normalize, bool gaussianTol,
34 int batchSize,
35 int cores, int verbose);
36
37 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
38 private static extern IntPtr findTopCandidatesBatchedInt(IntPtr cV, IntPtr cI,
39 IntPtr sV, IntPtr sI,
40 int cVL, int cIL,
41 int sVL, int sIL,
42 int n, float tolerance,
43 bool normalize, bool gaussianTol,
44 int batchSize,
45 int cores, int verbose);
46
47 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
48 private static extern IntPtr findTopCandidates2(IntPtr cV, IntPtr cI,
49 IntPtr sV, IntPtr sI,
50 int cVL, int cIL,
51 int sVL, int sIL,
52 int n, float tolerance,
53 bool normalize, bool gaussianTol,
54 int cores, int verbose);
55
56 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
57 private static extern IntPtr findTopCandidates2Int(IntPtr cV, IntPtr cI,
58 IntPtr sV, IntPtr sI,
59 int cVL, int cIL,
60 int sVL, int sIL,
61 int n, float tolerance,
62 bool normalize, bool gaussianTol,
63 int cores, int verbose);
64
65 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
66 private static extern IntPtr findTopCandidatesBatched2(IntPtr cV, IntPtr cI,
67 IntPtr sV, IntPtr sI,
68 int cVL, int cIL,
69 int sVL, int sIL,
70 int n, float tolerance,
71 bool normalize, bool gaussianTol,
72 int batchSize,
73 int cores, int verbose);
74
75 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
76 private static extern IntPtr findTopCandidatesBatched2Int(IntPtr cV, IntPtr cI,
77 IntPtr sV, IntPtr sI,
78 int cVL, int cIL,
79 int sVL, int sIL,
80 int n, float tolerance,
81 bool normalize, bool gaussianTol,
82 int batchSize,
83 int cores, int verbose);
84
85 [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
86 private static extern int releaseMemory(IntPtr result);
87
100 public static int Eigen(int nrCandidates, int nrSpectra, int topN, int batchSize, Random r, bool batched, bool sparse, bool useInt = false)
101 {
102 // generate candidate vectors
103 var candidateValues = new int[nrCandidates * 100];
104 var candidatesIdx = new int[nrCandidates];
105 var currentIdx = 0;
106 for (int i = 0; i < candidateValues.Length; i += 100)
107 {
108 candidatesIdx[currentIdx] = i;
109 var tmpValues = new int[100];
110 for (int j = 0; j < tmpValues.Length; j++)
111 {
112 var val = r.Next(ENCODING_SIZE);
113 while (Array.Exists(tmpValues, x => x == val))
114 {
115 val = r.Next(ENCODING_SIZE);
116 }
117 tmpValues[j] = val;
118 }
119 Array.Sort(tmpValues);
120 for (int j = 0; j < tmpValues.Length; j++)
121 {
122 candidateValues[i + j] = tmpValues[j];
123 }
124 currentIdx++;
125 if (currentIdx % 5000 == 0)
126 {
127 Console.WriteLine($"Generated {currentIdx} candidates...");
128 }
129 }
130
131 // generate spectra vectors
132 var spectraValues = new int[nrSpectra * 500];
133 var spectraIdx = new int[nrSpectra];
134 currentIdx = 0;
135 for (int i = 0; i < spectraValues.Length; i += 500)
136 {
137 spectraIdx[currentIdx] = i;
138 var tmpValues = new int[500];
139 for (int j = 0; j < tmpValues.Length; j++)
140 {
141 var val = r.Next(ENCODING_SIZE);
142 while (Array.Exists(tmpValues, x => x == val))
143 {
144 val = r.Next(ENCODING_SIZE);
145 }
146 tmpValues[j] = val;
147 }
148 Array.Sort(tmpValues);
149 for (int j = 0; j < tmpValues.Length; j++)
150 {
151 spectraValues[i + j] = tmpValues[j];
152 }
153 currentIdx++;
154 }
155
156 // time c++ call
157 var sw = Stopwatch.StartNew();
158
159 // get pointer addresses and call c++ function
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];
165 var memStat = 1;
166 try
167 {
168 IntPtr cValuesPtr = cValuesLoc.AddrOfPinnedObject();
169 IntPtr cIdxPtr = cIdxLoc.AddrOfPinnedObject();
170 IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
171 IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();
172
173 if (useInt)
174 {
175 if (batched)
176 {
177 if (sparse)
178 {
179 IntPtr result = findTopCandidatesBatchedInt(cValuesPtr, cIdxPtr,
180 sValuesPtr, sIdxPtr,
181 candidateValues.Length, candidatesIdx.Length,
182 spectraValues.Length, spectraIdx.Length,
183 topN, (float) 0.02,
184 NORMALIZE, USE_GAUSSIAN,
185 batchSize,
186 0, 1000);
187
188 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
189
190 memStat = releaseMemory(result);
191 }
192 else
193 {
194 IntPtr result = findTopCandidatesBatched2Int(cValuesPtr, cIdxPtr,
195 sValuesPtr, sIdxPtr,
196 candidateValues.Length, candidatesIdx.Length,
197 spectraValues.Length, spectraIdx.Length,
198 topN, (float) 0.02,
199 NORMALIZE, USE_GAUSSIAN,
200 batchSize,
201 0, 1000);
202
203 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
204
205 memStat = releaseMemory(result);
206 }
207 }
208 else
209 {
210 if (sparse)
211 {
212 IntPtr result = findTopCandidatesInt(cValuesPtr, cIdxPtr,
213 sValuesPtr, sIdxPtr,
214 candidateValues.Length, candidatesIdx.Length,
215 spectraValues.Length, spectraIdx.Length,
216 topN, (float) 0.02,
217 NORMALIZE, USE_GAUSSIAN,
218 0, 1000);
219
220 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
221
222 memStat = releaseMemory(result);
223 }
224 else
225 {
226 IntPtr result = findTopCandidates2Int(cValuesPtr, cIdxPtr,
227 sValuesPtr, sIdxPtr,
228 candidateValues.Length, candidatesIdx.Length,
229 spectraValues.Length, spectraIdx.Length,
230 topN, (float) 0.02,
231 NORMALIZE, USE_GAUSSIAN,
232 0, 1000);
233
234 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
235
236 memStat = releaseMemory(result);
237 }
238 }
239 }
240 else if (!batched && sparse)
241 {
242 IntPtr result = findTopCandidates(cValuesPtr, cIdxPtr,
243 sValuesPtr, sIdxPtr,
244 candidateValues.Length, candidatesIdx.Length,
245 spectraValues.Length, spectraIdx.Length,
246 topN, (float) 0.02,
247 NORMALIZE, USE_GAUSSIAN,
248 0, 1000);
249
250 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
251
252 memStat = releaseMemory(result);
253 }
254 else if (!batched && !sparse)
255 {
256 IntPtr result = findTopCandidates2(cValuesPtr, cIdxPtr,
257 sValuesPtr, sIdxPtr,
258 candidateValues.Length, candidatesIdx.Length,
259 spectraValues.Length, spectraIdx.Length,
260 topN, (float) 0.02,
261 NORMALIZE, USE_GAUSSIAN,
262 0, 1000);
263
264 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
265
266 memStat = releaseMemory(result);
267 }
268 else if(batched && sparse)
269 {
270 IntPtr result = findTopCandidatesBatched(cValuesPtr, cIdxPtr,
271 sValuesPtr, sIdxPtr,
272 candidateValues.Length, candidatesIdx.Length,
273 spectraValues.Length, spectraIdx.Length,
274 topN, (float) 0.02,
275 NORMALIZE, USE_GAUSSIAN,
276 batchSize,
277 0, 1000);
278
279 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
280
281 memStat = releaseMemory(result);
282 }
283 else if(batched && !sparse)
284 {
285 IntPtr result = findTopCandidatesBatched2(cValuesPtr, cIdxPtr,
286 sValuesPtr, sIdxPtr,
287 candidateValues.Length, candidatesIdx.Length,
288 spectraValues.Length, spectraIdx.Length,
289 topN, (float) 0.02,
290 NORMALIZE, USE_GAUSSIAN,
291 batchSize,
292 0, 1000);
293
294 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
295
296 memStat = releaseMemory(result);
297 }
298 else
299 {
300 Console.WriteLine("Impossible case!");
301 }
302
303 }
304 catch (Exception ex)
305 {
306 Console.WriteLine("Something went wrong:");
307 Console.WriteLine(ex.ToString());
308 }
309 finally
310 {
311 if (cValuesLoc.IsAllocated) { cValuesLoc.Free(); }
312 if (cIdxLoc.IsAllocated) { cIdxLoc.Free(); }
313 if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
314 if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
315 }
316
317 // end time c++ call
318 sw.Stop();
319
320 for (int i = 0; i < topN; i++)
321 {
322 Console.WriteLine(resultArray[i]);
323 }
324
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());
329
330 //
331 GC.Collect();
332 GC.WaitForPendingFinalizers();
333
334 return 0;
335 }
336 }
337}
static int Eigen(int nrCandidates, int nrSpectra, int topN, int batchSize, Random r, bool batched, bool sparse, bool useInt=false)
Wrapper for testing CPU-based matrix multiplication functions.
Definition Eigen.cs:100