Skip to content

Commit b9d202f

Browse files
committed
refactor: common; docs for matrix
1 parent 04efa1e commit b9d202f

20 files changed

Lines changed: 260 additions & 83 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ open GraphBLAS.FSharp.Backend.Algorithms
55

66
[<RequireQualifiedAccess>]
77
module Algorithms =
8-
let singleSourceBFS = BFS.singleSource
8+
module BFS =
9+
let singleSource = BFS.singleSource

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

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,56 @@ open Microsoft.FSharp.Quotations
55
open GraphBLAS.FSharp.Backend.Common
66

77
module Common =
8-
module Bitonic =
9-
/// <summary>
10-
/// Sorts in-place input array of values by their 2d indices,
11-
/// which are stored in two given arrays of keys: rows and columns.
12-
/// When comparing, it first looks at rows, then columns.
13-
/// </summary>
14-
/// <example>
15-
/// <code>
16-
/// let rows = [| 0; 0; 3; 2; 1; 0; 5 |]
17-
/// let columns = [| 0; 2; 1; 2; 0; 3; 5; |]
18-
/// let values = [| 1.9; 2.8; 3.7; 4.6; 5.5; 6.4; 7.3; |]
19-
/// sortKeyValuesInplace clContext 32 processor rows columns values
20-
/// ...
21-
/// > val rows = [| 0; 0; 0; 1; 2; 3; 5 |]
22-
/// > let columns = [| 0; 2; 3; 0; 2; 1; 5; |]
23-
/// > val values = [| 1.9; 2.8; 6.4; 5.5; 4.6; 3.7; 7.3 |]
24-
/// </code>
25-
/// </example>
26-
let sortKeyValuesInplace<'n, 'a when 'n: comparison> =
27-
Sort.Bitonic.sortKeyValuesInplace<'n, 'a>
8+
module Sort =
9+
module Bitonic =
10+
/// <summary>
11+
/// Sorts in-place input array of values by their 2d indices,
12+
/// which are stored in two given arrays of keys: rows and columns.
13+
/// When comparing, it first looks at rows, then columns.
14+
/// </summary>
15+
/// <example>
16+
/// <code>
17+
/// let rows = [| 0; 0; 3; 2; 1; 0; 5 |]
18+
/// let columns = [| 0; 2; 1; 2; 0; 3; 5; |]
19+
/// let values = [| 1.9; 2.8; 3.7; 4.6; 5.5; 6.4; 7.3; |]
20+
/// sortKeyValuesInplace clContext 32 processor rows columns values
21+
/// ...
22+
/// > val rows = [| 0; 0; 0; 1; 2; 3; 5 |]
23+
/// > let columns = [| 0; 2; 3; 0; 2; 1; 5; |]
24+
/// > val values = [| 1.9; 2.8; 6.4; 5.5; 4.6; 3.7; 7.3 |]
25+
/// </code>
26+
/// </example>
27+
let sortKeyValuesInplace<'n, 'a when 'n: comparison> =
28+
Sort.Bitonic.sortKeyValuesInplace<'n, 'a>
2829

29-
module Radix =
30-
/// <summary>
31-
/// Sorts stable input array of values by given integer keys.
32-
/// </summary>
33-
/// <example>
34-
/// <code>
35-
/// let keys = [| 0; 4; 3; 1; 2; 6; 5 |]
36-
/// let values = [| 1.9; 2.8; 3.7; 4.6; 5.5; 6.4; 7.3; |]
37-
/// runByKeysStandard clContext 32 processor keys values
38-
/// ...
39-
/// > val keys = [| 0; 1; 2; 3; 4; 5; 6 |]
40-
/// > val values = [| 1.9; 4.6; 5.5; 3.7; 2.8; 7.3; 6.4 |]
41-
/// </code>
42-
/// </example>
43-
let runByKeysStandard = Sort.Radix.runByKeysStandard
30+
module Radix =
31+
/// <summary>
32+
/// Sorts stable input array of values by given integer keys.
33+
/// </summary>
34+
/// <example>
35+
/// <code>
36+
/// let keys = [| 0; 4; 3; 1; 2; 6; 5 |]
37+
/// let values = [| 1.9; 2.8; 3.7; 4.6; 5.5; 6.4; 7.3; |]
38+
/// runByKeysStandard clContext 32 processor keys values
39+
/// ...
40+
/// > val keys = [| 0; 1; 2; 3; 4; 5; 6 |]
41+
/// > val values = [| 1.9; 4.6; 5.5; 3.7; 2.8; 7.3; 6.4 |]
42+
/// </code>
43+
/// </example>
44+
let runByKeysStandard = Sort.Radix.runByKeysStandard
4445

