Skip to content

Commit

Permalink
Merge pull request #8 from hgb-bin-proteomics/develop
Browse files Browse the repository at this point in the history
CUDA SpMM implementation
  • Loading branch information
michabirklbauer authored Jul 25, 2023
2 parents 645a6a1 + ad0f06e commit d76321b
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 25 deletions.
19 changes: 19 additions & 0 deletions DataLoader/Compare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, Random r)
var resultArrayEigenB = 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];
var memStat = 1;
try
{
Expand Down Expand Up @@ -140,6 +141,20 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, Random r)

sw4.Stop();

var sw5 = Stopwatch.StartNew();

IntPtr resultCudaB2 = findTopCandidatesCudaBatched2(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
csrRowoffsets.Length, csrIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02, NORMALIZE, USE_GAUSSIAN, BATCH_SIZE, 0);

Marshal.Copy(resultCudaB2, resultArrayCudaB2, 0, spectraIdx.Length * topN);

memStat = releaseMemoryCuda(resultCudaB2);

sw5.Stop();

Console.WriteLine("Time for candidate search Eigen SpMV:");
Console.WriteLine(sw1.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Eigen SpMM:");
Expand All @@ -148,6 +163,8 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, Random r)
Console.WriteLine(sw3.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Cuda SpGEMM:");
Console.WriteLine(sw4.Elapsed.TotalSeconds.ToString());
Console.WriteLine("Time for candidate search Cuda SpMM:");
Console.WriteLine(sw5.Elapsed.TotalSeconds.ToString());
}
catch (Exception ex)
{
Expand All @@ -172,6 +189,7 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, Random r)
Console.WriteLine(resultArrayEigenB[i]);
Console.WriteLine(resultArrayCuda[i]);
Console.WriteLine(resultArrayCudaB[i]);
Console.WriteLine(resultArrayCudaB2[i]);
Console.WriteLine("-----");
}

Expand All @@ -183,6 +201,7 @@ public static int Compare(int nrCandidates, int nrSpectra, int topN, Random r)
Console.WriteLine(resultArrayEigenB[i]);
Console.WriteLine(resultArrayCuda[i]);
Console.WriteLine(resultArrayCudaB[i]);
Console.WriteLine(resultArrayCudaB2[i]);
Console.WriteLine("-----");
}

Expand Down
72 changes: 53 additions & 19 deletions DataLoader/Cuda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ private static extern IntPtr findTopCandidatesCudaBatched(IntPtr cR, IntPtr cI,
int batchSize,
int verbose);

[DllImport(dllCuda, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr findTopCandidatesCudaBatched2(IntPtr cR, IntPtr cI,
IntPtr sV, IntPtr sI,
int cRL, int cNNZ,
int sVL, int sIL,
int n, float tolerance,
bool normalize, bool gaussianTol,
int batchSize,
int verbose);

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

public static int Cuda(int nrCandidates, int nrSpectra, int topN, Random r, bool batched)
public static int Cuda(int nrCandidates, int nrSpectra, int topN, Random r, bool batched, int batchMode)
{
// generate candidate vectors
var csrRowoffsets = new int[nrCandidates + 1];
Expand Down Expand Up @@ -119,23 +129,46 @@ public static int Cuda(int nrCandidates, int nrSpectra, int topN, Random r, bool
}
else
{
IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();

IntPtr result = findTopCandidatesCudaBatched(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
csrRowoffsets.Length, csrIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02,
NORMALIZE, USE_GAUSSIAN,
BATCH_SIZE,
1000);

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

memStat = releaseMemoryCuda(result);
if (batchMode == 2)
{
IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();

IntPtr result = findTopCandidatesCudaBatched2(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
csrRowoffsets.Length, csrIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02,
NORMALIZE, USE_GAUSSIAN,
BATCH_SIZE,
1000);

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

memStat = releaseMemoryCuda(result);
}
else
{
IntPtr csrRowoffsetsPtr = csrRowoffsetsLoc.AddrOfPinnedObject();
IntPtr csrIdxPtr = csrIdxLoc.AddrOfPinnedObject();
IntPtr sValuesPtr = sValuesLoc.AddrOfPinnedObject();
IntPtr sIdxPtr = sIdxLoc.AddrOfPinnedObject();

IntPtr result = findTopCandidatesCudaBatched(csrRowoffsetsPtr, csrIdxPtr,
sValuesPtr, sIdxPtr,
csrRowoffsets.Length, csrIdx.Length,
spectraValues.Length, spectraIdx.Length,
topN, (float) 0.02,
NORMALIZE, USE_GAUSSIAN,
BATCH_SIZE,
1000);

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

memStat = releaseMemoryCuda(result);
}
}
}
catch (Exception ex)
Expand All @@ -160,7 +193,8 @@ public static int Cuda(int nrCandidates, int nrSpectra, int topN, Random r, bool
}

Console.WriteLine($"MemStat: {memStat}");
Console.WriteLine("Time for candidate search (SpMV):");
var mode = batched ? batchMode == 2 ? "(SpMM)" : "(SpGEMM)" : "(SpMV)";
Console.WriteLine($"Time for candidate search {mode}:");
Console.WriteLine(sw.Elapsed.TotalSeconds.ToString());

//
Expand Down
9 changes: 7 additions & 2 deletions DataLoader/DataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ public static void Main(string[] args)
// call subroutine for specified mode
if (mode == "Cuda")
{
var status = Cuda(nrCandidates, nrSpectra, topN, r, false);
var status = Cuda(nrCandidates, nrSpectra, topN, r, false, 1);
Console.WriteLine($"Cuda routine exited with status: {status}");
}
else if (mode == "CudaB")
{
var status = Cuda(nrCandidates, nrSpectra, topN, r, true);
var status = Cuda(nrCandidates, nrSpectra, topN, r, true, 1);
Console.WriteLine($"Cuda routine exited with status: {status}");
}
else if (mode == "CudaBAlt")
{
var status = Cuda(nrCandidates, nrSpectra, topN, r, true, 2);
Console.WriteLine($"Cuda routine exited with status: {status}");
}
else if (mode == "Eigen")
Expand Down
Loading

0 comments on commit d76321b

Please sign in to comment.