Skip to content

Commit f8b4bf6

Browse files
committed
Fixed mistake in prefix sum
1 parent bdea9df commit f8b4bf6

3 files changed

Lines changed: 235 additions & 209 deletions

File tree

src/App/Program.fs

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,69 @@ open GraphBLAS.FSharp.Predefined
1010

1111
[<EntryPoint>]
1212
let main argv =
13-
let fstMatrix = COOMatrix(100, 100, [|0;1;1;2;5|], [|4;1;50;2;5|], [|1.0;2.0;76.0;3.0;6.0|])
14-
let sndMatrix = COOMatrix(100, 100, [|0;1;1;2;5|], [|4;2;50;3;5|], [|-0.8;2.0;-76.0;3.0;6.0|])
15-
let workflow =
16-
opencl {
17-
let! result = fstMatrix.EWiseAdd sndMatrix None FloatSemiring.addMult
18-
let! _ = result.ToHost ()
19-
return result
20-
}
21-
let res: COOMatrix<float> = downcast oclContext.RunSync workflow
13+
// // let fstMatrix = COOMatrix(1, 2, [|0|], [|1|], [|true|])
14+
// // let sndMatrix = COOMatrix(1, 2, [|0|], [|0|], [|true|])
15+
// let fstMatrix = COOMatrix(100, 100, [|0;1;1;2;5|], [|4;1;50;2;5|], [|1.0;2.0;76.0;3.0;6.0|])
16+
// let sndMatrix = COOMatrix(100, 100, [|0;1;1;2;5|], [|4;2;50;3;5|], [|-0.8;2.0;-76.0;3.0;6.0|])
17+
// let workflow =
18+
// opencl {
19+
// let! result = fstMatrix.EWiseAdd sndMatrix None FloatSemiring.addMult
20+
// let! _ = result.ToHost ()
21+
// return result
22+
// }
23+
// let res: COOMatrix<float> = downcast oclContext.RunSync workflow
2224

23-
let indices = res.Indices
24-
// let rows = res.Rows
25-
// let columns = res.Columns
26-
let values = res.Values
25+
// let indices = res.Indices
26+
// // let rows = res.Rows
27+
// // let columns = res.Columns
28+
// let values = res.Values
2729

28-
// for i in 0 .. rows.Length - 1 do
29-
// printfn "(%i, %i, %A)" rows.[i] columns.[i] values.[i]
30+
// // for i in 0 .. rows.Length - 1 do
31+
// // printfn "(%i, %i, %A)" rows.[i] columns.[i] values.[i]
3032

31-
for i in 0 .. indices.Length - 1 do
32-
let index = indices.[i]
33-
let a = int (index >>> 32)
34-
let b = int index
35-
printfn "(%i, %i, %A)" a b values.[i]
33+
// for i in 0 .. indices.Length - 1 do
34+
// let index = indices.[i]
35+
// let a = int (index >>> 32)
36+
// let b = int index
37+
// printfn "(%i, %i, %A)" a b values.[i]
3638

3739
// let allIndicesLength = 42
38-
// let workGroupSize = 256
39-
// let longSide = 72
40-
// let shortSide = 27
40+
let workGroupSize = 256
41+
let longSide = 72
42+
let shortSide = 27
43+
let defaultIdx = Unchecked.defaultof<int>
4144

42-
// let command =
43-
// <@
44-
// fun (ndRange: _1D)
45-
// (arr: int[]) ->
45+
// let arrLength = 10
4646

47-
// arr.[ndRange.GlobalID0] <-
48-
// if true then 41 else 42
49-
// @>
47+
let command =
48+
<@
49+
fun (ndRange: _1D)
50+
(buf: int[]) ->
5051

51-
// let translate2opencl (provider: ComputeProvider) (command: Quotations.Expr<(_1D -> int[] -> unit)>) : string =
52-
// let options = ComputeProvider.DefaultOptions_p
53-
// let tOptions = []
54-
// provider.SetCompileOptions options
52+
let i = ndRange.GlobalID0
53+
let f a b =
54+
if a > b
55+
then
56+
let x = a * a
57+
if x > 42
58+
then x + b
59+
else a + x
60+
else a - b
61+
buf.[i] <- f 2 3
62+
@>
5563

56-
// let kernel = System.Activator.CreateInstance<Kernel<_1D>>()
57-
// CLCodeGenerator.GenerateKernel(command, provider, kernel, tOptions) |> ignore
58-
// let code = (kernel :> ICLKernel).Source.ToString()
59-
// code
64+
let translate2opencl (provider: ComputeProvider) (command: Quotations.Expr<(_1D -> int[] -> unit)>) : string =
65+
let options = ComputeProvider.DefaultOptions_p
66+
let tOptions = []
67+
provider.SetCompileOptions options
6068

61-
// let code = translate2opencl oclContext.Provider command
69+
let kernel = System.Activator.CreateInstance<Kernel<_1D>>()
70+
CLCodeGenerator.GenerateKernel(command, provider, kernel, tOptions) |> ignore
71+
let code = (kernel :> ICLKernel).Source.ToString()
72+
code
6273

