Skip to content

Commit a60b12a

Browse files
committed
add: spgemm row wise
1 parent cf48196 commit a60b12a

24 files changed

Lines changed: 435 additions & 386 deletions

File tree

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,7 @@ module ClArray =
588588

589589
processor.Post(
590590
Msg.MsgSetArguments
591-
(fun () ->
592-
kernel.KernelFunc ndRange targetPosition sourceArray.Length sourceArray targetArray)
591+
(fun () -> kernel.KernelFunc ndRange targetPosition sourceArray.Length sourceArray targetArray)
593592
)
594593

595594
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
@@ -632,22 +631,25 @@ module ClArray =
632631
let kernel = clContext.Compile fill
633632

634633
fun (processor: MailboxProcessor<_>) value firstPosition count (targetArray: ClArray<'a>) ->
635-
if count = 0 then ()
634+
if count = 0 then
635+
()
636636
else
637637
if firstPosition + count > targetArray.Length then
638638
failwith "The array should fit completely"
639639

640-
if firstPosition < 0 then failwith "The starting position cannot be less than zero"
641-
if count < 0 then failwith "The count cannot be less than zero"
640+
if firstPosition < 0 then
641+
failwith "The starting position cannot be less than zero"
642+
643+
if count < 0 then
644+
failwith "The count cannot be less than zero"
642645

643646
let ndRange =
644647
Range1D.CreateValid(count, workGroupSize)
645648

646649
let kernel = kernel.GetKernel()
647650

648651
processor.Post(
649-
Msg.MsgSetArguments
650-
(fun () -> kernel.KernelFunc ndRange firstPosition count value targetArray)
652+
Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange firstPosition count value targetArray)
651653
)
652654

653655
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
@@ -660,7 +662,8 @@ module ClArray =
660662
let incGather =
661663
Gather.runInit Map.inc clContext workGroupSize
662664

663-
let map = map2 clContext workGroupSize <@ fun first second -> (first, second) @>
665+
let map =
666+
map2 clContext workGroupSize <@ fun first second -> (first, second) @>
664667

665668
fun (processor: MailboxProcessor<_>) allocationMode (values: ClArray<'a>) ->
666669
if values.Length > 1 then
@@ -676,10 +679,12 @@ module ClArray =
676679

677680
incGather processor values secondItems
678681

679-
let result = map processor allocationMode firstItems secondItems
682+
let result =
683+
map processor allocationMode firstItems secondItems
680684

681685
firstItems.Free processor
682686
secondItems.Free processor
683687

684688
Some result
685-
else None
689+
else
690+
None

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ module Reduce =
567567
(prefixSum processor resultPositions)
568568
.ToHostAndFree processor
569569

570-
if resultLength = 0 then None
570+
if resultLength = 0 then
571+
None
571572
else
572573
// write values
573574
let resultValues =
@@ -586,7 +587,8 @@ module Reduce =
586587
reducedKeys.Free processor
587588
resultPositions.Free processor
588589

589-
Some (resultValues, resultKeys)
590+
Some(resultValues, resultKeys)
591+
590592
module ByKey2D =
591593
/// <summary>
592594
/// Reduce an array of values by 2D keys using a single work item.
@@ -845,7 +847,8 @@ module Reduce =
845847
(prefixSum processor resultPositions)
846848
.ToHostAndFree processor
847849

848-
if resultLength = 0 then None
850+
if resultLength = 0 then
851+
None
849852
else
850853
// write value
851854
let resultValues =
@@ -873,4 +876,4 @@ module Reduce =
873876

874877
resultPositions.Free processor
875878

876-
Some (resultValues, resultFirstKeys, resultSecondKeys)
879+
Some(resultValues, resultFirstKeys, resultSecondKeys)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module Matrix =
162162
Rows = rows
163163
NNZ = matrix.NNZ }
164164

165-
let getRowsLength (clContext: ClContext) workGroupSize =
165+
let NNZInRows (clContext: ClContext) workGroupSize =
166166

167167
let pairwise = ClArray.pairwise clContext workGroupSize
168168

@@ -173,10 +173,11 @@ module Matrix =
173173
let pointerPairs =
174174
pairwise processor DeviceOnly matrix.RowPointers
175175
// since row pointers length in matrix always >= 2
176-
|> Option.defaultWith (fun () ->
177-
failwith "The state of the matrix is broken. The length of the rowPointers must be >= 2")
176+
|> Option.defaultWith
177+
(fun () -> failwith "The state of the matrix is broken. The length of the rowPointers must be >= 2")
178178

179-
let rowsLength = subtract processor allocationMode pointerPairs
179+
let rowsLength =
180+
subtract processor allocationMode pointerPairs
180181

181182
pointerPairs.Free processor
182183

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module Matrix =
4444
Rows = copy processor allocationMode m.Rows
4545
ColumnPointers = copy processor allocationMode m.ColumnPointers
4646
Values = copyData processor allocationMode m.Values }
47-
| ClMatrix.Rows matrix ->
47+
| ClMatrix.LIL matrix ->
4848
matrix.Rows
4949
|> Array.map (
5050
Option.bind
@@ -56,7 +56,7 @@ module Matrix =
5656
ColumnCount = matrix.ColumnCount
5757
Rows = rows
5858
NNZ = matrix.NNZ }
59-
|> ClMatrix.Rows
59+
|> ClMatrix.LIL
6060

