Skip to content

Commit 91513e1

Browse files
committed
Option.map
1 parent 808ea01 commit 91513e1

3 files changed

Lines changed: 53 additions & 58 deletions

File tree

src/GraphBLAS-sharp.Backend/Vector/SpMSpV.fs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -236,36 +236,40 @@ module SpMSpV =
236236
Reduce.ByKey.Option.segmentSequential add clContext workGroupSize
237237

238238
fun (queue: MailboxProcessor<_>) (matrix: ClMatrix.CSR<'a>) (vector: ClVector.Sparse<'b>) ->
239+
let result =
240+
gather queue matrix vector
241+
|> Option.map
242+
(fun (gatherRows, gatherIndices, gatherValues) ->
243+
sort queue gatherIndices gatherRows gatherValues
239244

240-
match gather queue matrix vector with
241-
| None -> None
242-
| Some (gatherRows, gatherIndices, gatherValues) ->
243-
sort queue gatherIndices gatherRows gatherValues
245+
let sortedRows, sortedIndices, sortedValues = gatherRows, gatherIndices, gatherValues
244246

245-
let sortedRows, sortedIndices, sortedValues = gatherRows, gatherIndices, gatherValues
247+
let multipliedValues =
248+
multiplyScalar queue sortedRows sortedValues vector
246249

247-
let multipliedValues =
248-
multiplyScalar queue sortedRows sortedValues vector
250+
sortedValues.Free queue
251+
sortedRows.Free queue
249252

250-
sortedValues.Free queue
251-
sortedRows.Free queue
253+
let result =
254+
segReduce queue DeviceOnly sortedIndices multipliedValues
255+
|> Option.map
256+
(fun (reducedValues, reducedKeys) ->
252257

253-
match segReduce queue DeviceOnly sortedIndices multipliedValues with
254-
| Some (reducedValues, reducedKeys) ->
255-
multipliedValues.Free queue
256-
sortedIndices.Free queue
258+
{ Context = clContext
259+
Indices = reducedKeys
260+
Values = reducedValues
261+
Size = matrix.ColumnCount })
257262

258-
Some(
259-
{ Context = clContext
260-
Indices = reducedKeys
261-
Values = reducedValues
262-
Size = matrix.ColumnCount }
263-
)
264-
| None ->
265-
multipliedValues.Free queue
266-
sortedIndices.Free queue
263+
multipliedValues.Free queue
264+
sortedIndices.Free queue
265+
266+
result)
267+
268+
//Unwrap 't option option to 't option
269+
match result with
270+
| Some result -> result
271+
| None -> None
267272

268-
None
269273

270274
let runBoolStandard
271275
(add: Expr<'c option -> 'c option -> 'c option>)
@@ -286,22 +290,20 @@ module SpMSpV =
286290

287291
fun (queue: MailboxProcessor<_>) (matrix: ClMatrix.CSR<'a>) (vector: ClVector.Sparse<'b>) ->
288292

289-
match gather queue matrix vector with
290-
| None -> None
291-
| Some (gatherRows, gatherIndices, gatherValues) ->
292-
gatherRows.Free queue
293-
gatherValues.Free queue
293+
gather queue matrix vector
294+
|> Option.map
295+
(fun (gatherRows, gatherIndices, gatherValues) ->
296+
gatherRows.Free queue
297+
gatherValues.Free queue
294298

295-
let sortedIndices = sort queue gatherIndices
299+
let sortedIndices = sort queue gatherIndices
296300

297-
let resultIndices = removeDuplicates queue sortedIndices
301+
let resultIndices = removeDuplicates queue sortedIndices
298302

299-
gatherIndices.Free queue
300-
sortedIndices.Free queue
303+
gatherIndices.Free queue
304+
sortedIndices.Free queue
301305

302-
Some(
303306
{ Context = clContext
304307
Indices = resultIndices
305308
Values = create queue DeviceOnly resultIndices.Length true
306-
Size = matrix.ColumnCount }
307-
)
309+
Size = matrix.ColumnCount })

src/GraphBLAS-sharp.Backend/Vector/Sparse/Map2.fs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open Microsoft.FSharp.Control
66
open GraphBLAS.FSharp.Backend.Objects
77
open GraphBLAS.FSharp.Backend.Objects.ClVector
88
open GraphBLAS.FSharp.Backend.Objects.ClContext
9+
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
910
open GraphBLAS.FSharp.Backend.Quotes
1011

1112
module internal Map2 =
@@ -180,24 +181,20 @@ module internal Map2 =
180181
let bitmap, allValues, allIndices =
181182
prepare processor leftVector.NNZ leftVector.Values leftVector.Indices rightVector
182183

183-
match setPositions processor allocationMode allValues allIndices bitmap with
184-
| Some (resultValues, resultIndices) ->
185-
186-
processor.Post(Msg.CreateFreeMsg<_>(allIndices))
187-
processor.Post(Msg.CreateFreeMsg<_>(allValues))
188-
processor.Post(Msg.CreateFreeMsg<_>(bitmap))
189-
190-
Some(
191-
{ Context = clContext
192-
Values = resultValues
193-
Indices = resultIndices
194-
Size = leftVector.Size }
195-
)
196-
| None ->
197-
processor.Post(Msg.CreateFreeMsg<_>(allIndices))
198-
processor.Post(Msg.CreateFreeMsg<_>(allValues))
199-
processor.Post(Msg.CreateFreeMsg<_>(bitmap))
200-
None
184+
let result =
185+
setPositions processor allocationMode allValues allIndices bitmap
186+
|> Option.map
187+
(fun (resultValues, resultIndices) ->
188+
{ Context = clContext
189+
Values = resultValues
190+
Indices = resultIndices
191+
Size = leftVector.Size })
192+
193+
allIndices.Free processor
194+
allValues.Free processor
195+
bitmap.Free processor
196+
197+
result
201198

202199
let private preparePositionsAssignByMask<'a, 'b when 'a: struct and 'b: struct>
203200
op

tests/GraphBLAS-sharp.Tests/Backend/Algorithms/SSSP.fs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ let testFixtures (testContext: TestContext) =
3939

4040
let expected =
4141
SSSP.runUndirected matrix (directedFromArray2D matrix 0) source
42-
|> Array.map
43-
(fun x ->
44-
match x with
45-
| Some x -> Some(int x)
46-
| None -> None)
42+
|> Array.map (Option.map int)
4743

4844
let matrixHost =
4945
Utils.createMatrixFromArray2D CSR matrix ((=) 0)

0 commit comments

Comments
 (0)