From e5345a7f2927644e8d400467b665b47d6348a2a5 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 5 Sep 2022 16:57:24 +0300 Subject: [PATCH 1/4] Added generator for bitonic sort --- .../BackendCommonTests/BitonicSortTests.fs | 38 +++++--- tests/GraphBLAS-sharp.Tests/Helpers.fs | 88 +++++++++++++++++-- tests/GraphBLAS-sharp.Tests/Program.fs | 14 +-- 3 files changed, 111 insertions(+), 29 deletions(-) diff --git a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs index 88cd21c7..2193fa5b 100644 --- a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs +++ b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs @@ -27,14 +27,26 @@ let testContext = |> Seq.tryHead let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (filter: 'a -> bool) (array: ('n * 'n * 'a) []) = - if array.Length > 0 then - let projection (row: 'n) (col: 'n) (v: 'a) = row, col + let projection (row: 'n) (col: 'n) (v: 'a) = row, col - let rows, cols, vals = - array - |> Array.distinctBy ((<|||) projection) - |> Array.filter (fun (_, _, v) -> filter v) - |> Array.unzip3 + let rows, cols, vals = + array + |> Array.distinctBy ((<|||) projection) + |> Array.filter (fun (_, _, v) -> filter v) + |> Array.unzip3 + + if rows.Length > 0 then + logger.debug ( + eventX "Initial size is {size}" + >> setField "size" (sprintf "%A" rows.Length) + ) + + // logger.debug ( + // eventX "Initial are {rows}, {cols}, {vals}" + // >> setField "rows" (sprintf "%A" rows) + // >> setField "cols" (sprintf "%A" cols) + // >> setField "vals" (sprintf "%A" vals) + // ) use clRows = context.CreateClArray rows use clCols = context.CreateClArray cols @@ -55,12 +67,12 @@ let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (filter: 'a -> b rows, cols, vals - logger.debug ( - eventX "Actual are {actualRows}, {actualCols}, {actualVals}" - >> setField "actualRows" (sprintf "%A" actualRows) - >> setField "actualCols" (sprintf "%A" actualCols) - >> setField "actualVals" (sprintf "%A" actualVals) - ) + // logger.debug ( + // eventX "Actual are {actualRows}, {actualCols}, {actualVals}" + // >> setField "actualRows" (sprintf "%A" actualRows) + // >> setField "actualCols" (sprintf "%A" actualCols) + // >> setField "actualVals" (sprintf "%A" actualVals) + // ) let expectedRows, expectedCols, expectedVals = (rows, cols, vals) diff --git a/tests/GraphBLAS-sharp.Tests/Helpers.fs b/tests/GraphBLAS-sharp.Tests/Helpers.fs index bb841d02..d85c4d0f 100644 --- a/tests/GraphBLAS-sharp.Tests/Helpers.fs +++ b/tests/GraphBLAS-sharp.Tests/Helpers.fs @@ -331,14 +331,84 @@ module Generators = |> genericSparseGenerator false Arb.generate |> Arb.fromGen -// type ArrayOfDistinctKeys() = -// // Stack overflow. -// static member ArrayOfDistinctKeysArb() = -// gen { -// let! array = Arb.generate<(uint64 * int)[]> -// return Array.distinctBy (fun (key, _) -> key) array -// } -// |> Arb.fromGen + type ArrayOfDistinctKeys() = + static let arrayOfDistinctKeysGenerator (keysGenerator: Gen<'n>) (valuesGenerator: Gen<'a>) = + let tuplesGenerator = + Gen.map3 + <| fun a b c -> a, b, c + <| keysGenerator + <| keysGenerator + <| valuesGenerator + + gen { + let! length = + Gen.sized + <| fun size -> Gen.choose (1, size) + + let! array = + Gen.arrayOfLength + <| length + <| tuplesGenerator + + return Array.distinctBy (fun (r, c, _) -> r, c) array + } + + static member IntType() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + + static member FloatType() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| (Arb.Default.NormalFloat() + |> Arb.toGen + |> Gen.map float) + |> Arb.fromGen + + static member SByteType() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + + static member ByteType() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + + static member Int16Type() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + + static member UInt16Type() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + + static member Int32Type() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + + static member UInt32Type() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + + static member BoolType() = + arrayOfDistinctKeysGenerator + <| Arb.generate + <| Arb.generate + |> Arb.fromGen + module Utils = type TestContext = @@ -356,7 +426,7 @@ module Utils = typeof typeof typeof - // typeof + typeof ] } let rec cartesian listOfLists = diff --git a/tests/GraphBLAS-sharp.Tests/Program.fs b/tests/GraphBLAS-sharp.Tests/Program.fs index aaa5d092..1c2e40ef 100644 --- a/tests/GraphBLAS-sharp.Tests/Program.fs +++ b/tests/GraphBLAS-sharp.Tests/Program.fs @@ -11,13 +11,13 @@ open GraphBLAS.FSharp.IO let allTests = testList "All tests" - [ Backend.BitonicSort.tests - Backend.PrefixSum.tests - Backend.Convert.tests - Backend.RemoveDuplicates.tests - Backend.Copy.tests - Backend.Replicate.tests - Backend.EwiseAdd.tests + [ //Backend.BitonicSort.tests + // Backend.PrefixSum.tests + // Backend.Convert.tests + // Backend.RemoveDuplicates.tests + // Backend.Copy.tests + // Backend.Replicate.tests + // Backend.EwiseAdd.tests Backend.Transpose.tests //Matrix.GetTuples.tests //Matrix.Mxv.tests From 409cd9bd6dbeb2d08e6d80b0c42c8b1cdc98e68a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Sep 2022 20:10:26 +0300 Subject: [PATCH 2/4] Fix tests except ewiseadd --- src/GraphBLAS-sharp/Objects/Matrix.fs | 2 +- .../BackendCommonTests/BitonicSortTests.fs | 80 +++++------------- .../BackendCommonTests/ConvertTests.fs | 28 +++---- .../BackendCommonTests/PrefixSumTests.fs | 8 +- .../BackendCommonTests/TransposeTests.fs | 82 +++++++++---------- tests/GraphBLAS-sharp.Tests/Helpers.fs | 34 +++++--- tests/GraphBLAS-sharp.Tests/Program.fs | 14 ++-- 7 files changed, 107 insertions(+), 141 deletions(-) diff --git a/src/GraphBLAS-sharp/Objects/Matrix.fs b/src/GraphBLAS-sharp/Objects/Matrix.fs index f581e5f2..19082340 100644 --- a/src/GraphBLAS-sharp/Objects/Matrix.fs +++ b/src/GraphBLAS-sharp/Objects/Matrix.fs @@ -3,7 +3,7 @@ namespace GraphBLAS.FSharp open Brahma.FSharp open GraphBLAS.FSharp.Backend -type MatrixFromat = +type MatrixFormat = | CSR | COO diff --git a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs index 2193fa5b..c0211c37 100644 --- a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs +++ b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/BitonicSortTests.fs @@ -6,47 +6,19 @@ open Expecto.Logging.Message open GraphBLAS.FSharp.Backend.Common open Brahma.FSharp open GraphBLAS.FSharp.Tests.Utils -open OpenCL.Net let logger = Log.create "BitonicSort.Tests" -let testContext = - "" - |> avaliableContexts - |> Seq.filter - (fun context -> - let mutable e = ErrorCode.Unknown - let device = context.ClContext.ClDevice.Device +let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (array: ('n * 'n * 'a) []) = + if array.Length > 0 then + let projection (row: 'n) (col: 'n) (v: 'a) = row, col - let deviceType = - Cl - .GetDeviceInfo(device, DeviceInfo.Type, &e) - .CastTo() - - deviceType = DeviceType.Gpu) - |> Seq.tryHead - -let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (filter: 'a -> bool) (array: ('n * 'n * 'a) []) = - let projection (row: 'n) (col: 'n) (v: 'a) = row, col - - let rows, cols, vals = - array - |> Array.distinctBy ((<|||) projection) - |> Array.filter (fun (_, _, v) -> filter v) - |> Array.unzip3 - - if rows.Length > 0 then logger.debug ( eventX "Initial size is {size}" - >> setField "size" (sprintf "%A" rows.Length) + >> setField "size" (sprintf "%A" array.Length) ) - // logger.debug ( - // eventX "Initial are {rows}, {cols}, {vals}" - // >> setField "rows" (sprintf "%A" rows) - // >> setField "cols" (sprintf "%A" cols) - // >> setField "vals" (sprintf "%A" vals) - // ) + let rows, cols, vals = Array.unzip3 array use clRows = context.CreateClArray rows use clCols = context.CreateClArray cols @@ -67,13 +39,6 @@ let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (filter: 'a -> b rows, cols, vals - // logger.debug ( - // eventX "Actual are {actualRows}, {actualCols}, {actualVals}" - // >> setField "actualRows" (sprintf "%A" actualRows) - // >> setField "actualCols" (sprintf "%A" actualCols) - // >> setField "actualVals" (sprintf "%A" actualVals) - // ) - let expectedRows, expectedCols, expectedVals = (rows, cols, vals) |||> Array.zip3 @@ -81,42 +46,39 @@ let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (filter: 'a -> b |> Array.unzip3 (sprintf "Row arrays should be equal. Actual is \n%A, expected \n%A, input is \n%A" actualRows expectedRows rows) - |> Expect.sequenceEqual actualRows expectedRows + |> compareArrays (=) actualRows expectedRows (sprintf "Column arrays should be equal. Actual is \n%A, expected \n%A, input is \n%A" actualCols expectedCols cols) - |> Expect.sequenceEqual actualCols expectedCols + |> compareArrays (=) actualCols expectedCols (sprintf "Value arrays should be equal. Actual is \n%A, expected \n%A, input is \n%A" actualVals expectedVals vals) - |> Expect.sequenceEqual actualVals expectedVals + |> compareArrays (=) actualVals expectedVals -let testFixtures<'a when 'a: equality> config wgSize context q filter = +let testFixtures<'a when 'a: equality> config wgSize context q = let sort: MailboxProcessor<_> -> ClArray -> ClArray -> ClArray<'a> -> unit = BitonicSort.sortKeyValuesInplace context wgSize - makeTest context q sort filter + makeTest context q sort |> testPropertyWithConfig config (sprintf "Correctness on %A" typeof<'a>) let tests = - match testContext with - | Some c -> - let context = c.ClContext - let config = defaultConfig - - let wgSize = 128 - let q = c.Queue - q.Error.Add(fun e -> failwithf "%A" e) - - [ testFixtures config wgSize context q (fun _ -> true) - testFixtures config wgSize context q (System.Double.IsNaN >> not) - testFixtures config wgSize context q (fun _ -> true) - testFixtures config wgSize context q (fun _ -> true) ] - | _ -> [] + let context = defaultContext.ClContext + let config = { defaultConfig with endSize = 1000000 } + + let wgSize = 32 + let q = defaultContext.Queue + q.Error.Add(fun e -> failwithf "%A" e) + + [ testFixtures config wgSize context q + testFixtures config wgSize context q + testFixtures config wgSize context q + testFixtures config wgSize context q ] |> testList "Backend.Common.BitonicSort tests" diff --git a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/ConvertTests.fs b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/ConvertTests.fs index 683620ba..efb31703 100644 --- a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/ConvertTests.fs +++ b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/ConvertTests.fs @@ -12,11 +12,10 @@ open OpenCL.Net let logger = Log.create "Convert.Tests" -let context = defaultContext.ClContext let config = defaultConfig -let wgSize = 128 +let wgSize = 32 -let makeTestCSR q toCOO isZero (array: 'a [,]) = +let makeTestCSR context q toCOO isZero (array: 'a [,]) = let mtx = createMatrixFromArray2D CSR array isZero if mtx.NNZCount > 0 then @@ -38,7 +37,7 @@ let makeTestCSR q toCOO isZero (array: 'a [,]) = "Matrices should be equal" |> Expect.equal actual expected -let makeTestCOO q toCSR isZero (array: 'a [,]) = +let makeTestCOO context q toCSR isZero (array: 'a [,]) = let mtx = createMatrixFromArray2D COO array isZero if mtx.NNZCount > 0 then @@ -68,49 +67,50 @@ let testFixtures case = System.Double.IsNaN x || abs x < Accuracy.medium.absolute - let q = defaultContext.Queue + let context = case.ClContext.ClContext + let q = case.ClContext.Queue q.Error.Add(fun e -> failwithf "%A" e) match case.MatrixCase with | COO -> [ let toCSR = Matrix.toCSR context wgSize - makeTestCOO q toCSR ((=) 0) + makeTestCOO context q toCSR ((=) 0) |> testPropertyWithConfig config (getCorrectnessTestName "int") let toCSR = Matrix.toCSR context wgSize - makeTestCOO q toCSR filterFloat + makeTestCOO context q toCSR filterFloat |> testPropertyWithConfig config (getCorrectnessTestName "float") let toCSR = Matrix.toCSR context wgSize - makeTestCOO q toCSR ((=) 0uy) + makeTestCOO context q toCSR ((=) 0uy) |> testPropertyWithConfig config (getCorrectnessTestName "byte") let toCSR = Matrix.toCSR context wgSize - makeTestCOO q toCSR ((=) false) + makeTestCOO context q toCSR ((=) false) |> testPropertyWithConfig config (getCorrectnessTestName "bool") ] | CSR -> [ let toCOO = Matrix.toCOO context wgSize - makeTestCSR q toCOO ((=) 0) + makeTestCSR context q toCOO ((=) 0) |> testPropertyWithConfig config (getCorrectnessTestName "int") let toCOO = Matrix.toCOO context wgSize - makeTestCSR q toCOO filterFloat + makeTestCSR context q toCOO filterFloat |> testPropertyWithConfig config (getCorrectnessTestName "float") let toCOO = Matrix.toCOO context wgSize - makeTestCSR q toCOO ((=) 0uy) + makeTestCSR context q toCOO ((=) 0uy) |> testPropertyWithConfig config (getCorrectnessTestName "byte") let toCOO = Matrix.toCOO context wgSize - makeTestCSR q toCOO ((=) false) + makeTestCSR context q toCOO ((=) false) |> testPropertyWithConfig config (getCorrectnessTestName "bool") ] let tests = @@ -126,6 +126,6 @@ let tests = .CastTo() deviceType = DeviceType.Gpu) - |> List.distinctBy (fun case -> case.MatrixCase) + |> List.distinctBy (fun case -> case.ClContext.ClContext.ClDevice.DeviceType, case.MatrixCase) |> List.collect testFixtures |> testList "Convert tests" diff --git a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/PrefixSumTests.fs b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/PrefixSumTests.fs index 00feaacf..ae61d124 100644 --- a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/PrefixSumTests.fs +++ b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/PrefixSumTests.fs @@ -10,11 +10,7 @@ open GraphBLAS.FSharp.Tests.Utils let logger = Log.create "PrefixSum.Tests" -let testContext = - let contexts = "" |> avaliableContexts |> Seq.toList - contexts.[0] - -let context = testContext.ClContext +let context = defaultContext.ClContext let makeTest (q: MailboxProcessor<_>) scan plus zero isEqual (filter: 'a [] -> 'a []) (array: 'a []) = if array.Length > 0 then @@ -75,7 +71,7 @@ let tests = let config = defaultConfig let wgSize = 128 - let q = testContext.Queue + let q = defaultContext.Queue q.Error.Add(fun e -> failwithf "%A" e) let filterFloats = diff --git a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/TransposeTests.fs b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/TransposeTests.fs index 444769fc..8edc2299 100644 --- a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/TransposeTests.fs +++ b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/TransposeTests.fs @@ -10,50 +10,49 @@ open OpenCL.Net let logger = Log.create "Transpose.Tests" -let context = defaultContext.ClContext let config = defaultConfig -let wgSize = 128 +let wgSize = 32 let checkResult areEqual zero actual (expected2D: 'a [,]) = - let actual2D = - match actual with - | MatrixCOO actual -> - let actual2D = - Array2D.create actual.RowCount actual.ColumnCount zero + match actual with + | MatrixCOO actual -> + let (MatrixCOO expected) = + createMatrixFromArray2D COO expected2D (areEqual zero) - for i in 0 .. actual.Values.Length - 1 do - "Resulting zeroes should be filtered." - |> Expect.isFalse (areEqual zero actual.Values.[i]) + "The number of rows should be the same" + |> Expect.equal actual.RowCount expected.RowCount - actual2D.[actual.Rows.[i], actual.Columns.[i]] <- actual.Values.[i] + "The number of columns should be the same" + |> Expect.equal actual.ColumnCount expected.ColumnCount - actual2D - | MatrixCSR actual -> - let actual2D = - Array2D.create actual.RowCount actual.ColumnCount zero + "Row arrays should be equal" + |> compareArrays (=) actual.Rows expected.Rows - for i in 0 .. actual.RowCount - 1 do - for j in actual.RowPointers.[i] .. actual.RowPointers.[i + 1] - 1 do - "Resulting zeroes should be filtered." - |> Expect.isFalse (areEqual zero actual.Values.[j]) + "Column arrays should be equal" + |> compareArrays (=) actual.Columns expected.Columns - actual2D.[i, actual.ColumnIndices.[j]] <- actual.Values.[j] + "Value arrays should be equal" + |> compareArrays areEqual actual.Values expected.Values + | MatrixCSR actual -> + let (MatrixCSR expected) = + createMatrixFromArray2D CSR expected2D (areEqual zero) - actual2D + "The number of rows should be the same" + |> Expect.equal actual.RowCount expected.RowCount - "The number of rows should be the same" - |> Expect.equal (Array2D.length1 actual2D) (Array2D.length1 expected2D) + "The number of columns should be the same" + |> Expect.equal actual.ColumnCount expected.ColumnCount - "The number of columns should be the same" - |> Expect.equal (Array2D.length2 actual2D) (Array2D.length2 expected2D) + "Row pointer arrays should be equal" + |> compareArrays (=) actual.RowPointers expected.RowPointers + "Column arrays should be equal" + |> compareArrays (=) actual.ColumnIndices expected.ColumnIndices - for i in 0 .. Array2D.length1 actual2D - 1 do - for j in 0 .. Array2D.length2 actual2D - 1 do - "Matrices should be equal" - |> Expect.isTrue (areEqual actual2D.[i, j] expected2D.[i, j]) + "Value arrays should be equal" + |> compareArrays areEqual actual.Values expected.Values -let makeTestRegular q transposeFun areEqual zero case (array: 'a [,]) = +let makeTestRegular context q transposeFun areEqual zero case (array: 'a [,]) = let mtx = createMatrixFromArray2D case.MatrixCase array (areEqual zero) @@ -80,7 +79,7 @@ let makeTestRegular q transposeFun areEqual zero case (array: 'a [,]) = checkResult areEqual zero actual expected2D -let makeTestTwiceTranspose q transposeFun areEqual zero case (array: 'a [,]) = +let makeTestTwiceTranspose context q transposeFun areEqual zero case (array: 'a [,]) = let mtx = createMatrixFromArray2D case.MatrixCase array (areEqual zero) @@ -110,48 +109,49 @@ let testFixtures case = System.Double.IsNaN x && System.Double.IsNaN y || x = y - let q = defaultContext.Queue + let context = case.ClContext.ClContext + let q = case.ClContext.Queue q.Error.Add(fun e -> failwithf "%A" e) [ let transposeFun = Matrix.transpose context wgSize case - |> makeTestRegular q transposeFun (=) 0 + |> makeTestRegular context q transposeFun (=) 0 |> testPropertyWithConfig config (getCorrectnessTestName "int") case - |> makeTestTwiceTranspose q transposeFun (=) 0 + |> makeTestTwiceTranspose context q transposeFun (=) 0 |> testPropertyWithConfig config (getCorrectnessTestName "int (twice transpose)") let transposeFun = Matrix.transpose context wgSize case - |> makeTestRegular q transposeFun areEqualFloat 0.0 + |> makeTestRegular context q transposeFun areEqualFloat 0.0 |> testPropertyWithConfig config (getCorrectnessTestName "float") case - |> makeTestTwiceTranspose q transposeFun areEqualFloat 0.0 + |> makeTestTwiceTranspose context q transposeFun areEqualFloat 0.0 |> testPropertyWithConfig config (getCorrectnessTestName "float (twice transpose)") let transposeFun = Matrix.transpose context wgSize case - |> makeTestRegular q transposeFun (=) 0uy + |> makeTestRegular context q transposeFun (=) 0uy |> testPropertyWithConfig config (getCorrectnessTestName "byte") case - |> makeTestTwiceTranspose q transposeFun (=) 0uy + |> makeTestTwiceTranspose context q transposeFun (=) 0uy |> testPropertyWithConfig config (getCorrectnessTestName "byte (twice transpose)") let transposeFun = Matrix.transpose context wgSize case - |> makeTestRegular q transposeFun (=) false + |> makeTestRegular context q transposeFun (=) false |> testPropertyWithConfig config (getCorrectnessTestName "bool") case - |> makeTestTwiceTranspose q transposeFun (=) false + |> makeTestTwiceTranspose context q transposeFun (=) false |> testPropertyWithConfig config (getCorrectnessTestName "bool (twice transpose)") ] let tests = @@ -167,6 +167,6 @@ let tests = .CastTo() deviceType = DeviceType.Gpu) - |> List.distinctBy (fun case -> case.MatrixCase) + |> List.distinctBy (fun case -> case.ClContext.ClContext.ClDevice.DeviceType, case.MatrixCase) |> List.collect testFixtures |> testList "Transpose tests" diff --git a/tests/GraphBLAS-sharp.Tests/Helpers.fs b/tests/GraphBLAS-sharp.Tests/Helpers.fs index d85c4d0f..ff4d2586 100644 --- a/tests/GraphBLAS-sharp.Tests/Helpers.fs +++ b/tests/GraphBLAS-sharp.Tests/Helpers.fs @@ -341,14 +341,9 @@ module Generators = <| valuesGenerator gen { - let! length = - Gen.sized - <| fun size -> Gen.choose (1, size) + let! length = Gen.sized <| fun size -> Gen.choose (1, size) - let! array = - Gen.arrayOfLength - <| length - <| tuplesGenerator + let! array = Gen.arrayOfLength <| length <| tuplesGenerator return Array.distinctBy (fun (r, c, _) -> r, c) array } @@ -363,8 +358,8 @@ module Generators = arrayOfDistinctKeysGenerator <| Arb.generate <| (Arb.Default.NormalFloat() - |> Arb.toGen - |> Gen.map float) + |> Arb.toGen + |> Gen.map float) |> Arb.fromGen static member SByteType() = @@ -426,8 +421,7 @@ module Utils = typeof typeof typeof - typeof - ] } + typeof ] } let rec cartesian listOfLists = match listOfLists with @@ -523,11 +517,11 @@ module Utils = type OperationCase = { ClContext: TestContext - MatrixCase: MatrixFromat } + MatrixCase: MatrixFormat } let testCases = [ avaliableContexts "" |> Seq.map box - listOfUnionCases |> Seq.map box ] + listOfUnionCases |> Seq.map box ] |> List.map List.ofSeq |> cartesian |> List.map @@ -543,3 +537,17 @@ module Utils = let createVectorFromArray vectorCase array isZero = match vectorCase with | VectorFormat.COO -> VectorCOO <| COOVector.FromArray(array, isZero) + + let compareArrays areEqual (actual: 'a []) (expected: 'a []) message = + sprintf "%s. Lengths should be equal. Actual is %A, expected %A" message actual expected + |> Expect.equal actual.Length expected.Length + + for i in 0 .. actual.Length - 1 do + if not (areEqual actual.[i] expected.[i]) then + sprintf "%s. Arrays differ at position %A of %A. Actual value is %A, expected %A" + <| message + <| i + <| (actual.Length - 1) + <| actual.[i] + <| expected.[i] + |> failtestf "%s" diff --git a/tests/GraphBLAS-sharp.Tests/Program.fs b/tests/GraphBLAS-sharp.Tests/Program.fs index 1c2e40ef..19edbb4b 100644 --- a/tests/GraphBLAS-sharp.Tests/Program.fs +++ b/tests/GraphBLAS-sharp.Tests/Program.fs @@ -11,13 +11,13 @@ open GraphBLAS.FSharp.IO let allTests = testList "All tests" - [ //Backend.BitonicSort.tests - // Backend.PrefixSum.tests - // Backend.Convert.tests - // Backend.RemoveDuplicates.tests - // Backend.Copy.tests - // Backend.Replicate.tests - // Backend.EwiseAdd.tests + [ Backend.BitonicSort.tests + Backend.PrefixSum.tests + Backend.Convert.tests + Backend.RemoveDuplicates.tests + Backend.Copy.tests + Backend.Replicate.tests + // Backend.EwiseAdd.tests Backend.Transpose.tests //Matrix.GetTuples.tests //Matrix.Mxv.tests From 654043f2bb53ceef16692843164e85a4ba0aee5b Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Sep 2022 23:00:55 +0300 Subject: [PATCH 3/4] Ewiseadd fix --- .../Matrix/CSRMatrix/CSRMatrix.fs | 52 ++++++++++++------- .../BackendCommonTests/MatrixEwiseAddTests.fs | 9 ++-- tests/GraphBLAS-sharp.Tests/Program.fs | 4 +- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs b/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs index a1b3124b..9bd51ade 100644 --- a/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs +++ b/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs @@ -81,8 +81,7 @@ module CSRMatrix = let eWiseAdd (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize = - let toCOOInplaceLeft = toCOOInplace clContext workGroupSize - let toCOOInplaceRight = toCOOInplace clContext workGroupSize + let prepareRows = prepareRows clContext workGroupSize let eWiseCOO = COOMatrix.eWiseAdd clContext opAdd workGroupSize @@ -91,24 +90,32 @@ module CSRMatrix = COOMatrix.toCSRInplace clContext workGroupSize fun (processor: MailboxProcessor<_>) (m1: CSRMatrix<'a>) (m2: CSRMatrix<'b>) -> - - let m1COO = toCOOInplaceLeft processor m1 - let m2COO = toCOOInplaceRight processor m2 + let m1COO = + { Context = clContext + RowCount = m1.RowCount + ColumnCount = m1.ColumnCount + Rows = prepareRows processor m1.RowPointers m1.Values.Length m1.RowCount + Columns = m1.Columns + Values = m1.Values } + + let m2COO = + { Context = clContext + RowCount = m2.RowCount + ColumnCount = m2.ColumnCount + Rows = prepareRows processor m2.RowPointers m2.Values.Length m2.RowCount + Columns = m2.Columns + Values = m2.Values } let m3COO = eWiseCOO processor m1COO m2COO processor.Post(Msg.CreateFreeMsg(m1COO.Rows)) processor.Post(Msg.CreateFreeMsg(m2COO.Rows)) - let m3 = toCSRInplace processor m3COO - processor.Post(Msg.CreateFreeMsg(m3COO.Rows)) - - m3 + toCSRInplace processor m3COO let eWiseAddAtLeastOne (clContext: ClContext) (opAdd: Expr -> 'c option>) workGroupSize = - let toCOOInplaceLeft = toCOOInplace clContext workGroupSize - let toCOOInplaceRight = toCOOInplace clContext workGroupSize + let prepareRows = prepareRows clContext workGroupSize let eWiseCOO = COOMatrix.eWiseAddAtLeastOne clContext opAdd workGroupSize @@ -117,19 +124,28 @@ module CSRMatrix = COOMatrix.toCSRInplace clContext workGroupSize fun (processor: MailboxProcessor<_>) (m1: CSRMatrix<'a>) (m2: CSRMatrix<'b>) -> - - let m1COO = toCOOInplaceLeft processor m1 - let m2COO = toCOOInplaceRight processor m2 + let m1COO = + { Context = clContext + RowCount = m1.RowCount + ColumnCount = m1.ColumnCount + Rows = prepareRows processor m1.RowPointers m1.Values.Length m1.RowCount + Columns = m1.Columns + Values = m1.Values } + + let m2COO = + { Context = clContext + RowCount = m2.RowCount + ColumnCount = m2.ColumnCount + Rows = prepareRows processor m2.RowPointers m2.Values.Length m2.RowCount + Columns = m2.Columns + Values = m2.Values } let m3COO = eWiseCOO processor m1COO m2COO processor.Post(Msg.CreateFreeMsg(m1COO.Rows)) processor.Post(Msg.CreateFreeMsg(m2COO.Rows)) - let m3 = toCSRInplace processor m3COO - processor.Post(Msg.CreateFreeMsg(m3COO.Rows)) - - m3 + toCSRInplace processor m3COO let transposeInplace (clContext: ClContext) workGroupSize = diff --git a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs index 748a5aa1..239f86f2 100644 --- a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs +++ b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs @@ -86,7 +86,7 @@ let correctnessGenericTest let testFixturesEWiseAdd case = [ let config = defaultConfig - let wgSize = 256 + let wgSize = 32 let getCorrectnessTestName datatype = sprintf "Correctness on %s, %A" datatype case @@ -140,12 +140,13 @@ let tests = .CastTo() deviceType = DeviceType.Gpu) + |> List.distinctBy (fun case -> case.ClContext.ClContext.ClDevice.DeviceType, case.MatrixCase) |> List.collect testFixturesEWiseAdd |> testList "Backend.Matrix.eWiseAdd tests" let testFixturesEWiseAddAtLeastOne case = [ let config = defaultConfig - let wgSize = 256 + let wgSize = 32 let getCorrectnessTestName datatype = sprintf "Correctness on %s, %A" datatype case @@ -203,13 +204,14 @@ let tests2 = .CastTo() deviceType = DeviceType.Gpu) + |> List.distinctBy (fun case -> case.ClContext.ClContext.ClDevice.DeviceType, case.MatrixCase) |> List.collect testFixturesEWiseAddAtLeastOne |> testList "Backend.Matrix.eWiseAddAtLeastOne tests" let testFixturesEWiseMulAtLeastOne case = [ let config = defaultConfig - let wgSize = 256 + let wgSize = 32 let getCorrectnessTestName datatype = sprintf "Correctness on %s, %A" datatype case @@ -267,5 +269,6 @@ let tests3 = .CastTo() deviceType = DeviceType.Gpu) + |> List.distinctBy (fun case -> case.ClContext.ClContext.ClDevice.DeviceType, case.MatrixCase) |> List.collect testFixturesEWiseMulAtLeastOne |> testList "Backend.Matrix.eWiseMulAtLeastOne tests" diff --git a/tests/GraphBLAS-sharp.Tests/Program.fs b/tests/GraphBLAS-sharp.Tests/Program.fs index 19edbb4b..1a4e09d3 100644 --- a/tests/GraphBLAS-sharp.Tests/Program.fs +++ b/tests/GraphBLAS-sharp.Tests/Program.fs @@ -17,7 +17,9 @@ let allTests = Backend.RemoveDuplicates.tests Backend.Copy.tests Backend.Replicate.tests - // Backend.EwiseAdd.tests + Backend.EwiseAdd.tests + Backend.EwiseAdd.tests2 + //Backend.EwiseAdd.tests3 Backend.Transpose.tests //Matrix.GetTuples.tests //Matrix.Mxv.tests From ca2c68749d376d39a9a53ce4d7fe3ce1b00d6324 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Sep 2022 23:27:44 +0300 Subject: [PATCH 4/4] Added documentation for converting and transposing --- src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs | 35 ++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs index 10adf1e6..72b6284a 100644 --- a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs +++ b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs @@ -36,6 +36,11 @@ module Matrix = MatrixCSR res + /// + /// Creates a new matrix, represented in CSR format, that is equal to the given one. + /// + ///OpenCL context. + ///Should be a power of 2 and greater than 1. let toCSR (clContext: ClContext) workGroupSize = let toCSR = COOMatrix.toCSR clContext workGroupSize let copy = copy clContext workGroupSize @@ -45,6 +50,12 @@ module Matrix = | MatrixCOO m -> toCSR processor m |> MatrixCSR | MatrixCSR _ -> copy processor matrix + /// + /// Returns the matrix, represented in CSR format, that is equal to the given one. + /// The given matrix should neither be used afterwards nor be disposed. + /// + ///OpenCL context. + ///Should be a power of 2 and greater than 1. let toCSRInplace (clContext: ClContext) workGroupSize = let toCSRInplace = COOMatrix.toCSRInplace clContext workGroupSize @@ -54,6 +65,11 @@ module Matrix = | MatrixCOO m -> toCSRInplace processor m |> MatrixCSR | MatrixCSR _ -> matrix + /// + /// Creates a new matrix, represented in COO format, that is equal to the given one. + /// + ///OpenCL context. + ///Should be a power of 2 and greater than 1. let toCOO (clContext: ClContext) workGroupSize = let toCOO = CSRMatrix.toCOO clContext workGroupSize let copy = copy clContext workGroupSize @@ -63,6 +79,12 @@ module Matrix = | MatrixCOO _ -> copy processor matrix | MatrixCSR m -> toCOO processor m |> MatrixCOO + /// + /// Returns the matrix, represented in COO format, that is equal to the given one. + /// The given matrix should neither be used afterwards nor be disposed. + /// + ///OpenCL context. + ///Should be a power of 2 and greater than 1. let toCOOInplace (clContext: ClContext) workGroupSize = let toCOOInplace = CSRMatrix.toCOOInplace clContext workGroupSize @@ -98,7 +120,12 @@ module Matrix = | MatrixCSR m1, MatrixCSR m2 -> CSReWiseAdd processor m1 m2 |> MatrixCSR | _ -> failwith "Matrix formats are not matching" - + /// + /// Transposes the given matrix and returns result. The storage format is preserved. + /// The given matrix should neither be used afterwards nor be disposed. + /// + ///OpenCL context. + ///Should be a power of 2 and greater than 1. let transposeInplace (clContext: ClContext) workGroupSize = let COOtransposeInplace = COOMatrix.transposeInplace clContext workGroupSize @@ -111,7 +138,11 @@ module Matrix = | MatrixCOO m -> COOtransposeInplace processor m |> MatrixCOO | MatrixCSR m -> CSRtransposeInplace processor m |> MatrixCSR - + /// + /// Transposes the given matrix and returns result as a new matrix. The storage format is preserved. + /// + ///OpenCL context. + ///Should be a power of 2 and greater than 1. let transpose (clContext: ClContext) workGroupSize = let COOtranspose = COOMatrix.transpose clContext workGroupSize