Skip to content

Commit a79542d

Browse files
committed
add: Vector.map
1 parent 0cfe112 commit a79542d

File tree

12 files changed

+327
-44
lines changed

12 files changed

+327
-44
lines changed

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksMxm.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type MxmBenchmarks<'elem when 'elem : struct>(
8383
member this.FunCSC2CSR =
8484
match funCSC2CSR with
8585
| None ->
86-
let x = Matrix.toCSRInplace this.OclContext this.WorkGroupSize
86+
let x = Matrix.toCSRInPlace this.OclContext this.WorkGroupSize
8787
funCSC2CSR <- Some x
8888
x
8989
| Some x -> x

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

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,35 +412,82 @@ module ClArray =
412412
|> Seq.map (fun lazyValue -> lazyValue.Value)
413413
|> Seq.toArray
414414

415-
let append<'a> (clContext: ClContext) workGroupSize =
415+
let assign<'a> (clContext: ClContext) workGroupSize =
416416

417-
let set =
418-
<@ fun (ndRange: Range1D) sourceArrayLength appendedArrayLength (inputArray: ClArray<'a>) (result: ClArray<'a>) ->
417+
let assign =
418+
<@ fun (ndRange: Range1D) startPosition appendedArrayLength (inputArray: ClArray<'a>) (result: ClArray<'a>) ->
419419

420420
let gid = ndRange.GlobalID0
421421

422-
let resultPosition = gid + sourceArrayLength
422+
let resultPosition = gid + startPosition
423423

424424
if gid < appendedArrayLength then
425425

426426
result.[resultPosition] <- inputArray.[gid] @>
427427

428-
let kernel = clContext.Compile set
428+
let kernel = clContext.Compile assign
429429

430-
fun (processor: MailboxProcessor<_>) allocationMode (sourceArray: ClArray<'a>) (appendedArray: ClArray<'a>) ->
431-
432-
let resultLength = sourceArray.Length + appendedArray.Length
433-
434-
let result =
435-
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
430+
fun (processor: MailboxProcessor<_>) allocationMode (targetArray: ClArray<'a>) startPosition (appendedArray: ClArray<'a>) ->
431+
if startPosition < 0 then failwith "The starting position cannot be less than zero"
432+
if startPosition + appendedArray.Length > targetArray.Length then
433+
failwith "The array should fit completely"
436434

437435
let ndRange =
438436
Range1D.CreateValid(appendedArray.Length, workGroupSize)
439437

440438
let kernel = kernel.GetKernel()
441439

442-
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange sourceArray.Length appendedArray.Length appendedArray result))
440+
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange appendedArray.Length appendedArray.Length appendedArray targetArray))
443441

444442
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
445443

