Skip to content

Commit 661e085

Browse files
committed
add: Matrix.Merge.tests
1 parent 848a11b commit 661e085

File tree

21 files changed

+512
-334
lines changed

21 files changed

+512
-334
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
<Compile Include="Common/Sort/Bitonic.fs" />
3737
<Compile Include="Common/Sum.fs" />
3838
<Compile Include="Matrix/Common.fs" />
39-
<Compile Include="Matrix/COOMatrix/Map2.fs" />
40-
<Compile Include="Matrix/COOMatrix/Map2AtLeastOne.fs" />
41-
<Compile Include="Matrix/COOMatrix/Map.fs" />
42-
<Compile Include="Matrix/COOMatrix/Matrix.fs" />
43-
<Compile Include="Matrix/CSRMatrix/Map2.fs" />
44-
<Compile Include="Matrix/CSRMatrix/SpGEMM/Expand.fs" />
45-
<Compile Include="Matrix/CSRMatrix/SpGEMM/Masked.fs" />
46-
<Compile Include="Matrix/CSRMatrix/Map2AtLeastOne.fs" />
47-
<Compile Include="Matrix/CSRMatrix/Map.fs" />
48-
<Compile Include="Matrix/CSRMatrix/Matrix.fs" />
39+
<Compile Include="Matrix\COO\Map.fs" />
40+
<Compile Include="Matrix\COO\Merge.fs" />
41+
<Compile Include="Matrix\COO\Map2.fs" />
42+
<Compile Include="Matrix\COO\Matrix.fs" />
43+
<Compile Include="Matrix\CSR\Merge.fs" />
44+
<Compile Include="Matrix\CSR\Map2.fs" />
45+
<Compile Include="Matrix\CSR\SpGEMM\Expand.fs" />
46+
<Compile Include="Matrix\CSR\SpGEMM\Masked.fs" />
47+
<Compile Include="Matrix\CSR\Map.fs" />
48+
<Compile Include="Matrix\CSR\Matrix.fs" />
4949
<Compile Include="Matrix/Matrix.fs" />
5050
<Compile Include="Vector/SparseVector/Common.fs" />
5151
<Compile Include="Vector/SparseVector/Merge.fs" />

src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/Map2.fs renamed to src/GraphBLAS-sharp.Backend/Matrix/COO/Map2.fs

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ open GraphBLAS.FSharp.Backend
88
open GraphBLAS.FSharp.Backend.Quotes
99
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
1010
open GraphBLAS.FSharp.Backend.Objects.ClContext
11-
open GraphBLAS.FSharp.Backend.Quotes
1211

1312
module internal Map2 =
14-
1513
let preparePositions<'a, 'b, 'c> (clContext: ClContext) workGroupSize opAdd =
1614

