Skip to content

Commit d517fff

Browse files
committed
Change assignment and extraction in interfaces
1 parent 205e218 commit d517fff

6 files changed

Lines changed: 109 additions & 88 deletions

File tree

src/GraphBLAS-sharp/Abstracts.fs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
namespace GraphBLAS.FSharp
22

3+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
4+
35
[<AbstractClass>]
46
type Matrix<'a when 'a : struct and 'a : equality>(nrow: int, ncol: int) =
57
abstract RowCount: int
68
abstract ColumnCount: int
79
default this.RowCount = nrow
810
default this.ColumnCount = ncol
911

10-
abstract Mask: Mask2D option
11-
abstract Complemented: Mask2D option
12-
13-
abstract Item: Mask2D option -> Matrix<'a> with get, set
14-
abstract Item: Mask1D option * int -> Vector<'a> with get, set
15-
abstract Item: int * Mask1D option -> Vector<'a> with get, set
16-
abstract Item: int * int -> Scalar<'a> with get, set
17-
abstract Fill: Mask2D option -> Scalar<'a> with set
18-
abstract Fill: Mask1D option * int -> Scalar<'a> with set
19-
abstract Fill: int * Mask1D option -> Scalar<'a> with set
12+
abstract Extract: Mask2D option -> Matrix<'a>
13+
abstract Extract: (Mask1D option * int) -> Vector<'a>
14+
abstract Extract: (int * Mask1D option) -> Vector<'a>
15+
abstract Extract: (int * int) -> Scalar<'a>
16+
// Размерности должны совпадать
17+
abstract Assign: Mask2D option * Matrix<'a> -> unit
18+
abstract Assign: (Mask1D option * int) * Vector<'a> -> unit
19+
abstract Assign: (int * Mask1D option) * Vector<'a> -> unit
20+
abstract Assign: (int * int) * Scalar<'a> -> unit
21+
abstract Assign: Mask2D option * Scalar<'a> -> unit
22+
abstract Assign: (Mask1D option * int) * Scalar<'a> -> unit
23+
abstract Assign: (int * Mask1D option) * Scalar<'a> -> unit
24+
// abstract Resize
25+
// abstract Dup
26+
// abstract Clear
27+
// abstract NNZ
28+
// abstract Tuples: OpenCLEvaluation<{| Rows: int[]; Columns: int[]; Values: 'a[] |}>
2029

2130
abstract Mxm: Matrix<'a> -> Mask2D option -> Semiring<'a> -> Matrix<'a>
2231
abstract Mxv: Vector<'a> -> Mask1D option -> Semiring<'a> -> Vector<'a>
2332
abstract EWiseAdd: Matrix<'a> -> Mask2D option -> Monoid<'a> -> Matrix<'a>
2433
abstract EWiseMult: Matrix<'a> -> Mask2D option -> Monoid<'a> -> Matrix<'a>
25-
abstract Apply: Mask1D option -> UnaryOp<'a, 'b> -> Matrix<'b>
34+
abstract Apply: Mask2D option -> UnaryOp<'a, 'b> -> Matrix<'b>
35+
abstract Prune: Mask2D option -> UnaryOp<'a, bool> -> Matrix<'a>
2636
abstract ReduceIn: Mask1D option -> Monoid<'a> -> Vector<'a>
2737
abstract ReduceOut: Mask1D option -> Monoid<'a> -> Vector<'a>
2838
abstract Reduce: Monoid<'a> -> Scalar<'a>
2939
abstract T: Matrix<'a>
40+
// abstract Kronecker
41+
42+
abstract Mask: Mask2D option
43+
abstract Complemented: Mask2D option
3044

3145
static member inline (+) (x: Matrix<'a>, y: Matrix<'a>) = x.EWiseAdd y
3246
static member inline (*) (x: Matrix<'a>, y: Matrix<'a>) = x.EWiseMult y
@@ -37,22 +51,28 @@ and [<AbstractClass>] Vector<'a when 'a : struct and 'a : equality>(length: int)
3751
abstract Length: int
3852
default this.Length = length
3953

