Skip to content

Kronecker by rows #78

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

Closed
Closed
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
34 changes: 34 additions & 0 deletions src/GraphBLAS-sharp.Backend/Common/ClArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ module ClArray =

result

let mapWithValue<'a, 'b, 'c> (clContext: ClContext) workGroupSize (op: Expr<'a -> 'b -> 'c>) =

let map =
<@ fun (ndRange: Range1D) length (value: ClCell<'a>) (inputArray: ClArray<'b>) (result: ClArray<'c>) ->

let gid = ndRange.GlobalID0

if gid < length then
result.[gid] <- (%op) value.Value inputArray.[gid] @>

let kernel = clContext.Compile map

fun (processor: MailboxProcessor<_>) allocationMode (value: 'a) (inputArray: ClArray<'b>) ->

let valueClCell = clContext.CreateClCell value

let result =
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, inputArray.Length)

let ndRange =
Range1D.CreateValid(inputArray.Length, workGroupSize)

let kernel = kernel.GetKernel()

processor.Post(
Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange inputArray.Length valueClCell inputArray result)
)

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

valueClCell.Free processor

result

let map2InPlace<'a, 'b, 'c> (map: Expr<'a -> 'b -> 'c>) (clContext: ClContext) workGroupSize =

let kernel =
Expand Down
2 changes: 2 additions & 0 deletions src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Compile Include="Vector/Sparse/Common.fs" />
<Compile Include="Vector/Sparse/Merge.fs" />
<Compile Include="Vector/Sparse/Map2.fs" />
<Compile Include="Vector/Sparse/Map.fs" />
<Compile Include="Vector/Sparse/Vector.fs" />
<Compile Include="Vector/SpMV.fs" />
<Compile Include="Vector/Vector.fs" />
Expand All @@ -58,6 +59,7 @@
<Compile Include="Matrix/LIL/Matrix.fs" />
<Compile Include="Matrix/SpGeMM/Expand.fs" />
<Compile Include="Matrix/SpGeMM/Masked.fs" />
<Compile Include="Matrix/Kronecker/ByRows.fs" />
<Compile Include="Matrix/Matrix.fs" />

<Compile Include="Algorithms/BFS.fs" />
Expand Down
111 changes: 111 additions & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/CSR/Map.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open GraphBLAS.FSharp.Backend.Quotes
open GraphBLAS.FSharp.Backend.Matrix
open GraphBLAS.FSharp.Backend.Matrix.COO
open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend.Objects.ClCell
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
open GraphBLAS.FSharp.Backend.Objects.ClContext

Expand Down Expand Up @@ -112,3 +113,113 @@ module internal Map =
Rows = resultRows
Columns = resultColumns
Values = resultValues }

module WithValue =
let preparePositions<'a, 'b, 'c when 'b: struct> (clContext: ClContext) workGroupSize op =

let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) =
<@ fun (ndRange: Range1D) (operand: ClCell<'a option>) rowCount columnCount (values: ClArray<'b>) (rowPointers: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'c>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->

let gid = ndRange.GlobalID0

if gid < rowCount * columnCount then

let columnIndex = gid % columnCount
let rowIndex = gid / columnCount

let startIndex = rowPointers.[rowIndex]
let lastIndex = rowPointers.[rowIndex + 1] - 1

let value =
(%Search.Bin.inRange) startIndex lastIndex columnIndex columns values

match (%op) operand.Value value with
| Some resultValue ->
resultValues.[gid] <- resultValue
resultRows.[gid] <- rowIndex
resultColumns.[gid] <- columnIndex

resultBitmap.[gid] <- 1
| None -> resultBitmap.[gid] <- 0 @>

let kernel = clContext.Compile <| preparePositions op

fun (processor: MailboxProcessor<_>) (operand: ClCell<'a option>) (matrix: ClMatrix.CSR<'b>) ->

let resultLength = matrix.RowCount * matrix.ColumnCount

let resultBitmap =
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)

let resultRows =
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)

let resultColumns =
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)

let resultValues =
clContext.CreateClArrayWithSpecificAllocationMode<'c>(DeviceOnly, resultLength)

let ndRange =
Range1D.CreateValid(resultLength, workGroupSize)

let kernel = kernel.GetKernel()

processor.Post(
Msg.MsgSetArguments
(fun () ->
kernel.KernelFunc
ndRange
operand
matrix.RowCount
matrix.ColumnCount
matrix.Values
matrix.RowPointers
matrix.Columns
resultBitmap
resultValues
resultRows
resultColumns)
)

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

resultBitmap, resultValues, resultRows, resultColumns

let run<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
(clContext: ClContext)
(op: Expr<'a option -> 'b option -> 'c option>)
workGroupSize
=

let mapWithValue =
preparePositions clContext workGroupSize op

let setPositions =
Common.setPositionsOption<'c> clContext workGroupSize

fun (queue: MailboxProcessor<_>) allocationMode (value: 'a option) (matrix: ClMatrix.CSR<'b>) ->
let valueClCell = clContext.CreateClCell value

let bitmap, values, rows, columns = mapWithValue queue valueClCell matrix

valueClCell.Free queue

let result =
setPositions queue allocationMode rows columns values bitmap

queue.Post(Msg.CreateFreeMsg<_>(bitmap))
queue.Post(Msg.CreateFreeMsg<_>(values))
queue.Post(Msg.CreateFreeMsg<_>(rows))
queue.Post(Msg.CreateFreeMsg<_>(columns))

result
|> Option.bind
(fun (resRows, resCols, resValues, _) ->
{ Context = clContext
RowCount = matrix.RowCount
ColumnCount = matrix.ColumnCount
Rows = resRows
Columns = resCols
Values = resValues }
|> Some)
2 changes: 2 additions & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/CSR/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ module Matrix =

let map = CSR.Map.run

let mapWithValueOption = CSR.Map.WithValue.run

let map2 = Map2.run

let map2AtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
Expand Down
40 changes: 38 additions & 2 deletions src/GraphBLAS-sharp.Backend/Matrix/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ 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 internal Common =
///<param name="clContext">.</param>
Expand Down Expand Up @@ -42,3 +40,41 @@ module internal Common =
valuesScatter processor positions allValues resultValues

resultRows, resultColumns, resultValues, resultLength

///<param name="clContext">.</param>
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
let setPositionsOption<'a when 'a: struct> (clContext: ClContext) workGroupSize =

let indicesScatter =
Scatter.lastOccurrence clContext workGroupSize

let valuesScatter =
Scatter.lastOccurrence clContext workGroupSize

let sum =
PrefixSum.standardExcludeInPlace clContext workGroupSize

fun (processor: MailboxProcessor<_>) allocationMode (allRows: ClArray<int>) (allColumns: ClArray<int>) (allValues: ClArray<'a>) (positions: ClArray<int>) ->

let resultLength =
(sum processor positions).ToHostAndFree(processor)

if resultLength = 0 then
None
else
let resultRows =
clContext.CreateClArrayWithSpecificAllocationMode<int>(allocationMode, resultLength)

let resultColumns =
clContext.CreateClArrayWithSpecificAllocationMode<int>(allocationMode, resultLength)

let resultValues =
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)

indicesScatter processor positions allRows resultRows

indicesScatter processor positions allColumns resultColumns

valuesScatter processor positions allValues resultValues

Some(resultRows, resultColumns, resultValues, resultLength)
Loading