Skip to content

Commit 7885e8a

Browse files
authored
Merge pull request #68 from artemiipatov/map
Matrix.map
2 parents abdb353 + 4e54346 commit 7885e8a

File tree

13 files changed

+632
-24
lines changed

13 files changed

+632
-24
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@
3838
<Compile Include="Matrix/Common.fs" />
3939
<Compile Include="Matrix/COOMatrix/Map2.fs" />
4040
<Compile Include="Matrix/COOMatrix/Map2AtLeastOne.fs" />
41+
<Compile Include="Matrix/COOMatrix/Map.fs" />
4142
<Compile Include="Matrix/COOMatrix/Matrix.fs" />
43+
<Compile Include="Matrix/CSRMatrix/Map2.fs" />
4244
<Compile Include="Matrix/CSRMatrix/Map2AtLeastOne.fs" />
4345
<Compile Include="Matrix/CSRMatrix/SpGEMM.fs" />
46+
<Compile Include="Matrix/CSRMatrix/Map.fs" />
4447
<Compile Include="Matrix/CSRMatrix/Matrix.fs" />
4548
<Compile Include="Matrix/Matrix.fs" />
4649
<Compile Include="Vector/SparseVector/Common.fs" />
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
namespace GraphBLAS.FSharp.Backend.Matrix.COO
2+
3+
open System
4+
open Brahma.FSharp
5+
open GraphBLAS.FSharp.Backend.Matrix
6+
open GraphBLAS.FSharp.Backend.Quotes
7+
open Microsoft.FSharp.Quotations
8+
open GraphBLAS.FSharp.Backend.Objects
9+
open GraphBLAS.FSharp.Backend
10+
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
11+
open GraphBLAS.FSharp.Backend.Objects.ClContext
12+
13+
module internal Map =
14+
let preparePositions<'a, 'b> (clContext: ClContext) workGroupSize opAdd =
15+
16+
let preparePositions (op: Expr<'a option -> 'b option>) =
17+
<@ fun (ndRange: Range1D) rowCount columnCount valuesLength (values: ClArray<'a>) (rows: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'b>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->
18+
19+
let gid = ndRange.GlobalID0
20+
21+
if gid < rowCount * columnCount then
22+
23+
let columnIndex = gid % columnCount
24+
let rowIndex = gid / columnCount
25+
26+
let index =
27+
(uint64 rowIndex <<< 32) ||| (uint64 columnIndex)
28+
29+
let value =
30+
(%Search.Bin.byKey2D) valuesLength index rows columns values
31+
32+
match (%op) value with
33+
| Some resultValue ->
34+
resultValues.[gid] <- resultValue
35+
resultRows.[gid] <- rowIndex
36+
resultColumns.[gid] <- columnIndex
37+
38+
resultBitmap.[gid] <- 1
39+
| None -> resultBitmap.[gid] <- 0 @>
40+
41+
let kernel =
42+
clContext.Compile <| preparePositions opAdd
43+
44+
fun (processor: MailboxProcessor<_>) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) ->
45+
46+
let (resultLength: int) = columnCount * rowCount
47+
48+
let resultBitmap =
49+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
50+
51+
let resultRows =
52+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
53+
54+
let resultColumns =
55+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
56+
57+
let resultValues =
58+
clContext.CreateClArrayWithSpecificAllocationMode<'b>(DeviceOnly, resultLength)
59+
60+
let ndRange =
61+
Range1D.CreateValid(resultLength, workGroupSize)
62+
63+
let kernel = kernel.GetKernel()
64+
65+
processor.Post(
66+
Msg.MsgSetArguments
67+
(fun () ->
68+
kernel.KernelFunc
69+
ndRange
70+
rowCount
71+
columnCount
72+
values.Length
73+
values
74+
rowPointers
75+
columns
76+
resultBitmap
77+
resultValues
78+
resultRows
79+
resultColumns)
80+
)
81+
82+
processor.Post(Msg.CreateRunMsg<_, _> kernel)
83+
84+
resultBitmap, resultValues, resultRows, resultColumns
85+
86+
let run<'a, 'b when 'a: struct and 'b: struct and 'b: equality>
87+
(clContext: ClContext)
88+
(opAdd: Expr<'a option -> 'b option>)
89+
workGroupSize
90+
=
91+
92+
let map =
93+
preparePositions clContext workGroupSize opAdd
94+
95+
let setPositions =
96+
Common.setPositions<'b> clContext workGroupSize
97+
98+
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.COO<'a>) ->
99+
100+
let bitmap, values, rows, columns =
101+
map queue matrix.RowCount matrix.ColumnCount matrix.Values matrix.Rows matrix.Columns
102+
103+
let resultRows, resultColumns, resultValues, _ =
104+
setPositions queue allocationMode rows columns values bitmap
105+
106+
queue.Post(Msg.CreateFreeMsg<_>(bitmap))
107+
queue.Post(Msg.CreateFreeMsg<_>(values))
108+
queue.Post(Msg.CreateFreeMsg<_>(rows))
109+
queue.Post(Msg.CreateFreeMsg<_>(columns))
110+
111+
{ Context = clContext
112+
RowCount = matrix.RowCount
113+
ColumnCount = matrix.ColumnCount
114+
Rows = resultRows
115+
Columns = resultColumns
116+
Values = resultValues }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ open GraphBLAS.FSharp.Backend.Matrix
55
open Microsoft.FSharp.Quotations
66
open GraphBLAS.FSharp.Backend.Objects
77
open GraphBLAS.FSharp.Backend
8+
open GraphBLAS.FSharp.Backend.Quotes
89
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
910
open GraphBLAS.FSharp.Backend.Objects.ClContext
1011
open GraphBLAS.FSharp.Backend.Quotes

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ open GraphBLAS.FSharp.Backend.Objects
88
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
99

