Skip to content

Commit 22fbb59

Browse files
committed
Fixed tests for cpu, corrected scan
1 parent f22e9fa commit 22fbb59

4 files changed

Lines changed: 205 additions & 67 deletions

File tree

src/App/Program.fs

Lines changed: 162 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,21 @@ open Microsoft.FSharp.Reflection
99
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
1010
open GraphBLAS.FSharp.Predefined
1111

12-
open GraphBLAS.FSharp
12+
open OpenCL.Net
13+
open GraphBLAS.FSharp.Backend.Common
14+
open Brahma.FSharp.OpenCL.Core
1315

1416
[<EntryPoint>]
1517
let main argv =
1618

17-
// let mutable oclContext = OpenCLEvaluationContext(platform_name = "*")
18-
19-
// let xs = [|6|]
20-
// let command =
21-
// <@
22-
// fun (ndRange: _1D)
23-
// (xs: int[]) ->
24-
25-
// xs.[ndRange.GlobalID0] <- 42
26-
// @>
27-
// let wf =
28-
// opencl {
29-
// let binder k =
30-
// let ndRange = _1D(xs.Length)
31-
// k ndRange xs
32-
// do! RunCommand command binder
33-
// return! ToHost xs
34-
// }
35-
36-
// let res = oclContext.RunSync wf
37-
// printfn "%A" res
38-
39-
let mutable oclContext = OpenCLEvaluationContext()
40-
4119
let workGroupSize = 256
4220
let workSize n =
4321
let m = n - 1
4422
m - m % workGroupSize + workGroupSize
4523

24+
let mutable oclContext = OpenCLEvaluationContext("Intel*", DeviceType.Gpu)
25+
// let mutable oclContext = OpenCLEvaluationContext("NVIDIA*")
26+
4627
let leftMatrix = COOMatrix(100, 100, [|0;1;2;3;4;7|], [|1;7;5;6;0;1|], [|1.;2.;-4.;4.;5.;6.|])
4728
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.|])
4829
// let leftMatrix = COOMatrix(100, 100, [||], [||], [||])
@@ -59,8 +40,164 @@ let main argv =
5940
// let resultMatrix : COOMatrix<float> = downcast oclContext.RunSync workflow
6041
// printfn "%O" resultMatrix
6142
let res = oclContext.RunSync workflow
43+
6244
printfn "%A" res.RowIndices
6345
printfn "%A" res.ColumnIndices
6446
printfn "%A" res.Values
6547

