Skip to content

Commit 5409b2e

Browse files
authored
Merge pull request #66 from IgorErin/map2
Map2
2 parents b9ef287 + 564540d commit 5409b2e

File tree

19 files changed

+1010
-1047
lines changed

19 files changed

+1010
-1047
lines changed

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ open BenchmarkDotNet.Columns
99
open Brahma.FSharp
1010
open GraphBLAS.FSharp.Objects
1111
open GraphBLAS.FSharp.Backend.Objects
12-
open GraphBLAS.FSharp.Backend.Matrix.COO
13-
open GraphBLAS.FSharp.Backend.Matrix.CSR
12+
open GraphBLAS.FSharp.Backend.Matrix
1413
open GraphBLAS.FSharp.Objects.Matrix
1514
open GraphBLAS.FSharp.Benchmarks.MatrixExtensions
1615
open GraphBLAS.FSharp.Backend.Objects.ClContext
@@ -196,7 +195,7 @@ module M =
196195
type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
197196

198197
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<float32>,float32>(
199-
(fun context wgSize -> COOMatrix.map2 context ArithmeticOperations.float32Sum wgSize),
198+
(fun context wgSize -> COO.Matrix.map2 context ArithmeticOperations.float32Sum wgSize),
200199
float32,
201200
(fun _ -> Utils.nextSingle (System.Random())),
202201
Matrix.ToBackendCOO
@@ -208,7 +207,7 @@ type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
208207
type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
209208

210209
inherit EWiseAddBenchmarksWithDataTransfer<ClMatrix.COO<float32>,float32>(
211-
(fun context wgSize -> COOMatrix.map2 context ArithmeticOperations.float32Sum wgSize),
210+
(fun context wgSize -> COO.Matrix.map2 context ArithmeticOperations.float32Sum wgSize),
212211
float32,
213212
(fun _ -> Utils.nextSingle (System.Random())),
214213
Matrix.ToBackendCOO<float32>,
@@ -222,7 +221,7 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
222221
type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
223222

224223
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<bool>,bool>(
225-
(fun context wgSize -> COOMatrix.map2 context ArithmeticOperations.boolSum wgSize),
224+
(fun context wgSize -> COO.Matrix.map2 context ArithmeticOperations.boolSum wgSize),
226225
(fun _ -> true),
227226
(fun _ -> true),
228227
Matrix.ToBackendCOO<bool>
@@ -235,7 +234,7 @@ type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
235234
type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
236235

237236
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<float32>,float32>(
238-
(fun context wgSize -> CSRMatrix.map2 context ArithmeticOperations.float32Sum wgSize),
237+
(fun context wgSize -> CSR.Matrix.map2 context ArithmeticOperations.float32Sum wgSize),
239238
float32,
240239
(fun _ -> Utils.nextSingle (System.Random())),
241240
Matrix.ToBackendCSR
@@ -248,7 +247,7 @@ type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
248247
type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
249248

250249
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<bool>,bool>(
251-
(fun context wgSize -> CSRMatrix.map2 context ArithmeticOperations.boolSum wgSize),
250+
(fun context wgSize -> CSR.Matrix.map2 context ArithmeticOperations.boolSum wgSize),
252251
(fun _ -> true),
253252
(fun _ -> true),
254253
Matrix.ToBackendCSR
@@ -262,7 +261,7 @@ type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
262261
type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
263262

264263
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<bool>,bool>(
265-
(fun context wgSize -> COOMatrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
264+
(fun context wgSize -> COO.Matrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
266265
(fun _ -> true),
267266
(fun _ -> true),
268267
Matrix.ToBackendCOO<bool>
@@ -274,7 +273,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
274273
type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
275274

276275
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<bool>,bool>(
277-
(fun context wgSize -> CSRMatrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
276+
(fun context wgSize -> CSR.Matrix.map2AtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
278277
(fun _ -> true),
279278
(fun _ -> true),
280279
Matrix.ToBackendCSR
@@ -286,7 +285,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
286285
type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
287286

288287
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<float32>,float32>(
289-
(fun context wgSize -> COOMatrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
288+
(fun context wgSize -> COO.Matrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
290289
float32,
291290
(fun _ -> Utils.nextSingle (System.Random())),
292291
Matrix.ToBackendCOO<float32>
@@ -298,7 +297,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
298297
type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =
299298

300299
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<float32>,float32>(
301-
(fun context wgSize -> CSRMatrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
300+
(fun context wgSize -> CSR.Matrix.map2AtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
302301
float32,
303302
(fun _ -> Utils.nextSingle (System.Random())),
304303
Matrix.ToBackendCSR<float32>

benchmarks/GraphBLAS-sharp.Benchmarks/VectorEWiseAddGen.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ type VectorEWiseBenchmarksWithDataTransfer<'elem when 'elem : struct>(
155155
override this.GlobalCleanup() = ()
156156

157157
/// Without data transfer
158-
158+
/// AtLeastOne
159159
type VectorEWiseBenchmarks4FloatSparseWithoutDataTransfer() =
160160

161161
inherit VectorEWiseBenchmarksWithoutDataTransfer<float>(
@@ -173,13 +173,13 @@ type VectorEWiseBenchmarks4Int32SparseWithoutDataTransfer() =
173173
type VectorEWiseGeneralBenchmarks4FloatSparseWithoutDataTransfer() =
174174

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

179179
type VectorEWiseGeneralBenchmarks4Int32SparseWithoutDataTransfer() =
180180

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

185185
/// With data transfer
@@ -201,11 +201,11 @@ type VectorEWiseBenchmarks4Int32SparseWithDataTransfer() =
201201
type VectorEWiseGeneralBenchmarks4FloatSparseWithDataTransfer() =
202202

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

207207
type VectorEWiseGeneralBenchmarks4Int32SparseWithDataTransfer() =
208208

209209
inherit VectorEWiseBenchmarksWithDataTransfer<int32>(
210-
(fun context -> Vector.map2General context ArithmeticOperations.intSum),
210+
(fun context -> Vector.map2 context ArithmeticOperations.intSum),
211211
VectorGenerator.intPair Sparse)

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@
3535
<Compile Include="Predefined/PrefixSum.fs" />
3636
<!--Compile Include="Matrices.fs" /-->
3737
<Compile Include="Matrix/Common.fs" />
38-
<Compile Include="Matrix/COOMatrix/COOMatrix.fs" />
39-
<Compile Include="Matrix/CSRMatrix/Map2.fs" />
38+
<Compile Include="Matrix/COOMatrix/Map2.fs" />
39+
<Compile Include="Matrix/COOMatrix/Map2AtLeastOne.fs" />
40+
<Compile Include="Matrix/COOMatrix/Matrix.fs" />
41+
<Compile Include="Matrix/CSRMatrix/Map2AtLeastOne.fs" />
4042
<Compile Include="Matrix/CSRMatrix/SpGEMM.fs" />
41-
<Compile Include="Matrix/CSRMatrix/CSRMatrix.fs" />
43+
<Compile Include="Matrix/CSRMatrix/Matrix.fs" />
4244
<Compile Include="Matrix/Matrix.fs" />
45+
<Compile Include="Vector/SparseVector/Common.fs" />
4346
<Compile Include="Vector/SparseVector/Map2.fs" />
47+
<Compile Include="Vector/SparseVector/Map2AtLeastOne.fs" />
4448
<Compile Include="Vector/SparseVector/SparseVector.fs" />
4549
<Compile Include="Vector/DenseVector/DenseVector.fs" />
4650
<Compile Include="Vector/Vector.fs" />
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
namespace GraphBLAS.FSharp.Backend.Matrix.COO
2+
3+
open Brahma.FSharp
4+
open GraphBLAS.FSharp.Backend.Matrix
5+
open Microsoft.FSharp.Quotations
6+
open GraphBLAS.FSharp.Backend.Objects
7+
open GraphBLAS.FSharp.Backend
8+
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
9+
open GraphBLAS.FSharp.Backend.Objects.ClContext
10+
11+
module internal Map2 =
12+
let binSearch<'a> =
13+
<@ fun lenght sourceIndex (rowIndices: ClArray<int>) (columnIndices: ClArray<int>) (values: ClArray<'a>) ->
14+
15+
let mutable leftEdge = 0
16+
let mutable rightEdge = lenght - 1
17+
18+
let mutable result = None
19+
20+
while leftEdge <= rightEdge do
21+
let middleIdx = (leftEdge + rightEdge) / 2
22+
23+
let currentIndex: uint64 =
24+
((uint64 rowIndices.[middleIdx]) <<< 32)
25+
||| (uint64 columnIndices.[middleIdx])
26+
27+
if sourceIndex = currentIndex then
28+
result <- Some values.[middleIdx]
29+
30+
rightEdge <- -1 // TODO() break
31+
elif sourceIndex < currentIndex then
32+
rightEdge <- middleIdx - 1
33+
else
34+
leftEdge <- middleIdx + 1
35+
36+
result @>
37+
38+
let preparePositions<'a, 'b, 'c> (clContext: ClContext) workGroupSize opAdd =
39+
40+
let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) =
41+
<@ 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>) ->
42+
43+
let gid = ndRange.GlobalID0
44+
45+
if gid < rowCount * columnCount then
46+
47+
let columnIndex = gid % columnCount
48+
let rowIndex = gid / columnCount
49+
50+
let index =
51+
(uint64 rowIndex <<< 32) ||| (uint64 columnIndex)
52+
53+
let leftValue =
54+
(%binSearch) leftValuesLength index leftRows leftColumns leftValues
55+
56+
let rightValue =
57+
(%binSearch) rightValuesLength index rightRows rightColumn rightValues
58+
59+
match (%op) leftValue rightValue with
60+
| Some value ->
61+
resultValues.[gid] <- value
62+
resultRows.[gid] <- rowIndex
63+
resultColumns.[gid] <- columnIndex
64+
65+
resultBitmap.[gid] <- 1
66+
| None -> resultBitmap.[gid] <- 0 @>
67+
68+
let kernel =
69+
clContext.Compile <| preparePositions opAdd
70+
71+
fun (processor: MailboxProcessor<_>) rowCount columnCount (leftValues: ClArray<'a>) (leftRows: ClArray<int>) (leftColumns: ClArray<int>) (rightValues: ClArray<'b>) (rightRows: ClArray<int>) (rightColumns: ClArray<int>) ->
72+
73+
let (resultLength: int) = columnCount * rowCount
74+
75+
let resultBitmap =
76+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
77+
78+
let resultRows =
79+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
80+
81+
let resultColumns =
82+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
83+
84+
let resultValues =
85+
clContext.CreateClArrayWithSpecificAllocationMode<'c>(DeviceOnly, resultLength)
86+
87+
let ndRange =
88+
Range1D.CreateValid(resultLength, workGroupSize)
89+
90+
let kernel = kernel.GetKernel()
91+
92+
processor.Post(
93+
Msg.MsgSetArguments
94+
(fun () ->
95+
kernel.KernelFunc
96+
ndRange
97+
rowCount
98+
columnCount
99+
leftValues.Length
100+
rightValues.Length
101+
leftValues
102+
leftRows
103+
leftColumns
104+
rightValues
105+
rightRows
106+
rightColumns
107+
resultBitmap
108+
resultValues
109+
resultRows
110+
resultColumns)
111+
)
112+
113+
processor.Post(Msg.CreateRunMsg<_, _> kernel)
114+
115+
resultBitmap, resultValues, resultRows, resultColumns
116+
117+
///<param name="clContext">.</param>
118+
///<param name="opAdd">.</param>
119+
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
120+
let run<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
121+
(clContext: ClContext)
122+
(opAdd: Expr<'a option -> 'b option -> 'c option>)
123+
workGroupSize
124+
=
125+
126+
let map2 =
127+
preparePositions clContext workGroupSize opAdd
128+
129+
let setPositions =
130+
Common.setPositions<'c> clContext workGroupSize
131+
132+
fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.COO<'a>) (matrixRight: ClMatrix.COO<'b>) ->
133+
134+
let bitmap, values, rows, columns =
135+
map2
136+
queue
137+
matrixLeft.RowCount
138+
matrixLeft.ColumnCount
139+
matrixLeft.Values
140+
matrixLeft.Rows
141+
matrixLeft.Columns
142+
matrixRight.Values
143+
matrixRight.Rows
144+
matrixRight.Columns
145+
146+
let resultRows, resultColumns, resultValues, _ =
147+
setPositions queue allocationMode rows columns values bitmap
148+
149+
queue.Post(Msg.CreateFreeMsg<_>(bitmap))
150+
queue.Post(Msg.CreateFreeMsg<_>(values))
151+
queue.Post(Msg.CreateFreeMsg<_>(rows))
152+
queue.Post(Msg.CreateFreeMsg<_>(columns))
153+
154+
{ Context = clContext
155+
RowCount = matrixLeft.RowCount
156+
ColumnCount = matrixLeft.ColumnCount
157+
Rows = resultRows
158+
Columns = resultColumns
159+
Values = resultValues }

0 commit comments

Comments
 (0)