45-
/// <summary>
46-
/// Sorts stable input array of integer keys.
47-
/// </summary>
48-
/// <example>
49-
/// <code>
50-
/// let keys = [| 0; 4; 3; 1; 2; 6; 5 |]
51-
/// standardRunKeysOnly clContext 32 processor keys
52-
/// ...
53-
/// > val keys = [| 0; 1; 2; 3; 4; 5; 6 |]
54-
/// </code>
55-
/// </example>
56-
let standardRunKeysOnly = Sort.Radix.standardRunKeysOnly
46+
/// <summary>
47+
/// Sorts stable input array of integer keys.
48+
/// </summary>
49+
/// <example>
50+
/// <code>
51+
/// let keys = [| 0; 4; 3; 1; 2; 6; 5 |]
52+
/// standardRunKeysOnly clContext 32 processor keys
53+
/// ...
54+
/// > val keys = [| 0; 1; 2; 3; 4; 5; 6 |]
55+
/// </code>
56+
/// </example>
57+
let standardRunKeysOnly = Sort.Radix.standardRunKeysOnly
5758

5859
module Gather =
5960
/// <summary>
@@ -212,28 +213,28 @@ module Common =
212213
/// > val sum = [| 4 |]
213214
/// </code>
214215
/// </example>
216+
/// <param name="plus">Associative binary operation.</param>
215217
/// <param name="clContext">ClContext.</param>
216218
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
217-
/// <param name="plus">Associative binary operation.</param>
218219
/// <param name="zero">Zero element for binary operation.</param>
219220
let runIncludeInPlace plus = PrefixSum.runIncludeInPlace plus
220221

221222
/// <summary>
222223
/// Exclude in-place prefix sum. Array is scanned starting from the end.
223224
/// </summary>
225+
/// <param name="plus">Associative binary operation.</param>
224226
/// <param name="clContext">ClContext.</param>
225227
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
226-
/// <param name="plus">Associative binary operation.</param>
227228
/// <param name="zero">Zero element for binary operation.</param>
228229
let runBackwardsExcludeInPlace plus =
229230
PrefixSum.runBackwardsExcludeInPlace plus
230231

231232
/// <summary>
232233
/// Include in-place prefix sum. Array is scanned starting from the end.
233234
/// </summary>
235+
/// <param name="plus">Associative binary operation.</param>
234236
/// <param name="clContext">ClContext.</param>
235237
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
236-
/// <param name="plus">Associative binary operation.</param>
237238
/// <param name="zero">Zero element for binary operation.</param>
238239
let runBackwardsIncludeInPlace plus =
239240
PrefixSum.runBackwardsIncludeInPlace plus

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@ namespace GraphBLAS.FSharp.Backend.Common
22

33
open Brahma.FSharp
44

5-
module internal Gather =
5+
module Gather =
6+
/// <summary>
7+
/// Fills the given output array using the given value array and a function. The function maps old position
8+
/// of each element of the value array to its position in the output array.
9+
/// </summary>
10+
/// <remarks>
11+
/// If index is out of bounds, the value will be ignored.
12+
/// </remarks>
13+
/// <example>
14+
/// <code>
15+
/// let positions = [| 1; 0; 2; 6; 4; 3; 5 |]
16+
/// let values = [| 1.9; 2.8; 3.7; 4.6; 5.5; 6.4; 7.3; |]
17+
/// run clContext 32 processor positions values result
18+
/// ...
19+
/// > val result = [| 2.8; 1.9; 3.7; 7.3; 5.5; 4.6; 6.4 |]
20+
/// </code>
21+
/// </example>
622
let runInit positionMap (clContext: ClContext) workGroupSize =
723

