Skip to content

Commit

Permalink
Merge pull request #2 from hgb-bin-proteomics/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
michabirklbauer authored Jul 13, 2023
2 parents dd9f9d0 + dedcffd commit 22a27f1
Show file tree
Hide file tree
Showing 16 changed files with 1,092 additions and 140 deletions.
145 changes: 145 additions & 0 deletions DataLoader/Compare.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace FHOOE_IMP.MS_Annika.Utils.NonCleavableSearch
{
public partial class DataLoader
{
public static int Compare(int nrCandidates, int nrSpectra, int topN, Random r)
{
// generate candidate vectors
var candidateValues = new int[nrCandidates * 100];
var candidatesIdx = new int[nrCandidates];
var csrRowoffsets = new int[nrCandidates + 1];
var csrIdx = new int[nrCandidates * 100];
var currentIdx = 0;
for (int i = 0; i < candidateValues.Length; i += 100)
{
candidatesIdx[currentIdx] = i;
csrRowoffsets[currentIdx] = i;
var tmpValues = new int[100];
for (int j = 0; j < tmpValues.Length; j++)
{
var val = r.Next(ENCODING_SIZE);
while (Array.Exists(tmpValues, x => x == val))
{
val = r.Next(ENCODING_SIZE);
}
tmpValues[j] = val;
}
Array.Sort(tmpValues);
for (int j = 0; j < tmpValues.Length; j++)
{
candidateValues[i + j] = tmpValues[j];
csrIdx[i + j] = tmpValues[j];
}
currentIdx++;
if (currentIdx % 5000 == 0)
{
Console.WriteLine($"Generated {currentIdx} candidates...");
}
}
// add the end of matrix as specified in CSR format
csrRowoffsets[currentIdx++] = nrCandidates * 100;

// generate spectra vectors
var spectraValues = new int[nrSpectra * 500];
var spectraIdx = new int[nrSpectra];
currentIdx = 0;
for (int i = 0; i < spectraValues.Length; i += 500)
{
spectraIdx[currentIdx] = i;
var tmpValues = new int[500];
for (int j = 0; j < tmpValues.Length; j++)
{
var val = r.Next(ENCODING_SIZE);
while (Array.Exists(tmpValues, x => x == val))
{
val = r.Next(ENCODING_SIZE);
}
tmpValues[j] = val;
}
Array.Sort(tmpValues);
for (int j = 0; j < tmpValues.Length; j++)
{
spectraValues[i + j] = tmpValues[j];
}
currentIdx++;
}

// time c++ call
var sw = Stopwatch.StartNew();

// get pointer addresses and call c++ function
var cValuesLoc = GCHandle.Alloc(candidateValues, GCHandleType.Pinned);
var cIdxLoc = GCHandle.Alloc(candidatesIdx, GCHandleType.Pinned);
var csrRowoffsetsLoc = GCHandle.Alloc(csrRowoffsets, GCHandleType.Pinned);
var csrIdxLoc = GCHandle.Alloc(csrIdx, GCHandleType.Pinned);
var sValuesLoc = GCHandle.Alloc(spectraValues, GCHandleType.Pinned);
var sIdxLoc = GCHandle.Alloc(spectraIdx, GCHandleType.Pinned);
var resultArrayEigen = new int[spectraIdx.Length * topN];
var resultArrayCuda = new int[spectraIdx.Length * topN];
var memStat = 1;
try
{
IntPtr cValuesPtr = cValuesLoc.AddrOfPinnedObject();
IntPtr cIdxPtr = cIdxLoc.AddrOfPinnedObject();
IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();

IntPtr resultEigen = findTopCandidates(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02);

Marshal.Copy(resultEigen, resultArrayEigen, 0, spectraIdx.Length * topN);

memStat = releaseMemory(resultEigen);

IntPtr resultCuda = findTopCandidatesCuda(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
csrRowoffsets.Length, csrIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02);

Marshal.Copy(resultCuda, resultArrayCuda, 0, spectraIdx.Length * topN);

memStat = releaseMemoryCuda(resultCuda);
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong:");
Console.WriteLine(ex.ToString());
}
finally
{
if (cValuesLoc.IsAllocated) { cValuesLoc.Free(); }
if (cIdxLoc.IsAllocated) { cIdxLoc.Free(); }
if (csrRowoffsetsLoc.IsAllocated) { csrRowoffsetsLoc.Free(); }
if (csrIdxLoc.IsAllocated) { csrIdxLoc.Free(); }
if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
}

// end time c++ call
sw.Stop();

for (int i = 0; i < topN; i++)
{
Console.WriteLine(resultArrayEigen[i]);
Console.WriteLine(resultArrayCuda[i]);
}

Console.WriteLine($"MemStat: {memStat}");
Console.WriteLine("Time for candidate search (SpMV):");
Console.WriteLine(sw.Elapsed.TotalSeconds.ToString());

//
GC.Collect();
GC.WaitForPendingFinalizers();

return 0;
}
}
}
136 changes: 136 additions & 0 deletions DataLoader/Cuda.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace FHOOE_IMP.MS_Annika.Utils.NonCleavableSearch
{
public partial class DataLoader
{
const string dllCuda = @"VectorSearchCuda.dll";
[DllImport(dllCuda, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr findTopCandidatesCuda(IntPtr cR, IntPtr cI,
IntPtr sV, IntPtr sI,
int cRL, int cNNZ,
int sVL, int sIL,
int n, float tolerance);

[DllImport(dllCuda, CallingConvention = CallingConvention.Cdecl)]
private static extern int releaseMemoryCuda(IntPtr result);

public static int Cuda(int nrCandidates, int nrSpectra, int topN, Random r)
{
// generate candidate vectors
var csrRowoffsets = new int[nrCandidates + 1];
var csrIdx = new int[nrCandidates * 100];
var currentIdx = 0;
for (int i = 0; i < csrIdx.Length; i += 100)
{
csrRowoffsets[currentIdx] = i;
var tmpIdx = new int[100];
for (int j = 0; j < tmpIdx.Length; j++)
{
var val = r.Next(ENCODING_SIZE);
while (Array.Exists(tmpIdx, x => x == val))
{
val = r.Next(ENCODING_SIZE);
}
tmpIdx[j] = val;
}
Array.Sort(tmpIdx);
for (int j = 0; j < tmpIdx.Length; j++)
{
csrIdx[i + j] = tmpIdx[j];
}
currentIdx++;
if (currentIdx % 5000 == 0)
{
Console.WriteLine($"Generated {currentIdx} candidates...");
}
}
// add the end of matrix as specified in CSR format
csrRowoffsets[currentIdx++] = nrCandidates * 100;

// generate spectra vectors
var spectraValues = new int[nrSpectra * 500];
var spectraIdx = new int[nrSpectra];
currentIdx = 0;
for (int i = 0; i < spectraValues.Length; i += 500)
{
spectraIdx[currentIdx] = i;
var tmpValues = new int[500];
for (int j = 0; j < tmpValues.Length; j++)
{
var val = r.Next(ENCODING_SIZE);
while (Array.Exists(tmpValues, x => x == val))
{
val = r.Next(ENCODING_SIZE);
}
tmpValues[j] = val;
}
Array.Sort(tmpValues);
for (int j = 0; j < tmpValues.Length; j++)
{
spectraValues[i + j] = tmpValues[j];
}
currentIdx++;
}

// time c++ call
var sw = Stopwatch.StartNew();

// get pointer addresses and call c++ function
var csrRowoffsetsLoc = GCHandle.Alloc(csrRowoffsets, GCHandleType.Pinned);
var csrIdxLoc = GCHandle.Alloc(csrIdx, GCHandleType.Pinned);
var sValuesLoc = GCHandle.Alloc(spectraValues, GCHandleType.Pinned);
var sIdxLoc = GCHandle.Alloc(spectraIdx, GCHandleType.Pinned);
var resultArray = new int[spectraIdx.Length * topN];
var memStat = 1;
try
{
IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();

IntPtr result = findTopCandidatesCuda(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
csrRowoffsets.Length, csrIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02);

Marshal.Copy(result, resultArray, 0, spectraIdx.Length * topN);

memStat = releaseMemoryCuda(result);
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong:");
Console.WriteLine(ex.ToString());
}
finally
{
if (csrRowoffsetsLoc.IsAllocated) { csrRowoffsetsLoc.Free(); }
if (csrIdxLoc.IsAllocated) { csrIdxLoc.Free(); }
if (sValuesLoc.IsAllocated) { sValuesLoc.Free(); }
if (sIdxLoc.IsAllocated) { sIdxLoc.Free(); }
}

// end time c++ call
sw.Stop();

for (int i = 0; i < topN; i++)
{
Console.WriteLine(resultArray[i]);
}

Console.WriteLine($"MemStat: {memStat}");
Console.WriteLine("Time for candidate search (SpMV):");
Console.WriteLine(sw.Elapsed.TotalSeconds.ToString());

//
GC.Collect();
GC.WaitForPendingFinalizers();

return 0;
}
}
}
Loading

0 comments on commit 22a27f1

Please sign in to comment.