Skip to content

Commit c75fd57

Browse files
committed
wip: ArithmeticOps
1 parent 3d0b07b commit c75fd57

6 files changed

Lines changed: 227 additions & 173 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
<Compile Include="Objects/ClContextExtensions.fs" />
2020
<Compile Include="Objects/ClCell.fs" />
2121
<Compile Include="Quotes/SubReduce.fs" />
22+
<Compile Include="Quotes\Convert.fs" />
2223
<Compile Include="Quotes/Arithmetic.fs" />
23-
<Compile Include="Quotes/Convert.fs" />
2424
<Compile Include="Quotes/Mask.fs" />
2525
<Compile Include="Quotes/SubSum.fs" />
2626
<Compile Include="Quotes/PreparePositions.fs" />

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

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
open GraphBLAS.FSharp.Backend.Objects
44

55
module ArithmeticOperations =
6-
let inline mkUnaryOp zero unaryOp =
6+
// unary
7+
let inline optionUnOp zero unaryOp =
78
<@ fun x ->
89
let mutable res = zero
910

@@ -13,120 +14,104 @@ module ArithmeticOperations =
1314

1415
if res = zero then None else Some res @>
1516

16-
let inline mkNumericSum zero =
17-
<@ fun (x: 't option) (y: 't option) ->
18-
let mutable res = zero
19-
20-
match x, y with
21-
| Some f, Some s -> res <- f + s
22-
| Some f, None -> res <- f
23-
| None, Some s -> res <- s
24-
| None, None -> ()
17+
let inline addLeftConst zero constant =
18+
optionUnOp zero <@ fun x -> constant + x @>
2519

26-
if res = zero then None else Some res @>
20+
let inline addRightConst zero constant =
21+
optionUnOp zero <@ fun x -> x + constant @>
2722

28-
let inline mkNumericSumAtLeastOne zero =
29-
<@ fun (values: AtLeastOne<'t, 't>) ->
30-
let mutable res = zero
23+
let inline mulLeftConst zero constant =
24+
optionUnOp zero <@ fun x -> constant * x @>
3125

32-
match values with
33-
| Both (f, s) -> res <- f + s
34-
| Left f -> res <- f
35-
| Right s -> res <- s
26+
let inline mulRightConst zero constant =
27+
optionUnOp zero <@ fun x -> x * constant @>
3628

37-
if res = zero then None else Some res @>
29+
// binary
3830

39-
let inline mkNumericMul zero =
40-
<@ fun (x: 't option) (y: 't option) ->
31+
let inline optionBinOpQ zero binOp =
32+
<@ fun (x: 'a option) (y: 'a option) ->
4133
let mutable res = zero
4234

4335
match x, y with
44-
| Some f, Some s -> res <- f * s
45-
| _ -> ()
36+
| Some f, Some s -> res <- (%binOp) f s
37+
| Some f, None -> res <- f
38+
| None, Some s -> res <- s
39+
| None, None -> ()
4640

4741
if res = zero then None else Some res @>
4842

49-
let inline mkNumericMulAtLeastOne zero =
50-
<@ fun (values: AtLeastOne<'t, 't>) ->
43+
let inline optionBinOp zero binOp =
44+
fun (x: 'a option) (y: 'a option) ->
5145
let mutable res = zero
5246

53-
match values with
54-
| Both (f, s) -> res <- f * s
55-
| _ -> ()
56-
57-
if res = zero then None else Some res @>
58-
59-
let boolSum =
60-
<@ fun (x: bool option) (y: bool option) ->
61-
let mutable res = false
62-
6347
match x, y with
48+
| Some left, Some right -> res <- binOp left right
49+
| Some left, None -> res <- left
50+
| None, Some right -> res <- right
6451
| None, None -> ()
65-
| _ -> res <- true
6652

67-
if res then Some true else None @>
53+
if res = zero then None else Some res
6854

69-
let inline addLeftConst zero constant =
70-
mkUnaryOp zero <@ fun x -> constant + x @>
55+
let createOptionPair zero opQ op =
56+
optionBinOpQ zero opQ, optionBinOp zero op
7157

72-
let inline addRightConst zero constant =
73-
mkUnaryOp zero <@ fun x -> x + constant @>
58+
let inline createOptionSumPair zero = createOptionPair zero <@ (+) @> (+)
7459

75-
let intSumOption = mkNumericSum 0
76-
let byteSumOption = mkNumericSum 0uy
77-
let floatSumOption = mkNumericSum 0.0
78-
let float32SumOption = mkNumericSum 0f
60+
let intSumOption = createOptionSumPair 0
61+
let byteSumOption = createOptionSumPair 0uy
62+
let floatSumOption = createOptionSumPair 0.0
63+
let float32SumOption = createOptionSumPair 0f
7964

80-
let boolSumAtLeastOne =
81-
<@ fun (_: AtLeastOne<bool, bool>) -> Some true @>
65+
let boolSumOption = createOptionPair false <@ (||) @> (||)
8266

