Skip to content

Commit ad66391

Browse files
committed
refactor: ClArray.assign -> *.blit
1 parent 7bdaf88 commit ad66391

15 files changed

Lines changed: 88 additions & 75 deletions

File tree

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,24 @@ GraphBLAS# is a GPGPU-based [GraphBLAS](https://graphblas.org/)-like API impleme
2525

2626
### Operations
2727
- **Matrix-Matrix**
28-
- [x] COO-COO element-wize
29-
- [x] CSR-CSR element-wize
30-
- [ ] CSR-CSR multiplication
31-
- [ ] COO transpose
32-
- [ ] CSR transpose
28+
- [x] COO-COO `map2`
29+
- [x] CSR-CSR `map2`
30+
- [x] CSR-CSR multiplication
3331
- **Vector-Matrix**
3432
- [x] Dense-CSR multiplication
3533
- [ ] COO-CSR multiplication
3634
- **Vector-Vector**
3735
- [x] Dense-Dense element-wise
36+
- [x] Sparse-Sparse element-wise
3837
- [ ] ...
3938
- **Matrix**
40-
- [ ] `map`
39+
- [x] `map`
40+
- [x] COO transpose
41+
- [x] CSR transpose
4142
- [ ] `iter`
4243
- [ ] ...
4344
- **Vector**
44-
- [ ] `map`
45+
- [x] `map`
4546
- [ ] `iter`
4647
- [ ] `filter`
4748
- [ ] `contains`

src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ module BFS =
2929
let ofList = Vector.ofList clContext workGroupSize
3030

3131
let maskComplementedTo =
32-
DenseVector.map2Inplace clContext Mask.complementedOp workGroupSize
32+
Vector.map2InPlace clContext Mask.complementedOp workGroupSize
3333

3434
let fillSubVectorTo =
35-
DenseVector.assignByMaskInplace clContext (Convert.assignToOption Mask.assign) workGroupSize
35+
Vector.assignByMaskInPlace clContext (Convert.assignToOption Mask.assign) workGroupSize
3636

3737
let containsNonZero =
3838
ClArray.exists clContext workGroupSize Predicates.isSome

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -559,43 +559,53 @@ module ClArray =
559559
|> Seq.map (fun lazyValue -> lazyValue.Value)
560560
|> Seq.toArray
561561

562-
let assign<'a> (clContext: ClContext) workGroupSize =
562+
let blit<'a> (clContext: ClContext) workGroupSize =
563563

564564
let assign =
565-
<@ fun (ndRange: Range1D) targetPosition sourceArrayLength (sourceArray: ClArray<'a>) (targetArray: ClArray<'a>) ->
565+
<@ fun (ndRange: Range1D) sourceIndex (sourceArray: ClArray<'a>) (targetArray: ClArray<'a>) targetPosition count ->
566566

567567
let gid = ndRange.GlobalID0
568568

569-
let resultPosition = gid + targetPosition
570-
571-
if gid < sourceArrayLength then
569+
if gid < count then
570+
let readPosition = gid + sourceIndex
571+
let writePosition = gid + targetPosition
572572

573-
targetArray.[resultPosition] <- sourceArray.[gid] @>
573+
targetArray.[writePosition] <- sourceArray.[readPosition] @>
574574

575575
let kernel = clContext.Compile assign
576576

577-
fun (processor: MailboxProcessor<_>) (sourceArray: ClArray<'a>) targetPosition (targetArray: ClArray<'a>) ->
578-
if targetPosition < 0 then
579-
failwith "The starting position cannot be less than zero"
577+
fun (processor: MailboxProcessor<_>) (sourceArray: ClArray<'a>) sourceIndex (targetArray: ClArray<'a>) targetIndex count ->
578+
// check count
579+
if count < 0 then failwith "Count must be greater than zero"
580580

581-
if targetPosition + sourceArray.Length > targetArray.Length then
582-
failwith "The array should fit completely"
581+
// check sourceIndex
582+
if sourceIndex < 0
583+
&& sourceIndex + count >= sourceArray.Length
584+
then failwith "The source index does not match"
583585

