Skip to content

Commit 4b7c3ed

Browse files
committed
wip: expand refactor, tests in progress
1 parent 2db0be2 commit 4b7c3ed

16 files changed

Lines changed: 442 additions & 243 deletions

File tree

paket.dependencies

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ group Docs
5959
group Analyzers
6060
source https://www.nuget.org/api/v2
6161
source https://api.nuget.org/v3/index.json
62-
nuget BinaryDefense.FSharp.Analyzers.Hashing 0.2.2
62+
nuget BinaryDefense.FSharp.Analyzers.Hashing 0.2.2

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ module ClArray =
335335
let getUniqueBitmap2LastOccurrence clContext =
336336
getUniqueBitmap2General getUniqueBitmapLastOccurrence clContext
337337

338-
let private assignOption (clContext: ClContext) workGroupSize (op: Expr<'a -> 'b option>) =
338+
let assignOption (clContext: ClContext) workGroupSize (op: Expr<'a -> 'b option>) =
339339

340340
let assign =
341341
<@ fun (ndRange: Range1D) length (values: ClArray<'a>) (positions: ClArray<int>) (result: ClArray<'b>) resultLength ->
@@ -574,7 +574,7 @@ module ClArray =
574574

575575
let kernel = clContext.Compile assign
576576

577-
fun (processor: MailboxProcessor<_>) allocationMode (targetArray: ClArray<'a>) startPosition (appendedArray: ClArray<'a>) ->
577+
fun (processor: MailboxProcessor<_>) (targetArray: ClArray<'a>) startPosition (appendedArray: ClArray<'a>) ->
578578
if startPosition < 0 then
579579
failwith "The starting position cannot be less than zero"
580580

@@ -605,9 +605,9 @@ module ClArray =
605605
|> Seq.sumBy (fun array -> array.Length)
606606

607607
let result =
608-
clContext.CreateClArrayWithSpecificAllocationMode(DeviceOnly, resultLength)
608+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
609609

610-
let assign = assign processor allocationMode result
610+
let assign = assign processor result
611611

612612
// write each array to result
613613
Seq.fold
@@ -652,3 +652,27 @@ module ClArray =
652652
)
653653

