Skip to content

Commit f6090db

Browse files
committed
add: general Vector.assignByMask, tests
1 parent 1f9d687 commit f6090db

10 files changed

Lines changed: 61 additions & 98 deletions

File tree

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

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

3030
let maskComplementedTo =
31-
DenseVector.map2To clContext Mask.complementedMaskOp workGroupSize
31+
DenseVector.map2Inplace clContext Mask.complementedOp workGroupSize
3232

3333
let fillSubVectorTo =
34-
DenseVector.standardFillSubVectorTo<int, int> clContext workGroupSize
34+
DenseVector.assignByMaskInplace clContext (Convert.assignToOption Mask.assign) workGroupSize
3535

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

src/GraphBLAS-sharp.Backend/Quotes/Convert.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ module Convert =
1212
| Some left, None -> (%op) (Left left)
1313
| None, None -> None @>
1414

15-
let fillSubToOption (op: Expr<'a option -> 'a option -> 'a option>) =
15+
let assignToOption (op: Expr<'a option -> 'a option -> 'a option>) =
1616
<@ fun (leftItem: 'a option) (rightItem: 'b option) (value: 'a) ->
1717
match rightItem with
1818
| Some _ -> (%op) leftItem (Some value)
1919
| None -> (%op) leftItem None @>
2020

21-
let fillSubComplementedToOption (op: Expr<'a option -> 'a option -> 'a option>) =
21+
let assignComplementedToOption (op: Expr<'a option -> 'a option -> 'a option>) =
2222
<@ fun (leftItem: 'a option) (rightItem: 'b option) (value: 'a) ->
2323
match rightItem with
2424
| Some _ -> (%op) leftItem None

src/GraphBLAS-sharp.Backend/Quotes/Mask.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
namespace GraphBLAS.FSharp.Backend.Quotes
22

33
module Mask =
4-
let fillSubOp<'a when 'a: struct> =
4+
let assign<'a when 'a: struct> =
55
<@ fun (left: 'a option) (right: 'a option) ->
66
match left, right with
77
| _, None -> left
88
| _ -> right @>
99

10-
let maskOp<'a, 'b when 'a: struct and 'b: struct> =
10+
let op<'a, 'b when 'a: struct and 'b: struct> =
1111
<@ fun (left: 'a option) (right: 'b option) ->
1212
match right with
1313
| Some _ -> left
1414
| _ -> None @>
1515

16-
let complementedMaskOp<'a, 'b when 'a: struct and 'b: struct> =
16+
let complementedOp<'a, 'b when 'a: struct and 'b: struct> =
1717
<@ fun (left: 'a option) (right: 'b option) ->
1818
match right with
1919
| None -> left

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ open GraphBLAS.FSharp.Backend.Objects.ClVector
99
open GraphBLAS.FSharp.Backend.Objects.ClContext
1010

1111
module DenseVector =
12-
let map2To<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
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
1616
=
1717

18-
let elementWise =
18+
let map2 =
1919
<@ fun (ndRange: Range1D) resultLength (leftVector: ClArray<'a option>) (rightVector: ClArray<'b option>) (resultVector: ClArray<'c option>) ->
2020

2121
let gid = ndRange.GlobalID0
2222

2323
if gid < resultLength then
2424
resultVector.[gid] <- (%opAdd) leftVector.[gid] rightVector.[gid] @>
2525

26-
let kernel = clContext.Compile(elementWise)
26+
let kernel = clContext.Compile(map2)
2727

2828
fun (processor: MailboxProcessor<_>) (leftVector: ClArray<'a option>) (rightVector: ClArray<'b option>) (resultVector: ClArray<'c option>) ->
2929

@@ -45,7 +45,8 @@ module DenseVector =
4545
workGroupSize
4646
=
4747

48-
let elementWiseTo = map2To clContext opAdd workGroupSize
48+
let elementWiseTo =
49+
map2Inplace clContext opAdd workGroupSize
4950

5051
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClArray<'a option>) (rightVector: ClArray<'b option>) ->
5152
let resultVector =
@@ -58,7 +59,7 @@ module DenseVector =
5859
let map2AtLeastOne clContext op workGroupSize =
5960
map2 clContext (Convert.atLeastOneToOption op) workGroupSize
6061

61-
let fillSubVectorTo<'a, 'b when 'a: struct and 'b: struct>
62+
let assignByMaskInplace<'a, 'b when 'a: struct and 'b: struct>
6263
(clContext: ClContext)
6364
(maskOp: Expr<'a option -> 'b option -> 'a -> 'a option>)
6465
workGroupSize
@@ -88,26 +89,23 @@ module DenseVector =
8889

8990
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
9091

91-
let fillSubVector<'a, 'b when 'a: struct and 'b: struct>
92+
let assignByMask<'a, 'b when 'a: struct and 'b: struct>
9293
(clContext: ClContext)
9394
(maskOp: Expr<'a option -> 'b option -> 'a -> 'a option>)
9495
workGroupSize
9596
=
9697

97-
let fillSubVectorTo =
98-
fillSubVectorTo clContext maskOp workGroupSize
98+
let assignByMask =
99+
assignByMaskInplace clContext maskOp workGroupSize
99100

100101
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClArray<'a option>) (maskVector: ClArray<'b option>) (value: ClCell<'a>) ->
101102
let resultVector =
102103
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, leftVector.Length)
103104

104-
fillSubVectorTo processor leftVector maskVector value resultVector
105+
assignByMask processor leftVector maskVector value resultVector
105106

106107
resultVector
107108

108-
let standardFillSubVectorTo<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) workGroupSize =
109-
fillSubVectorTo<'a, 'b> clContext (Convert.fillSubToOption Mask.fillSubOp<'a>) workGroupSize
110-
111109
let private getBitmap<'a when 'a: struct> (clContext: ClContext) workGroupSize =
112110

113111
let getPositions =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module Map2 =
4949
resultBitmap.[gid] <- 1
5050
| None -> resultBitmap.[gid] <- 0 @>
5151

52-
let prepareFillGeneral op =
52+
let prepareAssign op =
5353
<@ fun (ndRange: Range1D) length leftValuesLength rightValuesLength (leftValues: ClArray<'a>) (leftIndices: ClArray<int>) (rightValues: ClArray<'b>) (rightIndices: ClArray<int>) (value: ClCell<'a>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'c>) (resultIndices: ClArray<int>) ->
5454

5555
let gid = ndRange.GlobalID0

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,14 @@ module SparseVector =
251251
let map2AtLeastOne (clContext: ClContext) opAdd workGroupSize allocationMode =
252252
map2 clContext (Convert.atLeastOneToOption opAdd) workGroupSize allocationMode
253253

254-
let private preparePositionsFillSubVector<'a, 'b when 'a: struct and 'b: struct>
254+
let private preparePositionsAssignByMask<'a, 'b when 'a: struct and 'b: struct>
255255
(clContext: ClContext)
256256
op
257257
workGroupSize
258258
=
259259

260260
let kernel =
261-
clContext.Compile(Map2.prepareFillGeneral op)
261+
clContext.Compile(Map2.prepareAssign op)
262262

263263
fun (processor: MailboxProcessor<_>) (vectorLenght: int) (leftValues: ClArray<'a>) (leftIndices: ClArray<int>) (rightValues: ClArray<'b>) (rightIndices: ClArray<int>) (value: ClCell<'a>) ->
264264

@@ -301,10 +301,10 @@ module SparseVector =
301301
///<param name="clContext">.</param>
302302
///<param name="op">.</param>
303303
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
304-
let fillSubVector<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) op workGroupSize =
304+
let assignByMask<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) op workGroupSize =
305305

306306
let prepare =
307-
preparePositionsFillSubVector clContext op workGroupSize
307+
preparePositionsAssignByMask clContext op workGroupSize
308308

309309
let setPositions = setPositions clContext workGroupSize
310310

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

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -127,38 +127,38 @@ module Vector =
127127
ClVector.Dense
128128
<| toDense processor allocationMode vector
129129

130-
let map2AtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
131-
let addSparse =
132-
SparseVector.map2AtLeastOne clContext opAdd workGroupSize
133-
130+
let map2 (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
134131
let addDense =
135-
DenseVector.map2AtLeastOne clContext opAdd workGroupSize
132+
DenseVector.map2 clContext opAdd workGroupSize
133+
134+
let addSparse =
135+
SparseVector.map2 clContext opAdd workGroupSize
136136

137137
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClVector<'a>) (rightVector: ClVector<'b>) ->
138138
match leftVector, rightVector with
139-
| ClVector.Sparse left, ClVector.Sparse right ->
140-
ClVector.Sparse
141-
<| addSparse processor allocationMode left right
142139
| ClVector.Dense left, ClVector.Dense right ->
143140
ClVector.Dense
144141
<| addDense processor allocationMode left right
142+
| ClVector.Sparse left, ClVector.Sparse right ->
143+
ClVector.Sparse
144+
<| addSparse processor allocationMode left right
145145
| _ -> failwith "Vector formats are not matching."
146146

147-
let map2 (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
148-
let addDense =
149-
DenseVector.map2 clContext opAdd workGroupSize
150-
147+
let map2AtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
151148
let addSparse =
152-
SparseVector.map2 clContext opAdd workGroupSize
149+
SparseVector.map2AtLeastOne clContext opAdd workGroupSize
150+
151+
let addDense =
152+
DenseVector.map2AtLeastOne clContext opAdd workGroupSize
153153

154154
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClVector<'a>) (rightVector: ClVector<'b>) ->
155155
match leftVector, rightVector with
156-
| ClVector.Dense left, ClVector.Dense right ->
157-
ClVector.Dense
158-
<| addDense processor allocationMode left right
159156
| ClVector.Sparse left, ClVector.Sparse right ->
160157
ClVector.Sparse
161158
<| addSparse processor allocationMode left right
159+
| ClVector.Dense left, ClVector.Dense right ->
160+
ClVector.Dense
161+
<| addDense processor allocationMode left right
162162
| _ -> failwith "Vector formats are not matching."
163163

164164
let map2General<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
@@ -183,21 +183,22 @@ module Vector =
183183
<| denseEWise processor allocationMode left right
184184
| _ -> failwith "Vector formats are not matching."
185185

186-
let fillSubVector<'a, 'b when 'a: struct and 'b: struct> maskOp (clContext: ClContext) workGroupSize =
186+
let private assignByMaskGeneral<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) op workGroupSize =
187+
187188
let sparseFillVector =
188-
SparseVector.fillSubVector clContext (Convert.fillSubToOption maskOp) workGroupSize
189+
SparseVector.assignByMask clContext op workGroupSize
189190

190191
let denseFillVector =
191-
DenseVector.fillSubVector clContext (Convert.fillSubToOption maskOp) workGroupSize
192+
DenseVector.assignByMask clContext op workGroupSize
192193

193194
let toSparseVector =
194195
DenseVector.toSparse clContext workGroupSize
195196

196197
let toSparseMask =
197198
DenseVector.toSparse clContext workGroupSize
198199

199-
fun (processor: MailboxProcessor<_>) allocationMode (vector: ClVector<'a>) (maskVector: ClVector<'b>) (value: ClCell<'a>) ->
200-
match vector, maskVector with
200+
fun (processor: MailboxProcessor<_>) allocationMode (vector: ClVector<'a>) (mask: ClVector<'b>) (value: ClCell<'a>) ->
201+
match vector, mask with
201202
| ClVector.Sparse vector, ClVector.Sparse mask ->
202203
ClVector.Sparse
203204
<| sparseFillVector processor allocationMode vector mask value
@@ -217,48 +218,11 @@ module Vector =
217218
ClVector.Dense
218219
<| denseFillVector processor allocationMode vector mask value
219220

220-
let fillSubVectorComplemented<'a, 'b when 'a: struct and 'b: struct> maskOp (clContext: ClContext) workGroupSize =
221-
222-
let denseFillVector =
223-
DenseVector.fillSubVector clContext (Convert.fillSubComplementedToOption maskOp) workGroupSize
224-
225-
let vectorToDense =
226-
SparseVector.toDense clContext workGroupSize
227-
228-
let maskToDense =
229-
SparseVector.toDense clContext workGroupSize
230-
231-
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClVector<'a>) (maskVector: ClVector<'b>) (value: ClCell<'a>) ->
232-
match leftVector, maskVector with
233-
| ClVector.Sparse vector, ClVector.Sparse mask ->
234-
let denseVector =
235-
vectorToDense processor allocationMode vector
236-
237-
let denseMask =
238-
maskToDense processor allocationMode mask
239-
240-
ClVector.Dense
241-
<| denseFillVector processor allocationMode denseVector denseMask value
242-
| ClVector.Dense vector, ClVector.Sparse mask ->
243-
let denseMask =
244-
maskToDense processor allocationMode mask
245-
246-
ClVector.Dense
247-
<| denseFillVector processor allocationMode vector denseMask value
248-
| ClVector.Sparse vector, ClVector.Dense mask ->
249-
let denseVector =
250-
vectorToDense processor allocationMode vector
251-
252-
ClVector.Dense
253-
<| denseFillVector processor allocationMode denseVector mask value
254-
| ClVector.Dense vector, ClVector.Dense mask ->
255-
ClVector.Dense
256-
<| denseFillVector processor allocationMode vector mask value
257-
258-
let standardFillSubVector<'a, 'b when 'a: struct and 'b: struct> = fillSubVector<'a, 'b> Mask.fillSubOp<'a>
221+
let assignByMask<'a, 'b when 'a: struct and 'b: struct> clContext op workGroupSize =
222+
assignByMaskGeneral<'a, 'b> clContext (Convert.assignToOption op) workGroupSize
259223

260-
let standardFillSubVectorComplemented<'a, 'b when 'a: struct and 'b: struct> =
261-
fillSubVectorComplemented<'a, 'b> Mask.fillSubOp<'a>
224+
let assignByMaskComplemented<'a, 'b when 'a: struct and 'b: struct> clContext op workGroupSize =
225+
assignByMaskGeneral<'a, 'b> clContext (Convert.assignComplementedToOption op) workGroupSize
262226

263227
let reduce (clContext: ClContext) workGroupSize (opAdd: Expr<'a -> 'a -> 'a>) =
264228
let sparseReduce =

tests/GraphBLAS-sharp.Tests/GraphBLAS-sharp.Tests.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<Compile Include="Vector/ZeroCreate.fs" />
3232
<Compile Include="Vector/OfList.fs" />
3333
<Compile Include="Vector/Copy.fs" />
34-
<Compile Include="Vector/FillSubVector.fs" />
34+
<Compile Include="Vector/AssignByMask.fs" />
3535
<Compile Include="Vector/Convert.fs" />
3636
<Compile Include="Vector/Reduce.fs" />
3737
<Compile Include="Vector/Map2.fs" />

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ let vectorTests =
4242
Vector.Map2.addGeneralTests
4343
Vector.Map2.mulGeneralTests
4444
Vector.Map2.complementedGeneralTests
45-
Vector.FillSubVector.tests
46-
Vector.FillSubVector.complementedTests
45+
Vector.AssignByMask.tests
46+
Vector.AssignByMask.complementedTests
4747
Vector.Reduce.tests ]
4848
|> testSequenced
4949

0 commit comments

Comments
 (0)