Skip to content

Commit 6a41198

Browse files
committed
refactor: Vector.fillSubVector, .fillSubVectorComplemented
1 parent d884d1c commit 6a41198

5 files changed

Lines changed: 44 additions & 46 deletions

File tree

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

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

3+
open FSharp.Quotations
4+
35
type AtLeastOne<'a, 'b when 'a: struct and 'b: struct> =
46
| Both of 'a * 'b
57
| Left of 'a
@@ -102,36 +104,28 @@ module StandardOperations =
102104
let floatMulAtLeastOne = mkNumericMulAtLeastOne 0.0
103105
let float32MulAtLeastOne = mkNumericMulAtLeastOne 0f
104106

105-
let mask<'a, 'b when 'a: struct and 'b: struct> =
106-
<@ fun (left: 'a option) (right: 'b option) value ->
107-
match left, right with
108-
| _, None -> left
109-
| _ -> Some value @>
110-
111-
let maskAtLeastOne<'a, 'b when 'a: struct and 'b: struct> =
112-
<@ fun (pair: AtLeastOne<'a, 'b>) value ->
113-
match pair with
114-
| Left left -> Some left
115-
| _ -> Some value @>
116-
117-
let complementedMask<'a, 'b when 'a: struct and 'b: struct> =
118-
<@ fun (left: 'a option) (right: 'b option) value ->
119-
match left, right with
120-
| _, Some _ -> left
121-
| _ -> Some value @>
122-
123-
let atLeastOneToNormalForm op =
107+
let atLeastOneToOption op =
124108
<@ fun (leftItem: 'a option) (rightItem: 'b option) ->
125109
match leftItem, rightItem with
126110
| Some left, Some right -> (%op) (Both(left, right))
127111
| None, Some right -> (%op) (Right right)
128112
| Some left, None -> (%op) (Left left)
129113
| None, None -> None @>
130114

