Skip to content

Commit 5c4f3e1

Browse files
committed
Remove MonoidicType; remove building from array
1 parent 805aa5f commit 5c4f3e1

11 files changed

Lines changed: 126 additions & 165 deletions

File tree

src/GraphBLAS-sharp/AlgebraicStructures.fs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,3 @@ type Semiring<'a> =
5959
member this.Times = this.TimesSemigroup.AssociativeOp
6060
member this.Zero = this.PlusMonoid.Identity
6161
member this.Plus = this.PlusMonoid.AssociativeOp
62-
63-
(*
64-
мотивация:
65-
хотим, чтобы ноль был нулем (даже если он явно в матрице хранится)
66-
и все моноиды, определенные над MonoidicType 'a имели корректную семантику
67-
(если получился 0 и мы сменили моноид, то этот элемент все еще будет нулем в другом моноиде)
68-
*)
69-
70-
[<Struct>]
71-
type MonoidicType<'a> =
72-
| Just of 'a
73-
| Zero
74-
75-
module MonoidicType =
76-
let wrap (isZero: 'a -> bool) x =
77-
if isZero x then Zero
78-
else Just x

src/GraphBLAS-sharp/Backend/CSRMatrix/Mxv.fs

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,69 @@ open GraphBLAS.FSharp.Backend.Common
77
open Brahma.OpenCL
88

99
module internal Mxv =
10+
()
1011
// not finished
11-
let pcsr (matrix: CSRMatrix<'a>) (vector: BitmapVector<'a>) mask (semiring: ISemiring<'a>) = opencl {
12-
let (ClosedBinaryOp plus) = semiring.Plus
13-
let (ClosedBinaryOp times) = semiring.Times
12+
// let pcsr (matrix: CSRMatrix<'a>) (vector: BitmapVector<'a>) mask (semiring: ISemiring<'a>) = opencl {
13+
// let (ClosedBinaryOp plus) = semiring.Plus
14+
// let (ClosedBinaryOp times) = semiring.Times
1415

15-
let matrixLength = matrix.Values.Length
16+
// let matrixLength = matrix.Values.Length
1617

17-
let kernel1 =
18-
<@
19-
fun (ndRange: _1D)
20-
(matrixColumns: int[])
21-
(matrixValues: 'a[])
22-
(vectorBitmap: bool[])
23-
(vectorValues: 'a[])
24-
(intermediateArray: 'a[]) ->
18+
// let kernel1 =
19+
// <@
20+
// fun (ndRange: _1D)
21+
// (matrixColumns: int[])
22+
// (matrixValues: 'a[])
23+
// (vectorBitmap: bool[])
24+
// (vectorValues: 'a[])
25+
// (intermediateArray: 'a[]) ->
2526

26-
let i = ndRange.GlobalID0
27-
if i < matrixLength && vectorBitmap.[i] then
28-
let value = matrixValues.[i]
29-
let column = matrixColumns.[i]
30-
intermediateArray.[i] <- (%times) value vectorValues.[column]
31-
@>
27+
// let i = ndRange.GlobalID0
28+
// if i < matrixLength && vectorBitmap.[i] then
29+
// let value = matrixValues.[i]
30+
// let column = matrixColumns.[i]
31+
// intermediateArray.[i] <- (%times) value vectorValues.[column]
32+
// @>
3233

33-
let kernel2 =
34-
<@
35-
fun (ndRange: _1D)
36-
(intermediateArray: 'a[])
37-
(matrixPtr: int[])
38-
(outputVector: 'a[]) ->
34+
// let kernel2 =
35+
// <@
36+
// fun (ndRange: _1D)
37+
// (intermediateArray: 'a[])
38+
// (matrixPtr: int[])
39+
// (outputVector: 'a[]) ->
3940

40-
let gid = ndRange.GlobalID0
41-
let lid = ndRange.LocalID0
41+
// let gid = ndRange.GlobalID0
42+
// let lid = ndRange.LocalID0
4243

43-
let localPtr = localArray<int> (Utils.workGroupSize + 1)
44-
localPtr.[lid] <- matrixPtr.[gid]
45-
if lid = 0 then
46-
localPtr.[Utils.workGroupSize] <- matrixPtr.[gid + Utils.workGroupSize]
47-
barrier ()
48-
@>
44+
// let localPtr = localArray<int> (Utils.workGroupSize + 1)
45+
// localPtr.[lid] <- matrixPtr.[gid]
46+
// if lid = 0 then
47+
// localPtr.[Utils.workGroupSize] <- matrixPtr.[gid + Utils.workGroupSize]
48+
// barrier ()
49+
// @>
4950

50-
let intermediateArray = Array.zeroCreate<'a> matrixLength
51-
do! RunCommand kernel1 <| fun kernelPrepare ->
52-
let range = _1D(Utils.workSize matrixLength, Utils.workGroupSize)
53-
kernelPrepare
54-
range
55-
matrix.ColumnIndices
56-
matrix.Values
57-
vector.Bitmap
58-
vector.Values
59-
intermediateArray
51+
// let intermediateArray = Array.zeroCreate<'a> matrixLength
52+
// do! RunCommand kernel1 <| fun kernelPrepare ->
53+
// let range = _1D(Utils.workSize matrixLength, Utils.workGroupSize)
54+
// kernelPrepare
55+
// range
56+
// matrix.ColumnIndices
57+
// matrix.Values
58+
// vector.Bitmap
59+
// vector.Values
60+
// intermediateArray
6061

61-
let outputVector = Array.zeroCreate<'a> matrix.RowCount
62-
do! RunCommand kernel2 <| fun kernelPrepare ->
63-
let range = _1D(Utils.workSize matrixLength, Utils.workGroupSize)
64-
kernelPrepare
65-
range
66-
intermediateArray
67-
matrix.RowPointers
68-
outputVector
62+
// let outputVector = Array.zeroCreate<'a> matrix.RowCount
63+
// do! RunCommand kernel2 <| fun kernelPrepare ->
64+
// let range = _1D(Utils.workSize matrixLength, Utils.workGroupSize)
65+
// kernelPrepare
66+
// range
67+
// intermediateArray
68+
// matrix.RowPointers
69+
// outputVector
6970

70-
return {
71-
Bitmap = vector.Bitmap
72-
Values = outputVector
73-
}
74-
}
71+
// return {
72+
// Bitmap = vector.Bitmap
73+
// Values = outputVector
74+
// }
75+
// }

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

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,48 @@ open Brahma.OpenCL
44
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
55

66
module internal RemoveDuplicates =
7-
let run (array: 'a[]) = opencl {
8-
let inputLength = array.Length
9-
10-
let getUniqueBitmap =
11-
<@
12-
fun (ndRange: _1D)
13-
(inputArray: 'a[])
14-
(isUniqueBitmap: int[]) ->
15-
16-
let i = ndRange.GlobalID0
17-
if i < inputLength - 1 && inputArray.[i] = inputArray.[i + 1] then
18-
isUniqueBitmap.[i] <- 0
19-
@>
20-
21-
let setPositions =
22-
<@
23-
fun (ndRange: _1D)
24-
(inputArray: 'a[])
25-
(positions: int[])
26-
(ouputArray: 'a[]) ->
27-
28-
let i = ndRange.GlobalID0
29-
if i < inputLength then
30-
let position = positions.[i] - 1
31-
ouputArray.[position] <- inputArray.[i]
32-
@>
33-
34-
let bitmap = Array.create inputLength 1
35-
do! RunCommand getUniqueBitmap <| fun kernelPrepare ->
36-
let range = _1D(Utils.workSize inputLength, Utils.workGroupSize)
37-
kernelPrepare range array bitmap
38-
39-
let resultLength = Array.zeroCreate 1
40-
do! PrefixSum.runInplace bitmap resultLength
41-
let! _ = ToHost resultLength
42-
let resultLength = resultLength.[0]
43-
44-
let outputArray = Array.zeroCreate resultLength
45-
do! RunCommand setPositions <| fun kernelPrepare ->
46-
let range = _1D(Utils.workSize inputLength, Utils.workGroupSize)
47-
kernelPrepare range array bitmap outputArray
48-
49-
return outputArray
50-
}
7+
()
8+
// let run (array: 'a[]) = opencl {
9+
// let inputLength = array.Length
10+
11+
// let getUniqueBitmap =
12+
// <@
13+
// fun (ndRange: _1D)
14+
// (inputArray: 'a[])
15+
// (isUniqueBitmap: int[]) ->
16+
17+
// let i = ndRange.GlobalID0
18+
// if i < inputLength - 1 && inputArray.[i] = inputArray.[i + 1] then
19+
// isUniqueBitmap.[i] <- 0
20+
// @>
21+
22+
// let setPositions =
23+
// <@
24+
// fun (ndRange: _1D)
25+
// (inputArray: 'a[])
26+
// (positions: int[])
27+
// (ouputArray: 'a[]) ->
28+
29+
// let i = ndRange.GlobalID0
30+
// if i < inputLength then
31+
// let position = positions.[i] - 1
32+
// ouputArray.[position] <- inputArray.[i]
33+
// @>
34+
35+
// let bitmap = Array.create inputLength 1
36+
// do! RunCommand getUniqueBitmap <| fun kernelPrepare ->
37+
// let range = _1D(Utils.workSize inputLength, Utils.workGroupSize)
38+
// kernelPrepare range array bitmap
39+
40+
// let resultLength = Array.zeroCreate 1
41+
// do! PrefixSum.runInplace bitmap resultLength
42+
// let! _ = ToHost resultLength
43+
// let resultLength = resultLength.[0]
44+
45+
// let outputArray = Array.zeroCreate resultLength
46+
// do! RunCommand setPositions <| fun kernelPrepare ->
47+
// let range = _1D(Utils.workSize inputLength, Utils.workGroupSize)
48+
// kernelPrepare range array bitmap outputArray
49+
50+
// return outputArray
51+
// }

src/GraphBLAS-sharp/GraphBLAS-sharp.fsproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Compile Include="Backend/Common/Utils.fs" />
2121
<Compile Include="Backend/Common/Copy.fs" />
2222
<Compile Include="Backend/Common/PrefixSum.fs" />
23-
<Compile Include="Backend\Common\RemoveDuplicates.fs" />
23+
<Compile Include="Backend/Common/RemoveDuplicates.fs" />
2424
<Compile Include="Backend/CSRMatrix/Mxv.fs" />
2525
<Compile Include="Backend/COOMatrix/Utilities/Merge.fs" />
2626
<Compile Include="Backend/COOMatrix/Utilities/PreparePositions.fs" />
@@ -48,4 +48,4 @@
4848
</Content>
4949
</ItemGroup>
5050
<Import Project="..\..\.paket\Paket.Restore.targets" />
51-
</Project>
51+
</Project>

src/GraphBLAS-sharp/GraphblasEvaluation.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,5 @@ type GraphblasBuilder() =
8585
[<AutoOpen>]
8686
module GraphblasBuilder =
8787
let graphblas = GraphblasBuilder()
88+
89+
let (>>=) x f = EvalGB.bind f x

src/GraphBLAS-sharp/Methods/Matrix.fs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@ module Matrix =
1919
let ofList (rowCount: int) (columnCount: int) (elements: (int * int * 'a) list) : GraphblasEvaluation<Matrix<'a>> =
2020
failwith "Not Implemented yet"
2121

22-
let ofArray2D (array: 'a[,]) : GraphblasEvaluation<Matrix<'a>> =
23-
failwith "Not Implemented yet"
22+
// можно оставить, но с условием, что будет создаваться full matrix,
23+
// которую можно будет проредить потом (но вообще это initом эмулируется)
24+
// let ofArray2D (array: 'a[,]) : GraphblasEvaluation<Matrix<'a>> =
25+
// failwith "Not Implemented yet""
2426

2527
let init (rowCount: int) (columnCount: int) (initializer: int -> int -> 'a) : GraphblasEvaluation<Matrix<'a>> =
2628
failwith "Not Implemented yet"
2729

2830
let create (rowCount: int) (columnCount: int) (value: 'a) : GraphblasEvaluation<Matrix<'a>> =
2931
failwith "Not Implemented yet"
3032

31-
let zeroCreate (rowCount: int) (columnCount: int) : GraphblasEvaluation<Matrix<'a>> =
32-
failwith "Not Implemented yet"
33-
34-
let fromFile (pathToMatrix: string) : GraphblasEvaluation<Matrix<'a>> =
33+
let zeroCreate<'a when 'a : struct> (rowCount: int) (columnCount: int) : GraphblasEvaluation<Matrix<'a>> =
3534
failwith "Not Implemented yet"
3635

3736
(*
@@ -146,16 +145,7 @@ module Matrix =
146145
let mxm (semiring: ISemiring<'a>) (leftMatrix: Matrix<'a>) (rightMatrix: Matrix<'a>) : GraphblasEvaluation<Matrix<'a>> = failwith "Not Implemented yet"
147146

148147
let mxv (semiring: ISemiring<'a>) (matrix: Matrix<'a>) (vector: Vector<'a>) : GraphblasEvaluation<Vector<'a>> =
149-
let operationResult =
150-
match matrix, vector with
151-
| MatrixCSR matrix, VectorBitmap vector ->
152-
opencl {
153-
let! result = CSRMatrix.Mxv.pcsr matrix vector None semiring
154-
return VectorBitmap result
155-
}
156-
| _ -> failwith "Not Implemented"
157-
158-
graphblas { return! EvalGB.fromCl operationResult }
148+
failwith "Not Implemented"
159149

160150
let eWiseAdd (monoid: IMonoid<'a>) (leftMatrix: Matrix<'a>) (rightMatrix: Matrix<'a>) : GraphblasEvaluation<Matrix<'a>> =
161151
let operationResult =

src/GraphBLAS-sharp/Methods/Vector.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Vector =
99
constructors
1010
*)
1111

12-
let build (size: int) (indices: int[]) (values: int[]) : GraphblasEvaluation<Vector<'a>> =
12+
let build (size: int) (indices: int[]) (values: 'a[]) : GraphblasEvaluation<Vector<'a>> =
1313
failwith "Not Implemented yet"
1414

1515
let ofTuples (size: int) (tuples: VectorTuples<'a>) : GraphblasEvaluation<Vector<'a>> =
@@ -18,16 +18,17 @@ module Vector =
1818
let ofList (size: int) (elements: (int * 'a) list) : GraphblasEvaluation<Vector<'a>> =
1919
failwith "Not Implemented yet"
2020

21-
let ofArray (array: 'a[]) : GraphblasEvaluation<Vector<'a>> =
22-
failwith "Not Implemented yet"
21+
// можно оставить, но с условием, что будет создаваться full vector
22+
// let ofArray (array: 'a[]) : GraphblasEvaluation<Vector<'a>> =
23+
// failwith "Not Implemented yet"
2324

2425
let init (size: int) (initializer: int -> 'a) : GraphblasEvaluation<Vector<'a>> =
2526
failwith "Not Implemented yet"
2627

2728
let create (size: int) (value: 'a) : GraphblasEvaluation<Vector<'a>> =
2829
failwith "Not Implemented yet"
2930

30-
let zeroCreate (size: int) : GraphblasEvaluation<Vector<'a>> =
31+
let zeroCreate<'a when 'a : struct> (size: int) : GraphblasEvaluation<Vector<'a>> =
3132
failwith "Not Implemented yet"
3233

3334
(*
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
namespace GraphBLAS.FSharp
22

3-
type Scalar<'a> = Scalar of 'a
3+
type Scalar<'a when 'a : struct> = Scalar of 'a

src/GraphBLAS-sharp/Objects/Vector.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ namespace GraphBLAS.FSharp
22

33
type VectorType =
44
| COO
5-
| Bitmap
5+
// | Bitmap
66

77
type Vector<'a when 'a : struct> =
88
| VectorCOO of COOVector<'a>
9-
| VectorBitmap of BitmapVector<'a>
9+
// | VectorBitmap of BitmapVector<'a>
1010

1111
and COOVector<'a> =
1212
{
@@ -24,11 +24,11 @@ and COOVector<'a> =
2424
]
2525
|> String.concat ""
2626

27-
and BitmapVector<'a> =
28-
{
29-
Bitmap: bool[]
30-
Values: 'a[]
31-
}
27+
// and BitmapVector<'a> =
28+
// {
29+
// Bitmap: bool[]
30+
// Values: 'a[]
31+
// }
3232

3333
type VectorTuples<'a> =
3434
{

src/GraphBLAS-sharp/Predefined/Monoids/Add.fs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,3 @@ module Add =
4444
AssociativeOp = ClosedBinaryOp <@ (+) @>
4545
Identity = 0us
4646
}
47-
48-
let monoidicFloat: Monoid<MonoidicType<float>> =
49-
{
50-
AssociativeOp =
51-
<@
52-
fun x y ->
53-
match x, y with
54-
| Just x, Just y ->
55-
let result = x + y
56-
if abs result < 1e-16 then Zero else Just result
57-
| Just x, Zero -> Just x
58-
| Zero, Just y -> Just y
59-
| Zero, Zero -> Zero
60-
@> |> ClosedBinaryOp
61-
62-
Identity = Zero
63-
}

0 commit comments

Comments
 (0)