Skip to content

SpGeMM #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions benchmarks/GraphBLAS-sharp.Benchmarks/Matrix/SpGeMM/Expand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Benchmarks<'elem when 'elem : struct>(
let mutable firstMatrixHost = Unchecked.defaultof<_>
let mutable secondMatrixHost = Unchecked.defaultof<_>

member val ResultMatrix = Unchecked.defaultof<ClMatrix<'elem>> with get, set
member val ResultMatrix = Unchecked.defaultof<ClMatrix.COO<'elem> option> with get, set

[<ParamsSource("AvailableContexts")>]
member val OclContextInfo = Unchecked.defaultof<Utils.BenchmarkContext * int> with get, set
Expand Down Expand Up @@ -85,7 +85,9 @@ type Benchmarks<'elem when 'elem : struct>(
secondMatrix.Dispose this.Processor

member this.ClearResult() =
this.ResultMatrix.Dispose this.Processor
match this.ResultMatrix with
| Some matrix -> matrix.Dispose this.Processor
| None -> ()

member this.ReadMatrices() =
firstMatrixHost <- this.ReadMatrix this.InputMatrixReader
Expand Down
339 changes: 216 additions & 123 deletions src/GraphBLAS-sharp.Backend/Common/ClArray.fs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/COO/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
open GraphBLAS.FSharp.Backend.Objects.ClCell
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
open GraphBLAS.FSharp.Backend.Objects.ClContext

module Matrix =
let map = Map.run
Expand Down
133 changes: 126 additions & 7 deletions src/GraphBLAS-sharp.Backend/Matrix/CSR/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,138 @@ open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions


module Matrix =
let expandRowPointers (clContext: ClContext) workGroupSize =

let kernel =
<@ fun (ndRange: Range1D) columnsLength pointersLength (pointers: ClArray<int>) (results: ClArray<int>) ->

let gid = ndRange.GlobalID0

if gid < columnsLength then
let result =
(%Search.Bin.lowerBound) pointersLength gid pointers

results.[gid] <- result - 1 @>

let program = clContext.Compile kernel

fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->

let rows =
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, matrix.Columns.Length)

let kernel = program.GetKernel()

let ndRange =
Range1D.CreateValid(matrix.Columns.Length, workGroupSize)

processor.Post(
Msg.MsgSetArguments
(fun () ->
kernel.KernelFunc
ndRange
matrix.Columns.Length
matrix.RowPointers.Length
matrix.RowPointers
rows)
)

processor.Post(Msg.CreateRunMsg<_, _> kernel)

rows

let subRows (clContext: ClContext) workGroupSize =

let kernel =
<@ fun (ndRange: Range1D) resultLength sourceRow pointersLength (pointers: ClArray<int>) (results: ClArray<int>) ->

let gid = ndRange.GlobalID0

let shift = pointers.[sourceRow]
let shiftedId = gid + shift

if gid < resultLength then
let result =
(%Search.Bin.lowerBound) pointersLength shiftedId pointers

results.[gid] <- result - 1 @>

let program = clContext.Compile kernel

let blit = ClArray.blit clContext workGroupSize

let blitData = ClArray.blit clContext workGroupSize

fun (processor: MailboxProcessor<_>) allocationMode startIndex count (matrix: ClMatrix.CSR<'a>) ->
if count <= 0 then
failwith "Count must be greater than zero"

if startIndex < 0 then
failwith "startIndex must be greater then zero"

if startIndex + count > matrix.RowCount then
failwith "startIndex and count sum is larger than the matrix row count"

// extract rows
let rowPointers = matrix.RowPointers.ToHost processor

let resultLength =
rowPointers.[startIndex + count]
- rowPointers.[startIndex]

let rows =
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)

let kernel = program.GetKernel()

let ndRange =
Range1D.CreateValid(matrix.Columns.Length, workGroupSize)

processor.Post(
Msg.MsgSetArguments
(fun () ->
kernel.KernelFunc
ndRange
resultLength
startIndex
matrix.RowPointers.Length
matrix.RowPointers
rows)
)

processor.Post(Msg.CreateRunMsg<_, _> kernel)

let startPosition = rowPointers.[startIndex]

// extract values
let values =
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)

blitData processor matrix.Values startPosition values 0 resultLength

// extract indices
let columns =
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)

blit processor matrix.Columns startPosition columns 0 resultLength

