Skip to content

Commit 4281f95

Browse files
committed
add: PapseVector.toDense, tests
1 parent 286b78f commit 4281f95

15 files changed

Lines changed: 271 additions & 341 deletions

File tree

src/GraphBLAS-sharp.Backend/Vector/DenseVector/DenseVector.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ module DenseVector =
261261

262262
resultValues, resultIndices
263263

264-
let toCoo<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) =
264+
let toSparse<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) =
265265

266266
let getValuesAndIndices =
267267
getValuesAndIndices clContext workGroupSize

src/GraphBLAS-sharp.Backend/Vector/SparseVector/SparseVector.fs

Lines changed: 16 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -367,125 +367,38 @@ module SparseVector =
367367
fun (processor: MailboxProcessor<_>) (leftVector: ClSparseVector<'a>) (rightVector: ClSparseVector<'b>) ->
368368
eWiseAdd processor leftVector rightVector
369369

370-
let preparePositionsComplemented (clContext: ClContext) (workGroupSize: int) =
371-
372-
let preparePositions =
373-
<@ fun (ndRange: Range1D) indicesArrayLength (inputIndices: ClArray<int>) (positions: ClArray<int>) ->
370+
let toDense (clContext: ClContext) (workGroupSize: int) =
374371

372+
let toDense =
373+
<@ fun (ndRange: Range1D) length (values: ClArray<'a>) (indices: ClArray<int>) (resultArray: ClArray<'a option>) ->
375374
let gid = ndRange.GlobalID0
376375

377-
if gid < indicesArrayLength then
378-
let index = inputIndices.[gid]
379-
380-
positions.[index] <- 0 @>
381-
382-
let kernel = clContext.Compile(preparePositions)
383-
384-
let creat = ClArray.create clContext workGroupSize
385-
386-
fun (processor: MailboxProcessor<_>) (inputIndices: ClArray<int>) (vectorSize: int) ->
387-
388-
let positions = creat processor vectorSize 1
389-
390-
let ndRange =
391-
Range1D.CreateValid(inputIndices.Length, workGroupSize)
376+
if gid < length then
377+
let index = indices.[gid]
392378

393-
let kernel = kernel.GetKernel()
379+
resultArray.[index] <- Some values.[gid] @>
394380

395-
processor.Post(
396-
Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange inputIndices.Length inputIndices positions)
397-
)
381+
let kernel = clContext.Compile(toDense)
398382

399-
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
383+
let zeroCreate = ClArray.zeroCreate clContext workGroupSize
400384

401-
positions
402-
403-
let setPositionsComplemented (clContext: ClContext) (workGroupSize: int) =
404-
405-
let setPositions =
406-
<@ fun (ndRange: Range1D) length (positions: ClArray<int>) (resultIndices: ClArray<int>) ->
407-
408-
let gid = ndRange.GlobalID0
409-
410-
if gid = length - 1
411-
|| gid < length
412-
&& positions.[gid] <> positions.[gid + 1] then
413-
let index = positions.[gid]
414-
415-
resultIndices.[index] <- gid @>
416-
417-
let kernel = clContext.Compile(setPositions)
418-
419-
let sum =
420-
ClArray.prefixSumExcludeInplace clContext workGroupSize
421-
422-
let resultLength = Array.zeroCreate 1
423-
424-
fun (processor: MailboxProcessor<_>) (positions: ClArray<int>) ->
425-
426-
let prefixArrayLenght = positions.Length
427-
428-
let resultLengthGpu = clContext.CreateClCell 0
429-
430-
let _, r = sum processor positions resultLengthGpu
431-
432-
let resultLength =
433-
let res =
434-
processor.PostAndReply(fun ch -> Msg.CreateToHostMsg<_>(r, resultLength, ch))
385+
fun (processor: MailboxProcessor<_>) (vector: ClSparseVector<'a>) ->
435386

436-
processor.Post(Msg.CreateFreeMsg<_>(r))
387+
let resultArray = zeroCreate processor vector.Size
437388

438-
res.[0]
439-
440-
let resultIndices =
441-
clContext.CreateClArray<int>(
442-
resultLength,
443-
hostAccessMode = HostAccessMode.NotAccessible,
444-
deviceAccessMode = DeviceAccessMode.ReadWrite,
445-
allocationMode = AllocationMode.Default
446-
)
447-
448-
let ndRange =
449-
Range1D.CreateValid(prefixArrayLenght, workGroupSize)
389+
let ndRange = Range1D.CreateValid(vector.Indices.Length, workGroupSize)
450390

451391
let kernel = kernel.GetKernel()
452392

453393
processor.Post(
454-
Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange prefixArrayLenght positions resultIndices)
394+
Msg.MsgSetArguments
395+
(fun () ->
396+
kernel.KernelFunc ndRange vector.Indices.Length vector.Values vector.Indices resultArray)
455397
)
456398

457-
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
458-
459-
resultIndices
460-
461-
let complemented<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) =
462-
463-
let preparePositions =
464-
preparePositionsComplemented clContext workGroupSize
465-
466-
let create =
467-
ClArray.zeroCreate clContext workGroupSize
399+
processor.Post(Msg.CreateRunMsg(kernel))
468400

