52 public static int Cuda(
int nrCandidates,
int nrSpectra,
int topN,
int batchSize, Random r,
bool batched,
int batchMode)
55 var csrRowoffsets =
new int[nrCandidates + 1];
56 var csrIdx =
new int[nrCandidates * 100];
58 for (
int i = 0; i < csrIdx.Length; i += 100)
60 csrRowoffsets[currentIdx] = i;
61 var tmpIdx =
new int[100];
62 for (
int j = 0; j < tmpIdx.Length; j++)
64 var val = r.Next(ENCODING_SIZE);
65 while (Array.Exists(tmpIdx, x => x == val))
67 val = r.Next(ENCODING_SIZE);
72 for (
int j = 0; j < tmpIdx.Length; j++)
74 csrIdx[i + j] = tmpIdx[j];
77 if (currentIdx % 5000 == 0)
79 Console.WriteLine($
"Generated {currentIdx} candidates...");
83 csrRowoffsets[currentIdx++] = nrCandidates * 100;
86 var spectraValues =
new int[nrSpectra * 500];
87 var spectraIdx =
new int[nrSpectra];
89 for (
int i = 0; i < spectraValues.Length; i += 500)
91 spectraIdx[currentIdx] = i;
92 var tmpValues =
new int[500];
93 for (
int j = 0; j < tmpValues.Length; j++)
95 var val = r.Next(ENCODING_SIZE);
96 while (Array.Exists(tmpValues, x => x == val))
98 val = r.Next(ENCODING_SIZE);
102 Array.Sort(tmpValues);
103 for (
int j = 0; j < tmpValues.Length; j++)
105 spectraValues[i + j] = tmpValues[j];
111 var sw = Stopwatch.StartNew();
114 var csrRowoffsetsLoc = GCHandle.Alloc(csrRowoffsets, GCHandleType.Pinned);
115 var csrIdxLoc = GCHandle.Alloc(csrIdx, GCHandleType.Pinned);
116 var sValuesLoc = GCHandle.Alloc(spectraValues, GCHandleType.Pinned);
117 var sIdxLoc = GCHandle.Alloc(spectraIdx, GCHandleType.Pinned);
118 var resultArray =
new int[spectraIdx.Length * topN];
124 IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
125 IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
126 IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
127 IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();
129 IntPtr result = findTopCandidatesCuda(csrRowoffsetsPtr, csrIdxPtr,
131 csrRowoffsets.Length, csrIdx.Length,
132 spectraValues.Length, spectraIdx.Length,
134 NORMALIZE, USE_GAUSSIAN,
137 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
139 memStat = releaseMemoryCuda(result);
145 IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
146 IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
147 IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
148 IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();
150 IntPtr result = findTopCandidatesCudaBatched2(csrRowoffsetsPtr, csrIdxPtr,
152 csrRowoffsets.Length, csrIdx.Length,
153 spectraValues.Length, spectraIdx.Length,
155 NORMALIZE, USE_GAUSSIAN,
159 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
161 memStat = releaseMemoryCuda(result);
165 IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
166 IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
167 IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
168 IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();
170 IntPtr result = findTopCandidatesCudaBatched(csrRowoffsetsPtr, csrIdxPtr,
172 csrRowoffsets.Length, csrIdx.Length,
173 spectraValues.Length, spectraIdx.Length,
175 NORMALIZE, USE_GAUSSIAN,
179 Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);
181 memStat = releaseMemoryCuda(result);
187 Console.WriteLine(
"Something went wrong:");
188 Console.WriteLine(ex.ToString());
192 if (csrRowoffsetsLoc.IsAllocated) { csrRowoffsetsLoc.Free(); }
193 if (csrIdxLoc.IsAllocated) { csrIdxLoc.Free(); }
194 if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
195 if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
201 for (
int i = 0; i < topN; i++)
203 Console.WriteLine(resultArray[i]);
206 Console.WriteLine($
"MemStat: {memStat}");
207 var mode = batched ? batchMode == 2 ?
"(SpMM)" :
"(SpGEMM)" :
"(SpMV)";
208 Console.WriteLine($
"Time for candidate search {mode}:");
209 Console.WriteLine(sw.Elapsed.TotalSeconds.ToString());
213 GC.WaitForPendingFinalizers();