584-
let ndRange =
585-
Range1D.CreateValid(targetArray.Length, workGroupSize)
586+
// check targetPosition
587+
if targetIndex < 0
588+
&& targetIndex + count >= targetArray.Length
589+
then failwith "The target index does not match"
586590

587-
let kernel = kernel.GetKernel()
591+
if count = 0 then ()
592+
// nothing to do
593+
else
594+
let ndRange =
595+
Range1D.CreateValid(targetArray.Length, workGroupSize)
588596

589-
processor.Post(
590-
Msg.MsgSetArguments
591-
(fun () -> kernel.KernelFunc ndRange targetPosition sourceArray.Length sourceArray targetArray)
592-
)
597+
let kernel = kernel.GetKernel()
593598

594-
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
599+
processor.Post(
600+
Msg.MsgSetArguments
601+
(fun () -> kernel.KernelFunc ndRange sourceIndex sourceArray targetArray targetIndex count)
602+
)
603+
604+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
595605

596606
let concat (clContext: ClContext) workGroupSize =
597607

598-
let assign = assign clContext workGroupSize
608+
let blit = blit clContext workGroupSize
599609

600610
fun (processor: MailboxProcessor<_>) allocationMode (sourceArrays: ClArray<'a> seq) ->
601611

@@ -609,7 +619,7 @@ module ClArray =
609619
// write each array to result
610620
Seq.fold
611621
(fun previousLength (array: ClArray<_>) ->
612-
assign processor array previousLength result
622+
blit processor array 0 result previousLength array.Length
613623
previousLength + array.Length)
614624
0
615625
sourceArrays

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
<Compile Include="Common/Sort/Radix.fs" />
3636
<Compile Include="Common/Sort/Bitonic.fs" />
3737
<Compile Include="Common/Sum.fs" />
38-
<Compile Include="Vector/Dense/DenseVector.fs" />
38+
<Compile Include="Vector\Dense\Vector.fs" />
3939
<Compile Include="Vector/Sparse/Common.fs" />
4040
<Compile Include="Vector/Sparse/Map2AtLeastOne.fs" />
4141
<Compile Include="Vector/Sparse/Map2.fs" />
42-
<Compile Include="Vector/Sparse/SparseVector.fs" />
42+
<Compile Include="Vector\Sparse\Vector.fs" />
4343
<Compile Include="Vector/SpMV.fs" />
4444
<Compile Include="Vector/Vector.fs" />
4545
<Compile Include="Matrix/Common.fs" />

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ open GraphBLAS.FSharp.Backend.Quotes
66
open Microsoft.FSharp.Quotations
77
open GraphBLAS.FSharp.Backend.Objects
88
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
9+
open GraphBLAS.FSharp.Backend.Objects.ClCell
10+
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
911

1012
module Matrix =
1113
let map = Map.run
@@ -78,8 +80,7 @@ module Matrix =
7880
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange rowIndices nnz rowPointers))
7981
processor.Post(Msg.CreateRunMsg<_, _> kernel)
8082

81-
let result = scan processor rowPointers nnz
82-
processor.Post <| Msg.CreateFreeMsg(result)
83+
(scan processor rowPointers nnz).Free processor
8384

8485
rowPointers
8586

@@ -114,7 +115,7 @@ module Matrix =
114115
let rowPointers =
115116
prepare processor allocationMode matrix.Rows matrix.RowCount
116117

117-
processor.Post(Msg.CreateFreeMsg(matrix.Rows))
118+
matrix.Rows.Free processor
118119