824
let gather =
@@ -29,6 +45,22 @@ module internal Gather =
2945

3046
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
3147

48+
/// <summary>
49+
/// Fills the given output array using the given value and position arrays. Array of positions indicates
50+
/// which element from the value array should be in each position of the output array.
51+
/// </summary>
52+
/// <remarks>
53+
/// If index is out of bounds, the value will be ignored.
54+
/// </remarks>
55+
/// <example>
56+
/// <code>
57+
/// let positions = [| 1; 0; 2; 6; 4; 3; 5 |]
58+
/// let values = [| 1.9; 2.8; 3.7; 4.6; 5.5; 6.4; 7.3; |]
59+
/// run clContext 32 processor positions values result
60+
/// ...
61+
/// > val result = [| 2.8; 1.9; 3.7; 7.3; 5.5; 4.6; 6.4 |]
62+
/// </code>
63+
/// </example>
3264
let run (clContext: ClContext) workGroupSize =
3365

3466
let gather =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open GraphBLAS.FSharp.Backend.Quotes
66
open GraphBLAS.FSharp.Objects.ArraysExtensions
77
open GraphBLAS.FSharp.Objects.ClCellExtensions
88

9-
module internal PrefixSum =
9+
module PrefixSum =
1010
let private update (opAdd: Expr<'a -> 'a -> 'a>) (clContext: ClContext) workGroupSize =
1111

1212
let update =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace GraphBLAS.FSharp.Backend.Common
33
open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend.Quotes
55

6-
module internal Scatter =
6+
module Scatter =
77
let private general<'a> predicate (clContext: ClContext) workGroupSize =
88

99
let run =

src/GraphBLAS-sharp.Backend/Common/Sort/Bitonic.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace GraphBLAS.FSharp.Backend.Common.Sort
33
open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend.Common
55

6-
module internal Bitonic =
6+
module Bitonic =
77
let private localBegin (clContext: ClContext) workGroupSize =
88

99
let processedSize = workGroupSize * 2

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open GraphBLAS.FSharp.Objects.ClContextExtensions
88
open GraphBLAS.FSharp.Objects.ArraysExtensions
99
open GraphBLAS.FSharp.Objects.ClCellExtensions
1010

11-
module internal Reduce =
11+
module Reduce =
1212
/// <summary>
1313
/// Generalized reduction pattern.
1414
/// </summary>

src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<Compile Include="Quotes/PreparePositions.fs" />
2828
<Compile Include="Quotes/Predicates.fs" />
2929
<Compile Include="Quotes/Map.fs" />
30-
3130
<Compile Include="Quotes/Search.fs" />
31+
3232
<Compile Include="Common/Scatter.fs" />
3333
<Compile Include="Common/Utils.fs" />
3434
<Compile Include="Common/PrefixSum.fs" />

src/GraphBLAS-sharp.Backend/Matrix/COO/Matrix.fs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,40 @@ open GraphBLAS.FSharp.Objects.ClCellExtensions
1010
open GraphBLAS.FSharp.Objects.ArraysExtensions
1111

1212
module Matrix =
13+
/// <summary>
14+
/// Builds a new COO matrix whose elements are the results of applying the given function
15+
/// to each of the elements of the matrix.
16+
/// </summary>
17+
/// <param name="op">
18+
/// A function to transform values of the input matrix.
19+
/// Operand and result types should be optional to distinguish explicit and implicit zeroes
20+
/// </param>
21+
/// <param name="clContext">OpenCL context.</param>
22+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
1323
let map = Map.run
1424

25+
/// <summary>
26+
/// Builds a new COO matrix whose values are the results of applying the given function
27+
/// to the corresponding pairs of values from the two matrices.
28+
/// </summary>
29+
/// <param name="op">
30+
/// A function to transform pairs of values from the input matrices.
31+
/// Operands and result types should be optional to distinguish explicit and implicit zeroes
32+
/// </param>
33+
/// <param name="clContext">OpenCL context.</param>
34+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
1535
let map2 = Map2.run
1636

37+
/// <summary>
38+
/// Builds a new COO matrix whose values are the results of applying the given function
39+
/// to the corresponding pairs of values from the two matrices.
40+
/// </summary>
41+
/// <param name="op">
42+
/// A function to transform pairs of values from the input matrices.
43+
/// Operation assumption: one of the operands should always be non-zero.
44+
/// </param>
45+
/// <param name="clContext">OpenCL context.</param>
46+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
1747
let rec map2AtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
1848
(clContext: ClContext)
1949
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
@@ -22,6 +52,11 @@ module Matrix =
2252

2353
Map2.AtLeastOne.run clContext (Convert.atLeastOneToOption opAdd) workGroupSize
2454

55+
/// <summary>
56+
/// Converts <c>COO</c> matrix format to <c>Tuple</c>.
57+
/// </summary>
58+
/// <param name="clContext">OpenCL context.</param>
59+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
2560
let getTuples (clContext: ClContext) workGroupSize =
2661

2762
let copy = ClArray.copy clContext workGroupSize
@@ -44,6 +79,11 @@ module Matrix =
4479
ColumnIndices = resultColumns
4580
Values = resultValues }
4681

