Skip to content

Commit 951c192

Browse files
committed
Refactoring
1 parent 22fbb59 commit 951c192

11 files changed

Lines changed: 316 additions & 502 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"FSharp.enableAnalyzers": true,
44
"FSharp.analyzersPath": [
55
"./packages/analyzers"
6-
]
6+
],
7+
"FSharp.suggestSdkScripts": false
78
}

src/App/Program.fs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ let main argv =
2424
let mutable oclContext = OpenCLEvaluationContext("Intel*", DeviceType.Gpu)
2525
// let mutable oclContext = OpenCLEvaluationContext("NVIDIA*")
2626

27-
let leftMatrix = COOMatrix(100, 100, [|0;1;2;3;4;7|], [|1;7;5;6;0;1|], [|1.;2.;-4.;4.;5.;6.|])
28-
let rightMatrix = COOMatrix(100, 100, [|0;0;1;2;3;4;7|], [|1;5;4;5;7;0;1|], [|1.;2.;-4.;4.;5.;6.;7.|])
29-
// let leftMatrix = COOMatrix(100, 100, [||], [||], [||])
30-
// let rightMatrix = COOMatrix(100, 100, [||], [||], [||])
27+
// let leftMatrix = COOMatrix(100, 100, [|0;1;2;3;4;7|], [|1;7;5;6;0;1|], [|1.;2.;-4.;4.;5.;6.|])
28+
// let rightMatrix = COOMatrix(100, 100, [|0;0;1;2;3;4;7|], [|1;5;4;5;7;0;1|], [|1.;2.;-4.;4.;5.;6.;7.|])
29+
// let leftMatrix = COOMatrix(100, 100, [|0;0|], [|0;1|], [|true;true|])
30+
// let rightMatrix = COOMatrix(100, 100, [|0;1|], [|0;0|], [|true;true|])
31+
let leftMatrix = COOMatrix(300, 300, Array.init 500 (fun i -> 10 + i / 300), Array.init 500 (fun i -> i % 300), Array.create 500 true)
32+
let rightMatrix = COOMatrix(300, 300, Array.init 600 (fun i -> i / 300), Array.init 600 (fun i -> i % 300), Array.create 600 true)
3133

