Skip to content

Commit 91820fe

Browse files
committed
Toolbox changed to internal module, shared methods moved to toolbox, operator if then else formatted
1 parent 4c8ee4a commit 91820fe

2 files changed

Lines changed: 66 additions & 117 deletions

File tree

src/GraphBLAS-sharp/Implementations.fs

Lines changed: 31 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
173173

174174
//It is useful to consider that the first array is longer than the second one
175175
let firstRows, firstColumns, firstValues, secondRows, secondColumns, secondValues, plus =
176-
if this.Rows.Length > matrix.Rows.Length
177-
then this.Rows, this.Columns, this.Values, matrix.Rows, matrix.Columns, matrix.Values, append
178-
else matrix.Rows, matrix.Columns, matrix.Values, this.Rows, this.Columns, this.Values, <@ fun x y -> (%append) y x @>
176+
if this.Rows.Length > matrix.Rows.Length then
177+
this.Rows, this.Columns, this.Values, matrix.Rows, matrix.Columns, matrix.Values, append
178+
else
179+
matrix.Rows, matrix.Columns, matrix.Values, this.Rows, this.Columns, this.Values, <@ fun x y -> (%append) y x @>
179180

180181
let filterThroughMask =
181182
opencl {
@@ -205,38 +206,25 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
205206

206207
let i = ndRange.GlobalID0
207208

208-
let f n =
209-
if 0 > n + 1 - shortSide
210-
then 0
211-
else n + 1 - shortSide
209+
let f n = if 0 > n + 1 - shortSide then 0 else n + 1 - shortSide
212210
let mutable leftEdge = f i
213-
// if 0 > i + 1 - shortSide
214-
// then 0
215-
// else i + 1 - shortSide
216-
217-
let g n =
218-
if n > longSide - 1
219-
then longSide - 1
220-
else n
211+
// if 0 > i + 1 - shortSide then 0 else i + 1 - shortSide
212+
213+
let g n = if n > longSide - 1 then longSide - 1 else n
221214
let mutable rightEdge = g i
222-
// if i > longSide - 1
223-
// then longSide - 1
224-
// else i
215+
// if i > longSide - 1 then longSide - 1 else i
225216

226217
while leftEdge <= rightEdge do
227218
let middleIdx = (leftEdge + rightEdge) / 2
228219
let firstRow, firstColumn = firstRowsBuffer.[middleIdx], firstColumnsBuffer.[middleIdx]
229220
let secondRow, secondColumn = secondRowsBuffer.[i - middleIdx], secondColumnsBuffer.[i - middleIdx]
230-
if firstRow < secondRow || firstRow = secondRow && firstColumn < secondColumn
231-
then leftEdge <- middleIdx + 1
232-
else rightEdge <- middleIdx - 1
221+
if firstRow < secondRow || firstRow = secondRow && firstColumn < secondColumn then leftEdge <- middleIdx + 1 else rightEdge <- middleIdx - 1
233222

234223
let boundaryX, boundaryY = rightEdge, i - leftEdge
235224
let firstRow, firstColumn = firstRowsBuffer.[boundaryX], firstColumnsBuffer.[boundaryX]
236225
let secondRow, secondColumn = secondRowsBuffer.[boundaryY], secondColumnsBuffer.[boundaryY]
237226

238-
if boundaryX < 0 || boundaryY >= 0 && (firstRow < secondRow || firstRow = secondRow && firstColumn < secondColumn)
239-
then
227+
if boundaryX < 0 || boundaryY >= 0 && (firstRow < secondRow || firstRow = secondRow && firstColumn < secondColumn) then
240228
allRowsBuffer.[i] <- secondRowsBuffer.[boundaryY]
241229
allColumnsBuffer.[i] <- secondColumnsBuffer.[boundaryY]
242230
allValuesBuffer.[i] <- secondValuesBuffer.[boundaryY]
@@ -276,8 +264,7 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
276264

277265
let i = ndRange.GlobalID0
278266

279-
if allRowsBuffer.[i] = allRowsBuffer.[i + 1] && allColumnsBuffer.[i] = allColumnsBuffer.[i + 1]
280-
then
267+
if allRowsBuffer.[i] = allRowsBuffer.[i + 1] && allColumnsBuffer.[i] = allColumnsBuffer.[i + 1] then
281268
auxiliaryArrayBuffer.[i + 1] <- 0
282269
allValuesBuffer.[i] <- (%plus) allValuesBuffer.[i] allValuesBuffer.[i + 1]
283270
@>
@@ -295,29 +282,6 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
295282
do! RunCommand fillAuxiliaryArray binder
296283
}
297284