48+
49+
50+
51+
// let len = 257
52+
// let xs = Array.create len 1
53+
// let command =
54+
// <@
55+
// fun (ndRange: _1D)
56+
// (xs: int[]) ->
57+
58+
// if ndRange.GlobalID0 < len then
59+
// barrier ()
60+
// xs.[ndRange.GlobalID0] <- 42
61+
// @>
62+
// let wf =
63+
// opencl {
64+
// do! RunCommand command <| fun kernelP ->
65+
// let ndRange = _1D(workSize xs.Length, workGroupSize)
66+
// kernelP
67+
// ndRange
68+
// xs
69+
// return! ToHost xs
70+
// }
71+
72+
// let res = oclContext.RunSync wf
73+
// printfn "%A" res.[len - 1]
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
// let longSide = 7
84+
// let shortSide = 6
85+
// let sumOfSides = longSide + shortSide
86+
// let merge =
87+
// <@
88+
// fun (ndRange: _1D)
89+
// (firstRowsBuffer: int[])
90+
// (firstColumnsBuffer: int[])
91+
// (firstValuesBuffer: float[])
92+
// (secondRowsBuffer: int[])
93+
// (secondColumnsBuffer: int[])
94+
// (secondValuesBuffer: float[])
95+
// (allRowsBuffer: int[])
96+
// (allColumnsBuffer: int[])
97+
// (allValuesBuffer: float[]) ->
98+
99+
// let i = ndRange.GlobalID0
100+
101+
// if i < sumOfSides then
102+
// let mutable beginIdxLocal = local ()
103+
// let mutable endIdxLocal = local ()
104+
// let localID = ndRange.LocalID0
105+
// if localID < 2 then
106+
// let mutable x = localID * (workGroupSize - 1) + i - 1
107+
// if x >= sumOfSides then x <- sumOfSides - 1
108+
// let diagonalNumber = x
109+
110+
// let mutable leftEdge = diagonalNumber + 1 - shortSide
111+
// if leftEdge < 0 then leftEdge <- 0
112+
113+
// let mutable rightEdge = longSide - 1
114+
// if rightEdge > diagonalNumber then rightEdge <- diagonalNumber
115+
116+
// while leftEdge <= rightEdge do
117+
// let middleIdx = (leftEdge + rightEdge) / 2
118+
// let firstIndex: uint64 = ((uint64 firstRowsBuffer.[middleIdx]) <<< 32) ||| (uint64 firstColumnsBuffer.[middleIdx])
119+
// let secondIndex: uint64 = ((uint64 secondRowsBuffer.[diagonalNumber - middleIdx]) <<< 32) ||| (uint64 secondColumnsBuffer.[diagonalNumber - middleIdx])
120+
// if firstIndex < secondIndex then leftEdge <- middleIdx + 1 else rightEdge <- middleIdx - 1
121+
122+
// // Here localID equals either 0 or 1
123+
// if localID = 0 then beginIdxLocal <- leftEdge else endIdxLocal <- leftEdge
124+
// barrier ()
125+
126+
// let beginIdx = beginIdxLocal
127+
// let endIdx = endIdxLocal
128+
// let firstLocalLength = endIdx - beginIdx
129+
// let mutable x = workGroupSize - firstLocalLength
130+
// if endIdx = longSide then x <- shortSide - i + localID + beginIdx
131+
// let secondLocalLength = x
132+
133+
// //First indices are from 0 to firstLocalLength - 1 inclusive
134+
// //Second indices are from firstLocalLength to firstLocalLength + secondLocalLength - 1 inclusive
135+
// let localIndices = localArray<uint64> workGroupSize
136+
137+
// if localID < firstLocalLength then
138+
// localIndices.[localID] <- ((uint64 firstRowsBuffer.[beginIdx + localID]) <<< 32) ||| (uint64 firstColumnsBuffer.[beginIdx + localID])
139+
// if localID < secondLocalLength then
140+
// localIndices.[firstLocalLength + localID] <- ((uint64 secondRowsBuffer.[i - beginIdx]) <<< 32) ||| (uint64 secondColumnsBuffer.[i - beginIdx])
141+
// barrier ()
142+
143+
// let mutable leftEdge = localID + 1 - secondLocalLength
144+
// if leftEdge < 0 then leftEdge <- 0
145+
146+
// let mutable rightEdge = firstLocalLength - 1
147+
// if rightEdge > localID then rightEdge <- localID
148+
149+
// while leftEdge <= rightEdge do
150+
// let middleIdx = (leftEdge + rightEdge) / 2
151+
// let firstIndex = localIndices.[middleIdx]
152+
// let secondIndex = localIndices.[firstLocalLength + localID - middleIdx]
153+
// if firstIndex < secondIndex then leftEdge <- middleIdx + 1 else rightEdge <- middleIdx - 1
154+
155+
// let boundaryX = rightEdge
156+
// let boundaryY = localID - leftEdge
157+
158+
// // boundaryX and boundaryY can't be off the right edge of array (only off the left edge)
159+
// let isValidX = boundaryX >= 0
160+
// let isValidY = boundaryY >= 0
161+
162+
// let mutable fstIdx = uint64 0
163+
// if isValidX then fstIdx <- localIndices.[boundaryX]
164+
165+
// let mutable sndIdx = uint64 0
166+
// if isValidY then sndIdx <- localIndices.[firstLocalLength + boundaryY]
167+
168+
// if not isValidX || isValidY && fstIdx < sndIdx then
169+
// allRowsBuffer.[i] <- int (sndIdx >>> 32)
170+
// allColumnsBuffer.[i] <- int sndIdx
171+
// allValuesBuffer.[i] <- secondValuesBuffer.[i - localID - beginIdx + boundaryY]
172+
// else
173+
// allRowsBuffer.[i] <- int (fstIdx >>> 32)
174+
// allColumnsBuffer.[i] <- int fstIdx
175+
// allValuesBuffer.[i] <- firstValuesBuffer.[beginIdx + boundaryX]
176+
// @>
177+
178+
// let len = 257
179+
// let command =
180+
// <@
181+
// fun (ndRange: _1D)
182+
// (xs: int[]) ->
183+
184+
// if ndRange.GlobalID0 < len then
185+
// barrier ()
186+
// xs.[ndRange.GlobalID0] <- 42
187+
// @>
188+
189+
// //'_1D -> int[] -> int[] -> float[] -> int[] -> int[] -> float[] -> int[] -> int[] -> float[] -> unit
190+
// let translate2opencl (provider: ComputeProvider) (command: Quotations.Expr<('_1D -> int[] -> unit)>) : string =
191+
// let options = ComputeProvider.DefaultOptions_p
192+
// let tOptions = []
193+
// provider.SetCompileOptions options
194+
195+
// let kernel = System.Activator.CreateInstance<Kernel<'_1D>>()
196+
// CLCodeGenerator.GenerateKernel(command, provider, kernel, tOptions) |> ignore
197+
// let code = (kernel :> ICLKernel).Source.ToString()
198+
// code
199+
200+
// let code = translate2opencl oclContext.Provider command
201+
// printfn "%s" code
202+
66203
0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module internal Scan =
112112
while verticesLength > 1 do
113113
let fstVertices = fst verticesArrays
114114
let sndVertices = snd verticesArrays
115-
do! scan fstVertices verticesLength sndVertices verticesLength
115+
do! scan fstVertices verticesLength sndVertices ((verticesLength - 1) / workGroupSize + 1)
116116
do! update fstVertices bunchLength
117117