654654
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
655+
656+
let pairwise (clContext: ClContext) workGroupSize =
657+
658+
let idGather =
659+
Gather.runInit Map.id clContext workGroupSize
660+
661+
let incGather =
662+
Gather.runInit Map.inc clContext workGroupSize
663+
664+
fun (processor: MailboxProcessor<_>) allocationMode (values: ClArray<'a>) ->
665+
666+
let resultLength = values.Length - 1
667+
668+
let firstItems =
669+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
670+
671+
idGather processor values firstItems
672+
673+
let secondItems =
674+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
675+
676+
incGather processor values secondItems
677+
678+
firstItems, secondItems

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

Lines changed: 133 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ module Reduce =
315315

316316
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
317317

318-
reducedKeys, reducedValues
318+
reducedValues, reducedKeys
319319

320320
/// <summary>
321321
/// Reduces values by key. Each segment is reduced by one work item.
@@ -381,7 +381,7 @@ module Reduce =
381381

382382
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
383383

384-
reducedKeys, reducedValues
384+
reducedValues, reducedKeys
385385

386386
/// <summary>
387387
/// Reduces values by key. One work group participates in the reduction.
@@ -470,8 +470,120 @@ module Reduce =
470470

471471
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
472472

473-
reducedKeys, reducedValues
473+
reducedValues, reducedKeys
474474

475+
/// <summary>
476+
/// Reduces values by key. Each segment is reduced by one work item.
477+
/// </summary>
478+
/// <param name="clContext">ClContext.</param>
479+
/// <param name="workGroupSize">Work group size.</param>
480+
/// <param name="reduceOp">Operation for reducing values.</param>
481+
/// <remarks>
482+
/// The length of the result must be calculated in advance.
483+
/// </remarks>
484+
let segmentSequentialOption<'a> (clContext: ClContext) workGroupSize (reduceOp: Expr<'a -> 'a -> 'a option>) =
485+
486+
let kernel =
487+
<@ fun (ndRange: Range1D) uniqueKeyCount keysLength (offsets: ClArray<int>) (keys: ClArray<int>) (values: ClArray<'a>) (reducedValues: ClArray<'a>) (firstReducedKeys: ClArray<int>) (resultPositions: ClArray<int>) ->
488+
489+
let gid = ndRange.GlobalID0
490+
491+
if gid < uniqueKeyCount then
492+
let startPosition = offsets.[gid]
493+
494+
let firstSourceKey = keys.[startPosition]
495+
496+
let mutable sum = Some values.[startPosition]
497+
498+
let mutable currentPosition = startPosition + 1
499+
500+
while currentPosition < keysLength
501+
&& firstSourceKey = keys.[currentPosition] do
502+
503+
match sum with
504+
| Some value ->
505+
let result =
506+
((%reduceOp) value values.[currentPosition]) // brahma error
507+
508+
sum <- result
509+
| None -> sum <- Some values.[currentPosition]
510+
511+
currentPosition <- currentPosition + 1
512+
513+
match sum with
514+
| Some value ->
515+
reducedValues.[gid] <- value
516+
resultPositions.[gid] <- 1
517+
| None -> resultPositions.[gid] <- 0
518+
519+
firstReducedKeys.[gid] <- firstSourceKey @>
520+
521+
let kernel = clContext.Compile kernel
522+
523+
let scatterData =
524+
Scatter.lastOccurrence clContext workGroupSize
525+
526+
let scatterIndices =
527+
Scatter.lastOccurrence clContext workGroupSize
528+
529+
let prefixSum =
530+
PrefixSum.standardExcludeInplace clContext workGroupSize
531+
532+
fun (processor: MailboxProcessor<_>) allocationMode (resultLength: int) (offsets: ClArray<int>) (keys: ClArray<int>) (values: ClArray<'a>) ->
533+
534+
let reducedValues =
535+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
536+
537+
let reducedKeys =
538+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
539+
540+
let resultPositions =
541+
clContext.CreateClArrayWithSpecificAllocationMode(DeviceOnly, resultLength)
542+
543+
let ndRange =
544+
Range1D.CreateValid(resultLength, workGroupSize)
545+
546+
let kernel = kernel.GetKernel()
547+
548+
processor.Post(
549+
Msg.MsgSetArguments
550+
(fun () ->
551+
kernel.KernelFunc
552+
ndRange
553+
resultLength
554+
keys.Length
555+
offsets
556+
keys
557+
values
558+
reducedValues
559+
reducedKeys
560+
resultPositions)
561+
)
562+
563+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
564+
565+
let resultLength =
566+
(prefixSum processor resultPositions)
567+
.ToHostAndFree processor
568+
569+
if resultLength = 0 then None
570+
else
571+
let resultValues =
572+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
573+
574+
scatterData processor resultPositions reducedValues resultValues
575+
576+
reducedValues.Free processor
577+
578+
let resultKeys =
579+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
580+
581+
scatterIndices processor resultPositions reducedKeys resultKeys // TODO(mb error)
582+
583+
reducedKeys.Free processor
584+
resultPositions.Free processor
585+
586+
Some (resultValues, reducedKeys)
475587
module ByKey2D =
476588
/// <summary>
477589
/// Reduce an array of values by 2D keys using a single work item.
@@ -550,7 +662,7 @@ module Reduce =
550662

551663
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
552664

553-
firstReducedKeys, secondReducedKeys, reducedValues
665+
reducedValues, firstReducedKeys, secondReducedKeys
554666

555667
/// <summary>
556668
/// Reduces values by key. Each segment is reduced by one work item.
@@ -625,7 +737,7 @@ module Reduce =
625737

626738
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
627739

628-
firstReducedKeys, secondReducedKeys, reducedValues
740+
reducedValues, firstReducedKeys, secondReducedKeys
629741

630742
/// <summary>
631743
/// Reduces values by key. Each segment is reduced by one work item.
@@ -729,27 +841,29 @@ module Reduce =
729841
(prefixSum processor resultPositions)
730842
.ToHostAndFree processor
731843

732-
let resultValues =
733-
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
844+
if resultLength = 0 then None
845+
else
846+
let resultValues =
847+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
734848

735-
scatterData processor resultPositions reducedValues resultValues
849+
scatterData processor resultPositions reducedValues resultValues
736850

737-
reducedValues.Free processor
851+
reducedValues.Free processor
738852

739-
let resultFirstKeys =
740-
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
853+
let resultFirstKeys =
854+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
741855

742-
scatterIndices processor resultPositions firstReducedKeys resultFirstKeys
856+
scatterIndices processor resultPositions firstReducedKeys resultFirstKeys
743857

744-
firstReducedKeys.Free processor
858+
firstReducedKeys.Free processor
745859

746-
let resultSecondKeys =
747-
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
860+
let resultSecondKeys =
861+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
748862

749-
scatterIndices processor resultPositions secondReducedKeys resultSecondKeys
863+
scatterIndices processor resultPositions secondReducedKeys resultSecondKeys
750864

751-
secondReducedKeys.Free processor
865+
secondReducedKeys.Free processor
752866

753-
resultPositions.Free processor
867+
resultPositions.Free processor
754868

755-
resultFirstKeys, resultSecondKeys, resultValues
869+
Some (resultValues, resultFirstKeys, resultSecondKeys)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
<Compile Include="Common/Scatter.fs" />
3131
<Compile Include="Common/Utils.fs" />
3232
<Compile Include="Common/PrefixSum.fs" />
33+
<Compile Include="Common\Gather.fs" />
3334
<Compile Include="Common/ClArray.fs" />
34-
<Compile Include="Common/Gather.fs" />
3535
<Compile Include="Common/Sort/Radix.fs" />
3636
<Compile Include="Common/Sort/Bitonic.fs" />
3737
<Compile Include="Common/Sum.fs" />
@@ -51,10 +51,10 @@
5151
<Compile Include="Matrix\CSR\Map.fs" />
5252
<Compile Include="Matrix\CSR\Map2.fs" />
5353
<Compile Include="Matrix\CSR\Map2AtLeastOne.fs" />
54-
<Compile Include="Matrix\CSR\SpGEMM\Masked.fs" />
55-
<Compile Include="Matrix\CSR\SpGEMM\Expand.fs" />
5654
<Compile Include="Matrix\CSR\Matrix.fs" />
5755
<Compile Include="Matrix\Rows\Matrix.fs" />
56+
<Compile Include="Matrix\SpGeMM\Expand.fs" />
57+
<Compile Include="Matrix\SpGeMM\Masked.fs" />
5858
<Compile Include="Matrix\Matrix.fs" />
5959
<Compile Include="Algorithms/BFS.fs" />
6060
</ItemGroup>

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

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Matrix =
4040
Columns = cols
4141
Values = values }
4242

