From 7680fb41d70baa50010fda73a00d5744f6e18558 Mon Sep 17 00:00:00 2001 From: kirillgarbar Date: Sun, 29 Oct 2023 23:20:55 +0300 Subject: [PATCH] SpMV simple temp --- .../Algorithms/Algorithms.fs | 2 + src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs | 67 ++++++++++++++++- .../Operations/Operations.fs | 30 ++++++++ .../Operations/SpMV.fs | 73 ++++++++++++++++++- 4 files changed, 170 insertions(+), 2 deletions(-) diff --git a/src/GraphBLAS-sharp.Backend/Algorithms/Algorithms.fs b/src/GraphBLAS-sharp.Backend/Algorithms/Algorithms.fs index e758cfdf..6c58c814 100644 --- a/src/GraphBLAS-sharp.Backend/Algorithms/Algorithms.fs +++ b/src/GraphBLAS-sharp.Backend/Algorithms/Algorithms.fs @@ -10,6 +10,8 @@ module Algorithms = let singleSourceSPLA = BFS.singleSourceSPLA + let singleSourceSimple = BFS.singleSourceSimple + let singleSourceSparse = BFS.singleSourceSparse let singleSourcePushPull = BFS.singleSourcePushPull diff --git a/src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs b/src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs index 1b6e90aa..684e1b48 100644 --- a/src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs +++ b/src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs @@ -41,7 +41,72 @@ module internal BFS = zeroCreate queue DeviceOnly vertexCount Dense let mutable front = - ofList queue HostInterop Dense vertexCount [ source, 1 ] + ofList queue DeviceOnly Dense vertexCount [ source, 1 ] + + let mutable next = + zeroCreate queue DeviceOnly vertexCount Dense + + let mutable level = 0 + let mutable stop = false + + while not stop do + level <- level + 1 + + //Assigning new level values + fillSubVectorTo queue levels front level + + //Getting new frontier + spMVInPlace queue matrix front next + + maskComplementedInPlace queue next levels + + //Checking if front is empty + stop <- + not + <| (containsNonZero queue next).ToHostAndFree queue + + let temp = front + front <- next + next <- temp + + + front.Dispose queue + next.Dispose queue + + levels + + let singleSourceSimple + (add: Expr int option -> int option>) + (mul: Expr<'a option -> int option -> int option>) + (clContext: ClContext) + workGroupSize + = + + let spMVInPlace = + Operations.SpMVSimpleTo add mul clContext workGroupSize + + let zeroCreate = + Vector.zeroCreate clContext workGroupSize + + let ofList = Vector.ofList clContext workGroupSize + + let maskComplementedInPlace = + Vector.map2InPlace Mask.complementedOp clContext workGroupSize + + let fillSubVectorTo = + Vector.assignByMaskInPlace Mask.assign clContext workGroupSize + + let containsNonZero = + Vector.exists Predicates.isSome clContext workGroupSize + + fun (queue: MailboxProcessor) (matrix: ClMatrix<'a>) (source: int) -> + let vertexCount = matrix.RowCount + + let levels = + zeroCreate queue DeviceOnly vertexCount Dense + + let mutable front = + ofList queue DeviceOnly Dense vertexCount [ source, 1 ] let mutable next = zeroCreate queue DeviceOnly vertexCount Dense diff --git a/src/GraphBLAS-sharp.Backend/Operations/Operations.fs b/src/GraphBLAS-sharp.Backend/Operations/Operations.fs index 08a83db1..59bed536 100644 --- a/src/GraphBLAS-sharp.Backend/Operations/Operations.fs +++ b/src/GraphBLAS-sharp.Backend/Operations/Operations.fs @@ -241,6 +241,36 @@ module Operations = | ClMatrix.CSR m, ClVector.Dense v, ClVector.Dense r -> runTo queue m v r | _ -> failwith "Not implemented yet" + let SpMVSimpleTo + (add: Expr<'c option -> 'c option -> 'c option>) + (mul: Expr<'a option -> 'b option -> 'c option>) + (clContext: ClContext) + workGroupSize + = + + let runTo = + Backend.Operations.SpMV.runSimpleTo add mul clContext workGroupSize + + fun (queue: MailboxProcessor<_>) (matrix: ClMatrix<'a>) (vector: ClVector<'b>) (result: ClVector<'c>) -> + match matrix, vector, result with + | ClMatrix.CSR m, ClVector.Dense v, ClVector.Dense r -> runTo queue m v r + | _ -> failwith "Not implemented yet" + + let SpMVSimple + (add: Expr<'c option -> 'c option -> 'c option>) + (mul: Expr<'a option -> 'b option -> 'c option>) + (clContext: ClContext) + workGroupSize + = + + let run = + Backend.Operations.SpMV.runSimple add mul clContext workGroupSize + + fun (queue: MailboxProcessor<_>) allocationFlag (matrix: ClMatrix<'a>) (vector: ClVector<'b>) -> + match matrix, vector with + | ClMatrix.CSR m, ClVector.Dense v -> run queue allocationFlag m v |> ClVector.Dense + | _ -> failwith "Not implemented yet" + /// /// CSR Matrix - dense vector multiplication. /// diff --git a/src/GraphBLAS-sharp.Backend/Operations/SpMV.fs b/src/GraphBLAS-sharp.Backend/Operations/SpMV.fs index aa1baf7d..0bc2ca66 100644 --- a/src/GraphBLAS-sharp.Backend/Operations/SpMV.fs +++ b/src/GraphBLAS-sharp.Backend/Operations/SpMV.fs @@ -48,7 +48,6 @@ module internal SpMV = while i < workEnd do let columnIndex = matrixColumns.[i] let mulRes = (%mul) (Some matrixValues.[i]) vectorValues.[columnIndex] // Brahma exception - let res = (%add) sum mulRes sum <- res i <- i + localSize @@ -116,6 +115,78 @@ module internal SpMV = result + let runSimpleTo + (add: Expr<'c option -> 'c option -> 'c option>) + (mul: Expr<'a option -> 'b option -> 'c option>) + (clContext: ClContext) + workGroupSize + = + + let spmv = + <@ fun (ndRange: Range1D) + (numberOfRows: int) + (matrixPtr: ClArray) + (matrixColumns: ClArray) + (matrixValues: ClArray<'a>) + (vectorValues: ClArray<'b option>) + (resultValues: ClArray<'c option>) -> + + let gid = ndRange.GlobalID0 // id of row + + if gid < numberOfRows then + let rowStart = matrixPtr.[gid] + let rowEnd = matrixPtr.[gid + 1] + + let mutable sum: 'c option = None + + for i in rowStart .. rowEnd - 1 do + let columnIndex = matrixColumns.[i] + let mulRes = (%mul) (Some matrixValues.[i]) vectorValues.[columnIndex] // Brahma exception + let res = (%add) sum mulRes + sum <- res + + resultValues.[gid] <- sum @> + + let spmv = clContext.Compile spmv + + fun (queue: MailboxProcessor<_>) (matrix: ClMatrix.CSR<'a>) (vector: ClArray<'b option>) (result: ClArray<'c option>) -> + + let ndRange = Range1D.CreateValid(matrix.RowCount, workGroupSize) + + let kernel = spmv.GetKernel() + + queue.Post( + Msg.MsgSetArguments + (fun () -> + kernel.KernelFunc + ndRange + matrix.RowCount + matrix.RowPointers + matrix.Columns + matrix.Values + vector + result) + ) + + queue.Post(Msg.CreateRunMsg<_, _>(kernel)) + + let runSimple + (add: Expr<'c option -> 'c option -> 'c option>) + (mul: Expr<'a option -> 'b option -> 'c option>) + (clContext: ClContext) + workGroupSize + = + let runTo = runSimpleTo add mul clContext workGroupSize + + fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) (vector: ClArray<'b option>) -> + + let result = + clContext.CreateClArrayWithSpecificAllocationMode<'c option>(allocationMode, matrix.RowCount) + + runTo queue matrix vector result + + result + let runTo (add: Expr<'c option -> 'c option -> 'c option>) (mul: Expr<'a option -> 'b option -> 'c option>)