118118
bunchLength <- bunchLength * workGroupSize

src/GraphBLAS-sharp/Backend/EWiseAdd.fs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,48 +48,48 @@ module internal EWiseAdd =
4848

4949
let i = ndRange.GlobalID0
5050

51-
if i < sumOfSides then
52-
let mutable beginIdxLocal = local ()
53-
let mutable endIdxLocal = local ()
54-
let localID = ndRange.LocalID0
55-
if localID < 2 then
56-
let mutable x = localID * (workGroupSize - 1) + i - 1
57-
if x >= sumOfSides then x <- sumOfSides - 1
58-
let diagonalNumber = x
59-
60-
let mutable leftEdge = diagonalNumber + 1 - shortSide
61-
if leftEdge < 0 then leftEdge <- 0
62-
63-
let mutable rightEdge = longSide - 1
64-
if rightEdge > diagonalNumber then rightEdge <- diagonalNumber
65-
66-
while leftEdge <= rightEdge do
67-
let middleIdx = (leftEdge + rightEdge) / 2
68-
let firstIndex: uint64 = ((uint64 firstRowsBuffer.[middleIdx]) <<< 32) ||| (uint64 firstColumnsBuffer.[middleIdx])
69-
let secondIndex: uint64 = ((uint64 secondRowsBuffer.[diagonalNumber - middleIdx]) <<< 32) ||| (uint64 secondColumnsBuffer.[diagonalNumber - middleIdx])
70-
if firstIndex < secondIndex then leftEdge <- middleIdx + 1 else rightEdge <- middleIdx - 1
71-
72-
// Here localID equals either 0 or 1
73-
if localID = 0 then beginIdxLocal <- leftEdge else endIdxLocal <- leftEdge
74-
barrier ()
75-
76-
let beginIdx = beginIdxLocal
77-
let endIdx = endIdxLocal
78-
let firstLocalLength = endIdx - beginIdx
79-
let mutable x = workGroupSize - firstLocalLength
80-
if endIdx = longSide then x <- shortSide - i + localID + beginIdx
81-
let secondLocalLength = x
82-
83-
//First indices are from 0 to firstLocalLength - 1 inclusive
84-
//Second indices are from firstLocalLength to firstLocalLength + secondLocalLength - 1 inclusive
85-
let localIndices = localArray<uint64> workGroupSize
86-
87-
if localID < firstLocalLength then
88-
localIndices.[localID] <- ((uint64 firstRowsBuffer.[beginIdx + localID]) <<< 32) ||| (uint64 firstColumnsBuffer.[beginIdx + localID])
89-
if localID < secondLocalLength then
90-
localIndices.[firstLocalLength + localID] <- ((uint64 secondRowsBuffer.[i - beginIdx]) <<< 32) ||| (uint64 secondColumnsBuffer.[i - beginIdx])
91-
barrier ()
51+
let mutable beginIdxLocal = local ()
52+
let mutable endIdxLocal = local ()
53+
let localID = ndRange.LocalID0
54+
if localID < 2 then
55+
let mutable x = localID * (workGroupSize - 1) + i - 1
56+
if x >= sumOfSides then x <- sumOfSides - 1
57+
let diagonalNumber = x
58+
59+
let mutable leftEdge = diagonalNumber + 1 - shortSide
60+
if leftEdge < 0 then leftEdge <- 0
61+
62+
let mutable rightEdge = longSide - 1
63+
if rightEdge > diagonalNumber then rightEdge <- diagonalNumber
64+
65+
while leftEdge <= rightEdge do
66+
let middleIdx = (leftEdge + rightEdge) / 2
67+
let firstIndex: uint64 = ((uint64 firstRowsBuffer.[middleIdx]) <<< 32) ||| (uint64 firstColumnsBuffer.[middleIdx])
68+
let secondIndex: uint64 = ((uint64 secondRowsBuffer.[diagonalNumber - middleIdx]) <<< 32) ||| (uint64 secondColumnsBuffer.[diagonalNumber - middleIdx])
69+
if firstIndex < secondIndex then leftEdge <- middleIdx + 1 else rightEdge <- middleIdx - 1
70+
71+
// Here localID equals either 0 or 1
72+
if localID = 0 then beginIdxLocal <- leftEdge else endIdxLocal <- leftEdge
73+
barrier ()
74+
75+
let beginIdx = beginIdxLocal
76+
let endIdx = endIdxLocal
77+
let firstLocalLength = endIdx - beginIdx
78+
let mutable x = workGroupSize - firstLocalLength
79+
if endIdx = longSide then x <- shortSide - i + localID + beginIdx
80+
let secondLocalLength = x
9281