83-
let intSumAtLeastOne = mkNumericSumAtLeastOne 0
84-
let byteSumAtLeastOne = mkNumericSumAtLeastOne 0uy
85-
let floatSumAtLeastOne = mkNumericSumAtLeastOne 0.0
86-
let float32SumAtLeastOne = mkNumericSumAtLeastOne 0f
67+
let inline createOptionMulPair zero = createOptionPair zero <@ (*) @> (*)
8768

88-
let boolMulOption =
89-
<@ fun (x: bool option) (y: bool option) ->
90-
let mutable res = false
69+
let intMulOption = createOptionMulPair 0
70+
let byteMulOption = createOptionMulPair 0uy
71+
let floatMulOption = createOptionMulPair 0.0
72+
let float32MulOption = createOptionMulPair 0f
9173

92-
match x, y with
93-
| Some _, Some _ -> res <- true
94-
| _ -> ()
74+
let boolMulOption = createOptionPair true <@ (&&) @> (&&)
9575

96-
if res then Some true else None @>
76+
let inline atLeastOneBinOpQ zero binOp =
77+
Convert.optionToAtLeastOne <| optionBinOpQ zero binOp
9778

98-
let inline mulLeftConst zero constant =
99-
mkUnaryOp zero <@ fun x -> constant * x @>
79+
let inline atLeastOneBinOp zero binOp =
80+
let optionOp = optionBinOp zero binOp
81+
// convert AtLeastOne -> Option
82+
function
83+
| Both (left, right) -> optionOp (Some left) (Some right)
84+
| Left left -> optionOp (Some left) None
85+
| Right right -> optionOp None (Some right)
10086

101-
let inline mulRightConst zero constant =
102-
mkUnaryOp zero <@ fun x -> x * constant @>
87+
let inline createAtLeastOnePair zero opQ op =
88+
atLeastOneBinOpQ zero opQ, atLeastOneBinOp zero op
89+
90+
let inline createAtLeastOneSumPair zero = createAtLeastOnePair zero <@ (+) @> (+)
10391

104-
let intMulOption = mkNumericMul 0
105-
let byteMulOption = mkNumericMul 0uy
106-
let floatMulOption = mkNumericMul 0.0
107-
let float32MulOption = mkNumericMul 0f
92+
let intSumAtLeastOne = createAtLeastOneSumPair 0
93+
let byteSumAtLeastOne = createAtLeastOneSumPair 0uy
94+
let floatSumAtLeastOne = createAtLeastOneSumPair 0.0
95+
let float32SumAtLeastOne = createAtLeastOneSumPair 0f
10896

109-
let boolMulAtLeastOne =
110-
<@ fun (values: AtLeastOne<bool, bool>) ->
111-
let mutable res = false
97+
let boolSumAtLeastOne = createAtLeastOnePair false <@ (||) @> (||)
11298

113-
match values with
114-
| Both _ -> res <- true
115-
| _ -> ()
99+
let inline createAtLeastOneMulPair zero = createAtLeastOnePair zero <@ (*) @> (*)
116100

117-
if res then Some true else None @>
101+
let intMulAtLeastOne = createAtLeastOneMulPair 0
102+
let byteMulAtLeastOne = createAtLeastOneMulPair 0uy
103+
let floatMulAtLeastOne = createAtLeastOneMulPair 0.0
104+
let float32MulAtLeastOne = createAtLeastOneMulPair 0f
118105

119-
let intMulAtLeastOne = mkNumericMulAtLeastOne 0
120-
let byteMulAtLeastOne = mkNumericMulAtLeastOne 0uy
121-
let floatMulAtLeastOne = mkNumericMulAtLeastOne 0.0
122-
let float32MulAtLeastOne = mkNumericMulAtLeastOne 0f
106+
let boolMulAtLeastOne = createAtLeastOnePair true <@ (&&) @> (&&)
123107

124108
let notOption =
125109
<@ fun x ->
126110
match x with
127111
| Some true -> None
128112
| _ -> Some true @>
129113

114+
// unwrapped operands
130115
let inline private binOpQ zero op =
131116
<@ fun (left: 'a) (right: 'a) ->
132117
let result = (%op) left right

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

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

15+
let optionToAtLeastOne (op: Expr<'a option -> 'b option -> 'c option>) =
16+
<@ fun (item: AtLeastOne<'a, 'b>) ->
17+
match item with
18+
| Both (left, right) -> (%op) (Some left) (Some right)
19+
| Left left -> (%op) (Some left) None
20+
| Right right -> (%op) None (Some right) @>
21+
1522
let assignToOption (op: Expr<'a option -> 'a option -> 'a option>) =
1623
<@ fun (leftItem: 'a option) (rightItem: 'b option) (value: 'a) ->
1724
match rightItem with

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="Vector/Reduce.fs" />
5050
<Compile Include="Vector/Map2.fs" />
5151
<Compile Include="Vector/SpMV.fs" />
52+
<Compile Include="Vector\Map.fs" />
5253
<Compile Include="Algorithms/BFS.fs" />
5354
<Compile Include="Matrix/Convert.fs" />
5455
<Compile Include="Matrix/Map2.fs" />

0 commit comments

Comments
 (0)