298-
let dropExplicitZeroes =
299-
<@
300-
fun (ndRange: _1D)
301-
(allValuesBuffer: 'a[])
302-
(auxiliaryArrayBuffer: int[]) ->
303-
304-
let i = ndRange.GlobalID0
305-
306-
if allValuesBuffer.[i] = zero
307-
then auxiliaryArrayBuffer.[i] <- 0
308-
@>
309-
310-
let dropExplicitZeroes =
311-
opencl {
312-
let binder kernelP =
313-
let ndRange = _1D(allValues.Length)
314-
kernelP
315-
ndRange
316-
allValues
317-
auxiliaryArray
318-
do! RunCommand dropExplicitZeroes binder
319-
}
320-
321285
let createUnion =
322286
<@
323287
fun (ndRange: _1D)
@@ -332,8 +296,7 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
332296

333297
let i = ndRange.GlobalID0
334298

335-
if auxiliaryArrayBuffer.[i] = 1
336-
then
299+
if auxiliaryArrayBuffer.[i] = 1 then
337300
let index = prefixSumArrayBuffer.[i] - 1
338301

339302
resultRowsBuffer.[index] <- allRowsBuffer.[i]
@@ -347,7 +310,7 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
347310

348311
let createUnion =
349312
opencl {
350-
let! prefixSumArray = Toolbox.PrefixSum auxiliaryArray
313+
let! prefixSumArray = Toolbox.prefixSum auxiliaryArray
351314
let binder kernelP =
352315
let ndRange = _1D(auxiliaryArray.Length)
353316
kernelP
@@ -367,7 +330,7 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
367330
do! createSortedConcatenation
368331
do! filterThroughMask
369332
do! fillAuxiliaryArray
370-
do! dropExplicitZeroes
333+
do! Toolbox.EWiseAdd.dropExplicitZeroes zero allValues auxiliaryArray
371334
do! createUnion
372335

373336
return upcast COOMatrix<'a>(this.RowCount, this.ColumnCount, resultRows, resultColumns, resultValues)
@@ -455,9 +418,10 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
455418

456419
//It is useful to consider that the first array is longer than the second one
457420
let firstIndices, firstValues, secondIndices, secondValues, plus =
458-
if this.Indices.Length > vector.Indices.Length
459-
then this.Indices, this.Values, vector.Indices, vector.Values, append
460-
else vector.Indices, vector.Values, this.Indices, this.Values, <@ fun x y -> (%append) y x @>
421+
if this.Indices.Length > vector.Indices.Length then
422+
this.Indices, this.Values, vector.Indices, vector.Values, append
423+
else
424+
vector.Indices, vector.Values, this.Indices, this.Values, <@ fun x y -> (%append) y x @>
461425

462426
let filterThroughMask =
463427
opencl {
@@ -483,34 +447,21 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
483447

484448
let i = ndRange.GlobalID0
485449

486-
let f n =
487-
if 0 > n + 1 - shortSide
488-
then 0
489-
else n + 1 - shortSide
450+
let f n = if 0 > n + 1 - shortSide then 0 else n + 1 - shortSide
490451
let mutable leftEdge = f i
491-
// if 0 > i + 1 - shortSide
492-
// then 0
493-
// else i + 1 - shortSide
494-
495-
let g n =
496-
if n > longSide - 1
497-
then longSide - 1
498-
else n
452+
// if 0 > i + 1 - shortSide then 0 else i + 1 - shortSide
453+
454+
let g n = if n > longSide - 1 then longSide - 1 else n
499455
let mutable rightEdge = g i
500-
// if i > longSide - 1
501-
// then longSide - 1
502-
// else i
456+
// if i > longSide - 1 then longSide - 1 else i
503457

504458
while leftEdge <= rightEdge do
505459
let middleIdx = (leftEdge + rightEdge) / 2
506-
if firstIndicesBuffer.[middleIdx] < secondIndicesBuffer.[i - middleIdx]
507-
then leftEdge <- middleIdx + 1
508-
else rightEdge <- middleIdx - 1
460+
if firstIndicesBuffer.[middleIdx] < secondIndicesBuffer.[i - middleIdx] then leftEdge <- middleIdx + 1 else rightEdge <- middleIdx - 1
509461

510462
let boundaryX, boundaryY = rightEdge, i - leftEdge
511463

512-
if boundaryX < 0 || boundaryY >= 0 && firstIndicesBuffer.[boundaryX] < secondIndicesBuffer.[boundaryY]
513-
then
464+
if boundaryX < 0 || boundaryY >= 0 && firstIndicesBuffer.[boundaryX] < secondIndicesBuffer.[boundaryY] then
514465
allIndicesBuffer.[i] <- secondIndicesBuffer.[boundaryY]
515466
allValuesBuffer.[i] <- secondValuesBuffer.[boundaryY]
516467
else
@@ -544,8 +495,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
544495

545496
let i = ndRange.GlobalID0
546497

547-
if allIndicesBuffer.[i] = allIndicesBuffer.[i + 1]
548-
then
498+
if allIndicesBuffer.[i] = allIndicesBuffer.[i + 1] then
549499
auxiliaryArrayBuffer.[i + 1] <- 0
550500
allValuesBuffer.[i] <- (%plus) allValuesBuffer.[i] allValuesBuffer.[i + 1]
551501
@>
@@ -562,29 +512,6 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
562512
do! RunCommand fillAuxiliaryArray binder
563513
}
564514

565-
let dropExplicitZeroes =
566-
<@
567-
fun (ndRange: _1D)
568-
(allValuesBuffer: 'a[])
569-
(auxiliaryArrayBuffer: int[]) ->
570-
571-
let i = ndRange.GlobalID0
572-
573-
if allValuesBuffer.[i] = zero
574-
then auxiliaryArrayBuffer.[i] <- 0
575-
@>
576-
577-
let dropExplicitZeroes =
578-
opencl {
579-
let binder kernelP =
580-
let ndRange = _1D(allValues.Length)
581-
kernelP
582-
ndRange
583-
allValues
584-
auxiliaryArray
585-
do! RunCommand dropExplicitZeroes binder
586-
}
587-
588515
let createUnion =
589516
<@
590517
fun (ndRange: _1D)
@@ -597,8 +524,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
597524

598525
let i = ndRange.GlobalID0
599526

600-
if auxiliaryArrayBuffer.[i] = 1
601-
then
527+
if auxiliaryArrayBuffer.[i] = 1 then
602528
let index = prefixSumArrayBuffer.[i] - 1
603529

604530
resultIndicesBuffer.[index] <- allIndicesBuffer.[i]
@@ -610,7 +536,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
610536

611537
let createUnion =
612538
opencl {
613-
let! prefixSumArray = Toolbox.PrefixSum auxiliaryArray
539+
let! prefixSumArray = Toolbox.prefixSum auxiliaryArray
614540
let binder kernelP =
615541
let ndRange = _1D(auxiliaryArray.Length)
616542
kernelP
@@ -628,7 +554,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
628554
do! createSortedConcatenation
629555
do! filterThroughMask
630556
do! fillAuxiliaryArray
631-
do! dropExplicitZeroes
557+
do! Toolbox.EWiseAdd.dropExplicitZeroes zero allValues auxiliaryArray
632558
do! createUnion
633559

634560
return upcast SparseVector<'a>(this.Size, resultIndices, resultValues)
@@ -639,8 +565,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
639565
(mask: Mask1D option)
640566
(semiring: Semiring<'a>) =
641567

642-
if vector.Size <> this.Size
643-
then
568+
if vector.Size <> this.Size then
644569
invalidArg
645570
"vector"
646571
(sprintf "Argument has invalid dimension. Need %i, but given %i" this.Size vector.Size)

src/GraphBLAS-sharp/Toolbox.fs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ open FSharp.Quotations.Evaluator
99
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
1010
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
1111

12-
type Toolbox =
13-
static member internal PrefixSum
12+
module internal Toolbox =
13+
let rec internal prefixSum
1414
(inputArray: int[]) =
1515

1616
let outputArray = Array.zeroCreate inputArray.Length
1717

18-
if inputArray.Length = 1
19-
then
18+
if inputArray.Length = 1 then
2019
let fillOutputArray =
2120
<@
2221
fun (ndRange: _1D)
@@ -48,8 +47,8 @@ type Toolbox =
4847
(intermediateArrayBuffer: int[]) ->
4948

5049
let i = ndRange.GlobalID0
51-
if 2 * i + 1 < inputArrayLength
52-
then intermediateArrayBuffer.[i] <- inputArrayBuffer.[2 * i] + inputArrayBuffer.[2 * i + 1]
50+
if 2 * i + 1 < inputArrayLength then
51+
intermediateArrayBuffer.[i] <- inputArrayBuffer.[2 * i] + inputArrayBuffer.[2 * i + 1]
5352
else intermediateArrayBuffer.[i] <- inputArrayBuffer.[2 * i]
5453
@>
5554

@@ -73,17 +72,15 @@ type Toolbox =
7372

7473
let i = ndRange.GlobalID0
7574
let j = (i - 1) / 2
76-
if i % 2 = 0
77-
then
78-
if i = 0
79-
then outputArrayBuffer.[i] <- inputArrayBuffer.[i]
75+
if i % 2 = 0 then
76+
if i = 0 then outputArrayBuffer.[i] <- inputArrayBuffer.[i]
8077
else outputArrayBuffer.[i] <- auxiliaryPrefixSumArrayBuffer.[j] + inputArrayBuffer.[i]
8178
else outputArrayBuffer.[i] <- auxiliaryPrefixSumArrayBuffer.[j]
8279
@>
8380

8481
opencl {
8582
do! fillIntermediateArray
86-
let! auxiliaryPrefixSumArray = Toolbox.PrefixSum intermediateArray
83+
let! auxiliaryPrefixSumArray = prefixSum intermediateArray
8784

8885
let binder kernelP =
8986
let ndRange = _1D(inputArray.Length)
@@ -96,3 +93,30 @@ type Toolbox =
9693

9794
return outputArray
9895
}
96+
97+
module internal EWiseAdd =
98+
let internal dropExplicitZeroes
99+
(zero: 'a)
100+
(allValues: 'a[])
101+
(auxiliaryArray: int[]) =
102+
103+
let command =
104+
<@
105+
fun (ndRange: _1D)
106+
(allValuesBuffer: 'a[])
107+
(auxiliaryArrayBuffer: int[]) ->
108+
109+
let i = ndRange.GlobalID0
110+
if allValuesBuffer.[i] = zero then auxiliaryArrayBuffer.[i] <- 0
111+
@>
112+
113+
let binder kernelP =
114+
let ndRange = _1D(allValues.Length)
115+
kernelP
116+
ndRange
117+
allValues
118+
auxiliaryArray
119+
120+
opencl {
121+
do! RunCommand command binder
122+
}

0 commit comments

Comments
 (0)