63-
// printf "%s" code
74+
let code = translate2opencl oclContext.Provider command
75+
76+
printf "%s" code
6477

6578
0

src/GraphBLAS-sharp/Implementations.fs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
143143
yield (uint64 rows.[i]) <<< 32 ||| (uint64 columns.[i]) |]
144144
COOMatrix(rowCount, columnCount, indices, values)
145145

146+
override this.ToString() =
147+
[
148+
sprintf "COO Matrix %ix%i \n" rowCount columnCount
149+
sprintf "Indices: %A \n" indices
150+
sprintf "Values: %A \n" values
151+
]
152+
|> String.concat ""
153+
146154
override this.Clear () = failwith "Not Implemented"
147155
override this.Copy () = failwith "Not Implemented"
148156
override this.Resize a b = failwith "Not Implemented"
@@ -246,7 +254,7 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
246254

247255
let allIndicesLength = allIndices.Length
248256

249-
let createSortedConcatenation =
257+
let merge =
250258
<@
251259
fun (ndRange: _1D)
252260
(firstIndicesBuffer: uint64[])
@@ -259,7 +267,8 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
259267
let i = ndRange.GlobalID0
260268

261269
if i < allIndicesLength then
262-
let knots = localArray<int> 2
270+
let mutable beginIdxLocal = local ()
271+
let mutable endIdxLocal = local ()
263272
let localID = ndRange.LocalID0
264273
if localID < 2 then
265274
let mutable x = localID * (workGroupSize - 1) + i - 1
@@ -278,11 +287,12 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
278287
let secondIndex = secondIndicesBuffer.[diagonalNumber - middleIdx]
279288
if firstIndex < secondIndex then leftEdge <- middleIdx + 1 else rightEdge <- middleIdx - 1
280289

281-
knots.[localID] <- leftEdge
290+
// Here localID equals either 0 or 1
291+
if localID = 0 then beginIdxLocal <- leftEdge else endIdxLocal <- leftEdge
282292
barrier ()
283293

284-
let beginIdx = knots.[0] // BANK CONFLICTS?
285-
let endIdx = knots.[1]
294+
let beginIdx = beginIdxLocal
295+
let endIdx = endIdxLocal
286296
let firstLocalLength = endIdx - beginIdx
287297
let mutable x = workGroupSize - firstLocalLength
288298
if endIdx = longSide then x <- shortSide - i + localID + beginIdx
@@ -331,7 +341,7 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
331341
allValuesBuffer.[i] <- firstValuesBuffer.[beginIdx + boundaryX]
332342
@>
333343

334-
let createSortedConcatenation =
344+
let merge =
335345
opencl {
336346
let binder kernelP =
337347
let ndRange = _1D(workSize allIndices.Length, workGroupSize)
@@ -343,7 +353,7 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
343353
secondValues
344354
allIndices
345355
allValues
346-
do! RunCommand createSortedConcatenation binder
356+
do! RunCommand merge binder
347357
}
348358

349359
let auxiliaryArray = Array.create allIndices.Length 1
@@ -400,29 +410,25 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
400410
resultValuesBuffer.[index] <- allValuesBuffer.[i]
401411
@>
402412

403-
let resultIndices = Array.zeroCreate allIndices.Length
404-
let resultValues = Array.create allValues.Length zero
405-
406-
let createUnion =
407-
opencl {
408-
let! prefixSumArray = prefixSum3 auxiliaryArray
409-
let binder kernelP =
410-
let ndRange = _1D(workSize auxiliaryArray.Length, workGroupSize)
411-
kernelP
412-
ndRange
413-
allIndices
414-
allValues
415-
prefixSumArray
416-
resultIndices
417-
resultValues
418-
do! RunCommand createUnion binder
419-
}
420-
421413
opencl {
422-
do! createSortedConcatenation
414+
do! merge
423415
do! filterThroughMask
424416
do! fillAuxiliaryArray
425-
do! createUnion
417+
let! prefixSumArray, resultLength = prefixSum auxiliaryArray
418+
let! _ = ToHost resultLength
419+
let resultLength = resultLength.[0]
420+
let resultIndices = Array.zeroCreate resultLength
421+
let resultValues = Array.create resultLength zero
422+
let binder kernelP =
423+
let ndRange = _1D(workSize prefixSumArray.Length, workGroupSize)
424+
kernelP
425+
ndRange
426+
allIndices
427+
allValues
428+
prefixSumArray
429+
resultIndices
430+
resultValues
431+
do! RunCommand createUnion binder
426432

427433
return upcast COOMatrix<'a>(this.RowCount, this.ColumnCount, resultIndices, resultValues)
428434
}
@@ -638,7 +644,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
638644

639645
let createUnion =
640646
opencl {
641-
let! prefixSumArray = Toolbox.prefixSum2 auxiliaryArray
647+
let! prefixSumArray, _ = prefixSum auxiliaryArray
642648
let binder kernelP =
643649
let ndRange = _1D(workSize auxiliaryArray.Length, workGroupSize)
644650
kernelP

0 commit comments

Comments
 (0)