43-
let toCOOInplace (clContext: ClContext) workGroupSize =
43+
let toCOOInPlace (clContext: ClContext) workGroupSize =
4444
let prepare =
4545
Common.expandRowPointers clContext workGroupSize
4646

@@ -77,35 +77,35 @@ module Matrix =
7777

7878
Map2AtLeastOne.run clContext (Convert.atLeastOneToOption opAdd) workGroupSize
7979

80-
let transposeInplace (clContext: ClContext) workGroupSize =
80+
let transposeInPlace (clContext: ClContext) workGroupSize =
8181

82-
let toCOOInplace = toCOOInplace clContext workGroupSize
82+
let toCOOInPlace = toCOOInPlace clContext workGroupSize
8383

84-
let transposeInplace =
84+
let transposeInPlace =
8585
COO.Matrix.transposeInplace clContext workGroupSize
8686

87-
let toCSRInplace =
87+
let toCSRInPlace =
8888
COO.Matrix.toCSRInplace clContext workGroupSize
8989

9090
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
91-
toCOOInplace queue allocationMode matrix
92-
|> transposeInplace queue
93-
|> toCSRInplace queue allocationMode
91+
toCOOInPlace queue allocationMode matrix
92+
|> transposeInPlace queue
93+
|> toCSRInPlace queue allocationMode
9494

9595
let transpose (clContext: ClContext) workGroupSize =
9696

9797
let toCOO = toCOO clContext workGroupSize
9898

99-
let transposeInplace =
99+
let transposeInPlace =
100100
COO.Matrix.transposeInplace clContext workGroupSize
101101

102-
let toCSRInplace =
102+
let toCSRInPlace =
103103
COO.Matrix.toCSRInplace clContext workGroupSize
104104

105105
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
106106
toCOO queue allocationMode matrix
107-
|> transposeInplace queue
108-
|> toCSRInplace queue allocationMode
107+
|> transposeInPlace queue
108+
|> toCSRInPlace queue allocationMode
109109

110110
let byRowsLazy (clContext: ClContext) workGroupSize =
111111

@@ -148,40 +148,3 @@ module Matrix =
148148
runLazy processor allocationMode matrix
149149
|> Seq.map (fun lazyValue -> lazyValue.Value)
150150
|> Seq.toArray
151-
152-
module SpGeMM =
153-
let masked
154-
(clContext: ClContext)
155-
workGroupSize
156-
(opAdd: Expr<'c -> 'c -> 'c option>)
157-
(opMul: Expr<'a -> 'b -> 'c option>)
158-
=
159-
160-
let run =
161-
SpGeMM.Masked.run clContext workGroupSize opAdd opMul
162-
163-
fun (queue: MailboxProcessor<_>) (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSC<'b>) (mask: ClMatrix.COO<_>) ->
164-
165-
run queue matrixLeft matrixRight mask
166-
167-
let expand
168-
(clContext: ClContext)
169-
workGroupSize
170-
(opAdd: Expr<'c -> 'c -> 'c option>)
171-
(opMul: Expr<'a -> 'b -> 'c option>)
172-
=
173-
174-
let run =
175-
SpGeMM.Expand.run clContext workGroupSize opAdd opMul
176-
177-
fun (queue: MailboxProcessor<_>) allocationMode (leftMatrix: ClMatrix.CSR<'a>) (rightMatrix: ClMatrix.CSR<'b>) ->
178-
179-
let values, columns, rows =
180-
run queue allocationMode leftMatrix rightMatrix
181-
182-
{ COO.Context = clContext
183-
ColumnCount = rightMatrix.ColumnCount
184-
RowCount = leftMatrix.RowCount
185-
Values = values
186-
Columns = columns
187-
Rows = rows }

0 commit comments

Comments
 (0)