Skip to content
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
3 changes: 3 additions & 0 deletions src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
<Compile Include="Matrix/Common.fs" />
<Compile Include="Matrix/COOMatrix/Map2.fs" />
<Compile Include="Matrix/COOMatrix/Map2AtLeastOne.fs" />
<Compile Include="Matrix/COOMatrix/Map.fs" />
<Compile Include="Matrix/COOMatrix/Matrix.fs" />
<Compile Include="Matrix/CSRMatrix/Map2.fs" />
<Compile Include="Matrix/CSRMatrix/Map2AtLeastOne.fs" />
<Compile Include="Matrix/CSRMatrix/SpGEMM.fs" />
<Compile Include="Matrix/CSRMatrix/Map.fs" />
<Compile Include="Matrix/CSRMatrix/Matrix.fs" />
<Compile Include="Matrix/Matrix.fs" />
<Compile Include="Vector/SparseVector/Common.fs" />
Expand Down
116 changes: 116 additions & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/Map.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
namespace GraphBLAS.FSharp.Backend.Matrix.COO

open System
open Brahma.FSharp
open GraphBLAS.FSharp.Backend.Matrix
open GraphBLAS.FSharp.Backend.Quotes
open Microsoft.FSharp.Quotations
open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
open GraphBLAS.FSharp.Backend.Objects.ClContext

module internal Map =
let preparePositions<'a, 'b> (clContext: ClContext) workGroupSize opAdd =

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

let gid = ndRange.GlobalID0

if gid < rowCount * columnCount then

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

let index =
(uint64 rowIndex <<< 32) ||| (uint64 columnIndex)

let value =
(%Search.Bin.byKey2D) valuesLength index rows columns values

match (%op) 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 opAdd

fun (processor: MailboxProcessor<_>) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) ->

let (resultLength: int) = columnCount * rowCount

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<'b>(DeviceOnly, resultLength)

let ndRange =
Range1D.CreateValid(resultLength, workGroupSize)

let kernel = kernel.GetKernel()

processor.Post(
Msg.MsgSetArguments
(fun () ->
kernel.KernelFunc
ndRange
rowCount
columnCount
values.Length
values
rowPointers
columns
resultBitmap
resultValues
resultRows
resultColumns)
)

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

resultBitmap, resultValues, resultRows, resultColumns

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

let map =
preparePositions clContext workGroupSize opAdd

let setPositions =
Common.setPositions<'b> clContext workGroupSize

fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.COO<'a>) ->

let bitmap, values, rows, columns =
map queue matrix.RowCount matrix.ColumnCount matrix.Values matrix.Rows matrix.Columns

let resultRows, resultColumns, resultValues, _ =
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))

{ Context = clContext
RowCount = matrix.RowCount
ColumnCount = matrix.ColumnCount
Rows = resultRows
Columns = resultColumns
Values = resultValues }
1 change: 1 addition & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/Map2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open GraphBLAS.FSharp.Backend.Matrix
open Microsoft.FSharp.Quotations
open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend
open GraphBLAS.FSharp.Backend.Quotes
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
open GraphBLAS.FSharp.Backend.Objects.ClContext
open GraphBLAS.FSharp.Backend.Quotes
Expand Down
2 changes: 2 additions & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend.Objects.ClMatrix

module Matrix =
let map = Map.run

let map2 = Map2.run

///<param name="clContext">.</param>
Expand Down
129 changes: 129 additions & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/Map.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
namespace GraphBLAS.FSharp.Backend.Matrix.CSR

open Brahma.FSharp
open FSharp.Quotations
open GraphBLAS.FSharp.Backend
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.ClMatrix
open GraphBLAS.FSharp.Backend.Objects.ClContext

module internal Map =
let preparePositions<'a, 'b> (clContext: ClContext) workGroupSize op =

let preparePositions (op: Expr<'a option -> 'b option>) =
<@ fun (ndRange: Range1D) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'b>) (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) 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<_>) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) ->

let (resultLength: int) = columnCount * rowCount

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<'b>(DeviceOnly, resultLength)

let ndRange =
Range1D.CreateValid(resultLength, workGroupSize)

let kernel = kernel.GetKernel()

processor.Post(
Msg.MsgSetArguments
(fun () ->
kernel.KernelFunc
ndRange
rowCount
columnCount
values
rowPointers
columns
resultBitmap
resultValues
resultRows
resultColumns)
)

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

resultBitmap, resultValues, resultRows, resultColumns

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

let map =
preparePositions clContext workGroupSize opAdd

let setPositions =
Common.setPositions<'b> clContext workGroupSize

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

let bitmap, values, rows, columns =
map queue matrix.RowCount matrix.ColumnCount matrix.Values matrix.RowPointers matrix.Columns

let resultRows, resultColumns, resultValues, _ =
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))

{ Context = clContext
RowCount = matrix.RowCount
ColumnCount = matrix.ColumnCount
Rows = resultRows
Columns = resultColumns
Values = resultValues }

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

let mapToCOO = runToCOO clContext opAdd workGroupSize

let toCSRInplace =
Matrix.toCSRInplace clContext workGroupSize

fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
mapToCOO queue allocationMode matrix
|> toCSRInplace queue allocationMode
Loading