40-
abstract AsArray: 'a[]
4154
abstract Clear: unit -> unit
42-
43-
abstract Mask: Mask1D option
44-
abstract Complemented: Mask1D option
45-
46-
abstract Item: Mask1D option -> Vector<'a> with get, set
47-
abstract Item: int -> Scalar<'a> with get, set
48-
abstract Fill: Mask1D option -> Scalar<'a> with set
55+
abstract Extract: Mask1D option -> Vector<'a>
56+
abstract Extract: int -> Scalar<'a>
57+
abstract Assign: Mask1D option * Vector<'a> -> unit
58+
abstract Assign: int * Scalar<'a> -> unit
59+
abstract Assign: Mask1D option * Scalar<'a> -> unit
60+
// abstract Dup
61+
// abstract Resize
62+
// abstrct Clear
63+
// abstract NNZ
64+
// abstract Tuples: {| Indices: int[]; Values: 'a[] |}
4965

5066
abstract Vxm: Matrix<'a> -> Mask1D option -> Semiring<'a> -> Vector<'a>
5167
abstract EWiseAdd: Vector<'a> -> Mask1D option -> Monoid<'a> -> Vector<'a>
5268
abstract EWiseMult: Vector<'a> -> Mask1D option -> Monoid<'a> -> Vector<'a>
5369
abstract Apply: Mask1D option -> UnaryOp<'a, 'b> -> Vector<'b>
70+
abstract Prune: Mask1D option -> UnaryOp<'a, bool> -> Vector<'a>
5471
abstract Reduce: Monoid<'a> -> Scalar<'a>
5572

73+
abstract Mask: Mask1D option
74+
abstract Complemented: Mask1D option
75+
5676
static member inline (+) (x: Vector<'a>, y: Vector<'a>) = x.EWiseAdd y
5777
static member inline (*) (x: Vector<'a>, y: Vector<'a>) = x.EWiseMult y
5878
static member inline (@.) (x: Vector<'a>, y: Matrix<'a>) = x.Vxm y

src/GraphBLAS-sharp/Algorithms/BFS.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ module BFS =
1111
let levels = DenseVector(Array.zeroCreate vertexCount, IntegerMonoid.add)
1212

1313
let frontier = SparseVector(vertexCount, [source, true])
14-
1514
let mutable currentLevel = 1
15+
1616
while !> (frontier.Reduce BooleanMonoid.any) && currentLevel < vertexCount do
17-
levels.Fill(frontier.Mask) <- Scalar currentLevel
17+
levels.Assign(frontier.Mask, Scalar currentLevel)
1818
frontier.Clear()
19-
frontier.[levels.Complemented] <- (frontier @. matrix) levels.Complemented BooleanSemiring.anyAll
19+
frontier.Assign(levels.Complemented, (frontier @. matrix) levels.Complemented BooleanSemiring.anyAll)
2020
currentLevel <- currentLevel + 1
2121

2222
upcast levels

src/GraphBLAS-sharp/Algorithms/SSSP.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ module SSSP =
1010
let distance = SparseVector(vertexCount, [source, 0.])
1111

1212
for _ in 1 .. vertexCount - 1 do
13-
distance.[None] <- (distance @. matrix) None FloatSemiring.minAdd
13+
distance.Assign(None, (distance @. matrix) None FloatSemiring.minAdd)
1414

1515
upcast distance

src/GraphBLAS-sharp/Implementations.fs

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type CSRMatrix<'a when 'a : struct and 'a : equality>(csrTuples: CSRFormat<'a>)
2929
let rowCount = base.RowCount
3030
let columnCount = base.ColumnCount
3131

32-
let spMV (vector: Vector<'a>) (mask: Mask1D) (semiring: Semiring<'a>) : Vector<'a> =
32+
let spMV (vector: DenseVector<'a>) (mask: Mask1D) (semiring: Semiring<'a>) : Vector<'a> =
3333
let csrMatrixRowCount = rowCount
3434
let csrMatrixColumnCount = columnCount
3535
let vectorLength = vector.Length
@@ -67,7 +67,7 @@ type CSRMatrix<'a when 'a : struct and 'a : equality>(csrTuples: CSRFormat<'a>)
6767
csrTuples.Values
6868
csrTuples.Columns
6969
csrTuples.RowPointers
70-
vector.AsArray
70+
vector.Values
7171

7272
let eval = opencl {
7373
do! RunCommand command binder
@@ -76,81 +76,77 @@ type CSRMatrix<'a when 'a : struct and 'a : equality>(csrTuples: CSRFormat<'a>)
7676