469-
let setPositions =
470-
setPositionsComplemented clContext workGroupSize
471-
472-
fun (processor: MailboxProcessor<_>) (vector: ClSparseVector<'a>) ->
473-
474-
let positions =
475-
preparePositions processor vector.Indices vector.Size
476-
477-
let resultIndices = setPositions processor positions
478-
479-
let resultLenght = resultIndices.Length
480-
481-
let (ResultValues: ClArray<'a>) = create processor resultLenght
482-
483-
processor.Post(Msg.CreateFreeMsg<_>(positions))
484-
485-
{ Context = clContext
486-
Indices = resultIndices
487-
Values = ResultValues
488-
Size = vector.Size }
401+
resultArray
489402

490403
let reduce<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) =
491404

src/GraphBLAS-sharp.Backend/Vector/Vector.fs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ open GraphBLAS.FSharp.Backend.Common
88

99
module Vector =
1010
let zeroCreate (clContext: ClContext) (workGroupSize: int) =
11-
1211
let zeroCreate =
1312
ClArray.zeroCreate clContext workGroupSize
1413

@@ -69,19 +68,31 @@ module Vector =
6968

7069
let mask = copy
7170

72-
let toCoo (clContext: ClContext) (workGroupSize: int) =
73-
let toCoo =
74-
DenseVector.toCoo clContext workGroupSize
71+
let toSparse (clContext: ClContext) (workGroupSize: int) =
72+
let toSparse =
73+
DenseVector.toSparse clContext workGroupSize
7574

7675
let copy = copy clContext workGroupSize
7776

7877
fun (processor: MailboxProcessor<_>) (vector: ClVector<'a>) ->
7978
match vector with
80-
| ClVectorDense vector -> ClVectorSparse <| toCoo processor vector
79+
| ClVectorDense vector -> ClVectorSparse <| toSparse processor vector
8180
| ClVectorSparse _ -> copy processor vector
8281

83-
let elementWiseAddAtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
82+
let toDense (clContext: ClContext) (workGroupSize: int) =
83+
let toDense =
84+
SparseVector.toDense clContext workGroupSize
85+
86+
let copy = ClArray.copy clContext workGroupSize
87+
88+
fun (processor: MailboxProcessor<_>) (vector: ClVector<'a>) ->
89+
match vector with
90+
| ClVectorDense vector ->
91+
ClVectorDense <| copy processor vector
92+
| ClVectorSparse vector ->
93+
ClVectorDense <| toDense processor vector
8494

95+
let elementWiseAddAtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
8596
let addCoo =
8697
SparseVector.elementWiseAtLeastOne clContext opAdd workGroupSize
8798

@@ -102,10 +113,10 @@ module Vector =
102113
DenseVector.fillSubVector clContext workGroupSize
103114

104115
let toCooVector =
105-
DenseVector.toCoo clContext workGroupSize
116+
DenseVector.toSparse clContext workGroupSize
106117

107118
let toCooMask =
108-
DenseVector.toCoo clContext workGroupSize
119+
DenseVector.toSparse clContext workGroupSize
109120

110121
fun (processor: MailboxProcessor<_>) (vector: ClVector<'a>) (maskVector: ClVector<'b>) (value: 'a) ->
111122
match vector, maskVector with
@@ -126,20 +137,6 @@ module Vector =
126137
ClVectorDense
127138
<| denseFillVector value processor vector mask
128139

129-
let complemented (clContext: ClContext) (workGroupSize: int) =
130-
let cooComplemented =
131-
SparseVector.complemented clContext workGroupSize
132-
133-
let denseComplemented =
134-
DenseVector.complemented clContext workGroupSize
135-
136-
fun (processor: MailboxProcessor<_>) (vector: ClVector<'a>) ->
137-
match vector with
138-
| ClVectorSparse vector -> ClVectorSparse <| cooComplemented processor vector
139-
| ClVectorDense vector ->
140-
ClVectorDense
141-
<| denseComplemented processor vector
142-
143140
let reduce (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) =
144141
let cooReduce =
145142
SparseVector.reduce clContext workGroupSize opAdd

tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixElementwiseTests.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ let testFixturesEWiseAdd case =
135135
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
136136

137137
let elementwiseAddTests =
138-
getTestFromFixtures testFixturesEWiseAdd "Backend.Matrix.EWiseAdd tests"
138+
testsWithFixtures testFixturesEWiseAdd "Backend.Matrix.EWiseAdd tests"
139139

140140
let testFixturesEWiseAddAtLeastOne case =
141141
[ let config = defaultConfig
@@ -185,7 +185,7 @@ let testFixturesEWiseAddAtLeastOne case =
185185
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
186186

187187
let elementwiseAddAtLeastOneTests =
188-
getTestFromFixtures testFixturesEWiseAddAtLeastOne "Backend.Matrix.EWiseAddAtLeastOne tests"
188+
testsWithFixtures testFixturesEWiseAddAtLeastOne "Backend.Matrix.EWiseAddAtLeastOne tests"
189189

190190
let testFixturesEWiseAddAtLeastOneToCOO case =
191191
[ let config = defaultConfig
@@ -235,7 +235,7 @@ let testFixturesEWiseAddAtLeastOneToCOO case =
235235
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
236236

237237
let elementwiseAddAtLeastOneToCOOTests =
238-
getTestFromFixtures testFixturesEWiseAddAtLeastOneToCOO "Backend.Matrix.EWiseAddAtLeastOneToCOO tests"
238+
testsWithFixtures testFixturesEWiseAddAtLeastOneToCOO "Backend.Matrix.EWiseAddAtLeastOneToCOO tests"
239239

240240
let testFixturesEWiseMulAtLeastOne case =
241241
[ let config = defaultConfig
@@ -285,4 +285,4 @@ let testFixturesEWiseMulAtLeastOne case =
285285
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
286286

287287
let elementwiseMulAtLeastOneTests =
288-
getTestFromFixtures testFixturesEWiseMulAtLeastOne "Backend.Matrix.eWiseMulAtLeastOne tests"
288+
testsWithFixtures testFixturesEWiseMulAtLeastOne "Backend.Matrix.eWiseMulAtLeastOne tests"

tests/GraphBLAS-sharp.Tests/BackendCommonTests/TransposeTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,4 @@ let testFixtures case =
173173
|> testPropertyWithConfig config (getCorrectnessTestName "bool (twice transpose)") ]
174174

175175
let tests =
176-
getTestFromFixtures testFixtures "Transpose tests"
176+
testsWithFixtures testFixtures "Transpose tests"

tests/GraphBLAS-sharp.Tests/Helpers.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ module Utils =
740740
<| expected.[i]
741741
|> failtestf "%s"
742742

743-
let getTestFromFixtures<'a when 'a: equality> testFixtures name =
743+
let testsWithFixtures<'a when 'a: equality> testFixtures name =
744744
testCases<'a>
745745
|> List.filter
746746
(fun case ->

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,34 @@ open GraphBLAS.FSharp.IO
1111
let allTests =
1212
testList
1313
"All tests"
14-
[ Backend.Mxm.tests
15-
Backend.BitonicSort.tests
16-
Backend.PrefixSum.tests
17-
Backend.Scatter.tests
18-
Backend.Convert.tests
19-
Backend.RemoveDuplicates.tests
20-
Backend.Copy.tests
21-
Backend.Replicate.tests
22-
//Backend.Elementwise.elementwiseAddTests
23-
//Backend.Elementwise.elementwiseAddAtLeastOneTests
24-
//Backend.Elementwise.elementwiseAddAtLeastOneToCOOTests
25-
//Backend.Elementwise.elementwiseMulAtLeastOneTests
26-
Backend.Transpose.tests
27-
//Matrix.GetTuples.tests
28-
//Matrix.Mxv.tests
29-
//Algo.Bfs.tests
30-
Backend.Reduce.tests
31-
Backend.Vector.ZeroCreate.tests
32-
Backend.Vector.OfList.tests
33-
Backend.Vector.Copy.tests
14+
[
15+
// [ Backend.Mxm.tests
16+
// Backend.BitonicSort.tests
17+
// Backend.PrefixSum.tests
18+
// Backend.Scatter.tests
19+
// Backend.Convert.tests
20+
// Backend.RemoveDuplicates.tests
21+
// Backend.Copy.tests
22+
// Backend.Replicate.tests
23+
// //Backend.Elementwise.elementwiseAddTests
24+
// //Backend.Elementwise.elementwiseAddAtLeastOneTests
25+
// //Backend.Elementwise.elementwiseAddAtLeastOneToCOOTests
26+
// //Backend.Elementwise.elementwiseMulAtLeastOneTests
27+
// Backend.Transpose.tests
28+
// //Matrix.GetTuples.tests
29+
// //Matrix.Mxv.tests
30+
// //Algo.Bfs.tests
31+
// Backend.Reduce.tests
32+
// Backend.Vector.ZeroCreate.tests
33+
// Backend.Vector.OfList.tests
34+
// Backend.Vector.Copy.tests
3435
Backend.Vector.Convert.tests
35-
Backend.Vector.ElementWiseAddAtLeastOne.addTests
36-
Backend.Vector.ElementWiseAddAtLeastOne.mulTests
37-
Backend.Vector.FillSubVector.tests
38-
Backend.Vector.Complemented.tests
39-
Backend.Vector.Reduce.tests ]
36+
// Backend.Vector.ElementWiseAddAtLeastOne.addTests
37+
// Backend.Vector.ElementWiseAddAtLeastOne.mulTests
38+
// Backend.Vector.FillSubVector.tests
39+
// Backend.Vector.Complemented.tests
40+
// Backend.Vector.Reduce.tests ]
41+
]
4042
|> testSequenced
4143

4244
[<EntryPoint>]

0 commit comments

Comments
 (0)