|
| 1 | +open System |
| 2 | + |
| 3 | +open Brahma.OpenCL |
| 4 | +open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic |
| 5 | + |
| 6 | +open GraphBLAS.FSharp |
| 7 | +open GraphBLAS.FSharp.Predefined |
| 8 | + |
| 9 | +open GraphBLAS.FSharp |
| 10 | +open GraphBLAS.FSharp.Algorithms |
| 11 | +open System.IO |
| 12 | +open System |
| 13 | +open MatrixBackend |
| 14 | +open GraphBLAS.FSharp.Predefined |
| 15 | +open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic |
| 16 | +open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation |
| 17 | + |
| 18 | +[<EntryPoint>] |
| 19 | +let main argv = |
| 20 | + |
| 21 | + // let workGroupSize = 256 |
| 22 | + // let workSize n = |
| 23 | + // let m = n - 1 |
| 24 | + // m - m % workGroupSize + workGroupSize |
| 25 | + |
| 26 | + // let arr = [|5;4;3;2;1|] |
| 27 | + // let arrLength = arr.Length |
| 28 | + // let arr2 = [|6;7;8;9;10|] |
| 29 | + // let command = |
| 30 | + // <@ |
| 31 | + // fun (ndRange: _1D) |
| 32 | + // (input1: int[]) |
| 33 | + // (input2: int[]) -> |
| 34 | + |
| 35 | + // let i = ndRange.GlobalID0 |
| 36 | + // if i < arrLength then |
| 37 | + // let (a, b) = (41, 42) |
| 38 | + // input1.[i] <- a |
| 39 | + // input2.[i] <- b |
| 40 | + // @> |
| 41 | + // let binder kernelP = |
| 42 | + // let ndRange = _1D(workSize arr.Length, workGroupSize) |
| 43 | + // kernelP |
| 44 | + // ndRange |
| 45 | + // arr |
| 46 | + // arr2 |
| 47 | + // let wf = |
| 48 | + // opencl { |
| 49 | + // do! RunCommand command binder |
| 50 | + // let! _ = ToHost arr |
| 51 | + // let! _ = ToHost arr2 |
| 52 | + // () |
| 53 | + // } |
| 54 | + // oclContext.RunSync wf |> ignore |
| 55 | + // for i in 0 .. arr.Length - 1 do |
| 56 | + // printfn "%i, %i" arr.[i] arr2.[i] |
| 57 | + |
| 58 | + let readMatrix (fileName: string) = |
| 59 | + use streamReader = new StreamReader(fileName) |
| 60 | + |
| 61 | + while streamReader.Peek() = int '%' do |
| 62 | + streamReader.ReadLine() |> ignore |
| 63 | + |
| 64 | + let matrixInfo = |
| 65 | + streamReader.ReadLine().Split(' ') |
| 66 | + |> Array.map int |
| 67 | + |
| 68 | + let (nrows, ncols, nnz) = |
| 69 | + matrixInfo.[0], matrixInfo.[1], matrixInfo.[2] |
| 70 | + |
| 71 | + [ 0 .. nnz - 1 ] |
| 72 | + |> List.map |
| 73 | + (fun _ -> |
| 74 | + streamReader.ReadLine().Split(' ') |
| 75 | + |> (fun line -> int line.[0], int line.[1], float line.[2])) |
| 76 | + |> List.toArray |
| 77 | + |> Array.sort |
| 78 | + |> Array.unzip3 |
| 79 | + |> fun (rows, cols, values) -> |
| 80 | + let c f x y = f y x |
| 81 | + let rows = rows |> Array.map (c (-) 1) |
| 82 | + let cols = cols |> Array.map (c (-) 1) |
| 83 | + rows, cols, values, nrows, ncols |
| 84 | + |
| 85 | + // for i in 0 .. rows.Length - 1 do |
| 86 | + // printfn "(%i, %i, %A)" rows.[i] cols.[i] values.[i] |
| 87 | + |
| 88 | + // let leftMatrix = |
| 89 | + // COOMatrix<float>(100, 100, |
| 90 | + // [|0;1;2;3;3;3;5;5;5;7|], [|0;0;2;0;1;2;7;8;9;0|], [|1.1;9.4;16.0;0.1;0.1;0.1;0.1;0.1;0.1;0.1|]) |
| 91 | + // let rightMatrix = |
| 92 | + // COOMatrix<float>(100, 100, |
| 93 | + // [|0;0;2;4;4;4;4|], [|0;1;2;0;1;2;3|], [|-1.1;5.72;-6.0;0.1;0.1;0.1;0.1|]) |
| 94 | + |
| 95 | + // let leftMatrix = |
| 96 | + // COOMatrix<float>(100, 100, |
| 97 | + // [|0;1;2|], [|0;0;2|], [|1.1;9.4;16.0|]) |
| 98 | + // let rightMatrix = |
| 99 | + // COOMatrix<float>(100, 100, |
| 100 | + // [|0;0;2|], [|0;1;2|], [|-1.1;5.72;-6.0|]) |
| 101 | + |
| 102 | + let rows, cols, values, nrows, ncols = readMatrix "matrix.mtx" |
| 103 | + let leftMatrix = COOMatrix<float>(nrows, ncols, rows, cols, values) |
| 104 | + |
| 105 | + let rows, cols, values, nrows, ncols = readMatrix "matrix2.mtx" |
| 106 | + let rightMatrix = COOMatrix<float>(nrows, ncols, rows, cols, values) |
| 107 | + |
| 108 | + let workflow = |
| 109 | + opencl { |
| 110 | + let! resultMatrix = leftMatrix.EWiseAdd rightMatrix None FloatSemiring.addMult |
| 111 | + return! resultMatrix.ToHost() |
| 112 | + } |
| 113 | + |
| 114 | + let resultMatrix : COOMatrix<float> = downcast oclContext.RunSync workflow |
| 115 | + |
| 116 | + let rows = resultMatrix.Rows |
| 117 | + let columns = resultMatrix.Columns |
| 118 | + let values = resultMatrix.Values |
| 119 | + |
| 120 | + // for i in 0 .. values.Length - 1 do |
| 121 | + // printfn "(%i, %i, %A)" rows.[i] columns.[i] values.[i] |
| 122 | + |
| 123 | + for i in 0 .. values.Length / 2 - 1 do |
| 124 | + printfn "(%i, %i, %A)" rows.[i] columns.[i] values.[i] |
| 125 | + printfn "========================================" |
| 126 | + for i in values.Length / 2 .. values.Length - 1 do |
| 127 | + printfn "(%i, %i, %A)" rows.[i] columns.[i] values.[i] |
| 128 | + |
| 129 | + 0 |
0 commit comments