|
| 1 | +namespace GraphBLAS.FSharp.Backend.Algorithms |
| 2 | + |
| 3 | +open GraphBLAS.FSharp.Backend |
| 4 | +open Brahma.FSharp |
| 5 | +open FSharp.Quotations |
| 6 | +open GraphBLAS.FSharp.Backend.Objects |
| 7 | +open GraphBLAS.FSharp.Backend.Common |
| 8 | +open GraphBLAS.FSharp.Backend.Quotes |
| 9 | +open GraphBLAS.FSharp.Backend.Vector |
| 10 | +open GraphBLAS.FSharp.Backend.Vector.Dense |
| 11 | +open GraphBLAS.FSharp.Backend.Objects.ClContext |
| 12 | +open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions |
| 13 | +open GraphBLAS.FSharp.Backend.Objects.ClCell |
| 14 | + |
| 15 | +module SSSP = |
| 16 | + let run (clContext: ClContext) workGroupSize = |
| 17 | + |
| 18 | + let less = ArithmeticOperations.less<int> |
| 19 | + let min = ArithmeticOperations.min<int> |
| 20 | + let plus = ArithmeticOperations.intSumAsMul |
| 21 | + |
| 22 | + let spMVTo = |
| 23 | + SpMV.runTo min plus clContext workGroupSize |
| 24 | + |
| 25 | + let create = ClArray.create clContext workGroupSize |
| 26 | + |
| 27 | + let createMask = ClArray.create clContext workGroupSize |
| 28 | + |
| 29 | + let ofList = Vector.ofList clContext workGroupSize |
| 30 | + |
| 31 | + let eWiseMulLess = |
| 32 | + ClArray.map2InPlace less clContext workGroupSize |
| 33 | + |
| 34 | + let eWiseAddMin = |
| 35 | + ClArray.map2InPlace min clContext workGroupSize |
| 36 | + |
| 37 | + let fillSubVectorTo = |
| 38 | + Vector.assignByMaskInPlace (Convert.assignToOption Mask.assignComplemented) clContext workGroupSize |
| 39 | + |
| 40 | + let containsNonZero = |
| 41 | + ClArray.exists Predicates.isSome clContext workGroupSize |
| 42 | + |
| 43 | + fun (queue: MailboxProcessor<Msg>) (matrix: ClMatrix.CSR<int>) (source: int) -> |
| 44 | + let vertexCount = matrix.RowCount |
| 45 | + |
| 46 | + //None is System.Int32.MaxValue |
| 47 | + let distance = |
| 48 | + ofList queue DeviceOnly Dense vertexCount [ source, 0 ] |
| 49 | + |
| 50 | + let mutable f1 = |
| 51 | + ofList queue DeviceOnly Dense vertexCount [ source, 0 ] |
| 52 | + |
| 53 | + let mutable f2 = |
| 54 | + create queue DeviceOnly vertexCount None |
| 55 | + |> ClVector.Dense |
| 56 | + |
| 57 | + let m = |
| 58 | + createMask queue DeviceOnly vertexCount None |
| 59 | + |> ClVector.Dense |
| 60 | + |
| 61 | + let mutable stop = false |
| 62 | + |
| 63 | + while not stop do |
| 64 | + match f1, f2, distance, m with |
| 65 | + | ClVector.Dense front1, ClVector.Dense front2, ClVector.Dense dist, ClVector.Dense mask -> |
| 66 | + //Getting new frontier |
| 67 | + spMVTo queue matrix front1 front2 |
| 68 | + |
| 69 | + //Checking which distances were updated |
| 70 | + eWiseMulLess queue front2 dist mask |
| 71 | + //Updating |
| 72 | + eWiseAddMin queue dist front2 dist |
| 73 | + |
| 74 | + //Filtering unproductive vertices |
| 75 | + fillSubVectorTo queue front2 mask (clContext.CreateClCell 0) front2 |
| 76 | + |
| 77 | + //Swap fronts |
| 78 | + let temp = f1 |
| 79 | + f1 <- f2 |
| 80 | + f2 <- temp |
| 81 | + |
| 82 | + //Checking if no distances were updated |
| 83 | + stop <- |
| 84 | + not |
| 85 | + <| (containsNonZero queue mask).ToHostAndFree(queue) |
| 86 | + |
| 87 | + | _ -> failwith "not implemented" |
| 88 | + |
| 89 | + f1.Dispose queue |
| 90 | + f2.Dispose queue |
| 91 | + m.Dispose queue |
| 92 | + |
| 93 | + match distance with |
| 94 | + | ClVector.Dense dist -> dist |
| 95 | + | _ -> failwith "not implemented" |
0 commit comments