Skip to content

Commit 089e11e

Browse files
committed
add: Matrix.Rows.Convert
1 parent 92b186f commit 089e11e

21 files changed

Lines changed: 249 additions & 297 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksMxm.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type MxmBenchmarks<'elem when 'elem : struct>(
7575
member this.FunCSR2CSC =
7676
match funCSR2CSC with
7777
| None ->
78-
let x = Matrix.toCSCInplace this.OclContext this.WorkGroupSize
78+
let x = Matrix.toCSCInPlace this.OclContext this.WorkGroupSize
7979
funCSR2CSC <- Some x
8080
x
8181
| Some x -> x

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,10 @@ module ClArray =
635635
if count = 0 then ()
636636
else
637637
if firstPosition + count > targetArray.Length then
638-
failwith ""
638+
failwith "The array should fit completely"
639639

640-
if firstPosition < 0 then failwith ""
641-
if count < 0 then failwith "" // TODO()
640+
if firstPosition < 0 then failwith "The starting position cannot be less than zero"
641+
if count < 0 then failwith "The count cannot be less than zero"
642642

643643
let ndRange =
644644
Range1D.CreateValid(count, workGroupSize)

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module Matrix =
107107
Columns = cols
108108
Values = values }
109109

110-
let toCSRInplace (clContext: ClContext) workGroupSize =
110+
let toCSRInPlace (clContext: ClContext) workGroupSize =
111111
let prepare = compressRows clContext workGroupSize
112112

113113
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.COO<'a>) ->
@@ -123,7 +123,7 @@ module Matrix =
123123
Columns = matrix.Columns
124124
Values = matrix.Values }
125125

126-
let transposeInplace (clContext: ClContext) workGroupSize =
126+
let transposeInPlace (clContext: ClContext) workGroupSize =
127127

128128
let sort =
129129
Sort.Bitonic.sortKeyValuesInplace clContext workGroupSize
@@ -140,7 +140,7 @@ module Matrix =
140140

141141
let transpose (clContext: ClContext) workGroupSize =
142142

143-
let transposeInplace = transposeInplace clContext workGroupSize
143+
let transposeInPlace = transposeInPlace clContext workGroupSize
144144

145145
let copy = ClArray.copy clContext workGroupSize
146146

