Skip to content

Map2 #66

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 5 commits into from
Mar 14, 2023
Merged

Map2 #66

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
21 changes: 10 additions & 11 deletions benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ open BenchmarkDotNet.Columns
open Brahma.FSharp
open GraphBLAS.FSharp.Objects
open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend.Matrix.COO
open GraphBLAS.FSharp.Backend.Matrix.CSR
open GraphBLAS.FSharp.Backend.Matrix
open GraphBLAS.FSharp.Objects.Matrix
open GraphBLAS.FSharp.Benchmarks.MatrixExtensions
open GraphBLAS.FSharp.Backend.Objects.ClContext
Expand Down Expand Up @@ -196,7 +195,7 @@ module M =
type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<float32>,float32>(
(fun context wgSize -> COOMatrix.map2 context ArithmeticOperations.float32Sum wgSize),
(fun context wgSize -> COO.Matrix.map2 context ArithmeticOperations.float32Sum wgSize),
float32,
(fun _ -> Utils.nextSingle (System.Random())),
Matrix.ToBackendCOO
Expand All @@ -208,7 +207,7 @@ type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
type EWiseAddBenchmarks4Float32COOWithDataTransfer() =

inherit EWiseAddBenchmarksWithDataTransfer<ClMatrix.COO<float32>,float32>(
(fun context wgSize -> COOMatrix.map2 context ArithmeticOperations.float32Sum wgSize),
(fun context wgSize -> COO.Matrix.map2 context ArithmeticOperations.float32Sum wgSize),
float32,
(fun _ -> Utils.nextSingle (System.Random())),
Matrix.ToBackendCOO<float32>,
Expand All @@ -222,7 +221,7 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<bool>,bool>(
(fun context wgSize -> COOMatrix.map2 context ArithmeticOperations.boolSum wgSize),
(fun context wgSize -> COO.Matrix.map2 context ArithmeticOperations.boolSum wgSize),
(fun _ -> true),
(fun _ -> true),
Matrix.ToBackendCOO<bool>
Expand All @@ -235,7 +234,7 @@ type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<float32>,float32>(
(fun context wgSize -> CSRMatrix.map2 context ArithmeticOperations.float32Sum wgSize),
(fun context wgSize -> CSR.Matrix.map2 context ArithmeticOperations.float32Sum wgSize),
float32,
(fun _ -> Utils.nextSingle (System.Random())),
Matrix.ToBackendCSR
Expand All @@ -248,7 +247,7 @@ type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<bool>,bool>(
(fun context wgSize -> CSRMatrix.map2 context ArithmeticOperations.boolSum wgSize),
(fun context wgSize -> CSR.Matrix.map2 context ArithmeticOperations.boolSum wgSize),
(fun _ -> true),
(fun _ -> true),
Matrix.ToBackendCSR
Expand All @@ -262,7 +261,7 @@ type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<bool>,bool>(
(fun context wgSize -> COOMatrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
(fun context wgSize -> COO.Matrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
(fun _ -> true),
(fun _ -> true),
Matrix.ToBackendCOO<bool>
Expand All @@ -274,7 +273,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<bool>,bool>(
(fun context wgSize -> CSRMatrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
(fun context wgSize -> CSR.Matrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
(fun _ -> true),
(fun _ -> true),
Matrix.ToBackendCSR
Expand All @@ -286,7 +285,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<float32>,float32>(
(fun context wgSize -> COOMatrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
(fun context wgSize -> COO.Matrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
float32,
(fun _ -> Utils.nextSingle (System.Random())),
Matrix.ToBackendCOO<float32>
Expand All @@ -298,7 +297,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =

inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<float32>,float32>(
(fun context wgSize -> CSRMatrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
(fun context wgSize -> CSR.Matrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
float32,
(fun _ -> Utils.nextSingle (System.Random())),
Matrix.ToBackendCSR<float32>
Expand Down
10 changes: 5 additions & 5 deletions benchmarks/GraphBLAS-sharp.Benchmarks/VectorEWiseAddGen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type VectorEWiseBenchmarksWithDataTransfer<'elem when 'elem : struct>(
override this.GlobalCleanup() = ()

/// Without data transfer

/// AtLeastOne
type VectorEWiseBenchmarks4FloatSparseWithoutDataTransfer() =

inherit VectorEWiseBenchmarksWithoutDataTransfer<float>(
Expand All @@ -173,13 +173,13 @@ type VectorEWiseBenchmarks4Int32SparseWithoutDataTransfer() =
type VectorEWiseGeneralBenchmarks4FloatSparseWithoutDataTransfer() =

inherit VectorEWiseBenchmarksWithoutDataTransfer<float>(
(fun context -> Vector.map2General context ArithmeticOperations.floatSum),
(fun context -> Vector.map2 context ArithmeticOperations.floatSum),
VectorGenerator.floatPair Sparse)

type VectorEWiseGeneralBenchmarks4Int32SparseWithoutDataTransfer() =

inherit VectorEWiseBenchmarksWithoutDataTransfer<int32>(
(fun context -> Vector.map2General context ArithmeticOperations.intSum),
(fun context -> Vector.map2 context ArithmeticOperations.intSum),
VectorGenerator.intPair Sparse)

/// With data transfer
Expand All @@ -201,11 +201,11 @@ type VectorEWiseBenchmarks4Int32SparseWithDataTransfer() =
type VectorEWiseGeneralBenchmarks4FloatSparseWithDataTransfer() =

inherit VectorEWiseBenchmarksWithDataTransfer<float>(
(fun context -> Vector.map2General context ArithmeticOperations.floatSum),
(fun context -> Vector.map2 context ArithmeticOperations.floatSum),
VectorGenerator.floatPair Sparse)

type VectorEWiseGeneralBenchmarks4Int32SparseWithDataTransfer() =

inherit VectorEWiseBenchmarksWithDataTransfer<int32>(
(fun context -> Vector.map2General context ArithmeticOperations.intSum),
(fun context -> Vector.map2 context ArithmeticOperations.intSum),
VectorGenerator.intPair Sparse)
10 changes: 7 additions & 3 deletions src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@
<Compile Include="Predefined/PrefixSum.fs" />
<!--Compile Include="Matrices.fs" /-->
<Compile Include="Matrix/Common.fs" />
<Compile Include="Matrix/COOMatrix/COOMatrix.fs" />
<Compile Include="Matrix/CSRMatrix/Map2.fs" />
<Compile Include="Matrix/COOMatrix/Map2.fs" />
<Compile Include="Matrix/COOMatrix/Map2AtLeastOne.fs" />
<Compile Include="Matrix/COOMatrix/Matrix.fs" />
<Compile Include="Matrix/CSRMatrix/Map2AtLeastOne.fs" />
<Compile Include="Matrix/CSRMatrix/SpGEMM.fs" />
<Compile Include="Matrix/CSRMatrix/CSRMatrix.fs" />
<Compile Include="Matrix/CSRMatrix/Matrix.fs" />
<Compile Include="Matrix/Matrix.fs" />
<Compile Include="Vector/SparseVector/Common.fs" />
<Compile Include="Vector/SparseVector/Map2.fs" />
<Compile Include="Vector/SparseVector/Map2AtLeastOne.fs" />
<Compile Include="Vector/SparseVector/SparseVector.fs" />
<Compile Include="Vector/DenseVector/DenseVector.fs" />
<Compile Include="Vector/Vector.fs" />
Expand Down
159 changes: 159 additions & 0 deletions src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/Map2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
namespace GraphBLAS.FSharp.Backend.Matrix.COO

open Brahma.FSharp
open GraphBLAS.FSharp.Backend.Matrix
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 Map2 =
let binSearch<'a> =
<@ fun lenght sourceIndex (rowIndices: ClArray<int>) (columnIndices: ClArray<int>) (values: ClArray<'a>) ->

let mutable leftEdge = 0
let mutable rightEdge = lenght - 1

let mutable result = None

while leftEdge <= rightEdge do
let middleIdx = (leftEdge + rightEdge) / 2

let currentIndex: uint64 =
((uint64 rowIndices.[middleIdx]) <<< 32)
||| (uint64 columnIndices.[middleIdx])

if sourceIndex = currentIndex then
result <- Some values.[middleIdx]

rightEdge <- -1 // TODO() break
elif sourceIndex < currentIndex then
rightEdge <- middleIdx - 1
else
leftEdge <- middleIdx + 1

result @>

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

let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) =
<@ fun (ndRange: Range1D) rowCount columnCount leftValuesLength rightValuesLength (leftValues: ClArray<'a>) (leftRows: ClArray<int>) (leftColumns: ClArray<int>) (rightValues: ClArray<'b>) (rightRows: ClArray<int>) (rightColumn: 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 index =
(uint64 rowIndex <<< 32) ||| (uint64 columnIndex)

let leftValue =
(%binSearch) leftValuesLength index leftRows leftColumns leftValues

let rightValue =
(%binSearch) rightValuesLength index rightRows rightColumn rightValues

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

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

let kernel =
clContext.Compile <| preparePositions opAdd

fun (processor: MailboxProcessor<_>) rowCount columnCount (leftValues: ClArray<'a>) (leftRows: ClArray<int>) (leftColumns: ClArray<int>) (rightValues: ClArray<'b>) (rightRows: ClArray<int>) (rightColumns: 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<'c>(DeviceOnly, resultLength)

let ndRange =
Range1D.CreateValid(resultLength, workGroupSize)

let kernel = kernel.GetKernel()

processor.Post(
Msg.MsgSetArguments
(fun () ->
kernel.KernelFunc
ndRange
rowCount
columnCount
leftValues.Length
rightValues.Length
leftValues
leftRows
leftColumns
rightValues
rightRows
rightColumns
resultBitmap
resultValues
resultRows
resultColumns)
)

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

resultBitmap, resultValues, resultRows, resultColumns

///<param name="clContext">.</param>
///<param name="opAdd">.</param>
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
let run<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
(clContext: ClContext)
(opAdd: Expr<'a option -> 'b option -> 'c option>)
workGroupSize
=

let map2 =
preparePositions clContext workGroupSize opAdd

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

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

let bitmap, values, rows, columns =
map2
queue
matrixLeft.RowCount
matrixLeft.ColumnCount
matrixLeft.Values
matrixLeft.Rows
matrixLeft.Columns
matrixRight.Values
matrixRight.Rows
matrixRight.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 = matrixLeft.RowCount
ColumnCount = matrixLeft.ColumnCount
Rows = resultRows
Columns = resultColumns
Values = resultValues }
Loading