82+
/// <summary>
83+
/// Converts rows of given COO matrix to rows in CSR format of the same matrix.
84+
/// </summary>
85+
/// <param name="clContext">OpenCL context.</param>
86+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
4787
let private compressRows (clContext: ClContext) workGroupSize =
4888

4989
let compressRows =
@@ -81,6 +121,12 @@ module Matrix =
81121

82122
rowPointers
83123

124+
/// <summary>
125+
/// Converts the given COO matrix to CSR format.
126+
/// Values and columns are copied and do not depend on input COO matrix anymore.
127+
/// </summary>
128+
/// <param name="clContext">OpenCL context.</param>
129+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
84130
let toCSR (clContext: ClContext) workGroupSize =
85131
let prepare = compressRows clContext workGroupSize
86132

@@ -105,6 +151,12 @@ module Matrix =
105151
Columns = cols
106152
Values = values }
107153

154+
/// <summary>
155+
/// Converts the given COO matrix to CSR format.
156+
/// Values and columns are NOT copied and still depend on the input COO matrix.
157+
/// </summary>
158+
/// <param name="clContext">OpenCL context.</param>
159+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
108160
let toCSRInPlace (clContext: ClContext) workGroupSize =
109161
let prepare = compressRows clContext workGroupSize
110162

@@ -121,10 +173,16 @@ module Matrix =
121173
Columns = matrix.Columns
122174
Values = matrix.Values }
123175

176+
/// <summary>
177+
/// Transposes the given matrix and returns result.
178+
/// The given matrix should neither be used afterwards nor be disposed.
179+
/// </summary>
180+
/// <param name="clContext">OpenCL context.</param>
181+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
124182
let transposeInPlace (clContext: ClContext) workGroupSize =
125183

126184
let sort =
127-
Common.Bitonic.sortKeyValuesInplace clContext workGroupSize
185+
Common.Sort.Bitonic.sortKeyValuesInplace clContext workGroupSize
128186

129187
fun (queue: MailboxProcessor<_>) (matrix: ClMatrix.COO<'a>) ->
130188
sort queue matrix.Columns matrix.Rows matrix.Values
@@ -136,6 +194,11 @@ module Matrix =
136194
Columns = matrix.Rows
137195
Values = matrix.Values }
138196

197+
/// <summary>
198+
/// Transposes the given matrix and returns result as a new matrix.
199+
/// </summary>
200+
///<param name="clContext">OpenCL context.</param>
201+
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
139202
let transpose (clContext: ClContext) workGroupSize =
140203

141204
let transposeInPlace = transposeInPlace clContext workGroupSize

src/GraphBLAS-sharp.Backend/Matrix/CSR/Map2.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ module internal Map2 =
205205
=
206206

207207
let merge =
208-
GraphBLAS.FSharp.Backend.Matrix.CSR.Merge.run clContext workGroupSize
208+
Merge.run clContext workGroupSize
209209

210210
let preparePositions =
211211
preparePositions opAdd clContext workGroupSize

0 commit comments

Comments
 (0)