Skip to content

Commit

Permalink
Merge pull request #13 from hgb-bin-proteomics/develop
Browse files Browse the repository at this point in the history
dll v1.5.0 eigen
  • Loading branch information
michabirklbauer authored Sep 6, 2023
2 parents 52f87e9 + 6e666bc commit 416a3fe
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 50 deletions.
62 changes: 48 additions & 14 deletions DataLoader/Compare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi
var sValuesLoc = GCHandle.Alloc(spectraValues, GCHandleType.Pinned);
var sIdxLoc = GCHandle.Alloc(spectraIdx, GCHandleType.Pinned);
var resultArrayEigen = new int[spectraIdx.Length * topN];
var resultArrayEigen2 = new int[spectraIdx.Length * topN];
var resultArrayEigenB = new int[spectraIdx.Length * topN];
var resultArrayEigenB2 = new int[spectraIdx.Length * topN];
var resultArrayCuda = new int[spectraIdx.Length * topN];
var resultArrayCudaB = new int[spectraIdx.Length * topN];
var resultArrayCudaB2 = new int[spectraIdx.Length * topN];
Expand All @@ -93,7 +95,7 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi

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

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

Expand All @@ -103,17 +105,41 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi

var sw2 = Stopwatch.StartNew();

IntPtr resultEigen2 = findTopCandidates2(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02, NORMALIZE, USE_GAUSSIAN, 0, 0);

Marshal.Copy(resultEigen2, resultArrayEigen2, 0, spectraIdx.Length * topN);

memStat = releaseMemory(resultEigen2);

sw2.Stop();

var sw3 = Stopwatch.StartNew();

IntPtr resultEigenB = findTopCandidatesBatched(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0);
topN, (float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);

Marshal.Copy(resultEigenB, resultArrayEigenB, 0, spectraIdx.Length * topN);

memStat = releaseMemory(resultEigenB);

sw2.Stop();
sw3.Stop();

var sw3 = Stopwatch.StartNew();
var sw4 = Stopwatch.StartNew();

IntPtr resultEigenB2 = findTopCandidatesBatched2(cValuesPtr, cIdxPtr, sValuesPtr, sIdxPtr,
candidateValues.Length, candidatesIdx.Length, spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02, NORMALIZE, USE_GAUSSIAN, batchSize, 0, 0);

Marshal.Copy(resultEigenB2, resultArrayEigenB2, 0, spectraIdx.Length * topN);

memStat = releaseMemory(resultEigenB2);

sw4.Stop();

var sw5 = Stopwatch.StartNew();

