Skip to content

Commit 45baacd

Browse files
committed
refactor: move Matrix.map, Vector.map; add: Vector.map tests
1 parent 4c5dede commit 45baacd

10 files changed

Lines changed: 214 additions & 64 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ open GraphBLAS.FSharp.Objects.ClMatrix
1111
open GraphBLAS.FSharp.Objects.ClContextExtensions
1212

1313
module internal Map =
14-
let preparePositions<'a, 'b> opAdd (clContext: ClContext) workGroupSize =
14+
let private preparePositions<'a, 'b> opAdd (clContext: ClContext) workGroupSize =
1515

1616
let preparePositions (op: Expr<'a option -> 'b option>) =
1717
<@ fun (ndRange: Range1D) rowCount columnCount valuesLength (values: ClArray<'a>) (rows: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'b>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ open GraphBLAS.FSharp.Backend.Matrix
1111

1212
module internal Map2 =
1313

14-
let preparePositions<'a, 'b, 'c> opAdd (clContext: ClContext) workGroupSize =
14+
let private preparePositions<'a, 'b, 'c> opAdd (clContext: ClContext) workGroupSize =
1515

1616
let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) =
1717
<@ fun (ndRange: Range1D) rowCount columnCount leftValuesLength rightValuesLength (leftValues: ClArray<'a>) (leftRows: ClArray<int>) (leftColumns: ClArray<int>) (rightValues: ClArray<'b>) (rightRows: ClArray<int>) (rightColumn: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'c>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->
@@ -135,7 +135,7 @@ module internal Map2 =
135135
Values = resultValues }
136136

137137
module AtLeastOne =
138-
let preparePositionsAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
138+
let private preparePositionsAtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
139139
(clContext: ClContext)
140140
(opAdd: Expr<'a option -> 'b option -> 'c option>)
141141
workGroupSize

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ open GraphBLAS.FSharp.Objects.ClContextExtensions
1212
open GraphBLAS.FSharp.Objects.ArraysExtensions
1313

1414
module internal Map =
15-
let preparePositions<'a, 'b> op (clContext: ClContext) workGroupSize =
15+
let private preparePositions<'a, 'b> op (clContext: ClContext) workGroupSize =
1616

1717
let preparePositions (op: Expr<'a option -> 'b option>) =
1818
<@ fun (ndRange: Range1D) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'b>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->
@@ -115,7 +115,7 @@ module internal Map =
115115
Values = resultValues }
116116

117117
module WithValue =
118-
let preparePositions<'a, 'b, 'c when 'b: struct> (clContext: ClContext) workGroupSize op =
118+
let private preparePositions<'a, 'b, 'c when 'b: struct> (clContext: ClContext) workGroupSize op =
119119

120120
let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) =
121121
<@ fun (ndRange: Range1D) (operand: ClCell<'a option>) rowCount columnCount (values: ClArray<'b>) (rowPointers: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'c>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ open GraphBLAS.FSharp.Objects.ClMatrix
1010
open GraphBLAS.FSharp.Objects.ClContextExtensions
1111

1212
module internal Map2 =
13-
let preparePositions<'a, 'b, 'c> opAdd (clContext: ClContext) workGroupSize =
13+
let private preparePositions<'a, 'b, 'c> opAdd (clContext: ClContext) workGroupSize =
1414

1515
let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) =
1616
<@ fun (ndRange: Range1D) rowCount columnCount (leftValues: ClArray<'a>) (leftRowPointers: ClArray<int>) (leftColumns: ClArray<int>) (rightValues: ClArray<'b>) (rightRowPointers: ClArray<int>) (rightColumn: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'c>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->
@@ -135,7 +135,7 @@ module internal Map2 =
135135
Values = resultValues }
136136