131-
let fillSubVectorAtLeastOneToNormalForm op =
115+
let fillSubToOption (op: Expr<'a option -> 'a option -> 'a option>) =
132116
<@ fun (leftItem: 'a option) (rightItem: 'b option) (value: 'a) ->
133-
match leftItem, rightItem with
134-
| Some left, Some right -> (%op) (Both(left, right)) value
135-
| None, Some right -> (%op) (Right right) value
136-
| Some left, None -> (%op) (Left left) value
137-
| None, None -> None @>
117+
match rightItem with
118+
| Some _ -> (%op) leftItem (Some value)
119+
| None -> (%op) leftItem None @>
120+
121+
let fillSubComplementedToOption (op: Expr<'a option -> 'a option -> 'a option>) =
122+
<@ fun (leftItem: 'a option) (rightItem: 'b option) (value: 'a) ->
123+
match rightItem with
124+
| Some _ -> (%op) leftItem None
125+
| None -> (%op) leftItem (Some value) @>
126+
127+
let mask<'a when 'a: struct> =
128+
<@ fun (left: 'a option) (right: 'a option) ->
129+
match left, right with
130+
| _, None -> left
131+
| _ -> right @>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module DenseVector =
4646
resultVector
4747

4848
let elementWiseAtLeastOne clContext op workGroupSize =
49-
elementWise clContext (StandardOperations.atLeastOneToNormalForm op) workGroupSize
49+
elementWise clContext (StandardOperations.atLeastOneToOption op) workGroupSize
5050

5151
let fillSubVector<'a, 'b when 'a: struct and 'b: struct>
5252
(clContext: ClContext)
@@ -87,9 +87,6 @@ module DenseVector =
8787

8888
resultVector
8989

90-
let fillSubVectorAtLeasOne clContext opAdd workGroupSize =
91-
fillSubVector clContext (StandardOperations.fillSubVectorAtLeastOneToNormalForm opAdd) workGroupSize
92-
9390
let private getBitmap<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) =
9491

9592
let getPositions =

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ module SparseVector =
218218
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
219219
(workGroupSize: int)
220220
=
221-
elementWise clContext (StandardOperations.atLeastOneToNormalForm opAdd) workGroupSize
221+
elementWise clContext (StandardOperations.atLeastOneToOption opAdd) workGroupSize
222222

223223
let private preparePositionsFillSubVector<'a, 'b when 'a: struct and 'b: struct>
224224
(clContext: ClContext)
@@ -309,9 +309,6 @@ module SparseVector =
309309
Indices = resultIndices
310310
Size = max leftVector.Size rightVector.Size }
311311

312-
let fillSubVectorAtLeastOne (clContext: ClContext) opAdd (workGroupSize: int) =
313-
fillSubVector clContext (StandardOperations.fillSubVectorAtLeastOneToNormalForm opAdd) workGroupSize
314-
315312
let toDense (clContext: ClContext) (workGroupSize: int) =
316313

317314
let toDense =

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ module Vector =
118118
| ClVectorSparse left, ClVectorSparse right -> ClVectorSparse <| addSparse processor left right
119119
| _ -> failwith "Vector formats are not matching."
120120

121-
let fillSubVector (clContext: ClContext) maskOp (workGroupSize: int) =
121+
let fillSubVector<'a, 'b when 'a: struct and 'b: struct> maskOp (clContext: ClContext) (workGroupSize: int) =
122122
let sparseFillVector =
123-
SparseVector.fillSubVector clContext maskOp workGroupSize
123+
SparseVector.fillSubVector clContext (StandardOperations.fillSubToOption maskOp) workGroupSize
124124

125125
let denseFillVector =
126-
DenseVector.fillSubVector clContext maskOp workGroupSize
126+
DenseVector.fillSubVector clContext (StandardOperations.fillSubToOption maskOp) workGroupSize
127127

128128
let toSparseVector =
129129
DenseVector.toSparse clContext workGroupSize
@@ -150,9 +150,13 @@ module Vector =
150150
ClVectorDense
151151
<| denseFillVector processor vector mask value
152152

153-
let fillSubVectorComplemented (clContext: ClContext) maskOp (workGroupSize: int) =
153+
let fillSubVectorComplemented<'a, 'b when 'a: struct and 'b: struct>
154+
maskOp
155+
(clContext: ClContext)
156+
(workGroupSize: int)
157+
=
154158
let denseFillVector =
155-
DenseVector.fillSubVector clContext maskOp workGroupSize
159+
DenseVector.fillSubVector clContext (StandardOperations.fillSubComplementedToOption maskOp) workGroupSize
156160

157161
let vectorToDense =
158162
SparseVector.toDense clContext workGroupSize
@@ -182,6 +186,12 @@ module Vector =
182186
ClVectorDense
183187
<| denseFillVector processor vector mask value
184188

189+
let standardFillSubVector<'a, 'b when 'a: struct and 'b: struct> =
190+
fillSubVector<'a, 'b> StandardOperations.mask<'a>
191+
192+
let standardFillSubVectorComplemented<'a, 'b when 'a: struct and 'b: struct> =
193+
fillSubVectorComplemented<'a, 'b> StandardOperations.mask<'a>
194+
185195
let reduce (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) =
186196
let sparseReduce =
187197
SparseVector.reduce clContext workGroupSize opAdd

tests/GraphBLAS-sharp.Tests/Vector/FillSubVector.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ let testFixtures case =
124124
let isComplemented = false
125125

126126
[ let intFill =
127-
Vector.fillSubVector context StandardOperations.mask wgSize
127+
Vector.standardFillSubVector context wgSize
128128

129129
let intToCoo = Vector.toSparse context wgSize
130130

@@ -133,7 +133,7 @@ let testFixtures case =
133133
|> testPropertyWithConfig config (getCorrectnessTestName "int")
134134

135135
let floatFill =
136-
Vector.fillSubVector context StandardOperations.mask wgSize
136+
Vector.standardFillSubVector context wgSize
137137

138138
let floatToCoo = Vector.toSparse context wgSize
139139

@@ -142,7 +142,7 @@ let testFixtures case =
142142
|> testPropertyWithConfig config (getCorrectnessTestName "float")
143143

144144
let byteFill =
145-
Vector.fillSubVector context StandardOperations.mask wgSize
145+
Vector.standardFillSubVector context wgSize
146146

147147
let byteToCoo = Vector.toSparse context wgSize
148148

@@ -151,7 +151,7 @@ let testFixtures case =
151151
|> testPropertyWithConfig config (getCorrectnessTestName "byte")
152152

153153
let boolFill =
154-
Vector.fillSubVector context StandardOperations.mask wgSize
154+
Vector.standardFillSubVector context wgSize
155155

156156
let boolToCoo = Vector.toSparse context wgSize
157157

@@ -177,7 +177,7 @@ let testFixturesComplemented case =
177177
let isComplemented = true
178178

179179
[ let intFill =
180-
Vector.fillSubVectorComplemented context StandardOperations.complementedMask wgSize
180+
Vector.standardFillSubVectorComplemented context wgSize
181181

182182
let intToCoo = Vector.toSparse context wgSize
183183

@@ -186,7 +186,7 @@ let testFixturesComplemented case =
186186
|> testPropertyWithConfig config (getCorrectnessTestName "int")
187187

188188
let floatFill =
189-
Vector.fillSubVectorComplemented context StandardOperations.complementedMask wgSize
189+
Vector.standardFillSubVectorComplemented context wgSize
190190

191191
let floatToCoo = Vector.toSparse context wgSize
192192

@@ -195,7 +195,7 @@ let testFixturesComplemented case =
195195
|> testPropertyWithConfig config (getCorrectnessTestName "float")
196196

197197
let byteFill =
198-
Vector.fillSubVectorComplemented context StandardOperations.complementedMask wgSize
198+
Vector.standardFillSubVectorComplemented context wgSize
199199

200200
let byteToCoo = Vector.toSparse context wgSize
201201

@@ -204,7 +204,7 @@ let testFixturesComplemented case =
204204
|> testPropertyWithConfig config (getCorrectnessTestName "byte")
205205

206206
let boolFill =
207-
Vector.fillSubVectorComplemented context StandardOperations.complementedMask wgSize
207+
Vector.standardFillSubVectorComplemented context wgSize
208208

209209
let boolToCoo = Vector.toSparse context wgSize
210210

0 commit comments

Comments
 (0)