-
Notifications
You must be signed in to change notification settings - Fork 7
/
Matrix.fs
142 lines (105 loc) · 4.86 KB
/
Matrix.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
namespace GraphBLAS.FSharp.Backend.Matrix.CSR
open Brahma.FSharp
open GraphBLAS.FSharp.Backend.Common
open GraphBLAS.FSharp.Backend.Matrix
open GraphBLAS.FSharp.Backend.Quotes
open Microsoft.FSharp.Quotations
open GraphBLAS.FSharp.Backend.Objects
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
open GraphBLAS.FSharp.Backend.Objects.ClContext
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
open GraphBLAS.FSharp.Backend.Objects.ClCell
module Matrix =
let toCOO (clContext: ClContext) workGroupSize =
let prepare =
Common.expandRowPointers clContext workGroupSize
let copy = ClArray.copy clContext workGroupSize
let copyData = ClArray.copy clContext workGroupSize
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
let rows =
prepare processor allocationMode matrix.RowPointers matrix.Columns.Length matrix.RowCount
let cols =
copy processor allocationMode matrix.Columns
let values =
copyData processor allocationMode matrix.Values
{ Context = clContext
RowCount = matrix.RowCount
ColumnCount = matrix.ColumnCount
Rows = rows
Columns = cols
Values = values }
let toCOOInplace (clContext: ClContext) workGroupSize =
let prepare =
Common.expandRowPointers clContext workGroupSize
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
let rows =
prepare processor allocationMode matrix.RowPointers matrix.Columns.Length matrix.RowCount
processor.Post(Msg.CreateFreeMsg(matrix.RowPointers))
{ Context = clContext
RowCount = matrix.RowCount
ColumnCount = matrix.ColumnCount
Rows = rows
Columns = matrix.Columns
Values = matrix.Values }
let map = CSR.Map.run
let map2 = Map2.run
let map2AtLeastOneToCOO<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
(clContext: ClContext)
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
workGroupSize
=
Map2AtLeastOne.runToCOO clContext (Convert.atLeastOneToOption opAdd) workGroupSize
let map2AtLeastOne<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality>
(clContext: ClContext)
(opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>)
workGroupSize
=
Map2AtLeastOne.run clContext (Convert.atLeastOneToOption opAdd) workGroupSize
let transposeInplace (clContext: ClContext) workGroupSize =
let toCOOInplace = toCOOInplace clContext workGroupSize
let transposeInplace =
COO.Matrix.transposeInplace clContext workGroupSize
let toCSRInplace =
COO.Matrix.toCSRInplace clContext workGroupSize
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
toCOOInplace queue allocationMode matrix
|> transposeInplace queue
|> toCSRInplace queue allocationMode
let transpose (clContext: ClContext) workGroupSize =
let toCOO = toCOO clContext workGroupSize
let transposeInplace =
COO.Matrix.transposeInplace clContext workGroupSize
let toCSRInplace =
COO.Matrix.toCSRInplace clContext workGroupSize
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
toCOO queue allocationMode matrix
|> transposeInplace queue
|> toCSRInplace queue allocationMode
module SpGeMM =
let masked
(clContext: ClContext)
workGroupSize
(opAdd: Expr<'c -> 'c -> 'c option>)
(opMul: Expr<'a -> 'b -> 'c option>)
=
let run =
SpGeMM.Masked.run clContext workGroupSize opAdd opMul
fun (queue: MailboxProcessor<_>) (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSC<'b>) (mask: ClMatrix.COO<_>) ->
run queue matrixLeft matrixRight mask
let expand
(clContext: ClContext)
workGroupSize
(opAdd: Expr<'c -> 'c -> 'c option>)
(opMul: Expr<'a -> 'b -> 'c option>)
=
let run =
SpGeMM.Expand.run clContext workGroupSize opAdd opMul
fun (queue: MailboxProcessor<_>) allocationMode (leftMatrix: ClMatrix.CSR<'a>) (rightMatrix: ClMatrix.CSR<'b>) ->
let values, columns, rows =
run queue allocationMode leftMatrix rightMatrix
{ COO.Context = clContext
ColumnCount = rightMatrix.ColumnCount
RowCount = leftMatrix.RowCount
Values = values
Columns = columns
Rows = rows }