119120
{ Context = clContext
120121
RowCount = matrix.RowCount

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ open GraphBLAS.FSharp.Backend
99
open GraphBLAS.FSharp.Backend.Matrix.COO
1010
open GraphBLAS.FSharp.Backend.Objects
1111
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
12+
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
1213

1314
module internal Map2AtLeastOne =
1415
let preparePositions<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
@@ -310,8 +311,8 @@ module internal Map2AtLeastOne =
310311
let positions, allValues =
311312
preparePositions queue allColumns leftMergedValues rightMergedValues isRowEnd isLeft
312313

313-
queue.Post(Msg.CreateFreeMsg<_>(leftMergedValues))
314-
queue.Post(Msg.CreateFreeMsg<_>(rightMergedValues))
314+
leftMergedValues.Free queue
315+
rightMergedValues.Free queue
315316

316317
let resultRows, resultColumns, resultValues, _ =
317318
setPositions queue allocationMode allRows allColumns allValues positions
@@ -338,9 +339,9 @@ module internal Map2AtLeastOne =
338339

339340
let elementwiseToCOO = runToCOO clContext opAdd workGroupSize
340341

341-
let toCSRInplace =
342+
let toCSRInPlace =
342343
Matrix.toCSRInPlace clContext workGroupSize
343344

344345
fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSR<'b>) ->
345346
elementwiseToCOO queue allocationMode matrixLeft matrixRight
346-
|> toCSRInplace queue allocationMode
347+
|> toCSRInPlace queue allocationMode

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Matrix =
1616
let copyData = ClArray.copy clContext workGroupSize
1717

1818
let vectorCopy =
19-
Vector.Sparse.SparseVector.copy clContext workGroupSize
19+
Vector.Sparse.Vector.copy clContext workGroupSize
2020

2121
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
2222
match matrix with
@@ -237,7 +237,7 @@ module Matrix =
237237
let COOToCSR = COO.Matrix.toCSR clContext workGroupSize
238238

239239
let transposeCSR =
240-
CSR.Matrix.transposeInPlace clContext workGroupSize
240+
CSR.Matrix.transpose clContext workGroupSize
241241

242242
let CSRToLIL = CSR.Matrix.toLIL clContext workGroupSize
243243

src/GraphBLAS-sharp.Backend/Vector/Dense/DenseVector.fs renamed to src/GraphBLAS-sharp.Backend/Vector/Dense/Vector.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ open GraphBLAS.FSharp.Backend.Objects.ClVector
88
open GraphBLAS.FSharp.Backend.Objects.ClContext
99
open GraphBLAS.FSharp.Backend.Objects.ClCell
1010

11-
module DenseVector =
12-
let map2Inplace<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
11+
module Vector =
12+
let map2InPlace<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
1313
(clContext: ClContext)
1414
(opAdd: Expr<'a option -> 'b option -> 'c option>)
1515
workGroupSize
@@ -40,7 +40,7 @@ module DenseVector =
4040
let map2AtLeastOne clContext op workGroupSize =
4141
map2 clContext (Convert.atLeastOneToOption op) workGroupSize
4242

43-
let assignByMaskInplace<'a, 'b when 'a: struct and 'b: struct>
43+
let assignByMaskInPlace<'a, 'b when 'a: struct and 'b: struct>
4444
(clContext: ClContext)
4545
(maskOp: Expr<'a option -> 'b option -> 'a -> 'a option>)
4646
workGroupSize
@@ -77,7 +77,7 @@ module DenseVector =
7777
=
7878

7979
let assignByMask =
80-
assignByMaskInplace clContext maskOp workGroupSize
80+
assignByMaskInPlace clContext maskOp workGroupSize
8181

8282
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClArray<'a option>) (maskVector: ClArray<'b option>) (value: ClCell<'a>) ->
8383
let resultVector =

src/GraphBLAS-sharp.Backend/Vector/Sparse/SparseVector.fs renamed to src/GraphBLAS-sharp.Backend/Vector/Sparse/Vector.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open Microsoft.FSharp.Quotations
88
open GraphBLAS.FSharp.Backend.Objects
99
open GraphBLAS.FSharp.Backend.Objects.ClVector
1010

11-
module SparseVector =
11+
module Vector =
1212
let copy (clContext: ClContext) workGroupSize =
1313
let copy = ClArray.copy clContext workGroupSize
1414

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ open GraphBLAS.FSharp.Backend.Quotes
55
open Microsoft.FSharp.Control
66
open Microsoft.FSharp.Quotations
77
open GraphBLAS.FSharp.Backend.Common
8-
open GraphBLAS.FSharp.Backend.Vector.Dense
9-
open GraphBLAS.FSharp.Backend.Vector.Sparse
108
open GraphBLAS.FSharp.Backend.Objects
119
open GraphBLAS.FSharp.Backend.Objects.ClContext
1210
open GraphBLAS.FSharp.Backend.Objects.ClVector
@@ -80,7 +78,7 @@ module Vector =
8078

8179
let copy (clContext: ClContext) workGroupSize =
8280
let sparseCopy =
83-
SparseVector.copy clContext workGroupSize
81+
Sparse.Vector.copy clContext workGroupSize
8482

8583
let copyOptionData = ClArray.copy clContext workGroupSize
8684

@@ -95,7 +93,7 @@ module Vector =
9593

9694
let toSparse (clContext: ClContext) workGroupSize =
9795
let toSparse =
98-
DenseVector.toSparse clContext workGroupSize
96+
Dense.Vector.toSparse clContext workGroupSize
9997

10098
let copy = copy clContext workGroupSize
10199

@@ -108,7 +106,7 @@ module Vector =
108106

109107
let toDense (clContext: ClContext) workGroupSize =
110108
let toDense =
111-
SparseVector.toDense clContext workGroupSize
109+
Sparse.Vector.toDense clContext workGroupSize
112110

113111
let copy = ClArray.copy clContext workGroupSize
114112

@@ -123,10 +121,10 @@ module Vector =
123121

124122
let map2 (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
125123
let map2Dense =
126-
DenseVector.map2 clContext opAdd workGroupSize
124+
Dense.Vector.map2 clContext opAdd workGroupSize
127125

128126
let map2Sparse =
129-
SparseVector.map2 clContext opAdd workGroupSize
127+
Sparse.Vector.map2 clContext opAdd workGroupSize
130128

131129
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClVector<'a>) (rightVector: ClVector<'b>) ->
132130
match leftVector, rightVector with
@@ -140,10 +138,10 @@ module Vector =
140138

141139
let map2AtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
142140
let map2Sparse =
143-
SparseVector.map2AtLeastOne clContext opAdd workGroupSize
141+
Sparse.Vector.map2AtLeastOne clContext opAdd workGroupSize
144142

145143
let map2Dense =
146-
DenseVector.map2AtLeastOne clContext opAdd workGroupSize
144+
Dense.Vector.map2AtLeastOne clContext opAdd workGroupSize
147145

148146
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClVector<'a>) (rightVector: ClVector<'b>) ->
149147
match leftVector, rightVector with
@@ -158,10 +156,10 @@ module Vector =
158156
let private assignByMaskGeneral<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) op workGroupSize =
159157

160158
let sparseFillVector =
161-
SparseVector.assignByMask clContext op workGroupSize
159+
Sparse.Vector.assignByMask clContext op workGroupSize
162160

163161
let denseFillVector =
164-
DenseVector.assignByMask clContext op workGroupSize
162+
Dense.Vector.assignByMask clContext op workGroupSize
165163

166164
fun (processor: MailboxProcessor<_>) allocationMode (vector: ClVector<'a>) (mask: ClVector<'b>) (value: ClCell<'a>) ->
167165
match vector, mask with
@@ -181,10 +179,10 @@ module Vector =
181179

182180
let reduce (clContext: ClContext) workGroupSize (opAdd: Expr<'a -> 'a -> 'a>) =
183181
let sparseReduce =
184-
SparseVector.reduce clContext workGroupSize opAdd
182+
Sparse.Vector.reduce clContext workGroupSize opAdd
185183

186184
let denseReduce =
187-
DenseVector.reduce clContext workGroupSize opAdd
185+
Dense.Vector.reduce clContext workGroupSize opAdd
188186

189187
fun (processor: MailboxProcessor<_>) (vector: ClVector<'a>) ->
190188
match vector with

0 commit comments

Comments
 (0)