137137
module AtLeastOne =
138-
let preparePositions<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
138+
let private preparePositions<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
139139
(opAdd: Expr<'a option -> 'b option -> 'c option>)
140140
(clContext: ClContext)
141141
workGroupSize

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -266,36 +266,6 @@ module Matrix =
266266
|> ClMatrix.LIL
267267
| ClMatrix.LIL _ -> copy processor allocationMode matrix
268268

269-
/// <summary>
270-
/// Builds a new matrix whose elements are the results of applying the given function
271-
/// to each of the elements of the matrix.
272-
/// </summary>
273-
/// <param name="op">
274-
/// A function to transform values of the input matrix.
275-
/// Operand and result types should be optional to distinguish explicit and implicit zeroes
276-
/// </param>
277-
/// <param name="clContext">OpenCL context.</param>
278-
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
279-
let map (op: Expr<'a option -> 'b option>) (clContext: ClContext) workGroupSize =
280-
let mapCOO =
281-
COO.Matrix.map op clContext workGroupSize
282-
283-
let mapCSR =
284-
CSR.Matrix.map op clContext workGroupSize
285-
286-
let transposeCOO =
287-
COO.Matrix.transposeInPlace clContext workGroupSize
288-
289-
fun (processor: MailboxProcessor<_>) allocationMode matrix ->
290-
match matrix with
291-
| ClMatrix.COO m -> mapCOO processor allocationMode m |> ClMatrix.COO
292-
| ClMatrix.CSR m -> mapCSR processor allocationMode m |> ClMatrix.COO
293-
| ClMatrix.CSC m ->
294-
(mapCSR processor allocationMode m.ToCSR)
295-
|> transposeCOO processor
296-
|> ClMatrix.COO
297-
| _ -> failwith "Not yet implemented"
298-
299269
/// <summary>
300270
/// Transposes the given matrix and returns result.
301271
/// The given matrix should neither be used afterwards nor be disposed.

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@ open GraphBLAS.FSharp.Backend.Operations
1111
[<RequireQualifiedAccess>]
1212
module Operations =
1313
module Vector =
14+
/// <summary>
15+
/// Builds a new vector whose elements are the results of applying the given function
16+
/// to each of the elements of the vector.
17+
/// </summary>
18+
/// <param name="op">
19+
/// A function to transform values of the input vector.
20+
/// Operand and result types should be optional to distinguish explicit and implicit zeroes.
21+
/// </param>
22+
/// <param name="clContext">OpenCL context.</param>
23+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
24+
let map (op: Expr<'a option -> 'b option>) (clContext: ClContext) workGroupSize =
25+
let mapSparse =
26+
Sparse.Vector.map op clContext workGroupSize
27+
28+
let mapDense =
29+
Dense.Vector.map op clContext workGroupSize
30+
31+
fun (processor: MailboxProcessor<_>) allocationMode matrix ->
32+
match matrix with
33+
| ClVector.Sparse v ->
34+
mapSparse processor allocationMode v
35+
|> ClVector.Sparse
36+
| ClVector.Dense v ->
37+
mapDense processor allocationMode v
38+
|> ClVector.Dense
39+
1440
/// <summary>
1541
/// Builds a new vector whose values are the results of applying the given function
1642
/// to the corresponding pairs of values from the two vectors.
@@ -72,6 +98,36 @@ module Operations =
7298
| _ -> failwith "Vector formats are not matching."
7399

74100
module Matrix =
101+
/// <summary>
102+
/// Builds a new matrix whose elements are the results of applying the given function
103+
/// to each of the elements of the matrix.
104+
/// </summary>
105+
/// <param name="op">
106+
/// A function to transform values of the input matrix.
107+
/// Operand and result types should be optional to distinguish explicit and implicit zeroes
108+
/// </param>
109+
/// <param name="clContext">OpenCL context.</param>
110+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
111+
let map (op: Expr<'a option -> 'b option>) (clContext: ClContext) workGroupSize =
112+
let mapCOO =
113+
COO.Matrix.map op clContext workGroupSize
114+
115+
let mapCSR =
116+
CSR.Matrix.map op clContext workGroupSize
117+
118+
let transposeCOO =
119+
COO.Matrix.transposeInPlace clContext workGroupSize
120+
121+
fun (processor: MailboxProcessor<_>) allocationMode matrix ->
122+
match matrix with
123+
| ClMatrix.COO m -> mapCOO processor allocationMode m |> ClMatrix.COO
124+
| ClMatrix.CSR m -> mapCSR processor allocationMode m |> ClMatrix.COO
125+
| ClMatrix.CSC m ->
126+
(mapCSR processor allocationMode m.ToCSR)
127+
|> transposeCOO processor
128+
|> ClMatrix.COO
129+
| _ -> failwith "Not yet implemented"
130+
75131
/// <summary>
76132
/// Builds a new matrix whose values are the results of applying the given function
77133
/// to the corresponding pairs of values from the two matrices.

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -148,32 +148,6 @@ module Vector =
148148
ClVector.Dense
149149
<| toDense processor allocationMode vector
150150

151-
/// <summary>
152-
/// Builds a new vector whose elements are the results of applying the given function
153-
/// to each of the elements of the vector.
154-
/// </summary>
155-
/// <param name="op">
156-
/// A function to transform values of the input vector.
157-
/// Operand and result types should be optional to distinguish explicit and implicit zeroes.
158-
/// </param>
159-
/// <param name="clContext">OpenCL context.</param>
160-
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
161-
let map (op: Expr<'a option -> 'b option>) (clContext: ClContext) workGroupSize =
162-
let mapSparse =
163-
Sparse.Vector.map op clContext workGroupSize
164-
165-
let mapDense =
166-
Dense.Vector.map op clContext workGroupSize
167-
168-
fun (processor: MailboxProcessor<_>) allocationMode matrix ->
169-
match matrix with
170-
| ClVector.Sparse v ->
171-
mapSparse processor allocationMode v
172-
|> ClVector.Sparse
173-
| ClVector.Dense v ->
174-
mapDense processor allocationMode v
175-
|> ClVector.Dense
176-
177151
let private assignByMaskGeneral<'a, 'b when 'a: struct and 'b: struct> op (clContext: ClContext) workGroupSize =
178152

179153
let sparseFillVector =

tests/GraphBLAS-sharp.Tests/Backend/Matrix/Map.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ let createTestMap case (zero: 'a) (constant: 'a) binOp isEqual opQ =
9797
let unaryOp = binOp constant
9898
let unaryOpQ = opQ zero constant
9999

100-
let map = Matrix.map unaryOpQ context wgSize
100+
let map =
101+
Operations.Matrix.map unaryOpQ context wgSize
101102

102103
let toCOO = Matrix.toCOO context wgSize
103104

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
module GraphBLAS.FSharp.Tests.Backend.Vector.Map
2+
3+
open Expecto
4+
open Expecto.Logging
5+
open Expecto.Logging.Message
6+
open Microsoft.FSharp.Collections
7+
open GraphBLAS.FSharp
8+
open GraphBLAS.FSharp.Backend
9+
open GraphBLAS.FSharp.Backend.Quotes
10+
open GraphBLAS.FSharp.Tests
11+
open GraphBLAS.FSharp.Tests.Backend
12+
open GraphBLAS.FSharp.Tests.TestCases
13+
open GraphBLAS.FSharp.Objects
14+
open GraphBLAS.FSharp.Objects.ClContextExtensions
15+
open GraphBLAS.FSharp.Objects.ClVectorExtensions
16+
open Mono.CompilerServices.SymbolWriter
17+
18+
let logger = Log.create "Vector.Map.Tests"
19+
20+
let config = Utils.defaultConfig
21+
let wgSize = Utils.defaultWorkGroupSize
22+
23+
let getCorrectnessTestName case datatype =
24+
$"Correctness on %s{datatype}, %A{case}"
25+
26+
let checkResult isEqual op zero (baseVector: 'a []) (actual: Vector<'b>) =
27+
28+
let expectedArrayLength = baseVector.Length
29+
30+
let expectedArray =
31+
Array.create expectedArrayLength zero
32+
33+
for i in 0 .. expectedArrayLength - 1 do
34+
expectedArray.[i] <- op baseVector.[i]
35+
36+
let expected =
37+
Utils.createVectorFromArray Dense expectedArray (isEqual zero)
38+
|> Utils.vectorToDenseVector
39+
40+
match actual with
41+
| Vector.Dense actual ->
42+
"arrays must have the same values"
43+
|> Expect.equal actual expected
44+
| _ -> failwith "Vector format must be Sparse."
45+
46+
let correctnessGenericTest
47+
zero
48+
op
49+
(addFun: MailboxProcessor<_> -> AllocationFlag -> ClVector<'a> -> ClVector<'a>)
50+
(toDense: MailboxProcessor<_> -> AllocationFlag -> ClVector<'a> -> ClVector<'a>)
51+
(isEqual: 'a -> 'a -> bool)
52+
(case: OperationCase<VectorFormat>)
53+
(array: 'a [])
54+
=
55+
56+
let isZero = (isEqual zero)
57+
58+
let vectorHost =
59+
Utils.createVectorFromArray case.Format array isZero
60+
61+
if vectorHost.NNZ > 0 then
62+
63+
let context = case.TestContext.ClContext
64+
let q = case.TestContext.Queue
65+
66+
let vector = vectorHost.ToDevice context
67+
68+
try
69+
let res =
70+
addFun q HostInterop vector
71+
72+
vector.Dispose q
73+
74+
let denseActual = toDense q HostInterop res
75+
76+
let actual = denseActual.ToHost q
77+
78+
res.Dispose q
79+
denseActual.Dispose q
80+
81+
checkResult isEqual op zero array actual
82+
with
83+
| ex when ex.Message = "InvalidBufferSize" -> ()
84+
| ex -> raise ex
85+
86+
let createTestMap case (zero: 'a) (constant: 'a) binOp isEqual opQ =
87+
let getCorrectnessTestName = getCorrectnessTestName case
88+
89+
let context = case.TestContext.ClContext
90+
let q = case.TestContext.Queue
91+
92+
let unaryOp = binOp constant
93+
let unaryOpQ = opQ zero constant
94+
95+
let map =
96+
Operations.Vector.map unaryOpQ context wgSize
97+
98+
let toDense = Vector.toDense context wgSize
99+
100+
case
101+
|> correctnessGenericTest zero unaryOp map toDense isEqual
102+
|> testPropertyWithConfig config (getCorrectnessTestName $"{typeof<'a>}")
103+
104+
let testFixturesMapNot case =
105+
[ let q = case.TestContext.Queue
106+
q.Error.Add(fun e -> failwithf "%A" e)
107+
108+
createTestMap case false true (fun _ -> not) (=) (fun _ _ -> ArithmeticOperations.notOption) ]
109+
110+
let notTests =
111+
operationGPUTests "not" testFixturesMapNot
112+
113+
let testFixturesMapAdd case =
114+
[ let context = case.TestContext.ClContext
115+
let q = case.TestContext.Queue
116+
q.Error.Add(fun e -> failwithf "%A" e)
117+
118+
createTestMap case 0 10 (+) (=) ArithmeticOperations.addLeftConst
119+
120+
if Utils.isFloat64Available context.ClDevice then
121+
createTestMap case 0.0 10.0 (+) Utils.floatIsEqual ArithmeticOperations.addLeftConst
122+
123+
createTestMap case 0.0f 10.0f (+) Utils.float32IsEqual ArithmeticOperations.addLeftConst
124+
125+
createTestMap case 0uy 10uy (+) (=) ArithmeticOperations.addLeftConst ]
126+
127+
let addTests =
128+
operationGPUTests "add" testFixturesMapAdd
129+
130+
let testFixturesMapMul case =
131+
[ let context = case.TestContext.ClContext
132+
let q = case.TestContext.Queue
133+
q.Error.Add(fun e -> failwithf "%A" e)
134+
135+
createTestMap case 0 10 (*) (=) ArithmeticOperations.mulLeftConst
136+
137+
if Utils.isFloat64Available context.ClDevice then
138+
createTestMap case 0.0 10.0 (*) Utils.floatIsEqual ArithmeticOperations.mulLeftConst
139+
140+
createTestMap case 0.0f 10.0f (*) Utils.float32IsEqual ArithmeticOperations.mulLeftConst
141+
142+
createTestMap case 0uy 10uy (*) (=) ArithmeticOperations.mulLeftConst ]
143+
144+
let mulTests =
145+
operationGPUTests "mul" testFixturesMapMul
146+
147+
let allTests =
148+
testList "Map" [ addTests; mulTests; notTests ]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="Backend/Vector/SpMV.fs" />
6363
<Compile Include="Backend/Vector/ZeroCreate.fs" />
6464
<Compile Include="Backend/Vector/Merge.fs" />
65+
<Compile Include="Backend/Vector/Map.fs" />
6566
<Compile Include="Host/Matrix/FromArray2D.fs" />
6667
<Compile Include="Host/Matrix/Convert.fs" />
6768
<Compile Include="Host/IO/MtxReader.fs" />

0 commit comments

Comments
 (0)