1715
let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) =
@@ -134,3 +132,123 @@ module internal Map2 =
134132
Rows = resultRows
135133
Columns = resultColumns
136134
Values = resultValues }
135+
136+
module AtLeastOne =
137+
let preparePositionsAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
138+
(clContext: ClContext)
139+
(opAdd: Expr<'a option -> 'b option -> 'c option>)
140+
workGroupSize
141+
=
142+
143+
let preparePositions =
144+
<@ fun (ndRange: Range1D) length (allRowsBuffer: ClArray<int>) (allColumnsBuffer: ClArray<int>) (leftValuesBuffer: ClArray<'a>) (rightValuesBuffer: ClArray<'b>) (allValuesBuffer: ClArray<'c>) (rawPositionsBuffer: ClArray<int>) (isLeftBitmap: ClArray<int>) ->
145+
146+
let i = ndRange.GlobalID0
147+
148+
if (i < length - 1
149+
&& allRowsBuffer.[i] = allRowsBuffer.[i + 1]
150+
&& allColumnsBuffer.[i] = allColumnsBuffer.[i + 1]) then
151+
152+
let result =
153+
(%opAdd) (Some leftValuesBuffer.[i + 1]) (Some rightValuesBuffer.[i])
154+
155+
(%PreparePositions.both) i result rawPositionsBuffer allValuesBuffer
156+
elif (i > 0
157+
&& i < length
158+
&& (allRowsBuffer.[i] <> allRowsBuffer.[i - 1]
159+
|| allColumnsBuffer.[i] <> allColumnsBuffer.[i - 1]))
160+
|| i = 0 then
161+
162+
let leftResult =
163+
(%opAdd) (Some leftValuesBuffer.[i]) None
164+
165+
let rightResult =
166+
(%opAdd) None (Some rightValuesBuffer.[i])
167+
168+
(%PreparePositions.leftRight)
169+
i
170+
leftResult
171+
rightResult
172+
isLeftBitmap
173+
allValuesBuffer
174+
rawPositionsBuffer @>
175+
176+
let kernel = clContext.Compile(preparePositions)
177+
178+
fun (processor: MailboxProcessor<_>) (allRows: ClArray<int>) (allColumns: ClArray<int>) (leftValues: ClArray<'a>) (rightValues: ClArray<'b>) (isLeft: ClArray<int>) ->
179+
let length = leftValues.Length
180+
181+
let ndRange =
182+
Range1D.CreateValid(length, workGroupSize)
183+
184+
let rawPositionsGpu =
185+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, length)
186+
187+
let allValues =
188+
clContext.CreateClArrayWithSpecificAllocationMode<'c>(DeviceOnly, length)
189+
190+
let kernel = kernel.GetKernel()
191+
192+
processor.Post(
193+
Msg.MsgSetArguments
194+
(fun () ->
195+
kernel.KernelFunc
196+
ndRange
197+
length
198+
allRows
199+
allColumns
200+
leftValues
201+
rightValues
202+
allValues
203+
rawPositionsGpu
204+
isLeft)
205+
)
206+
207+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
208+
209+
rawPositionsGpu, allValues
210+
211+
212+
///<param name="clContext">.</param>
213+
///<param name="opAdd">.</param>
214+
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
215+
let run<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
216+
(clContext: ClContext)
217+
(opAdd: Expr<'a option -> 'b option -> 'c option>)
218+
workGroupSize
219+
=
220+
221+
let merge = Merge.run clContext workGroupSize
222+
223+
let preparePositions =
224+
preparePositionsAtLeastOne clContext opAdd workGroupSize
225+
226+
let setPositions =
227+
Common.setPositions<'c> clContext workGroupSize
228+
229+
fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.COO<'a>) (matrixRight: ClMatrix.COO<'b>) ->
230+
231+
let allRows, allColumns, leftMergedValues, rightMergedValues, isLeft =
232+
merge queue matrixLeft matrixRight
233+
234+
let rawPositions, allValues =
235+
preparePositions queue allRows allColumns leftMergedValues rightMergedValues isLeft
236+
237+
queue.Post(Msg.CreateFreeMsg<_>(leftMergedValues))
238+
queue.Post(Msg.CreateFreeMsg<_>(rightMergedValues))
239+
240+
let resultRows, resultColumns, resultValues, _ =
241+
setPositions queue allocationMode allRows allColumns allValues rawPositions
242+
243+
queue.Post(Msg.CreateFreeMsg<_>(isLeft))
244+
queue.Post(Msg.CreateFreeMsg<_>(rawPositions))
245+
queue.Post(Msg.CreateFreeMsg<_>(allRows))
246+
queue.Post(Msg.CreateFreeMsg<_>(allColumns))
247+
queue.Post(Msg.CreateFreeMsg<_>(allValues))
248+
249+
{ Context = clContext
250+
RowCount = matrixLeft.RowCount
251+
ColumnCount = matrixLeft.ColumnCount
252+
Rows = resultRows
253+
Columns = resultColumns
254+
Values = resultValues }

src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/Matrix.fs renamed to src/GraphBLAS-sharp.Backend/Matrix/COO/Matrix.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Matrix =
2121
workGroupSize
2222
=
2323

24-
Map2AtLeastOne.run clContext (Convert.atLeastOneToOption opAdd) workGroupSize
24+
Map2.AtLeastOne.run clContext (Convert.atLeastOneToOption opAdd) workGroupSize
2525

2626
let getTuples (clContext: ClContext) workGroupSize =
2727

src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/Map2AtLeastOne.fs renamed to src/GraphBLAS-sharp.Backend/Matrix/COO/Merge.fs

Lines changed: 12 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,11 @@
11
namespace GraphBLAS.FSharp.Backend.Matrix.COO
22

33
open Brahma.FSharp
4-
open GraphBLAS.FSharp.Backend.Matrix
5-
open GraphBLAS.FSharp.Backend.Quotes
6-
open Microsoft.FSharp.Quotations
7-
open GraphBLAS.FSharp.Backend.Objects
8-
open GraphBLAS.FSharp.Backend
9-
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
104
open GraphBLAS.FSharp.Backend.Objects.ClContext
5+
open GraphBLAS.FSharp.Backend.Objects
116

12-
module internal Map2AtLeastOne =
13-
let preparePositionsAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
14-
(clContext: ClContext)
15-
(opAdd: Expr<'a option -> 'b option -> 'c option>)
16-
workGroupSize
17-
=
18-
19-
let preparePositions =
20-
<@ fun (ndRange: Range1D) length (allRowsBuffer: ClArray<int>) (allColumnsBuffer: ClArray<int>) (leftValuesBuffer: ClArray<'a>) (rightValuesBuffer: ClArray<'b>) (allValuesBuffer: ClArray<'c>) (rawPositionsBuffer: ClArray<int>) (isLeftBitmap: ClArray<int>) ->
21-
22-
let i = ndRange.GlobalID0
23-
24-
if (i < length - 1
25-
&& allRowsBuffer.[i] = allRowsBuffer.[i + 1]
26-
&& allColumnsBuffer.[i] = allColumnsBuffer.[i + 1]) then
27-
28-
let result =
29-
(%opAdd) (Some leftValuesBuffer.[i + 1]) (Some rightValuesBuffer.[i])
30-
31-
(%PreparePositions.both) i result rawPositionsBuffer allValuesBuffer
32-
elif (i > 0
33-
&& i < length
34-
&& (allRowsBuffer.[i] <> allRowsBuffer.[i - 1]
35-
|| allColumnsBuffer.[i] <> allColumnsBuffer.[i - 1]))
36-
|| i = 0 then
37-
38-
let leftResult =
39-
(%opAdd) (Some leftValuesBuffer.[i]) None
40-
41-
let rightResult =
42-
(%opAdd) None (Some rightValuesBuffer.[i])
43-
44-
(%PreparePositions.leftRight)
45-
i
46-
leftResult
47-
rightResult
48-
isLeftBitmap
49-
allValuesBuffer
50-
rawPositionsBuffer @>
51-
52-
let kernel = clContext.Compile(preparePositions)
53-
54-
fun (processor: MailboxProcessor<_>) (allRows: ClArray<int>) (allColumns: ClArray<int>) (leftValues: ClArray<'a>) (rightValues: ClArray<'b>) (isLeft: ClArray<int>) ->
55-
let length = leftValues.Length
56-
57-
let ndRange =
58-
Range1D.CreateValid(length, workGroupSize)
59-
60-
let rawPositionsGpu =
61-
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, length)
62-
63-
let allValues =
64-
clContext.CreateClArrayWithSpecificAllocationMode<'c>(DeviceOnly, length)
65-
66-
let kernel = kernel.GetKernel()
67-
68-
processor.Post(
69-
Msg.MsgSetArguments
70-
(fun () ->
71-
kernel.KernelFunc
72-
ndRange
73-
length
74-
allRows
75-
allColumns
76-
leftValues
77-
rightValues
78-
allValues
79-
rawPositionsGpu
80-
isLeft)
81-
)
82-
83-
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
84-
85-
rawPositionsGpu, allValues
86-
87-
let merge<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) workGroupSize =
7+
module Merge =
8+
let run<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) workGroupSize =
889

