CandidateVectorSearch 1.7.2
Searching for peptide candidates using sparse matrix + matrix/vector multiplication.
Loading...
Searching...
No Matches
DeterministicCompare.cs
Go to the documentation of this file.
1using System.Diagnostics;
2using System.Runtime.InteropServices;
3
5{
6 public partial class DataLoader
7 {
14 public static int DeterministicCompare()
15 {
16 // EXAMPLE PROBLEM
17 //
18 // need to set encoding size to 5 in c++ dlls
19 //
20 // [ [0, 1, 0, 0, 1] - 2
21 // [1, 1, 1, 1, 0] - 4
22 // [0, 1, 1, 1, 0] - 3
23 // [0, 1, 0, 0, 1] - 2
24 // [1, 1, 1, 1, 1] ] - 5
25 //
26 // [0, 1, 1, 0, 0]
27 // [1, 1, 1, 0, 1]
28 //
29 // Result 1:
30 // [0.5, 0.5, 0.6, 0.5, 0.4]
31 //
32 // Result 2;
33 // [1, 0.75, 0.6, 1, 0.8]
34 // generate candidate vectors
35 var candidateValues = new int[] { 1, 4, 0, 1, 2, 3, 1, 2, 3, 1, 4, 0, 1, 2, 3, 4};
36 var candidatesIdx = new int[] { 0, 2, 6, 9, 11 };
37 var csrRowoffsets = new int[] { 0, 2, 6, 9, 11, 16 };
38 var csrIdx = new int[] { 1, 4, 0, 1, 2, 3, 1, 2, 3, 1, 4, 0, 1, 2, 3, 4 };
39
40 // generate spectra vectors
41 var spectraValues = new int[] { 1, 2, 0, 1, 2, 4 };
42 var spectraIdx = new int[] { 0, 2 };
43
44 // topN
45 var topN = 5;
46
47 // time c++ call
48 var sw = Stopwatch.StartNew();
49
50 // get pointer addresses and call c++ function
51 var cValuesLoc = GCHandle.Alloc(candidateValues, GCHandleType.Pinned);
52 var cIdxLoc = GCHandle.Alloc(candidatesIdx, GCHandleType.Pinned);
53 var csrRowoffsetsLoc = GCHandle.Alloc(csrRowoffsets, GCHandleType.Pinned);
54 var csrIdxLoc = GCHandle.Alloc(csrIdx, GCHandleType.Pinned);
55 var sValuesLoc = GCHandle.Alloc(spectraValues, GCHandleType.Pinned);
56 var sIdxLoc = GCHandle.Alloc(spectraIdx, GCHandleType.Pinned);
57 var resultArrayEigen = new int[spectraIdx.Length * topN];
58 var resultArrayCuda = new int[spectraIdx.Length * topN];
59 var memStat = 1;
60 try
61 {
62 IntPtr cValuesPtr = cValuesLoc.AddrOfPinnedObject();
63 IntPtr cIdxPtr = cIdxLoc.AddrOfPinnedObject();
64 IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
65 IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
66 IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
67 IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();
68
69 //IntPtr resultEigen = findTopCandidates(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
70 // candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
71 // topN, (float) 0.0, NORMALIZE, USE_GAUSSIAN, 0);
72 //
73 //Marshal.Copy(resultEigen, resultArrayEigen, 0, spectraIdx.Length * topN);
74 //
75 //memStat = releaseMemory(resultEigen);
76
77 IntPtr resultCuda = findTopCandidatesCudaBatched(csrRowoffsetsPtr, csrIdxPtr,
78 sValuesPtr, sIdxPtr,
79 csrRowoffsets.Length, csrIdx.Length,
80 spectraValues.Length, spectraIdx.Length,
81 topN, (float) 0.0,
82 NORMALIZE, USE_GAUSSIAN, 2,
83 1000);
84
85 Marshal.Copy(resultCuda, resultArrayCuda, 0, spectraIdx.Length * topN);
86
87 memStat = releaseMemoryCuda(resultCuda);
88 }
89 catch (Exception ex)
90 {
91 Console.WriteLine("Something went wrong:");
92 Console.WriteLine(ex.ToString());
93 }
94 finally
95 {
96 if (cValuesLoc.IsAllocated) { cValuesLoc.Free(); }
97 if (cIdxLoc.IsAllocated) { cIdxLoc.Free(); }
98 if (csrRowoffsetsLoc.IsAllocated) { csrRowoffsetsLoc.Free(); }
99 if (csrIdxLoc.IsAllocated) { csrIdxLoc.Free(); }
100 if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
101 if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
102 }
103
104 // end time c++ call
105 sw.Stop();
106
107 for (int i = 0; i < spectraIdx.Length * topN; i++)
108 {
109 Console.WriteLine($"Eigen: {resultArrayEigen[i]}");
110 }
111 for (int i = 0; i < spectraIdx.Length * topN; i++)
112 {
113 Console.WriteLine($"CUDA: {resultArrayCuda[i]}");
114 }
115
116 Console.WriteLine($"MemStat: {memStat}");
117 Console.WriteLine("Time for candidate search (SpMV):");
118 Console.WriteLine(sw.Elapsed.TotalSeconds.ToString());
119
120 //
121 GC.Collect();
122 GC.WaitForPendingFinalizers();
123
124 return 0;
125 }
126 }
127}
static int DeterministicCompare()
[DEPRECATED] Function for testing a simple matrix multiplication of a deterministic problem....