444+
let concat (clContext: ClContext) workGroupSize =
445+
446+
let assign = assign clContext workGroupSize
447+
448+
fun (processor: MailboxProcessor<_>) allocationMode (sourceArrays: ClArray<'a> seq) ->
449+
450+
let resultLength =
451+
sourceArrays |> Seq.sumBy (fun array -> array.Length)
452+
453+
let result = clContext.CreateClArrayWithSpecificAllocationMode(DeviceOnly, resultLength)
454+
455+
let assign = assign processor allocationMode result
456+
457+
// write each array to result
458+
Seq.fold (fun previousLength array ->
459+
assign previousLength array
460+
previousLength + array.Length) 0 sourceArrays
461+
|> ignore
462+
446463
result
464+
465+
let fill (clContext: ClContext) workGroupSize =
466+
467+
let fill =
468+
<@ fun (ndRange: Range1D) firstPosition endPosition (value: ClCell<'a>) (targetArray: ClArray<'a>) ->
469+
470+
let gid = ndRange.GlobalID0
471+
let writePosition = gid + firstPosition
472+
473+
if writePosition < endPosition then
474+
475+
targetArray.[writePosition] <- value.Value @>
476+
477+
let kernel = clContext.Compile fill
478+
479+
fun (processor: MailboxProcessor<_>) value firstPosition count (targetArray: ClArray<'a>) ->
480+
if firstPosition + count > targetArray.Length then failwith ""
481+
if firstPosition < 0 then failwith ""
482+
if count <= 0 then failwith "" // TODO()
483+
484+
let ndRange =
485+
Range1D.CreateValid(count, workGroupSize)
486+
487+
let kernel = kernel.GetKernel()
488+
489+
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange firstPosition (firstPosition + count) value targetArray))
490+
491+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
492+
493+
()

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
<Compile Include="Common/Sort/Radix.fs" />
3535
<Compile Include="Common/Sort/Bitonic.fs" />
3636
<Compile Include="Common/Sum.fs" />
37-
<Compile Include="Vector/SparseVector/Common.fs" />
38-
<Compile Include="Vector/SparseVector/Map2.fs" />
39-
<Compile Include="Vector/SparseVector/Map2AtLeastOne.fs" />
40-
<Compile Include="Vector/SparseVector/SparseVector.fs" />
41-
<Compile Include="Vector/DenseVector/DenseVector.fs" />
37+
<Compile Include="Vector\Sparse\Common.fs" />
38+
<Compile Include="Vector\Sparse\Map2.fs" />
39+
<Compile Include="Vector\Sparse\Map2AtLeastOne.fs" />
40+
<Compile Include="Vector\Sparse\SparseVector.fs" />
41+
<Compile Include="Vector\Sparse\Map.fs" />
42+
<Compile Include="Vector\Dense\DenseVector.fs" />
4243
<Compile Include="Vector/Vector.fs" />
4344
<Compile Include="Vector/SpMV.fs" />
4445
<Compile Include="Matrix/Common.fs" />

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,33 @@ module Matrix =
155155
Columns = copy queue allocationMode matrix.Columns
156156
Values = copyData queue allocationMode matrix.Values }
157157
|> 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 }

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

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ module Matrix =
4545
Values = copyData processor allocationMode m.Values }
4646
| ClMatrix.Rows matrix ->
4747
matrix.Rows
48-
|> Array.map (function
49-
Some vector -> Some <| vectorCopy processor allocationMode vector
50-
| None -> None)
48+
|> Array.map (Option.bind <| (Some << (vectorCopy processor allocationMode)))
5149
|> fun rows ->
5250
{ Context = clContext
5351
RowCount = matrix.RowCount
@@ -69,6 +67,8 @@ module Matrix =
6967
let transpose =
7068
CSR.Matrix.transpose clContext workGroupSize
7169

70+
let rowsToCSR = Rows.Matrix.toCSR clContext workGroupSize
71+
7272
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
7373
match matrix with
7474
| ClMatrix.COO m -> toCSR processor allocationMode m |> ClMatrix.CSR
@@ -77,30 +77,36 @@ module Matrix =
7777
m.ToCSR
7878
|> transpose processor allocationMode
7979
|> ClMatrix.CSR
80+
| ClMatrix.Rows m ->
81+
rowsToCSR processor allocationMode m
82+
|> ClMatrix.CSR
8083

8184
/// <summary>
8285
/// Returns the matrix, represented in CSR format, that is equal to the given one.
8386
/// The given matrix should neither be used afterwards nor be disposed.
8487
/// </summary>
8588
///<param name="clContext">OpenCL context.</param>
8689
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
87-
let toCSRInplace (clContext: ClContext) workGroupSize =
88-
let toCSRInplace =
90+
let toCSRInPlace (clContext: ClContext) workGroupSize =
91+
let toCSRInPlace =
8992
COO.Matrix.toCSRInplace clContext workGroupSize
9093

91-
let transposeInplace =
94+
let transposeInPlace =
9295
CSR.Matrix.transposeInplace clContext workGroupSize
9396

97+
let rowsToCSR = Rows.Matrix.toCSR clContext workGroupSize
98+
9499
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
95100
match matrix with
96101
| ClMatrix.COO m ->
97-
toCSRInplace processor allocationMode m
102+
toCSRInPlace processor allocationMode m
98103
|> ClMatrix.CSR
99104
| ClMatrix.CSR _ -> matrix
100105
| ClMatrix.CSC m ->
101106
m.ToCSR
102-
|> transposeInplace processor allocationMode
107+
|> transposeInPlace processor allocationMode
103108
|> ClMatrix.CSR
109+
| _ -> failwith "not yet supported"
104110

105111
/// <summary>
106112
/// Creates a new matrix, represented in COO format, that is equal to the given one.
@@ -115,6 +121,9 @@ module Matrix =
115121
let transposeInplace =
116122
COO.Matrix.transposeInplace clContext workGroupSize
117123

124+
let rowsToCOO =
125+
Rows.Matrix.toCOO clContext workGroupSize
126+
118127
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
119128
match matrix with
120129
| ClMatrix.COO _ -> copy processor allocationMode matrix
@@ -124,39 +133,46 @@ module Matrix =
124133
|> toCOO processor allocationMode
125134
|> transposeInplace processor
126135
|> ClMatrix.COO
136+
| ClMatrix.Rows m ->
137+
rowsToCOO processor allocationMode m
138+
|> ClMatrix.COO
127139

128140
/// <summary>
129141
/// Returns the matrix, represented in COO format, that is equal to the given one.
130142
/// The given matrix should neither be used afterwards nor be disposed.
131143
/// </summary>
132144
///<param name="clContext">OpenCL context.</param>
133145
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
134-
let toCOOInplace (clContext: ClContext) workGroupSize =
135-
let toCOOInplace =
146+
let toCOOInPlace (clContext: ClContext) workGroupSize =
147+
let toCOOInPlace =
136148
CSR.Matrix.toCOOInplace clContext workGroupSize
137149

138-
let transposeInplace =
150+
let transposeInPlace =
139151
COO.Matrix.transposeInplace clContext workGroupSize
140152

153+
let rowsToCOO =
154+
Rows.Matrix.toCOO clContext workGroupSize
155+
141156
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix<'a>) ->
142157
match matrix with
143158
| ClMatrix.COO _ -> matrix
144159
| ClMatrix.CSR m ->
145-
toCOOInplace processor allocationMode m
160+
toCOOInPlace processor allocationMode m
146161
|> ClMatrix.COO
147162
| ClMatrix.CSC m ->
148163
m.ToCSR
149-
|> toCOOInplace processor allocationMode
150-
|> transposeInplace processor
164+
|> toCOOInPlace processor allocationMode
165+
|> transposeInPlace processor
151166
|> ClMatrix.COO
167+
| _ -> failwith "not yet supported"
152168

153169
/// <summary>
154170
/// Creates a new matrix, represented in CSC format, that is equal to the given one.
155171
/// </summary>
156172
///<param name="clContext">OpenCL context.</param>
157173
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
158174
let toCSC (clContext: ClContext) workGroupSize =
159-
let toCSR = COO.Matrix.toCSR clContext workGroupSize
175+
let COOtoCSR = COO.Matrix.toCSR clContext workGroupSize
160176

161177
let copy = copy clContext workGroupSize
162178

@@ -174,9 +190,10 @@ module Matrix =
174190
|> ClMatrix.CSC
175191
| ClMatrix.COO m ->
176192
(transposeCOO processor allocationMode m
177-
|> toCSR processor allocationMode)
193+
|> COOtoCSR processor allocationMode)
178194
.ToCSC
179195
|> ClMatrix.CSC
196+
| _ -> failwith "not yet supported"
180197

