Skip to content

Commit 835e7bf

Browse files
authored
Merge pull request #46 from kirillgarbar/elementwise
Elementwise
2 parents 7ae6f99 + 5814867 commit 835e7bf

20 files changed

Lines changed: 980 additions & 129 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ module M =
274274
type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
275275

276276
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<float32>,float32>(
277-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
277+
(fun context wgSize -> Backend.COOMatrix.elementwise context Backend.Common.StandardOperations.float32Sum wgSize),
278278
float32,
279279
(fun _ -> Utils.nextSingle (System.Random())),
280280
M.buildCooMatrix
@@ -286,7 +286,7 @@ type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
286286
type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
287287

288288
inherit EWiseAddBenchmarksWithDataTransfer<Backend.COOMatrix<float32>,float32>(
289-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
289+
(fun context wgSize -> Backend.COOMatrix.elementwise context Backend.Common.StandardOperations.float32Sum wgSize),
290290
float32,
291291
(fun _ -> Utils.nextSingle (System.Random())),
292292
M.buildCooMatrix,
@@ -300,7 +300,7 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
300300
type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
301301

302302
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<bool>,bool>(
303-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
303+
(fun context wgSize -> Backend.COOMatrix.elementwise context Backend.Common.StandardOperations.boolSum wgSize),
304304
(fun _ -> true),
305305
(fun _ -> true),
306306
M.buildCooMatrix
@@ -313,7 +313,7 @@ type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
313313
type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
314314

315315
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<float32>,float32>(
316-
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
316+
(fun context wgSize -> Backend.CSRMatrix.elementwise context Backend.Common.StandardOperations.float32Sum wgSize),
317317
float32,
318318
(fun _ -> Utils.nextSingle (System.Random())),
319319
M.buildCsrMatrix
@@ -326,7 +326,7 @@ type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
326326
type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
327327

328328
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<bool>,bool>(
329-
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
329+
(fun context wgSize -> Backend.CSRMatrix.elementwise context Backend.Common.StandardOperations.boolSum wgSize),
330330
(fun _ -> true),
331331
(fun _ -> true),
332332
M.buildCsrMatrix
@@ -340,7 +340,7 @@ type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
340340
type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
341341

342342
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<bool>,bool>(
343-
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
343+
(fun context wgSize -> Backend.COOMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
344344
(fun _ -> true),
345345
(fun _ -> true),
346346
M.buildCooMatrix
@@ -352,7 +352,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
352352
type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
353353

354354
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<bool>,bool>(
355-
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
355+
(fun context wgSize -> Backend.CSRMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
356356
(fun _ -> true),
357357
(fun _ -> true),
358358
M.buildCsrMatrix
@@ -364,7 +364,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
364364
type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
365365

366366
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<float32>,float32>(
367-
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
367+
(fun context wgSize -> Backend.COOMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
368368
float32,
369369
(fun _ -> Utils.nextSingle (System.Random())),
370370
M.buildCooMatrix
@@ -376,7 +376,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
376376
type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =
377377

378378
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<float32>,float32>(
379-
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
379+
(fun context wgSize -> Backend.CSRMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
380380
float32,
381381
(fun _ -> Utils.nextSingle (System.Random())),
382382
M.buildCsrMatrix

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksMxv.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace GraphBLAS.FSharp.Benchmarks
22

33
open GraphBLAS.FSharp
4+
open GraphBLAS.FSharp.Backend
45
open BenchmarkDotNet.Attributes
56
open BenchmarkDotNet.Configs
67
open BenchmarkDotNet.Columns
@@ -75,4 +76,4 @@ type MxvBenchmarks() =
7576
| ".mtx" -> MtxReader(Utils.getFullPathToMatrix "Common" matrixFilename)
7677
| _ -> failwith "Unsupported matrix format"
7778
)
78-
*)
79+
*)

benchmarks/GraphBLAS-sharp.Benchmarks/Helpers.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ module Utils =
239239

240240
let rowIndices2rowPointers (rowIndices: int []) rowCount =
241241
let nnzPerRow = Array.zeroCreate rowCount
242-
let rowPointers = Array.zeroCreate rowCount
242+
let rowPointers = Array.zeroCreate (rowCount + 1)
243243

244244
Array.iter (fun rowIndex -> nnzPerRow.[rowIndex] <- nnzPerRow.[rowIndex] + 1) rowIndices
245245

246-
for i in 1 .. rowCount - 1 do
246+
for i in 1 .. rowCount do
247247
rowPointers.[i] <- rowPointers.[i - 1] + nnzPerRow.[i - 1]
248248

249249
rowPointers

src/GraphBLAS-sharp.Backend/Common/StandardOperations.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module StandardOperations =
8888
| Both _ -> res <- true
8989
| _ -> ()
9090

91-
if res then None else (Some true) @>
91+
if res then Some true else None @>
9292

9393
let intMulAtLeastOne = mkNumericMulAtLeastOne 0
9494
let byteMulAtLeastOne = mkNumericMulAtLeastOne 0uy

src/GraphBLAS-sharp.Backend/Common/Utils.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
namespace GraphBLAS.FSharp.Backend.Common
22

3+
open Brahma.FSharp
4+
35
module internal Utils =
6+
let defaultWorkGroupSize = 32
7+
48
let floorToPower2 =
59
fun x -> x ||| (x >>> 1)
610
>> fun x -> x ||| (x >>> 2)

src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33

44
<PropertyGroup>
@@ -18,12 +18,17 @@
1818
<Compile Include="Common/BitonicSort.fs" />
1919
<Compile Include="Common/StandardOperations.fs" />
2020
<Compile Include="Predefined/PrefixSum.fs" />
21-
<Compile Include="Matrices.fs" />
21+
<Compile Include="Objects/Common.fs" />
22+
<Compile Include="Objects/ArraysExtentions.fs" />
23+
<Compile Include="Objects/Vector.fs" />
24+
<Compile Include="Objects/Matrix.fs" />
2225
<Compile Include="Matrix/COOMatrix/COOMatrix.fs" />
26+
<Compile Include="Matrix/CSRMatrix/Elementwise.fs" />
2327
<Compile Include="Matrix/CSRMatrix/CSRMatrix.fs" />
2428
<Compile Include="Matrix/CSRMatrix/SpGEMM.fs" />
2529
<Compile Include="Matrix/CSRMatrix/SpMV.fs" />
2630
<Compile Include="Matrix/Matrix.fs" />
31+
<Folder Include="Vector" />
2732
<!--Compile Include="Backend/CSRMatrix/GetTuples.fs" /-->
2833
<!--Compile Include="Backend/CSRMatrix/SpMSpV.fs" /-->
2934
<!--Compile Include="Backend/CSRMatrix/Transpose.fs" /-->

src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/COOMatrix.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ module COOMatrix =
1111
let private setPositions<'a when 'a: struct> (clContext: ClContext) workGroupSize =
1212

1313
let setPositions =
14-
<@ fun (ndRange: Range1D) prefixSumArrayLength (allRowsBuffer: ClArray<int>) (allColumnsBuffer: ClArray<int>) (allValuesBuffer: ClArray<'a>) (prefixSumArrayBuffer: ClArray<int>) (resultRowsBuffer: ClArray<int>) (resultColumnsBuffer: ClArray<int>) (resultValuesBuffer: ClArray<'a>) ->
14+
<@ fun (ndRange: Range1D) prefixSumArrayLength resultLength (allRowsBuffer: ClArray<int>) (allColumnsBuffer: ClArray<int>) (allValuesBuffer: ClArray<'a>) (prefixSumArrayBuffer: ClArray<int>) (resultRowsBuffer: ClArray<int>) (resultColumnsBuffer: ClArray<int>) (resultValuesBuffer: ClArray<'a>) ->
1515

1616
let i = ndRange.GlobalID0
17+
let index = prefixSumArrayBuffer.[i]
1718

18-
if i = prefixSumArrayLength - 1
19-
|| i < prefixSumArrayLength
20-
&& prefixSumArrayBuffer.[i]
21-
<> prefixSumArrayBuffer.[i + 1] then
22-
let index = prefixSumArrayBuffer.[i]
23-
19+
if (i < prefixSumArrayLength - 1
20+
&& index <> prefixSumArrayBuffer.[i + 1])
21+
|| (i = prefixSumArrayLength - 1
22+
&& index < resultLength) then
2423
resultRowsBuffer.[index] <- allRowsBuffer.[i]
2524
resultColumnsBuffer.[index] <- allColumnsBuffer.[i]
2625
resultValuesBuffer.[index] <- allValuesBuffer.[i] @>
@@ -82,6 +81,7 @@ module COOMatrix =
8281
kernel.KernelFunc
8382
ndRange
8483
prefixSumArrayLength
84+
resultLength
8585
allRows
8686
allColumns
8787
allValues
@@ -385,7 +385,7 @@ module COOMatrix =
385385
///<param name="clContext">.</param>
386386
///<param name="opAdd">.</param>
387387
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
388-
let eWiseAdd<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
388+
let elementwise<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
389389
(clContext: ClContext)
390390
(opAdd: Expr<'a option -> 'b option -> 'c option>)
391391
workGroupSize
@@ -613,7 +613,7 @@ module COOMatrix =
613613
///<param name="clContext">.</param>
614614
///<param name="opAdd">.</param>
615615
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
616-
let eWiseAddAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
616+
let elementwiseAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
617617
(clContext: ClContext)
618618
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
619619
workGroupSize

src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs

Lines changed: 147 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace GraphBLAS.FSharp.Backend
33
open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend
55
open GraphBLAS.FSharp.Backend.Common
6+
open GraphBLAS.FSharp.Backend.Elementwise
67
open Microsoft.FSharp.Quotations
78

89
module CSRMatrix =
@@ -79,12 +80,13 @@ module CSRMatrix =
7980
Columns = matrix.Columns
8081
Values = matrix.Values }
8182

82-
let eWiseAdd (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
83+
///<remarks>Old version</remarks>
84+
let elementwiseWithCOO (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
8385

8486
let prepareRows = prepareRows clContext workGroupSize
8587

8688
let eWiseCOO =
87-
COOMatrix.eWiseAdd clContext opAdd workGroupSize
89+
COOMatrix.elementwise clContext opAdd workGroupSize
8890

8991
let toCSRInplace =
9092
COOMatrix.toCSRInplace clContext workGroupSize
@@ -113,12 +115,17 @@ module CSRMatrix =
113115

114116
toCSRInplace processor m3COO
115117

116-
let eWiseAddAtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
118+
///<remarks>Old version</remarks>
119+
let elementwiseAtLeastOneWithCOO
120+
(clContext: ClContext)
121+
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
122+
workGroupSize
123+
=
117124

118125
let prepareRows = prepareRows clContext workGroupSize
119126

120127
let eWiseCOO =
121-
COOMatrix.eWiseAddAtLeastOne clContext opAdd workGroupSize
128+
COOMatrix.elementwiseAtLeastOne clContext opAdd workGroupSize
122129

123130
let toCSRInplace =
124131
COOMatrix.toCSRInplace clContext workGroupSize
@@ -177,3 +184,139 @@ module CSRMatrix =
177184
let coo = toCOO queue matrix
178185
let transposedCoo = transposeInplace queue coo
179186
toCSRInplace queue transposedCoo
187+
188+
let elementwiseToCOO<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
189+
(clContext: ClContext)
190+
(opAdd: Expr<'a option -> 'b option -> 'c option>)
191+
workGroupSize
192+
=
193+
194+
let merge = merge clContext workGroupSize
195+
196+
let preparePositions =
197+
preparePositions clContext opAdd Utils.defaultWorkGroupSize
198+
199+
let setPositions =
200+
setPositions<'c> clContext Utils.defaultWorkGroupSize
201+
202+
fun (queue: MailboxProcessor<_>) (matrixLeft: CSRMatrix<'a>) (matrixRight: CSRMatrix<'b>) ->
203+
204+
let allRows, allColumns, leftMergedValues, rightMergedValues, isRowEnd, isLeft =
205+
merge
206+
queue
207+
matrixLeft.RowPointers
208+
matrixLeft.Columns
209+
matrixLeft.Values
210+
matrixRight.RowPointers
211+
matrixRight.Columns
212+
matrixRight.Values
213+
214+
let positions, allValues =
215+
preparePositions queue allColumns leftMergedValues rightMergedValues isRowEnd isLeft
216+
217+
queue.Post(Msg.CreateFreeMsg<_>(leftMergedValues))
218+
queue.Post(Msg.CreateFreeMsg<_>(rightMergedValues))
219+
220+
let resultRows, resultColumns, resultValues, positions, positionsSum =
221+
setPositions queue allRows allColumns allValues positions
222+
223+
queue.Post(Msg.CreateFreeMsg<_>(allRows))
224+
queue.Post(Msg.CreateFreeMsg<_>(isLeft))
225+
queue.Post(Msg.CreateFreeMsg<_>(isRowEnd))
226+
queue.Post(Msg.CreateFreeMsg<_>(positions))
227+
queue.Post(Msg.CreateFreeMsg<_>(allColumns))
228+
queue.Post(Msg.CreateFreeMsg<_>(allValues))
229+
230+
{ Context = clContext
231+
RowCount = matrixLeft.RowCount
232+
ColumnCount = matrixLeft.ColumnCount
233+
Rows = resultRows
234+
Columns = resultColumns
235+
Values = resultValues }
236+
237+
let elementwise<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
238+
(clContext: ClContext)
239+
(opAdd: Expr<'a option -> 'b option -> 'c option>)
240+
workGroupSize
241+
=
242+
243+
let elementwiseToCOO =
244+
elementwiseToCOO clContext opAdd workGroupSize
245+
246+
let toCSRInplace =
247+
COOMatrix.toCSRInplace clContext Utils.defaultWorkGroupSize
248+
249+
fun (queue: MailboxProcessor<_>) (matrixLeft: CSRMatrix<'a>) (matrixRight: CSRMatrix<'b>) ->
250+
251+
let cooRes =
252+
elementwiseToCOO queue matrixLeft matrixRight
253+
254+
toCSRInplace queue cooRes
255+
256+
let elementwiseAtLeastOneToCOO<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
257+
(clContext: ClContext)
258+
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
259+
workGroupSize
260+
=
261+
262+
let merge = merge clContext workGroupSize
263+
264+
let preparePositions =
265+
preparePositionsAtLeastOne clContext opAdd Utils.defaultWorkGroupSize
266+
267+
let setPositions =
268+
setPositions<'c> clContext Utils.defaultWorkGroupSize
269+
270+
fun (queue: MailboxProcessor<_>) (matrixLeft: CSRMatrix<'a>) (matrixRight: CSRMatrix<'b>) ->
271+
272+
let allRows, allColumns, leftMergedValues, rightMergedValues, isRowEnd, isLeft =
273+
merge
274+
queue
275+
matrixLeft.RowPointers
276+
matrixLeft.Columns
277+
matrixLeft.Values
278+
matrixRight.RowPointers
279+
matrixRight.Columns
280+
matrixRight.Values
281+
282+
let positions, allValues =
283+
preparePositions queue allColumns leftMergedValues rightMergedValues isRowEnd isLeft
284+
285+
queue.Post(Msg.CreateFreeMsg<_>(leftMergedValues))
286+
queue.Post(Msg.CreateFreeMsg<_>(rightMergedValues))
287+
288+
let resultRows, resultColumns, resultValues, positions, positionsSum =
289+
setPositions queue allRows allColumns allValues positions
290+
291+
queue.Post(Msg.CreateFreeMsg<_>(allRows))
292+
queue.Post(Msg.CreateFreeMsg<_>(isLeft))
293+
queue.Post(Msg.CreateFreeMsg<_>(isRowEnd))
294+
queue.Post(Msg.CreateFreeMsg<_>(positions))
295+
queue.Post(Msg.CreateFreeMsg<_>(allColumns))
296+
queue.Post(Msg.CreateFreeMsg<_>(allValues))
297+
298+
{ Context = clContext
299+
RowCount = matrixLeft.RowCount
300+
ColumnCount = matrixLeft.ColumnCount
301+
Rows = resultRows
302+
Columns = resultColumns
303+
Values = resultValues }
304+
305+
let elementwiseAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
306+
(clContext: ClContext)
307+
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
308+
workGroupSize
309+
=
310+
311+
let elementwiseAtLeastOneToCOO =
312+
elementwiseAtLeastOneToCOO clContext opAdd workGroupSize
313+
314+
let toCSRInplace =
315+
COOMatrix.toCSRInplace clContext Utils.defaultWorkGroupSize
316+
317+
fun (queue: MailboxProcessor<_>) (matrixLeft: CSRMatrix<'a>) (matrixRight: CSRMatrix<'b>) ->
318+
319+
let cooRes =
320+
elementwiseAtLeastOneToCOO queue matrixLeft matrixRight
321+
322+
toCSRInplace queue cooRes

0 commit comments

Comments
 (0)