7777
upcast DenseVector(oclContext.RunSync eval, semiring.PlusMonoid)
7878

79+
new(rows: int[], columns: int[], values: 'a[]) = CSRMatrix(CSRFormat.CreateEmpty())
80+
7981
member this.Values = csrTuples.Values
8082
member this.Columns = csrTuples.Columns
8183
member this.RowPointers = csrTuples.RowPointers
8284

83-
override this.Mask = failwith "Not implemented"
84-
override this.Complemented = failwith "Not implemented"
85-
86-
override this.Item
87-
with get (mask: Mask2D option) : Matrix<'a> = failwith "Not Implemented"
88-
and set (mask: Mask2D option) (value: Matrix<'a>) = failwith "Not Implemented"
89-
override this.Item
90-
with get (vectorMask: Mask1D option, colIdx: int) : Vector<'a> = failwith "Not Implemented"
91-
and set (vectorMask: Mask1D option, colIdx: int) (value: Vector<'a>) = failwith "Not Implemented"
92-
override this.Item
93-
with get (rowIdx: int, vectorMask: Mask1D option) : Vector<'a> = failwith "Not Implemented"
94-
and set (rowIdx: int, vectorMask: Mask1D option) (value: Vector<'a>) = failwith "Not Implemented"
95-
override this.Item
96-
with get (rowIdx: int, colIdx: int) : Scalar<'a> = failwith "Not Implemented"
97-
and set (rowIdx: int, colIdx: int) (value: Scalar<'a>) = failwith "Not Implemented"
98-
override this.Fill
99-
with set (mask: Mask2D option) (value: Scalar<'a>) = failwith "Not Implemented"
100-
override this.Fill
101-
with set (vectorMask: Mask1D option, colIdx: int) (value: Scalar<'a>) = failwith "Not Implemented"
102-
override this.Fill
103-
with set (rowIdx: int, vectorMask: Mask1D option) (value: Scalar<'a>) = failwith "Not Implemented"
85+
override this.Extract (mask: Mask2D option) : Matrix<'a> = failwith "Not Implemented"
86+
override this.Extract (colMask: Mask1D option * int) : Vector<'a> = failwith "Not Implemented"
87+
override this.Extract (rowMask: int * Mask1D option) : Vector<'a> = failwith "Not Implemented"
88+
override this.Extract (idx: int * int) : Scalar<'a> = failwith "Not Implemented"
89+
override this.Assign (mask: Mask2D option, value: Matrix<'a>) : unit = failwith "Not Implemented"
90+
override this.Assign (colMask: Mask1D option * int, value: Vector<'a>) : unit = failwith "Not Implemented"
91+
override this.Assign (rowMask: int * Mask1D option, value: Vector<'a>) : unit = failwith "Not Implemented"
92+
override this.Assign (idx: int * int, value: Scalar<'a>) : unit = failwith "Not Implemented"
93+
override this.Assign (mask: Mask2D option, value: Scalar<'a>) : unit = failwith "Not Implemented"
94+
override this.Assign (colMask: Mask1D option * int, value: Scalar<'a>) : unit = failwith "Not Implemented"
95+
override this.Assign (rowMask: int * Mask1D option, value: Scalar<'a>) : unit = failwith "Not Implemented"
10496

10597
override this.Mxm a b c = failwith "Not Implemented"
10698
override this.Mxv a b c = failwith "Not Implemented"
10799
override this.EWiseAdd a b c = failwith "Not Implemented"
108100
override this.EWiseMult a b c = failwith "Not Implemented"
109101
override this.Apply a b = failwith "Not Implemented"
102+
override this.Prune a b = failwith "Not Implemented"
110103
override this.ReduceIn a b = failwith "Not Implemented"
111104
override this.ReduceOut a b = failwith "Not Implemented"
112105
override this.Reduce a = failwith "Not Implemented"
113106
override this.T = failwith "Not Implemented"
114107

108+
override this.Mask = failwith "Not implemented"
109+
override this.Complemented = failwith "Not implemented"
110+
115111
and SparseVector<'a when 'a : struct and 'a : equality>(size: int, listOfNonzeroes: (int * 'a) list) =
116112
inherit Vector<'a>(size)
117113

118-
member this.AsList: (int * 'a) list = listOfNonzeroes
119-
120-
override this.AsArray = failwith "Not Implemented"
121114
override this.Clear() = failwith "Not Implemented"
122-
123-
override this.Mask = failwith "Not implemented"
124-
override this.Complemented = failwith "Not implemented"
125-
126-
override this.Item
127-
with get (mask: Mask1D option) : Vector<'a> = failwith "Not Implemented"
128-
and set (mask: Mask1D option) (value: Vector<'a>) = failwith "Not Implemented"
129-
override this.Item
130-
with get (idx: int) : Scalar<'a> = failwith "Not Implemented"
131-
and set (idx: int) (value: Scalar<'a>) = failwith "Not Implemented"
132-
override this.Fill
133-
with set (mask: Mask1D option) (value: Scalar<'a>) = failwith "Not Implemented"
115+
override this.Extract (mask: Mask1D option) : Vector<'a> = failwith "Not Implemented"
116+
override this.Extract (idx: int) : Scalar<'a> = failwith "Not Implemented"
117+
override this.Assign(mask: Mask1D option, vector: Vector<'a>) : unit = failwith "Not Implemented"
118+
override this.Assign(idx: int, Scalar (value: 'a)) : unit = failwith "Not Implemented"
119+
override this.Assign(mask: Mask1D option, Scalar (value: 'a)) : unit = failwith "Not Implemented"
134120

135121
override this.Vxm (matrix: Matrix<'a>) (mask: Mask1D option) (semiring: Semiring<'a>) : Vector<'a> = failwith "Not Implemented"
136122
override this.EWiseAdd a b c = failwith "Not Implemented"
137123
override this.EWiseMult a b c = failwith "Not Implemented"
138124
override this.Apply a b = failwith "Not Implemented"
125+
override this.Prune a b = failwith "Not Implemented"
139126
override this.Reduce (monoid: Monoid<'a>) = failwith "Not Implemented"
140127

128+
override this.Mask = failwith "Not implemented"
129+
override this.Complemented = failwith "Not implemented"
130+
141131
and DenseVector<'a when 'a : struct and 'a : equality>(vector: 'a[], monoid: Monoid<'a>) =
142132
inherit Vector<'a>(vector.Length)
143133

144-
// Not Implemented
145-
new(monoid: Monoid<'a>) = DenseVector(Array.zeroCreate<'a> 0, monoid)
146-
// Not Implemented
147-
new(listOfIndices: int list, monoid: Monoid<'a>) = DenseVector(Array.zeroCreate<'a> 0, monoid)
148-
149134
member this.Monoid = monoid
150-
member this.Values = vector
135+
member this.Values: 'a[] = vector
151136

152-
override this.AsArray = failwith "Not Implemented"
153137
override this.Clear() = failwith "Not Implemented"
138+
override this.Extract (mask: Mask1D option) : Vector<'a> = failwith "Not Implemented"
139+
override this.Extract (idx: int) : Scalar<'a> = failwith "Not Implemented"
140+
override this.Assign(mask: Mask1D option, vector: Vector<'a>) : unit = failwith "Not Implemented"
141+
override this.Assign(idx: int, Scalar (value: 'a)) : unit = failwith "Not Implemented"
142+
override this.Assign(mask: Mask1D option, Scalar (value: 'a)) : unit = failwith "Not Implemented"
143+
144+
override this.Vxm a b c = failwith "Not Implemented"
145+
override this.EWiseAdd a b c = failwith "Not Implemented"
146+
override this.EWiseMult a b c = failwith "Not Implemented"
147+
override this.Apply a b = failwith "Not Implemented"
148+
override this.Prune a b = failwith "Not Implemented"
149+
override this.Reduce (monoid: Monoid<'a>) = failwith "Not Implemented"
154150

155151
override this.Mask =
156152
let indices =
@@ -163,18 +159,3 @@ and DenseVector<'a when 'a : struct and 'a : equality>(vector: 'a[], monoid: Mon
163159
[| for i in 0 .. this.Length - 1 do
164160
if this.Values.[i] <> this.Monoid.Zero then yield i |]
165161
Some <| Mask1D(indices, this.Length, true)
166-
167-
override this.Item
168-
with get (mask: Mask1D option) : Vector<'a> = failwith "Not Implemented"
169-
and set (mask: Mask1D option) (value: Vector<'a>) = failwith "Not Implemented"
170-
override this.Item
171-
with get (idx: int) : Scalar<'a> = failwith "Not Implemented"
172-
and set (idx: int) (value: Scalar<'a>) = failwith "Not Implemented"
173-
override this.Fill
174-
with set (mask: Mask1D option) (value: Scalar<'a>) = failwith "Not Implemented"
175-
176-
override this.Vxm a b c = failwith "Not Implemented"
177-
override this.EWiseAdd a b c = failwith "Not Implemented"
178-
override this.EWiseMult a b c = failwith "Not Implemented"
179-
override this.Apply a b = failwith "Not Implemented"
180-
override this.Reduce (monoid: Monoid<'a>) = failwith "Not Implemented"

src/GraphBLAS-sharp/Matrix.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
namespace GraphBLAS.FSharp
22

33
module MatrixBackend =
4-
// можно убрать перегрузку и добавить возможность partitial application
54
type Matrix<'a when 'a : struct and 'a : equality> with
65
static member Build(denseMatrix: 'T[,], zero: 'T, matrixType: MatrixBackendFormat) : Matrix<'T> =
76
failwith "Not Implemented"
7+
8+
static member Build(rows: int[], columns: int[], values: 'T[], matrixType: MatrixBackendFormat) : Matrix<'T> =
9+
match matrixType with
10+
| CSR -> upcast CSRMatrix(rows, columns, values)
11+
| _ -> failwith "Not Implemented"
12+
813
static member Build(pathToMatrix: string, matrixType: MatrixBackendFormat) : Matrix<'T> =
914
failwith "Not Implemented"
15+
1016
static member Build(rowCount: int, columnCount: int, initializer: int -> int -> 'T, matrixType: MatrixBackendFormat) : Matrix<'T> =
1117
failwith "Not Implemented"
18+
1219
static member ZeroCreate(rowCount: int, columnCount: int, matrixType: MatrixBackendFormat) : Matrix<'T> =
1320
failwith "Not Implemented"
1421

@@ -18,10 +25,16 @@ module MatrixExtensions =
1825
type Matrix<'a when 'a : struct and 'a : equality> with
1926
static member Build(denseMatrix: 'T[,], zero: 'T) : Matrix<'T> =
2027
Matrix.Build(denseMatrix, zero, matrixBackendFormat)
28+
29+
static member Build(rows: int[], columns: int[], values: 'T[]) : Matrix<'T> =
30+
Matrix.Build(rows, columns, values)
31+
2132
static member Build(pathToMatrix: string) : Matrix<'T> =
2233
Matrix.Build(pathToMatrix, matrixBackendFormat)
34+
2335
static member Build(rowCount: int, columnCount: int, initializer: int -> int -> 'T) : Matrix<'T> =
2436
Matrix.Build(rowCount, columnCount, initializer, matrixBackendFormat)
37+
2538
static member ZeroCreate(rowCount: int, columnCount: int) : Matrix<'T> =
2639
Matrix.ZeroCreate(rowCount, columnCount)
2740

src/GraphBLAS-sharp/Vector.fs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ module VectorExtensions =
55
type Vector<'a when 'a : struct and 'a : equality> with
66
static member Sparse(denseVector: 'T[], zero: 'T) : Vector<'T> =
77
failwith "Not Implemented"
8+
89
static member Sparse(length: int, initializer: int -> 'T) : Vector<'T> =
10+
// upcast SparseVector(length, List.init)
911
failwith "Not Implemented"
12+
1013
static member ZeroSparse(length: int) : Vector<'T> =
11-
failwith "Not Implemented"
14+
upcast SparseVector(length, List.empty)
15+
1216

1317
static member Dense(denseVector: 'T[], monoid: Monoid<'T>) : Vector<'T> =
14-
failwith "Not Implemented"
18+
upcast DenseVector(denseVector, monoid)
19+
1520
static member Dense(length: int, initializer: int -> 'T, monoid: Monoid<'T>) : Vector<'T> =
16-
failwith "Not Implemented"
21+
upcast DenseVector(Array.init length initializer, monoid)
22+
1723
static member ZeroDense(length: int, monoid: Monoid<'T>) : Vector<'T> =
18-
failwith "Not Implemented"
24+
upcast DenseVector(Array.create length monoid.Zero, monoid)
25+
1926

2027
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
2128
module Vector =

0 commit comments

Comments
 (0)