{ Context = clContext
RowCount = matrix.RowCount
ColumnCount = matrix.ColumnCount
Rows = rows
Columns = columns
Values = values }

let toCOO (clContext: ClContext) workGroupSize =
let prepare =
Common.expandRowPointers clContext workGroupSize
expandRowPointers clContext workGroupSize

let copy = ClArray.copy clContext workGroupSize

let copyData = ClArray.copy clContext workGroupSize

fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
let rows =
prepare processor allocationMode matrix.RowPointers matrix.Columns.Length matrix.RowCount
let rows = prepare processor allocationMode matrix

let cols =
copy processor allocationMode matrix.Columns
Expand All @@ -40,11 +161,10 @@ module Matrix =

let toCOOInPlace (clContext: ClContext) workGroupSize =
let prepare =
Common.expandRowPointers clContext workGroupSize
expandRowPointers clContext workGroupSize

fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
let rows =
prepare processor allocationMode matrix.RowPointers matrix.Columns.Length matrix.RowCount
let rows = prepare processor allocationMode matrix

processor.Post(Msg.CreateFreeMsg(matrix.RowPointers))

Expand Down Expand Up @@ -92,7 +212,6 @@ module Matrix =
let toCSRInPlace =
COO.Matrix.toCSRInPlace clContext workGroupSize


fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
toCOO queue allocationMode matrix
|> transposeInPlace queue
Expand Down
41 changes: 3 additions & 38 deletions src/GraphBLAS-sharp.Backend/Matrix/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ open Brahma.FSharp
open GraphBLAS.FSharp.Backend.Common
open GraphBLAS.FSharp.Backend.Objects.ClContext
open GraphBLAS.FSharp.Backend.Objects.ClCell
open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend.Quotes

module Common =
module internal Common =
///<param name="clContext">.</param>
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
let setPositions<'a when 'a: struct> (clContext: ClContext) workGroupSize =
Expand Down Expand Up @@ -40,40 +42,3 @@ module Common =
valuesScatter processor positions allValues resultValues

resultRows, resultColumns, resultValues, resultLength

let expandRowPointers (clContext: ClContext) workGroupSize =

let expandRowPointers =
<@ fun (ndRange: Range1D) (rowPointers: ClArray<int>) (rowCount: int) (rows: ClArray<int>) ->

let i = ndRange.GlobalID0

if i < rowCount then
let rowPointer = rowPointers.[i]

if rowPointer <> rowPointers.[i + 1] then
rows.[rowPointer] <- i @>

let program = clContext.Compile expandRowPointers

let create =
ClArray.zeroCreate clContext workGroupSize

let scan =
PrefixSum.runIncludeInPlace <@ max @> clContext workGroupSize

fun (processor: MailboxProcessor<_>) allocationMode (rowPointers: ClArray<int>) nnz rowCount ->

let rows = create processor allocationMode nnz

let kernel = program.GetKernel()

let ndRange =
Range1D.CreateValid(rowCount, workGroupSize)

processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange rowPointers rowCount rows))
processor.Post(Msg.CreateRunMsg<_, _> kernel)

(scan processor rows 0).Free processor

rows
15 changes: 12 additions & 3 deletions src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open GraphBLAS.FSharp.Backend.Matrix
open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
open GraphBLAS.FSharp.Backend.Vector
open GraphBLAS.FSharp.Backend.Objects.ClContext

module Matrix =
let copy (clContext: ClContext) workGroupSize =
Expand Down Expand Up @@ -413,11 +414,19 @@ module Matrix =
=

let run =
SpGeMM.Expand.run clContext workGroupSize opAdd opMul
SpGeMM.Expand.run opAdd opMul clContext workGroupSize

fun (processor: MailboxProcessor<_>) allocationMode (leftMatrix: ClMatrix<'a>) (rightMatrix: ClMatrix<'b>) ->
match leftMatrix, rightMatrix with
| ClMatrix.CSR leftMatrix, ClMatrix.CSR rightMatrix ->
ClMatrix.LIL
<| run processor allocationMode leftMatrix rightMatrix
let allocCapacity =
List.max [ sizeof<'a>
sizeof<'c>
sizeof<'b> ]
* 1<Byte>

let resultCapacity =
(clContext.MaxMemAllocSize / allocCapacity) / 3

run processor allocationMode resultCapacity leftMatrix rightMatrix
| _ -> failwith "Matrix formats are not matching"
Loading