6161
/// <summary>
6262
/// Creates a new matrix, represented in CSR format, that is equal to the given one.
@@ -82,7 +82,7 @@ module Matrix =
8282
m.ToCSR
8383
|> transpose processor allocationMode
8484
|> ClMatrix.CSR
85-
| ClMatrix.Rows m ->
85+
| ClMatrix.LIL m ->
8686
rowsToCSR processor allocationMode m
8787
|> ClMatrix.CSR
8888

@@ -136,7 +136,7 @@ module Matrix =
136136
|> toCOO processor allocationMode
137137
|> transposeInPlace processor
138138
|> ClMatrix.COO
139-
| ClMatrix.Rows m ->
139+
| ClMatrix.LIL m ->
140140
rowsToCSR processor allocationMode m
141141
|> toCOO processor allocationMode
142142
|> ClMatrix.COO
@@ -183,7 +183,8 @@ module Matrix =
183183
let transposeCOO =
184184
COO.Matrix.transpose clContext workGroupSize
185185

186-
let rowsToCSR = Rows.Matrix.toCSR clContext workGroupSize
186+
let rowsToCSR =
187+
Rows.Matrix.toCSR clContext workGroupSize
187188

188189
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
189190
match matrix with
@@ -196,7 +197,7 @@ module Matrix =
196197
|> COOtoCSR processor allocationMode)
197198
.ToCSC
198199
|> ClMatrix.CSC
199-
| ClMatrix.Rows m ->
200+
| ClMatrix.LIL m ->
200201
rowsToCSR processor allocationMode m
201202
|> transposeCSR processor allocationMode
202203
|> fun m -> m.ToCSC
@@ -241,23 +242,24 @@ module Matrix =
241242
let transposeCSR =
242243
CSR.Matrix.transposeInPlace clContext workGroupSize
243244

244-
let CSRToRows = CSR.Matrix.toRows clContext workGroupSize
245+
let CSRToRows =
246+
CSR.Matrix.toRows clContext workGroupSize
245247

246248
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
247249
match matrix with
248250
| ClMatrix.CSC m ->
249251
m.ToCSR
250252
|> transposeCSR processor allocationMode
251253
|> CSRToRows processor allocationMode
252-
|> ClMatrix.Rows
254+
|> ClMatrix.LIL
253255
| ClMatrix.CSR m ->
254256
CSRToRows processor allocationMode m
255-
|> ClMatrix.Rows
257+
|> ClMatrix.LIL
256258
| ClMatrix.COO m ->
257259
COOToCSR processor allocationMode m
258260
|> CSRToRows processor allocationMode
259-
|> ClMatrix.Rows
260-
| ClMatrix.Rows _ -> copy processor allocationMode matrix
261+
|> ClMatrix.LIL
262+
| ClMatrix.LIL _ -> copy processor allocationMode matrix
261263

262264
let map (clContext: ClContext) (opAdd: Expr<'a option -> 'b option>) workGroupSize =
263265
let mapCOO =
@@ -363,7 +365,7 @@ module Matrix =
363365
| ClMatrix.COO m -> COOTransposeInPlace processor m |> ClMatrix.COO
364366
| ClMatrix.CSR m -> ClMatrix.CSC m.ToCSC
365367
| ClMatrix.CSC m -> ClMatrix.CSR m.ToCSR
366-
| ClMatrix.Rows _ -> failwith "Not yet implemented"
368+
| ClMatrix.LIL _ -> failwith "Not yet implemented"
367369

368370
/// <summary>
369371
/// Transposes the given matrix and returns result as a new matrix.
@@ -406,7 +408,7 @@ module Matrix =
406408
Columns = copy processor allocationMode m.Rows
407409
Values = copyData processor allocationMode m.Values }
408410
|> ClMatrix.CSR
409-
| ClMatrix.Rows _ -> failwith "Not yet implemented"
411+
| ClMatrix.LIL _ -> failwith "Not yet implemented"
410412

411413
module SpGeMM =
412414
let masked
@@ -437,5 +439,6 @@ module Matrix =
437439
fun (processor: MailboxProcessor<_>) allocationMode (leftMatrix: ClMatrix<'a>) (rightMatrix: ClMatrix<'b>) ->
438440
match leftMatrix, rightMatrix with
439441
| ClMatrix.CSR leftMatrix, ClMatrix.CSR rightMatrix ->
440-
ClMatrix.Rows <| run processor allocationMode leftMatrix rightMatrix
442+
ClMatrix.LIL
443+
<| run processor allocationMode leftMatrix rightMatrix
441444
| _ -> failwith "Matrix formats are not matching"

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ module Matrix =
1717

1818
let concatValues = ClArray.concat clContext workGroupSize
1919

20-
fun (processor: MailboxProcessor<_>) allocationMode (matrix: Rows<'a>) ->
20+
fun (processor: MailboxProcessor<_>) allocationMode (matrix: LIL<'a>) ->
2121

2222
let rowsPointers =
2323
matrix.Rows
24-
|> Array.map (function None -> 0 | Some vector -> vector.Values.Length)
24+
|> Array.map
25+
(function
26+
| None -> 0
27+
| Some vector -> vector.Values.Length)
2528
// prefix sum
2629
|> Array.scan (+) 0
27-
|> fun pointers ->
28-
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, pointers)
30+
|> fun pointers -> clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, pointers)
2931

3032
let valuesByRows, columnsIndicesByRows =
3133
matrix.Rows
@@ -45,5 +47,3 @@ module Matrix =
4547
RowPointers = rowsPointers
4648
Columns = columnsIndices
4749
Values = values }
48-
49-

0 commit comments

Comments
 (0)