Skip to content

Commit 3063225

Browse files
committed
Merged net5
2 parents 66b6bf1 + 835e7bf commit 3063225

18 files changed

Lines changed: 1073 additions & 123 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

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

222222
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<float32>,float32>(
223-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
223+
(fun context wgSize -> Backend.COOMatrix.elementwise context Backend.Common.StandardOperations.float32Sum wgSize),
224224
float32,
225225
(fun _ -> Utils.nextSingle (System.Random())),
226226
COOMatrix<float32>.ToBackend
@@ -232,7 +232,7 @@ type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
232232
type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
233233

234234
inherit EWiseAddBenchmarksWithDataTransfer<Backend.COOMatrix<float32>,float32>(
235-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
235+
(fun context wgSize -> Backend.COOMatrix.elementwise context Backend.Common.StandardOperations.float32Sum wgSize),
236236
float32,
237237
(fun _ -> Utils.nextSingle (System.Random())),
238238
COOMatrix<float32>.ToBackend,
@@ -246,7 +246,7 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
246246
type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
247247

248248
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<bool>,bool>(
249-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
249+
(fun context wgSize -> Backend.COOMatrix.elementwise context Backend.Common.StandardOperations.boolSum wgSize),
250250
(fun _ -> true),
251251
(fun _ -> true),
252252
COOMatrix<bool>.ToBackend
@@ -259,7 +259,7 @@ type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
259259
type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
260260

261261
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<float32>,float32>(
262-
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
262+
(fun context wgSize -> Backend.CSRMatrix.elementwise context Backend.Common.StandardOperations.float32Sum wgSize),
263263
float32,
264264
(fun _ -> Utils.nextSingle (System.Random())),
265265
CSRMatrix<float32>.ToBackend
@@ -272,7 +272,7 @@ type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
272272
type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
273273

274274
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<bool>,bool>(
275-
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
275+
(fun context wgSize -> Backend.CSRMatrix.elementwise context Backend.Common.StandardOperations.boolSum wgSize),
276276
(fun _ -> true),
277277
(fun _ -> true),
278278
CSRMatrix<bool>.ToBackend
@@ -286,7 +286,7 @@ type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
286286
type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
287287

288288
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<bool>,bool>(
289-
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
289+
(fun context wgSize -> Backend.COOMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
290290
(fun _ -> true),
291291
(fun _ -> true),
292292
COOMatrix<bool>.ToBackend
@@ -298,7 +298,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
298298
type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
299299

300300
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<bool>,bool>(
301-
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
301+
(fun context wgSize -> Backend.CSRMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
302302
(fun _ -> true),
303303
(fun _ -> true),
304304
CSRMatrix<bool>.ToBackend
@@ -310,7 +310,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
310310
type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
311311

312312
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<float32>,float32>(
313-
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
313+
(fun context wgSize -> Backend.COOMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
314314
float32,
315315
(fun _ -> Utils.nextSingle (System.Random())),
316316
COOMatrix<float32>.ToBackend
@@ -322,7 +322,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
322322
type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =
323323

324324
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<float32>,float32>(
325-
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
325+
(fun context wgSize -> Backend.CSRMatrix.elementwiseAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
326326
float32,
327327
(fun _ -> Utils.nextSingle (System.Random())),
328328
CSRMatrix<float32>.ToBackend

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+
*)

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: 9 additions & 1 deletion
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>
@@ -22,10 +22,18 @@
2222
<Compile Include="Matrices.fs" />
2323
<Compile Include="Masks.fs" />
2424
<Compile Include="Matrix/COOMatrix/COOMatrix.fs" />
25+
<Compile Include="Objects/Common.fs" />
26+
<Compile Include="Objects/ArraysExtentions.fs" />
27+
<Compile Include="Objects/Vector.fs" />
28+
<Compile Include="Objects/Matrix.fs" />
29+
<Compile Include="Matrix/COOMatrix/COOMatrix.fs" />
30+
<Compile Include="Matrix/CSRMatrix/Elementwise.fs" />
31+
<Compile Include="Matrix/CSRMatrix/CSRMatrix.fs" />
2532
<Compile Include="Matrix/CSRMatrix/SpGEMM.fs" />
2633
<Compile Include="Matrix/CSRMatrix/CSRMatrix.fs" />
2734
<Compile Include="Matrix/CSRMatrix/SpMV.fs" />
2835
<Compile Include="Matrix/Matrix.fs" />
36+
<Folder Include="Vector" />
2937
<!--Compile Include="Backend/CSRMatrix/GetTuples.fs" /-->
3038
<!--Compile Include="Backend/CSRMatrix/SpMSpV.fs" /-->
3139
<!--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
@@ -611,7 +611,7 @@ module COOMatrix =
611611
///<param name="clContext">.</param>
612612
///<param name="opAdd">.</param>
613613
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
614-
let eWiseAddAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
614+
let elementwiseAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
615615
(clContext: ClContext)
616616
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
617617
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 =
@@ -82,13 +83,14 @@ module CSRMatrix =
8283
Columns = matrix.Columns
8384
Values = matrix.Values }
8485

85-
let eWiseAdd (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
86+
///<remarks>Old version</remarks>
87+
let elementwiseWithCOO (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
8688

8789
let prepareRows =
8890
expandRowPointers clContext workGroupSize
8991

9092
let eWiseCOO =
91-
COOMatrix.eWiseAdd clContext opAdd workGroupSize
93+
COOMatrix.elementwise clContext opAdd workGroupSize
9294

9395
let toCSRInplace =
9496
COOMatrix.toCSRInplace clContext workGroupSize
@@ -117,13 +119,18 @@ module CSRMatrix =
117119

118120
toCSRInplace processor m3COO
119121

120-
let eWiseAddAtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
122+
///<remarks>Old version</remarks>
123+
let elementwiseAtLeastOneWithCOO
124+
(clContext: ClContext)
125+
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
126+
workGroupSize
127+
=
121128

122129
let prepareRows =
123130
expandRowPointers clContext workGroupSize
124131

125132
let eWiseCOO =
126-
COOMatrix.eWiseAddAtLeastOne clContext opAdd workGroupSize
133+
COOMatrix.elementwiseAtLeastOne clContext opAdd workGroupSize
127134

128135
let toCSRInplace =
129136
COOMatrix.toCSRInplace clContext workGroupSize
@@ -183,6 +190,142 @@ module CSRMatrix =
183190
let transposedCoo = transposeInplace queue coo
184191
toCSRInplace queue transposedCoo
185192

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

0 commit comments

Comments
 (0)