8910
let merge =
9011
<@ fun (ndRange: Range1D) firstSide secondSide sumOfSides (firstRowsBuffer: ClArray<int>) (firstColumnsBuffer: ClArray<int>) (firstValuesBuffer: ClArray<'a>) (secondRowsBuffer: ClArray<int>) (secondColumnsBuffer: ClArray<int>) (secondValuesBuffer: ClArray<'b>) (allRowsBuffer: ClArray<int>) (allColumnsBuffer: ClArray<int>) (leftMergedValuesBuffer: ClArray<'a>) (rightMergedValuesBuffer: ClArray<'b>) (isLeftBitmap: ClArray<int>) ->
@@ -209,10 +130,10 @@ module internal Map2AtLeastOne =
209130

210131
let kernel = clContext.Compile(merge)
211132

212-
fun (processor: MailboxProcessor<_>) (matrixLeftRows: ClArray<int>) (matrixLeftColumns: ClArray<int>) (matrixLeftValues: ClArray<'a>) (matrixRightRows: ClArray<int>) (matrixRightColumns: ClArray<int>) (matrixRightValues: ClArray<'b>) ->
133+
fun (processor: MailboxProcessor<_>) (leftMatrix: ClMatrix.COO<'a>) (rightMatrix: ClMatrix.COO<'b>) ->
213134