181198
/// <summary>
182199
/// Returns the matrix, represented in CSC format, that is equal to the given one.
@@ -206,6 +223,7 @@ module Matrix =
206223
|> toCSRInplace processor allocationMode)
207224
.ToCSC
208225
|> ClMatrix.CSC
226+
| _ -> failwith "not yet supported"
209227

210228
let map (clContext: ClContext) (opAdd: Expr<'a option -> 'b option>) workGroupSize =
211229
let mapCOO =
@@ -271,7 +289,7 @@ module Matrix =
271289
let CSRElementwise =
272290
CSR.Matrix.map2AtLeastOneToCOO clContext opAdd workGroupSize
273291

274-
let transposeCOOInplace =
292+
let transposeCOOInPlace =
275293
COO.Matrix.transposeInplace clContext workGroupSize
276294

277295
fun (processor: MailboxProcessor<_>) allocationMode matrix1 matrix2 ->
@@ -284,7 +302,7 @@ module Matrix =
284302
|> ClMatrix.COO
285303
| ClMatrix.CSC m1, ClMatrix.CSC m2 ->
286304
CSRElementwise processor allocationMode m1.ToCSR m2.ToCSR
287-
|> transposeCOOInplace processor
305+
|> transposeCOOInPlace processor
288306
|> ClMatrix.COO
289307
| _ -> failwith "Matrix formats are not matching"
290308

@@ -301,15 +319,16 @@ module Matrix =
301319
/// </remarks>
302320
///<param name="clContext">OpenCL context.</param>
303321
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
304-
let transposeInplace (clContext: ClContext) workGroupSize =
305-
let COOtransposeInplace =
322+
let transposeInPlace (clContext: ClContext) workGroupSize =
323+
let COOTransposeInPlace =
306324
COO.Matrix.transposeInplace clContext workGroupSize
307325

308326
fun (processor: MailboxProcessor<_>) matrix ->
309327
match matrix with
310-
| ClMatrix.COO m -> COOtransposeInplace processor m |> ClMatrix.COO
328+
| ClMatrix.COO m -> COOTransposeInPlace processor m |> ClMatrix.COO
311329
| ClMatrix.CSR m -> ClMatrix.CSC m.ToCSC
312330
| ClMatrix.CSC m -> ClMatrix.CSR m.ToCSR
331+
| ClMatrix.Rows _ -> failwith "not yet supported"
313332

314333
/// <summary>
315334
/// Transposes the given matrix and returns result as a new matrix.
@@ -352,6 +371,7 @@ module Matrix =
352371
Columns = copy processor allocationMode m.Rows
353372
Values = copyData processor allocationMode m.Values }
354373
|> ClMatrix.CSR
374+
| ClMatrix.Rows _ -> failwith "not yet supported"
355375

356376
let mxm
357377
(opAdd: Expr<'c -> 'c -> 'c option>)

0 commit comments

Comments
 (0)