IntPtr resultCuda = findTopCandidatesCuda(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
Expand All @@ -125,9 +151,9 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi

memStat = releaseMemoryCuda(resultCuda);

sw3.Stop();
sw5.Stop();

var sw4 = Stopwatch.StartNew();
var sw6 = Stopwatch.StartNew();

IntPtr resultCudaB = findTopCandidatesCudaBatched(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
Expand All @@ -139,9 +165,9 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi

memStat = releaseMemoryCuda(resultCudaB);

sw4.Stop();
sw6.Stop();

var sw5 = Stopwatch.StartNew();
var sw7 = Stopwatch.StartNew();

IntPtr resultCudaB2 = findTopCandidatesCudaBatched2(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
Expand All @@ -153,18 +179,22 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi

memStat = releaseMemoryCuda(resultCudaB2);

sw5.Stop();
sw7.Stop();

Console.WriteLine("Time for candidate search Eigen SpMV:");
Console.WriteLine("Time for candidate search Eigen SpM*SpV:");
Console.WriteLine(sw1.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Eigen SpMM:");
Console.WriteLine("Time for candidate search Eigen SpM*V:");
Console.WriteLine(sw2.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Cuda SpMV:");
Console.WriteLine("Time for candidate search Eigen SpM*SpM:");
Console.WriteLine(sw3.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Cuda SpGEMM:");
Console.WriteLine("Time for candidate search Eigen SpM*M:");
Console.WriteLine(sw4.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Cuda SpMM:");
Console.WriteLine("Time for candidate search Cuda SpMV:");
Console.WriteLine(sw5.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Cuda SpGEMM:");
Console.WriteLine(sw6.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Cuda SpMM:");
Console.WriteLine(sw7.Elapsed.TotalSeconds.ToString());
}
catch (Exception ex)
{
Expand All @@ -186,7 +216,9 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi
for (int i = 0; i < topN; i++)
{
Console.WriteLine(resultArrayEigen[i]);
Console.WriteLine(resultArrayEigen2[i]);
Console.WriteLine(resultArrayEigenB[i]);
Console.WriteLine(resultArrayEigenB2[i]);
Console.WriteLine(resultArrayCuda[i]);
Console.WriteLine(resultArrayCudaB[i]);
Console.WriteLine(resultArrayCudaB2[i]);
Expand All @@ -198,7 +230,9 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, int batchSi
for (int i = spectraIdx.Length * topN - topN; i < spectraIdx.Length * topN; i++)
{
Console.WriteLine(resultArrayEigen[i]);
Console.WriteLine(resultArrayEigen2[i]);
Console.WriteLine(resultArrayEigenB[i]);
Console.WriteLine(resultArrayEigenB2[i]);
Console.WriteLine(resultArrayCuda[i]);
Console.WriteLine(resultArrayCudaB[i]);
Console.WriteLine(resultArrayCudaB2[i]);
Expand Down
16 changes: 13 additions & 3 deletions DataLoader/DataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,22 @@ public static void Main(string[] args)
}
else if (mode == "Eigen")
{
var status = Eigen(nrCandidates, nrSpectra, topN, batchSize, r, false);
var status = Eigen(nrCandidates, nrSpectra, topN, batchSize, r, false, false);
Console.WriteLine($"Eigen routine exited with status: {status}");
}
else if (mode == "EigenB")
{
var status = Eigen(nrCandidates, nrSpectra, topN, batchSize, r, true);
var status = Eigen(nrCandidates, nrSpectra, topN, batchSize, r, true, false);
Console.WriteLine($"Eigen routine exited with status: {status}");
}
else if (mode == "EigenS")
{
var status = Eigen(nrCandidates, nrSpectra, topN, batchSize, r, false, true);
Console.WriteLine($"Eigen routine exited with status: {status}");
}
else if (mode == "EigenSB")
{
var status = Eigen(nrCandidates, nrSpectra, topN, batchSize, r, true, true);
Console.WriteLine($"Eigen routine exited with status: {status}");
}
else if (mode == "Compare")
Expand All @@ -82,7 +92,7 @@ public static void Main(string[] args)
}
else
{
Console.WriteLine("No mode selected, has to be one of: Eigen, EigenB, Cuda, Compare.");
Console.WriteLine("No mode selected, has to be one of: Eigen(S), Eigen(S)B, Cuda, Compare.");
}

Console.WriteLine("Done!");
Expand Down
67 changes: 60 additions & 7 deletions DataLoader/Eigen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private static extern IntPtr findTopCandidates(IntPtr cV, IntPtr cI,
int sVL, int sIL,
int n, float tolerance,
bool normalize, bool gaussianTol,
int verbose);
int cores, int verbose);

[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr findTopCandidatesBatched(IntPtr cV, IntPtr cI,
Expand All @@ -23,12 +23,31 @@ private static extern IntPtr findTopCandidatesBatched(IntPtr cV, IntPtr cI,
int n, float tolerance,
bool normalize, bool gaussianTol,
int batchSize,
int verbose);
int cores, int verbose);

[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr findTopCandidates2(IntPtr cV, IntPtr cI,
IntPtr sV, IntPtr sI,
int cVL, int cIL,
int sVL, int sIL,
int n, float tolerance,
bool normalize, bool gaussianTol,
int cores, int verbose);

[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr findTopCandidatesBatched2(IntPtr cV, IntPtr cI,
IntPtr sV, IntPtr sI,
int cVL, int cIL,
int sVL, int sIL,
int n, float tolerance,
bool normalize, bool gaussianTol,
int batchSize,
int cores, int verbose);

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

public static int Eigen(int nrCandidates, int nrSpectra, int topN, int batchSize, Random r, bool batched)
public static int Eigen(int nrCandidates, int nrSpectra, int topN, int batchSize, Random r, bool batched, bool sparse)
{
// generate candidate vectors
var candidateValues = new int[nrCandidates * 100];
Expand Down Expand Up @@ -101,21 +120,35 @@ public static int Eigen(int nrCandidates, int nrSpectra, int topN, int batchSize
IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();

if (!batched)
if (!batched && sparse)
{
IntPtr result = findTopCandidates(cValuesPtr, cIdxPtr,
sValuesPtr, sIdxPtr,
candidateValues.Length, candidatesIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02,
NORMALIZE, USE_GAUSSIAN,
1000);
0, 1000);

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

memStat = releaseMemory(result);
}
else
else if (!batched && !sparse)
{
IntPtr result = findTopCandidates2(cValuesPtr, cIdxPtr,
sValuesPtr, sIdxPtr,
candidateValues.Length, candidatesIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02,
NORMALIZE, USE_GAUSSIAN,
0, 1000);

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

memStat = releaseMemory(result);
}
else if(batched && sparse)
{
IntPtr result = findTopCandidatesBatched(cValuesPtr, cIdxPtr,
sValuesPtr, sIdxPtr,
Expand All @@ -124,12 +157,32 @@ public static int Eigen(int nrCandidates, int nrSpectra, int topN, int batchSize
topN, (float) 0.02,
NORMALIZE, USE_GAUSSIAN,
batchSize,
1000);
0, 1000);

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

memStat = releaseMemory(result);
}
else if(batched && !sparse)
{
IntPtr result = findTopCandidatesBatched2(cValuesPtr, cIdxPtr,
sValuesPtr, sIdxPtr,
candidateValues.Length, candidatesIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02,
NORMALIZE, USE_GAUSSIAN,
batchSize,
0, 1000);

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

memStat = releaseMemory(result);
}
else
{
Console.WriteLine("Impossible case!");
}

}
catch (Exception ex)
{
Expand Down
Binary file modified DataLoaderExecutable/DataLoader.dll
Binary file not shown.
Binary file modified DataLoaderExecutable/VectorSearch.dll
Binary file not shown.
Loading

0 comments on commit 416a3fe

Please sign in to comment.