1010
module Matrix =
11+
let map = Map.run
12+
1113
let map2 = Map2.run
1214

1315
///<param name="clContext">.</param>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
namespace GraphBLAS.FSharp.Backend.Matrix.CSR
2+
3+
open Brahma.FSharp
4+
open FSharp.Quotations
5+
open GraphBLAS.FSharp.Backend
6+
open GraphBLAS.FSharp.Backend.Quotes
7+
open GraphBLAS.FSharp.Backend.Matrix
8+
open GraphBLAS.FSharp.Backend.Matrix.COO
9+
open GraphBLAS.FSharp.Backend.Objects
10+
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
11+
open GraphBLAS.FSharp.Backend.Objects.ClContext
12+
13+
module internal Map =
14+
let preparePositions<'a, 'b> (clContext: ClContext) workGroupSize op =
15+
16+
let preparePositions (op: Expr<'a option -> 'b option>) =
17+
<@ fun (ndRange: Range1D) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'b>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) ->
18+
19+
let gid = ndRange.GlobalID0
20+
21+
if gid < rowCount * columnCount then
22+
23+
let columnIndex = gid % columnCount
24+
let rowIndex = gid / columnCount
25+
26+
let startIndex = rowPointers.[rowIndex]
27+
let lastIndex = rowPointers.[rowIndex + 1] - 1
28+
29+
let value =
30+
(%Search.Bin.inRange) startIndex lastIndex columnIndex columns values
31+
32+
match (%op) value with
33+
| Some resultValue ->
34+
resultValues.[gid] <- resultValue
35+
resultRows.[gid] <- rowIndex
36+
resultColumns.[gid] <- columnIndex
37+
38+
resultBitmap.[gid] <- 1
39+
| None -> resultBitmap.[gid] <- 0 @>
40+
41+
let kernel = clContext.Compile <| preparePositions op
42+
43+
fun (processor: MailboxProcessor<_>) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) ->
44+
45+
let (resultLength: int) = columnCount * rowCount
46+
47+
let resultBitmap =
48+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
49+
50+
let resultRows =
51+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
52+
53+
let resultColumns =
54+
clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength)
55+
56+
let resultValues =
57+
clContext.CreateClArrayWithSpecificAllocationMode<'b>(DeviceOnly, resultLength)
58+
59+
let ndRange =
60+
Range1D.CreateValid(resultLength, workGroupSize)
61+
62+
let kernel = kernel.GetKernel()
63+
64+
processor.Post(
65+
Msg.MsgSetArguments
66+
(fun () ->
67+
kernel.KernelFunc
68+
ndRange
69+
rowCount
70+
columnCount
71+
values
72+
rowPointers
73+
columns
74+
resultBitmap
75+
resultValues
76+
resultRows
77+
resultColumns)
78+
)
79+
80+
processor.Post(Msg.CreateRunMsg<_, _> kernel)
81+
82+
resultBitmap, resultValues, resultRows, resultColumns
83+
84+
let runToCOO<'a, 'b when 'a: struct and 'b: struct and 'b: equality>
85+
(clContext: ClContext)
86+
(opAdd: Expr<'a option -> 'b option>)
87+
workGroupSize
88+
=
89+
90+
let map =
91+
preparePositions clContext workGroupSize opAdd
92+
93+
let setPositions =
94+
Common.setPositions<'b> clContext workGroupSize
95+
96+
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
97+
98+
let bitmap, values, rows, columns =
99+
map queue matrix.RowCount matrix.ColumnCount matrix.Values matrix.RowPointers matrix.Columns
100+
101+
let resultRows, resultColumns, resultValues, _ =
102+
setPositions queue allocationMode rows columns values bitmap
103+
104+
queue.Post(Msg.CreateFreeMsg<_>(bitmap))
105+
queue.Post(Msg.CreateFreeMsg<_>(values))
106+
queue.Post(Msg.CreateFreeMsg<_>(rows))
107+
queue.Post(Msg.CreateFreeMsg<_>(columns))
108+
109+
{ Context = clContext
110+
RowCount = matrix.RowCount
111+
ColumnCount = matrix.ColumnCount
112+
Rows = resultRows
113+
Columns = resultColumns
114+
Values = resultValues }
115+
116+
let run<'a, 'b when 'a: struct and 'b: struct and 'b: equality>
117+
(clContext: ClContext)
118+
(opAdd: Expr<'a option -> 'b option>)
119+
workGroupSize
120+
=
121+
122+
let mapToCOO = runToCOO clContext opAdd workGroupSize
123+
124+
let toCSRInplace =
125+
Matrix.toCSRInplace clContext workGroupSize
126+
127+
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
128+
mapToCOO queue allocationMode matrix
129+
|> toCSRInplace queue allocationMode

0 commit comments

Comments
 (0)