3234
let workflow =
3335
opencl {
34-
let! resultMatrix = leftMatrix.EWiseAdd rightMatrix None FloatSemiring.addMult
36+
let! resultMatrix = leftMatrix.EWiseAdd rightMatrix None BooleanSemiring.anyAll//FloatSemiring.addMult
3537
let! tuples = resultMatrix.GetTuples()
3638
return! tuples.ToHost()
3739
//return! resultMatrix.ToHost()
@@ -41,9 +43,12 @@ let main argv =
4143
// printfn "%O" resultMatrix
4244
let res = oclContext.RunSync workflow
4345

44-
printfn "%A" res.RowIndices
45-
printfn "%A" res.ColumnIndices
46-
printfn "%A" res.Values
46+
for i in 0 .. res.Values.Length - 1 do
47+
printf "(%A, %A, %A); " res.RowIndices.[i] res.ColumnIndices.[i] res.Values.[i]
48+
49+
// printfn "%A" res.RowIndices
50+
// printfn "%A" res.ColumnIndices
51+
// printfn "%A" res.Values
4752

4853

4954

src/GraphBLAS-sharp/Backend/Common/Copy.fs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ namespace GraphBLAS.FSharp.Backend.Common
22

33
open Brahma.OpenCL
44
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
5-
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
65
open Utils
76

87
module internal Copy =
9-
let runNotEmpty (inputArray: 'a[]) =
10-
let outputArray = Array.zeroCreate inputArray.Length
8+
let runNotEmpty (inputArray: 'a[]) = opencl {
119
let inputArrayLength = inputArray.Length
1210
let copy =
1311
<@
@@ -19,12 +17,13 @@ module internal Copy =
1917
if i < inputArrayLength then
2018
outputArrayBuffer.[i] <- inputArrayBuffer.[i]
2119
@>
22-
let binder kernelP =
20+
21+
let outputArray = Array.zeroCreate inputArray.Length
22+
23+
do! RunCommand copy <| fun kernelPrepare ->
2324
let ndRange = _1D(workSize inputArray.Length, workGroupSize)
24-
kernelP ndRange inputArray outputArray
25-
opencl {
26-
do! RunCommand copy binder
27-
return outputArray
28-
}
25+
kernelPrepare ndRange inputArray outputArray
26+
return outputArray
27+
}
2928

3029
let run (inputArray: 'a[]) = if inputArray.Length = 0 then opencl { return [||] } else runNotEmpty inputArray

src/GraphBLAS-sharp/Backend/Common/Scan.fs renamed to src/GraphBLAS-sharp/Backend/Common/PrefixSum.fs

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,25 @@ namespace GraphBLAS.FSharp.Backend.Common
33
open Brahma.OpenCL
44
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
55
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
6-
open Utils
76

87
// functions in mudule could be named run\get\if\it\t
98
// like mentioned here https://www.reddit.com/r/fsharp/comments/5kvsyk/modules_or_namespaces/dbt0zf7?utm_source=share&utm_medium=web2x&context=3
10-
module internal Scan =
11-
// Changes inputArray
12-
let run (inputArray: int[]) (totalSum: int[]) =
13-
let outputArray = inputArray
14-
let outputArrayLength = outputArray.Length
15-
let workGroupSize = workGroupSize
9+
module internal PrefixSum =
10+
let scan (inputArray: int[]) (inputArrayLength: int) (vertices: int[]) (verticesLength: int) (totalSum: int[]) : OpenCLEvaluation<unit> = opencl {
11+
let workGroupSize = Utils.workGroupSize
1612

1713
let scan =
1814
<@
1915
fun (ndRange: _1D)
2016
(resultBuffer: int[])
21-
(resultLength: int)
2217
(verticesBuffer: int[])
23-
(verticesLength: int)
2418
(totalSumBuffer: int[]) ->
2519

2620
let resultLocalBuffer = localArray<int> workGroupSize
2721
let i = ndRange.GlobalID0
2822
let localID = ndRange.LocalID0
2923

30-
if i < resultLength then resultLocalBuffer.[localID] <- resultBuffer.[i] else resultLocalBuffer.[localID] <- 0
24+
if i < inputArrayLength then resultLocalBuffer.[localID] <- resultBuffer.[i] else resultLocalBuffer.[localID] <- 0
3125

3226
let mutable step = 2
3327
while step <= workGroupSize do
@@ -56,71 +50,63 @@ module internal Scan =
5650
step <- step >>> 1
5751
barrier ()
5852

59-
if i < resultLength then resultBuffer.[i] <- resultLocalBuffer.[localID]
53+
if i < inputArrayLength then resultBuffer.[i] <- resultLocalBuffer.[localID]
6054
@>
6155

62-
let scan array length vertices verticesLength =
63-
opencl {
64-
let binder kernelP =
65-
let ndRange = _1D(workSize length, workGroupSize)
66-
kernelP
67-
ndRange
68-
array
69-
length
70-
vertices
71-
verticesLength
72-
totalSum
73-
do! RunCommand scan binder
74-
}
56+
do! RunCommand scan <| fun kernelPrepare ->
57+
let ndRange = _1D(Utils.workSize inputArrayLength, workGroupSize)
58+
kernelPrepare
59+
ndRange
60+
inputArray
61+
vertices
62+
totalSum
63+
}
64+
65+
let update (inputArray: int[]) (inputArrayLength: int) (vertices: int[]) (bunchLength: int) : OpenCLEvaluation<unit> = opencl {
66+
let workGroupSize = Utils.workGroupSize
7567

7668
let update =
7769
<@
7870
fun (ndRange: _1D)
7971
(resultBuffer: int[])
80-
(resultLength: int)
81-
(verticesBuffer: int[])
82-
(bunchLength: int) ->
72+
(verticesBuffer: int[]) ->
8373

8474
let i = ndRange.GlobalID0 + bunchLength
85-
if i < resultLength then
75+
if i < inputArrayLength then
8676
resultBuffer.[i] <- resultBuffer.[i] + verticesBuffer.[i / bunchLength]
8777
@>
8878

89-
let update vertices depth =
90-
opencl {
91-
let binder kernelP =
92-
let ndRange = _1D(workSize outputArrayLength - depth, workGroupSize)
93-
kernelP
94-
ndRange
95-
outputArray
96-
outputArrayLength
97-
vertices
98-
depth
99-
do! RunCommand update binder
100-
}
101-
102-
let firstVertices = Array.zeroCreate <| (outputArrayLength - 1) / workGroupSize + 1
79+
do! RunCommand update <| fun kernelPrepare ->
80+
let ndRange = _1D(Utils.workSize inputArrayLength - bunchLength, workGroupSize)
81+
kernelPrepare
82+
ndRange
83+
inputArray
84+
vertices
85+
}
86+
87+
// Changes received arrays
88+
let run (inputArray: int[]) (totalSum: int[]) = opencl {
89+
let workGroupSize = Utils.workGroupSize
90+
91+
let firstVertices = Array.zeroCreate <| (inputArray.Length - 1) / workGroupSize + 1
10392
let secondVertices = Array.zeroCreate <| (firstVertices.Length - 1) / workGroupSize + 1
10493
let mutable verticesArrays = firstVertices, secondVertices
10594
let swap (a, b) = (b, a)
10695

107-
opencl {
108-
let mutable verticesLength = (outputArrayLength - 1) / workGroupSize + 1
109-
let mutable bunchLength = workGroupSize
110-
111-
do! scan outputArray outputArrayLength <| fst verticesArrays <| verticesLength
112-
while verticesLength > 1 do
113-
let fstVertices = fst verticesArrays
114-
let sndVertices = snd verticesArrays
115-
do! scan fstVertices verticesLength sndVertices ((verticesLength - 1) / workGroupSize + 1)
116-
do! update fstVertices bunchLength
96+
let mutable verticesLength = (inputArray.Length - 1) / workGroupSize + 1
97+
let mutable bunchLength = workGroupSize
11798

118-
bunchLength <- bunchLength * workGroupSize
119-
verticesArrays <- swap verticesArrays
120-
verticesLength <- (verticesLength - 1) / workGroupSize + 1
99+
do! scan inputArray inputArray.Length (fst verticesArrays) verticesLength totalSum
100+
while verticesLength > 1 do
101+
let fstVertices = fst verticesArrays
102+
let sndVertices = snd verticesArrays
103+
do! scan fstVertices verticesLength sndVertices ((verticesLength - 1) / workGroupSize + 1) totalSum
104+
do! update inputArray inputArray.Length fstVertices bunchLength
121105

122-
return outputArray, totalSum
123-
}
106+
bunchLength <- bunchLength * workGroupSize
107+
verticesArrays <- swap verticesArrays
108+
verticesLength <- (verticesLength - 1) / workGroupSize + 1
109+
}
124110

125111
// let rec v1 (inputArray: int[]) =
126112
// let outputArray = Array.zeroCreate inputArray.Length

0 commit comments

Comments
 (0)