@@ -154,34 +154,4 @@ module Matrix =
154154
Rows = copy queue allocationMode matrix.Rows
155155
Columns = copy queue allocationMode matrix.Columns
156156
Values = copyData queue allocationMode matrix.Values }
157-
|> transposeInplace queue
158-
159-
let concat (clContext: ClContext) workGroupSize =
160-
161-
let concatValues = ClArray.concat clContext workGroupSize
162-
163-
let concatIndices = ClArray.concat clContext workGroupSize
164-
165-
fun (processor: MailboxProcessor<_>) allocationMode columnCount rowCount (matrices: ClMatrix.COO<'a> seq) ->
166-
167-
let resultValues =
168-
matrices
169-
|> Seq.map (fun matrix -> matrix.Values)
170-
|> concatValues processor allocationMode
171-
172-
let resultColumns =
173-
matrices
174-
|> Seq.map (fun matrix -> matrix.Columns)
175-
|> concatIndices processor allocationMode
176-
177-
let resultRows =
178-
matrices
179-
|> Seq.map (fun matrix -> matrix.Rows)
180-
|> concatIndices processor allocationMode
181-
182-
{ Context = clContext
183-
RowCount = rowCount
184-
ColumnCount = columnCount
185-
Rows = resultRows
186-
Columns = resultColumns
187-
Values = resultValues }
157+
|> transposeInPlace queue

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ module internal Map =
122122
let mapToCOO = runToCOO clContext opAdd workGroupSize
123123

124124
let toCSRInplace =
125-
Matrix.toCSRInplace clContext workGroupSize
125+
Matrix.toCSRInPlace clContext workGroupSize
126126

127127
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
128128
mapToCOO queue allocationMode matrix

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ module internal Map2 =
144144
let map2ToCOO = runToCOO clContext opAdd workGroupSize
145145

146146
let toCSRInplace =
147-
Matrix.toCSRInplace clContext workGroupSize
147+
Matrix.toCSRInPlace clContext workGroupSize
148148

149149
fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSR<'b>) ->
150150
map2ToCOO queue allocationMode matrixLeft matrixRight

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ module internal Map2AtLeastOne =
339339
let elementwiseToCOO = runToCOO clContext opAdd workGroupSize
340340

341341
let toCSRInplace =
342-
Matrix.toCSRInplace clContext workGroupSize
342+
Matrix.toCSRInPlace clContext workGroupSize
343343

344344
fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSR<'b>) ->
345345
elementwiseToCOO queue allocationMode matrixLeft matrixRight

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ module Matrix =
8282
let toCOOInPlace = toCOOInPlace clContext workGroupSize
8383

8484
let transposeInPlace =
85-
COO.Matrix.transposeInplace clContext workGroupSize
85+
COO.Matrix.transposeInPlace clContext workGroupSize
8686

8787
let toCSRInPlace =
88-
COO.Matrix.toCSRInplace clContext workGroupSize
88+
COO.Matrix.toCSRInPlace clContext workGroupSize
8989

9090
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
9191
toCOOInPlace queue allocationMode matrix
@@ -97,10 +97,10 @@ module Matrix =
9797
let toCOO = toCOO clContext workGroupSize
9898

9999
let transposeInPlace =
100-
COO.Matrix.transposeInplace clContext workGroupSize
100+
COO.Matrix.transposeInPlace clContext workGroupSize
101101

102102
let toCSRInPlace =
103-
COO.Matrix.toCSRInplace clContext workGroupSize
103+
COO.Matrix.toCSRInPlace clContext workGroupSize
104104

105105
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
106106
toCOO queue allocationMode matrix
@@ -148,3 +148,36 @@ module Matrix =
148148
runLazy processor allocationMode matrix
149149
|> Seq.map (fun lazyValue -> lazyValue.Value)
150150
|> Seq.toArray
151+
152+
let toRows (clContext: ClContext) workGroupSize =
153+
154+
let byRows = byRows clContext workGroupSize
155+
156+
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
157+
let rows = byRows processor allocationMode matrix
158+
159+
{ Context = clContext
160+
RowCount = matrix.RowCount
161+
ColumnCount = matrix.ColumnCount
162+
Rows = rows
163+
NNZ = matrix.NNZ }
164+
165+
let getRowsLength (clContext: ClContext) workGroupSize =
166+
167+
let pairwise = ClArray.pairwise clContext workGroupSize
168+
169+
let subtract =
170+
ClArray.map clContext workGroupSize Map.pairSubtraction
171+
172+
fun (processor: MailboxProcessor<_>) (matrix: ClMatrix.CSR<'b>) ->
173+
let pointerPairs =
174+
pairwise processor DeviceOnly matrix.RowPointers
175+
// since row pointers length in matrix always >= 2
176+
|> Option.defaultWith (fun () ->
177+
failwith "The state of the matrix is broken. The length of the rowPointers must be >= 2")
178+
179+
let rowsLength = subtract processor DeviceOnly pointerPairs
180+
181+
pointerPairs.Free processor
182+
183+
rowsLength

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

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module Matrix =
9494
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
9595
let toCSRInPlace (clContext: ClContext) workGroupSize =
9696
let toCSRInPlace =
97-
COO.Matrix.toCSRInplace clContext workGroupSize
97+
COO.Matrix.toCSRInPlace clContext workGroupSize
9898

9999
let transposeInPlace =
100100
CSR.Matrix.transposeInPlace clContext workGroupSize
@@ -121,11 +121,11 @@ module Matrix =
121121

122122
let copy = copy clContext workGroupSize
123123

124-
let transposeInplace =
125-
COO.Matrix.transposeInplace clContext workGroupSize
124+
let transposeInPlace =
125+
COO.Matrix.transposeInPlace clContext workGroupSize
126126

127-
let rowsToCOO =
128-
Rows.Matrix.toCOO clContext workGroupSize
127+
let rowsToCSR =
128+
Rows.Matrix.toCSR clContext workGroupSize
129129

130130
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
131131
match matrix with
@@ -134,10 +134,11 @@ module Matrix =
134134
| ClMatrix.CSC m ->
135135
m.ToCSR
136136
|> toCOO processor allocationMode
137-
|> transposeInplace processor
137+
|> transposeInPlace processor
138138
|> ClMatrix.COO
139139
| ClMatrix.Rows m ->
140-
rowsToCOO processor allocationMode m
140+
rowsToCSR processor allocationMode m
141+
|> toCOO processor allocationMode
141142
|> ClMatrix.COO
142143

143144
/// <summary>
@@ -151,7 +152,7 @@ module Matrix =
151152
CSR.Matrix.toCOOInPlace clContext workGroupSize
152153

153154
let transposeInPlace =
154-
COO.Matrix.transposeInplace clContext workGroupSize
155+
COO.Matrix.transposeInPlace clContext workGroupSize
155156

156157
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
157158
match matrix with
@@ -182,6 +183,8 @@ module Matrix =
182183
let transposeCOO =
183184
COO.Matrix.transpose clContext workGroupSize
184185

186+
let rowsToCSR = Rows.Matrix.toCSR clContext workGroupSize
187+
185188
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
186189
match matrix with
187190
| ClMatrix.CSC _ -> copy processor allocationMode matrix
@@ -193,37 +196,68 @@ module Matrix =
193196
|> COOtoCSR processor allocationMode)
194197
.ToCSC
195198
|> ClMatrix.CSC
196-
| _ -> failwith "Not yet implemented"
199+
| ClMatrix.Rows m ->
200+
rowsToCSR processor allocationMode m
201+
|> transposeCSR processor allocationMode
202+
|> fun m -> m.ToCSC
203+
|> ClMatrix.CSC
197204

198205
/// <summary>
199206
/// Returns the matrix, represented in CSC format, that is equal to the given one.
200207
/// The given matrix should neither be used afterwards nor be disposed.
201208
/// </summary>
202209
///<param name="clContext">OpenCL context.</param>
203210
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
204-
let toCSCInplace (clContext: ClContext) workGroupSize =
205-
let toCSRInplace =
206-
COO.Matrix.toCSRInplace clContext workGroupSize
211+
let toCSCInPlace (clContext: ClContext) workGroupSize =
212+
let toCSRInPlace =
213+
COO.Matrix.toCSRInPlace clContext workGroupSize
207214

208-
let transposeCSRInplace =
215+
let transposeCSRInPlace =
209216
CSR.Matrix.transposeInPlace clContext workGroupSize
210217

211-
let transposeCOOInplace =
212-
COO.Matrix.transposeInplace clContext workGroupSize
218+
let transposeCOOInPlace =
219+
COO.Matrix.transposeInPlace clContext workGroupSize
213220

214221
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
215222
match matrix with
216223
| ClMatrix.CSC _ -> matrix
217224
| ClMatrix.CSR m ->
218-
(transposeCSRInplace processor allocationMode m)
225+
(transposeCSRInPlace processor allocationMode m)
219226
.ToCSC
220227
|> ClMatrix.CSC
221228
| ClMatrix.COO m ->
222-
(transposeCOOInplace processor m
223-
|> toCSRInplace processor allocationMode)
229+
(transposeCOOInPlace processor m
230+
|> toCSRInPlace processor allocationMode)
224231
.ToCSC
225232
|> ClMatrix.CSC
226-
| _ -> failwith "not yet supported"
233+
| _ -> failwith "Not yet implemented"
234+
235+
let toRows (clContext: ClContext) workGroupSize =
236+
237+
let copy = copy clContext workGroupSize
238+
239+
let COOToCSR = COO.Matrix.toCSR clContext workGroupSize
240+
241+
let transposeCSR =
242+
CSR.Matrix.transposeInPlace clContext workGroupSize
243+
244+
let CSRToRows = CSR.Matrix.toRows clContext workGroupSize
245+
246+
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
247+
match matrix with
248+
| ClMatrix.CSC m ->
249+
m.ToCSR
250+
|> transposeCSR processor allocationMode
251+
|> CSRToRows processor allocationMode
252+
|> ClMatrix.Rows
253+
| ClMatrix.CSR m ->
254+
CSRToRows processor allocationMode m
255+
|> ClMatrix.Rows
256+
| ClMatrix.COO m ->
257+
COOToCSR processor allocationMode m
258+
|> CSRToRows processor allocationMode
259+
|> ClMatrix.Rows
260+
| ClMatrix.Rows _ -> copy processor allocationMode matrix
227261

228262
let map (clContext: ClContext) (opAdd: Expr<'a option -> 'b option>) workGroupSize =
229263
let mapCOO =
@@ -241,7 +275,7 @@ module Matrix =
241275
|> ClMatrix.CSC
242276
| _ -> failwith "Not yet implemented"
243277

244-
let map2 (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize = // TODO()
278+
let map2 (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) workGroupSize =
245279
let map2COO =
246280
COO.Matrix.map2 clContext opAdd workGroupSize
247281

@@ -291,7 +325,7 @@ module Matrix =
291325
CSR.Matrix.map2AtLeastOneToCOO clContext opAdd workGroupSize
292326

293327
let transposeCOOInPlace =
294-
COO.Matrix.transposeInplace clContext workGroupSize
328+
COO.Matrix.transposeInPlace clContext workGroupSize
295329

296330
fun (processor: MailboxProcessor<_>) allocationMode matrix1 matrix2 ->
297331
match matrix1, matrix2 with
@@ -322,14 +356,14 @@ module Matrix =
322356
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
323357
let transposeInPlace (clContext: ClContext) workGroupSize =
324358
let COOTransposeInPlace =
325-
COO.Matrix.transposeInplace clContext workGroupSize
359+
COO.Matrix.transposeInPlace clContext workGroupSize
326360

327361
fun (processor: MailboxProcessor<_>) matrix ->
328362
match matrix with
329363
| ClMatrix.COO m -> COOTransposeInPlace processor m |> ClMatrix.COO
330364
| ClMatrix.CSR m -> ClMatrix.CSC m.ToCSC
331365
| ClMatrix.CSC m -> ClMatrix.CSR m.ToCSR
332-
| ClMatrix.Rows _ -> failwith "not yet supported"
366+
| ClMatrix.Rows _ -> failwith "Not yet implemented"
333367

334368
/// <summary>
335369
/// Transposes the given matrix and returns result as a new matrix.
@@ -344,7 +378,7 @@ module Matrix =
344378
///<param name="clContext">OpenCL context.</param>
345379
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
346380
let transpose (clContext: ClContext) workGroupSize =
347-
let COOtranspose =
381+
let COOTranspose =
348382
COO.Matrix.transpose clContext workGroupSize
349383

350384
let copy = ClArray.copy clContext workGroupSize
@@ -354,7 +388,7 @@ module Matrix =
354388
fun (processor: MailboxProcessor<_>) allocationMode matrix ->
355389
match matrix with
356390
| ClMatrix.COO m ->
357-
COOtranspose processor allocationMode m
391+
COOTranspose processor allocationMode m
358392
|> ClMatrix.COO
359393
| ClMatrix.CSR m ->
360394
{ Context = m.Context
@@ -372,7 +406,7 @@ module Matrix =
372406
Columns = copy processor allocationMode m.Rows
373407
Values = copyData processor allocationMode m.Values }
374408
|> ClMatrix.CSR
375-
| ClMatrix.Rows _ -> failwith "not yet supported"
409+
| ClMatrix.Rows _ -> failwith "Not yet implemented"
376410

377411
module SpGeMM =
378412
let masked

0 commit comments

Comments
 (0)