214-
let firstSide = matrixLeftValues.Length
215-
let secondSide = matrixRightValues.Length
135+
let firstSide = leftMatrix.Columns.Length
136+
let secondSide = rightMatrix.Columns.Length
216137
let sumOfSides = firstSide + secondSide
217138

218139
let allRows =
@@ -243,12 +164,12 @@ module internal Map2AtLeastOne =
243164
firstSide
244165
secondSide
245166
sumOfSides
246-
matrixLeftRows
247-
matrixLeftColumns
248-
matrixLeftValues
249-
matrixRightRows
250-
matrixRightColumns
251-
matrixRightValues
167+
leftMatrix.Rows
168+
leftMatrix.Columns
169+
leftMatrix.Values
170+
rightMatrix.Rows
171+
rightMatrix.Columns
172+
rightMatrix.Values
252173
allRows
253174
allColumns
254175
leftMergedValues
@@ -259,54 +180,3 @@ module internal Map2AtLeastOne =
259180
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
260181

261182
allRows, allColumns, leftMergedValues, rightMergedValues, isLeft
262-
263-
///<param name="clContext">.</param>
264-
///<param name="opAdd">.</param>
265-
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
266-
let run<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
267-
(clContext: ClContext)
268-
(opAdd: Expr<'a option -> 'b option -> 'c option>)
269-
workGroupSize
270-
=
271-
272-
let merge = merge clContext workGroupSize
273-
274-
let preparePositions =
275-
preparePositionsAtLeastOne clContext opAdd workGroupSize
276-
277-
let setPositions =
278-
Common.setPositions<'c> clContext workGroupSize
279-
280-
fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.COO<'a>) (matrixRight: ClMatrix.COO<'b>) ->
281-
282-
let allRows, allColumns, leftMergedValues, rightMergedValues, isLeft =
283-
merge
284-
queue
285-
matrixLeft.Rows
286-
matrixLeft.Columns
287-
matrixLeft.Values
288-
matrixRight.Rows
289-
matrixRight.Columns
290-
matrixRight.Values
291-
292-
let rawPositions, allValues =
293-
preparePositions queue allRows allColumns leftMergedValues rightMergedValues isLeft
294-
295-
queue.Post(Msg.CreateFreeMsg<_>(leftMergedValues))
296-
queue.Post(Msg.CreateFreeMsg<_>(rightMergedValues))
297-
298-
let resultRows, resultColumns, resultValues, _ =
299-
setPositions queue allocationMode allRows allColumns allValues rawPositions
300-
301-
queue.Post(Msg.CreateFreeMsg<_>(isLeft))
302-
queue.Post(Msg.CreateFreeMsg<_>(rawPositions))
303-
queue.Post(Msg.CreateFreeMsg<_>(allRows))
304-
queue.Post(Msg.CreateFreeMsg<_>(allColumns))
305-
queue.Post(Msg.CreateFreeMsg<_>(allValues))
306-
307-
{ Context = clContext
308-
RowCount = matrixLeft.RowCount
309-
ColumnCount = matrixLeft.ColumnCount
310-
Rows = resultRows
311-
Columns = resultColumns
312-
Values = resultValues }

0 commit comments

Comments
 (0)