Skip to content

Commit

Permalink
SpMV simple temp
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgarbar committed Oct 29, 2023
1 parent 53d76c9 commit 7680fb4
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/GraphBLAS-sharp.Backend/Algorithms/Algorithms.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Algorithms =

let singleSourceSPLA = BFS.singleSourceSPLA

let singleSourceSimple = BFS.singleSourceSimple

let singleSourceSparse = BFS.singleSourceSparse

let singleSourcePushPull = BFS.singleSourcePushPull
Expand Down
67 changes: 66 additions & 1 deletion src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> 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<Msg>) (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
Expand Down
30 changes: 30 additions & 0 deletions src/GraphBLAS-sharp.Backend/Operations/Operations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

/// <summary>
/// CSR Matrix - dense vector multiplication.
/// </summary>
Expand Down
73 changes: 72 additions & 1 deletion src/GraphBLAS-sharp.Backend/Operations/SpMV.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<int>)
(matrixColumns: ClArray<int>)
(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>)
Expand Down

0 comments on commit 7680fb4

Please sign in to comment.