From e80ef7efa9399ac2eed7581f7ea2916f788b2ee8 Mon Sep 17 00:00:00 2001 From: Kirill Garbar Date: Mon, 24 Jan 2022 19:25:01 +0300 Subject: [PATCH 1/6] Matrix.copy added and used in converters, Matrix.Dispose --- src/GraphBLAS-sharp.Backend/Matrices.fs | 5 +++ src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs | 33 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/GraphBLAS-sharp.Backend/Matrices.fs b/src/GraphBLAS-sharp.Backend/Matrices.fs index 7daeb1bf..a8abf24e 100644 --- a/src/GraphBLAS-sharp.Backend/Matrices.fs +++ b/src/GraphBLAS-sharp.Backend/Matrices.fs @@ -23,6 +23,11 @@ type Matrix<'a when 'a: struct> = | MatrixCSR matrix -> matrix.ColumnCount | MatrixCOO matrix -> matrix.ColumnCount + member this.Dispose() = + match this with + | MatrixCSR matrix -> (matrix :> IDeviceMemObject).Dispose() + | MatrixCOO matrix -> (matrix :> IDeviceMemObject).Dispose() + and CSRMatrix<'elem when 'elem: struct> = { Context: ClContext RowCount: int diff --git a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs index 66178941..e547e247 100644 --- a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs +++ b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs @@ -4,20 +4,49 @@ open Brahma.FSharp.OpenCL open Microsoft.FSharp.Quotations module Matrix = + let copy (clContext: ClContext) = + let copy = GraphBLAS.FSharp.Backend.ClArray.copy clContext + let copyData = GraphBLAS.FSharp.Backend.ClArray.copy clContext + + fun (processor: MailboxProcessor<_>) workGroupSize (matrix: Matrix<'a>) -> + match matrix with + | MatrixCOO m -> + let res = + { Context = clContext + RowCount = m.RowCount + ColumnCount = m.ColumnCount + Rows = copy processor workGroupSize m.Rows + Columns = copy processor workGroupSize m.Columns + Values = copyData processor workGroupSize m.Values + } + MatrixCOO res + | MatrixCSR m -> + let res = + { Context = clContext + RowCount = m.RowCount + ColumnCount = m.ColumnCount + RowPointers = copy processor workGroupSize m.RowPointers + Columns = copy processor workGroupSize m.Columns + Values = copyData processor workGroupSize m.Values + } + MatrixCSR res + let toCSR (clContext: ClContext) workGroupSize = let toCSR = COOMatrix.toCSR clContext workGroupSize + let copy = copy clContext fun (processor: MailboxProcessor<_>) (matrix: Matrix<'a>) -> match matrix with | MatrixCOO m -> toCSR processor m |> MatrixCSR - | MatrixCSR _ -> matrix + | MatrixCSR _ -> copy processor workGroupSize matrix let toCOO (clContext: ClContext) workGroupSize = let toCOO = CSRMatrix.toCOO clContext + let copy = copy clContext fun (processor: MailboxProcessor<_>) (matrix: Matrix<'a>) -> match matrix with - | MatrixCOO _ -> matrix + | MatrixCOO _ -> copy processor workGroupSize matrix | MatrixCSR m -> toCOO workGroupSize processor m |> MatrixCOO let eWiseAdd (clContext: ClContext) (opAdd: Expr<'a -> 'a -> 'a>) workGroupSize = From 127df0c6b1d50be3c8a455408208a70dfadfb707 Mon Sep 17 00:00:00 2001 From: Kirill Garbar Date: Mon, 24 Jan 2022 19:26:14 +0300 Subject: [PATCH 2/6] availableContexts for test's helpers --- tests/GraphBLAS-sharp.Tests/Helpers.fs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/GraphBLAS-sharp.Tests/Helpers.fs b/tests/GraphBLAS-sharp.Tests/Helpers.fs index cfd0ba42..89e59787 100644 --- a/tests/GraphBLAS-sharp.Tests/Helpers.fs +++ b/tests/GraphBLAS-sharp.Tests/Helpers.fs @@ -406,19 +406,25 @@ module Utils = .GetDeviceInfo(device, DeviceInfo.Platform, &e) .CastTo() - let platformName = + let clPlatform = Cl .GetPlatformInfo(platform, PlatformInfo.Name, &e) .ToString() + |> ClPlatform.Custom let deviceType = Cl .GetDeviceInfo(device, DeviceInfo.Type, &e) .CastTo() - failwith "fix me" - //ClContext(platformName, deviceType) - ClContext()) + let clDeviceType = + match deviceType with + | DeviceType.Cpu -> ClDeviceType.CPU + | DeviceType.Gpu -> ClDeviceType.GPU + | DeviceType.Default -> ClDeviceType.Default + | _ -> failwith "Unsupported" + + Brahma.FSharp.OpenCL.ClContext(clPlatform, clDeviceType)) let createMatrixFromArray2D matrixCase array isZero = match matrixCase with From 2fca550fb8068839a4296adbfb1004727cc45064 Mon Sep 17 00:00:00 2001 From: Kirill Garbar Date: Tue, 25 Jan 2022 21:49:58 +0300 Subject: [PATCH 3/6] CSR expandRows correction and Matrix module formatting and transfer functions --- .../Matrix/CSRMatrix/CSRMatrix.fs | 13 ++- src/GraphBLAS-sharp/Objects/Matrix.fs | 86 +++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs b/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs index 7bbd3c70..03021572 100644 --- a/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs +++ b/src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs @@ -7,13 +7,19 @@ open Microsoft.FSharp.Quotations module CSRMatrix = let private expandRows (clContext: ClContext) = let expandRows = - <@ fun (range: Range1D) workGroupSize (rowPointers: ClArray) (rowIndices: ClArray) -> + <@ fun (range: Range1D) workGroupSize (rowPointers: ClArray) (rowIndices: ClArray) rowCount nnz -> let lid = range.LocalID0 let groupId = range.GlobalID0 / workGroupSize let rowStart = rowPointers.[groupId] - let rowEnd = rowPointers.[groupId + 1] + + let rowEnd = + if groupId <> rowCount - 1 then + rowPointers.[groupId + 1] + else + nnz + let rowLength = rowEnd - rowStart let mutable i = lid @@ -36,7 +42,8 @@ module CSRMatrix = ) processor.Post( - Msg.MsgSetArguments(fun () -> kernel.SetArguments ndRange workGroupSize rowPointers rowIndices) + Msg.MsgSetArguments + (fun () -> kernel.SetArguments ndRange workGroupSize rowPointers rowIndices rowCount nnz) ) processor.Post(Msg.CreateRunMsg<_, _> kernel) diff --git a/src/GraphBLAS-sharp/Objects/Matrix.fs b/src/GraphBLAS-sharp/Objects/Matrix.fs index 5556ab37..b2cc1a5f 100644 --- a/src/GraphBLAS-sharp/Objects/Matrix.fs +++ b/src/GraphBLAS-sharp/Objects/Matrix.fs @@ -1,5 +1,8 @@ namespace GraphBLAS.FSharp +open Brahma.FSharp.OpenCL +open GraphBLAS.FSharp.Backend + type MatrixFromat = | CSR | COO @@ -18,6 +21,89 @@ type Matrix<'a when 'a: struct> = | MatrixCSR matrix -> matrix.ColumnCount | MatrixCOO matrix -> matrix.ColumnCount + member this.NNZCount = + match this with + | MatrixCOO m -> m.Values.Length + | MatrixCSR m -> m.Values.Length + + member this.ToBackend(context: ClContext) = + match this with + | MatrixCOO m -> + let rows = context.CreateClArray m.Rows + let columns = context.CreateClArray m.Columns + let values = context.CreateClArray m.Values + + let result = + { Backend.COOMatrix.Context = context + RowCount = m.RowCount + ColumnCount = m.ColumnCount + Rows = rows + Columns = columns + Values = values } + + Backend.MatrixCOO result + | MatrixCSR m -> + let rows = context.CreateClArray m.RowPointers + let columns = context.CreateClArray m.ColumnIndices + let values = context.CreateClArray m.Values + + let result = + { Backend.CSRMatrix.Context = context + RowCount = m.RowCount + ColumnCount = m.ColumnCount + RowPointers = rows + Columns = columns + Values = values } + + Backend.MatrixCSR result + + static member FromBackend (q: MailboxProcessor<_>) matrix = + match matrix with + | Backend.MatrixCOO m -> + let rows = Array.zeroCreate m.Rows.Length + let columns = Array.zeroCreate m.Columns.Length + let values = Array.zeroCreate m.Values.Length + + let _ = + q.Post(Msg.CreateToHostMsg(m.Rows, rows)) + + let _ = + q.Post(Msg.CreateToHostMsg(m.Columns, columns)) + + let _ = + q.PostAndReply(fun ch -> Msg.CreateToHostMsg(m.Values, values, ch)) + + let result = + { RowCount = m.RowCount + ColumnCount = m.ColumnCount + Rows = rows + Columns = columns + Values = values } + + MatrixCOO result + | Backend.MatrixCSR m -> + let rows = Array.zeroCreate m.RowPointers.Length + let columns = Array.zeroCreate m.Columns.Length + let values = Array.zeroCreate m.Values.Length + + let _ = + q.Post(Msg.CreateToHostMsg(m.RowPointers, rows)) + + let _ = + q.Post(Msg.CreateToHostMsg(m.Columns, columns)) + + let _ = + q.PostAndReply(fun ch -> Msg.CreateToHostMsg(m.Values, values, ch)) + + let result = + { RowCount = m.RowCount + ColumnCount = m.ColumnCount + RowPointers = rows + ColumnIndices = columns + Values = values } + + MatrixCSR result + and CSRMatrix<'a> = { RowCount: int ColumnCount: int From 5ca7d0dc3bbb0849f0d7ecb26dccc8ac16cb3a8d Mon Sep 17 00:00:00 2001 From: Kirill Garbar Date: Tue, 25 Jan 2022 21:50:47 +0300 Subject: [PATCH 4/6] OperationCase added to Helpers --- src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs | 4 ++-- tests/GraphBLAS-sharp.Tests/Helpers.fs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs index e547e247..440be6a9 100644 --- a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs +++ b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs @@ -41,13 +41,13 @@ module Matrix = | MatrixCSR _ -> copy processor workGroupSize matrix let toCOO (clContext: ClContext) workGroupSize = - let toCOO = CSRMatrix.toCOO clContext + let toCOO = CSRMatrix.toCOO clContext workGroupSize let copy = copy clContext fun (processor: MailboxProcessor<_>) (matrix: Matrix<'a>) -> match matrix with | MatrixCOO _ -> copy processor workGroupSize matrix - | MatrixCSR m -> toCOO workGroupSize processor m |> MatrixCOO + | MatrixCSR m -> toCOO processor m |> MatrixCOO let eWiseAdd (clContext: ClContext) (opAdd: Expr<'a -> 'a -> 'a>) workGroupSize = let COOeWiseAdd = diff --git a/tests/GraphBLAS-sharp.Tests/Helpers.fs b/tests/GraphBLAS-sharp.Tests/Helpers.fs index 89e59787..60aeec63 100644 --- a/tests/GraphBLAS-sharp.Tests/Helpers.fs +++ b/tests/GraphBLAS-sharp.Tests/Helpers.fs @@ -426,6 +426,21 @@ module Utils = Brahma.FSharp.OpenCL.ClContext(clPlatform, clDeviceType)) + type OperationCase = + { ClContext: ClContext + MatrixCase: MatrixFromat } + + let testCases = + [ avaliableContexts "" |> Seq.map box + listOfUnionCases + |> Seq.map box ] + |> List.map List.ofSeq + |> cartesian + |> List.map + (fun list -> + { ClContext = unbox list.[0] + MatrixCase = unbox list.[1] }) + let createMatrixFromArray2D matrixCase array isZero = match matrixCase with | CSR -> MatrixCSR <| CSRMatrix.FromArray2D(array, isZero) From f0d4850019c322b30a40817af0c8a4b224db90aa Mon Sep 17 00:00:00 2001 From: Kirill Garbar Date: Tue, 25 Jan 2022 21:57:56 +0300 Subject: [PATCH 5/6] EWiseAdd tests generalization, proper float comparison, core compilation before tests --- .../BackendCommonTests/MatrixEwiseAddTests.fs | 285 ++++++------------ 1 file changed, 85 insertions(+), 200 deletions(-) diff --git a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs index 4ea1d48c..c5ca1ab5 100644 --- a/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs +++ b/tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixEwiseAddTests.fs @@ -1,250 +1,135 @@ module Backend.EwiseAdd -open FsCheck open Expecto open Expecto.Logging open Expecto.Logging.Message open Brahma.FSharp.OpenCL open GraphBLAS.FSharp.Backend open GraphBLAS.FSharp -open GraphBLAS.FSharp.Tests.Generators open GraphBLAS.FSharp.Tests.Utils +open OpenCL.Net let logger = Log.create "EwiseAdd.Tests" -let context = - let deviceType = ClDeviceType.Default - let platformName = ClPlatform.Any - ClContext(platformName, deviceType) - -let getMatricesToAdd generator size isZero mFormat = - let gen = generator |> Arb.toGen - let m1, m2 = (Gen.sample (abs size) 1 gen).[0] - - let mtx1, mtx2 = - createMatrixFromArray2D mFormat m1 isZero, createMatrixFromArray2D mFormat m2 isZero - - mtx1, mtx2, m1, m2 - -let checkResult op zero (baseMtx1: 'a [,]) (baseMtx2: 'a [,]) (actual: Matrix<'a>) = +let checkResult isEqual op zero (baseMtx1: 'a [,]) (baseMtx2: 'a [,]) (actual: Matrix<'a>) = let rows = Array2D.length1 baseMtx1 let columns = Array2D.length2 baseMtx1 Expect.equal columns actual.ColumnCount "The number of columns should be the same." Expect.equal rows actual.RowCount "The number of rows should be the same." - let expected = Array2D.create rows columns zero + let expected2D = Array2D.create rows columns zero for i in 0 .. rows - 1 do for j in 0 .. columns - 1 do - expected.[i, j] <- op baseMtx1.[i, j] baseMtx2.[i, j] - - let actual2D = - Array2D.create actual.RowCount actual.ColumnCount zero - - let actual2D = - match actual with - | MatrixCOO actual -> - for i in 0 .. actual.Rows.Length - 1 do - actual2D.[actual.Rows.[i], actual.Columns.[i]] <- actual.Values.[i] - - actual2D - | MatrixCSR actual -> - let rowIndices = - Array.create actual.ColumnIndices.Length 0 + expected2D.[i, j] <- op baseMtx1.[i, j] baseMtx2.[i, j] - for i in 0 .. actual.RowCount - 1 do - if i < actual.RowCount - 1 then - let rowStart = actual.RowPointers.[i] - let rowEnd = actual.RowPointers.[i + 1] - let rowLength = rowEnd - rowStart + let actual2D = Array2D.create rows columns zero - for j in 0 .. rowLength - 1 do - rowIndices.[rowStart + j] <- i - else - let rowStart = actual.RowPointers.[actual.RowCount - 1] - let rowLength = rowIndices.Length - rowStart - - for j in 0 .. rowLength - 1 do - rowIndices.[rowStart + j] <- i - - for i in 0 .. rowIndices.Length - 1 do - actual2D.[rowIndices.[i], actual.ColumnIndices.[i]] <- actual.Values.[i] - - actual2D + match actual with + | MatrixCOO actual -> + for i in 0 .. actual.Rows.Length - 1 do + actual2D.[actual.Rows.[i], actual.Columns.[i]] <- actual.Values.[i] + | _ -> failwith "Impossible case." for i in 0 .. rows - 1 do for j in 0 .. columns - 1 do - Expect.equal actual2D.[i, j] expected.[i, j] "Elements of matrices should be equals." - -let testCases = - let q = context.Provider.CommandQueue + Expect.isTrue (isEqual actual2D.[i, j] expected2D.[i, j]) "Values should be the same." + +let correctnessGenericTest + zero + op + (addFun: MailboxProcessor -> Backend.Matrix<'a> -> Backend.Matrix<'a> -> Backend.Matrix<'a>) + toCOOFun + (isEqual: 'a -> 'a -> bool) + (case: OperationCase) + (leftMatrix: 'a [,], rightMatrix: 'a [,]) + = + let q = case.ClContext.Provider.CommandQueue q.Error.Add(fun e -> failwithf "%A" e) - let setSizeForAddFun mAdd = - fun (array: array<_>) -> - let wgSize = - [| for i in 0 .. 5 -> pown 2 i |] - |> Array.filter (fun i -> array.Length % i = 0) - |> Array.max - - mAdd (if wgSize = 1 then 2 else wgSize) q - - let makeTest (context: ClContext) generator size mFormat op qOp zero = - let mtx1, mtx2, baseMtx1, baseMtx2 = - getMatricesToAdd generator size ((=) zero) mFormat - - match mtx1, mtx2 with - | MatrixCOO mtx1, MatrixCOO mtx2 -> - if mtx1.Values.Length > 0 && mtx2.Values.Length > 0 then - use clRows1 = context.CreateClArray mtx1.Rows - use clColumns1 = context.CreateClArray mtx1.Columns - use clValues1 = context.CreateClArray mtx1.Values - - let m1 = - { Context = context - Backend.COOMatrix.RowCount = mtx1.RowCount - ColumnCount = mtx1.ColumnCount - Rows = clRows1 - Columns = clColumns1 - Values = clValues1 } - - use clRows2 = context.CreateClArray mtx2.Rows - use clColumns2 = context.CreateClArray mtx2.Columns - use clValues2 = context.CreateClArray mtx2.Values - - let m2 = - { Context = context - Backend.COOMatrix.RowCount = mtx2.RowCount - ColumnCount = mtx2.ColumnCount - Rows = clRows2 - Columns = clColumns2 - Values = clValues2 } - - let getAddFun = - COOMatrix.eWiseAdd context qOp |> setSizeForAddFun - - let add = getAddFun mtx1.Values - - let actual = - let res: Backend.COOMatrix<'a> = add m1 m2 - let actualRows = Array.zeroCreate res.Rows.Length - let actualColumns = Array.zeroCreate res.Columns.Length - let actualValues = Array.zeroCreate res.Values.Length - - let _ = - q.Post(Msg.CreateToHostMsg(res.Rows, actualRows)) - - let _ = - q.Post(Msg.CreateToHostMsg(res.Columns, actualColumns)) - - let _ = - q.PostAndReply(fun ch -> Msg.CreateToHostMsg(res.Values, actualValues, ch)) - - q.Post(Msg.CreateFreeMsg<_>(res.Columns)) - q.Post(Msg.CreateFreeMsg<_>(res.Rows)) - q.Post(Msg.CreateFreeMsg<_>(res.Values)) - - { RowCount = res.RowCount - ColumnCount = res.ColumnCount - Rows = actualRows - Columns = actualColumns - Values = actualValues } - - logger.debug ( - eventX "Actual is {actual}" - >> setField "actual" (sprintf "%A" actual) - ) - - checkResult op zero baseMtx1 baseMtx2 (MatrixCOO actual) + let mtx1 = + createMatrixFromArray2D case.MatrixCase leftMatrix (isEqual zero) - | MatrixCSR mtx1, MatrixCSR mtx2 -> - if mtx1.Values.Length > 0 && mtx2.Values.Length > 0 then - use clRows1 = context.CreateClArray mtx1.RowPointers - use clColumns1 = context.CreateClArray mtx1.ColumnIndices - use clValues1 = context.CreateClArray mtx1.Values + let mtx2 = + createMatrixFromArray2D case.MatrixCase rightMatrix (isEqual zero) - let m1 = - { Context = context - Backend.CSRMatrix.RowCount = mtx1.RowCount - ColumnCount = mtx1.ColumnCount - RowPointers = clRows1 - Columns = clColumns1 - Values = clValues1 } + if mtx1.NNZCount > 0 && mtx2.NNZCount > 0 then + let m1 = mtx1.ToBackend case.ClContext + let m2 = mtx2.ToBackend case.ClContext - use clRows2 = context.CreateClArray mtx2.RowPointers - use clColumns2 = context.CreateClArray mtx2.ColumnIndices - use clValues2 = context.CreateClArray mtx2.Values + let res = addFun q m1 m2 - let m2 = - { Context = context - Backend.CSRMatrix.RowCount = mtx2.RowCount - ColumnCount = mtx2.ColumnCount - RowPointers = clRows2 - Columns = clColumns2 - Values = clValues2 } - - let getAddFun = - CSRMatrix.eWiseAdd context qOp |> setSizeForAddFun + m1.Dispose() + m2.Dispose() - let add = getAddFun mtx1.Values + let cooRes = toCOOFun q res + let actual = Matrix.FromBackend q cooRes - let actual = - let res: Backend.CSRMatrix<'a> = add m1 m2 - let actualRows = Array.zeroCreate res.RowPointers.Length - let actualColumns = Array.zeroCreate res.Columns.Length - let actualValues = Array.zeroCreate res.Values.Length - - let _ = - q.Post(Msg.CreateToHostMsg(res.RowPointers, actualRows)) + cooRes.Dispose() + res.Dispose() - let _ = - q.Post(Msg.CreateToHostMsg(res.Columns, actualColumns)) + logger.debug ( + eventX "Actual is {actual}" + >> setField "actual" (sprintf "%A" actual) + ) - let _ = - q.PostAndReply(fun ch -> Msg.CreateToHostMsg(res.Values, actualValues, ch)) + checkResult isEqual op zero leftMatrix rightMatrix actual - q.Post(Msg.CreateFreeMsg<_>(res.Columns)) - q.Post(Msg.CreateFreeMsg<_>(res.RowPointers)) - q.Post(Msg.CreateFreeMsg<_>(res.Values)) +let testFixtures case = + [ let config = defaultConfig + let wgSize = 128 + //Test name on multiple devices can be duplicated due to the ClContext.toString + let getCorrectnessTestName datatype = + sprintf "Correctness on %s, %A" datatype case - { CSRMatrix.RowCount = res.RowCount - ColumnCount = res.ColumnCount - RowPointers = actualRows - ColumnIndices = actualColumns - Values = actualValues } + let boolAdd = + Matrix.eWiseAdd case.ClContext <@ (||) @> wgSize - logger.debug ( - eventX "Actual is {actual}" - >> setField "actual" (sprintf "%A" actual) - ) + let boolToCOO = Matrix.toCOO case.ClContext wgSize - checkResult op zero baseMtx1 baseMtx2 (MatrixCSR(actual)) + case + |> correctnessGenericTest false (||) boolAdd boolToCOO (=) + |> testPropertyWithConfig config (getCorrectnessTestName "bool") - | _ -> failwith "No other types of matrices tested yet." + let intAdd = + Matrix.eWiseAdd case.ClContext <@ (+) @> wgSize - [ testProperty "Correctness test on random int matrices COO" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.IntType()) size COO (+) <@ (+) @> 0) + let intToCOO = Matrix.toCOO case.ClContext wgSize - testProperty "Correctness test on random bool matrices COO" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.BoolType()) size COO (||) <@ (||) @> false) + case + |> correctnessGenericTest 0 (+) intAdd intToCOO (=) + |> testPropertyWithConfig config (getCorrectnessTestName "int") - testProperty "Correctness test on random float matrices COO" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.FloatType()) size COO (+) <@ (+) @> 0.0) + let floatAdd = + Matrix.eWiseAdd case.ClContext <@ (+) @> wgSize - testProperty "Correctness test on random byte matrices COO" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.ByteType()) size COO (+) <@ (+) @> 0uy) + let floatToCOO = Matrix.toCOO case.ClContext wgSize - testProperty "Correctness test on random int matrices CSR" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.IntType()) size CSR (+) <@ (+) @> 0) + case + |> correctnessGenericTest 0.0 (+) floatAdd floatToCOO (fun x y -> abs (x - y) < Accuracy.medium.absolute) + |> testPropertyWithConfig config (getCorrectnessTestName "float") - testProperty "Correctness test on random bool matrices CSR" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.BoolType()) size CSR (||) <@ (||) @> false) + let byteAdd = + Matrix.eWiseAdd case.ClContext <@ (+) @> wgSize - testProperty "Correctness test on random float matrices CSR" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.FloatType()) size CSR (+) <@ (+) @> 0.0) + let byteToCOO = Matrix.toCOO case.ClContext wgSize - testProperty "Correctness test on random byte matrices CSR" - <| (fun size -> makeTest context (PairOfSparseMatricesOfEqualSize.ByteType()) size CSR (+) <@ (+) @> 0uy) ] + case + |> correctnessGenericTest 0uy (+) byteAdd byteToCOO (=) + |> testPropertyWithConfig config (getCorrectnessTestName "byte") ] let tests = - testCases |> testList "Backend.EwiseAdd tests" + testCases + |> List.filter + (fun case -> + let mutable e = ErrorCode.Unknown + let device = case.ClContext.Device + + let deviceType = + Cl + .GetDeviceInfo(device, DeviceInfo.Type, &e) + .CastTo() + + deviceType = DeviceType.Default) + |> List.collect testFixtures + |> testList "Backend.Matrix.eWiseAdd tests" From 0cab5ce5b259fbb605c1d55f9274921669a382b7 Mon Sep 17 00:00:00 2001 From: Kirill Garbar Date: Tue, 25 Jan 2022 22:03:41 +0300 Subject: [PATCH 6/6] Some fantomas formatting --- src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs | 15 +++++++++------ tests/GraphBLAS-sharp.Tests/Helpers.fs | 5 ++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs index 440be6a9..f6d7454a 100644 --- a/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs +++ b/src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs @@ -5,8 +5,11 @@ open Microsoft.FSharp.Quotations module Matrix = let copy (clContext: ClContext) = - let copy = GraphBLAS.FSharp.Backend.ClArray.copy clContext - let copyData = GraphBLAS.FSharp.Backend.ClArray.copy clContext + let copy = + GraphBLAS.FSharp.Backend.ClArray.copy clContext + + let copyData = + GraphBLAS.FSharp.Backend.ClArray.copy clContext fun (processor: MailboxProcessor<_>) workGroupSize (matrix: Matrix<'a>) -> match matrix with @@ -17,8 +20,8 @@ module Matrix = ColumnCount = m.ColumnCount Rows = copy processor workGroupSize m.Rows Columns = copy processor workGroupSize m.Columns - Values = copyData processor workGroupSize m.Values - } + Values = copyData processor workGroupSize m.Values } + MatrixCOO res | MatrixCSR m -> let res = @@ -27,8 +30,8 @@ module Matrix = ColumnCount = m.ColumnCount RowPointers = copy processor workGroupSize m.RowPointers Columns = copy processor workGroupSize m.Columns - Values = copyData processor workGroupSize m.Values - } + Values = copyData processor workGroupSize m.Values } + MatrixCSR res let toCSR (clContext: ClContext) workGroupSize = diff --git a/tests/GraphBLAS-sharp.Tests/Helpers.fs b/tests/GraphBLAS-sharp.Tests/Helpers.fs index 60aeec63..5cdbdea1 100644 --- a/tests/GraphBLAS-sharp.Tests/Helpers.fs +++ b/tests/GraphBLAS-sharp.Tests/Helpers.fs @@ -429,11 +429,10 @@ module Utils = type OperationCase = { ClContext: ClContext MatrixCase: MatrixFromat } - + let testCases = [ avaliableContexts "" |> Seq.map box - listOfUnionCases - |> Seq.map box ] + listOfUnionCases |> Seq.map box ] |> List.map List.ofSeq |> cartesian |> List.map