82+
//First indices are from 0 to firstLocalLength - 1 inclusive
83+
//Second indices are from firstLocalLength to firstLocalLength + secondLocalLength - 1 inclusive
84+
let localIndices = localArray<uint64> workGroupSize
85+
86+
if localID < firstLocalLength then
87+
localIndices.[localID] <- ((uint64 firstRowsBuffer.[beginIdx + localID]) <<< 32) ||| (uint64 firstColumnsBuffer.[beginIdx + localID])
88+
if localID < secondLocalLength then
89+
localIndices.[firstLocalLength + localID] <- ((uint64 secondRowsBuffer.[i - beginIdx]) <<< 32) ||| (uint64 secondColumnsBuffer.[i - beginIdx])
90+
barrier ()
91+
92+
if i < sumOfSides then
9393
let mutable leftEdge = localID + 1 - secondLocalLength
9494
if leftEdge < 0 then leftEdge <- 0
9595

tests/GraphBLAS-sharp.Tests/OperationsTests/EWiseAddTests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ let config = {
204204
arbitrary = [typeof<PairOfSparseMatricesOfEqualSize>]
205205
startSize = 0
206206
maxTest = 10
207+
endSize = 1000000
207208
}
208209

209210
// https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/language-specification/types#value-types

0 commit comments

Comments
 (0)